blob: 6dc03bab0c9d1b51c3f8572d046c8ac4b49560c3 [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>
David Sterba55e20bd2020-06-09 19:56:06 +02009#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040010#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040011#include <linux/fs.h>
12#include <linux/pagemap.h>
13#include <linux/highmem.h>
14#include <linux/time.h>
15#include <linux/init.h>
16#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040019#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050020#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040021#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040022#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020024#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000025#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050026#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040027#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080028#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040029#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050030#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070031#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080032#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030033#include <linux/sched/mm.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"
Li Zefan581bb052011-04-20 10:06:11 +080048#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000049#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080050#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040051#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040052#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010053#include "space-info.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020056 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040057 struct btrfs_root *root;
58};
59
Filipe Mananaf28a4922015-12-08 19:23:20 +000060struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000061 u64 reserve;
David Sterba55e20bd2020-06-09 19:56:06 +020062 u64 unsubmitted_oe_range_start;
63 u64 unsubmitted_oe_range_end;
64 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000065};
66
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070067static const struct inode_operations btrfs_dir_inode_operations;
68static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_special_inode_operations;
70static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070071static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010073static const struct extent_io_ops btrfs_extent_io_ops;
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/*
99 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100100 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800101 *
102 * NOTE: caller must ensure that when an error happens, it can not call
103 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
104 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
105 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200106 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800107 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300108static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200109 struct page *locked_page,
110 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800111{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900112 unsigned long index = offset >> PAGE_SHIFT;
113 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200114 u64 page_start = page_offset(locked_page);
115 u64 page_end = page_start + PAGE_SIZE - 1;
116
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900117 struct page *page;
118
119 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300120 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900121 index++;
122 if (!page)
123 continue;
124 ClearPagePrivate2(page);
125 put_page(page);
126 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200127
128 /*
129 * In case this page belongs to the delalloc range being instantiated
130 * then skip it, since the first page of a range is going to be
131 * properly cleaned up by the caller of run_delalloc_range
132 */
133 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
134 offset += PAGE_SIZE;
135 bytes -= PAGE_SIZE;
136 }
137
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300138 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800139}
140
Eric Sandeen48a3b632013-04-25 20:41:01 +0000141static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400142
Josef Bacik6a3891c2015-03-16 17:38:52 -0400143#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
144void btrfs_test_inode_set_ops(struct inode *inode)
145{
146 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
147}
148#endif
149
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000150static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500151 struct inode *inode, struct inode *dir,
152 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500153{
154 int err;
155
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000156 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500157 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500158 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500159 return err;
160}
161
Chris Masond352ac62008-09-29 15:18:18 -0400162/*
Chris Masonc8b97812008-10-29 14:49:59 -0400163 * this does all the hard work for inserting an inline extent into
164 * the btree. The caller should have done a btrfs_drop_extents so that
165 * no overlapping inline items exist in the btree
166 */
Chris Mason40f76582014-05-21 13:35:51 -0700167static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000168 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400169 struct btrfs_root *root, struct inode *inode,
170 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000171 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400172 struct page **compressed_pages)
173{
Chris Masonc8b97812008-10-29 14:49:59 -0400174 struct extent_buffer *leaf;
175 struct page *page = NULL;
176 char *kaddr;
177 unsigned long ptr;
178 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400179 int ret;
180 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400181 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400182
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800183 ASSERT((compressed_size > 0 && compressed_pages) ||
184 (compressed_size == 0 && !compressed_pages));
185
Li Zefanfe3f5662011-03-28 08:30:38 +0000186 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400187 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400188
Chris Masonc8b97812008-10-29 14:49:59 -0400189 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000190
191 if (!extent_inserted) {
192 struct btrfs_key key;
193 size_t datasize;
194
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200195 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000196 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200197 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000198
199 datasize = btrfs_file_extent_calc_inline_size(cur_size);
200 path->leave_spinning = 1;
201 ret = btrfs_insert_empty_item(trans, root, path, &key,
202 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200203 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000204 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400205 }
206 leaf = path->nodes[0];
207 ei = btrfs_item_ptr(leaf, path->slots[0],
208 struct btrfs_file_extent_item);
209 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
210 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
211 btrfs_set_file_extent_encryption(leaf, ei, 0);
212 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
213 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
214 ptr = btrfs_file_extent_inline_start(ei);
215
Li Zefan261507a02010-12-17 14:21:50 +0800216 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400217 struct page *cpage;
218 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500219 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400220 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500221 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300222 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Cong Wang7ac687d2011-11-25 23:14:28 +0800224 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400225 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800226 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400227
228 i++;
229 ptr += cur_size;
230 compressed_size -= cur_size;
231 }
232 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800233 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400234 } else {
235 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300236 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800238 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100239 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400240 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800241 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300242 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400243 }
244 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000245 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400246
Yan, Zhengc2167752009-11-12 09:34:21 +0000247 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500248 * We align size to sectorsize for inline extents just for simplicity
249 * sake.
250 */
251 size = ALIGN(size, root->fs_info->sectorsize);
252 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
253 if (ret)
254 goto fail;
255
256 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 * we're an inline extent, so nobody can
258 * extend the file past i_size without locking
259 * a page we already have locked.
260 *
261 * We must do any isize and inode updates
262 * before we unlock the pages. Otherwise we
263 * could end up racing with unlink.
264 */
Chris Masonc8b97812008-10-29 14:49:59 -0400265 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000267
Chris Masonc8b97812008-10-29 14:49:59 -0400268fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200269 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400270}
271
272
273/*
274 * conditionally insert an inline extent into the file. This
275 * does the checks required to make sure the data is small enough
276 * to fit as an inline extent.
277 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300278static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400279 u64 end, size_t compressed_size,
280 int compress_type,
281 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400282{
Nikolay Borisova0349402020-06-03 08:55:12 +0300283 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400284 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400285 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300286 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 actual_end = min(end + 1, isize);
288 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400289 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400290 u64 data_len = inline_len;
291 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000292 struct btrfs_path *path;
293 int extent_inserted = 0;
294 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400295
296 if (compressed_size)
297 data_len = compressed_size;
298
299 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400300 actual_end > fs_info->sectorsize ||
301 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400302 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400303 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400304 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400305 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400306 return 1;
307 }
308
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000309 path = btrfs_alloc_path();
310 if (!path)
311 return -ENOMEM;
312
Josef Bacik00361582013-08-14 14:02:47 -0400313 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000314 if (IS_ERR(trans)) {
315 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400316 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000317 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300318 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400319
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000320 if (compressed_size && compressed_pages)
321 extent_item_size = btrfs_file_extent_calc_inline_size(
322 compressed_size);
323 else
324 extent_item_size = btrfs_file_extent_calc_inline_size(
325 inline_len);
326
Nikolay Borisova0349402020-06-03 08:55:12 +0300327 ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end,
328 NULL, 1, 1, extent_item_size,
329 &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400330 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400331 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400332 goto out;
333 }
Chris Masonc8b97812008-10-29 14:49:59 -0400334
335 if (isize > actual_end)
336 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000337 ret = insert_inline_extent(trans, path, extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300338 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400339 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000340 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400341 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400342 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400343 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400344 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400345 ret = 1;
346 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100347 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400348
Nikolay Borisova0349402020-06-03 08:55:12 +0300349 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
350 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400351out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800352 /*
353 * Don't forget to free the reserved space, as for inlined extent
354 * it won't count as data extent, free them directly here.
355 * And at reserve time, it's always aligned to page size, so
356 * just free one page here.
357 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300358 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000359 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400360 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400361 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400362}
363
Chris Mason771ed682008-11-06 22:02:51 -0500364struct async_extent {
365 u64 start;
366 u64 ram_size;
367 u64 compressed_size;
368 struct page **pages;
369 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800370 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500371 struct list_head list;
372};
373
Nikolay Borisov97db1202019-03-12 17:20:24 +0200374struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500375 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500376 struct page *locked_page;
377 u64 start;
378 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600379 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500380 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700381 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200383 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500384};
385
Nikolay Borisov97db1202019-03-12 17:20:24 +0200386struct async_cow {
387 /* Number of chunks in flight; must be first in the structure */
388 atomic_t num_chunks;
389 struct async_chunk chunks[];
390};
391
392static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500393 u64 start, u64 ram_size,
394 u64 compressed_size,
395 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800396 unsigned long nr_pages,
397 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500398{
399 struct async_extent *async_extent;
400
401 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100402 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500403 async_extent->start = start;
404 async_extent->ram_size = ram_size;
405 async_extent->compressed_size = compressed_size;
406 async_extent->pages = pages;
407 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800408 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500409 list_add_tail(&async_extent->list, &cow->extents);
410 return 0;
411}
412
Qu Wenruo42c16da2019-07-01 05:12:46 +0000413/*
414 * Check if the inode has flags compatible with compression
415 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300416static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000417{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300418 if (inode->flags & BTRFS_INODE_NODATACOW ||
419 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000420 return false;
421 return true;
422}
423
424/*
425 * Check if the inode needs to be submitted to compression, based on mount
426 * options, defragmentation, properties or heuristics.
427 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300428static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
429 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800430{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300431 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800432
Nikolay Borisov808a1292020-06-03 08:55:27 +0300433 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000434 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
435 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300436 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000437 return 0;
438 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800439 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400440 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800441 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200442 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300443 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200444 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800445 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300446 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800447 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400448 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300449 inode->flags & BTRFS_INODE_COMPRESS ||
450 inode->prop_compress)
451 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800452 return 0;
453}
454
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200455static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800456 u64 start, u64 end, u64 num_bytes, u64 small_write)
457{
458 /* If this is a small write inside eof, kick off a defrag */
459 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200460 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800461 btrfs_add_inode_defrag(NULL, inode);
462}
463
Chris Masonc8b97812008-10-29 14:49:59 -0400464/*
Chris Mason771ed682008-11-06 22:02:51 -0500465 * we create compressed extents in two phases. The first
466 * phase compresses a range of pages that have already been
467 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400468 *
Chris Mason771ed682008-11-06 22:02:51 -0500469 * This is done inside an ordered work queue, and the compression
470 * is spread across many cpus. The actual IO submission is step
471 * two, and the ordered work queue takes care of making sure that
472 * happens in the same order things were put onto the queue by
473 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400474 *
Chris Mason771ed682008-11-06 22:02:51 -0500475 * If this code finds it can't get good compression, it puts an
476 * entry onto the work queue to write the uncompressed bytes. This
477 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300478 * are written in the same order that the flusher thread sent them
479 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400480 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300481static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db2b2007-08-27 16:49:44 -0400482{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200483 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400484 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400485 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200486 u64 start = async_chunk->start;
487 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400488 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400489 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400490 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400491 struct page **pages = NULL;
492 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400493 unsigned long total_compressed = 0;
494 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400495 int i;
496 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400497 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300498 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400499 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400500
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200501 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
502 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400503
Josef Bacikd98da492019-10-11 09:03:54 -0400504 /*
505 * We need to save i_size before now because it could change in between
506 * us evaluating the size and assigning it. This is because we lock and
507 * unlock the page in truncate and fallocate, and then modify the i_size
508 * later on.
509 *
510 * The barriers are to emulate READ_ONCE, remove that once i_size_read
511 * does that for us.
512 */
513 barrier();
514 i_size = i_size_read(inode);
515 barrier();
516 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400517again:
518 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300519 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100520 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
521 nr_pages = min_t(unsigned long, nr_pages,
522 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400523
Chris Masonf03d9301f2009-02-04 09:31:06 -0500524 /*
525 * we don't want to send crud past the end of i_size through
526 * compression, that's just a waste of CPU time. So, if the
527 * end of the file is before the start of our current
528 * requested range of bytes, we bail out to the uncompressed
529 * cleanup code that can deal with all of this.
530 *
531 * It isn't really the fastest way to fix things, but this is a
532 * very uncommon corner.
533 */
534 if (actual_end <= start)
535 goto cleanup_and_bail_uncompressed;
536
Chris Masonc8b97812008-10-29 14:49:59 -0400537 total_compressed = actual_end - start;
538
Shilong Wang4bcbb332014-10-07 18:44:35 -0400539 /*
540 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400541 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400542 */
543 if (total_compressed <= blocksize &&
544 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
545 goto cleanup_and_bail_uncompressed;
546
David Sterba069eac72017-02-14 19:36:54 +0100547 total_compressed = min_t(unsigned long, total_compressed,
548 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400549 total_in = 0;
550 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400551
Chris Mason771ed682008-11-06 22:02:51 -0500552 /*
553 * we do compression for mount -o compress and when the
554 * inode has not been flagged as nocompress. This flag can
555 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400556 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300557 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400558 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100559 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800560 if (!pages) {
561 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100562 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800563 goto cont;
564 }
Chris Mason179e29e2007-11-01 11:28:41 -0400565
David Sterbaeec63c62017-07-17 19:41:31 +0200566 if (BTRFS_I(inode)->defrag_compress)
567 compress_type = BTRFS_I(inode)->defrag_compress;
568 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200569 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800570
Chris Mason4adaa612013-03-26 13:07:00 -0400571 /*
572 * we need to call clear_page_dirty_for_io on each
573 * page in the range. Otherwise applications with the file
574 * mmap'd can wander in and change the page contents while
575 * we are compressing them.
576 *
577 * If the compression fails for any reason, we set the pages
578 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300579 *
580 * Note that the remaining part is redirtied, the start pointer
581 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400582 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300583 if (!redirty) {
584 extent_range_clear_dirty_for_io(inode, start, end);
585 redirty = 1;
586 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200587
588 /* Compression level is applied here and only here */
589 ret = btrfs_compress_pages(
590 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800591 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100592 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100593 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800594 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100595 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400596
597 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100598 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100599 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400600 char *kaddr;
601
602 /* zero the tail end of the last page, we might be
603 * sending it down to disk
604 */
605 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800606 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400607 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300608 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800609 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400610 }
611 will_compress = 1;
612 }
613 }
Li Zefan560f7d72011-09-08 10:22:01 +0800614cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400615 if (start == 0) {
616 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300617 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400618 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500619 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400620 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300621 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
622 0, BTRFS_COMPRESS_NONE,
623 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400624 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500625 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300626 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000627 total_compressed,
628 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400629 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100630 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400631 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400632 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
633 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100634 unsigned long page_error_op;
635
Filipe Mananae6eb4312014-10-10 10:45:12 +0100636 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400637
Chris Mason771ed682008-11-06 22:02:51 -0500638 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100639 * inline extent creation worked or returned error,
640 * we don't need to create any more async work items.
641 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400642 *
643 * We use DO_ACCOUNTING here because we need the
644 * delalloc_release_metadata to be done _after_ we drop
645 * our outstanding extent for clearing delalloc for this
646 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500647 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300648 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
649 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300650 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800651 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400652 PAGE_CLEAR_DIRTY |
653 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100654 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400655 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300656
657 for (i = 0; i < nr_pages; i++) {
658 WARN_ON(pages[i]->mapping);
659 put_page(pages[i]);
660 }
661 kfree(pages);
662
663 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400664 }
665 }
666
667 if (will_compress) {
668 /*
669 * we aren't doing an inline extent round the compressed size
670 * up to a block size boundary so the allocator does sane
671 * things
672 */
Qu Wenruofda28322013-02-26 08:10:22 +0000673 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400674
675 /*
676 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300677 * win, compare the page count read with the blocks on disk,
678 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400679 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300680 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300681 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300682 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700683
684 /*
685 * The async work queues will take care of doing actual
686 * allocation on disk for these compressed pages, and
687 * will submit them to the elevator.
688 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200689 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100690 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700691 compress_type);
692
Timofey Titovets11708622017-10-03 18:06:01 +0300693 if (start + total_in < end) {
694 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700695 pages = NULL;
696 cond_resched();
697 goto again;
698 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300699 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400700 }
701 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700702 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400703 /*
704 * the compression code ran but failed to make things smaller,
705 * free any pages it allocated and our page pointer array
706 */
David Sterba4d3a8002017-02-14 19:04:07 +0100707 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400708 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300709 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400710 }
711 kfree(pages);
712 pages = NULL;
713 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100714 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400715
716 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400717 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200718 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500719 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500720 }
Chris Masonc8b97812008-10-29 14:49:59 -0400721 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500722cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700723 /*
724 * No compression, but we still need to write the pages in the file
725 * we've been given so far. redirty the locked page if it corresponds
726 * to our extent and set things up for the async work queue to run
727 * cow_file_range to do the normal delalloc dance.
728 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700729 if (async_chunk->locked_page &&
730 (page_offset(async_chunk->locked_page) >= start &&
731 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200732 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700733 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700734 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700735
736 if (redirty)
737 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200738 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700739 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300740 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500741
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300742 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500743}
Chris Mason771ed682008-11-06 22:02:51 -0500744
Filipe Manana40ae8372014-10-06 22:14:24 +0100745static void free_async_extent_pages(struct async_extent *async_extent)
746{
747 int i;
748
749 if (!async_extent->pages)
750 return;
751
752 for (i = 0; i < async_extent->nr_pages; i++) {
753 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300754 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100755 }
756 kfree(async_extent->pages);
757 async_extent->nr_pages = 0;
758 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500759}
760
761/*
762 * phase two of compressed writeback. This is the ordered portion
763 * of the code, which only gets called in the order the work was
764 * queued. We walk all the async extents created by compress_file_range
765 * and send them down to the disk.
766 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200767static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500768{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300769 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
770 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500771 struct async_extent *async_extent;
772 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500773 struct btrfs_key ins;
774 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300775 struct btrfs_root *root = inode->root;
776 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500777 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500778
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500779again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200780 while (!list_empty(&async_chunk->extents)) {
781 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500782 struct async_extent, list);
783 list_del(&async_extent->list);
784
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500785retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200786 lock_extent(io_tree, async_extent->start,
787 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500788 /* did the compression code fall back to uncompressed IO? */
789 if (!async_extent->pages) {
790 int page_started = 0;
791 unsigned long nr_written = 0;
792
Chris Mason771ed682008-11-06 22:02:51 -0500793 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300794 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500795 async_extent->start,
796 async_extent->start +
797 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300798 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500799
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100800 /* JDM XXX */
801
Chris Mason771ed682008-11-06 22:02:51 -0500802 /*
803 * if page_started, cow_file_range inserted an
804 * inline extent and took care of all the unlocking
805 * and IO for us. Otherwise, we need to submit
806 * all those pages down to the drive.
807 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500808 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300809 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200810 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500811 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500812 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500813 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700814 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200815 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500816 kfree(async_extent);
817 cond_resched();
818 continue;
819 }
820
Wang Xiaoguang18513092016-07-25 15:51:40 +0800821 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500822 async_extent->compressed_size,
823 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800824 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500825 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100826 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500827
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400828 if (ret == -ENOSPC) {
829 unlock_extent(io_tree, async_extent->start,
830 async_extent->start +
831 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800832
833 /*
834 * we need to redirty the pages if we decide to
835 * fallback to uncompressed IO, otherwise we
836 * will not submit these pages down to lower
837 * layers.
838 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300839 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800840 async_extent->start,
841 async_extent->start +
842 async_extent->ram_size - 1);
843
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100844 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400845 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500846 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500847 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000848 /*
849 * here we're doing allocation and writeback of the
850 * compressed pages
851 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300852 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800853 async_extent->ram_size, /* len */
854 async_extent->start, /* orig_start */
855 ins.objectid, /* block_start */
856 ins.offset, /* block_len */
857 ins.offset, /* orig_block_len */
858 async_extent->ram_size, /* ram_bytes */
859 async_extent->compress_type,
860 BTRFS_ORDERED_COMPRESSED);
861 if (IS_ERR(em))
862 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500863 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800864 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500865
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300866 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800867 async_extent->start,
868 ins.objectid,
869 async_extent->ram_size,
870 ins.offset,
871 BTRFS_ORDERED_COMPRESSED,
872 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100873 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300874 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100875 async_extent->start +
876 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500877 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100878 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400879 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500880
Chris Mason771ed682008-11-06 22:02:51 -0500881 /*
882 * clear dirty, set writeback and unlock the pages.
883 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300884 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400885 async_extent->start +
886 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400887 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
888 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400889 PAGE_SET_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300890 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500891 async_extent->ram_size,
892 ins.objectid,
893 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600894 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700895 async_chunk->write_flags,
896 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100897 struct page *p = async_extent->pages[0];
898 const u64 start = async_extent->start;
899 const u64 end = start + async_extent->ram_size - 1;
900
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300901 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200902 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200903
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100904 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300905 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100906 PAGE_END_WRITEBACK |
907 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100908 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100909 }
Chris Mason771ed682008-11-06 22:02:51 -0500910 alloc_hint = ins.objectid + ins.offset;
911 kfree(async_extent);
912 cond_resched();
913 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100914 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500915out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400916 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400917 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100918out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300919 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500920 async_extent->start +
921 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400922 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100923 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400924 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
925 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100926 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
927 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100928 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100929 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500930 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500931}
932
Nikolay Borisov43c69842020-06-03 08:55:02 +0300933static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400934 u64 num_bytes)
935{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300936 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400937 struct extent_map *em;
938 u64 alloc_hint = 0;
939
940 read_lock(&em_tree->lock);
941 em = search_extent_mapping(em_tree, start, num_bytes);
942 if (em) {
943 /*
944 * if block start isn't an actual block number then find the
945 * first block in this inode and use that as a hint. If that
946 * block is also bogus then just don't worry about it.
947 */
948 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
949 free_extent_map(em);
950 em = search_extent_mapping(em_tree, 0, 0);
951 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
952 alloc_hint = em->block_start;
953 if (em)
954 free_extent_map(em);
955 } else {
956 alloc_hint = em->block_start;
957 free_extent_map(em);
958 }
959 }
960 read_unlock(&em_tree->lock);
961
962 return alloc_hint;
963}
964
Chris Mason771ed682008-11-06 22:02:51 -0500965/*
966 * when extent_io.c finds a delayed allocation range in the file,
967 * the call backs end up in this code. The basic idea is to
968 * allocate extents on disk for the range, and create ordered data structs
969 * in ram to track those extents.
970 *
971 * locked_page is the page that writepage had locked already. We use
972 * it to make sure we don't do extra locks or unlocks.
973 *
974 * *page_started is set to one if we unlock locked_page and do everything
975 * required to start IO on it. It may be clean and already done with
976 * IO when we return.
977 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300978static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -0400979 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300980 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300981 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500982{
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300983 struct btrfs_root *root = inode->root;
984 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500985 u64 alloc_hint = 0;
986 u64 num_bytes;
987 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000988 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +0100989 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400990 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500991 struct btrfs_key ins;
992 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000993 unsigned clear_bits;
994 unsigned long page_ops;
995 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500996 int ret = 0;
997
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300998 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400999 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001000 ret = -EINVAL;
1001 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001002 }
Chris Mason771ed682008-11-06 22:02:51 -05001003
Qu Wenruofda28322013-02-26 08:10:22 +00001004 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001005 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001006 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001007
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001008 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001009
Chris Mason771ed682008-11-06 22:02:51 -05001010 if (start == 0) {
1011 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001012 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001013 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001014 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001015 /*
1016 * We use DO_ACCOUNTING here because we need the
1017 * delalloc_release_metadata to be run _after_ we drop
1018 * our outstanding extent for clearing delalloc for this
1019 * range.
1020 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001021 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001022 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001023 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1024 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001025 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1026 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001027 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001028 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001029 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001030 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001032 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001033 }
1034 }
Chris Masonc8b97812008-10-29 14:49:59 -04001035
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001036 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1037 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001038
Filipe Manana432cd2a2020-06-08 13:32:55 +01001039 /*
1040 * Relocation relies on the relocated extents to have exactly the same
1041 * size as the original extents. Normally writeback for relocation data
1042 * extents follows a NOCOW path because relocation preallocates the
1043 * extents. However, due to an operation such as scrub turning a block
1044 * group to RO mode, it may fallback to COW mode, so we must make sure
1045 * an extent allocated during COW has exactly the requested size and can
1046 * not be split into smaller extents, otherwise relocation breaks and
1047 * fails during the stage where it updates the bytenr of file extent
1048 * items.
1049 */
1050 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1051 min_alloc_size = num_bytes;
1052 else
1053 min_alloc_size = fs_info->sectorsize;
1054
Anand Jain3752d222018-02-15 12:29:38 +08001055 while (num_bytes > 0) {
1056 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001057 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001058 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001059 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001060 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001061 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001062 cur_alloc_size = ins.offset;
1063 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001064
Chris Mason771ed682008-11-06 22:02:51 -05001065 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001066 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001067 start, /* orig_start */
1068 ins.objectid, /* block_start */
1069 ins.offset, /* block_len */
1070 ins.offset, /* orig_block_len */
1071 ram_size, /* ram_bytes */
1072 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001073 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001074 if (IS_ERR(em)) {
1075 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001076 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001077 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001078 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001079
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001080 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1081 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001082 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001083 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001084
Yan Zheng17d217f2008-12-12 10:03:38 -05001085 if (root->root_key.objectid ==
1086 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001087 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001088 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001089 /*
1090 * Only drop cache here, and process as normal.
1091 *
1092 * We must not allow extent_clear_unlock_delalloc()
1093 * at out_unlock label to free meta of this ordered
1094 * extent, as its meta should be freed by
1095 * btrfs_finish_ordered_io().
1096 *
1097 * So we must continue until @start is increased to
1098 * skip current ordered extent.
1099 */
Josef Bacik00361582013-08-14 14:02:47 -04001100 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001101 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001102 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001103 }
1104
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001105 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001106
Chris Masonc8b97812008-10-29 14:49:59 -04001107 /* we're not doing compressed IO, don't unlock the first
1108 * page (which the caller expects to stay locked), don't
1109 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001110 *
1111 * Do set the Private2 bit so we know this page was properly
1112 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001113 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001114 page_ops = unlock ? PAGE_UNLOCK : 0;
1115 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001116
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001117 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001118 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001119 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001120 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001121 if (num_bytes < cur_alloc_size)
1122 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001123 else
Anand Jain3752d222018-02-15 12:29:38 +08001124 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001125 alloc_hint = ins.objectid + ins.offset;
1126 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001127 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001128
1129 /*
1130 * btrfs_reloc_clone_csums() error, since start is increased
1131 * extent_clear_unlock_delalloc() at out_unlock label won't
1132 * free metadata of current ordered extent, we're OK to exit.
1133 */
1134 if (ret)
1135 goto out_unlock;
Chris Masonb888db2b2007-08-27 16:49:44 -04001136 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001137out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001138 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001139
Filipe Mananad9f85962014-08-25 10:43:00 +01001140out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001141 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001142out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001143 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001144 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001145out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001146 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1147 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001148 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1149 PAGE_END_WRITEBACK;
1150 /*
1151 * If we reserved an extent for our delalloc range (or a subrange) and
1152 * failed to create the respective ordered extent, then it means that
1153 * when we reserved the extent we decremented the extent's size from
1154 * the data space_info's bytes_may_use counter and incremented the
1155 * space_info's bytes_reserved counter by the same amount. We must make
1156 * sure extent_clear_unlock_delalloc() does not try to decrement again
1157 * the data space_info's bytes_may_use counter, therefore we do not pass
1158 * it the flag EXTENT_CLEAR_DATA_RESV.
1159 */
1160 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001161 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001162 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001163 locked_page,
1164 clear_bits,
1165 page_ops);
1166 start += cur_alloc_size;
1167 if (start >= end)
1168 goto out;
1169 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001170 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001171 clear_bits | EXTENT_CLEAR_DATA_RESV,
1172 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001173 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001174}
Chris Masonc8b97812008-10-29 14:49:59 -04001175
Chris Mason771ed682008-11-06 22:02:51 -05001176/*
1177 * work queue call back to started compression on a file and pages
1178 */
1179static noinline void async_cow_start(struct btrfs_work *work)
1180{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001181 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001182 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001183
Nikolay Borisovb5326272019-03-12 17:20:25 +02001184 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001185
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001186 compressed_extents = compress_file_range(async_chunk);
1187 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001188 btrfs_add_delayed_iput(async_chunk->inode);
1189 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001190 }
Chris Mason771ed682008-11-06 22:02:51 -05001191}
1192
1193/*
1194 * work queue call back to submit previously compressed pages
1195 */
1196static noinline void async_cow_submit(struct btrfs_work *work)
1197{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001198 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1199 work);
1200 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001201 unsigned long nr_pages;
1202
Nikolay Borisovb5326272019-03-12 17:20:25 +02001203 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001204 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001205
David Sterba093258e2018-02-26 16:15:17 +01001206 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001207 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001208 5 * SZ_1M)
1209 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001210
Nikolay Borisov4546d172019-01-03 10:50:03 +02001211 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001212 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001213 * in which case we don't have anything to submit, yet we need to
1214 * always adjust ->async_delalloc_pages as its paired with the init
1215 * happening in cow_file_range_async
1216 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001217 if (async_chunk->inode)
1218 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001219}
Chris Masonc8b97812008-10-29 14:49:59 -04001220
Chris Mason771ed682008-11-06 22:02:51 -05001221static noinline void async_cow_free(struct btrfs_work *work)
1222{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001223 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001224
Nikolay Borisovb5326272019-03-12 17:20:25 +02001225 async_chunk = container_of(work, struct async_chunk, work);
1226 if (async_chunk->inode)
1227 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001228 if (async_chunk->blkcg_css)
1229 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001230 /*
1231 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001232 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001233 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001234 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001235 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001236}
1237
Nikolay Borisov751b6432020-06-03 08:55:22 +03001238static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001239 struct writeback_control *wbc,
1240 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001241 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001242 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001243{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001244 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001245 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001246 struct async_cow *ctx;
1247 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001248 unsigned long nr_pages;
1249 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001250 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1251 int i;
1252 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001253 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001254 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001255
Nikolay Borisov751b6432020-06-03 08:55:22 +03001256 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001257
Nikolay Borisov751b6432020-06-03 08:55:22 +03001258 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001259 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1260 num_chunks = 1;
1261 should_compress = false;
1262 } else {
1263 should_compress = true;
1264 }
1265
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001266 nofs_flag = memalloc_nofs_save();
1267 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1268 memalloc_nofs_restore(nofs_flag);
1269
Nikolay Borisov97db1202019-03-12 17:20:24 +02001270 if (!ctx) {
1271 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1272 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1273 EXTENT_DO_ACCOUNTING;
1274 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1275 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1276 PAGE_SET_ERROR;
1277
Nikolay Borisov751b6432020-06-03 08:55:22 +03001278 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1279 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001280 return -ENOMEM;
1281 }
1282
1283 async_chunk = ctx->chunks;
1284 atomic_set(&ctx->num_chunks, num_chunks);
1285
1286 for (i = 0; i < num_chunks; i++) {
1287 if (should_compress)
1288 cur_end = min(end, start + SZ_512K - 1);
1289 else
1290 cur_end = end;
1291
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001292 /*
1293 * igrab is called higher up in the call chain, take only the
1294 * lightweight reference for the callback lifetime
1295 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001296 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001297 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001298 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001299 async_chunk[i].start = start;
1300 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001301 async_chunk[i].write_flags = write_flags;
1302 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001303
Chris Mason1d53c9e2019-07-10 12:28:16 -07001304 /*
1305 * The locked_page comes all the way from writepage and its
1306 * the original page we were actually given. As we spread
1307 * this large delalloc region across multiple async_chunk
1308 * structs, only the first struct needs a pointer to locked_page
1309 *
1310 * This way we don't need racey decisions about who is supposed
1311 * to unlock it.
1312 */
1313 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001314 /*
1315 * Depending on the compressibility, the pages might or
1316 * might not go through async. We want all of them to
1317 * be accounted against wbc once. Let's do it here
1318 * before the paths diverge. wbc accounting is used
1319 * only for foreign writeback detection and doesn't
1320 * need full accuracy. Just account the whole thing
1321 * against the first page.
1322 */
1323 wbc_account_cgroup_owner(wbc, locked_page,
1324 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001325 async_chunk[i].locked_page = locked_page;
1326 locked_page = NULL;
1327 } else {
1328 async_chunk[i].locked_page = NULL;
1329 }
1330
Chris Masonec39f762019-07-10 12:28:17 -07001331 if (blkcg_css != blkcg_root_css) {
1332 css_get(blkcg_css);
1333 async_chunk[i].blkcg_css = blkcg_css;
1334 } else {
1335 async_chunk[i].blkcg_css = NULL;
1336 }
1337
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001338 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1339 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001340
Nikolay Borisov97db1202019-03-12 17:20:24 +02001341 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001342 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001343
Nikolay Borisov97db1202019-03-12 17:20:24 +02001344 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001345
Chris Mason771ed682008-11-06 22:02:51 -05001346 *nr_written += nr_pages;
1347 start = cur_end + 1;
1348 }
1349 *page_started = 1;
1350 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001351}
1352
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001353static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001354 u64 bytenr, u64 num_bytes)
1355{
1356 int ret;
1357 struct btrfs_ordered_sum *sums;
1358 LIST_HEAD(list);
1359
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001360 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001361 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001362 if (ret == 0 && list_empty(&list))
1363 return 0;
1364
1365 while (!list_empty(&list)) {
1366 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1367 list_del(&sums->list);
1368 kfree(sums);
1369 }
Liu Bo58113752018-01-31 17:09:13 -07001370 if (ret < 0)
1371 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001372 return 1;
1373}
1374
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001375static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001376 const u64 start, const u64 end,
1377 int *page_started, unsigned long *nr_written)
1378{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001379 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1380 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001381 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001382 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001383 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001384 u64 range_start = start;
1385 u64 count;
1386
1387 /*
1388 * If EXTENT_NORESERVE is set it means that when the buffered write was
1389 * made we had not enough available data space and therefore we did not
1390 * reserve data space for it, since we though we could do NOCOW for the
1391 * respective file range (either there is prealloc extent or the inode
1392 * has the NOCOW bit set).
1393 *
1394 * However when we need to fallback to COW mode (because for example the
1395 * block group for the corresponding extent was turned to RO mode by a
1396 * scrub or relocation) we need to do the following:
1397 *
1398 * 1) We increment the bytes_may_use counter of the data space info.
1399 * If COW succeeds, it allocates a new data extent and after doing
1400 * that it decrements the space info's bytes_may_use counter and
1401 * increments its bytes_reserved counter by the same amount (we do
1402 * this at btrfs_add_reserved_bytes()). So we need to increment the
1403 * bytes_may_use counter to compensate (when space is reserved at
1404 * buffered write time, the bytes_may_use counter is incremented);
1405 *
1406 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1407 * that if the COW path fails for any reason, it decrements (through
1408 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1409 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001410 *
1411 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001412 * space cache inode or an inode of the data relocation tree, we must
1413 * also increment bytes_may_use of the data space_info for the same
1414 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001415 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001416 * group that contains that extent to RO mode and therefore force COW
1417 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001418 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001419 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001420 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001421 if (count > 0 || is_space_ino || is_reloc_ino) {
1422 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001423 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001424 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1425
Filipe Manana6bd335b2020-06-08 13:33:05 +01001426 if (is_space_ino || is_reloc_ino)
1427 bytes = range_bytes;
1428
Filipe Manana467dc472020-05-27 11:16:07 +01001429 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001430 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001431 spin_unlock(&sinfo->lock);
1432
Filipe Manana2166e5e2020-05-27 11:16:19 +01001433 if (count > 0)
1434 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1435 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001436 }
1437
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001438 return cow_file_range(inode, locked_page, start, end, page_started,
1439 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001440}
1441
Chris Masond352ac62008-09-29 15:18:18 -04001442/*
1443 * when nowcow writeback call back. This checks for snapshots or COW copies
1444 * of the extents that exist in the file, and COWs the file as required.
1445 *
1446 * If no cow copies or snapshots exist, we write directly to the existing
1447 * blocks on disk
1448 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001449static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001450 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001451 const u64 start, const u64 end,
1452 int *page_started, int force,
1453 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001454{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001455 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1456 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001457 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001458 u64 cow_start = (u64)-1;
1459 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001460 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001461 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001462 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1463 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001464 bool nocow = false;
1465 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001466
1467 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001468 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001469 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001470 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001471 EXTENT_DO_ACCOUNTING |
1472 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001473 PAGE_CLEAR_DIRTY |
1474 PAGE_SET_WRITEBACK |
1475 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001476 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001477 }
Li Zefan82d59022011-04-20 10:33:24 +08001478
Yan Zheng80ff3852008-10-30 14:20:02 -04001479 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001480 struct btrfs_key found_key;
1481 struct btrfs_file_extent_item *fi;
1482 struct extent_buffer *leaf;
1483 u64 extent_end;
1484 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001485 u64 num_bytes = 0;
1486 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001487 u64 ram_bytes;
1488 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001489
1490 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001491
Liu Boe4c3b2d2017-01-30 12:25:28 -08001492 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001493 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001494 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001495 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001496
1497 /*
1498 * If there is no extent for our range when doing the initial
1499 * search, then go back to the previous slot as it will be the
1500 * one containing the search offset
1501 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001502 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1503 leaf = path->nodes[0];
1504 btrfs_item_key_to_cpu(leaf, &found_key,
1505 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001506 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001507 found_key.type == BTRFS_EXTENT_DATA_KEY)
1508 path->slots[0]--;
1509 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001510 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001511next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001512 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001513 leaf = path->nodes[0];
1514 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1515 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001516 if (ret < 0) {
1517 if (cow_start != (u64)-1)
1518 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001519 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001520 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001521 if (ret > 0)
1522 break;
1523 leaf = path->nodes[0];
1524 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001525
Yan Zheng80ff3852008-10-30 14:20:02 -04001526 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001527
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001528 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001529 if (found_key.objectid > ino)
1530 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001531 /*
1532 * Keep searching until we find an EXTENT_ITEM or there are no
1533 * more extents for this inode
1534 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001535 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1536 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1537 path->slots[0]++;
1538 goto next_slot;
1539 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001540
1541 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001542 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001543 found_key.offset > end)
1544 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001545
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001546 /*
1547 * If the found extent starts after requested offset, then
1548 * adjust extent_end to be right before this extent begins
1549 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001550 if (found_key.offset > cur_offset) {
1551 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001552 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001553 goto out_check;
1554 }
Chris Masonc31f8832008-01-08 15:46:31 -05001555
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001556 /*
1557 * Found extent which begins before our range and potentially
1558 * intersect it
1559 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001560 fi = btrfs_item_ptr(leaf, path->slots[0],
1561 struct btrfs_file_extent_item);
1562 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001563
Josef Bacikcc95bef2013-04-04 14:31:27 -04001564 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001565 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1566 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001567 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001568 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001569 extent_end = found_key.offset +
1570 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001571 disk_num_bytes =
1572 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001573 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001574 * If the extent we got ends before our current offset,
1575 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001576 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001577 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001578 path->slots[0]++;
1579 goto next_slot;
1580 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001581 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001582 if (disk_bytenr == 0)
1583 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001584 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001585 if (btrfs_file_extent_compression(leaf, fi) ||
1586 btrfs_file_extent_encryption(leaf, fi) ||
1587 btrfs_file_extent_other_encoding(leaf, fi))
1588 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001589 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001590 * If extent is created before the last volume's snapshot
1591 * this implies the extent is shared, hence we can't do
1592 * nocow. This is the same check as in
1593 * btrfs_cross_ref_exist but without calling
1594 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001595 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001596 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001597 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001598 btrfs_root_last_snapshot(&root->root_item))
1599 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001600 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1601 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001602 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001603 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001604 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001605 ret = btrfs_cross_ref_exist(root, ino,
1606 found_key.offset -
1607 extent_offset, disk_bytenr);
1608 if (ret) {
1609 /*
1610 * ret could be -EIO if the above fails to read
1611 * metadata.
1612 */
1613 if (ret < 0) {
1614 if (cow_start != (u64)-1)
1615 cur_offset = cow_start;
1616 goto error;
1617 }
1618
Nikolay Borisov3e024842019-08-21 10:42:03 +03001619 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001620 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001621 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001622 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001623 disk_bytenr += cur_offset - found_key.offset;
1624 num_bytes = min(end + 1, extent_end) - cur_offset;
1625 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001626 * If there are pending snapshots for this root, we
1627 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001628 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001629 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001630 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001631 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001632 * force cow if csum exists in the range.
1633 * this ensure that csum for a given extent are
1634 * either valid or do not exist.
1635 */
Liu Bo58113752018-01-31 17:09:13 -07001636 ret = csum_exist_in_range(fs_info, disk_bytenr,
1637 num_bytes);
1638 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001639 /*
1640 * ret could be -EIO if the above fails to read
1641 * metadata.
1642 */
1643 if (ret < 0) {
1644 if (cow_start != (u64)-1)
1645 cur_offset = cow_start;
1646 goto error;
1647 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001648 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001649 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001650 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001651 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001652 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001653 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001654 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001655 extent_end = found_key.offset + ram_bytes;
1656 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001657 /* Skip extents outside of our requested range */
1658 if (extent_end <= start) {
1659 path->slots[0]++;
1660 goto next_slot;
1661 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001662 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001663 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001664 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001665 }
1666out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001667 /*
1668 * If nocow is false then record the beginning of the range
1669 * that needs to be COWed
1670 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001671 if (!nocow) {
1672 if (cow_start == (u64)-1)
1673 cow_start = cur_offset;
1674 cur_offset = extent_end;
1675 if (cur_offset > end)
1676 break;
1677 path->slots[0]++;
1678 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001679 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001680
David Sterbab3b4aa72011-04-21 01:20:15 +02001681 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001682
1683 /*
1684 * COW range from cow_start to found_key.offset - 1. As the key
1685 * will contain the beginning of the first extent that can be
1686 * NOCOW, following one which needs to be COW'ed
1687 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001688 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001689 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001690 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001691 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001692 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001693 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001694 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001695 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001696
Yan Zhengd899e052008-10-30 14:25:28 -04001697 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001698 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001699 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001700
Nikolay Borisov968322c2020-06-03 08:55:21 +03001701 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001702 orig_start,
1703 disk_bytenr, /* block_start */
1704 num_bytes, /* block_len */
1705 disk_num_bytes, /* orig_block_len */
1706 ram_bytes, BTRFS_COMPRESS_NONE,
1707 BTRFS_ORDERED_PREALLOC);
1708 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001709 ret = PTR_ERR(em);
1710 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001711 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001712 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001713 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001714 disk_bytenr, num_bytes,
1715 num_bytes,
1716 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001717 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001718 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001719 cur_offset + num_bytes - 1,
1720 0);
1721 goto error;
1722 }
Yan Zhengd899e052008-10-30 14:25:28 -04001723 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001724 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001725 disk_bytenr, num_bytes,
1726 num_bytes,
1727 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001728 if (ret)
1729 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001730 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001731
Filipe Mananaf78c4362016-05-09 13:15:41 +01001732 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001733 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001734 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001735
Yan, Zhengefa56462010-05-16 10:49:59 -04001736 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001737 BTRFS_DATA_RELOC_TREE_OBJECTID)
1738 /*
1739 * Error handled later, as we must prevent
1740 * extent_clear_unlock_delalloc() in error handler
1741 * from freeing metadata of created ordered extent.
1742 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001743 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001744 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001745
Nikolay Borisov968322c2020-06-03 08:55:21 +03001746 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001747 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001748 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001749 EXTENT_DELALLOC |
1750 EXTENT_CLEAR_DATA_RESV,
1751 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1752
Yan Zheng80ff3852008-10-30 14:20:02 -04001753 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001754
1755 /*
1756 * btrfs_reloc_clone_csums() error, now we're OK to call error
1757 * handler, as metadata for created ordered extent will only
1758 * be freed by btrfs_finish_ordered_io().
1759 */
1760 if (ret)
1761 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001762 if (cur_offset > end)
1763 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001764 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001765 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001766
Robbie Ko506481b2018-10-30 18:04:04 +08001767 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001768 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001769
Yan Zheng80ff3852008-10-30 14:20:02 -04001770 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001771 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001772 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1773 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001774 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001775 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001776 }
1777
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001778error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001779 if (nocow)
1780 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1781
Josef Bacik17ca04a2012-05-31 15:58:55 -04001782 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001783 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001784 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001785 EXTENT_DELALLOC | EXTENT_DEFRAG |
1786 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1787 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001788 PAGE_SET_WRITEBACK |
1789 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001790 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001791 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001792}
1793
Nikolay Borisov0c494222020-06-03 08:55:28 +03001794static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001795{
1796
Nikolay Borisov0c494222020-06-03 08:55:28 +03001797 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1798 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001799 return 0;
1800
1801 /*
1802 * @defrag_bytes is a hint value, no spinlock held here,
1803 * if is not zero, it means the file is defragging.
1804 * Force cow if given extent needs to be defragged.
1805 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001806 if (inode->defrag_bytes &&
1807 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001808 return 1;
1809
1810 return 0;
1811}
1812
Chris Masond352ac62008-09-29 15:18:18 -04001813/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001814 * Function to process delayed allocation (create CoW) for ranges which are
1815 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001816 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001817int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001818 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1819 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001820{
Chris Masonbe20aa92007-12-17 20:14:01 -05001821 int ret;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001822 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001823
Nikolay Borisov98456b92020-06-03 08:55:29 +03001824 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1825 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001826 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001827 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1828 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001829 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001830 } else if (!inode_can_compress(inode) ||
1831 !inode_need_compress(inode, start, end)) {
1832 ret = cow_file_range(inode, locked_page, start, end,
1833 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001834 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001835 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1836 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001837 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001838 }
Qu Wenruo524272602017-03-08 10:25:52 +08001839 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001840 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001841 end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001842 return ret;
1843}
1844
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001845void btrfs_split_delalloc_extent(struct inode *inode,
1846 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001847{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001848 u64 size;
1849
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001850 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001851 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001852 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001853
Josef Bacikdcab6a32015-02-11 15:08:59 -05001854 size = orig->end - orig->start + 1;
1855 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001856 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001857 u64 new_size;
1858
1859 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001860 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001861 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001862 */
1863 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001864 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001865 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001866 num_extents += count_max_extents(new_size);
1867 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001868 return;
1869 }
1870
Josef Bacik9e0baf62011-07-15 15:16:44 +00001871 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001872 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001873 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001874}
1875
1876/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001877 * Handle merged delayed allocation extents so we can keep track of new extents
1878 * that are just merged onto old extents, such as when we are doing sequential
1879 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001880 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001881void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1882 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001883{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001884 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001885 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001886
Josef Bacik9ed74f22009-09-11 16:12:44 -04001887 /* not delalloc, ignore it */
1888 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001889 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001890
Josef Bacik8461a3d2015-03-13 15:12:08 -04001891 if (new->start > other->start)
1892 new_size = new->end - other->start + 1;
1893 else
1894 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001895
1896 /* we're not bigger than the max, unreserve the space and go */
1897 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1898 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001899 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001900 spin_unlock(&BTRFS_I(inode)->lock);
1901 return;
1902 }
1903
1904 /*
Josef Bacikba117212015-03-13 15:01:24 -04001905 * We have to add up either side to figure out how many extents were
1906 * accounted for before we merged into one big extent. If the number of
1907 * extents we accounted for is <= the amount we need for the new range
1908 * then we can return, otherwise drop. Think of it like this
1909 *
1910 * [ 4k][MAX_SIZE]
1911 *
1912 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1913 * need 2 outstanding extents, on one side we have 1 and the other side
1914 * we have 1 so they are == and we can return. But in this case
1915 *
1916 * [MAX_SIZE+4k][MAX_SIZE+4k]
1917 *
1918 * Each range on their own accounts for 2 extents, but merged together
1919 * they are only 3 extents worth of accounting, so we need to drop in
1920 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001921 */
Josef Bacikba117212015-03-13 15:01:24 -04001922 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001923 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001924 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001925 num_extents += count_max_extents(old_size);
1926 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001927 return;
1928
Josef Bacik9e0baf62011-07-15 15:16:44 +00001929 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001930 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001931 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001932}
1933
Miao Xieeb73c1b2013-05-15 07:48:22 +00001934static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1935 struct inode *inode)
1936{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001937 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1938
Miao Xieeb73c1b2013-05-15 07:48:22 +00001939 spin_lock(&root->delalloc_lock);
1940 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1941 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1942 &root->delalloc_inodes);
1943 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1944 &BTRFS_I(inode)->runtime_flags);
1945 root->nr_delalloc_inodes++;
1946 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001947 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001948 BUG_ON(!list_empty(&root->delalloc_root));
1949 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001950 &fs_info->delalloc_roots);
1951 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001952 }
1953 }
1954 spin_unlock(&root->delalloc_lock);
1955}
1956
Nikolay Borisov2b877332018-04-27 12:21:51 +03001957
1958void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1959 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001960{
David Sterba3ffbd682018-06-29 10:56:42 +02001961 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001962
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001963 if (!list_empty(&inode->delalloc_inodes)) {
1964 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001965 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001966 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001967 root->nr_delalloc_inodes--;
1968 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001969 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001970 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001971 BUG_ON(list_empty(&root->delalloc_root));
1972 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001973 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001974 }
1975 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001976}
1977
1978static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1979 struct btrfs_inode *inode)
1980{
1981 spin_lock(&root->delalloc_lock);
1982 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001983 spin_unlock(&root->delalloc_lock);
1984}
1985
Chris Masond352ac62008-09-29 15:18:18 -04001986/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001987 * Properly track delayed allocation bytes in the inode and to maintain the
1988 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001989 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001990void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1991 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001992{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001993 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1994
Wang Shilong47059d92014-07-03 18:22:07 +08001995 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1996 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001997 /*
1998 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001999 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002000 * bit, which is only set or cleared with irqs on
2001 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002002 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002003 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002004 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002005 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002006 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002007
Josef Bacik8b62f872017-10-19 14:15:55 -04002008 spin_lock(&BTRFS_I(inode)->lock);
2009 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2010 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002011
Josef Bacik6a3891c2015-03-16 17:38:52 -04002012 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002013 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002014 return;
2015
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002016 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2017 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002018 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002019 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002020 if (*bits & EXTENT_DEFRAG)
2021 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002022 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002023 &BTRFS_I(inode)->runtime_flags))
2024 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002025 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002026 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002027
2028 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2029 (*bits & EXTENT_DELALLOC_NEW)) {
2030 spin_lock(&BTRFS_I(inode)->lock);
2031 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2032 state->start;
2033 spin_unlock(&BTRFS_I(inode)->lock);
2034 }
Chris Mason291d6732008-01-29 15:55:23 -05002035}
2036
Chris Masond352ac62008-09-29 15:18:18 -04002037/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002038 * Once a range is no longer delalloc this function ensures that proper
2039 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002040 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002041void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2042 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002043{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002044 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2045 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002046 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002047 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002048
Filipe Manana4a4b9642017-07-27 19:52:55 +01002049 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2050 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002051 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002052 spin_unlock(&inode->lock);
2053 }
Wang Shilong47059d92014-07-03 18:22:07 +08002054
Chris Mason75eff682008-12-15 15:54:40 -05002055 /*
2056 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002057 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002058 * bit, which is only set or cleared with irqs on
2059 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002060 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002061 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002062 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002063
Josef Bacik8b62f872017-10-19 14:15:55 -04002064 spin_lock(&inode->lock);
2065 btrfs_mod_outstanding_extents(inode, -num_extents);
2066 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002067
Josef Bacikb6d08f02013-09-27 14:57:43 -04002068 /*
2069 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002070 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002071 * error.
2072 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002073 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002074 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002075 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002076
Josef Bacik6a3891c2015-03-16 17:38:52 -04002077 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002078 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002079 return;
2080
Filipe Mananaa315e682017-03-06 23:04:20 +00002081 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2082 do_list && !(state->state & EXTENT_NORESERVE) &&
2083 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002084 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002085
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002086 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2087 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002088 spin_lock(&inode->lock);
2089 inode->delalloc_bytes -= len;
2090 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002091 test_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 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002094 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002095 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002096
2097 if ((state->state & EXTENT_DELALLOC_NEW) &&
2098 (*bits & EXTENT_DELALLOC_NEW)) {
2099 spin_lock(&inode->lock);
2100 ASSERT(inode->new_delalloc_bytes >= len);
2101 inode->new_delalloc_bytes -= len;
2102 spin_unlock(&inode->lock);
2103 }
Chris Mason291d6732008-01-29 15:55:23 -05002104}
2105
Chris Masond352ac62008-09-29 15:18:18 -04002106/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002107 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2108 * in a chunk's stripe. This function ensures that bios do not span a
2109 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002110 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002111 * @page - The page we are about to add to the bio
2112 * @size - size we want to add to the bio
2113 * @bio - bio we want to ensure is smaller than a stripe
2114 * @bio_flags - flags of the bio
2115 *
2116 * return 1 if page cannot be added to the bio
2117 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002118 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002119 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002120int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2121 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002122{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002123 struct inode *inode = page->mapping->host;
2124 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002125 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002126 u64 length = 0;
2127 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002128 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002129 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002130
Chris Mason771ed682008-11-06 22:02:51 -05002131 if (bio_flags & EXTENT_BIO_COMPRESSED)
2132 return 0;
2133
Kent Overstreet4f024f32013-10-11 15:44:27 -07002134 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002135 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002136 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2137 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002138 if (ret < 0)
2139 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002140
2141 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002142 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002143 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002144}
2145
Chris Masond352ac62008-09-29 15:18:18 -04002146/*
2147 * in order to insert checksums into the metadata in large chunks,
2148 * we wait until bio submission time. All the pages in the bio are
2149 * checksummed and sums are attached onto the ordered extent record.
2150 *
2151 * At IO completion time the cums attached on the ordered extent record
2152 * are inserted into the btree
2153 */
David Sterbad0ee3932018-03-08 14:35:48 +01002154static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002155 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002156{
Josef Bacikc6100a42017-05-05 11:57:13 -04002157 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002158 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002159
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002160 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002161 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002162 return 0;
2163}
Chris Masone0156402008-04-16 11:15:20 -04002164
Chris Mason4a69a412008-11-06 22:03:00 -05002165/*
Chris Masoncad321a2008-12-17 14:51:42 -05002166 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002167 * on write, or reading the csums from the tree before a read.
2168 *
2169 * Rules about async/sync submit,
2170 * a) read: sync submit
2171 *
2172 * b) write without checksum: sync submit
2173 *
2174 * c) write with checksum:
2175 * c-1) if bio is issued by fsync: sync submit
2176 * (sync_writers != 0)
2177 *
2178 * c-2) if root is reloc root: sync submit
2179 * (only in case of buffered IO)
2180 *
2181 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002182 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002183static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002184 int mirror_num,
2185 unsigned long bio_flags)
2186
Chris Mason44b8bd72008-04-16 11:14:51 -04002187{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002188 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002189 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302190 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002191 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002192 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002193 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002194
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002195 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002196
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002197 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302198 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002199
Mike Christie37226b22016-06-05 14:31:52 -05002200 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002201 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002202 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002203 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002204
Chris Masond20f7042008-12-08 16:58:54 -05002205 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002206 ret = btrfs_submit_compressed_read(inode, bio,
2207 mirror_num,
2208 bio_flags);
2209 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002210 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002211 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002212 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002213 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002214 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002215 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002216 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002217 /* csum items have already been cloned */
2218 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2219 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002220 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002221 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002222 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002223 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002224 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002225 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002226 if (ret)
2227 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002228 }
2229
Chris Mason0b86a832008-03-24 15:01:56 -04002230mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002231 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002232
2233out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002234 if (ret) {
2235 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002236 bio_endio(bio);
2237 }
Stefan Behrens61891922012-11-05 18:51:52 +01002238 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002239}
Chris Mason6885f302008-02-20 16:11:05 -05002240
Chris Masond352ac62008-09-29 15:18:18 -04002241/*
2242 * given a list of ordered sums record them in the inode. This happens
2243 * at IO completion time based on sums calculated at bio submission time.
2244 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002245static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002246 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002247{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002248 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002249 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002250
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002251 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002252 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002253 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002254 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002255 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002256 if (ret)
2257 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002258 }
2259 return 0;
2260}
2261
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002262int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002263 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002264 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002265{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002266 WARN_ON(PAGE_ALIGNED(end));
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002267 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2268 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002269}
2270
Chris Masond352ac62008-09-29 15:18:18 -04002271/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002272struct btrfs_writepage_fixup {
2273 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002274 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002275 struct btrfs_work work;
2276};
2277
Christoph Hellwigb2950862008-12-02 09:54:17 -05002278static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002279{
2280 struct btrfs_writepage_fixup *fixup;
2281 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002282 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002283 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002284 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002285 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002286 u64 page_start;
2287 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002288 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002289 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002290
2291 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2292 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002293 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002294 page_start = page_offset(page);
2295 page_end = page_offset(page) + PAGE_SIZE - 1;
2296
2297 /*
2298 * This is similar to page_mkwrite, we need to reserve the space before
2299 * we take the page lock.
2300 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002301 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2302 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002303again:
Chris Mason247e7432008-07-17 12:53:51 -04002304 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002305
Chris Mason25f3c502020-01-21 11:51:42 -05002306 /*
2307 * Before we queued this fixup, we took a reference on the page.
2308 * page->mapping may go NULL, but it shouldn't be moved to a different
2309 * address space.
2310 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002311 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2312 /*
2313 * Unfortunately this is a little tricky, either
2314 *
2315 * 1) We got here and our page had already been dealt with and
2316 * we reserved our space, thus ret == 0, so we need to just
2317 * drop our space reservation and bail. This can happen the
2318 * first time we come into the fixup worker, or could happen
2319 * while waiting for the ordered extent.
2320 * 2) Our page was already dealt with, but we happened to get an
2321 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2322 * this case we obviously don't have anything to release, but
2323 * because the page was already dealt with we don't want to
2324 * mark the page with an error, so make sure we're resetting
2325 * ret to 0. This is why we have this check _before_ the ret
2326 * check, because we do not want to have a surprise ENOSPC
2327 * when the page was already properly dealt with.
2328 */
2329 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002330 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2331 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002332 page_start, PAGE_SIZE,
2333 true);
2334 }
2335 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002336 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002337 }
Chris Mason25f3c502020-01-21 11:51:42 -05002338
2339 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002340 * We can't mess with the page state unless it is locked, so now that
2341 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002342 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002343 if (ret)
2344 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002345
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002346 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002347
2348 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002349 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002350 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002351
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002352 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002353 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002354 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2355 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002356 unlock_page(page);
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002357 btrfs_start_ordered_extent(&inode->vfs_inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002358 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002359 goto again;
2360 }
Chris Mason247e7432008-07-17 12:53:51 -04002361
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002362 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002363 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002364 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002365 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002366
Chris Mason25f3c502020-01-21 11:51:42 -05002367 /*
2368 * Everything went as planned, we're now the owner of a dirty page with
2369 * delayed allocation bits set and space reserved for our COW
2370 * destination.
2371 *
2372 * The page was dirty when we started, nothing should have cleaned it.
2373 */
2374 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002375 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002376out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002377 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002378 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002379 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2380 PAGE_SIZE, true);
2381 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002382 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002383out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002384 if (ret) {
2385 /*
2386 * We hit ENOSPC or other errors. Update the mapping and page
2387 * to reflect the errors and clean the page.
2388 */
2389 mapping_set_error(page->mapping, ret);
2390 end_extent_writepage(page, ret, page_start, page_end);
2391 clear_page_dirty_for_io(page);
2392 SetPageError(page);
2393 }
2394 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002395 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002396 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002397 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002398 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002399 /*
2400 * As a precaution, do a delayed iput in case it would be the last iput
2401 * that could need flushing space. Recursing back to fixup worker would
2402 * deadlock.
2403 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002404 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002405}
2406
2407/*
2408 * There are a few paths in the higher layers of the kernel that directly
2409 * set the page dirty bit without asking the filesystem if it is a
2410 * good idea. This causes problems because we want to make sure COW
2411 * properly happens and the data=ordered rules are followed.
2412 *
Chris Masonc8b97812008-10-29 14:49:59 -04002413 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002414 * hasn't been properly setup for IO. We kick off an async process
2415 * to fix it up. The async helper will wait for ordered extents, set
2416 * the delalloc bit and make it safe to write the page.
2417 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002418int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002419{
2420 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002421 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002422 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002423
Chris Mason8b62b722009-09-02 16:53:46 -04002424 /* this page is properly in the ordered list */
2425 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002426 return 0;
2427
Chris Mason25f3c502020-01-21 11:51:42 -05002428 /*
2429 * PageChecked is set below when we create a fixup worker for this page,
2430 * don't try to create another one if we're already PageChecked()
2431 *
2432 * The extent_io writepage code will redirty the page if we send back
2433 * EAGAIN.
2434 */
Chris Mason247e7432008-07-17 12:53:51 -04002435 if (PageChecked(page))
2436 return -EAGAIN;
2437
2438 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2439 if (!fixup)
2440 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002441
Josef Bacikf4b13632020-01-21 14:34:52 -05002442 /*
2443 * We are already holding a reference to this inode from
2444 * write_cache_pages. We need to hold it because the space reservation
2445 * takes place outside of the page lock, and we can't trust
2446 * page->mapping outside of the page lock.
2447 */
2448 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002449 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002450 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002451 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002452 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002453 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002454 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002455
2456 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002457}
2458
Yan Zhengd899e052008-10-30 14:25:28 -04002459static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002460 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002461 struct btrfs_file_extent_item *stack_fi,
2462 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002463{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002464 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002465 struct btrfs_path *path;
2466 struct extent_buffer *leaf;
2467 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002468 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2469 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2470 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2471 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002472 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002473 int ret;
2474
2475 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002476 if (!path)
2477 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002478
Chris Masona1ed8352009-09-11 12:27:37 -04002479 /*
2480 * we may be replacing one extent in the tree with another.
2481 * The new extent is pinned in the extent map, and we don't want
2482 * to drop it from the cache until it is completely in the btree.
2483 *
2484 * So, tell btrfs_drop_extents to leave this extent in the cache.
2485 * the caller is expected to unpin it and allow it to be merged
2486 * with the others.
2487 */
Nikolay Borisovc553f942020-06-03 08:55:19 +03002488 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002489 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002490 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002491 if (ret)
2492 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002493
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002494 if (!extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002495 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002496 ins.offset = file_pos;
2497 ins.type = BTRFS_EXTENT_DATA_KEY;
2498
2499 path->leave_spinning = 1;
2500 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002501 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002502 if (ret)
2503 goto out;
2504 }
Yan Zhengd899e052008-10-30 14:25:28 -04002505 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002506 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2507 write_extent_buffer(leaf, stack_fi,
2508 btrfs_item_ptr_offset(leaf, path->slots[0]),
2509 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002510
Yan Zhengd899e052008-10-30 14:25:28 -04002511 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002512 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002513
Nikolay Borisovc553f942020-06-03 08:55:19 +03002514 inode_add_bytes(&inode->vfs_inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002515
2516 ins.objectid = disk_bytenr;
2517 ins.offset = disk_num_bytes;
2518 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002519
Nikolay Borisovc553f942020-06-03 08:55:19 +03002520 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002521 if (ret)
2522 goto out;
2523
Nikolay Borisovc553f942020-06-03 08:55:19 +03002524 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002525 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002526out:
Yan Zhengd899e052008-10-30 14:25:28 -04002527 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002528
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002529 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002530}
2531
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002532static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002533 u64 start, u64 len)
2534{
David Sterba32da53862019-10-29 19:20:18 +01002535 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002536
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002537 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002538 ASSERT(cache);
2539
2540 spin_lock(&cache->lock);
2541 cache->delalloc_bytes -= len;
2542 spin_unlock(&cache->lock);
2543
2544 btrfs_put_block_group(cache);
2545}
2546
Qu Wenruo203f44c52020-06-10 09:04:40 +08002547static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2548 struct inode *inode,
2549 struct btrfs_ordered_extent *oe)
2550{
2551 struct btrfs_file_extent_item stack_fi;
2552 u64 logical_len;
2553
2554 memset(&stack_fi, 0, sizeof(stack_fi));
2555 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2556 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2557 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2558 oe->disk_num_bytes);
2559 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2560 logical_len = oe->truncated_len;
2561 else
2562 logical_len = oe->num_bytes;
2563 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2564 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2565 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2566 /* Encryption and other encoding is reserved and all 0 */
2567
Nikolay Borisovc553f942020-06-03 08:55:19 +03002568 return insert_reserved_file_extent(trans, BTRFS_I(inode), oe->file_offset,
Qu Wenruo7dbeaad2020-06-10 09:04:43 +08002569 &stack_fi, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002570}
2571
2572/*
2573 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002574 * an ordered extent if the range of bytes in the file it covers are
2575 * fully written.
2576 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002577static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002578{
Josef Bacik5fd02042012-05-02 14:00:54 -04002579 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002580 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002581 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002582 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002584 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002585 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002586 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002587 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002588 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002589 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002590 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002591 bool range_locked = false;
2592 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002593 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002594 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002595
Omar Sandovalbffe6332019-12-02 17:34:19 -08002596 start = ordered_extent->file_offset;
2597 end = start + ordered_extent->num_bytes - 1;
2598
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002599 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2600 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2601 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2602 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002603
Nikolay Borisov8d510122019-10-08 20:43:06 +03002604 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002605
Josef Bacik5fd02042012-05-02 14:00:54 -04002606 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2607 ret = -EIO;
2608 goto out;
2609 }
2610
Omar Sandovalbffe6332019-12-02 17:34:19 -08002611 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002612
Josef Bacik77cef2e2013-08-29 13:57:21 -04002613 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2614 truncated = true;
2615 logical_len = ordered_extent->truncated_len;
2616 /* Truncated the entire extent, don't bother adding */
2617 if (!logical_len)
2618 goto out;
2619 }
2620
Yan, Zhengc2167752009-11-12 09:34:21 +00002621 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002622 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002623
Josef Bacikd923afe2020-01-17 09:02:23 -05002624 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002625 if (freespace_inode)
2626 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002627 else
2628 trans = btrfs_join_transaction(root);
2629 if (IS_ERR(trans)) {
2630 ret = PTR_ERR(trans);
2631 trans = NULL;
2632 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002633 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002634 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002635 ret = btrfs_update_inode_fallback(trans, root, inode);
2636 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002637 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002638 goto out;
2639 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002640
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002641 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002642 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002643
Nikolay Borisov8d510122019-10-08 20:43:06 +03002644 if (freespace_inode)
2645 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002646 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002647 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002648 if (IS_ERR(trans)) {
2649 ret = PTR_ERR(trans);
2650 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002651 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002652 }
Chris Masona79b7d42014-05-22 16:18:52 -07002653
Josef Bacik69fe2d72017-10-19 14:15:57 -04002654 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002655
Chris Masonc8b97812008-10-29 14:49:59 -04002656 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002657 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002658 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002659 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002660 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002661 ordered_extent->file_offset,
2662 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002663 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002664 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002665 BUG_ON(root == fs_info->tree_root);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002666 ret = insert_ordered_extent_file_extent(trans, inode,
2667 ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002668 if (!ret) {
2669 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002670 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002671 ordered_extent->disk_bytenr,
2672 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002673 }
Yan Zhengd899e052008-10-30 14:25:28 -04002674 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002675 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002676 ordered_extent->file_offset,
2677 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002678 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002679 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002680 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002681 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002682
Nikolay Borisovac01f262018-01-08 10:59:43 +02002683 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2684 if (ret) {
2685 btrfs_abort_transaction(trans, ret);
2686 goto out;
2687 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002688
Josef Bacikd923afe2020-01-17 09:02:23 -05002689 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002690 ret = btrfs_update_inode_fallback(trans, root, inode);
2691 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002692 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002693 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002694 }
2695 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002696out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002697 clear_bits = EXTENT_DEFRAG;
2698 if (range_locked)
2699 clear_bits |= EXTENT_LOCKED;
2700 if (clear_new_delalloc_bytes)
2701 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002702 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2703 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002704 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002705
Miao Xiea698d0752012-09-20 01:51:59 -06002706 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002707 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002708
Josef Bacik77cef2e2013-08-29 13:57:21 -04002709 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002710 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002711
2712 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002713 unwritten_start += logical_len;
2714 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002715
2716 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002717 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002718
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002719 /*
2720 * If the ordered extent had an IOERR or something else went
2721 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002722 * back to the allocator. We only free the extent in the
2723 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002724 *
2725 * If we made it past insert_reserved_file_extent before we
2726 * errored out then we don't need to do this as the accounting
2727 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002728 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002729 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002730 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002731 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002732 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2733 /*
2734 * Discard the range before returning it back to the
2735 * free space pool
2736 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002737 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002738 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002739 ordered_extent->disk_bytenr,
2740 ordered_extent->disk_num_bytes,
2741 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002742 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002743 ordered_extent->disk_bytenr,
2744 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002745 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002746 }
2747
Josef Bacik5fd02042012-05-02 14:00:54 -04002748 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002749 * This needs to be done to make sure anybody waiting knows we are done
2750 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002751 */
2752 btrfs_remove_ordered_extent(inode, ordered_extent);
2753
Chris Masone6dcd2d2008-07-17 12:53:50 -04002754 /* once for us */
2755 btrfs_put_ordered_extent(ordered_extent);
2756 /* once for the tree */
2757 btrfs_put_ordered_extent(ordered_extent);
2758
Josef Bacik5fd02042012-05-02 14:00:54 -04002759 return ret;
2760}
2761
2762static void finish_ordered_fn(struct btrfs_work *work)
2763{
2764 struct btrfs_ordered_extent *ordered_extent;
2765 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2766 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002767}
2768
Nikolay Borisovc6297322018-11-08 10:18:08 +02002769void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2770 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002771{
Josef Bacik5fd02042012-05-02 14:00:54 -04002772 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002773 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002774 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002775 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002776
liubo1abe9b82011-03-24 11:18:59 +00002777 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2778
Chris Mason8b62b722009-09-02 16:53:46 -04002779 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002780 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2781 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002782 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002783
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002784 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002785 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002786 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002787 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002788
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002789 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002790 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002791}
2792
Omar Sandoval47df7762020-04-16 14:46:17 -07002793static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2794 int icsum, struct page *page, int pgoff, u64 start,
2795 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002796{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002797 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2798 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002799 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002800 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2801 u8 *csum_expected;
2802 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002803
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002804 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002805
2806 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002807 shash->tfm = fs_info->csum_shash;
2808
Eric Biggersfd080012020-04-30 23:51:59 -07002809 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002810
2811 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002812 goto zeroit;
2813
2814 kunmap_atomic(kaddr);
2815 return 0;
2816zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002817 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2818 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03002819 if (io_bio->device)
2820 btrfs_dev_stat_inc_and_print(io_bio->device,
2821 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08002822 memset(kaddr + pgoff, 1, len);
2823 flush_dcache_page(page);
2824 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002825 return -EIO;
2826}
2827
Chris Masond352ac62008-09-29 15:18:18 -04002828/*
Chris Masond352ac62008-09-29 15:18:18 -04002829 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002830 * if there's a match, we allow the bio to finish. If not, the code in
2831 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002832 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002833static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2834 u64 phy_offset, struct page *page,
2835 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002836{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002837 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002838 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002839 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002840 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002841
Chris Masond20f7042008-12-08 16:58:54 -05002842 if (PageChecked(page)) {
2843 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002844 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002845 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002846
2847 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002848 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002849
Yan Zheng17d217f2008-12-12 10:03:38 -05002850 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002851 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002852 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002853 return 0;
2854 }
2855
Miao Xiefacc8a222013-07-25 19:22:34 +08002856 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002857 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2858 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002859}
Chris Masonb888db2b2007-08-27 16:49:44 -04002860
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002861/*
2862 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2863 *
2864 * @inode: The inode we want to perform iput on
2865 *
2866 * This function uses the generic vfs_inode::i_count to track whether we should
2867 * just decrement it (in case it's > 1) or if this is the last iput then link
2868 * the inode to the delayed iput machinery. Delayed iputs are processed at
2869 * transaction commit time/superblock commit/cleaner kthread.
2870 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002871void btrfs_add_delayed_iput(struct inode *inode)
2872{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002873 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002874 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002875
2876 if (atomic_add_unless(&inode->i_count, -1, 1))
2877 return;
2878
Josef Bacik034f7842018-12-03 11:06:52 -05002879 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002880 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002881 ASSERT(list_empty(&binode->delayed_iput));
2882 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002883 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002884 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2885 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002886}
2887
Josef Bacik63611e72019-06-18 10:59:18 -04002888static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2889 struct btrfs_inode *inode)
2890{
2891 list_del_init(&inode->delayed_iput);
2892 spin_unlock(&fs_info->delayed_iput_lock);
2893 iput(&inode->vfs_inode);
2894 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2895 wake_up(&fs_info->delayed_iputs_wait);
2896 spin_lock(&fs_info->delayed_iput_lock);
2897}
2898
2899static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2900 struct btrfs_inode *inode)
2901{
2902 if (!list_empty(&inode->delayed_iput)) {
2903 spin_lock(&fs_info->delayed_iput_lock);
2904 if (!list_empty(&inode->delayed_iput))
2905 run_delayed_iput_locked(fs_info, inode);
2906 spin_unlock(&fs_info->delayed_iput_lock);
2907 }
2908}
2909
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002910void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002911{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002912
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002913 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002914 while (!list_empty(&fs_info->delayed_iputs)) {
2915 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002916
David Sterba8089fe62015-11-19 14:15:51 +01002917 inode = list_first_entry(&fs_info->delayed_iputs,
2918 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002919 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002920 }
David Sterba8089fe62015-11-19 14:15:51 +01002921 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002922}
2923
Josef Bacik034f7842018-12-03 11:06:52 -05002924/**
2925 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2926 * @fs_info - the fs_info for this fs
2927 * @return - EINTR if we were killed, 0 if nothing's pending
2928 *
2929 * This will wait on any delayed iputs that are currently running with KILLABLE
2930 * set. Once they are all done running we will return, unless we are killed in
2931 * which case we return EINTR. This helps in user operations like fallocate etc
2932 * that might get blocked on the iputs.
2933 */
2934int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2935{
2936 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2937 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2938 if (ret)
2939 return -EINTR;
2940 return 0;
2941}
2942
Yan, Zhengd68fc572010-05-16 10:49:58 -04002943/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002944 * This creates an orphan entry for the given inode in case something goes wrong
2945 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002946 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002947int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002948 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002949{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002950 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002951
Omar Sandoval27919062018-05-11 13:13:37 -07002952 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2953 if (ret && ret != -EEXIST) {
2954 btrfs_abort_transaction(trans, ret);
2955 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002956 }
2957
Yan, Zhengd68fc572010-05-16 10:49:58 -04002958 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002959}
2960
2961/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002962 * We have done the delete so we can go ahead and remove the orphan item for
2963 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002964 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002965static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002966 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002967{
Omar Sandoval27919062018-05-11 13:13:37 -07002968 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002969}
2970
2971/*
2972 * this cleans up any orphans that may be left on the list from the last use
2973 * of this root.
2974 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002975int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002976{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002977 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002978 struct btrfs_path *path;
2979 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002980 struct btrfs_key key, found_key;
2981 struct btrfs_trans_handle *trans;
2982 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002983 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002984 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002985
Yan, Zhengd68fc572010-05-16 10:49:58 -04002986 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002987 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002988
2989 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002990 if (!path) {
2991 ret = -ENOMEM;
2992 goto out;
2993 }
David Sterbae4058b52015-11-27 16:31:35 +01002994 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002995
2996 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002997 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04002998 key.offset = (u64)-1;
2999
Josef Bacik7b128762008-07-24 12:17:14 -04003000 while (1) {
3001 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003002 if (ret < 0)
3003 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003004
3005 /*
3006 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003007 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003008 * find the key and see if we have stuff that matches
3009 */
3010 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003011 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003012 if (path->slots[0] == 0)
3013 break;
3014 path->slots[0]--;
3015 }
3016
3017 /* pull out the item */
3018 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003019 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3020
3021 /* make sure the item matches what we want */
3022 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3023 break;
David Sterba962a2982014-06-04 18:41:45 +02003024 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003025 break;
3026
3027 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003028 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003029
3030 /*
3031 * this is where we are basically btrfs_lookup, without the
3032 * crossing root thing. we store the inode number in the
3033 * offset of the orphan item.
3034 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003035
3036 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003037 btrfs_err(fs_info,
3038 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003039 ret = -EINVAL;
3040 goto out;
3041 }
3042
3043 last_objectid = found_key.offset;
3044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003045 found_key.objectid = found_key.offset;
3046 found_key.type = BTRFS_INODE_ITEM_KEY;
3047 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003048 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303049 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003050 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003051 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003052
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003053 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003054 struct btrfs_root *dead_root;
3055 struct btrfs_fs_info *fs_info = root->fs_info;
3056 int is_dead_root = 0;
3057
3058 /*
3059 * this is an orphan in the tree root. Currently these
3060 * could come from 2 sources:
3061 * a) a snapshot deletion in progress
3062 * b) a free space cache inode
3063 * We need to distinguish those two, as the snapshot
3064 * orphan must not get deleted.
3065 * find_dead_roots already ran before us, so if this
3066 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003067 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003068 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003069
3070 spin_lock(&fs_info->fs_roots_radix_lock);
3071 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3072 (unsigned long)found_key.objectid);
3073 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3074 is_dead_root = 1;
3075 spin_unlock(&fs_info->fs_roots_radix_lock);
3076
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003077 if (is_dead_root) {
3078 /* prevent this orphan from being found again */
3079 key.offset = found_key.objectid - 1;
3080 continue;
3081 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003082
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003083 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003084
Josef Bacika8c9e572011-09-21 16:55:59 -04003085 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003086 * If we have an inode with links, there are a couple of
3087 * possibilities. Old kernels (before v3.12) used to create an
3088 * orphan item for truncate indicating that there were possibly
3089 * extent items past i_size that needed to be deleted. In v3.12,
3090 * truncate was changed to update i_size in sync with the extent
3091 * items, but the (useless) orphan item was still created. Since
3092 * v4.18, we don't create the orphan item for truncate at all.
3093 *
3094 * So, this item could mean that we need to do a truncate, but
3095 * only if this filesystem was last used on a pre-v3.12 kernel
3096 * and was not cleanly unmounted. The odds of that are quite
3097 * slim, and it's a pain to do the truncate now, so just delete
3098 * the orphan item.
3099 *
3100 * It's also possible that this orphan item was supposed to be
3101 * deleted but wasn't. The inode number may have been reused,
3102 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003103 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003104 if (ret == -ENOENT || inode->i_nlink) {
3105 if (!ret)
3106 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003107 trans = btrfs_start_transaction(root, 1);
3108 if (IS_ERR(trans)) {
3109 ret = PTR_ERR(trans);
3110 goto out;
3111 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003112 btrfs_debug(fs_info, "auto deleting %Lu",
3113 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003114 ret = btrfs_del_orphan_item(trans, root,
3115 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003116 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003117 if (ret)
3118 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003119 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003120 }
Josef Bacik7b128762008-07-24 12:17:14 -04003121
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003122 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003123
3124 /* this will do delete_inode and everything for us */
3125 iput(inode);
3126 }
Miao Xie3254c872011-11-10 20:45:05 -05003127 /* release the path since we're done with it */
3128 btrfs_release_path(path);
3129
Yan, Zhengd68fc572010-05-16 10:49:58 -04003130 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3131
Omar Sandovala575cee2018-05-11 13:13:38 -07003132 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003133 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003134 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003135 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003136 }
Josef Bacik7b128762008-07-24 12:17:14 -04003137
3138 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003139 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003140
3141out:
3142 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003143 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003144 btrfs_free_path(path);
3145 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003146}
3147
Chris Masond352ac62008-09-29 15:18:18 -04003148/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003149 * very simple check to peek ahead in the leaf looking for xattrs. If we
3150 * don't find any xattrs, we know there can't be any acls.
3151 *
3152 * slot is the slot the inode is in, objectid is the objectid of the inode
3153 */
3154static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003155 int slot, u64 objectid,
3156 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003157{
3158 u32 nritems = btrfs_header_nritems(leaf);
3159 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003160 static u64 xattr_access = 0;
3161 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003162 int scanned = 0;
3163
Josef Bacikf23b5a52013-06-19 10:16:26 -04003164 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003165 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3166 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3167 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3168 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003169 }
3170
Chris Mason46a53cc2009-04-27 11:47:50 -04003171 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003172 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003173 while (slot < nritems) {
3174 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3175
3176 /* we found a different objectid, there must not be acls */
3177 if (found_key.objectid != objectid)
3178 return 0;
3179
3180 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003181 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003182 if (*first_xattr_slot == -1)
3183 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003184 if (found_key.offset == xattr_access ||
3185 found_key.offset == xattr_default)
3186 return 1;
3187 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003188
3189 /*
3190 * we found a key greater than an xattr key, there can't
3191 * be any acls later on
3192 */
3193 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3194 return 0;
3195
3196 slot++;
3197 scanned++;
3198
3199 /*
3200 * it goes inode, inode backrefs, xattrs, extents,
3201 * so if there are a ton of hard links to an inode there can
3202 * be a lot of backrefs. Don't waste time searching too hard,
3203 * this is just an optimization
3204 */
3205 if (scanned >= 8)
3206 break;
3207 }
3208 /* we hit the end of the leaf before we found an xattr or
3209 * something larger than an xattr. We have to assume the inode
3210 * has acls
3211 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003212 if (*first_xattr_slot == -1)
3213 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003214 return 1;
3215}
3216
3217/*
Chris Masond352ac62008-09-29 15:18:18 -04003218 * read an inode from the btree into the in-memory inode
3219 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003220static int btrfs_read_locked_inode(struct inode *inode,
3221 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003222{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003223 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003224 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003225 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003226 struct btrfs_inode_item *inode_item;
3227 struct btrfs_root *root = BTRFS_I(inode)->root;
3228 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003229 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003230 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003231 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003232 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003233 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003234 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003235
3236 ret = btrfs_fill_inode(inode, &rdev);
3237 if (!ret)
3238 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003239
Filipe Manana4222ea72018-10-24 10:13:03 +01003240 if (!path) {
3241 path = btrfs_alloc_path();
3242 if (!path)
3243 return -ENOMEM;
3244 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003245
Chris Mason39279cc2007-06-12 06:35:45 -04003246 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003247
Chris Mason39279cc2007-06-12 06:35:45 -04003248 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003249 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003250 if (path != in_path)
3251 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003252 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003253 }
Chris Mason39279cc2007-06-12 06:35:45 -04003254
Chris Mason5f39d392007-10-15 16:14:19 -04003255 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003256
3257 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003258 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003259
Chris Mason5f39d392007-10-15 16:14:19 -04003260 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3261 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003262 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003263 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003264 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3265 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003266 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003267 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3268 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003269
David Sterbaa937b972014-12-12 17:39:12 +01003270 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3271 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003272
David Sterbaa937b972014-12-12 17:39:12 +01003273 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3274 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003275
David Sterbaa937b972014-12-12 17:39:12 +01003276 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3277 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003278
chandan r9cc97d62012-07-04 12:48:07 +05303279 BTRFS_I(inode)->i_otime.tv_sec =
3280 btrfs_timespec_sec(leaf, &inode_item->otime);
3281 BTRFS_I(inode)->i_otime.tv_nsec =
3282 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003283
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003284 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003285 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003286 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3287
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003288 inode_set_iversion_queried(inode,
3289 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003290 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003291 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003292 rdev = btrfs_inode_rdev(leaf, inode_item);
3293
Josef Bacikaec74772008-07-24 12:12:38 -04003294 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003295 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003296
3297cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003298 /*
3299 * If we were modified in the current generation and evicted from memory
3300 * and then re-read we need to do a full sync since we don't have any
3301 * idea about which extents were modified before we were evicted from
3302 * cache.
3303 *
3304 * This is required for both inode re-read from disk and delayed inode
3305 * in delayed_nodes_tree.
3306 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003307 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003308 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3309 &BTRFS_I(inode)->runtime_flags);
3310
Filipe Mananabde6c242015-07-24 00:00:19 +01003311 /*
3312 * We don't persist the id of the transaction where an unlink operation
3313 * against the inode was last made. So here we assume the inode might
3314 * have been evicted, and therefore the exact value of last_unlink_trans
3315 * lost, and set it to last_trans to avoid metadata inconsistencies
3316 * between the inode and its parent if the inode is fsync'ed and the log
3317 * replayed. For example, in the scenario:
3318 *
3319 * touch mydir/foo
3320 * ln mydir/foo mydir/bar
3321 * sync
3322 * unlink mydir/bar
3323 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3324 * xfs_io -c fsync mydir/foo
3325 * <power failure>
3326 * mount fs, triggers fsync log replay
3327 *
3328 * We must make sure that when we fsync our inode foo we also log its
3329 * parent inode, otherwise after log replay the parent still has the
3330 * dentry with the "bar" name but our inode foo has a link count of 1
3331 * and doesn't have an inode ref with the name "bar" anymore.
3332 *
3333 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003334 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003335 * transaction commits on fsync if our inode is a directory, or if our
3336 * inode is not a directory, logging its parent unnecessarily.
3337 */
3338 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3339
Filipe Manana3ebac172020-07-15 12:30:43 +01003340 /*
3341 * Same logic as for last_unlink_trans. We don't persist the generation
3342 * of the last transaction where this inode was used for a reflink
3343 * operation, so after eviction and reloading the inode we must be
3344 * pessimistic and assume the last transaction that modified the inode.
3345 */
3346 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3347
Miao Xie67de1172013-12-26 13:07:06 +08003348 path->slots[0]++;
3349 if (inode->i_nlink != 1 ||
3350 path->slots[0] >= btrfs_header_nritems(leaf))
3351 goto cache_acl;
3352
3353 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003354 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003355 goto cache_acl;
3356
3357 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3358 if (location.type == BTRFS_INODE_REF_KEY) {
3359 struct btrfs_inode_ref *ref;
3360
3361 ref = (struct btrfs_inode_ref *)ptr;
3362 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3363 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3364 struct btrfs_inode_extref *extref;
3365
3366 extref = (struct btrfs_inode_extref *)ptr;
3367 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3368 extref);
3369 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003370cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003371 /*
3372 * try to precache a NULL acl entry for files that don't have
3373 * any xattrs or acls
3374 */
Li Zefan33345d012011-04-20 10:31:50 +08003375 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003376 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003377 if (first_xattr_slot != -1) {
3378 path->slots[0] = first_xattr_slot;
3379 ret = btrfs_load_inode_props(inode, path);
3380 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003381 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003382 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003383 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003384 root->root_key.objectid, ret);
3385 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003386 if (path != in_path)
3387 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003388
Al Viro72c04902009-06-24 16:58:48 -04003389 if (!maybe_acls)
3390 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003391
Chris Mason39279cc2007-06-12 06:35:45 -04003392 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003393 case S_IFREG:
3394 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003395 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003396 inode->i_fop = &btrfs_file_operations;
3397 inode->i_op = &btrfs_file_inode_operations;
3398 break;
3399 case S_IFDIR:
3400 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003401 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003402 break;
3403 case S_IFLNK:
3404 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003405 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003406 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003407 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003408 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003409 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003410 init_special_inode(inode, inode->i_mode, rdev);
3411 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003412 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003413
David Sterba7b6a2212018-03-26 18:40:21 +02003414 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003415 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003416}
3417
Chris Masond352ac62008-09-29 15:18:18 -04003418/*
3419 * given a leaf and an inode, copy the inode fields into the leaf
3420 */
Chris Masone02119d2008-09-05 16:13:11 -04003421static void fill_inode_item(struct btrfs_trans_handle *trans,
3422 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003423 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003424 struct inode *inode)
3425{
Liu Bo51fab692012-12-27 09:01:21 +00003426 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003427
David Sterbac82f8232019-08-09 17:48:21 +02003428 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003429
David Sterbacc4c13d2020-04-29 02:15:56 +02003430 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3431 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3432 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3433 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3434 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003435
David Sterbacc4c13d2020-04-29 02:15:56 +02003436 btrfs_set_token_timespec_sec(&token, &item->atime,
3437 inode->i_atime.tv_sec);
3438 btrfs_set_token_timespec_nsec(&token, &item->atime,
3439 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003440
David Sterbacc4c13d2020-04-29 02:15:56 +02003441 btrfs_set_token_timespec_sec(&token, &item->mtime,
3442 inode->i_mtime.tv_sec);
3443 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3444 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003445
David Sterbacc4c13d2020-04-29 02:15:56 +02003446 btrfs_set_token_timespec_sec(&token, &item->ctime,
3447 inode->i_ctime.tv_sec);
3448 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3449 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003450
David Sterbacc4c13d2020-04-29 02:15:56 +02003451 btrfs_set_token_timespec_sec(&token, &item->otime,
3452 BTRFS_I(inode)->i_otime.tv_sec);
3453 btrfs_set_token_timespec_nsec(&token, &item->otime,
3454 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303455
David Sterbacc4c13d2020-04-29 02:15:56 +02003456 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3457 btrfs_set_token_inode_generation(&token, item,
3458 BTRFS_I(inode)->generation);
3459 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3460 btrfs_set_token_inode_transid(&token, item, trans->transid);
3461 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3462 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3463 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003464}
3465
Chris Masond352ac62008-09-29 15:18:18 -04003466/*
3467 * copy everything in the in-memory inode into the btree.
3468 */
Chris Mason21151332011-11-10 20:39:08 -05003469static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003470 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003471{
3472 struct btrfs_inode_item *inode_item;
3473 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003474 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003475 int ret;
3476
3477 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003478 if (!path)
3479 return -ENOMEM;
3480
Chris Masonb9473432009-03-13 11:00:37 -04003481 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003482 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3483 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003484 if (ret) {
3485 if (ret > 0)
3486 ret = -ENOENT;
3487 goto failed;
3488 }
3489
Chris Mason5f39d392007-10-15 16:14:19 -04003490 leaf = path->nodes[0];
3491 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003492 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003493
Chris Masone02119d2008-09-05 16:13:11 -04003494 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003495 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovd9094412020-06-05 10:41:13 +03003496 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04003497 ret = 0;
3498failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003499 btrfs_free_path(path);
3500 return ret;
3501}
3502
Chris Masond352ac62008-09-29 15:18:18 -04003503/*
Chris Mason21151332011-11-10 20:39:08 -05003504 * copy everything in the in-memory inode into the btree.
3505 */
3506noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3507 struct btrfs_root *root, struct inode *inode)
3508{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003509 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003510 int ret;
3511
3512 /*
3513 * If the inode is a free space inode, we can deadlock during commit
3514 * if we put it into the delayed code.
3515 *
3516 * The data relocation inode should also be directly updated
3517 * without delay
3518 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003519 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003520 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003521 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003522 btrfs_update_root_times(trans, root);
3523
Chris Mason21151332011-11-10 20:39:08 -05003524 ret = btrfs_delayed_update_inode(trans, root, inode);
3525 if (!ret)
Nikolay Borisovd9094412020-06-05 10:41:13 +03003526 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason21151332011-11-10 20:39:08 -05003527 return ret;
3528 }
3529
3530 return btrfs_update_inode_item(trans, root, inode);
3531}
3532
Josef Bacikbe6aef62012-10-22 15:43:12 -04003533noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3534 struct btrfs_root *root,
3535 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003536{
3537 int ret;
3538
3539 ret = btrfs_update_inode(trans, root, inode);
3540 if (ret == -ENOSPC)
3541 return btrfs_update_inode_item(trans, root, inode);
3542 return ret;
3543}
3544
3545/*
Chris Masond352ac62008-09-29 15:18:18 -04003546 * unlink helper that gets used here in inode.c and in the tree logging
3547 * recovery code. It remove a link in a directory with a given name, and
3548 * also drops the back refs in the inode to the directory
3549 */
Al Viro92986792011-03-04 17:14:37 +00003550static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3551 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003552 struct btrfs_inode *dir,
3553 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003554 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003555{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003556 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003557 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003558 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003559 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003560 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003561 u64 ino = btrfs_ino(inode);
3562 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003563
3564 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003565 if (!path) {
3566 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003567 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003568 }
3569
Chris Masonb9473432009-03-13 11:00:37 -04003570 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003571 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003572 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003573 if (IS_ERR_OR_NULL(di)) {
3574 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003575 goto err;
3576 }
Chris Mason39279cc2007-06-12 06:35:45 -04003577 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003578 if (ret)
3579 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003580 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003581
Miao Xie67de1172013-12-26 13:07:06 +08003582 /*
3583 * If we don't have dir index, we have to get it by looking up
3584 * the inode ref, since we get the inode ref, remove it directly,
3585 * it is unnecessary to do delayed deletion.
3586 *
3587 * But if we have dir index, needn't search inode ref to get it.
3588 * Since the inode ref is close to the inode item, it is better
3589 * that we delay to delete it, and just do this deletion when
3590 * we update the inode item.
3591 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003592 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003593 ret = btrfs_delayed_delete_inode_ref(inode);
3594 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003595 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003596 goto skip_backref;
3597 }
3598 }
3599
Li Zefan33345d012011-04-20 10:31:50 +08003600 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3601 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003602 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003603 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003604 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003605 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003606 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003607 goto err;
3608 }
Miao Xie67de1172013-12-26 13:07:06 +08003609skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003610 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003611 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003612 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003613 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003614 }
Chris Mason39279cc2007-06-12 06:35:45 -04003615
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003616 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3617 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003618 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003619 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003620 goto err;
3621 }
Chris Masone02119d2008-09-05 16:13:11 -04003622
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003623 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3624 index);
Chris Mason6418c962010-10-30 07:34:24 -04003625 if (ret == -ENOENT)
3626 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003627 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003628 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003629
3630 /*
3631 * If we have a pending delayed iput we could end up with the final iput
3632 * being run in btrfs-cleaner context. If we have enough of these built
3633 * up we can end up burning a lot of time in btrfs-cleaner without any
3634 * way to throttle the unlinks. Since we're currently holding a ref on
3635 * the inode we can run the delayed iput here without any issues as the
3636 * final iput won't be done until after we drop the ref we're currently
3637 * holding.
3638 */
3639 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003640err:
3641 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003642 if (ret)
3643 goto out;
3644
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003645 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003646 inode_inc_iversion(&inode->vfs_inode);
3647 inode_inc_iversion(&dir->vfs_inode);
3648 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3649 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3650 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003651out:
Chris Mason39279cc2007-06-12 06:35:45 -04003652 return ret;
3653}
3654
Al Viro92986792011-03-04 17:14:37 +00003655int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3656 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003657 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003658 const char *name, int name_len)
3659{
3660 int ret;
3661 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3662 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003663 drop_nlink(&inode->vfs_inode);
3664 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003665 }
3666 return ret;
3667}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668
3669/*
3670 * helper to start transaction for unlink and rmdir.
3671 *
Josef Bacikd52be812013-05-29 14:54:47 -04003672 * unlink and rmdir are special in btrfs, they do not always free space, so
3673 * if we cannot make our reservations the normal way try and see if there is
3674 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3675 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003676 */
Josef Bacikd52be812013-05-29 14:54:47 -04003677static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003679 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003680
Josef Bacike70bea52011-10-11 14:18:24 -04003681 /*
3682 * 1 for the possible orphan item
3683 * 1 for the dir item
3684 * 1 for the dir index
3685 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003686 * 1 for the inode
3687 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003688 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003689}
3690
Chris Mason39279cc2007-06-12 06:35:45 -04003691static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3692{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003694 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003695 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003696 int ret;
3697
Josef Bacikd52be812013-05-29 14:54:47 -04003698 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003699 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003700 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003701
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003702 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3703 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003704
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003705 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3706 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3707 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003708 if (ret)
3709 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003710
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003712 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003713 if (ret)
3714 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003715 }
Josef Bacik7b128762008-07-24 12:17:14 -04003716
Tsutomu Itohb5324022011-07-19 07:27:20 +00003717out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003718 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003719 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003720 return ret;
3721}
3722
Misono Tomohirof60a2362018-04-18 11:34:52 +09003723static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003724 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003725{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003726 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003727 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003728 struct btrfs_path *path;
3729 struct extent_buffer *leaf;
3730 struct btrfs_dir_item *di;
3731 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003732 const char *name = dentry->d_name.name;
3733 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003734 u64 index;
3735 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003736 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003737 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003738
Josef Bacik045d3962019-12-18 17:20:27 -05003739 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3740 objectid = inode->root->root_key.objectid;
3741 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3742 objectid = inode->location.objectid;
3743 } else {
3744 WARN_ON(1);
3745 return -EINVAL;
3746 }
3747
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003748 path = btrfs_alloc_path();
3749 if (!path)
3750 return -ENOMEM;
3751
Li Zefan33345d012011-04-20 10:31:50 +08003752 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003753 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003755 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003756 goto out;
3757 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003758
3759 leaf = path->nodes[0];
3760 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3761 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3762 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003763 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003764 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003765 goto out;
3766 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003767 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003768
Josef Bacikd49d3282019-12-18 17:20:28 -05003769 /*
3770 * This is a placeholder inode for a subvolume we didn't have a
3771 * reference to at the time of the snapshot creation. In the meantime
3772 * we could have renamed the real subvol link into our snapshot, so
3773 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3774 * Instead simply lookup the dir_index_item for this entry so we can
3775 * remove it. Otherwise we know we have a ref to the root and we can
3776 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3777 */
3778 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003779 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003780 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003781 if (IS_ERR_OR_NULL(di)) {
3782 if (!di)
3783 ret = -ENOENT;
3784 else
3785 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003786 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003787 goto out;
3788 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003789
3790 leaf = path->nodes[0];
3791 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003792 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003793 btrfs_release_path(path);
3794 } else {
3795 ret = btrfs_del_root_ref(trans, objectid,
3796 root->root_key.objectid, dir_ino,
3797 &index, name, name_len);
3798 if (ret) {
3799 btrfs_abort_transaction(trans, ret);
3800 goto out;
3801 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003802 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003803
Lu Fengqi9add2942018-08-01 11:32:26 +08003804 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003806 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003807 goto out;
3808 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003809
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003810 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003811 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003812 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003813 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003814 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003815 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003816out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003817 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003818 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003819}
3820
Misono Tomohiroec42f162018-04-18 11:34:13 +09003821/*
3822 * Helper to check if the subvolume references other subvolumes or if it's
3823 * default.
3824 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003825static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003826{
3827 struct btrfs_fs_info *fs_info = root->fs_info;
3828 struct btrfs_path *path;
3829 struct btrfs_dir_item *di;
3830 struct btrfs_key key;
3831 u64 dir_id;
3832 int ret;
3833
3834 path = btrfs_alloc_path();
3835 if (!path)
3836 return -ENOMEM;
3837
3838 /* Make sure this root isn't set as the default subvol */
3839 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3840 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3841 dir_id, "default", 7, 0);
3842 if (di && !IS_ERR(di)) {
3843 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3844 if (key.objectid == root->root_key.objectid) {
3845 ret = -EPERM;
3846 btrfs_err(fs_info,
3847 "deleting default subvolume %llu is not allowed",
3848 key.objectid);
3849 goto out;
3850 }
3851 btrfs_release_path(path);
3852 }
3853
3854 key.objectid = root->root_key.objectid;
3855 key.type = BTRFS_ROOT_REF_KEY;
3856 key.offset = (u64)-1;
3857
3858 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3859 if (ret < 0)
3860 goto out;
3861 BUG_ON(ret == 0);
3862
3863 ret = 0;
3864 if (path->slots[0] > 0) {
3865 path->slots[0]--;
3866 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3867 if (key.objectid == root->root_key.objectid &&
3868 key.type == BTRFS_ROOT_REF_KEY)
3869 ret = -ENOTEMPTY;
3870 }
3871out:
3872 btrfs_free_path(path);
3873 return ret;
3874}
3875
Nikolay Borisov20a68002018-04-27 14:36:24 +03003876/* Delete all dentries for inodes belonging to the root */
3877static void btrfs_prune_dentries(struct btrfs_root *root)
3878{
3879 struct btrfs_fs_info *fs_info = root->fs_info;
3880 struct rb_node *node;
3881 struct rb_node *prev;
3882 struct btrfs_inode *entry;
3883 struct inode *inode;
3884 u64 objectid = 0;
3885
3886 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3887 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3888
3889 spin_lock(&root->inode_lock);
3890again:
3891 node = root->inode_tree.rb_node;
3892 prev = NULL;
3893 while (node) {
3894 prev = node;
3895 entry = rb_entry(node, struct btrfs_inode, rb_node);
3896
David Sterba37508512018-06-29 10:56:40 +02003897 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003898 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003899 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003900 node = node->rb_right;
3901 else
3902 break;
3903 }
3904 if (!node) {
3905 while (prev) {
3906 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003907 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003908 node = prev;
3909 break;
3910 }
3911 prev = rb_next(prev);
3912 }
3913 }
3914 while (node) {
3915 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003916 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003917 inode = igrab(&entry->vfs_inode);
3918 if (inode) {
3919 spin_unlock(&root->inode_lock);
3920 if (atomic_read(&inode->i_count) > 1)
3921 d_prune_aliases(inode);
3922 /*
3923 * btrfs_drop_inode will have it removed from the inode
3924 * cache when its usage count hits zero.
3925 */
3926 iput(inode);
3927 cond_resched();
3928 spin_lock(&root->inode_lock);
3929 goto again;
3930 }
3931
3932 if (cond_resched_lock(&root->inode_lock))
3933 goto again;
3934
3935 node = rb_next(node);
3936 }
3937 spin_unlock(&root->inode_lock);
3938}
3939
Misono Tomohirof60a2362018-04-18 11:34:52 +09003940int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3941{
3942 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3943 struct btrfs_root *root = BTRFS_I(dir)->root;
3944 struct inode *inode = d_inode(dentry);
3945 struct btrfs_root *dest = BTRFS_I(inode)->root;
3946 struct btrfs_trans_handle *trans;
3947 struct btrfs_block_rsv block_rsv;
3948 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003949 int ret;
3950 int err;
3951
3952 /*
3953 * Don't allow to delete a subvolume with send in progress. This is
3954 * inside the inode lock so the error handling that has to drop the bit
3955 * again is not run concurrently.
3956 */
3957 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003958 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003959 spin_unlock(&dest->root_item_lock);
3960 btrfs_warn(fs_info,
3961 "attempt to delete subvolume %llu during send",
3962 dest->root_key.objectid);
3963 return -EPERM;
3964 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003965 root_flags = btrfs_root_flags(&dest->root_item);
3966 btrfs_set_root_flags(&dest->root_item,
3967 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3968 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003969
3970 down_write(&fs_info->subvol_sem);
3971
3972 err = may_destroy_subvol(dest);
3973 if (err)
3974 goto out_up_write;
3975
3976 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3977 /*
3978 * One for dir inode,
3979 * two for dir entries,
3980 * two for root ref/backref.
3981 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003982 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003983 if (err)
3984 goto out_up_write;
3985
3986 trans = btrfs_start_transaction(root, 0);
3987 if (IS_ERR(trans)) {
3988 err = PTR_ERR(trans);
3989 goto out_release;
3990 }
3991 trans->block_rsv = &block_rsv;
3992 trans->bytes_reserved = block_rsv.size;
3993
3994 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3995
Josef Bacik045d3962019-12-18 17:20:27 -05003996 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003997 if (ret) {
3998 err = ret;
3999 btrfs_abort_transaction(trans, ret);
4000 goto out_end_trans;
4001 }
4002
4003 btrfs_record_root_in_trans(trans, dest);
4004
4005 memset(&dest->root_item.drop_progress, 0,
4006 sizeof(dest->root_item.drop_progress));
4007 dest->root_item.drop_level = 0;
4008 btrfs_set_root_refs(&dest->root_item, 0);
4009
4010 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4011 ret = btrfs_insert_orphan_item(trans,
4012 fs_info->tree_root,
4013 dest->root_key.objectid);
4014 if (ret) {
4015 btrfs_abort_transaction(trans, ret);
4016 err = ret;
4017 goto out_end_trans;
4018 }
4019 }
4020
Lu Fengqid1957792018-05-29 15:01:54 +08004021 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004022 BTRFS_UUID_KEY_SUBVOL,
4023 dest->root_key.objectid);
4024 if (ret && ret != -ENOENT) {
4025 btrfs_abort_transaction(trans, ret);
4026 err = ret;
4027 goto out_end_trans;
4028 }
4029 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004030 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004031 dest->root_item.received_uuid,
4032 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4033 dest->root_key.objectid);
4034 if (ret && ret != -ENOENT) {
4035 btrfs_abort_transaction(trans, ret);
4036 err = ret;
4037 goto out_end_trans;
4038 }
4039 }
4040
Qu Wenruo082b6c92020-06-16 10:17:37 +08004041 free_anon_bdev(dest->anon_dev);
4042 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004043out_end_trans:
4044 trans->block_rsv = NULL;
4045 trans->bytes_reserved = 0;
4046 ret = btrfs_end_transaction(trans);
4047 if (ret && !err)
4048 err = ret;
4049 inode->i_flags |= S_DEAD;
4050out_release:
4051 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4052out_up_write:
4053 up_write(&fs_info->subvol_sem);
4054 if (err) {
4055 spin_lock(&dest->root_item_lock);
4056 root_flags = btrfs_root_flags(&dest->root_item);
4057 btrfs_set_root_flags(&dest->root_item,
4058 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4059 spin_unlock(&dest->root_item_lock);
4060 } else {
4061 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004062 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004063 ASSERT(dest->send_in_progress == 0);
4064
4065 /* the last ref */
4066 if (dest->ino_cache_inode) {
4067 iput(dest->ino_cache_inode);
4068 dest->ino_cache_inode = NULL;
4069 }
4070 }
4071
4072 return err;
4073}
4074
Chris Mason39279cc2007-06-12 06:35:45 -04004075static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4076{
David Howells2b0143b2015-03-17 22:25:59 +00004077 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004078 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004079 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004080 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004081 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004082
David Sterbab3ae2442012-09-13 16:04:34 -06004083 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004084 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004085 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004086 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004087
Josef Bacikd52be812013-05-29 14:54:47 -04004088 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004089 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004090 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004091
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004092 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004093 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004094 goto out;
4095 }
4096
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004097 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004098 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004099 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004100
Filipe Manana44f714d2016-06-06 16:11:13 +01004101 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4102
Chris Mason39279cc2007-06-12 06:35:45 -04004103 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004104 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4105 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4106 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004107 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004108 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004109 /*
4110 * Propagate the last_unlink_trans value of the deleted dir to
4111 * its parent directory. This is to prevent an unrecoverable
4112 * log tree in the case we do something like this:
4113 * 1) create dir foo
4114 * 2) create snapshot under dir foo
4115 * 3) delete the snapshot
4116 * 4) rmdir foo
4117 * 5) mkdir foo
4118 * 6) fsync foo or some file inside foo
4119 */
4120 if (last_unlink_trans >= trans->transid)
4121 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4122 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004123out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004124 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004125 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004126
Chris Mason39279cc2007-06-12 06:35:45 -04004127 return err;
4128}
4129
Josef Bacikddfae632017-10-19 14:16:02 -04004130/*
4131 * Return this if we need to call truncate_block for the last bit of the
4132 * truncate.
4133 */
4134#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004135
Chris Mason323ac952008-10-01 19:05:46 -04004136/*
Chris Mason39279cc2007-06-12 06:35:45 -04004137 * this can truncate away extent items, csum items and directory items.
4138 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004139 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004140 *
4141 * csum items that cross the new i_size are truncated to the new size
4142 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004143 *
4144 * min_type is the minimum key type to truncate down to. If set to 0, this
4145 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004146 */
Yan, Zheng80825102009-11-12 09:35:36 +00004147int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4148 struct btrfs_root *root,
4149 struct inode *inode,
4150 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004151{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004152 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004153 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004154 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004156 struct btrfs_key key;
4157 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004158 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004159 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004160 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004161 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004162 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004163 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004164 int found_extent;
4165 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004166 int pending_del_nr = 0;
4167 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004168 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004169 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004170 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004171 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004172 bool be_nice = false;
4173 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004174 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4175 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004176
4177 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004178
Chris Mason28ed1342014-12-17 09:41:04 -08004179 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004180 * For non-free space inodes and non-shareable roots, we want to back
4181 * off from time to time. This means all inodes in subvolume roots,
4182 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004183 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004184 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004185 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004186 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004187
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004188 path = btrfs_alloc_path();
4189 if (!path)
4190 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004191 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004192
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004193 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004194 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4195 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004196
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004197 /*
4198 * We want to drop from the next block forward in case this
4199 * new size is not block aligned since we will be keeping the
4200 * last block of the extent just the way it is.
4201 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004202 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004203 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004204 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004205 }
Yan, Zheng80825102009-11-12 09:35:36 +00004206
Miao Xie16cdcec2011-04-22 18:12:22 +08004207 /*
4208 * This function is also used to drop the items in the log tree before
4209 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004210 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004211 * items.
4212 */
4213 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004214 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004215
Li Zefan33345d012011-04-20 10:31:50 +08004216 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004217 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004218 key.type = (u8)-1;
4219
Chris Mason85e21ba2008-01-29 15:11:36 -05004220search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004221 /*
4222 * with a 16K leaf size and 128MB extents, you can actually queue
4223 * up a huge file in a single leaf. Most of the time that
4224 * bytes_deleted is > 0, it will be huge by the time we get here
4225 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004226 if (be_nice && bytes_deleted > SZ_32M &&
4227 btrfs_should_end_transaction(trans)) {
4228 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004229 goto out;
4230 }
Chris Masond3977122009-01-05 21:25:51 -05004231
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004232 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4233 if (ret < 0)
4234 goto out;
4235
Chris Mason85e21ba2008-01-29 15:11:36 -05004236 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004237 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004238 /* there are no items in the tree for us to truncate, we're
4239 * done
4240 */
Yan, Zheng80825102009-11-12 09:35:36 +00004241 if (path->slots[0] == 0)
4242 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004243 path->slots[0]--;
4244 }
4245
Chris Masond3977122009-01-05 21:25:51 -05004246 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004247 u64 clear_start = 0, clear_len = 0;
4248
Chris Mason39279cc2007-06-12 06:35:45 -04004249 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004250 leaf = path->nodes[0];
4251 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004252 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004253
Li Zefan33345d012011-04-20 10:31:50 +08004254 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004255 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004256
Chris Mason85e21ba2008-01-29 15:11:36 -05004257 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004258 break;
4259
Chris Mason5f39d392007-10-15 16:14:19 -04004260 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004261 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004262 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004263 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004264 extent_type = btrfs_file_extent_type(leaf, fi);
4265 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004266 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004267 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004268
4269 trace_btrfs_truncate_show_fi_regular(
4270 BTRFS_I(inode), leaf, fi,
4271 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004272 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004273 item_end += btrfs_file_extent_ram_bytes(leaf,
4274 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004275
4276 trace_btrfs_truncate_show_fi_inline(
4277 BTRFS_I(inode), leaf, fi, path->slots[0],
4278 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004279 }
Yan008630c2007-11-07 13:31:09 -05004280 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004281 }
Yan, Zheng80825102009-11-12 09:35:36 +00004282 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004283 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004284 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004285 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004286 break;
4287 if (found_key.offset >= new_size)
4288 del_item = 1;
4289 else
4290 del_item = 0;
4291 }
Chris Mason39279cc2007-06-12 06:35:45 -04004292 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004293 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004294 if (found_type != BTRFS_EXTENT_DATA_KEY)
4295 goto delete;
4296
4297 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004298 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004299
4300 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004301 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004302 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004303 u64 orig_num_bytes =
4304 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004305 extent_num_bytes = ALIGN(new_size -
4306 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004307 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004308 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004309 btrfs_set_file_extent_num_bytes(leaf, fi,
4310 extent_num_bytes);
4311 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004312 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004313 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004314 &root->state) &&
4315 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004316 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004317 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004318 } else {
Chris Masondb945352007-10-15 16:15:53 -04004319 extent_num_bytes =
4320 btrfs_file_extent_disk_num_bytes(leaf,
4321 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004322 extent_offset = found_key.offset -
4323 btrfs_file_extent_offset(leaf, fi);
4324
Chris Mason39279cc2007-06-12 06:35:45 -04004325 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004326 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004327 if (extent_start != 0) {
4328 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004329 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004330 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004331 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004332 }
4333 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004334 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004335 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004336 /*
4337 * we can't truncate inline items that have had
4338 * special encodings
4339 */
4340 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004341 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004342 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4343 btrfs_file_extent_compression(leaf, fi) == 0) {
4344 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004345
Josef Bacikddfae632017-10-19 14:16:02 -04004346 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4347 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004348 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004349 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004350 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004351 * We have to bail so the last_size is set to
4352 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004353 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004354 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004355 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004356 } else {
4357 /*
4358 * Inline extents are special, we just treat
4359 * them as a full sector worth in the file
4360 * extent tree just for simplicity sake.
4361 */
4362 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004363 }
Josef Bacikddfae632017-10-19 14:16:02 -04004364
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004365 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004366 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004367 }
Chris Mason179e29e2007-11-01 11:28:41 -04004368delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004369 /*
4370 * We use btrfs_truncate_inode_items() to clean up log trees for
4371 * multiple fsyncs, and in this case we don't want to clear the
4372 * file extent range because it's just the log.
4373 */
4374 if (root == BTRFS_I(inode)->root) {
4375 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4376 clear_start, clear_len);
4377 if (ret) {
4378 btrfs_abort_transaction(trans, ret);
4379 break;
4380 }
4381 }
4382
Josef Bacikddfae632017-10-19 14:16:02 -04004383 if (del_item)
4384 last_size = found_key.offset;
4385 else
4386 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004387 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004388 if (!pending_del_nr) {
4389 /* no pending yet, add ourselves */
4390 pending_del_slot = path->slots[0];
4391 pending_del_nr = 1;
4392 } else if (pending_del_nr &&
4393 path->slots[0] + 1 == pending_del_slot) {
4394 /* hop on the pending chunk */
4395 pending_del_nr++;
4396 pending_del_slot = path->slots[0];
4397 } else {
Chris Masond3977122009-01-05 21:25:51 -05004398 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004399 }
Chris Mason39279cc2007-06-12 06:35:45 -04004400 } else {
4401 break;
4402 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004403 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004404
Miao Xie27cdeb72014-04-02 19:51:05 +08004405 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004406 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004407 struct btrfs_ref ref = { 0 };
4408
Chris Mason28ed1342014-12-17 09:41:04 -08004409 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004410
4411 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4412 extent_start, extent_num_bytes, 0);
4413 ref.real_root = root->root_key.objectid;
4414 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4415 ino, extent_offset);
4416 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004417 if (ret) {
4418 btrfs_abort_transaction(trans, ret);
4419 break;
4420 }
Chris Mason28f75a02015-02-04 06:59:29 -08004421 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004422 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004423 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004424 }
Chris Mason39279cc2007-06-12 06:35:45 -04004425 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004426
Yan, Zheng80825102009-11-12 09:35:36 +00004427 if (found_type == BTRFS_INODE_ITEM_KEY)
4428 break;
4429
4430 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004431 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004432 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004433 if (pending_del_nr) {
4434 ret = btrfs_del_items(trans, root, path,
4435 pending_del_slot,
4436 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004437 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004438 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004439 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004440 }
Yan, Zheng80825102009-11-12 09:35:36 +00004441 pending_del_nr = 0;
4442 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004443 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004444
Chris Mason28f75a02015-02-04 06:59:29 -08004445 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004446 * We can generate a lot of delayed refs, so we need to
4447 * throttle every once and a while and make sure we're
4448 * adding enough space to keep up with the work we are
4449 * generating. Since we hold a transaction here we
4450 * can't flush, and we don't want to FLUSH_LIMIT because
4451 * we could have generated too many delayed refs to
4452 * actually allocate, so just bail if we're short and
4453 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004454 */
Josef Bacik28bad212018-12-03 10:20:38 -05004455 if (should_throttle) {
4456 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4457 BTRFS_RESERVE_NO_FLUSH);
4458 if (ret) {
4459 ret = -EAGAIN;
4460 break;
4461 }
Chris Mason28f75a02015-02-04 06:59:29 -08004462 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004463 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004464 } else {
4465 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004466 }
Chris Mason39279cc2007-06-12 06:35:45 -04004467 }
Yan, Zheng80825102009-11-12 09:35:36 +00004468out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004469 if (ret >= 0 && pending_del_nr) {
4470 int err;
4471
4472 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004473 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004474 if (err) {
4475 btrfs_abort_transaction(trans, err);
4476 ret = err;
4477 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004478 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004479 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4480 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004481 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004482 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004483 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004484 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4485 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004486 }
Chris Mason28ed1342014-12-17 09:41:04 -08004487
Chris Mason39279cc2007-06-12 06:35:45 -04004488 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004489 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004490}
4491
Chris Masona52d9a82007-08-27 16:49:44 -04004492/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304493 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004494 * @inode - inode that we're zeroing
4495 * @from - the offset to start zeroing
4496 * @len - the length to zero, 0 to zero the entire range respective to the
4497 * offset
4498 * @front - zero up to the offset instead of from the offset on
4499 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304500 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004501 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004502 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304503int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004504 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004505{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004506 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004507 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004508 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4509 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004510 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004511 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004512 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004513 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004514 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004515 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304516 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004517 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004518 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004519 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004520 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304521 u64 block_start;
4522 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004523
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004524 if (IS_ALIGNED(offset, blocksize) &&
4525 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004526 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304527
Josef Bacik8b62f872017-10-19 14:15:55 -04004528 block_start = round_down(from, blocksize);
4529 block_end = block_start + blocksize - 1;
4530
Nikolay Borisov36ea6f32020-06-03 08:55:41 +03004531 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved,
4532 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004533 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004534 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4535 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004536 /* For nocow case, no need to reserve data space */
4537 only_release_metadata = true;
4538 } else {
4539 goto out;
4540 }
4541 }
4542 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4543 if (ret < 0) {
4544 if (!only_release_metadata)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03004545 btrfs_free_reserved_data_space(BTRFS_I(inode),
4546 data_reserved, block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004547 goto out;
4548 }
Chris Mason211c17f2008-05-15 09:13:45 -04004549again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004550 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004551 if (!page) {
Nikolay Borisov86d52922020-06-03 08:55:40 +03004552 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004553 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004554 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004555 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004556 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004557 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004558
Chris Masona52d9a82007-08-27 16:49:44 -04004559 if (!PageUptodate(page)) {
4560 ret = btrfs_readpage(NULL, page);
4561 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004562 if (page->mapping != mapping) {
4563 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004564 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004565 goto again;
4566 }
Chris Masona52d9a82007-08-27 16:49:44 -04004567 if (!PageUptodate(page)) {
4568 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004569 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004570 }
4571 }
Chris Mason211c17f2008-05-15 09:13:45 -04004572 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004573
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304574 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004575 set_page_extent_mapped(page);
4576
Nikolay Borisovc3504372020-06-03 08:55:03 +03004577 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004578 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304579 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004580 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004581 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004582 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004583 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004584 btrfs_put_ordered_extent(ordered);
4585 goto again;
4586 }
4587
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304588 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004589 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4590 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004591
Nikolay Borisovc2566f22020-06-03 08:55:35 +03004592 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004593 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004594 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304595 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004596 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004597 goto out_unlock;
4598 }
4599
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304600 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004601 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304602 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004603 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004604 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304605 memset(kaddr + (block_start - page_offset(page)),
4606 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004607 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304608 memset(kaddr + (block_start - page_offset(page)) + offset,
4609 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004610 flush_dcache_page(page);
4611 kunmap(page);
4612 }
Chris Mason247e7432008-07-17 12:53:51 -04004613 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004614 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004615 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004616
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004617 if (only_release_metadata)
4618 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4619 block_end, EXTENT_NORESERVE, NULL, NULL,
4620 GFP_NOFS);
4621
Chris Mason89642222008-07-24 09:41:53 -04004622out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004623 if (ret) {
4624 if (only_release_metadata)
4625 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4626 blocksize, true);
4627 else
Nikolay Borisov86d52922020-06-03 08:55:40 +03004628 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004629 block_start, blocksize, true);
4630 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004631 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004632 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004633 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004634out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004635 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004636 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004637 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004638 return ret;
4639}
4640
Josef Bacik16e75492013-10-22 12:18:51 -04004641static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4642 u64 offset, u64 len)
4643{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004644 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004645 struct btrfs_trans_handle *trans;
4646 int ret;
4647
4648 /*
4649 * Still need to make sure the inode looks like it's been updated so
4650 * that any holes get logged if we fsync.
4651 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004652 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4653 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004654 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4655 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4656 return 0;
4657 }
4658
4659 /*
4660 * 1 - for the one we're dropping
4661 * 1 - for the one we're adding
4662 * 1 - for updating the inode.
4663 */
4664 trans = btrfs_start_transaction(root, 3);
4665 if (IS_ERR(trans))
4666 return PTR_ERR(trans);
4667
4668 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4669 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004670 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004671 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004672 return ret;
4673 }
4674
David Sterbaf85b7372017-01-20 14:54:07 +01004675 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4676 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004677 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004678 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004679 else
4680 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004681 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004682 return ret;
4683}
4684
Josef Bacik695a0d02011-03-04 15:46:53 -05004685/*
4686 * This function puts in dummy file extents for the area we're creating a hole
4687 * for. So if we are truncating this file to a larger size we need to insert
4688 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4689 * the range between oldsize and size
4690 */
Josef Bacika41ad392011-01-31 15:30:16 -05004691int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004692{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004693 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004694 struct btrfs_root *root = BTRFS_I(inode)->root;
4695 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004696 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004697 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004698 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004699 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4700 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004701 u64 last_byte;
4702 u64 cur_offset;
4703 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004704 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004705
Josef Bacika71754f2013-06-17 17:14:39 -04004706 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304707 * If our size started in the middle of a block we need to zero out the
4708 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004709 * expose stale data.
4710 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304711 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004712 if (err)
4713 return err;
4714
Yan Zheng9036c102008-10-30 14:19:41 -04004715 if (size <= hole_start)
4716 return 0;
4717
David Sterbab272ae22020-02-05 19:09:33 +01004718 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004719 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004720 cur_offset = hole_start;
4721 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004722 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004723 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004724 if (IS_ERR(em)) {
4725 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004726 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004727 break;
4728 }
Yan Zheng9036c102008-10-30 14:19:41 -04004729 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004730 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004731 hole_size = last_byte - cur_offset;
4732
Yan, Zheng80825102009-11-12 09:35:36 +00004733 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004734 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004735
Josef Bacik16e75492013-10-22 12:18:51 -04004736 err = maybe_insert_hole(root, inode, cur_offset,
4737 hole_size);
4738 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004739 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004740
4741 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4742 cur_offset, hole_size);
4743 if (err)
4744 break;
4745
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004746 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004747 cur_offset + hole_size - 1, 0);
4748 hole_em = alloc_extent_map();
4749 if (!hole_em) {
4750 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4751 &BTRFS_I(inode)->runtime_flags);
4752 goto next;
4753 }
4754 hole_em->start = cur_offset;
4755 hole_em->len = hole_size;
4756 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004757
Josef Bacik5dc562c2012-08-17 13:14:17 -04004758 hole_em->block_start = EXTENT_MAP_HOLE;
4759 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004760 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004761 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004762 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004763 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004764
4765 while (1) {
4766 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004767 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004768 write_unlock(&em_tree->lock);
4769 if (err != -EEXIST)
4770 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004771 btrfs_drop_extent_cache(BTRFS_I(inode),
4772 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004773 cur_offset +
4774 hole_size - 1, 0);
4775 }
4776 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004777 } else {
4778 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4779 cur_offset, hole_size);
4780 if (err)
4781 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004782 }
Josef Bacik16e75492013-10-22 12:18:51 -04004783next:
Yan Zheng9036c102008-10-30 14:19:41 -04004784 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004785 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004786 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004787 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004788 break;
4789 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004790 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004791 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004792 return err;
4793}
4794
Eric Sandeen3972f262013-01-12 02:57:22 +00004795static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004796{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004797 struct btrfs_root *root = BTRFS_I(inode)->root;
4798 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004799 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004800 loff_t newsize = attr->ia_size;
4801 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004802 int ret;
4803
Eric Sandeen3972f262013-01-12 02:57:22 +00004804 /*
4805 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4806 * special case where we need to update the times despite not having
4807 * these flags set. For all other operations the VFS set these flags
4808 * explicitly if it wants a timestamp update.
4809 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004810 if (newsize != oldsize) {
4811 inode_inc_iversion(inode);
4812 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4813 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004814 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004815 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004816
Josef Bacika41ad392011-01-31 15:30:16 -05004817 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004818 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004819 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004820 * This is to ensure the snapshot captures a fully consistent
4821 * state of this file - if the snapshot captures this expanding
4822 * truncation, it must capture all writes that happened before
4823 * this truncation.
4824 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004825 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004826 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004827 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004828 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004829 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004830 }
Yan, Zheng80825102009-11-12 09:35:36 +00004831
Miao Xief4a2f4c2011-12-14 20:12:01 -05004832 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004833 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004834 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004835 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004836 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004837
4838 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004839 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304840 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004841 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004842 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004843 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004844 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004845
Josef Bacika41ad392011-01-31 15:30:16 -05004846 /*
4847 * We're truncating a file that used to have good data down to
4848 * zero. Make sure it gets into the ordered flush list so that
4849 * any new writes get down to disk quickly.
4850 */
4851 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004852 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4853 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004854
Josef Bacika41ad392011-01-31 15:30:16 -05004855 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004856
David Sterba8e0fa5d2020-06-09 19:21:48 +02004857 /* Disable nonlocked read DIO to avoid the endless truncate */
4858 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004859 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004860 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004861
Filipe Manana213e8c52018-02-06 20:40:31 +00004862 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004863 if (ret && inode->i_nlink) {
4864 int err;
4865
4866 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004867 * Truncate failed, so fix up the in-memory size. We
4868 * adjusted disk_i_size down as we removed extents, so
4869 * wait for disk_i_size to be stable and then update the
4870 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004871 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004872 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004873 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004874 return err;
4875 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004876 }
Yan, Zheng80825102009-11-12 09:35:36 +00004877 }
4878
Josef Bacika41ad392011-01-31 15:30:16 -05004879 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004880}
4881
Chris Mason39279cc2007-06-12 06:35:45 -04004882static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4883{
David Howells2b0143b2015-03-17 22:25:59 +00004884 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004885 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004886 int err;
4887
Li Zefanb83cc962010-12-20 16:04:08 +08004888 if (btrfs_root_readonly(root))
4889 return -EROFS;
4890
Jan Kara31051c82016-05-26 16:55:18 +02004891 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004892 if (err)
4893 return err;
4894
Chris Mason5a3f23d2009-03-31 13:27:11 -04004895 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004896 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004897 if (err)
4898 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004899 }
Yan Zheng9036c102008-10-30 14:19:41 -04004900
Christoph Hellwig10257742010-06-04 11:30:02 +02004901 if (attr->ia_valid) {
4902 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004903 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004904 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004905
Josef Bacik22c44fe2011-11-30 10:45:38 -05004906 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004907 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004908 }
4909
Chris Mason39279cc2007-06-12 06:35:45 -04004910 return err;
4911}
Chris Mason61295eb2008-01-14 16:24:38 -05004912
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004913/*
4914 * While truncating the inode pages during eviction, we get the VFS calling
4915 * btrfs_invalidatepage() against each page of the inode. This is slow because
4916 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4917 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4918 * extent_state structures over and over, wasting lots of time.
4919 *
4920 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4921 * those expensive operations on a per page basis and do only the ordered io
4922 * finishing, while we release here the extent_map and extent_state structures,
4923 * without the excessive merging and splitting.
4924 */
4925static void evict_inode_truncate_pages(struct inode *inode)
4926{
4927 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4928 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4929 struct rb_node *node;
4930
4931 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004932 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004933
4934 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004935 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004936 struct extent_map *em;
4937
Liu Bo07e1ce02018-08-23 03:51:52 +08004938 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004939 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004940 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4941 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004942 remove_extent_mapping(map_tree, em);
4943 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004944 if (need_resched()) {
4945 write_unlock(&map_tree->lock);
4946 cond_resched();
4947 write_lock(&map_tree->lock);
4948 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004949 }
4950 write_unlock(&map_tree->lock);
4951
Filipe Manana6ca07092015-05-26 00:55:42 +01004952 /*
4953 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004954 * We can have ongoing bios started by readahead that have
4955 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004956 * still in progress (unlocked the pages in the bio but did not yet
4957 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004958 * ranges can still be locked and eviction started because before
4959 * submitting those bios, which are executed by a separate task (work
4960 * queue kthread), inode references (inode->i_count) were not taken
4961 * (which would be dropped in the end io callback of each bio).
4962 * Therefore here we effectively end up waiting for those bios and
4963 * anyone else holding locked ranges without having bumped the inode's
4964 * reference count - if we don't do it, when they access the inode's
4965 * io_tree to unlock a range it may be too late, leading to an
4966 * use-after-free issue.
4967 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004968 spin_lock(&io_tree->lock);
4969 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4970 struct extent_state *state;
4971 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004972 u64 start;
4973 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004974 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004975
4976 node = rb_first(&io_tree->state);
4977 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004978 start = state->start;
4979 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004980 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004981 spin_unlock(&io_tree->lock);
4982
David Sterbaff13db42015-12-03 14:30:40 +01004983 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004984
4985 /*
4986 * If still has DELALLOC flag, the extent didn't reach disk,
4987 * and its reserved space won't be freed by delayed_ref.
4988 * So we need to free its reserved space here.
4989 * (Refer to comment in btrfs_invalidatepage, case 2)
4990 *
4991 * Note, end is the bytenr of last byte, so we need + 1 here.
4992 */
Filipe Manana421f0922018-10-12 13:02:48 +01004993 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03004994 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
4995 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004996
Filipe Manana6ca07092015-05-26 00:55:42 +01004997 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004998 EXTENT_LOCKED | EXTENT_DELALLOC |
4999 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5000 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005001
Filipe Manana7064dd52014-08-08 02:47:05 +01005002 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005003 spin_lock(&io_tree->lock);
5004 }
5005 spin_unlock(&io_tree->lock);
5006}
5007
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005008static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005009 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005010{
5011 struct btrfs_fs_info *fs_info = root->fs_info;
5012 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005013 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005014 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005015 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005016
Josef Bacikd3984c92019-08-01 18:19:37 -04005017 /*
5018 * Eviction should be taking place at some place safe because of our
5019 * delayed iputs. However the normal flushing code will run delayed
5020 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5021 *
5022 * We reserve the delayed_refs_extra here again because we can't use
5023 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5024 * above. We reserve our extra bit here because we generate a ton of
5025 * delayed refs activity by truncating.
5026 *
5027 * If we cannot make our reservation we'll attempt to steal from the
5028 * global reserve, because we really want to be able to free up space.
5029 */
5030 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5031 BTRFS_RESERVE_FLUSH_EVICT);
5032 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005033 /*
5034 * Try to steal from the global reserve if there is space for
5035 * it.
5036 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005037 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5038 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5039 btrfs_warn(fs_info,
5040 "could not allocate space for delete; will truncate on mount");
5041 return ERR_PTR(-ENOSPC);
5042 }
5043 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005044 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005045
5046 trans = btrfs_join_transaction(root);
5047 if (IS_ERR(trans))
5048 return trans;
5049
5050 if (delayed_refs_extra) {
5051 trans->block_rsv = &fs_info->trans_block_rsv;
5052 trans->bytes_reserved = delayed_refs_extra;
5053 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5054 delayed_refs_extra, 1);
5055 }
5056 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005057}
5058
Al Virobd555972010-06-07 11:35:40 -04005059void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005060{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005061 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005062 struct btrfs_trans_handle *trans;
5063 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005064 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005065 int ret;
5066
liubo1abe9b82011-03-24 11:18:59 +00005067 trace_btrfs_inode_evict(inode);
5068
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005069 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005070 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005071 return;
5072 }
5073
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005074 evict_inode_truncate_pages(inode);
5075
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005076 if (inode->i_nlink &&
5077 ((btrfs_root_refs(&root->root_item) != 0 &&
5078 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005079 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005080 goto no_delete;
5081
Omar Sandoval27919062018-05-11 13:13:37 -07005082 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005083 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005084
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005085 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005086
Omar Sandoval7b40b692018-05-11 13:13:33 -07005087 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005088 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005089
Yan, Zheng76dda932009-09-21 16:00:26 -04005090 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005091 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5092 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005093 goto no_delete;
5094 }
5095
Nikolay Borisovaa790212017-01-10 20:35:40 +02005096 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005097 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005098 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005099
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005100 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005101 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005102 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005103 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005104 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005105
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005106 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005107
Yan, Zheng80825102009-11-12 09:35:36 +00005108 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005109 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005110 if (IS_ERR(trans))
5111 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005112
5113 trans->block_rsv = rsv;
5114
Yan, Zhengd68fc572010-05-16 10:49:58 -04005115 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005116 trans->block_rsv = &fs_info->trans_block_rsv;
5117 btrfs_end_transaction(trans);
5118 btrfs_btree_balance_dirty(fs_info);
5119 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5120 goto free_rsv;
5121 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005122 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005123 }
5124
Josef Bacik4ef31a42013-08-13 14:10:08 -04005125 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005126 * Errors here aren't a big deal, it just means we leave orphan items in
5127 * the tree. They will be cleaned up on the next mount. If the inode
5128 * number gets reused, cleanup deletes the orphan item without doing
5129 * anything, and unlink reuses the existing orphan item.
5130 *
5131 * If it turns out that we are dropping too many of these, we might want
5132 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005133 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005134 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005135 if (!IS_ERR(trans)) {
5136 trans->block_rsv = rsv;
5137 btrfs_orphan_del(trans, BTRFS_I(inode));
5138 trans->block_rsv = &fs_info->trans_block_rsv;
5139 btrfs_end_transaction(trans);
5140 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005141
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005142 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005143 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005144 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005145
Omar Sandoval27919062018-05-11 13:13:37 -07005146free_rsv:
5147 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005148no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005149 /*
5150 * If we didn't successfully delete, the orphan item will still be in
5151 * the tree and we'll retry on the next mount. Again, we might also want
5152 * to retry these periodically in the future.
5153 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005154 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005155 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005156}
5157
5158/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005159 * Return the key found in the dir entry in the location pointer, fill @type
5160 * with BTRFS_FT_*, and return 0.
5161 *
Su Yue005d6712018-03-05 17:13:37 +08005162 * If no dir entries were found, returns -ENOENT.
5163 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005164 */
5165static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005166 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005167{
5168 const char *name = dentry->d_name.name;
5169 int namelen = dentry->d_name.len;
5170 struct btrfs_dir_item *di;
5171 struct btrfs_path *path;
5172 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005173 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005174
5175 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005176 if (!path)
5177 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005178
David Sterbaf85b7372017-01-20 14:54:07 +01005179 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5180 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005181 if (IS_ERR_OR_NULL(di)) {
5182 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005183 goto out;
5184 }
Chris Masond3977122009-01-05 21:25:51 -05005185
Chris Mason5f39d392007-10-15 16:14:19 -04005186 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005187 if (location->type != BTRFS_INODE_ITEM_KEY &&
5188 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005189 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005190 btrfs_warn(root->fs_info,
5191"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5192 __func__, name, btrfs_ino(BTRFS_I(dir)),
5193 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005194 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005195 if (!ret)
5196 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005197out:
Chris Mason39279cc2007-06-12 06:35:45 -04005198 btrfs_free_path(path);
5199 return ret;
5200}
5201
5202/*
5203 * when we hit a tree root in a directory, the btrfs part of the inode
5204 * needs to be changed to reflect the root directory of the tree root. This
5205 * is kind of like crossing a mount point.
5206 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005207static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005208 struct inode *dir,
5209 struct dentry *dentry,
5210 struct btrfs_key *location,
5211 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005212{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005213 struct btrfs_path *path;
5214 struct btrfs_root *new_root;
5215 struct btrfs_root_ref *ref;
5216 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005217 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005218 int ret;
5219 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005220
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005221 path = btrfs_alloc_path();
5222 if (!path) {
5223 err = -ENOMEM;
5224 goto out;
5225 }
Chris Mason39279cc2007-06-12 06:35:45 -04005226
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005227 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005228 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5229 key.type = BTRFS_ROOT_REF_KEY;
5230 key.offset = location->objectid;
5231
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005232 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005233 if (ret) {
5234 if (ret < 0)
5235 err = ret;
5236 goto out;
5237 }
Chris Mason39279cc2007-06-12 06:35:45 -04005238
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005239 leaf = path->nodes[0];
5240 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005241 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005242 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5243 goto out;
5244
5245 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5246 (unsigned long)(ref + 1),
5247 dentry->d_name.len);
5248 if (ret)
5249 goto out;
5250
David Sterbab3b4aa72011-04-21 01:20:15 +02005251 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005252
David Sterba56e93572020-05-15 19:35:55 +02005253 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005254 if (IS_ERR(new_root)) {
5255 err = PTR_ERR(new_root);
5256 goto out;
5257 }
5258
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005259 *sub_root = new_root;
5260 location->objectid = btrfs_root_dirid(&new_root->root_item);
5261 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005262 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005263 err = 0;
5264out:
5265 btrfs_free_path(path);
5266 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005267}
5268
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005269static void inode_tree_add(struct inode *inode)
5270{
5271 struct btrfs_root *root = BTRFS_I(inode)->root;
5272 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005273 struct rb_node **p;
5274 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005275 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005276 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005277
Al Viro1d3382cb2010-10-23 15:19:20 -04005278 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005279 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005280 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005281 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005282 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005283 while (*p) {
5284 parent = *p;
5285 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5286
David Sterba37508512018-06-29 10:56:40 +02005287 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005288 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005289 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005290 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005291 else {
5292 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005293 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005294 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005295 RB_CLEAR_NODE(parent);
5296 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005297 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005298 }
5299 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005300 rb_link_node(new, parent, p);
5301 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005302 spin_unlock(&root->inode_lock);
5303}
5304
5305static void inode_tree_del(struct inode *inode)
5306{
5307 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005308 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005309
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005310 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005311 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005312 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005313 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005314 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005315 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005316 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005317
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005318 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005319 spin_lock(&root->inode_lock);
5320 empty = RB_EMPTY_ROOT(&root->inode_tree);
5321 spin_unlock(&root->inode_lock);
5322 if (empty)
5323 btrfs_add_dead_root(root);
5324 }
5325}
5326
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005327
Chris Masone02119d2008-09-05 16:13:11 -04005328static int btrfs_init_locked_inode(struct inode *inode, void *p)
5329{
5330 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005331
5332 inode->i_ino = args->ino;
5333 BTRFS_I(inode)->location.objectid = args->ino;
5334 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5335 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005336 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5337 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005338 return 0;
5339}
5340
5341static int btrfs_find_actor(struct inode *inode, void *opaque)
5342{
5343 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005344
5345 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005346 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005347}
5348
David Sterba0202e832020-05-15 19:35:59 +02005349static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005350 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005351{
5352 struct inode *inode;
5353 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005354 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005355
David Sterba0202e832020-05-15 19:35:59 +02005356 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005357 args.root = root;
5358
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005359 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005360 btrfs_init_locked_inode,
5361 (void *)&args);
5362 return inode;
5363}
5364
David Sterba4c66e0d2019-10-03 19:09:35 +02005365/*
David Sterba0202e832020-05-15 19:35:59 +02005366 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005367 * Path can be preallocated to prevent recursing back to iget through
5368 * allocator. NULL is also valid but may require an additional allocation
5369 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305370 */
David Sterba0202e832020-05-15 19:35:59 +02005371struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005372 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305373{
5374 struct inode *inode;
5375
David Sterba0202e832020-05-15 19:35:59 +02005376 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305377 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005378 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305379
5380 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005381 int ret;
5382
Filipe Manana4222ea72018-10-24 10:13:03 +01005383 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005384 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005385 inode_tree_add(inode);
5386 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005387 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005388 iget_failed(inode);
5389 /*
5390 * ret > 0 can come from btrfs_search_slot called by
5391 * btrfs_read_locked_inode, this means the inode item
5392 * was not found.
5393 */
5394 if (ret > 0)
5395 ret = -ENOENT;
5396 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005397 }
5398 }
5399
Balaji Rao1a54ef82008-07-21 02:01:04 +05305400 return inode;
5401}
5402
David Sterba0202e832020-05-15 19:35:59 +02005403struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005404{
David Sterba0202e832020-05-15 19:35:59 +02005405 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005406}
5407
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005408static struct inode *new_simple_dir(struct super_block *s,
5409 struct btrfs_key *key,
5410 struct btrfs_root *root)
5411{
5412 struct inode *inode = new_inode(s);
5413
5414 if (!inode)
5415 return ERR_PTR(-ENOMEM);
5416
Josef Bacik5c8fd992020-02-14 16:11:43 -05005417 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005418 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005419 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005420
5421 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005422 /*
5423 * We only need lookup, the rest is read-only and there's no inode
5424 * associated with the dentry
5425 */
5426 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005427 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005428 inode->i_fop = &simple_dir_operations;
5429 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005430 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305431 inode->i_atime = inode->i_mtime;
5432 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005433 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005434
5435 return inode;
5436}
5437
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005438static inline u8 btrfs_inode_type(struct inode *inode)
5439{
5440 /*
5441 * Compile-time asserts that generic FT_* types still match
5442 * BTRFS_FT_* types
5443 */
5444 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5445 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5446 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5447 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5448 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5449 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5450 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5451 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5452
5453 return fs_umode_to_ftype(inode->i_mode);
5454}
5455
Chris Mason3de45862008-11-17 21:02:50 -05005456struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005457{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005458 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005459 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005460 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005461 struct btrfs_root *sub_root = root;
5462 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005463 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005464 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005465
5466 if (dentry->d_name.len > BTRFS_NAME_LEN)
5467 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005468
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005469 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005470 if (ret < 0)
5471 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005472
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005473 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005474 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005475 if (IS_ERR(inode))
5476 return inode;
5477
5478 /* Do extra check against inode mode with di_type */
5479 if (btrfs_inode_type(inode) != di_type) {
5480 btrfs_crit(fs_info,
5481"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5482 inode->i_mode, btrfs_inode_type(inode),
5483 di_type);
5484 iput(inode);
5485 return ERR_PTR(-EUCLEAN);
5486 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005487 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005488 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005490 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005491 &location, &sub_root);
5492 if (ret < 0) {
5493 if (ret != -ENOENT)
5494 inode = ERR_PTR(ret);
5495 else
5496 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5497 } else {
David Sterba0202e832020-05-15 19:35:59 +02005498 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005499 }
Josef Bacik87270022020-01-24 09:32:31 -05005500 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005501 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005502
Julia Lawall34d19ba2011-01-24 19:55:19 +00005503 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005504 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005505 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005506 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005507 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005508 if (ret) {
5509 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005510 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005511 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005512 }
5513
Chris Mason3de45862008-11-17 21:02:50 -05005514 return inode;
5515}
5516
Nick Pigginfe15ce42011-01-07 17:49:23 +11005517static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005518{
5519 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005520 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005521
Li Zefan848cce02012-02-21 17:04:28 +08005522 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005523 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005524
Li Zefan848cce02012-02-21 17:04:28 +08005525 if (inode) {
5526 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005527 if (btrfs_root_refs(&root->root_item) == 0)
5528 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005529
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005530 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005531 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005532 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005533 return 0;
5534}
5535
Chris Mason3de45862008-11-17 21:02:50 -05005536static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005537 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005538{
Al Viro3837d202018-10-10 16:38:27 -04005539 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005540
Al Viro3837d202018-10-10 16:38:27 -04005541 if (inode == ERR_PTR(-ENOENT))
5542 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005543 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005544}
5545
Josef Bacik23b5ec72017-07-24 15:14:25 -04005546/*
5547 * All this infrastructure exists because dir_emit can fault, and we are holding
5548 * the tree lock when doing readdir. For now just allocate a buffer and copy
5549 * our information into that, and then dir_emit from the buffer. This is
5550 * similar to what NFS does, only we don't keep the buffer around in pagecache
5551 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5552 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5553 * tree lock.
5554 */
5555static int btrfs_opendir(struct inode *inode, struct file *file)
5556{
5557 struct btrfs_file_private *private;
5558
5559 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5560 if (!private)
5561 return -ENOMEM;
5562 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5563 if (!private->filldir_buf) {
5564 kfree(private);
5565 return -ENOMEM;
5566 }
5567 file->private_data = private;
5568 return 0;
5569}
5570
5571struct dir_entry {
5572 u64 ino;
5573 u64 offset;
5574 unsigned type;
5575 int name_len;
5576};
5577
5578static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5579{
5580 while (entries--) {
5581 struct dir_entry *entry = addr;
5582 char *name = (char *)(entry + 1);
5583
David Sterba92d32172018-04-16 21:10:14 +02005584 ctx->pos = get_unaligned(&entry->offset);
5585 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5586 get_unaligned(&entry->ino),
5587 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005588 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005589 addr += sizeof(struct dir_entry) +
5590 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005591 ctx->pos++;
5592 }
5593 return 0;
5594}
5595
Al Viro9cdda8d2013-05-22 16:48:09 -04005596static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005597{
Al Viro9cdda8d2013-05-22 16:48:09 -04005598 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005599 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005600 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005601 struct btrfs_dir_item *di;
5602 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005603 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005604 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005605 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005606 struct list_head ins_list;
5607 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005608 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005609 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005610 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005611 char *name_ptr;
5612 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005613 int entries = 0;
5614 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005615 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005616 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005617
Al Viro9cdda8d2013-05-22 16:48:09 -04005618 if (!dir_emit_dots(file, ctx))
5619 return 0;
5620
David Woodhouse49593bf2008-08-17 17:08:36 +01005621 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005622 if (!path)
5623 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005624
Josef Bacik23b5ec72017-07-24 15:14:25 -04005625 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005626 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005627
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005628 INIT_LIST_HEAD(&ins_list);
5629 INIT_LIST_HEAD(&del_list);
5630 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005631
Josef Bacik23b5ec72017-07-24 15:14:25 -04005632again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005633 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005634 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005635 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005636
Chris Mason39279cc2007-06-12 06:35:45 -04005637 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5638 if (ret < 0)
5639 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005640
5641 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005642 struct dir_entry *entry;
5643
Chris Mason5f39d392007-10-15 16:14:19 -04005644 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005645 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005646 if (slot >= btrfs_header_nritems(leaf)) {
5647 ret = btrfs_next_leaf(root, path);
5648 if (ret < 0)
5649 goto err;
5650 else if (ret > 0)
5651 break;
5652 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005653 }
Chris Mason3de45862008-11-17 21:02:50 -05005654
Chris Mason5f39d392007-10-15 16:14:19 -04005655 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5656
5657 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005658 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005659 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005660 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005661 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005662 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005663 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005664 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005665 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005666 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005667 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5668 PAGE_SIZE) {
5669 btrfs_release_path(path);
5670 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5671 if (ret)
5672 goto nopos;
5673 addr = private->filldir_buf;
5674 entries = 0;
5675 total_len = 0;
5676 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005677 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005678
5679 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005680 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005681 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005682 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5683 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005684 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005685 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005686 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005687 put_unaligned(location.objectid, &entry->ino);
5688 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005689 entries++;
5690 addr += sizeof(struct dir_entry) + name_len;
5691 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005692next:
5693 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005694 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005695 btrfs_release_path(path);
5696
5697 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5698 if (ret)
5699 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005700
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005701 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005702 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005703 goto nopos;
5704
Zach Browndb62efb2013-07-11 16:19:42 -07005705 /*
5706 * Stop new entries from being returned after we return the last
5707 * entry.
5708 *
5709 * New directory entries are assigned a strictly increasing
5710 * offset. This means that new entries created during readdir
5711 * are *guaranteed* to be seen in the future by that readdir.
5712 * This has broken buggy programs which operate on names as
5713 * they're returned by readdir. Until we re-use freed offsets
5714 * we have this hack to stop new entries from being returned
5715 * under the assumption that they'll never reach this huge
5716 * offset.
5717 *
5718 * This is being careful not to overflow 32bit loff_t unless the
5719 * last entry requires it because doing so has broken 32bit apps
5720 * in the past.
5721 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005722 if (ctx->pos >= INT_MAX)
5723 ctx->pos = LLONG_MAX;
5724 else
5725 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005726nopos:
5727 ret = 0;
5728err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005729 if (put)
5730 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005731 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005732 return ret;
5733}
5734
Chris Mason39279cc2007-06-12 06:35:45 -04005735/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005736 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005737 * inode changes. But, it is most likely to find the inode in cache.
5738 * FIXME, needs more benchmarking...there are no reasons other than performance
5739 * to keep or drop this code.
5740 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005741static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005742{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005743 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005744 struct btrfs_root *root = BTRFS_I(inode)->root;
5745 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005746 int ret;
5747
Josef Bacik72ac3c02012-05-23 14:13:11 -04005748 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005749 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005750
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005751 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005752 if (IS_ERR(trans))
5753 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005754
5755 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005756 if (ret && ret == -ENOSPC) {
5757 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005758 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005759 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005760 if (IS_ERR(trans))
5761 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005762
Chris Mason94b60442010-05-26 11:02:00 -04005763 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005764 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005765 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005766 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005767 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005768
5769 return ret;
5770}
5771
5772/*
5773 * This is a copy of file_update_time. We need this so we can return error on
5774 * ENOSPC for updating the inode in the case of file write and mmap writes.
5775 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005776static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005777 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005778{
Alexander Block2bc5565282012-06-15 09:49:33 +02005779 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005780 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005781
5782 if (btrfs_root_readonly(root))
5783 return -EROFS;
5784
Josef Bacike41f9412012-03-26 09:46:47 -04005785 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005786 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005787 if (flags & S_CTIME)
5788 inode->i_ctime = *now;
5789 if (flags & S_MTIME)
5790 inode->i_mtime = *now;
5791 if (flags & S_ATIME)
5792 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005793 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005794}
5795
Chris Masond352ac62008-09-29 15:18:18 -04005796/*
5797 * find the highest existing sequence number in a directory
5798 * and then set the in-memory index_cnt variable to reflect
5799 * free sequence numbers
5800 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005801static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005802{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005803 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005804 struct btrfs_key key, found_key;
5805 struct btrfs_path *path;
5806 struct extent_buffer *leaf;
5807 int ret;
5808
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005809 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005810 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005811 key.offset = (u64)-1;
5812
5813 path = btrfs_alloc_path();
5814 if (!path)
5815 return -ENOMEM;
5816
5817 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5818 if (ret < 0)
5819 goto out;
5820 /* FIXME: we should be able to handle this */
5821 if (ret == 0)
5822 goto out;
5823 ret = 0;
5824
5825 /*
5826 * MAGIC NUMBER EXPLANATION:
5827 * since we search a directory based on f_pos we have to start at 2
5828 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5829 * else has to start at 2
5830 */
5831 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005832 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005833 goto out;
5834 }
5835
5836 path->slots[0]--;
5837
5838 leaf = path->nodes[0];
5839 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5840
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005841 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005842 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005843 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005844 goto out;
5845 }
5846
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005847 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005848out:
5849 btrfs_free_path(path);
5850 return ret;
5851}
5852
Chris Masond352ac62008-09-29 15:18:18 -04005853/*
5854 * helper to find a free sequence number in a given directory. This current
5855 * code is very simple, later versions will do smarter things in the btree
5856 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005857int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005858{
5859 int ret = 0;
5860
Nikolay Borisov877574e2017-02-20 13:50:33 +02005861 if (dir->index_cnt == (u64)-1) {
5862 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005863 if (ret) {
5864 ret = btrfs_set_inode_index_count(dir);
5865 if (ret)
5866 return ret;
5867 }
Josef Bacikaec74772008-07-24 12:12:38 -04005868 }
5869
Nikolay Borisov877574e2017-02-20 13:50:33 +02005870 *index = dir->index_cnt;
5871 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005872
5873 return ret;
5874}
5875
Chris Masonb0d5d102014-09-08 13:08:51 -07005876static int btrfs_insert_inode_locked(struct inode *inode)
5877{
5878 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005879
5880 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005881 args.root = BTRFS_I(inode)->root;
5882
5883 return insert_inode_locked4(inode,
5884 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5885 btrfs_find_actor, &args);
5886}
5887
Anand Jain19aee8d2017-07-18 17:37:05 +08005888/*
5889 * Inherit flags from the parent inode.
5890 *
5891 * Currently only the compression flags and the cow flags are inherited.
5892 */
5893static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5894{
5895 unsigned int flags;
5896
5897 if (!dir)
5898 return;
5899
5900 flags = BTRFS_I(dir)->flags;
5901
5902 if (flags & BTRFS_INODE_NOCOMPRESS) {
5903 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5904 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5905 } else if (flags & BTRFS_INODE_COMPRESS) {
5906 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5907 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5908 }
5909
5910 if (flags & BTRFS_INODE_NODATACOW) {
5911 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5912 if (S_ISREG(inode->i_mode))
5913 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5914 }
5915
David Sterba7b6a2212018-03-26 18:40:21 +02005916 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005917}
5918
Chris Mason39279cc2007-06-12 06:35:45 -04005919static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5920 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005921 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005922 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005923 u64 ref_objectid, u64 objectid,
5924 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005925{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005926 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005927 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005928 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005929 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005930 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005931 struct btrfs_inode_ref *ref;
5932 struct btrfs_key key[2];
5933 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005934 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005935 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005936 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005937 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005938
Chris Mason5f39d392007-10-15 16:14:19 -04005939 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005940 if (!path)
5941 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005942
Josef Bacik11a19a92019-09-09 10:12:04 -04005943 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005944 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005945 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005946 if (!inode) {
5947 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005948 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005949 }
Chris Mason39279cc2007-06-12 06:35:45 -04005950
Li Zefan581bb052011-04-20 10:06:11 +08005951 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005952 * O_TMPFILE, set link count to 0, so that after this point,
5953 * we fill in an inode item with the correct link count.
5954 */
5955 if (!name)
5956 set_nlink(inode, 0);
5957
5958 /*
Li Zefan581bb052011-04-20 10:06:11 +08005959 * we have to initialize this early, so we can reclaim the inode
5960 * number if we fail afterwards in this function.
5961 */
5962 inode->i_ino = objectid;
5963
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005964 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005965 trace_btrfs_inode_request(dir);
5966
Nikolay Borisov877574e2017-02-20 13:50:33 +02005967 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005968 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005969 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005970 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005971 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005972 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005973 } else if (dir) {
5974 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005975 }
5976 /*
5977 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005978 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005979 * number
5980 */
5981 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005982 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005983 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005984 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005985 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005986
Josef Bacik5dc562c2012-08-17 13:14:17 -04005987 /*
5988 * We could have gotten an inode number from somebody who was fsynced
5989 * and then removed in this same transaction, so let's just set full
5990 * sync since it will be a full sync anyway and this will blow away the
5991 * old info in the log.
5992 */
5993 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5994
Chris Mason9c583092008-01-29 15:15:18 -05005995 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005996 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005997 key[0].offset = 0;
5998
Chris Mason9c583092008-01-29 15:15:18 -05005999 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006000
6001 if (name) {
6002 /*
6003 * Start new inodes with an inode_ref. This is slightly more
6004 * efficient for small numbers of hard links since they will
6005 * be packed into one item. Extended refs will kick in if we
6006 * add more hard links than can fit in the ref item.
6007 */
6008 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006009 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006010 key[1].offset = ref_objectid;
6011
6012 sizes[1] = name_len + sizeof(*ref);
6013 }
Chris Mason9c583092008-01-29 15:15:18 -05006014
Chris Masonb0d5d102014-09-08 13:08:51 -07006015 location = &BTRFS_I(inode)->location;
6016 location->objectid = objectid;
6017 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006018 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006019
6020 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006021 if (ret < 0) {
6022 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006023 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006024 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006025
Chris Masonb9473432009-03-13 11:00:37 -04006026 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006027 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006028 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006029 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006030
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006031 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006032 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306033
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006034 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306035 inode->i_atime = inode->i_mtime;
6036 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006037 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306038
Chris Mason5f39d392007-10-15 16:14:19 -04006039 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6040 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006041 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006042 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006043 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006044
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006045 if (name) {
6046 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6047 struct btrfs_inode_ref);
6048 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6049 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6050 ptr = (unsigned long)(ref + 1);
6051 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6052 }
Chris Mason9c583092008-01-29 15:15:18 -05006053
Chris Mason5f39d392007-10-15 16:14:19 -04006054 btrfs_mark_buffer_dirty(path->nodes[0]);
6055 btrfs_free_path(path);
6056
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006057 btrfs_inherit_iflags(inode, dir);
6058
Al Viro569254b2011-07-24 17:08:40 -04006059 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006060 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006061 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006062 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006063 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6064 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006065 }
6066
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006067 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006068
6069 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006070 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006071
Alexander Block8ea05e32012-07-25 17:35:53 +02006072 btrfs_update_root_times(trans, root);
6073
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006074 ret = btrfs_inode_inherit_props(trans, inode, dir);
6075 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006076 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006077 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006078 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006079
Chris Mason39279cc2007-06-12 06:35:45 -04006080 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006081
6082fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006083 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006084fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006085 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006086 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006087 btrfs_free_path(path);
6088 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006089}
6090
Chris Masond352ac62008-09-29 15:18:18 -04006091/*
6092 * utility function to add 'inode' into 'parent_inode' with
6093 * a give name and a given sequence number.
6094 * if 'add_backref' is true, also insert a backref from the
6095 * inode to the parent directory.
6096 */
Chris Masone02119d2008-09-05 16:13:11 -04006097int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006098 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006099 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006100{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006101 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006102 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006103 struct btrfs_root *root = parent_inode->root;
6104 u64 ino = btrfs_ino(inode);
6105 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006106
Li Zefan33345d012011-04-20 10:31:50 +08006107 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006108 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006109 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006110 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006111 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006112 key.offset = 0;
6113 }
Chris Mason39279cc2007-06-12 06:35:45 -04006114
Li Zefan33345d012011-04-20 10:31:50 +08006115 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006116 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006117 root->root_key.objectid, parent_ino,
6118 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006119 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006120 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6121 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006122 }
6123
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006124 /* Nothing to clean up yet */
6125 if (ret)
6126 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006127
Lu Fengqi684572d2018-08-04 21:10:57 +08006128 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006129 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006130 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006131 goto fail_dir_item;
6132 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006133 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006134 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006135 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006136
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006137 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006138 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006139 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006140 /*
6141 * If we are replaying a log tree, we do not want to update the mtime
6142 * and ctime of the parent directory with the current time, since the
6143 * log replay procedure is responsible for setting them to their correct
6144 * values (the ones it had when the fsync was done).
6145 */
6146 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6147 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6148
6149 parent_inode->vfs_inode.i_mtime = now;
6150 parent_inode->vfs_inode.i_ctime = now;
6151 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006152 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006153 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006154 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006155 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006156
6157fail_dir_item:
6158 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6159 u64 local_index;
6160 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006161 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006162 root->root_key.objectid, parent_ino,
6163 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006164 if (err)
6165 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006166 } else if (add_backref) {
6167 u64 local_index;
6168 int err;
6169
6170 err = btrfs_del_inode_ref(trans, root, name, name_len,
6171 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006172 if (err)
6173 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006174 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006175
6176 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006177 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006178}
6179
6180static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006181 struct btrfs_inode *dir, struct dentry *dentry,
6182 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006183{
Josef Bacika1b075d2010-11-19 20:36:11 +00006184 int err = btrfs_add_link(trans, dir, inode,
6185 dentry->d_name.name, dentry->d_name.len,
6186 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006187 if (err > 0)
6188 err = -EEXIST;
6189 return err;
6190}
6191
Josef Bacik618e21d2007-07-11 10:18:17 -04006192static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006193 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006194{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006195 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006196 struct btrfs_trans_handle *trans;
6197 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006198 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006199 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006200 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006201 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006202
Josef Bacik9ed74f22009-09-11 16:12:44 -04006203 /*
6204 * 2 for inode item and ref
6205 * 2 for dir items
6206 * 1 for xattr if selinux is on
6207 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006208 trans = btrfs_start_transaction(root, 5);
6209 if (IS_ERR(trans))
6210 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006211
Li Zefan581bb052011-04-20 10:06:11 +08006212 err = btrfs_find_free_ino(root, &objectid);
6213 if (err)
6214 goto out_unlock;
6215
Josef Bacikaec74772008-07-24 12:12:38 -04006216 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006217 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6218 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006219 if (IS_ERR(inode)) {
6220 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006221 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006222 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006223 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006224
Casey Schauflerad19db72011-12-15 10:09:07 -05006225 /*
6226 * If the active LSM wants to access the inode during
6227 * d_instantiate it needs these. Smack checks to see
6228 * if the filesystem supports xattrs by looking at the
6229 * ops vector.
6230 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006231 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006232 init_special_inode(inode, inode->i_mode, rdev);
6233
6234 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006235 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006236 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006237
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006238 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6239 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006240 if (err)
6241 goto out_unlock;
6242
6243 btrfs_update_inode(trans, root, inode);
6244 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006245
Josef Bacik618e21d2007-07-11 10:18:17 -04006246out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006247 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006248 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006249 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006250 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006251 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006252 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006253 return err;
6254}
6255
Chris Mason39279cc2007-06-12 06:35:45 -04006256static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006257 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006258{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006259 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006260 struct btrfs_trans_handle *trans;
6261 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006262 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006263 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006264 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006265 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006266
Josef Bacik9ed74f22009-09-11 16:12:44 -04006267 /*
6268 * 2 for inode item and ref
6269 * 2 for dir items
6270 * 1 for xattr if selinux is on
6271 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006272 trans = btrfs_start_transaction(root, 5);
6273 if (IS_ERR(trans))
6274 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006275
Li Zefan581bb052011-04-20 10:06:11 +08006276 err = btrfs_find_free_ino(root, &objectid);
6277 if (err)
6278 goto out_unlock;
6279
Josef Bacikaec74772008-07-24 12:12:38 -04006280 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006281 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6282 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006283 if (IS_ERR(inode)) {
6284 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006285 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006286 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006287 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006288 /*
6289 * If the active LSM wants to access the inode during
6290 * d_instantiate it needs these. Smack checks to see
6291 * if the filesystem supports xattrs by looking at the
6292 * ops vector.
6293 */
6294 inode->i_fop = &btrfs_file_operations;
6295 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006296 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006297
6298 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6299 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006300 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006301
6302 err = btrfs_update_inode(trans, root, inode);
6303 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006304 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006305
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006306 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6307 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006308 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006309 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006310
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006311 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006312 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006313
Chris Mason39279cc2007-06-12 06:35:45 -04006314out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006315 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006316 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006317 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006318 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006319 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006320 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006321 return err;
6322}
6323
6324static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6325 struct dentry *dentry)
6326{
Filipe Manana271dba452016-01-05 16:24:05 +00006327 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006328 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006329 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006330 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006331 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006332 int err;
6333 int drop_inode = 0;
6334
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006335 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006336 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006337 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006338
Mark Fashehf1863732012-08-08 11:32:27 -07006339 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006340 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006341
Nikolay Borisov877574e2017-02-20 13:50:33 +02006342 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006343 if (err)
6344 goto fail;
6345
Yan, Zhenga22285a2010-05-16 10:48:46 -04006346 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006347 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006348 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006349 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006350 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006351 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006352 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006353 if (IS_ERR(trans)) {
6354 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006355 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006356 goto fail;
6357 }
Chris Mason5f39d392007-10-15 16:14:19 -04006358
Miao Xie67de1172013-12-26 13:07:06 +08006359 /* There are several dir indexes for this inode, clear the cache. */
6360 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006361 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006362 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006363 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006364 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006365 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006366
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006367 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6368 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006369
Yan, Zhenga5719522009-09-24 09:17:31 -04006370 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006371 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006372 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006373 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006374 int ret;
6375
Yan, Zhenga5719522009-09-24 09:17:31 -04006376 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006377 if (err)
6378 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006379 if (inode->i_nlink == 1) {
6380 /*
6381 * If new hard link count is 1, it's a file created
6382 * with open(2) O_TMPFILE flag.
6383 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006384 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006385 if (err)
6386 goto fail;
6387 }
Al Viro08c422c2011-12-23 07:58:13 -05006388 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006389 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6390 true, NULL);
6391 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6392 err = btrfs_commit_transaction(trans);
6393 trans = NULL;
6394 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006395 }
Chris Mason39279cc2007-06-12 06:35:45 -04006396
Chris Mason1832a6d2007-12-21 16:27:21 -05006397fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006398 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006399 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006400 if (drop_inode) {
6401 inode_dec_link_count(inode);
6402 iput(inode);
6403 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006404 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006405 return err;
6406}
6407
Al Viro18bb1db2011-07-26 01:41:39 -04006408static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006409{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006410 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006411 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006412 struct btrfs_trans_handle *trans;
6413 struct btrfs_root *root = BTRFS_I(dir)->root;
6414 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006415 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006416 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006417
Josef Bacik9ed74f22009-09-11 16:12:44 -04006418 /*
6419 * 2 items for inode and ref
6420 * 2 items for dir items
6421 * 1 for xattr if selinux is on
6422 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006423 trans = btrfs_start_transaction(root, 5);
6424 if (IS_ERR(trans))
6425 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006426
Li Zefan581bb052011-04-20 10:06:11 +08006427 err = btrfs_find_free_ino(root, &objectid);
6428 if (err)
6429 goto out_fail;
6430
Josef Bacikaec74772008-07-24 12:12:38 -04006431 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006432 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6433 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006434 if (IS_ERR(inode)) {
6435 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006436 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006437 goto out_fail;
6438 }
Chris Mason5f39d392007-10-15 16:14:19 -04006439
Chris Masonb0d5d102014-09-08 13:08:51 -07006440 /* these must be set before we unlock the inode */
6441 inode->i_op = &btrfs_dir_inode_operations;
6442 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006443
Eric Paris2a7dba32011-02-01 11:05:39 -05006444 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006445 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006446 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006447
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006448 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006449 err = btrfs_update_inode(trans, root, inode);
6450 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006451 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006452
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006453 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6454 dentry->d_name.name,
6455 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006456 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006457 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006458
Al Viro1e2e5472018-05-04 08:23:01 -04006459 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006460
6461out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006462 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006463 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006464 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006465 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006466 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006467 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006468 return err;
6469}
6470
Chris Masonc8b97812008-10-29 14:49:59 -04006471static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006472 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006473 size_t pg_offset, u64 extent_offset,
6474 struct btrfs_file_extent_item *item)
6475{
6476 int ret;
6477 struct extent_buffer *leaf = path->nodes[0];
6478 char *tmp;
6479 size_t max_size;
6480 unsigned long inline_size;
6481 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006482 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006483
6484 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006485 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006486 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6487 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006488 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006489 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006490 if (!tmp)
6491 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006492 ptr = btrfs_file_extent_inline_start(item);
6493
6494 read_extent_buffer(leaf, tmp, ptr, inline_size);
6495
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006496 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006497 ret = btrfs_decompress(compress_type, tmp, page,
6498 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006499
6500 /*
6501 * decompression code contains a memset to fill in any space between the end
6502 * of the uncompressed data and the end of max_size in case the decompressed
6503 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6504 * the end of an inline extent and the beginning of the next block, so we
6505 * cover that region here.
6506 */
6507
6508 if (max_size + pg_offset < PAGE_SIZE) {
6509 char *map = kmap(page);
6510 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6511 kunmap(page);
6512 }
Chris Masonc8b97812008-10-29 14:49:59 -04006513 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006514 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006515}
6516
Omar Sandoval39b07b52019-12-02 17:34:23 -08006517/**
6518 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6519 * @inode: file to search in
6520 * @page: page to read extent data into if the extent is inline
6521 * @pg_offset: offset into @page to copy to
6522 * @start: file offset
6523 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006524 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006525 * This returns the first &struct extent_map which overlaps with the given
6526 * range, reading it from the B-tree and caching it if necessary. Note that
6527 * there may be more extents which overlap the given range after the returned
6528 * extent_map.
6529 *
6530 * If @page is not NULL and the extent is inline, this also reads the extent
6531 * data directly into the page and marks the extent up to date in the io_tree.
6532 *
6533 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006534 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006535struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006536 struct page *page, size_t pg_offset,
6537 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006538{
David Sterba3ffbd682018-06-29 10:56:42 +02006539 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006540 int ret;
6541 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006542 u64 extent_start = 0;
6543 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006544 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006545 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006546 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006547 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006548 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006549 struct extent_buffer *leaf;
6550 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006551 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006552 struct extent_map_tree *em_tree = &inode->extent_tree;
6553 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006554
Chris Mason890871b2009-09-02 16:24:52 -04006555 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006556 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006557 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006558
Chris Masona52d9a82007-08-27 16:49:44 -04006559 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006560 if (em->start > start || em->start + em->len <= start)
6561 free_extent_map(em);
6562 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006563 free_extent_map(em);
6564 else
6565 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006566 }
David Sterba172ddd62011-04-21 00:48:27 +02006567 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006568 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006569 err = -ENOMEM;
6570 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006571 }
Chris Masond1310b22008-01-24 16:13:08 -05006572 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006573 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006574 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006575 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006576
Liu Bobee6ec82018-08-17 05:05:28 +08006577 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006578 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006579 err = -ENOMEM;
6580 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006581 }
6582
Liu Bobee6ec82018-08-17 05:05:28 +08006583 /* Chances are we'll be called again, so go ahead and do readahead */
6584 path->reada = READA_FORWARD;
6585
Liu Boe49aabd2018-08-25 13:47:09 +08006586 /*
6587 * Unless we're going to uncompress the inline extent, no sleep would
6588 * happen.
6589 */
6590 path->leave_spinning = 1;
6591
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006592 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006593 if (ret < 0) {
6594 err = ret;
6595 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006596 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006597 if (path->slots[0] == 0)
6598 goto not_found;
6599 path->slots[0]--;
6600 }
6601
Chris Mason5f39d392007-10-15 16:14:19 -04006602 leaf = path->nodes[0];
6603 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006604 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006605 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006606 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006607 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006608 /*
6609 * If we backup past the first extent we want to move forward
6610 * and see if there is an extent in front of us, otherwise we'll
6611 * say there is a hole for our whole search range which can
6612 * cause problems.
6613 */
6614 extent_end = start;
6615 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006616 }
6617
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006618 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006619 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006620 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006621 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6622 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006623 /* Only regular file could have regular/prealloc extent */
6624 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6625 ret = -EUCLEAN;
6626 btrfs_crit(fs_info,
6627 "regular/prealloc extent found for non-regular inode %llu",
6628 btrfs_ino(inode));
6629 goto out;
6630 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006631 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6632 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006633 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006634 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6635 path->slots[0],
6636 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006637 }
Josef Bacik25a50342013-10-14 12:08:38 -04006638next:
Yan Zheng9036c102008-10-30 14:19:41 -04006639 if (start >= extent_end) {
6640 path->slots[0]++;
6641 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6642 ret = btrfs_next_leaf(root, path);
6643 if (ret < 0) {
6644 err = ret;
6645 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006646 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006647 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006648 }
Yan Zheng9036c102008-10-30 14:19:41 -04006649 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006650 }
Yan Zheng9036c102008-10-30 14:19:41 -04006651 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6652 if (found_key.objectid != objectid ||
6653 found_key.type != BTRFS_EXTENT_DATA_KEY)
6654 goto not_found;
6655 if (start + len <= found_key.offset)
6656 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006657 if (start > found_key.offset)
6658 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006659
6660 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006661 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006662 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006663 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006664 em->block_start = EXTENT_MAP_HOLE;
6665 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006666 }
6667
Omar Sandoval39b07b52019-12-02 17:34:23 -08006668 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006669
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006670 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6671 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006672 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006673 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006674 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006675 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006676 size_t size;
6677 size_t extent_offset;
6678 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006679
Omar Sandoval39b07b52019-12-02 17:34:23 -08006680 if (!page)
Yan689f9342007-10-29 11:41:07 -04006681 goto out;
Yan689f9342007-10-29 11:41:07 -04006682
Qu Wenruoe41ca582018-06-06 15:41:49 +08006683 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006684 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006685 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6686 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006687 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006688 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006689 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006690 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006691 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006692
6693 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006694 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006695 if (btrfs_file_extent_compression(leaf, item) !=
6696 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006697 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006698 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006699 if (ret) {
6700 err = ret;
6701 goto out;
6702 }
Chris Masonc8b97812008-10-29 14:49:59 -04006703 } else {
6704 map = kmap(page);
6705 read_extent_buffer(leaf, map + pg_offset, ptr,
6706 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006707 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006708 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006709 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006710 copy_size);
6711 }
Chris Masonc8b97812008-10-29 14:49:59 -04006712 kunmap(page);
6713 }
Chris Mason179e29e2007-11-01 11:28:41 -04006714 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006715 }
Chris Masond1310b22008-01-24 16:13:08 -05006716 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006717 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006718 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006719 }
6720not_found:
6721 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006722 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006723 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006724 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006725insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006726 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006727 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006728 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006729 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6730 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006731 err = -EIO;
6732 goto out;
6733 }
Chris Masond1310b22008-01-24 16:13:08 -05006734
6735 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006736 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006737 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006738 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006739out:
Liu Boc6414282018-08-23 07:36:17 +08006740 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006741
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006742 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006743
Chris Masona52d9a82007-08-27 16:49:44 -04006744 if (err) {
6745 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006746 return ERR_PTR(err);
6747 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006748 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006749 return em;
6750}
6751
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006752struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006753 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006754{
6755 struct extent_map *em;
6756 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006757 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006758 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006759 u64 delalloc_len;
6760 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006761 int err = 0;
6762
Omar Sandoval39b07b52019-12-02 17:34:23 -08006763 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006764 if (IS_ERR(em))
6765 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006766 /*
6767 * If our em maps to:
6768 * - a hole or
6769 * - a pre-alloc extent,
6770 * there might actually be delalloc bytes behind it.
6771 */
6772 if (em->block_start != EXTENT_MAP_HOLE &&
6773 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6774 return em;
6775 else
6776 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006777
6778 /* check to see if we've wrapped (len == -1 or similar) */
6779 end = start + len;
6780 if (end < start)
6781 end = (u64)-1;
6782 else
6783 end -= 1;
6784
6785 em = NULL;
6786
6787 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006788 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006789 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006790 delalloc_end = delalloc_start + delalloc_len;
6791 if (delalloc_end < delalloc_start)
6792 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006793
6794 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006795 * We didn't find anything useful, return the original results from
6796 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006797 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006798 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006799 em = hole_em;
6800 hole_em = NULL;
6801 goto out;
6802 }
6803
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006804 /*
6805 * Adjust the delalloc_start to make sure it doesn't go backwards from
6806 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006807 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006808 delalloc_start = max(start, delalloc_start);
6809 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006810
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006811 if (delalloc_len > 0) {
6812 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006813 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006814 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006815
David Sterba172ddd62011-04-21 00:48:27 +02006816 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006817 if (!em) {
6818 err = -ENOMEM;
6819 goto out;
6820 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006821
6822 ASSERT(hole_em);
6823 /*
6824 * When btrfs_get_extent can't find anything it returns one
6825 * huge hole
6826 *
6827 * Make sure what it found really fits our range, and adjust to
6828 * make sure it is based on the start from the caller
6829 */
6830 if (hole_end <= start || hole_em->start > end) {
6831 free_extent_map(hole_em);
6832 hole_em = NULL;
6833 } else {
6834 hole_start = max(hole_em->start, start);
6835 hole_len = hole_end - hole_start;
6836 }
6837
6838 if (hole_em && delalloc_start > hole_start) {
6839 /*
6840 * Our hole starts before our delalloc, so we have to
6841 * return just the parts of the hole that go until the
6842 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006843 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006844 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006845 em->start = hole_start;
6846 em->orig_start = hole_start;
6847 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006848 * Don't adjust block start at all, it is fixed at
6849 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006850 */
6851 em->block_start = hole_em->block_start;
6852 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006853 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6854 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006855 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006856 /*
6857 * Hole is out of passed range or it starts after
6858 * delalloc range
6859 */
6860 em->start = delalloc_start;
6861 em->len = delalloc_len;
6862 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006863 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006864 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006865 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006866 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006867 return hole_em;
6868 }
6869out:
6870
6871 free_extent_map(hole_em);
6872 if (err) {
6873 free_extent_map(em);
6874 return ERR_PTR(err);
6875 }
6876 return em;
6877}
6878
Nikolay Borisov64f54182020-06-03 08:55:31 +03006879static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006880 const u64 start,
6881 const u64 len,
6882 const u64 orig_start,
6883 const u64 block_start,
6884 const u64 block_len,
6885 const u64 orig_block_len,
6886 const u64 ram_bytes,
6887 const int type)
6888{
6889 struct extent_map *em = NULL;
6890 int ret;
6891
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006892 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03006893 em = create_io_em(inode, start, len, orig_start, block_start,
6894 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08006895 BTRFS_COMPRESS_NONE, /* compress_type */
6896 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006897 if (IS_ERR(em))
6898 goto out;
6899 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03006900 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
6901 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006902 if (ret) {
6903 if (em) {
6904 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03006905 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006906 }
6907 em = ERR_PTR(ret);
6908 }
6909 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006910
6911 return em;
6912}
6913
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006914static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006915 u64 start, u64 len)
6916{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006917 struct btrfs_root *root = inode->root;
6918 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04006919 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006920 struct btrfs_key ins;
6921 u64 alloc_hint;
6922 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006923
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006924 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006925 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006926 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006927 if (ret)
6928 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006929
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006930 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006931 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006932 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006933 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006934 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006935 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
6936 1);
Josef Bacik00361582013-08-14 14:02:47 -04006937
Josef Bacik4b46fce2010-05-23 11:00:55 -04006938 return em;
6939}
6940
Chris Mason46bfbb52010-05-26 11:04:10 -04006941/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006942 * Check if we can do nocow write into the range [@offset, @offset + @len)
6943 *
6944 * @offset: File offset
6945 * @len: The length to write, will be updated to the nocow writeable
6946 * range
6947 * @orig_start: (optional) Return the original file offset of the file extent
6948 * @orig_len: (optional) Return the original on-disk length of the file extent
6949 * @ram_bytes: (optional) Return the ram_bytes of the file extent
6950 *
6951 * This function will flush ordered extents in the range to ensure proper
6952 * nocow checks for (nowait == false) case.
6953 *
6954 * Return:
6955 * >0 and update @len if we can do nocow write
6956 * 0 if we can't do nocow write
6957 * <0 if error happened
6958 *
6959 * NOTE: This only checks the file extents, caller is responsible to wait for
6960 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04006961 */
Josef Bacik00361582013-08-14 14:02:47 -04006962noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006963 u64 *orig_start, u64 *orig_block_len,
6964 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006965{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006966 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006967 struct btrfs_path *path;
6968 int ret;
6969 struct extent_buffer *leaf;
6970 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006971 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006972 struct btrfs_file_extent_item *fi;
6973 struct btrfs_key key;
6974 u64 disk_bytenr;
6975 u64 backref_offset;
6976 u64 extent_end;
6977 u64 num_bytes;
6978 int slot;
6979 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006980 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006981
Chris Mason46bfbb52010-05-26 11:04:10 -04006982 path = btrfs_alloc_path();
6983 if (!path)
6984 return -ENOMEM;
6985
David Sterbaf85b7372017-01-20 14:54:07 +01006986 ret = btrfs_lookup_file_extent(NULL, root, path,
6987 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006988 if (ret < 0)
6989 goto out;
6990
6991 slot = path->slots[0];
6992 if (ret == 1) {
6993 if (slot == 0) {
6994 /* can't find the item, must cow */
6995 ret = 0;
6996 goto out;
6997 }
6998 slot--;
6999 }
7000 ret = 0;
7001 leaf = path->nodes[0];
7002 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007003 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007004 key.type != BTRFS_EXTENT_DATA_KEY) {
7005 /* not our file or wrong item type, must cow */
7006 goto out;
7007 }
7008
7009 if (key.offset > offset) {
7010 /* Wrong offset, must cow */
7011 goto out;
7012 }
7013
7014 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7015 found_type = btrfs_file_extent_type(leaf, fi);
7016 if (found_type != BTRFS_FILE_EXTENT_REG &&
7017 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7018 /* not a regular extent, must cow */
7019 goto out;
7020 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007021
7022 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7023 goto out;
7024
Miao Xiee77751a2013-12-27 21:11:50 +08007025 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7026 if (extent_end <= offset)
7027 goto out;
7028
Chris Mason46bfbb52010-05-26 11:04:10 -04007029 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007030 if (disk_bytenr == 0)
7031 goto out;
7032
7033 if (btrfs_file_extent_compression(leaf, fi) ||
7034 btrfs_file_extent_encryption(leaf, fi) ||
7035 btrfs_file_extent_other_encoding(leaf, fi))
7036 goto out;
7037
Ethan Lien78d42952018-05-17 14:58:29 +08007038 /*
7039 * Do the same check as in btrfs_cross_ref_exist but without the
7040 * unnecessary search.
7041 */
7042 if (btrfs_file_extent_generation(leaf, fi) <=
7043 btrfs_root_last_snapshot(&root->root_item))
7044 goto out;
7045
Chris Mason46bfbb52010-05-26 11:04:10 -04007046 backref_offset = btrfs_file_extent_offset(leaf, fi);
7047
Josef Bacik7ee9e442013-06-21 16:37:03 -04007048 if (orig_start) {
7049 *orig_start = key.offset - backref_offset;
7050 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7051 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7052 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007053
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007054 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007055 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007056
7057 num_bytes = min(offset + *len, extent_end) - offset;
7058 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7059 u64 range_end;
7060
Jeff Mahoneyda170662016-06-15 09:22:56 -04007061 range_end = round_up(offset + num_bytes,
7062 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007063 ret = test_range_bit(io_tree, offset, range_end,
7064 EXTENT_DELALLOC, 0, NULL);
7065 if (ret) {
7066 ret = -EAGAIN;
7067 goto out;
7068 }
7069 }
7070
Josef Bacik1bda19e2013-10-18 12:10:36 -04007071 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007072
7073 /*
7074 * look for other files referencing this extent, if we
7075 * find any we must cow
7076 */
Josef Bacik00361582013-08-14 14:02:47 -04007077
Liu Boe4c3b2d2017-01-30 12:25:28 -08007078 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007079 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007080 if (ret) {
7081 ret = 0;
7082 goto out;
7083 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007084
7085 /*
7086 * adjust disk_bytenr and num_bytes to cover just the bytes
7087 * in this extent we are about to write. If there
7088 * are any csums in that range we have to cow in order
7089 * to keep the csums correct
7090 */
7091 disk_bytenr += backref_offset;
7092 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007093 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7094 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007095 /*
7096 * all of the above have passed, it is safe to overwrite this extent
7097 * without cow
7098 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007099 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007100 ret = 1;
7101out:
7102 btrfs_free_path(path);
7103 return ret;
7104}
7105
Josef Bacikeb838e72012-07-31 16:28:48 -04007106static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
David Sterba55e20bd2020-06-09 19:56:06 +02007107 struct extent_state **cached_state, int writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007108{
7109 struct btrfs_ordered_extent *ordered;
7110 int ret = 0;
7111
7112 while (1) {
7113 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007114 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007115 /*
7116 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007117 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007118 * extents in this range.
7119 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007120 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007121 lockend - lockstart + 1);
7122
7123 /*
7124 * We need to make sure there are no buffered pages in this
7125 * range either, we could have raced between the invalidate in
7126 * generic_file_direct_write and locking the extent. The
7127 * invalidate needs to happen so that reads after a write do not
7128 * get stale data.
7129 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007130 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007131 (!writing || !filemap_range_has_page(inode->i_mapping,
7132 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007133 break;
7134
7135 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007136 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007137
7138 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007139 /*
7140 * If we are doing a DIO read and the ordered extent we
7141 * found is for a buffered write, we can not wait for it
7142 * to complete and retry, because if we do so we can
7143 * deadlock with concurrent buffered writes on page
7144 * locks. This happens only if our DIO read covers more
7145 * than one extent map, if at this point has already
7146 * created an ordered extent for a previous extent map
7147 * and locked its range in the inode's io tree, and a
7148 * concurrent write against that previous extent map's
7149 * range and this range started (we unlock the ranges
7150 * in the io tree only when the bios complete and
7151 * buffered writes always lock pages before attempting
7152 * to lock range in the io tree).
7153 */
7154 if (writing ||
7155 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7156 btrfs_start_ordered_extent(inode, ordered, 1);
7157 else
7158 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007159 btrfs_put_ordered_extent(ordered);
7160 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007161 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007162 * We could trigger writeback for this range (and wait
7163 * for it to complete) and then invalidate the pages for
7164 * this range (through invalidate_inode_pages2_range()),
7165 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007166 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007167 * triggered a readahead) on a page lock due to an
7168 * ordered dio extent we created before but did not have
7169 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007170 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007171 * ordered extent to complete while holding a lock on
7172 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007173 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007174 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007175 }
7176
Filipe Mananaade77022016-02-18 14:28:55 +00007177 if (ret)
7178 break;
7179
Josef Bacikeb838e72012-07-31 16:28:48 -04007180 cond_resched();
7181 }
7182
7183 return ret;
7184}
7185
Liu Bo6f9994d2017-01-31 07:50:22 -08007186/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007187static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7188 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007189 u64 block_len, u64 orig_block_len,
7190 u64 ram_bytes, int compress_type,
7191 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007192{
7193 struct extent_map_tree *em_tree;
7194 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007195 int ret;
7196
Liu Bo6f9994d2017-01-31 07:50:22 -08007197 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7198 type == BTRFS_ORDERED_COMPRESSED ||
7199 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007200 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007201
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007202 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007203 em = alloc_extent_map();
7204 if (!em)
7205 return ERR_PTR(-ENOMEM);
7206
7207 em->start = start;
7208 em->orig_start = orig_start;
7209 em->len = len;
7210 em->block_len = block_len;
7211 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007212 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007213 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007214 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007215 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007216 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007217 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007218 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007219 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7220 em->compress_type = compress_type;
7221 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007222
7223 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007224 btrfs_drop_extent_cache(inode, em->start,
7225 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007226 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007227 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007228 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007229 /*
7230 * The caller has taken lock_extent(), who could race with us
7231 * to add em?
7232 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007233 } while (ret == -EEXIST);
7234
7235 if (ret) {
7236 free_extent_map(em);
7237 return ERR_PTR(ret);
7238 }
7239
Liu Bo6f9994d2017-01-31 07:50:22 -08007240 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007241 return em;
7242}
7243
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007244
David Sterba55e20bd2020-06-09 19:56:06 +02007245static int btrfs_get_blocks_direct_read(struct extent_map *em,
7246 struct buffer_head *bh_result,
7247 struct inode *inode,
7248 u64 start, u64 len)
7249{
7250 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7251
7252 if (em->block_start == EXTENT_MAP_HOLE ||
7253 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7254 return -ENOENT;
7255
7256 len = min(len, em->len - (start - em->start));
7257
7258 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7259 inode->i_blkbits;
7260 bh_result->b_size = len;
7261 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7262 set_buffer_mapped(bh_result);
7263
7264 return 0;
7265}
7266
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007267static int btrfs_get_blocks_direct_write(struct extent_map **map,
David Sterba55e20bd2020-06-09 19:56:06 +02007268 struct buffer_head *bh_result,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007269 struct inode *inode,
7270 struct btrfs_dio_data *dio_data,
7271 u64 start, u64 len)
7272{
7273 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7274 struct extent_map *em = *map;
7275 int ret = 0;
7276
7277 /*
7278 * We don't allocate a new extent in the following cases
7279 *
7280 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7281 * existing extent.
7282 * 2) The extent is marked as PREALLOC. We're good to go here and can
7283 * just use the extent.
7284 *
7285 */
7286 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7287 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7288 em->block_start != EXTENT_MAP_HOLE)) {
7289 int type;
7290 u64 block_start, orig_start, orig_block_len, ram_bytes;
7291
7292 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7293 type = BTRFS_ORDERED_PREALLOC;
7294 else
7295 type = BTRFS_ORDERED_NOCOW;
7296 len = min(len, em->len - (start - em->start));
7297 block_start = em->block_start + (start - em->start);
7298
7299 if (can_nocow_extent(inode, start, &len, &orig_start,
7300 &orig_block_len, &ram_bytes) == 1 &&
7301 btrfs_inc_nocow_writers(fs_info, block_start)) {
7302 struct extent_map *em2;
7303
Nikolay Borisov64f54182020-06-03 08:55:31 +03007304 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007305 orig_start, block_start,
7306 len, orig_block_len,
7307 ram_bytes, type);
7308 btrfs_dec_nocow_writers(fs_info, block_start);
7309 if (type == BTRFS_ORDERED_PREALLOC) {
7310 free_extent_map(em);
7311 *map = em = em2;
7312 }
7313
7314 if (em2 && IS_ERR(em2)) {
7315 ret = PTR_ERR(em2);
7316 goto out;
7317 }
7318 /*
7319 * For inode marked NODATACOW or extent marked PREALLOC,
7320 * use the existing or preallocated extent, so does not
7321 * need to adjust btrfs_space_info's bytes_may_use.
7322 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007323 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007324 goto skip_cow;
7325 }
7326 }
7327
7328 /* this will cow the extent */
David Sterba55e20bd2020-06-09 19:56:06 +02007329 len = bh_result->b_size;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007330 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007331 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007332 if (IS_ERR(em)) {
7333 ret = PTR_ERR(em);
7334 goto out;
7335 }
7336
7337 len = min(len, em->len - (start - em->start));
7338
7339skip_cow:
David Sterba55e20bd2020-06-09 19:56:06 +02007340 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7341 inode->i_blkbits;
7342 bh_result->b_size = len;
7343 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7344 set_buffer_mapped(bh_result);
7345
7346 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7347 set_buffer_new(bh_result);
7348
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007349 /*
7350 * Need to update the i_size under the extent lock so buffered
7351 * readers will get the updated i_size when we unlock.
7352 */
David Sterba55e20bd2020-06-09 19:56:06 +02007353 if (!dio_data->overwrite && start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007354 i_size_write(inode, start + len);
7355
David Sterba55e20bd2020-06-09 19:56:06 +02007356 WARN_ON(dio_data->reserve < len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007357 dio_data->reserve -= len;
David Sterba55e20bd2020-06-09 19:56:06 +02007358 dio_data->unsubmitted_oe_range_end = start + len;
7359 current->journal_info = dio_data;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007360out:
7361 return ret;
7362}
7363
David Sterba55e20bd2020-06-09 19:56:06 +02007364static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7365 struct buffer_head *bh_result, int create)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007366{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007367 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007368 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007369 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307370 struct btrfs_dio_data *dio_data = NULL;
David Sterba55e20bd2020-06-09 19:56:06 +02007371 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007372 u64 lockstart, lockend;
David Sterba55e20bd2020-06-09 19:56:06 +02007373 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007374 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007375
David Sterba55e20bd2020-06-09 19:56:06 +02007376 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007377 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007378
Josef Bacikc3298612012-08-03 16:49:19 -04007379 lockstart = start;
7380 lockend = start + len - 1;
7381
David Sterba55e20bd2020-06-09 19:56:06 +02007382 if (current->journal_info) {
7383 /*
7384 * Need to pull our outstanding extents and set journal_info to NULL so
7385 * that anything that needs to check if there's a transaction doesn't get
7386 * confused.
7387 */
7388 dio_data = current->journal_info;
7389 current->journal_info = NULL;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007390 }
7391
Josef Bacikeb838e72012-07-31 16:28:48 -04007392 /*
7393 * If this errors out it's because we couldn't invalidate pagecache for
7394 * this range and we need to fallback to buffered.
7395 */
David Sterba55e20bd2020-06-09 19:56:06 +02007396 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7397 create)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007398 ret = -ENOTBLK;
7399 goto err;
7400 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007401
Omar Sandoval39b07b52019-12-02 17:34:23 -08007402 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007403 if (IS_ERR(em)) {
7404 ret = PTR_ERR(em);
7405 goto unlock_err;
7406 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007407
7408 /*
7409 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7410 * io. INLINE is special, and we could probably kludge it in here, but
7411 * it's still buffered so for safety lets just fall back to the generic
7412 * buffered path.
7413 *
7414 * For COMPRESSED we _have_ to read the entire extent in so we can
7415 * decompress it, so there will be buffering required no matter what we
7416 * do, so go ahead and fallback to buffered.
7417 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007418 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007419 * to buffered IO. Don't blame me, this is the price we pay for using
7420 * the generic code.
7421 */
7422 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7423 em->block_start == EXTENT_MAP_INLINE) {
7424 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007425 ret = -ENOTBLK;
7426 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007427 }
7428
David Sterba55e20bd2020-06-09 19:56:06 +02007429 if (create) {
7430 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7431 dio_data, start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007432 if (ret < 0)
7433 goto unlock_err;
David Sterba55e20bd2020-06-09 19:56:06 +02007434
7435 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7436 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007437 } else {
David Sterba55e20bd2020-06-09 19:56:06 +02007438 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7439 start, len);
7440 /* Can be negative only if we read from a hole */
7441 if (ret < 0) {
7442 ret = 0;
7443 free_extent_map(em);
7444 goto unlock_err;
7445 }
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007446 /*
7447 * We need to unlock only the end area that we aren't using.
7448 * The rest is going to be unlocked by the endio routine.
7449 */
David Sterba55e20bd2020-06-09 19:56:06 +02007450 lockstart = start + bh_result->b_size;
7451 if (lockstart < lockend) {
7452 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7453 lockstart, lockend, &cached_state);
7454 } else {
7455 free_extent_state(cached_state);
7456 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007457 }
7458
Josef Bacik4b46fce2010-05-23 11:00:55 -04007459 free_extent_map(em);
7460
7461 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007462
7463unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007464 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7465 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007466err:
David Sterba55e20bd2020-06-09 19:56:06 +02007467 if (dio_data)
7468 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007469 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007470}
7471
Omar Sandoval769b4f22020-04-16 14:46:22 -07007472static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7473{
7474 /*
7475 * This implies a barrier so that stores to dio_bio->bi_status before
7476 * this and loads of dio_bio->bi_status after this are fully ordered.
7477 */
7478 if (!refcount_dec_and_test(&dip->refs))
7479 return;
7480
7481 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007482 __endio_write_update_ordered(BTRFS_I(dip->inode),
7483 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007484 dip->bytes,
7485 !dip->dio_bio->bi_status);
7486 } else {
7487 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7488 dip->logical_offset,
7489 dip->logical_offset + dip->bytes - 1);
7490 }
7491
David Sterba55e20bd2020-06-09 19:56:06 +02007492 dio_end_io(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007493 kfree(dip);
7494}
7495
Omar Sandoval77d5d682020-04-16 14:46:25 -07007496static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7497 int mirror_num,
7498 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007499{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007500 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007501 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007502 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007503
Mike Christie37226b22016-06-05 14:31:52 -05007504 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007505
Omar Sandoval5c047a62020-04-16 14:46:24 -07007506 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007507 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007508 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007509
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007510 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007511 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007512 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007513 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007514 return ret;
7515}
7516
Omar Sandoval769b4f22020-04-16 14:46:22 -07007517static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7518 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007519 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007520{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007521 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7522 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007523 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007524 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7525 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007526 struct bio_vec bvec;
7527 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007528 u64 start = io_bio->logical;
7529 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007530 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007531
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007532 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7533 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307534
Liu Bo17347ce2017-05-15 15:33:27 -07007535 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7536 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007537 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007538 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007539 if (uptodate &&
7540 (!csum || !check_data_csum(inode, io_bio, icsum,
7541 bvec.bv_page, pgoff,
7542 start, sectorsize))) {
7543 clean_io_failure(fs_info, failure_tree, io_tree,
7544 start, bvec.bv_page,
7545 btrfs_ino(BTRFS_I(inode)),
7546 pgoff);
7547 } else {
7548 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007549
Omar Sandoval77d5d682020-04-16 14:46:25 -07007550 status = btrfs_submit_read_repair(inode,
7551 &io_bio->bio,
7552 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007553 bvec.bv_page, pgoff,
7554 start,
7555 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007556 io_bio->mirror_num,
7557 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007558 if (status)
7559 err = status;
7560 }
7561 start += sectorsize;
7562 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307563 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307564 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007565 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007566 return err;
7567}
7568
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007569static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007570 const u64 offset, const u64 bytes,
7571 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007572{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007573 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007574 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007575 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007576 u64 ordered_offset = offset;
7577 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007578 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007579
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007580 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007581 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007582 else
Qu Wenruo524272602017-03-08 10:25:52 +08007583 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007584
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007585 while (ordered_offset < offset + bytes) {
7586 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007587 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007588 &ordered_offset,
7589 ordered_bytes,
7590 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007591 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7592 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007593 btrfs_queue_work(wq, &ordered->work);
7594 }
7595 /*
7596 * If btrfs_dec_test_ordered_pending does not find any ordered
7597 * extent in the range, we can exit.
7598 */
7599 if (ordered_offset == last_offset)
7600 return;
7601 /*
7602 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007603 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007604 */
7605 if (ordered_offset < offset + bytes) {
7606 ordered_bytes = offset + bytes - ordered_offset;
7607 ordered = NULL;
7608 }
Chris Mason163cf092010-11-28 19:56:33 -05007609 }
Filipe Manana14543772015-11-24 16:23:54 +00007610}
7611
David Sterbad0ee3932018-03-08 14:35:48 +01007612static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007613 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007614{
Josef Bacikc6100a42017-05-05 11:57:13 -04007615 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007616 blk_status_t ret;
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007617 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007618 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007619 return 0;
7620}
7621
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007622static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007623{
7624 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007625 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007626
Miao Xie8b110e32014-09-12 18:44:03 +08007627 if (err)
7628 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007629 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007630 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7631 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007632 (unsigned long long)bio->bi_iter.bi_sector,
7633 bio->bi_iter.bi_size, err);
7634
Omar Sandoval769b4f22020-04-16 14:46:22 -07007635 if (bio_op(bio) == REQ_OP_READ) {
7636 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007637 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007638 }
7639
Omar Sandoval769b4f22020-04-16 14:46:22 -07007640 if (err)
7641 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007642
Miao Xiee65e1532010-11-22 03:04:43 +00007643 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007644 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007645}
7646
David Sterbad0ee3932018-03-08 14:35:48 +01007647static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7648 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007649{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007650 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007651 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007652 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007653 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007654
Liu Bo4c274bc2017-11-01 17:19:27 -06007655 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007656 if (async_submit)
7657 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7658
Josef Bacik5fd02042012-05-02 14:00:54 -04007659 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007660 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007661 if (ret)
7662 goto err;
7663 }
Miao Xiee65e1532010-11-22 03:04:43 +00007664
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007665 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007666 goto map;
7667
7668 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007669 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7670 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007671 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007672 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007673 } else if (write) {
7674 /*
7675 * If we aren't doing async submit, calculate the csum of the
7676 * bio now.
7677 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007678 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007679 if (ret)
7680 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007681 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007682 u64 csum_offset;
7683
7684 csum_offset = file_offset - dip->logical_offset;
7685 csum_offset >>= inode->i_sb->s_blocksize_bits;
7686 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7687 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007688 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007689map:
Chris Mason08635ba2019-07-10 12:28:14 -07007690 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007691err:
Miao Xiee65e1532010-11-22 03:04:43 +00007692 return ret;
7693}
7694
Omar Sandovalc36cac22020-04-16 14:46:13 -07007695/*
7696 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7697 * or ordered extents whether or not we submit any bios.
7698 */
7699static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7700 struct inode *inode,
7701 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007702{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007703 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007704 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7705 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007706 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007707
Omar Sandoval85879572020-04-16 14:46:21 -07007708 dip_size = sizeof(*dip);
7709 if (!write && csum) {
7710 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7711 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7712 size_t nblocks;
7713
7714 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7715 dip_size += csum_size * nblocks;
7716 }
7717
7718 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007719 if (!dip)
7720 return NULL;
7721
Omar Sandovalc36cac22020-04-16 14:46:13 -07007722 dip->inode = inode;
7723 dip->logical_offset = file_offset;
7724 dip->bytes = dio_bio->bi_iter.bi_size;
7725 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007726 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007727 refcount_set(&dip->refs, 1);
David Sterba55e20bd2020-06-09 19:56:06 +02007728
7729 if (write) {
7730 struct btrfs_dio_data *dio_data = current->journal_info;
7731
7732 /*
7733 * Setting range start and end to the same value means that
7734 * no cleanup will happen in btrfs_direct_IO
7735 */
7736 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7737 dip->bytes;
7738 dio_data->unsubmitted_oe_range_start =
7739 dio_data->unsubmitted_oe_range_end;
7740 }
Omar Sandovalc36cac22020-04-16 14:46:13 -07007741 return dip;
7742}
7743
David Sterba55e20bd2020-06-09 19:56:06 +02007744static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7745 loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007746{
7747 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007748 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007749 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007750 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7751 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007752 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007753 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007754 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007755 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007756 u64 submit_len;
7757 int clone_offset = 0;
7758 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307759 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007760 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007761 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007762
Omar Sandovalc36cac22020-04-16 14:46:13 -07007763 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7764 if (!dip) {
7765 if (!write) {
7766 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7767 file_offset + dio_bio->bi_iter.bi_size - 1);
7768 }
7769 dio_bio->bi_status = BLK_STS_RESOURCE;
David Sterba55e20bd2020-06-09 19:56:06 +02007770 dio_end_io(dio_bio);
7771 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04007772 }
7773
Omar Sandoval85879572020-04-16 14:46:21 -07007774 if (!write && csum) {
7775 /*
7776 * Load the csums up front to reduce csum tree searches and
7777 * contention when submitting bios.
7778 */
7779 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7780 dip->csums);
7781 if (status != BLK_STS_OK)
7782 goto out_err;
7783 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007784
Omar Sandoval769b4f22020-04-16 14:46:22 -07007785 start_sector = dio_bio->bi_iter.bi_sector;
7786 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007787
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007788 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007789 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7790 start_sector << 9, submit_len,
7791 &geom);
7792 if (ret) {
7793 status = errno_to_blk_status(ret);
7794 goto out_err;
7795 }
7796 ASSERT(geom.len <= INT_MAX);
7797
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007798 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007799
Liu Bo725130b2017-05-16 09:51:39 -07007800 /*
7801 * This will never fail as it's passing GPF_NOFS and
7802 * the allocation is backed by btrfs_bioset.
7803 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007804 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007805 bio->bi_private = dip;
7806 bio->bi_end_io = btrfs_end_dio_bio;
7807 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007808
Liu Bo725130b2017-05-16 09:51:39 -07007809 ASSERT(submit_len >= clone_len);
7810 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007811
Liu Bo725130b2017-05-16 09:51:39 -07007812 /*
7813 * Increase the count before we submit the bio so we know
7814 * the end IO handler won't happen before we increase the
7815 * count. Otherwise, the dip might get freed before we're
7816 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007817 *
7818 * We transfer the initial reference to the last bio, so we
7819 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007820 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007821 if (submit_len > 0) {
7822 refcount_inc(&dip->refs);
7823 /*
7824 * If we are submitting more than one bio, submit them
7825 * all asynchronously. The exception is RAID 5 or 6, as
7826 * asynchronous checksums make it difficult to collect
7827 * full stripe writes.
7828 */
7829 if (!raid56)
7830 async_submit = 1;
7831 }
Miao Xiee65e1532010-11-22 03:04:43 +00007832
David Sterbad0ee3932018-03-08 14:35:48 +01007833 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007834 async_submit);
7835 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007836 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007837 if (submit_len > 0)
7838 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007839 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007840 }
Liu Bo725130b2017-05-16 09:51:39 -07007841
7842 clone_offset += clone_len;
7843 start_sector += clone_len >> 9;
7844 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007845 } while (submit_len > 0);
David Sterba55e20bd2020-06-09 19:56:06 +02007846 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007847
Miao Xiee65e1532010-11-22 03:04:43 +00007848out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007849 dip->dio_bio->bi_status = status;
7850 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007851}
7852
David Sterbaf4c48b42020-06-09 19:19:27 +02007853static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7854 const struct iov_iter *iter, loff_t offset)
7855{
7856 int seg;
7857 int i;
7858 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7859 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007860
David Sterbaf4c48b42020-06-09 19:19:27 +02007861 if (offset & blocksize_mask)
7862 goto out;
7863
7864 if (iov_iter_alignment(iter) & blocksize_mask)
7865 goto out;
7866
7867 /* If this is a write we don't need to check anymore */
7868 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7869 return 0;
7870 /*
7871 * Check to make sure we don't have duplicate iov_base's in this
7872 * iovec, if so return EINVAL, otherwise we'll get csum errors
7873 * when reading back.
7874 */
7875 for (seg = 0; seg < iter->nr_segs; seg++) {
7876 for (i = seg + 1; i < iter->nr_segs; i++) {
7877 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7878 goto out;
7879 }
7880 }
7881 retval = 0;
7882out:
7883 return retval;
7884}
7885
David Sterba55e20bd2020-06-09 19:56:06 +02007886static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007887{
7888 struct file *file = iocb->ki_filp;
7889 struct inode *inode = file->f_mapping->host;
7890 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba55e20bd2020-06-09 19:56:06 +02007891 struct btrfs_dio_data dio_data = { 0 };
David Sterbaf4c48b42020-06-09 19:19:27 +02007892 struct extent_changeset *data_reserved = NULL;
7893 loff_t offset = iocb->ki_pos;
7894 size_t count = 0;
David Sterba55e20bd2020-06-09 19:56:06 +02007895 int flags = 0;
7896 bool wakeup = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007897 bool relock = false;
7898 ssize_t ret;
7899
7900 if (check_direct_IO(fs_info, iter, offset))
7901 return 0;
7902
David Sterba55e20bd2020-06-09 19:56:06 +02007903 inode_dio_begin(inode);
7904
7905 /*
7906 * The generic stuff only does filemap_write_and_wait_range, which
7907 * isn't enough if we've written compressed pages to this area, so
7908 * we need to flush the dirty pages again to make absolutely sure
7909 * that any outstanding dirty pages are on disk.
7910 */
David Sterbaf4c48b42020-06-09 19:19:27 +02007911 count = iov_iter_count(iter);
David Sterba55e20bd2020-06-09 19:56:06 +02007912 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7913 &BTRFS_I(inode)->runtime_flags))
7914 filemap_fdatawrite_range(inode->i_mapping, offset,
7915 offset + count - 1);
7916
David Sterbaf4c48b42020-06-09 19:19:27 +02007917 if (iov_iter_rw(iter) == WRITE) {
7918 /*
7919 * If the write DIO is beyond the EOF, we need update
7920 * the isize, but it is protected by i_mutex. So we can
7921 * not unlock the i_mutex at this case.
7922 */
7923 if (offset + count <= inode->i_size) {
David Sterba55e20bd2020-06-09 19:56:06 +02007924 dio_data.overwrite = 1;
David Sterbaf4c48b42020-06-09 19:19:27 +02007925 inode_unlock(inode);
7926 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007927 }
Nikolay Borisove5b72312020-06-03 08:55:42 +03007928 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
David Sterba55e20bd2020-06-09 19:56:06 +02007929 offset, count);
7930 if (ret)
7931 goto out;
7932
7933 /*
7934 * We need to know how many extents we reserved so that we can
7935 * do the accounting properly if we go over the number we
7936 * originally calculated. Abuse current->journal_info for this.
7937 */
7938 dio_data.reserve = round_up(count,
7939 fs_info->sectorsize);
7940 dio_data.unsubmitted_oe_range_start = (u64)offset;
7941 dio_data.unsubmitted_oe_range_end = (u64)offset;
7942 current->journal_info = &dio_data;
David Sterbaf4c48b42020-06-09 19:19:27 +02007943 down_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007944 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7945 &BTRFS_I(inode)->runtime_flags)) {
7946 inode_dio_end(inode);
7947 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7948 wakeup = false;
David Sterbaf4c48b42020-06-09 19:19:27 +02007949 }
7950
David Sterba55e20bd2020-06-09 19:56:06 +02007951 ret = __blockdev_direct_IO(iocb, inode,
7952 fs_info->fs_devices->latest_bdev,
7953 iter, btrfs_get_blocks_direct, NULL,
7954 btrfs_submit_direct, flags);
David Sterbaf4c48b42020-06-09 19:19:27 +02007955 if (iov_iter_rw(iter) == WRITE) {
7956 up_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007957 current->journal_info = NULL;
7958 if (ret < 0 && ret != -EIOCBQUEUED) {
7959 if (dio_data.reserve)
Nikolay Borisov86d52922020-06-03 08:55:40 +03007960 btrfs_delalloc_release_space(BTRFS_I(inode),
7961 data_reserved, offset, dio_data.reserve,
7962 true);
David Sterba55e20bd2020-06-09 19:56:06 +02007963 /*
7964 * On error we might have left some ordered extents
7965 * without submitting corresponding bios for them, so
7966 * cleanup them up to avoid other tasks getting them
7967 * and waiting for them to complete forever.
7968 */
7969 if (dio_data.unsubmitted_oe_range_start <
7970 dio_data.unsubmitted_oe_range_end)
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007971 __endio_write_update_ordered(BTRFS_I(inode),
David Sterba55e20bd2020-06-09 19:56:06 +02007972 dio_data.unsubmitted_oe_range_start,
7973 dio_data.unsubmitted_oe_range_end -
7974 dio_data.unsubmitted_oe_range_start,
7975 false);
7976 } else if (ret >= 0 && (size_t)ret < count)
Nikolay Borisov86d52922020-06-03 08:55:40 +03007977 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
David Sterba55e20bd2020-06-09 19:56:06 +02007978 offset, count - (size_t)ret, true);
7979 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
David Sterbaf4c48b42020-06-09 19:19:27 +02007980 }
7981out:
David Sterba55e20bd2020-06-09 19:56:06 +02007982 if (wakeup)
7983 inode_dio_end(inode);
David Sterbaf4c48b42020-06-09 19:19:27 +02007984 if (relock)
7985 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02007986
David Sterbaf4c48b42020-06-09 19:19:27 +02007987 extent_changeset_free(data_reserved);
7988 return ret;
7989}
Chris Mason16432982008-04-10 10:23:21 -04007990
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007991static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02007992 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007993{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007994 int ret;
7995
Christoph Hellwig45dd0522020-05-23 09:30:14 +02007996 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007997 if (ret)
7998 return ret;
7999
David Sterba2135fb92017-06-23 04:09:57 +02008000 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008001}
8002
Chris Mason9ebefb182007-06-15 13:50:00 -04008003int btrfs_readpage(struct file *file, struct page *page)
8004{
David Sterba71ad38b2020-02-05 19:09:35 +01008005 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008006}
Chris Mason1832a6d2007-12-21 16:27:21 -05008007
Chris Mason39279cc2007-06-12 06:35:45 -04008008static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8009{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008010 struct inode *inode = page->mapping->host;
8011 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008012
8013 if (current->flags & PF_MEMALLOC) {
8014 redirty_page_for_writepage(wbc, page);
8015 unlock_page(page);
8016 return 0;
8017 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008018
8019 /*
8020 * If we are under memory pressure we will call this directly from the
8021 * VM, we need to make sure we have the inode referenced for the ordered
8022 * extent. If not just return like we didn't do anything.
8023 */
8024 if (!igrab(inode)) {
8025 redirty_page_for_writepage(wbc, page);
8026 return AOP_WRITEPAGE_ACTIVATE;
8027 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008028 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008029 btrfs_add_delayed_iput(inode);
8030 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008031}
Chris Mason39279cc2007-06-12 06:35:45 -04008032
Eric Sandeen48a3b632013-04-25 20:41:01 +00008033static int btrfs_writepages(struct address_space *mapping,
8034 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008035{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008036 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008037}
8038
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008039static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008040{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008041 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008042}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008043
Chris Masone6dcd2d2008-07-17 12:53:50 -04008044static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008045{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008046 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008047 if (ret == 1)
8048 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008049 return ret;
8050}
Chris Mason39279cc2007-06-12 06:35:45 -04008051
Chris Masone6dcd2d2008-07-17 12:53:50 -04008052static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8053{
Chris Mason98509cf2008-09-11 15:51:43 -04008054 if (PageWriteback(page) || PageDirty(page))
8055 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008056 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008057}
8058
Roman Gushchinf8e66082020-03-04 16:57:35 -08008059#ifdef CONFIG_MIGRATION
8060static int btrfs_migratepage(struct address_space *mapping,
8061 struct page *newpage, struct page *page,
8062 enum migrate_mode mode)
8063{
8064 int ret;
8065
8066 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8067 if (ret != MIGRATEPAGE_SUCCESS)
8068 return ret;
8069
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008070 if (page_has_private(page))
8071 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008072
8073 if (PagePrivate2(page)) {
8074 ClearPagePrivate2(page);
8075 SetPagePrivate2(newpage);
8076 }
8077
8078 if (mode != MIGRATE_SYNC_NO_COPY)
8079 migrate_page_copy(newpage, page);
8080 else
8081 migrate_page_states(newpage, page);
8082 return MIGRATEPAGE_SUCCESS;
8083}
8084#endif
8085
Lukas Czernerd47992f2013-05-21 23:17:23 -04008086static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8087 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008088{
Josef Bacik5fd02042012-05-02 14:00:54 -04008089 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008090 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008091 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008092 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008093 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008094 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308095 u64 start;
8096 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008097 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008098
Chris Mason8b62b722009-09-02 16:53:46 -04008099 /*
8100 * we have the page locked, so new writeback can't start,
8101 * and the dirty bit won't be cleared while we are here.
8102 *
8103 * Wait for IO on this page so that we can safely clear
8104 * the PagePrivate2 bit and do ordered accounting
8105 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008106 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008107
Josef Bacik5fd02042012-05-02 14:00:54 -04008108 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008109 if (offset) {
8110 btrfs_releasepage(page, GFP_NOFS);
8111 return;
8112 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008113
8114 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008115 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308116again:
8117 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008118 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308119 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008120 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008121 end = min(page_end,
8122 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008123 /*
8124 * IO on this page will never be started, so we need
8125 * to account for any ordered extents now
8126 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008127 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308128 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008129 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008130 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008131 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008132 /*
8133 * whoever cleared the private bit is responsible
8134 * for the finish_ordered_io
8135 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008136 if (TestClearPagePrivate2(page)) {
8137 struct btrfs_ordered_inode_tree *tree;
8138 u64 new_len;
8139
8140 tree = &BTRFS_I(inode)->ordered_tree;
8141
8142 spin_lock_irq(&tree->lock);
8143 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308144 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008145 if (new_len < ordered->truncated_len)
8146 ordered->truncated_len = new_len;
8147 spin_unlock_irq(&tree->lock);
8148
8149 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308150 start,
8151 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008152 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008153 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008154 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008155 if (!inode_evicting) {
8156 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308157 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008158 &cached_state);
8159 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308160
8161 start = end + 1;
8162 if (start < page_end)
8163 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008164 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008165
Qu Wenruob9d0b382015-09-29 10:35:16 +08008166 /*
8167 * Qgroup reserved space handler
8168 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008169 * 1) Already written to disk or ordered extent already submitted
8170 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8171 * Qgroup will be handled by its qgroup_record then.
8172 * btrfs_qgroup_free_data() call will do nothing here.
8173 *
8174 * 2) Not written to disk yet
8175 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8176 * bit of its io_tree, and free the qgroup reserved data space.
8177 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008178 */
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03008179 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008180 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008181 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008182 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8183 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008184 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008185
8186 __btrfs_releasepage(page, GFP_NOFS);
8187 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008188
Chris Mason4a096752008-07-21 10:29:44 -04008189 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008190 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008191}
8192
Chris Mason9ebefb182007-06-15 13:50:00 -04008193/*
8194 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8195 * called from a page fault handler when a page is first dirtied. Hence we must
8196 * be careful to check for EOF conditions here. We set the page up correctly
8197 * for a written page which means we get ENOSPC checking when writing into
8198 * holes and correct delalloc and unwritten extent mapping on filesystems that
8199 * support these features.
8200 *
8201 * We are not allowed to take the i_mutex here so we have to play games to
8202 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008203 * truncate_setsize() writes the inode size before removing pages, once we have
8204 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008205 * beyond EOF, then the page is guaranteed safe against truncation until we
8206 * unlock the page.
8207 */
Souptick Joardera528a242018-06-06 19:54:44 +05308208vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008209{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008210 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008211 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008212 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008213 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8214 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008215 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008216 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008217 char *kaddr;
8218 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008219 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308220 vm_fault_t ret;
8221 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008222 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308223 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008224 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008225 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308226 u64 end;
8227
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008228 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008229
Jan Karab2b5ef52012-06-12 16:20:45 +02008230 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008231 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008232 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308233 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008234
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308235 /*
8236 * Reserving delalloc space after obtaining the page lock can lead to
8237 * deadlock. For example, if a dirty page is locked by this function
8238 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8239 * dirty page write out, then the btrfs_writepage() function could
8240 * end up waiting indefinitely to get a lock on the page currently
8241 * being processed by btrfs_page_mkwrite() function.
8242 */
Nikolay Borisove5b72312020-06-03 08:55:42 +03008243 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8244 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308245 if (!ret2) {
8246 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008247 reserved = 1;
8248 }
Souptick Joardera528a242018-06-06 19:54:44 +05308249 if (ret2) {
8250 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008251 if (reserved)
8252 goto out;
8253 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008254 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008255
Nick Piggin56a76f82009-03-31 15:23:23 -07008256 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008257again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008258 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008259 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008260
Chris Mason9ebefb182007-06-15 13:50:00 -04008261 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008262 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008263 /* page got truncated out from underneath us */
8264 goto out_unlock;
8265 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008266 wait_on_page_writeback(page);
8267
David Sterbaff13db42015-12-03 14:30:40 +01008268 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008269 set_page_extent_mapped(page);
8270
Chris Masoneb84ae02008-07-17 13:53:27 -04008271 /*
8272 * we can't set the delalloc bits if there are pending ordered
8273 * extents. Drop our locks and wait for them to finish
8274 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008275 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8276 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008277 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008278 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008279 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008280 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008281 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008282 btrfs_put_ordered_extent(ordered);
8283 goto again;
8284 }
8285
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008286 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008287 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008288 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008289 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308290 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008291 btrfs_delalloc_release_space(BTRFS_I(inode),
8292 data_reserved, page_start,
8293 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308294 }
8295 }
8296
Josef Bacikfbf19082009-10-01 17:10:23 -04008297 /*
Liu Bo54160342016-12-13 12:15:19 -08008298 * page_mkwrite gets called when the page is firstly dirtied after it's
8299 * faulted in, but write(2) could also dirty a page and set delalloc
8300 * bits, thus in this case for space account reason, we still need to
8301 * clear any delalloc bits within this page range since we have to
8302 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008303 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308304 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008305 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8306 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008307
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008308 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008309 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308310 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008311 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008312 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008313 ret = VM_FAULT_SIGBUS;
8314 goto out_unlock;
8315 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008316
8317 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008318 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008319 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008320 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008321 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008322
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008323 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008324 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008325 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008326 flush_dcache_page(page);
8327 kunmap(page);
8328 }
Chris Mason247e7432008-07-17 12:53:51 -04008329 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008330 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008331 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008332
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008333 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008334 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008335 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008336
David Sterbae43bbe52017-12-12 21:43:52 +01008337 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008338
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008339 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8340 sb_end_pagefault(inode->i_sb);
8341 extent_changeset_free(data_reserved);
8342 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008343
8344out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008345 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008346out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008347 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008348 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008349 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008350out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008351 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008352 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008353 return ret;
8354}
8355
Filipe Manana213e8c52018-02-06 20:40:31 +00008356static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008357{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008358 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008359 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008360 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008361 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008362 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008363 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008364 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008365
Filipe Manana213e8c52018-02-06 20:40:31 +00008366 if (!skip_writeback) {
8367 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8368 (u64)-1);
8369 if (ret)
8370 return ret;
8371 }
Chris Mason39279cc2007-06-12 06:35:45 -04008372
Josef Bacikfcb80c22011-05-03 10:40:22 -04008373 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008374 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8375 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008376 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008377 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008378 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008379 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008380 * be free'd up by the truncate operation, but also have some slack
8381 * space reserved in case it uses space during the truncate (thank you
8382 * very much snapshotting).
8383 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008384 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008385 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008386 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008387 * doesn't end up using space reserved for updating the inode. We also
8388 * need to be able to stop the transaction and start a new one, which
8389 * means we need to be able to update the inode several times, and we
8390 * have no idea of knowing how many times that will be, so we can't just
8391 * reserve 1 item for the entirety of the operation, so that has to be
8392 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008393 *
8394 * So that leaves us with
8395 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008396 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008397 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008398 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008399 * updating the inode.
8400 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008401 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008402 if (!rsv)
8403 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008404 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008405 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008406
Josef Bacik907cbce2011-08-08 13:46:15 -04008407 /*
Josef Bacik07127182011-08-19 10:29:59 -04008408 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008409 * 1 for updating the inode.
8410 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008411 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008412 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008413 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008414 goto out;
8415 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008416
Josef Bacik907cbce2011-08-08 13:46:15 -04008417 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008418 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008419 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008420 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008421
Chris Mason5a3f23d2009-03-31 13:27:11 -04008422 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008423 * So if we truncate and then write and fsync we normally would just
8424 * write the extents that changed, which is a problem if we need to
8425 * first truncate that entire inode. So set this flag so we write out
8426 * all of the extents in the inode to the sync log so we're completely
8427 * safe.
8428 */
8429 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008430 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008431
Yan, Zheng80825102009-11-12 09:35:36 +00008432 while (1) {
8433 ret = btrfs_truncate_inode_items(trans, root, inode,
8434 inode->i_size,
8435 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008436 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008437 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008438 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008439
Yan, Zheng80825102009-11-12 09:35:36 +00008440 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008441 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008442 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008443
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008444 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008445 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008446
8447 trans = btrfs_start_transaction(root, 2);
8448 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008449 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008450 trans = NULL;
8451 break;
8452 }
8453
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008454 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008455 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008456 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008457 BUG_ON(ret); /* shouldn't happen */
8458 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008459 }
8460
Josef Bacikddfae632017-10-19 14:16:02 -04008461 /*
8462 * We can't call btrfs_truncate_block inside a trans handle as we could
8463 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8464 * we've truncated everything except the last little bit, and can do
8465 * btrfs_truncate_block and then update the disk_i_size.
8466 */
8467 if (ret == NEED_TRUNCATE_BLOCK) {
8468 btrfs_end_transaction(trans);
8469 btrfs_btree_balance_dirty(fs_info);
8470
8471 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8472 if (ret)
8473 goto out;
8474 trans = btrfs_start_transaction(root, 1);
8475 if (IS_ERR(trans)) {
8476 ret = PTR_ERR(trans);
8477 goto out;
8478 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008479 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008480 }
8481
Chris Mason917c16b2011-11-08 14:49:59 -05008482 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008483 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008484
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008485 trans->block_rsv = &fs_info->trans_block_rsv;
8486 ret2 = btrfs_update_inode(trans, root, inode);
8487 if (ret2 && !ret)
8488 ret = ret2;
8489
8490 ret2 = btrfs_end_transaction(trans);
8491 if (ret2 && !ret)
8492 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008493 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008494 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008495out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008496 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008497
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008498 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008499}
8500
Sven Wegener3b963622008-06-09 21:57:42 -04008501/*
Chris Masond352ac62008-09-29 15:18:18 -04008502 * create a new subvolume directory/inode (helper for the ioctl).
8503 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008504int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008505 struct btrfs_root *new_root,
8506 struct btrfs_root *parent_root,
8507 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008508{
Chris Mason39279cc2007-06-12 06:35:45 -04008509 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008510 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008511 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008512
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008513 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8514 new_dirid, new_dirid,
8515 S_IFDIR | (~current_umask() & S_IRWXUGO),
8516 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008517 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008518 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008519 inode->i_op = &btrfs_dir_inode_operations;
8520 inode->i_fop = &btrfs_dir_file_operations;
8521
Miklos Szeredibfe86842011-10-28 14:13:29 +02008522 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008523 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008524 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008525
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008526 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8527 if (err)
8528 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008529 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008530 new_root->root_key.objectid, err);
8531
Yan, Zheng76dda932009-09-21 16:00:26 -04008532 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008533
Yan, Zheng76dda932009-09-21 16:00:26 -04008534 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008535 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008536}
8537
Chris Mason39279cc2007-06-12 06:35:45 -04008538struct inode *btrfs_alloc_inode(struct super_block *sb)
8539{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008540 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008541 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008542 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008543
David Sterba712e36c2017-10-31 17:08:27 +01008544 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008545 if (!ei)
8546 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008547
8548 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008549 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008550 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008551 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008552 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008553 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008554 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008555 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008556 ei->disk_i_size = 0;
8557 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008558 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008559 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008560 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008561 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008562 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008563 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008564
Josef Bacik9e0baf62011-07-15 15:16:44 +00008565 spin_lock_init(&ei->lock);
8566 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008567 if (sb->s_magic != BTRFS_TEST_MAGIC)
8568 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8569 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008570 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008571 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008572 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008573
Miao Xie16cdcec2011-04-22 18:12:22 +08008574 ei->delayed_node = NULL;
8575
chandan r9cc97d62012-07-04 12:48:07 +05308576 ei->i_otime.tv_sec = 0;
8577 ei->i_otime.tv_nsec = 0;
8578
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008579 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008580 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008581 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8582 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8583 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008584 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8585 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008586 ei->io_tree.track_uptodate = true;
8587 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008588 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008589 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008590 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008591 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008592 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008593 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008594 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008595
8596 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008597}
8598
Josef Bacikaaedb552013-10-11 14:44:09 -04008599#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8600void btrfs_test_destroy_inode(struct inode *inode)
8601{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008602 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008603 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8604}
8605#endif
8606
Al Viro26602ca2019-04-10 15:14:41 -04008607void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008608{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008609 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8610}
8611
Chris Mason39279cc2007-06-12 06:35:45 -04008612void btrfs_destroy_inode(struct inode *inode)
8613{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008614 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008615 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008616 struct btrfs_root *root = BTRFS_I(inode)->root;
8617
Al Virob3d9b7a2012-06-09 13:51:19 -04008618 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008619 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008620 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8621 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008622 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008623 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008624 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008625 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008626 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008627
Chris Mason5a3f23d2009-03-31 13:27:11 -04008628 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008629 * This can happen where we create an inode, but somebody else also
8630 * created the same inode and we need to destroy the one we already
8631 * created.
8632 */
8633 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008634 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008635
Chris Masond3977122009-01-05 21:25:51 -05008636 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008637 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8638 if (!ordered)
8639 break;
8640 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008641 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008642 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008643 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008644 btrfs_remove_ordered_extent(inode, ordered);
8645 btrfs_put_ordered_extent(ordered);
8646 btrfs_put_ordered_extent(ordered);
8647 }
8648 }
Nikolay Borisovcfdd4592020-06-03 08:55:46 +03008649 btrfs_qgroup_check_reserved_leak(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008650 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008651 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008652 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008653 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008654}
8655
Al Viro45321ac2010-06-07 13:43:19 -04008656int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008657{
8658 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008659
Naohiro Aota6379ef92013-06-06 09:56:34 +00008660 if (root == NULL)
8661 return 1;
8662
Liu Bofa6ac872013-02-20 14:10:23 +00008663 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008664 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008665 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008666 else
Al Viro45321ac2010-06-07 13:43:19 -04008667 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008668}
8669
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008670static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008671{
8672 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8673
8674 inode_init_once(&ei->vfs_inode);
8675}
8676
David Sterbae67c7182018-02-19 17:24:18 +01008677void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008678{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008679 /*
8680 * Make sure all delayed rcu free inodes are flushed before we
8681 * destroy cache.
8682 */
8683 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008684 kmem_cache_destroy(btrfs_inode_cachep);
8685 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008686 kmem_cache_destroy(btrfs_path_cachep);
8687 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008688 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008689}
8690
Liu Bof5c29bd2017-11-02 17:21:50 -06008691int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008692{
David Sterba837e1972012-09-07 03:00:48 -06008693 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008694 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008695 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8696 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008697 if (!btrfs_inode_cachep)
8698 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008699
David Sterba837e1972012-09-07 03:00:48 -06008700 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008701 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008702 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008703 if (!btrfs_trans_handle_cachep)
8704 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008705
David Sterba837e1972012-09-07 03:00:48 -06008706 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008707 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008708 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008709 if (!btrfs_path_cachep)
8710 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008711
David Sterba837e1972012-09-07 03:00:48 -06008712 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008713 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008714 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008715 if (!btrfs_free_space_cachep)
8716 goto fail;
8717
Christophe Leroy3acd4852019-08-21 15:05:55 +00008718 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8719 PAGE_SIZE, PAGE_SIZE,
8720 SLAB_RED_ZONE, NULL);
8721 if (!btrfs_free_space_bitmap_cachep)
8722 goto fail;
8723
Chris Mason39279cc2007-06-12 06:35:45 -04008724 return 0;
8725fail:
8726 btrfs_destroy_cachep();
8727 return -ENOMEM;
8728}
8729
David Howellsa528d352017-01-31 16:46:22 +00008730static int btrfs_getattr(const struct path *path, struct kstat *stat,
8731 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008732{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008733 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008734 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008735 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008736 u32 bi_flags = BTRFS_I(inode)->flags;
8737
8738 stat->result_mask |= STATX_BTIME;
8739 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8740 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8741 if (bi_flags & BTRFS_INODE_APPEND)
8742 stat->attributes |= STATX_ATTR_APPEND;
8743 if (bi_flags & BTRFS_INODE_COMPRESS)
8744 stat->attributes |= STATX_ATTR_COMPRESSED;
8745 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8746 stat->attributes |= STATX_ATTR_IMMUTABLE;
8747 if (bi_flags & BTRFS_INODE_NODUMP)
8748 stat->attributes |= STATX_ATTR_NODUMP;
8749
8750 stat->attributes_mask |= (STATX_ATTR_APPEND |
8751 STATX_ATTR_COMPRESSED |
8752 STATX_ATTR_IMMUTABLE |
8753 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008754
Chris Mason39279cc2007-06-12 06:35:45 -04008755 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008756 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008757
8758 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008759 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008760 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008761 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008762 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008763 return 0;
8764}
8765
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008766static int btrfs_rename_exchange(struct inode *old_dir,
8767 struct dentry *old_dentry,
8768 struct inode *new_dir,
8769 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008770{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008771 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008772 struct btrfs_trans_handle *trans;
8773 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008774 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008775 struct inode *new_inode = new_dentry->d_inode;
8776 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008777 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008778 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008779 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8780 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008781 u64 old_idx = 0;
8782 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008783 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008784 bool root_log_pinned = false;
8785 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008786 struct btrfs_log_ctx ctx_root;
8787 struct btrfs_log_ctx ctx_dest;
8788 bool sync_log_root = false;
8789 bool sync_log_dest = false;
8790 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008791
8792 /* we only allow rename subvolume link between subvolumes */
8793 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8794 return -EXDEV;
8795
Filipe Mananad4682ba2018-06-11 19:24:28 +01008796 btrfs_init_log_ctx(&ctx_root, old_inode);
8797 btrfs_init_log_ctx(&ctx_dest, new_inode);
8798
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008799 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008800 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8801 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008802 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008803
8804 /*
8805 * We want to reserve the absolute worst case amount of items. So if
8806 * both inodes are subvols and we need to unlink them then that would
8807 * require 4 item modifications, but if they are both normal inodes it
8808 * would require 5 item modifications, so we'll assume their normal
8809 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8810 * should cover the worst case number of items we'll modify.
8811 */
8812 trans = btrfs_start_transaction(root, 12);
8813 if (IS_ERR(trans)) {
8814 ret = PTR_ERR(trans);
8815 goto out_notrans;
8816 }
8817
Josef Bacik3e174092019-11-15 15:43:06 -05008818 if (dest != root)
8819 btrfs_record_root_in_trans(trans, dest);
8820
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008821 /*
8822 * We need to find a free sequence number both in the source and
8823 * in the destination directory for the exchange.
8824 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008825 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008826 if (ret)
8827 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008828 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008829 if (ret)
8830 goto out_fail;
8831
8832 BTRFS_I(old_inode)->dir_index = 0ULL;
8833 BTRFS_I(new_inode)->dir_index = 0ULL;
8834
8835 /* Reference for the source. */
8836 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8837 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008838 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008839 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008840 btrfs_pin_log_trans(root);
8841 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008842 ret = btrfs_insert_inode_ref(trans, dest,
8843 new_dentry->d_name.name,
8844 new_dentry->d_name.len,
8845 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008846 btrfs_ino(BTRFS_I(new_dir)),
8847 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008848 if (ret)
8849 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008850 }
8851
8852 /* And now for the dest. */
8853 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8854 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008855 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008856 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008857 btrfs_pin_log_trans(dest);
8858 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008859 ret = btrfs_insert_inode_ref(trans, root,
8860 old_dentry->d_name.name,
8861 old_dentry->d_name.len,
8862 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008863 btrfs_ino(BTRFS_I(old_dir)),
8864 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008865 if (ret)
8866 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008867 }
8868
8869 /* Update inode version and ctime/mtime. */
8870 inode_inc_iversion(old_dir);
8871 inode_inc_iversion(new_dir);
8872 inode_inc_iversion(old_inode);
8873 inode_inc_iversion(new_inode);
8874 old_dir->i_ctime = old_dir->i_mtime = ctime;
8875 new_dir->i_ctime = new_dir->i_mtime = ctime;
8876 old_inode->i_ctime = ctime;
8877 new_inode->i_ctime = ctime;
8878
8879 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008880 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8881 BTRFS_I(old_inode), 1);
8882 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8883 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008884 }
8885
8886 /* src is a subvolume */
8887 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008888 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008889 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008890 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8891 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008892 old_dentry->d_name.name,
8893 old_dentry->d_name.len);
8894 if (!ret)
8895 ret = btrfs_update_inode(trans, root, old_inode);
8896 }
8897 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008898 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008899 goto out_fail;
8900 }
8901
8902 /* dest is a subvolume */
8903 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008904 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008905 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008906 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8907 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008908 new_dentry->d_name.name,
8909 new_dentry->d_name.len);
8910 if (!ret)
8911 ret = btrfs_update_inode(trans, dest, new_inode);
8912 }
8913 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008914 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008915 goto out_fail;
8916 }
8917
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008918 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008919 new_dentry->d_name.name,
8920 new_dentry->d_name.len, 0, old_idx);
8921 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008922 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008923 goto out_fail;
8924 }
8925
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008926 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008927 old_dentry->d_name.name,
8928 old_dentry->d_name.len, 0, new_idx);
8929 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008930 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008931 goto out_fail;
8932 }
8933
8934 if (old_inode->i_nlink == 1)
8935 BTRFS_I(old_inode)->dir_index = old_idx;
8936 if (new_inode->i_nlink == 1)
8937 BTRFS_I(new_inode)->dir_index = new_idx;
8938
Filipe Manana86e8aa02016-05-05 02:02:27 +01008939 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008940 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008941 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8942 BTRFS_I(old_dir), parent,
8943 false, &ctx_root);
8944 if (ret == BTRFS_NEED_LOG_SYNC)
8945 sync_log_root = true;
8946 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8947 commit_transaction = true;
8948 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008949 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008950 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008951 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008952 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008953 if (!commit_transaction) {
8954 parent = old_dentry->d_parent;
8955 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8956 BTRFS_I(new_dir), parent,
8957 false, &ctx_dest);
8958 if (ret == BTRFS_NEED_LOG_SYNC)
8959 sync_log_dest = true;
8960 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8961 commit_transaction = true;
8962 ret = 0;
8963 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008964 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008965 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008966 }
8967out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008968 /*
8969 * If we have pinned a log and an error happened, we unpin tasks
8970 * trying to sync the log and force them to fallback to a transaction
8971 * commit if the log currently contains any of the inodes involved in
8972 * this rename operation (to ensure we do not persist a log with an
8973 * inconsistent state for any of these inodes or leading to any
8974 * inconsistencies when replayed). If the transaction was aborted, the
8975 * abortion reason is propagated to userspace when attempting to commit
8976 * the transaction. If the log does not contain any of these inodes, we
8977 * allow the tasks to sync it.
8978 */
8979 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008980 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8981 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8982 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008983 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008984 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008985 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008986
8987 if (root_log_pinned) {
8988 btrfs_end_log_trans(root);
8989 root_log_pinned = false;
8990 }
8991 if (dest_log_pinned) {
8992 btrfs_end_log_trans(dest);
8993 dest_log_pinned = false;
8994 }
8995 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008996 if (!ret && sync_log_root && !commit_transaction) {
8997 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
8998 &ctx_root);
8999 if (ret)
9000 commit_transaction = true;
9001 }
9002 if (!ret && sync_log_dest && !commit_transaction) {
9003 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9004 &ctx_dest);
9005 if (ret)
9006 commit_transaction = true;
9007 }
9008 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009009 /*
9010 * We may have set commit_transaction when logging the new name
9011 * in the destination root, in which case we left the source
9012 * root context in the list of log contextes. So make sure we
9013 * remove it to avoid invalid memory accesses, since the context
9014 * was allocated in our stack frame.
9015 */
9016 if (sync_log_root) {
9017 mutex_lock(&root->log_mutex);
9018 list_del_init(&ctx_root.list);
9019 mutex_unlock(&root->log_mutex);
9020 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009021 ret = btrfs_commit_transaction(trans);
9022 } else {
9023 int ret2;
9024
9025 ret2 = btrfs_end_transaction(trans);
9026 ret = ret ? ret : ret2;
9027 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009028out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009029 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9030 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009031 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009032
Filipe Mananae6c61712019-11-08 16:11:56 +00009033 ASSERT(list_empty(&ctx_root.list));
9034 ASSERT(list_empty(&ctx_dest.list));
9035
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009036 return ret;
9037}
9038
9039static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9040 struct btrfs_root *root,
9041 struct inode *dir,
9042 struct dentry *dentry)
9043{
9044 int ret;
9045 struct inode *inode;
9046 u64 objectid;
9047 u64 index;
9048
9049 ret = btrfs_find_free_ino(root, &objectid);
9050 if (ret)
9051 return ret;
9052
9053 inode = btrfs_new_inode(trans, root, dir,
9054 dentry->d_name.name,
9055 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009056 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009057 objectid,
9058 S_IFCHR | WHITEOUT_MODE,
9059 &index);
9060
9061 if (IS_ERR(inode)) {
9062 ret = PTR_ERR(inode);
9063 return ret;
9064 }
9065
9066 inode->i_op = &btrfs_special_inode_operations;
9067 init_special_inode(inode, inode->i_mode,
9068 WHITEOUT_DEV);
9069
9070 ret = btrfs_init_inode_security(trans, inode, dir,
9071 &dentry->d_name);
9072 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009073 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009074
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009075 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9076 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009077 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009078 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009079
9080 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009081out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009082 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009083 if (ret)
9084 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009085 iput(inode);
9086
Filipe Mananac9901612016-05-05 01:41:57 +01009087 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009088}
9089
Chris Mason39279cc2007-06-12 06:35:45 -04009090static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009091 struct inode *new_dir, struct dentry *new_dentry,
9092 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009093{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009094 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009095 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009096 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009097 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9098 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009099 struct inode *new_inode = d_inode(new_dentry);
9100 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009101 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009102 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009103 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009104 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009105 struct btrfs_log_ctx ctx;
9106 bool sync_log = false;
9107 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009108
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009109 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009110 return -EPERM;
9111
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009112 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009113 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009114 return -EXDEV;
9115
Li Zefan33345d012011-04-20 10:31:50 +08009116 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009117 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009118 return -ENOTEMPTY;
9119
Chris Mason39279cc2007-06-12 06:35:45 -04009120 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009121 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009122 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009123
9124
9125 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009126 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009127 new_dentry->d_name.name,
9128 new_dentry->d_name.len);
9129
9130 if (ret) {
9131 if (ret == -EEXIST) {
9132 /* we shouldn't get
9133 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309134 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009135 return ret;
9136 }
9137 } else {
9138 /* maybe -EOVERFLOW */
9139 return ret;
9140 }
9141 }
9142 ret = 0;
9143
Chris Mason5a3f23d2009-03-31 13:27:11 -04009144 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009145 * we're using rename to replace one file with another. Start IO on it
9146 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009147 */
Chris Mason8d875f92014-08-12 10:47:42 -07009148 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009149 filemap_flush(old_inode->i_mapping);
9150
Yan, Zheng76dda932009-09-21 16:00:26 -04009151 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009152 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009153 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009154 /*
9155 * We want to reserve the absolute worst case amount of items. So if
9156 * both inodes are subvols and we need to unlink them then that would
9157 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009158 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009159 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9160 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009161 * If our rename has the whiteout flag, we need more 5 units for the
9162 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9163 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009164 */
Filipe Manana5062af32016-05-05 10:26:26 +01009165 trans_num_items = 11;
9166 if (flags & RENAME_WHITEOUT)
9167 trans_num_items += 5;
9168 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009169 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009170 ret = PTR_ERR(trans);
9171 goto out_notrans;
9172 }
Chris Mason5f39d392007-10-15 16:14:19 -04009173
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009174 if (dest != root)
9175 btrfs_record_root_in_trans(trans, dest);
9176
Nikolay Borisov877574e2017-02-20 13:50:33 +02009177 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009178 if (ret)
9179 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009180
Miao Xie67de1172013-12-26 13:07:06 +08009181 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009182 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009183 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009184 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009185 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009186 btrfs_pin_log_trans(root);
9187 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009188 ret = btrfs_insert_inode_ref(trans, dest,
9189 new_dentry->d_name.name,
9190 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009191 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009192 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009193 if (ret)
9194 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009195 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009196
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009197 inode_inc_iversion(old_dir);
9198 inode_inc_iversion(new_dir);
9199 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009200 old_dir->i_ctime = old_dir->i_mtime =
9201 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009202 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009203
Chris Mason12fcfd22009-03-24 10:24:20 -04009204 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009205 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9206 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009207
Li Zefan33345d012011-04-20 10:31:50 +08009208 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009209 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009210 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009211 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9212 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009213 old_dentry->d_name.name,
9214 old_dentry->d_name.len);
9215 if (!ret)
9216 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009217 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009218 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009219 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009220 goto out_fail;
9221 }
Chris Mason39279cc2007-06-12 06:35:45 -04009222
9223 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009224 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009225 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009226 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009227 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009228 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009229 BUG_ON(new_inode->i_nlink == 0);
9230 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009231 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9232 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009233 new_dentry->d_name.name,
9234 new_dentry->d_name.len);
9235 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009236 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009237 ret = btrfs_orphan_add(trans,
9238 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009239 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009240 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009241 goto out_fail;
9242 }
Chris Mason39279cc2007-06-12 06:35:45 -04009243 }
Josef Bacikaec74772008-07-24 12:12:38 -04009244
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009245 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009246 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009247 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009248 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009249 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009250 goto out_fail;
9251 }
Chris Mason39279cc2007-06-12 06:35:45 -04009252
Miao Xie67de1172013-12-26 13:07:06 +08009253 if (old_inode->i_nlink == 1)
9254 BTRFS_I(old_inode)->dir_index = index;
9255
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009256 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009257 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009258
Filipe Mananad4682ba2018-06-11 19:24:28 +01009259 btrfs_init_log_ctx(&ctx, old_inode);
9260 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9261 BTRFS_I(old_dir), parent,
9262 false, &ctx);
9263 if (ret == BTRFS_NEED_LOG_SYNC)
9264 sync_log = true;
9265 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9266 commit_transaction = true;
9267 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009268 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009269 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009270 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009271
9272 if (flags & RENAME_WHITEOUT) {
9273 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9274 old_dentry);
9275
9276 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009277 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009278 goto out_fail;
9279 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009280 }
Chris Mason39279cc2007-06-12 06:35:45 -04009281out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009282 /*
9283 * If we have pinned the log and an error happened, we unpin tasks
9284 * trying to sync the log and force them to fallback to a transaction
9285 * commit if the log currently contains any of the inodes involved in
9286 * this rename operation (to ensure we do not persist a log with an
9287 * inconsistent state for any of these inodes or leading to any
9288 * inconsistencies when replayed). If the transaction was aborted, the
9289 * abortion reason is propagated to userspace when attempting to commit
9290 * the transaction. If the log does not contain any of these inodes, we
9291 * allow the tasks to sync it.
9292 */
9293 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009294 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9295 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9296 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009297 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009298 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009299 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009300
9301 btrfs_end_log_trans(root);
9302 log_pinned = false;
9303 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009304 if (!ret && sync_log) {
9305 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9306 if (ret)
9307 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009308 } else if (sync_log) {
9309 mutex_lock(&root->log_mutex);
9310 list_del(&ctx.list);
9311 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009312 }
9313 if (commit_transaction) {
9314 ret = btrfs_commit_transaction(trans);
9315 } else {
9316 int ret2;
9317
9318 ret2 = btrfs_end_transaction(trans);
9319 ret = ret ? ret : ret2;
9320 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009321out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009322 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009323 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009324
Chris Mason39279cc2007-06-12 06:35:45 -04009325 return ret;
9326}
9327
Miklos Szeredi80ace852014-07-23 15:15:32 +02009328static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9329 struct inode *new_dir, struct dentry *new_dentry,
9330 unsigned int flags)
9331{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009332 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009333 return -EINVAL;
9334
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009335 if (flags & RENAME_EXCHANGE)
9336 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9337 new_dentry);
9338
9339 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009340}
9341
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009342struct btrfs_delalloc_work {
9343 struct inode *inode;
9344 struct completion completion;
9345 struct list_head list;
9346 struct btrfs_work work;
9347};
9348
Miao Xie8ccf6f192012-10-25 09:28:04 +00009349static void btrfs_run_delalloc_work(struct btrfs_work *work)
9350{
9351 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009352 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009353
9354 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9355 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009356 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009357 filemap_flush(inode->i_mapping);
9358 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9359 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009360 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009361
Nikolay Borisov076da912018-04-23 10:54:16 +03009362 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009363 complete(&delalloc_work->completion);
9364}
9365
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009366static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009367{
9368 struct btrfs_delalloc_work *work;
9369
David Sterba100d5702015-12-08 14:39:32 +01009370 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009371 if (!work)
9372 return NULL;
9373
9374 init_completion(&work->completion);
9375 INIT_LIST_HEAD(&work->list);
9376 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009377 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009378
9379 return work;
9380}
9381
Chris Masond352ac62008-09-29 15:18:18 -04009382/*
9383 * some fairly slow code that needs optimization. This walks the list
9384 * of all the inodes with pending delalloc and forces them to disk.
9385 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009386static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009387{
Chris Masonea8c2812008-08-04 23:17:27 -04009388 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009389 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009390 struct btrfs_delalloc_work *work, *next;
9391 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009392 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009393 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009394
Miao Xie8ccf6f192012-10-25 09:28:04 +00009395 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009396 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009397
Miao Xie573bfb72014-03-06 13:55:03 +08009398 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009399 spin_lock(&root->delalloc_lock);
9400 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009401 while (!list_empty(&splice)) {
9402 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009403 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009404
Miao Xieeb73c1b2013-05-15 07:48:22 +00009405 list_move_tail(&binode->delalloc_inodes,
9406 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009407 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009408 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009409 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009410 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009411 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009412 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009413
Ethan Lien3cd24c62018-11-01 14:49:03 +08009414 if (snapshot)
9415 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9416 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009417 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009418 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009419 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009420 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009421 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009422 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009423 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009424 btrfs_queue_work(root->fs_info->flush_workers,
9425 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009426 ret++;
9427 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009428 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009429 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009430 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009431 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009432 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009433
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009434out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009435 list_for_each_entry_safe(work, next, &works, list) {
9436 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009437 wait_for_completion(&work->completion);
9438 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009439 }
9440
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009441 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009442 spin_lock(&root->delalloc_lock);
9443 list_splice_tail(&splice, &root->delalloc_inodes);
9444 spin_unlock(&root->delalloc_lock);
9445 }
Miao Xie573bfb72014-03-06 13:55:03 +08009446 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009447 return ret;
9448}
9449
Ethan Lien3cd24c62018-11-01 14:49:03 +08009450int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009451{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009452 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009453 int ret;
9454
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009455 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009456 return -EROFS;
9457
Ethan Lien3cd24c62018-11-01 14:49:03 +08009458 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009459 if (ret > 0)
9460 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009461 return ret;
9462}
9463
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009464int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009465{
9466 struct btrfs_root *root;
9467 struct list_head splice;
9468 int ret;
9469
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009470 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009471 return -EROFS;
9472
9473 INIT_LIST_HEAD(&splice);
9474
Miao Xie573bfb72014-03-06 13:55:03 +08009475 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009476 spin_lock(&fs_info->delalloc_root_lock);
9477 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009478 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009479 root = list_first_entry(&splice, struct btrfs_root,
9480 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009481 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009482 BUG_ON(!root);
9483 list_move_tail(&root->delalloc_root,
9484 &fs_info->delalloc_roots);
9485 spin_unlock(&fs_info->delalloc_root_lock);
9486
Ethan Lien3cd24c62018-11-01 14:49:03 +08009487 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009488 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009489 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009490 goto out;
9491
Miao Xie6c255e62014-03-06 13:55:01 +08009492 if (nr != -1) {
9493 nr -= ret;
9494 WARN_ON(nr < 0);
9495 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009496 spin_lock(&fs_info->delalloc_root_lock);
9497 }
9498 spin_unlock(&fs_info->delalloc_root_lock);
9499
Miao Xie6c255e62014-03-06 13:55:01 +08009500 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009501out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009502 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009503 spin_lock(&fs_info->delalloc_root_lock);
9504 list_splice_tail(&splice, &fs_info->delalloc_roots);
9505 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009506 }
Miao Xie573bfb72014-03-06 13:55:03 +08009507 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009508 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009509}
9510
Chris Mason39279cc2007-06-12 06:35:45 -04009511static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9512 const char *symname)
9513{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009514 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009515 struct btrfs_trans_handle *trans;
9516 struct btrfs_root *root = BTRFS_I(dir)->root;
9517 struct btrfs_path *path;
9518 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009519 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009520 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009521 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309522 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009523 int name_len;
9524 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009525 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009526 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009527 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009528
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009529 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009530 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009531 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009532
Josef Bacik9ed74f22009-09-11 16:12:44 -04009533 /*
9534 * 2 items for inode item and ref
9535 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009536 * 1 item for updating parent inode item
9537 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009538 * 1 item for xattr if selinux is on
9539 */
Filipe Manana9269d122015-12-31 18:16:29 +00009540 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009541 if (IS_ERR(trans))
9542 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009543
Li Zefan581bb052011-04-20 10:06:11 +08009544 err = btrfs_find_free_ino(root, &objectid);
9545 if (err)
9546 goto out_unlock;
9547
Josef Bacikaec74772008-07-24 12:12:38 -04009548 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009549 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9550 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009551 if (IS_ERR(inode)) {
9552 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009553 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009554 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009555 }
Chris Mason39279cc2007-06-12 06:35:45 -04009556
Casey Schauflerad19db72011-12-15 10:09:07 -05009557 /*
9558 * If the active LSM wants to access the inode during
9559 * d_instantiate it needs these. Smack checks to see
9560 * if the filesystem supports xattrs by looking at the
9561 * ops vector.
9562 */
9563 inode->i_fop = &btrfs_file_operations;
9564 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009565 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009566 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9567
9568 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9569 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009570 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009571
Chris Mason39279cc2007-06-12 06:35:45 -04009572 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009573 if (!path) {
9574 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009575 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009576 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009577 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009578 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009579 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009580 datasize = btrfs_file_extent_calc_inline_size(name_len);
9581 err = btrfs_insert_empty_item(trans, root, path, &key,
9582 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009583 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009584 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009585 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009586 }
Chris Mason5f39d392007-10-15 16:14:19 -04009587 leaf = path->nodes[0];
9588 ei = btrfs_item_ptr(leaf, path->slots[0],
9589 struct btrfs_file_extent_item);
9590 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9591 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009592 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009593 btrfs_set_file_extent_encryption(leaf, ei, 0);
9594 btrfs_set_file_extent_compression(leaf, ei, 0);
9595 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9596 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9597
Chris Mason39279cc2007-06-12 06:35:45 -04009598 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009599 write_extent_buffer(leaf, symname, ptr, name_len);
9600 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009601 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009602
Chris Mason39279cc2007-06-12 06:35:45 -04009603 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009604 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009605 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009606 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009607 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009608 /*
9609 * Last step, add directory indexes for our symlink inode. This is the
9610 * last step to avoid extra cleanup of these indexes if an error happens
9611 * elsewhere above.
9612 */
9613 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009614 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9615 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009616 if (err)
9617 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009618
Al Viro1e2e5472018-05-04 08:23:01 -04009619 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009620
9621out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009622 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009623 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009624 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009625 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009626 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009627 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009628 return err;
9629}
Chris Mason16432982008-04-10 10:23:21 -04009630
Qu Wenruo203f44c52020-06-10 09:04:40 +08009631static int insert_prealloc_file_extent(struct btrfs_trans_handle *trans,
9632 struct inode *inode, struct btrfs_key *ins,
9633 u64 file_offset)
9634{
9635 struct btrfs_file_extent_item stack_fi;
9636 u64 start = ins->objectid;
9637 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009638 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009639
9640 memset(&stack_fi, 0, sizeof(stack_fi));
9641
9642 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9643 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9644 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9645 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9646 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9647 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9648 /* Encryption and other encoding is reserved and all 0 */
9649
Nikolay Borisov72b7d152020-06-03 08:55:18 +03009650 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009651 if (ret < 0)
9652 return ret;
Nikolay Borisovc553f942020-06-03 08:55:19 +03009653 return insert_reserved_file_extent(trans, BTRFS_I(inode), file_offset,
Qu Wenruo9729f102020-06-10 09:04:41 +08009654 &stack_fi, ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009655}
Josef Bacik0af3d002010-06-21 14:48:16 -04009656static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9657 u64 start, u64 num_bytes, u64 min_size,
9658 loff_t actual_len, u64 *alloc_hint,
9659 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009660{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009661 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009662 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9663 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009664 struct btrfs_root *root = BTRFS_I(inode)->root;
9665 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009666 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009667 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009668 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009669 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009670 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009671 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009672 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009673 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009674
Josef Bacik0af3d002010-06-21 14:48:16 -04009675 if (trans)
9676 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009677 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009678 if (own_trans) {
9679 trans = btrfs_start_transaction(root, 3);
9680 if (IS_ERR(trans)) {
9681 ret = PTR_ERR(trans);
9682 break;
9683 }
Yan Zhengd899e052008-10-30 14:25:28 -04009684 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009685
Byongho Leeee221842015-12-15 01:42:10 +09009686 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009687 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009688 /*
9689 * If we are severely fragmented we could end up with really
9690 * small allocations, so if the allocator is returning small
9691 * chunks lets make its job easier by only searching for those
9692 * sized chunks.
9693 */
9694 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009695 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9696 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009697 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009698 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009699 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009700 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009701 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009702
9703 /*
9704 * We've reserved this space, and thus converted it from
9705 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9706 * from here on out we will only need to clear our reservation
9707 * for the remaining unreserved area, so advance our
9708 * clear_offset by our extent size.
9709 */
9710 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009711 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009712
Josef Bacik0b670dc2015-09-23 17:11:16 -04009713 last_alloc = ins.offset;
Nikolay Borisovc553f942020-06-03 08:55:19 +03009714 ret = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009715 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009716 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009717 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009718 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009719 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009720 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009721 break;
9722 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009723
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009724 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009725 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009726
Josef Bacik5dc562c2012-08-17 13:14:17 -04009727 em = alloc_extent_map();
9728 if (!em) {
9729 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9730 &BTRFS_I(inode)->runtime_flags);
9731 goto next;
9732 }
9733
9734 em->start = cur_offset;
9735 em->orig_start = cur_offset;
9736 em->len = ins.offset;
9737 em->block_start = ins.objectid;
9738 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009739 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009740 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009741 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9742 em->generation = trans->transid;
9743
9744 while (1) {
9745 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009746 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009747 write_unlock(&em_tree->lock);
9748 if (ret != -EEXIST)
9749 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009750 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009751 cur_offset + ins.offset - 1,
9752 0);
9753 }
9754 free_extent_map(em);
9755next:
Yan Zhengd899e052008-10-30 14:25:28 -04009756 num_bytes -= ins.offset;
9757 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009758 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009759
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009760 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009761 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009762 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009763 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009764 (actual_len > inode->i_size) &&
9765 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009766 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009767 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009768 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009769 i_size = cur_offset;
9770 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009771 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009772 }
9773
Yan Zhengd899e052008-10-30 14:25:28 -04009774 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009775
9776 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009777 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009778 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009779 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009780 break;
9781 }
Yan Zhengd899e052008-10-30 14:25:28 -04009782
Josef Bacik0af3d002010-06-21 14:48:16 -04009783 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009784 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009785 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009786 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009787 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009788 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009789 return ret;
9790}
9791
Josef Bacik0af3d002010-06-21 14:48:16 -04009792int btrfs_prealloc_file_range(struct inode *inode, int mode,
9793 u64 start, u64 num_bytes, u64 min_size,
9794 loff_t actual_len, u64 *alloc_hint)
9795{
9796 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9797 min_size, actual_len, alloc_hint,
9798 NULL);
9799}
9800
9801int btrfs_prealloc_file_range_trans(struct inode *inode,
9802 struct btrfs_trans_handle *trans, int mode,
9803 u64 start, u64 num_bytes, u64 min_size,
9804 loff_t actual_len, u64 *alloc_hint)
9805{
9806 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9807 min_size, actual_len, alloc_hint, trans);
9808}
9809
Chris Masone6dcd2d2008-07-17 12:53:50 -04009810static int btrfs_set_page_dirty(struct page *page)
9811{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009812 return __set_page_dirty_nobuffers(page);
9813}
9814
Al Viro10556cb2011-06-20 19:28:19 -04009815static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009816{
Li Zefanb83cc962010-12-20 16:04:08 +08009817 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009818 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009819
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009820 if (mask & MAY_WRITE &&
9821 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9822 if (btrfs_root_readonly(root))
9823 return -EROFS;
9824 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9825 return -EACCES;
9826 }
Al Viro2830ba72011-06-20 19:16:29 -04009827 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009828}
Chris Mason39279cc2007-06-12 06:35:45 -04009829
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009830static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9831{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009832 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009833 struct btrfs_trans_handle *trans;
9834 struct btrfs_root *root = BTRFS_I(dir)->root;
9835 struct inode *inode = NULL;
9836 u64 objectid;
9837 u64 index;
9838 int ret = 0;
9839
9840 /*
9841 * 5 units required for adding orphan entry
9842 */
9843 trans = btrfs_start_transaction(root, 5);
9844 if (IS_ERR(trans))
9845 return PTR_ERR(trans);
9846
9847 ret = btrfs_find_free_ino(root, &objectid);
9848 if (ret)
9849 goto out;
9850
9851 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009852 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009853 if (IS_ERR(inode)) {
9854 ret = PTR_ERR(inode);
9855 inode = NULL;
9856 goto out;
9857 }
9858
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009859 inode->i_fop = &btrfs_file_operations;
9860 inode->i_op = &btrfs_file_inode_operations;
9861
9862 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009863 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9864
Chris Masonb0d5d102014-09-08 13:08:51 -07009865 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9866 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009867 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009868
9869 ret = btrfs_update_inode(trans, root, inode);
9870 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009871 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009872 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009873 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009874 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009875
Filipe Manana5762b5c2014-08-01 00:10:32 +01009876 /*
9877 * We set number of links to 0 in btrfs_new_inode(), and here we set
9878 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9879 * through:
9880 *
9881 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9882 */
9883 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009884 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009885 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009886 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009887out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009888 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009889 if (ret && inode)
9890 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009891 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009892 return ret;
9893}
9894
David Sterba5cdc84b2018-07-18 20:32:52 +02009895void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009896{
David Sterba5cdc84b2018-07-18 20:32:52 +02009897 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009898 unsigned long index = start >> PAGE_SHIFT;
9899 unsigned long end_index = end >> PAGE_SHIFT;
9900 struct page *page;
9901
9902 while (index <= end_index) {
9903 page = find_get_page(inode->i_mapping, index);
9904 ASSERT(page); /* Pages should be in the extent_io_tree */
9905 set_page_writeback(page);
9906 put_page(page);
9907 index++;
9908 }
9909}
9910
Omar Sandovaled46ff32016-11-03 10:28:14 -07009911#ifdef CONFIG_SWAP
9912/*
9913 * Add an entry indicating a block group or device which is pinned by a
9914 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9915 * negative errno on failure.
9916 */
9917static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9918 bool is_block_group)
9919{
9920 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9921 struct btrfs_swapfile_pin *sp, *entry;
9922 struct rb_node **p;
9923 struct rb_node *parent = NULL;
9924
9925 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9926 if (!sp)
9927 return -ENOMEM;
9928 sp->ptr = ptr;
9929 sp->inode = inode;
9930 sp->is_block_group = is_block_group;
9931
9932 spin_lock(&fs_info->swapfile_pins_lock);
9933 p = &fs_info->swapfile_pins.rb_node;
9934 while (*p) {
9935 parent = *p;
9936 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9937 if (sp->ptr < entry->ptr ||
9938 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9939 p = &(*p)->rb_left;
9940 } else if (sp->ptr > entry->ptr ||
9941 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9942 p = &(*p)->rb_right;
9943 } else {
9944 spin_unlock(&fs_info->swapfile_pins_lock);
9945 kfree(sp);
9946 return 1;
9947 }
9948 }
9949 rb_link_node(&sp->node, parent, p);
9950 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9951 spin_unlock(&fs_info->swapfile_pins_lock);
9952 return 0;
9953}
9954
9955/* Free all of the entries pinned by this swapfile. */
9956static void btrfs_free_swapfile_pins(struct inode *inode)
9957{
9958 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9959 struct btrfs_swapfile_pin *sp;
9960 struct rb_node *node, *next;
9961
9962 spin_lock(&fs_info->swapfile_pins_lock);
9963 node = rb_first(&fs_info->swapfile_pins);
9964 while (node) {
9965 next = rb_next(node);
9966 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9967 if (sp->inode == inode) {
9968 rb_erase(&sp->node, &fs_info->swapfile_pins);
9969 if (sp->is_block_group)
9970 btrfs_put_block_group(sp->ptr);
9971 kfree(sp);
9972 }
9973 node = next;
9974 }
9975 spin_unlock(&fs_info->swapfile_pins_lock);
9976}
9977
9978struct btrfs_swap_info {
9979 u64 start;
9980 u64 block_start;
9981 u64 block_len;
9982 u64 lowest_ppage;
9983 u64 highest_ppage;
9984 unsigned long nr_pages;
9985 int nr_extents;
9986};
9987
9988static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9989 struct btrfs_swap_info *bsi)
9990{
9991 unsigned long nr_pages;
9992 u64 first_ppage, first_ppage_reported, next_ppage;
9993 int ret;
9994
9995 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9996 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9997 PAGE_SIZE) >> PAGE_SHIFT;
9998
9999 if (first_ppage >= next_ppage)
10000 return 0;
10001 nr_pages = next_ppage - first_ppage;
10002
10003 first_ppage_reported = first_ppage;
10004 if (bsi->start == 0)
10005 first_ppage_reported++;
10006 if (bsi->lowest_ppage > first_ppage_reported)
10007 bsi->lowest_ppage = first_ppage_reported;
10008 if (bsi->highest_ppage < (next_ppage - 1))
10009 bsi->highest_ppage = next_ppage - 1;
10010
10011 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10012 if (ret < 0)
10013 return ret;
10014 bsi->nr_extents += ret;
10015 bsi->nr_pages += nr_pages;
10016 return 0;
10017}
10018
10019static void btrfs_swap_deactivate(struct file *file)
10020{
10021 struct inode *inode = file_inode(file);
10022
10023 btrfs_free_swapfile_pins(inode);
10024 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10025}
10026
10027static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10028 sector_t *span)
10029{
10030 struct inode *inode = file_inode(file);
10031 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10032 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10033 struct extent_state *cached_state = NULL;
10034 struct extent_map *em = NULL;
10035 struct btrfs_device *device = NULL;
10036 struct btrfs_swap_info bsi = {
10037 .lowest_ppage = (sector_t)-1ULL,
10038 };
10039 int ret = 0;
10040 u64 isize;
10041 u64 start;
10042
10043 /*
10044 * If the swap file was just created, make sure delalloc is done. If the
10045 * file changes again after this, the user is doing something stupid and
10046 * we don't really care.
10047 */
10048 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10049 if (ret)
10050 return ret;
10051
10052 /*
10053 * The inode is locked, so these flags won't change after we check them.
10054 */
10055 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10056 btrfs_warn(fs_info, "swapfile must not be compressed");
10057 return -EINVAL;
10058 }
10059 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10060 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10061 return -EINVAL;
10062 }
10063 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10064 btrfs_warn(fs_info, "swapfile must not be checksummed");
10065 return -EINVAL;
10066 }
10067
10068 /*
10069 * Balance or device remove/replace/resize can move stuff around from
10070 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10071 * concurrently while we are mapping the swap extents, and
10072 * fs_info->swapfile_pins prevents them from running while the swap file
10073 * is active and moving the extents. Note that this also prevents a
10074 * concurrent device add which isn't actually necessary, but it's not
10075 * really worth the trouble to allow it.
10076 */
10077 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10078 btrfs_warn(fs_info,
10079 "cannot activate swapfile while exclusive operation is running");
10080 return -EBUSY;
10081 }
10082 /*
10083 * Snapshots can create extents which require COW even if NODATACOW is
10084 * set. We use this counter to prevent snapshots. We must increment it
10085 * before walking the extents because we don't want a concurrent
10086 * snapshot to run after we've already checked the extents.
10087 */
10088 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10089
10090 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10091
10092 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10093 start = 0;
10094 while (start < isize) {
10095 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010096 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010097 u64 len = isize - start;
10098
Omar Sandoval39b07b52019-12-02 17:34:23 -080010099 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010100 if (IS_ERR(em)) {
10101 ret = PTR_ERR(em);
10102 goto out;
10103 }
10104
10105 if (em->block_start == EXTENT_MAP_HOLE) {
10106 btrfs_warn(fs_info, "swapfile must not have holes");
10107 ret = -EINVAL;
10108 goto out;
10109 }
10110 if (em->block_start == EXTENT_MAP_INLINE) {
10111 /*
10112 * It's unlikely we'll ever actually find ourselves
10113 * here, as a file small enough to fit inline won't be
10114 * big enough to store more than the swap header, but in
10115 * case something changes in the future, let's catch it
10116 * here rather than later.
10117 */
10118 btrfs_warn(fs_info, "swapfile must not be inline");
10119 ret = -EINVAL;
10120 goto out;
10121 }
10122 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10123 btrfs_warn(fs_info, "swapfile must not be compressed");
10124 ret = -EINVAL;
10125 goto out;
10126 }
10127
10128 logical_block_start = em->block_start + (start - em->start);
10129 len = min(len, em->len - (start - em->start));
10130 free_extent_map(em);
10131 em = NULL;
10132
10133 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10134 if (ret < 0) {
10135 goto out;
10136 } else if (ret) {
10137 ret = 0;
10138 } else {
10139 btrfs_warn(fs_info,
10140 "swapfile must not be copy-on-write");
10141 ret = -EINVAL;
10142 goto out;
10143 }
10144
10145 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10146 if (IS_ERR(em)) {
10147 ret = PTR_ERR(em);
10148 goto out;
10149 }
10150
10151 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10152 btrfs_warn(fs_info,
10153 "swapfile must have single data profile");
10154 ret = -EINVAL;
10155 goto out;
10156 }
10157
10158 if (device == NULL) {
10159 device = em->map_lookup->stripes[0].dev;
10160 ret = btrfs_add_swapfile_pin(inode, device, false);
10161 if (ret == 1)
10162 ret = 0;
10163 else if (ret)
10164 goto out;
10165 } else if (device != em->map_lookup->stripes[0].dev) {
10166 btrfs_warn(fs_info, "swapfile must be on one device");
10167 ret = -EINVAL;
10168 goto out;
10169 }
10170
10171 physical_block_start = (em->map_lookup->stripes[0].physical +
10172 (logical_block_start - em->start));
10173 len = min(len, em->len - (logical_block_start - em->start));
10174 free_extent_map(em);
10175 em = NULL;
10176
10177 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10178 if (!bg) {
10179 btrfs_warn(fs_info,
10180 "could not find block group containing swapfile");
10181 ret = -EINVAL;
10182 goto out;
10183 }
10184
10185 ret = btrfs_add_swapfile_pin(inode, bg, true);
10186 if (ret) {
10187 btrfs_put_block_group(bg);
10188 if (ret == 1)
10189 ret = 0;
10190 else
10191 goto out;
10192 }
10193
10194 if (bsi.block_len &&
10195 bsi.block_start + bsi.block_len == physical_block_start) {
10196 bsi.block_len += len;
10197 } else {
10198 if (bsi.block_len) {
10199 ret = btrfs_add_swap_extent(sis, &bsi);
10200 if (ret)
10201 goto out;
10202 }
10203 bsi.start = start;
10204 bsi.block_start = physical_block_start;
10205 bsi.block_len = len;
10206 }
10207
10208 start += len;
10209 }
10210
10211 if (bsi.block_len)
10212 ret = btrfs_add_swap_extent(sis, &bsi);
10213
10214out:
10215 if (!IS_ERR_OR_NULL(em))
10216 free_extent_map(em);
10217
10218 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10219
10220 if (ret)
10221 btrfs_swap_deactivate(file);
10222
10223 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10224
10225 if (ret)
10226 return ret;
10227
10228 if (device)
10229 sis->bdev = device->bdev;
10230 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10231 sis->max = bsi.nr_pages;
10232 sis->pages = bsi.nr_pages - 1;
10233 sis->highest_bit = bsi.nr_pages - 1;
10234 return bsi.nr_extents;
10235}
10236#else
10237static void btrfs_swap_deactivate(struct file *file)
10238{
10239}
10240
10241static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10242 sector_t *span)
10243{
10244 return -EOPNOTSUPP;
10245}
10246#endif
10247
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010248static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010249 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010250 .lookup = btrfs_lookup,
10251 .create = btrfs_create,
10252 .unlink = btrfs_unlink,
10253 .link = btrfs_link,
10254 .mkdir = btrfs_mkdir,
10255 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010256 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010257 .symlink = btrfs_symlink,
10258 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010259 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010260 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010261 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010262 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010263 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010264 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010265 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010266};
Yan, Zheng76dda932009-09-21 16:00:26 -040010267
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010268static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010269 .llseek = generic_file_llseek,
10270 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010271 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010272 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010273 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010274#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010275 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010276#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010277 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010278 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010279};
10280
David Sterba20e55062015-11-19 11:42:28 +010010281static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010282 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010283 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010284 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10285};
10286
Chris Mason35054392009-01-21 13:11:13 -050010287/*
10288 * btrfs doesn't support the bmap operation because swapfiles
10289 * use bmap to make a mapping of extents in the file. They assume
10290 * these extents won't change over the life of the file and they
10291 * use the bmap result to do IO directly to the drive.
10292 *
10293 * the btrfs bmap call would return logical addresses that aren't
10294 * suitable for IO and they also will change frequently as COW
10295 * operations happen. So, swapfile + btrfs == corruption.
10296 *
10297 * For now we're avoiding this by dropping bmap.
10298 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010299static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010300 .readpage = btrfs_readpage,
10301 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010302 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010303 .readahead = btrfs_readahead,
David Sterba55e20bd2020-06-09 19:56:06 +020010304 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010305 .invalidatepage = btrfs_invalidatepage,
10306 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010307#ifdef CONFIG_MIGRATION
10308 .migratepage = btrfs_migratepage,
10309#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010310 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010311 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010312 .swap_activate = btrfs_swap_activate,
10313 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010314};
10315
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010316static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010317 .getattr = btrfs_getattr,
10318 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010319 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010320 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010321 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010322 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010323 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010324 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010325};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010326static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010327 .getattr = btrfs_getattr,
10328 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010329 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010330 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010331 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010332 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010333 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010334};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010335static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010336 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010337 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010338 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010339 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010340 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010341 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010342};
Yan, Zheng76dda932009-09-21 16:00:26 -040010343
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010344const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010345 .d_delete = btrfs_dentry_delete,
10346};