blob: a487454b049dc5a0526f5653355ee5d4a5a17b10 [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
Chris Mason8f18cf12008-04-25 16:53:30 -04006#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05007#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -04008#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080031#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030032#include <linux/sched/mm.h>
David Sterba92d32172018-04-16 21:10:14 +020033#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020034#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040035#include "ctree.h"
36#include "disk-io.h"
37#include "transaction.h"
38#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040040#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040041#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040042#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020043#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040044#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050045#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050046#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080047#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000048#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080049#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040050#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040051#include "block-group.h"
Chris Mason39279cc2007-06-12 06:35:45 -040052
53struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080054 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040055 struct btrfs_root *root;
56};
57
Filipe Mananaf28a4922015-12-08 19:23:20 +000058struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000059 u64 reserve;
60 u64 unsubmitted_oe_range_start;
61 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080062 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000063};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070067static const struct inode_operations btrfs_special_inode_operations;
68static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070069static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010071static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
74struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050076struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000077struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078
Eric Sandeen3972f262013-01-12 02:57:22 +000079static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000080static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040081static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050082static noinline int cow_file_range(struct inode *inode,
83 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030084 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030085 unsigned long *nr_written, int unlock);
Liu Bo6f9994d2017-01-31 07:50:22 -080086static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
87 u64 orig_start, u64 block_start,
88 u64 block_len, u64 orig_block_len,
89 u64 ram_bytes, int compress_type,
90 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040091
Qu Wenruo524272602017-03-08 10:25:52 +080092static void __endio_write_update_ordered(struct inode *inode,
93 const u64 offset, const u64 bytes,
94 const bool uptodate);
95
96/*
97 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +010098 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +080099 *
100 * NOTE: caller must ensure that when an error happens, it can not call
101 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
102 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
103 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200104 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800105 */
106static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200107 struct page *locked_page,
108 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800109{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900110 unsigned long index = offset >> PAGE_SHIFT;
111 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200112 u64 page_start = page_offset(locked_page);
113 u64 page_end = page_start + PAGE_SIZE - 1;
114
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900115 struct page *page;
116
117 while (index <= end_index) {
118 page = find_get_page(inode->i_mapping, index);
119 index++;
120 if (!page)
121 continue;
122 ClearPagePrivate2(page);
123 put_page(page);
124 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200125
126 /*
127 * In case this page belongs to the delalloc range being instantiated
128 * then skip it, since the first page of a range is going to be
129 * properly cleaned up by the caller of run_delalloc_range
130 */
131 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
132 offset += PAGE_SIZE;
133 bytes -= PAGE_SIZE;
134 }
135
136 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800137}
138
Eric Sandeen48a3b632013-04-25 20:41:01 +0000139static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400140
Josef Bacik6a3891c2015-03-16 17:38:52 -0400141#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
142void btrfs_test_inode_set_ops(struct inode *inode)
143{
144 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
145}
146#endif
147
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000148static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500149 struct inode *inode, struct inode *dir,
150 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500151{
152 int err;
153
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000154 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500155 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500156 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500157 return err;
158}
159
Chris Masond352ac62008-09-29 15:18:18 -0400160/*
Chris Masonc8b97812008-10-29 14:49:59 -0400161 * this does all the hard work for inserting an inline extent into
162 * the btree. The caller should have done a btrfs_drop_extents so that
163 * no overlapping inline items exist in the btree
164 */
Chris Mason40f76582014-05-21 13:35:51 -0700165static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000166 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400167 struct btrfs_root *root, struct inode *inode,
168 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000169 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400170 struct page **compressed_pages)
171{
Chris Masonc8b97812008-10-29 14:49:59 -0400172 struct extent_buffer *leaf;
173 struct page *page = NULL;
174 char *kaddr;
175 unsigned long ptr;
176 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400177 int ret;
178 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400179 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400180
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800181 ASSERT((compressed_size > 0 && compressed_pages) ||
182 (compressed_size == 0 && !compressed_pages));
183
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400185 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400186
Chris Masonc8b97812008-10-29 14:49:59 -0400187 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000188
189 if (!extent_inserted) {
190 struct btrfs_key key;
191 size_t datasize;
192
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200193 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000194 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200195 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000196
197 datasize = btrfs_file_extent_calc_inline_size(cur_size);
198 path->leave_spinning = 1;
199 ret = btrfs_insert_empty_item(trans, root, path, &key,
200 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200201 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000202 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400203 }
204 leaf = path->nodes[0];
205 ei = btrfs_item_ptr(leaf, path->slots[0],
206 struct btrfs_file_extent_item);
207 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
208 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
209 btrfs_set_file_extent_encryption(leaf, ei, 0);
210 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
211 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
212 ptr = btrfs_file_extent_inline_start(ei);
213
Li Zefan261507a02010-12-17 14:21:50 +0800214 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400215 struct page *cpage;
216 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500217 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400218 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500219 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300220 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400221
Cong Wang7ac687d2011-11-25 23:14:28 +0800222 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400223 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800224 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400225
226 i++;
227 ptr += cur_size;
228 compressed_size -= cur_size;
229 }
230 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800231 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400232 } else {
233 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300234 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100237 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800239 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300240 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 }
242 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000243 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400244
Yan, Zhengc2167752009-11-12 09:34:21 +0000245 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500246 * We align size to sectorsize for inline extents just for simplicity
247 * sake.
248 */
249 size = ALIGN(size, root->fs_info->sectorsize);
250 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
251 if (ret)
252 goto fail;
253
254 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000255 * we're an inline extent, so nobody can
256 * extend the file past i_size without locking
257 * a page we already have locked.
258 *
259 * We must do any isize and inode updates
260 * before we unlock the pages. Otherwise we
261 * could end up racing with unlink.
262 */
Chris Masonc8b97812008-10-29 14:49:59 -0400263 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100264 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000265
Chris Masonc8b97812008-10-29 14:49:59 -0400266fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200267 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400268}
269
270
271/*
272 * conditionally insert an inline extent into the file. This
273 * does the checks required to make sure the data is small enough
274 * to fit as an inline extent.
275 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200276static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400277 u64 end, size_t compressed_size,
278 int compress_type,
279 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400280{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200281 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400282 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400283 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400284 u64 isize = i_size_read(inode);
285 u64 actual_end = min(end + 1, isize);
286 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400288 u64 data_len = inline_len;
289 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000290 struct btrfs_path *path;
291 int extent_inserted = 0;
292 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400293
294 if (compressed_size)
295 data_len = compressed_size;
296
297 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400298 actual_end > fs_info->sectorsize ||
299 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400300 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400302 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400303 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400304 return 1;
305 }
306
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000307 path = btrfs_alloc_path();
308 if (!path)
309 return -ENOMEM;
310
Josef Bacik00361582013-08-14 14:02:47 -0400311 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000312 if (IS_ERR(trans)) {
313 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400314 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400316 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400317
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 if (compressed_size && compressed_pages)
319 extent_item_size = btrfs_file_extent_calc_inline_size(
320 compressed_size);
321 else
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 inline_len);
324
325 ret = __btrfs_drop_extents(trans, root, inode, path,
326 start, aligned_end, NULL,
327 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400328 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400329 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400330 goto out;
331 }
Chris Masonc8b97812008-10-29 14:49:59 -0400332
333 if (isize > actual_end)
334 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000335 ret = insert_inline_extent(trans, path, extent_inserted,
336 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400337 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000338 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400339 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400340 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400341 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400343 ret = 1;
344 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100345 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400346
Josef Bacikbdc20e62013-02-28 13:23:38 -0500347 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200348 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400349out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800350 /*
351 * Don't forget to free the reserved space, as for inlined extent
352 * it won't count as data extent, free them directly here.
353 * And at reserve time, it's always aligned to page size, so
354 * just free one page here.
355 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800356 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000357 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400358 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400359 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400360}
361
Chris Mason771ed682008-11-06 22:02:51 -0500362struct async_extent {
363 u64 start;
364 u64 ram_size;
365 u64 compressed_size;
366 struct page **pages;
367 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800368 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500369 struct list_head list;
370};
371
Nikolay Borisov97db1202019-03-12 17:20:24 +0200372struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500373 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500374 struct page *locked_page;
375 u64 start;
376 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600377 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500378 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700379 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500380 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200381 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500382};
383
Nikolay Borisov97db1202019-03-12 17:20:24 +0200384struct async_cow {
385 /* Number of chunks in flight; must be first in the structure */
386 atomic_t num_chunks;
387 struct async_chunk chunks[];
388};
389
390static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500391 u64 start, u64 ram_size,
392 u64 compressed_size,
393 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800394 unsigned long nr_pages,
395 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500396{
397 struct async_extent *async_extent;
398
399 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100400 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500401 async_extent->start = start;
402 async_extent->ram_size = ram_size;
403 async_extent->compressed_size = compressed_size;
404 async_extent->pages = pages;
405 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800406 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500407 list_add_tail(&async_extent->list, &cow->extents);
408 return 0;
409}
410
Qu Wenruo42c16da2019-07-01 05:12:46 +0000411/*
412 * Check if the inode has flags compatible with compression
413 */
414static inline bool inode_can_compress(struct inode *inode)
415{
416 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
417 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
418 return false;
419 return true;
420}
421
422/*
423 * Check if the inode needs to be submitted to compression, based on mount
424 * options, defragmentation, properties or heuristics.
425 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300426static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800427{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400428 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800429
Qu Wenruo42c16da2019-07-01 05:12:46 +0000430 if (!inode_can_compress(inode)) {
431 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
432 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
433 btrfs_ino(BTRFS_I(inode)));
434 return 0;
435 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800436 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400437 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800438 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200439 /* defrag ioctl */
440 if (BTRFS_I(inode)->defrag_compress)
441 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800442 /* bad compression ratios */
443 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
444 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400445 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800446 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200447 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300448 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800449 return 0;
450}
451
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200452static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800453 u64 start, u64 end, u64 num_bytes, u64 small_write)
454{
455 /* If this is a small write inside eof, kick off a defrag */
456 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200457 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800458 btrfs_add_inode_defrag(NULL, inode);
459}
460
Chris Masonc8b97812008-10-29 14:49:59 -0400461/*
Chris Mason771ed682008-11-06 22:02:51 -0500462 * we create compressed extents in two phases. The first
463 * phase compresses a range of pages that have already been
464 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400465 *
Chris Mason771ed682008-11-06 22:02:51 -0500466 * This is done inside an ordered work queue, and the compression
467 * is spread across many cpus. The actual IO submission is step
468 * two, and the ordered work queue takes care of making sure that
469 * happens in the same order things were put onto the queue by
470 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400471 *
Chris Mason771ed682008-11-06 22:02:51 -0500472 * If this code finds it can't get good compression, it puts an
473 * entry onto the work queue to write the uncompressed bytes. This
474 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300475 * are written in the same order that the flusher thread sent them
476 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400477 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300478static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400479{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200480 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400481 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400482 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200483 u64 start = async_chunk->start;
484 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400485 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400486 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400487 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400488 struct page **pages = NULL;
489 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400490 unsigned long total_compressed = 0;
491 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400492 int i;
493 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400494 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300495 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400496 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400497
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200498 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
499 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400500
Josef Bacikd98da492019-10-11 09:03:54 -0400501 /*
502 * We need to save i_size before now because it could change in between
503 * us evaluating the size and assigning it. This is because we lock and
504 * unlock the page in truncate and fallocate, and then modify the i_size
505 * later on.
506 *
507 * The barriers are to emulate READ_ONCE, remove that once i_size_read
508 * does that for us.
509 */
510 barrier();
511 i_size = i_size_read(inode);
512 barrier();
513 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400514again:
515 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300516 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100517 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
518 nr_pages = min_t(unsigned long, nr_pages,
519 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400520
Chris Masonf03d9301f2009-02-04 09:31:06 -0500521 /*
522 * we don't want to send crud past the end of i_size through
523 * compression, that's just a waste of CPU time. So, if the
524 * end of the file is before the start of our current
525 * requested range of bytes, we bail out to the uncompressed
526 * cleanup code that can deal with all of this.
527 *
528 * It isn't really the fastest way to fix things, but this is a
529 * very uncommon corner.
530 */
531 if (actual_end <= start)
532 goto cleanup_and_bail_uncompressed;
533
Chris Masonc8b97812008-10-29 14:49:59 -0400534 total_compressed = actual_end - start;
535
Shilong Wang4bcbb332014-10-07 18:44:35 -0400536 /*
537 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400538 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400539 */
540 if (total_compressed <= blocksize &&
541 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
542 goto cleanup_and_bail_uncompressed;
543
David Sterba069eac72017-02-14 19:36:54 +0100544 total_compressed = min_t(unsigned long, total_compressed,
545 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400546 total_in = 0;
547 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400548
Chris Mason771ed682008-11-06 22:02:51 -0500549 /*
550 * we do compression for mount -o compress and when the
551 * inode has not been flagged as nocompress. This flag can
552 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400553 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300554 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400555 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100556 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800557 if (!pages) {
558 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100559 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800560 goto cont;
561 }
Chris Mason179e29e2007-11-01 11:28:41 -0400562
David Sterbaeec63c62017-07-17 19:41:31 +0200563 if (BTRFS_I(inode)->defrag_compress)
564 compress_type = BTRFS_I(inode)->defrag_compress;
565 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200566 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800567
Chris Mason4adaa612013-03-26 13:07:00 -0400568 /*
569 * we need to call clear_page_dirty_for_io on each
570 * page in the range. Otherwise applications with the file
571 * mmap'd can wander in and change the page contents while
572 * we are compressing them.
573 *
574 * If the compression fails for any reason, we set the pages
575 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300576 *
577 * Note that the remaining part is redirtied, the start pointer
578 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400579 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300580 if (!redirty) {
581 extent_range_clear_dirty_for_io(inode, start, end);
582 redirty = 1;
583 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200584
585 /* Compression level is applied here and only here */
586 ret = btrfs_compress_pages(
587 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800588 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100589 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100590 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800591 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100592 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400593
594 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100595 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100596 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400597 char *kaddr;
598
599 /* zero the tail end of the last page, we might be
600 * sending it down to disk
601 */
602 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800603 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400604 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300605 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800606 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400607 }
608 will_compress = 1;
609 }
610 }
Li Zefan560f7d72011-09-08 10:22:01 +0800611cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400612 if (start == 0) {
613 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300614 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400615 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500616 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400617 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200618 ret = cow_file_range_inline(inode, start, end, 0,
619 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400620 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500621 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200622 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000623 total_compressed,
624 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400625 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100626 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400627 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400628 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
629 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100630 unsigned long page_error_op;
631
Filipe Mananae6eb4312014-10-10 10:45:12 +0100632 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400633
Chris Mason771ed682008-11-06 22:02:51 -0500634 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100635 * inline extent creation worked or returned error,
636 * we don't need to create any more async work items.
637 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400638 *
639 * We use DO_ACCOUNTING here because we need the
640 * delalloc_release_metadata to be done _after_ we drop
641 * our outstanding extent for clearing delalloc for this
642 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500643 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300644 extent_clear_unlock_delalloc(inode, start, end, NULL,
645 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800646 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400647 PAGE_CLEAR_DIRTY |
648 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100649 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400650 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300651
652 for (i = 0; i < nr_pages; i++) {
653 WARN_ON(pages[i]->mapping);
654 put_page(pages[i]);
655 }
656 kfree(pages);
657
658 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400659 }
660 }
661
662 if (will_compress) {
663 /*
664 * we aren't doing an inline extent round the compressed size
665 * up to a block size boundary so the allocator does sane
666 * things
667 */
Qu Wenruofda28322013-02-26 08:10:22 +0000668 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400669
670 /*
671 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300672 * win, compare the page count read with the blocks on disk,
673 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400674 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300675 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300676 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300677 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700678
679 /*
680 * The async work queues will take care of doing actual
681 * allocation on disk for these compressed pages, and
682 * will submit them to the elevator.
683 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200684 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100685 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700686 compress_type);
687
Timofey Titovets11708622017-10-03 18:06:01 +0300688 if (start + total_in < end) {
689 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700690 pages = NULL;
691 cond_resched();
692 goto again;
693 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300694 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400695 }
696 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700697 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400698 /*
699 * the compression code ran but failed to make things smaller,
700 * free any pages it allocated and our page pointer array
701 */
David Sterba4d3a8002017-02-14 19:04:07 +0100702 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400703 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300704 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400705 }
706 kfree(pages);
707 pages = NULL;
708 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100709 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400710
711 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400712 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200713 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500714 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500715 }
Chris Masonc8b97812008-10-29 14:49:59 -0400716 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500717cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700718 /*
719 * No compression, but we still need to write the pages in the file
720 * we've been given so far. redirty the locked page if it corresponds
721 * to our extent and set things up for the async work queue to run
722 * cow_file_range to do the normal delalloc dance.
723 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700724 if (async_chunk->locked_page &&
725 (page_offset(async_chunk->locked_page) >= start &&
726 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200727 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700728 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700729 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700730
731 if (redirty)
732 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200733 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700734 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300735 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500736
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300737 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500738}
Chris Mason771ed682008-11-06 22:02:51 -0500739
Filipe Manana40ae8372014-10-06 22:14:24 +0100740static void free_async_extent_pages(struct async_extent *async_extent)
741{
742 int i;
743
744 if (!async_extent->pages)
745 return;
746
747 for (i = 0; i < async_extent->nr_pages; i++) {
748 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300749 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100750 }
751 kfree(async_extent->pages);
752 async_extent->nr_pages = 0;
753 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500754}
755
756/*
757 * phase two of compressed writeback. This is the ordered portion
758 * of the code, which only gets called in the order the work was
759 * queued. We walk all the async extents created by compress_file_range
760 * and send them down to the disk.
761 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200762static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500763{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200764 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400765 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500766 struct async_extent *async_extent;
767 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500768 struct btrfs_key ins;
769 struct extent_map *em;
770 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200771 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500772 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500773
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200775 while (!list_empty(&async_chunk->extents)) {
776 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500777 struct async_extent, list);
778 list_del(&async_extent->list);
779
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500780retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200781 lock_extent(io_tree, async_extent->start,
782 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500783 /* did the compression code fall back to uncompressed IO? */
784 if (!async_extent->pages) {
785 int page_started = 0;
786 unsigned long nr_written = 0;
787
Chris Mason771ed682008-11-06 22:02:51 -0500788 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200789 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500790 async_extent->start,
791 async_extent->start +
792 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300793 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500794
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100795 /* JDM XXX */
796
Chris Mason771ed682008-11-06 22:02:51 -0500797 /*
798 * if page_started, cow_file_range inserted an
799 * inline extent and took care of all the unlocking
800 * and IO for us. Otherwise, we need to submit
801 * all those pages down to the drive.
802 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500803 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200804 extent_write_locked_range(inode,
805 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500806 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500807 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500808 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700809 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200810 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500811 kfree(async_extent);
812 cond_resched();
813 continue;
814 }
815
Wang Xiaoguang18513092016-07-25 15:51:40 +0800816 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500817 async_extent->compressed_size,
818 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800819 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500820 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100821 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500822
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400823 if (ret == -ENOSPC) {
824 unlock_extent(io_tree, async_extent->start,
825 async_extent->start +
826 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800827
828 /*
829 * we need to redirty the pages if we decide to
830 * fallback to uncompressed IO, otherwise we
831 * will not submit these pages down to lower
832 * layers.
833 */
834 extent_range_redirty_for_io(inode,
835 async_extent->start,
836 async_extent->start +
837 async_extent->ram_size - 1);
838
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100839 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400840 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500841 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500842 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000843 /*
844 * here we're doing allocation and writeback of the
845 * compressed pages
846 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800847 em = create_io_em(inode, async_extent->start,
848 async_extent->ram_size, /* len */
849 async_extent->start, /* orig_start */
850 ins.objectid, /* block_start */
851 ins.offset, /* block_len */
852 ins.offset, /* orig_block_len */
853 async_extent->ram_size, /* ram_bytes */
854 async_extent->compress_type,
855 BTRFS_ORDERED_COMPRESSED);
856 if (IS_ERR(em))
857 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500858 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800859 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500860
Li Zefan261507a02010-12-17 14:21:50 +0800861 ret = btrfs_add_ordered_extent_compress(inode,
862 async_extent->start,
863 ins.objectid,
864 async_extent->ram_size,
865 ins.offset,
866 BTRFS_ORDERED_COMPRESSED,
867 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100868 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200869 btrfs_drop_extent_cache(BTRFS_I(inode),
870 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100871 async_extent->start +
872 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500873 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100874 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400875 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500876
Chris Mason771ed682008-11-06 22:02:51 -0500877 /*
878 * clear dirty, set writeback and unlock the pages.
879 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400880 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400881 async_extent->start +
882 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400883 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
884 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400885 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200886 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500887 async_extent->start,
888 async_extent->ram_size,
889 ins.objectid,
890 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600891 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700892 async_chunk->write_flags,
893 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100894 struct page *p = async_extent->pages[0];
895 const u64 start = async_extent->start;
896 const u64 end = start + async_extent->ram_size - 1;
897
898 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200899 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200900
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100901 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300902 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800903 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100904 PAGE_END_WRITEBACK |
905 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100906 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100907 }
Chris Mason771ed682008-11-06 22:02:51 -0500908 alloc_hint = ins.objectid + ins.offset;
909 kfree(async_extent);
910 cond_resched();
911 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100912 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500913out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400914 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400915 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100916out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400917 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500918 async_extent->start +
919 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400920 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100921 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400922 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
923 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100924 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
925 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100926 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100927 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500928 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500929}
930
Josef Bacik4b46fce2010-05-23 11:00:55 -0400931static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
932 u64 num_bytes)
933{
934 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
935 struct extent_map *em;
936 u64 alloc_hint = 0;
937
938 read_lock(&em_tree->lock);
939 em = search_extent_mapping(em_tree, start, num_bytes);
940 if (em) {
941 /*
942 * if block start isn't an actual block number then find the
943 * first block in this inode and use that as a hint. If that
944 * block is also bogus then just don't worry about it.
945 */
946 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
947 free_extent_map(em);
948 em = search_extent_mapping(em_tree, 0, 0);
949 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
950 alloc_hint = em->block_start;
951 if (em)
952 free_extent_map(em);
953 } else {
954 alloc_hint = em->block_start;
955 free_extent_map(em);
956 }
957 }
958 read_unlock(&em_tree->lock);
959
960 return alloc_hint;
961}
962
Chris Mason771ed682008-11-06 22:02:51 -0500963/*
964 * when extent_io.c finds a delayed allocation range in the file,
965 * the call backs end up in this code. The basic idea is to
966 * allocate extents on disk for the range, and create ordered data structs
967 * in ram to track those extents.
968 *
969 * locked_page is the page that writepage had locked already. We use
970 * it to make sure we don't do extra locks or unlocks.
971 *
972 * *page_started is set to one if we unlock locked_page and do everything
973 * required to start IO on it. It may be clean and already done with
974 * IO when we return.
975 */
Josef Bacik00361582013-08-14 14:02:47 -0400976static noinline int cow_file_range(struct inode *inode,
977 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300978 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300979 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500980{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400981 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400982 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500983 u64 alloc_hint = 0;
984 u64 num_bytes;
985 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000986 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400987 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500988 struct btrfs_key ins;
989 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000990 unsigned clear_bits;
991 unsigned long page_ops;
992 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500993 int ret = 0;
994
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200995 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400996 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500997 ret = -EINVAL;
998 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400999 }
Chris Mason771ed682008-11-06 22:02:51 -05001000
Qu Wenruofda28322013-02-26 08:10:22 +00001001 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001002 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001003 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001004
Nikolay Borisov6158e1c2017-02-20 13:50:43 +02001005 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001006
Chris Mason771ed682008-11-06 22:02:51 -05001007 if (start == 0) {
1008 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001009 ret = cow_file_range_inline(inode, start, end, 0,
1010 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001011 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001012 /*
1013 * We use DO_ACCOUNTING here because we need the
1014 * delalloc_release_metadata to be run _after_ we drop
1015 * our outstanding extent for clearing delalloc for this
1016 * range.
1017 */
Nikolay Borisov74e91942019-07-17 16:18:16 +03001018 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001019 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001020 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1021 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001022 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1023 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001024 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001025 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001026 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001027 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001028 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001029 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001030 }
1031 }
Chris Masonc8b97812008-10-29 14:49:59 -04001032
Josef Bacik4b46fce2010-05-23 11:00:55 -04001033 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001034 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1035 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001036
Anand Jain3752d222018-02-15 12:29:38 +08001037 while (num_bytes > 0) {
1038 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001039 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001040 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001041 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001042 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001043 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001044 cur_alloc_size = ins.offset;
1045 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001046
Chris Mason771ed682008-11-06 22:02:51 -05001047 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001048 em = create_io_em(inode, start, ins.offset, /* len */
1049 start, /* orig_start */
1050 ins.objectid, /* block_start */
1051 ins.offset, /* block_len */
1052 ins.offset, /* orig_block_len */
1053 ram_size, /* ram_bytes */
1054 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001055 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001056 if (IS_ERR(em)) {
1057 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001058 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001059 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001060 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001061
Chris Masone6dcd2d2008-07-17 12:53:50 -04001062 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001063 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001064 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001065 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001066
Yan Zheng17d217f2008-12-12 10:03:38 -05001067 if (root->root_key.objectid ==
1068 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1069 ret = btrfs_reloc_clone_csums(inode, start,
1070 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001071 /*
1072 * Only drop cache here, and process as normal.
1073 *
1074 * We must not allow extent_clear_unlock_delalloc()
1075 * at out_unlock label to free meta of this ordered
1076 * extent, as its meta should be freed by
1077 * btrfs_finish_ordered_io().
1078 *
1079 * So we must continue until @start is increased to
1080 * skip current ordered extent.
1081 */
Josef Bacik00361582013-08-14 14:02:47 -04001082 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001083 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1084 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001085 }
1086
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001087 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001088
Chris Masonc8b97812008-10-29 14:49:59 -04001089 /* we're not doing compressed IO, don't unlock the first
1090 * page (which the caller expects to stay locked), don't
1091 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001092 *
1093 * Do set the Private2 bit so we know this page was properly
1094 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001095 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001096 page_ops = unlock ? PAGE_UNLOCK : 0;
1097 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001098
Josef Bacikc2790a22013-07-29 11:20:47 -04001099 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001100 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001101 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001102 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001103 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001104 if (num_bytes < cur_alloc_size)
1105 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001106 else
Anand Jain3752d222018-02-15 12:29:38 +08001107 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001108 alloc_hint = ins.objectid + ins.offset;
1109 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001110 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001111
1112 /*
1113 * btrfs_reloc_clone_csums() error, since start is increased
1114 * extent_clear_unlock_delalloc() at out_unlock label won't
1115 * free metadata of current ordered extent, we're OK to exit.
1116 */
1117 if (ret)
1118 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001119 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001120out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001121 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001122
Filipe Mananad9f85962014-08-25 10:43:00 +01001123out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001124 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001125out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001126 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001127 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001128out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001129 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1130 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001131 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1132 PAGE_END_WRITEBACK;
1133 /*
1134 * If we reserved an extent for our delalloc range (or a subrange) and
1135 * failed to create the respective ordered extent, then it means that
1136 * when we reserved the extent we decremented the extent's size from
1137 * the data space_info's bytes_may_use counter and incremented the
1138 * space_info's bytes_reserved counter by the same amount. We must make
1139 * sure extent_clear_unlock_delalloc() does not try to decrement again
1140 * the data space_info's bytes_may_use counter, therefore we do not pass
1141 * it the flag EXTENT_CLEAR_DATA_RESV.
1142 */
1143 if (extent_reserved) {
1144 extent_clear_unlock_delalloc(inode, start,
1145 start + cur_alloc_size,
Filipe Mananaa315e682017-03-06 23:04:20 +00001146 locked_page,
1147 clear_bits,
1148 page_ops);
1149 start += cur_alloc_size;
1150 if (start >= end)
1151 goto out;
1152 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001153 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001154 clear_bits | EXTENT_CLEAR_DATA_RESV,
1155 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001156 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001157}
Chris Masonc8b97812008-10-29 14:49:59 -04001158
Chris Mason771ed682008-11-06 22:02:51 -05001159/*
1160 * work queue call back to started compression on a file and pages
1161 */
1162static noinline void async_cow_start(struct btrfs_work *work)
1163{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001164 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001165 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001166
Nikolay Borisovb5326272019-03-12 17:20:25 +02001167 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001168
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001169 compressed_extents = compress_file_range(async_chunk);
1170 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001171 btrfs_add_delayed_iput(async_chunk->inode);
1172 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001173 }
Chris Mason771ed682008-11-06 22:02:51 -05001174}
1175
1176/*
1177 * work queue call back to submit previously compressed pages
1178 */
1179static noinline void async_cow_submit(struct btrfs_work *work)
1180{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001181 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1182 work);
1183 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001184 unsigned long nr_pages;
1185
Nikolay Borisovb5326272019-03-12 17:20:25 +02001186 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001187 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001188
David Sterba093258e2018-02-26 16:15:17 +01001189 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001190 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001191 5 * SZ_1M)
1192 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001193
Nikolay Borisov4546d172019-01-03 10:50:03 +02001194 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001195 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001196 * in which case we don't have anything to submit, yet we need to
1197 * always adjust ->async_delalloc_pages as its paired with the init
1198 * happening in cow_file_range_async
1199 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001200 if (async_chunk->inode)
1201 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001202}
Chris Masonc8b97812008-10-29 14:49:59 -04001203
Chris Mason771ed682008-11-06 22:02:51 -05001204static noinline void async_cow_free(struct btrfs_work *work)
1205{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001206 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001207
Nikolay Borisovb5326272019-03-12 17:20:25 +02001208 async_chunk = container_of(work, struct async_chunk, work);
1209 if (async_chunk->inode)
1210 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001211 if (async_chunk->blkcg_css)
1212 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001213 /*
1214 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001215 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001216 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001217 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001218 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001219}
1220
Chris Masonec39f762019-07-10 12:28:17 -07001221static int cow_file_range_async(struct inode *inode,
1222 struct writeback_control *wbc,
1223 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001224 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001225 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001226{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001227 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonec39f762019-07-10 12:28:17 -07001228 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001229 struct async_cow *ctx;
1230 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001231 unsigned long nr_pages;
1232 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001233 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1234 int i;
1235 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001236 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001237 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001238
Nikolay Borisov69684c52019-03-12 17:20:28 +02001239 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001240
1241 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1242 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1243 num_chunks = 1;
1244 should_compress = false;
1245 } else {
1246 should_compress = true;
1247 }
1248
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001249 nofs_flag = memalloc_nofs_save();
1250 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1251 memalloc_nofs_restore(nofs_flag);
1252
Nikolay Borisov97db1202019-03-12 17:20:24 +02001253 if (!ctx) {
1254 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1255 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1256 EXTENT_DO_ACCOUNTING;
1257 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1258 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1259 PAGE_SET_ERROR;
1260
Nikolay Borisov74e91942019-07-17 16:18:16 +03001261 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001262 clear_bits, page_ops);
1263 return -ENOMEM;
1264 }
1265
1266 async_chunk = ctx->chunks;
1267 atomic_set(&ctx->num_chunks, num_chunks);
1268
1269 for (i = 0; i < num_chunks; i++) {
1270 if (should_compress)
1271 cur_end = min(end, start + SZ_512K - 1);
1272 else
1273 cur_end = end;
1274
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001275 /*
1276 * igrab is called higher up in the call chain, take only the
1277 * lightweight reference for the callback lifetime
1278 */
1279 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001280 async_chunk[i].pending = &ctx->num_chunks;
1281 async_chunk[i].inode = inode;
1282 async_chunk[i].start = start;
1283 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001284 async_chunk[i].write_flags = write_flags;
1285 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001286
Chris Mason1d53c9e2019-07-10 12:28:16 -07001287 /*
1288 * The locked_page comes all the way from writepage and its
1289 * the original page we were actually given. As we spread
1290 * this large delalloc region across multiple async_chunk
1291 * structs, only the first struct needs a pointer to locked_page
1292 *
1293 * This way we don't need racey decisions about who is supposed
1294 * to unlock it.
1295 */
1296 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001297 /*
1298 * Depending on the compressibility, the pages might or
1299 * might not go through async. We want all of them to
1300 * be accounted against wbc once. Let's do it here
1301 * before the paths diverge. wbc accounting is used
1302 * only for foreign writeback detection and doesn't
1303 * need full accuracy. Just account the whole thing
1304 * against the first page.
1305 */
1306 wbc_account_cgroup_owner(wbc, locked_page,
1307 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001308 async_chunk[i].locked_page = locked_page;
1309 locked_page = NULL;
1310 } else {
1311 async_chunk[i].locked_page = NULL;
1312 }
1313
Chris Masonec39f762019-07-10 12:28:17 -07001314 if (blkcg_css != blkcg_root_css) {
1315 css_get(blkcg_css);
1316 async_chunk[i].blkcg_css = blkcg_css;
1317 } else {
1318 async_chunk[i].blkcg_css = NULL;
1319 }
1320
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001321 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1322 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001323
Nikolay Borisov97db1202019-03-12 17:20:24 +02001324 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001325 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001326
Nikolay Borisov97db1202019-03-12 17:20:24 +02001327 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001328
Chris Mason771ed682008-11-06 22:02:51 -05001329 *nr_written += nr_pages;
1330 start = cur_end + 1;
1331 }
1332 *page_started = 1;
1333 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001334}
1335
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001336static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001337 u64 bytenr, u64 num_bytes)
1338{
1339 int ret;
1340 struct btrfs_ordered_sum *sums;
1341 LIST_HEAD(list);
1342
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001343 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001344 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001345 if (ret == 0 && list_empty(&list))
1346 return 0;
1347
1348 while (!list_empty(&list)) {
1349 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1350 list_del(&sums->list);
1351 kfree(sums);
1352 }
Liu Bo58113752018-01-31 17:09:13 -07001353 if (ret < 0)
1354 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001355 return 1;
1356}
1357
Chris Masond352ac62008-09-29 15:18:18 -04001358/*
1359 * when nowcow writeback call back. This checks for snapshots or COW copies
1360 * of the extents that exist in the file, and COWs the file as required.
1361 *
1362 * If no cow copies or snapshots exist, we write directly to the existing
1363 * blocks on disk
1364 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001365static noinline int run_delalloc_nocow(struct inode *inode,
1366 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001367 const u64 start, const u64 end,
1368 int *page_started, int force,
1369 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001370{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001371 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001372 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001373 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001374 u64 cow_start = (u64)-1;
1375 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001376 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001377 bool check_prev = true;
1378 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001379 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001380 bool nocow = false;
1381 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001382
1383 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001384 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001385 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001386 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001387 EXTENT_DO_ACCOUNTING |
1388 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001389 PAGE_CLEAR_DIRTY |
1390 PAGE_SET_WRITEBACK |
1391 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001392 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001393 }
Li Zefan82d59022011-04-20 10:33:24 +08001394
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001396 struct btrfs_key found_key;
1397 struct btrfs_file_extent_item *fi;
1398 struct extent_buffer *leaf;
1399 u64 extent_end;
1400 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001401 u64 num_bytes = 0;
1402 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001403 u64 ram_bytes;
1404 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001405
1406 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001407
Liu Boe4c3b2d2017-01-30 12:25:28 -08001408 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001410 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001411 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001412
1413 /*
1414 * If there is no extent for our range when doing the initial
1415 * search, then go back to the previous slot as it will be the
1416 * one containing the search offset
1417 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001418 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1419 leaf = path->nodes[0];
1420 btrfs_item_key_to_cpu(leaf, &found_key,
1421 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001422 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001423 found_key.type == BTRFS_EXTENT_DATA_KEY)
1424 path->slots[0]--;
1425 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001426 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001427next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001428 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 leaf = path->nodes[0];
1430 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1431 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001432 if (ret < 0) {
1433 if (cow_start != (u64)-1)
1434 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001435 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001436 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 if (ret > 0)
1438 break;
1439 leaf = path->nodes[0];
1440 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001441
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001443
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001444 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001445 if (found_key.objectid > ino)
1446 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001447 /*
1448 * Keep searching until we find an EXTENT_ITEM or there are no
1449 * more extents for this inode
1450 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001451 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1452 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1453 path->slots[0]++;
1454 goto next_slot;
1455 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001456
1457 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001458 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001459 found_key.offset > end)
1460 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001461
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001462 /*
1463 * If the found extent starts after requested offset, then
1464 * adjust extent_end to be right before this extent begins
1465 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001466 if (found_key.offset > cur_offset) {
1467 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001468 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001469 goto out_check;
1470 }
Chris Masonc31f8832008-01-08 15:46:31 -05001471
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001472 /*
1473 * Found extent which begins before our range and potentially
1474 * intersect it
1475 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001476 fi = btrfs_item_ptr(leaf, path->slots[0],
1477 struct btrfs_file_extent_item);
1478 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001479
Josef Bacikcc95bef2013-04-04 14:31:27 -04001480 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001481 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1482 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001483 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001484 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001485 extent_end = found_key.offset +
1486 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001487 disk_num_bytes =
1488 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001489 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001490 * If the extent we got ends before our current offset,
1491 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001492 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001493 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001494 path->slots[0]++;
1495 goto next_slot;
1496 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001497 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001498 if (disk_bytenr == 0)
1499 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001500 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001501 if (btrfs_file_extent_compression(leaf, fi) ||
1502 btrfs_file_extent_encryption(leaf, fi) ||
1503 btrfs_file_extent_other_encoding(leaf, fi))
1504 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001505 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001506 * If extent is created before the last volume's snapshot
1507 * this implies the extent is shared, hence we can't do
1508 * nocow. This is the same check as in
1509 * btrfs_cross_ref_exist but without calling
1510 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001511 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001512 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001513 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001514 btrfs_root_last_snapshot(&root->root_item))
1515 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001516 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1517 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001518 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001519 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001521 ret = btrfs_cross_ref_exist(root, ino,
1522 found_key.offset -
1523 extent_offset, disk_bytenr);
1524 if (ret) {
1525 /*
1526 * ret could be -EIO if the above fails to read
1527 * metadata.
1528 */
1529 if (ret < 0) {
1530 if (cow_start != (u64)-1)
1531 cur_offset = cow_start;
1532 goto error;
1533 }
1534
Nikolay Borisov3e024842019-08-21 10:42:03 +03001535 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001536 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001537 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001538 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001539 disk_bytenr += cur_offset - found_key.offset;
1540 num_bytes = min(end + 1, extent_end) - cur_offset;
1541 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001542 * If there are pending snapshots for this root, we
1543 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001544 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001545 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001546 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001547 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001548 * force cow if csum exists in the range.
1549 * this ensure that csum for a given extent are
1550 * either valid or do not exist.
1551 */
Liu Bo58113752018-01-31 17:09:13 -07001552 ret = csum_exist_in_range(fs_info, disk_bytenr,
1553 num_bytes);
1554 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001555 /*
1556 * ret could be -EIO if the above fails to read
1557 * metadata.
1558 */
1559 if (ret < 0) {
1560 if (cow_start != (u64)-1)
1561 cur_offset = cow_start;
1562 goto error;
1563 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001564 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001565 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001566 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001567 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001568 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001569 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001570 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001571 extent_end = found_key.offset + ram_bytes;
1572 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001573 /* Skip extents outside of our requested range */
1574 if (extent_end <= start) {
1575 path->slots[0]++;
1576 goto next_slot;
1577 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001578 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001579 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001580 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001581 }
1582out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001583 /*
1584 * If nocow is false then record the beginning of the range
1585 * that needs to be COWed
1586 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001587 if (!nocow) {
1588 if (cow_start == (u64)-1)
1589 cow_start = cur_offset;
1590 cur_offset = extent_end;
1591 if (cur_offset > end)
1592 break;
1593 path->slots[0]++;
1594 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001595 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001596
David Sterbab3b4aa72011-04-21 01:20:15 +02001597 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001598
1599 /*
1600 * COW range from cow_start to found_key.offset - 1. As the key
1601 * will contain the beginning of the first extent that can be
1602 * NOCOW, following one which needs to be COW'ed
1603 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001604 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001605 ret = cow_file_range(inode, locked_page,
1606 cow_start, found_key.offset - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001607 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001608 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001609 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001610 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001611 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001612 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001613 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001614 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001615 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001616
Yan Zhengd899e052008-10-30 14:25:28 -04001617 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001618 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001619 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001620
1621 em = create_io_em(inode, cur_offset, num_bytes,
1622 orig_start,
1623 disk_bytenr, /* block_start */
1624 num_bytes, /* block_len */
1625 disk_num_bytes, /* orig_block_len */
1626 ram_bytes, BTRFS_COMPRESS_NONE,
1627 BTRFS_ORDERED_PREALLOC);
1628 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001629 if (nocow)
1630 btrfs_dec_nocow_writers(fs_info,
1631 disk_bytenr);
1632 ret = PTR_ERR(em);
1633 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001634 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001635 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001636 ret = btrfs_add_ordered_extent(inode, cur_offset,
1637 disk_bytenr, num_bytes,
1638 num_bytes,
1639 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001640 if (ret) {
1641 btrfs_drop_extent_cache(BTRFS_I(inode),
1642 cur_offset,
1643 cur_offset + num_bytes - 1,
1644 0);
1645 goto error;
1646 }
Yan Zhengd899e052008-10-30 14:25:28 -04001647 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001648 ret = btrfs_add_ordered_extent(inode, cur_offset,
1649 disk_bytenr, num_bytes,
1650 num_bytes,
1651 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001652 if (ret)
1653 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001654 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001655
Filipe Mananaf78c4362016-05-09 13:15:41 +01001656 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001657 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001658 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001659
Yan, Zhengefa56462010-05-16 10:49:59 -04001660 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001661 BTRFS_DATA_RELOC_TREE_OBJECTID)
1662 /*
1663 * Error handled later, as we must prevent
1664 * extent_clear_unlock_delalloc() in error handler
1665 * from freeing metadata of created ordered extent.
1666 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001667 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1668 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001669
Josef Bacikc2790a22013-07-29 11:20:47 -04001670 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001671 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001672 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001673 EXTENT_DELALLOC |
1674 EXTENT_CLEAR_DATA_RESV,
1675 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1676
Yan Zheng80ff3852008-10-30 14:20:02 -04001677 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001678
1679 /*
1680 * btrfs_reloc_clone_csums() error, now we're OK to call error
1681 * handler, as metadata for created ordered extent will only
1682 * be freed by btrfs_finish_ordered_io().
1683 */
1684 if (ret)
1685 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001686 if (cur_offset > end)
1687 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001688 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001689 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001690
Robbie Ko506481b2018-10-30 18:04:04 +08001691 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001692 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001693
Yan Zheng80ff3852008-10-30 14:20:02 -04001694 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001695 cur_offset = end;
Nikolay Borisov74e91942019-07-17 16:18:16 +03001696 ret = cow_file_range(inode, locked_page, cow_start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001697 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001698 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001699 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001700 }
1701
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001702error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001703 if (nocow)
1704 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1705
Josef Bacik17ca04a2012-05-31 15:58:55 -04001706 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001707 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001708 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001709 EXTENT_DELALLOC | EXTENT_DEFRAG |
1710 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1711 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001712 PAGE_SET_WRITEBACK |
1713 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001714 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001715 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001716}
1717
Wang Shilong47059d92014-07-03 18:22:07 +08001718static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1719{
1720
1721 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1722 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1723 return 0;
1724
1725 /*
1726 * @defrag_bytes is a hint value, no spinlock held here,
1727 * if is not zero, it means the file is defragging.
1728 * Force cow if given extent needs to be defragged.
1729 */
1730 if (BTRFS_I(inode)->defrag_bytes &&
1731 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1732 EXTENT_DEFRAG, 0, NULL))
1733 return 1;
1734
1735 return 0;
1736}
1737
Chris Masond352ac62008-09-29 15:18:18 -04001738/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001739 * Function to process delayed allocation (create CoW) for ranges which are
1740 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001741 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001742int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001743 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1744 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001745{
Chris Masonbe20aa92007-12-17 20:14:01 -05001746 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001747 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001748
Wang Shilong47059d92014-07-03 18:22:07 +08001749 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001750 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001751 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001752 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001753 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001754 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001755 } else if (!inode_can_compress(inode) ||
1756 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001757 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001758 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001759 } else {
1760 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1761 &BTRFS_I(inode)->runtime_flags);
Chris Masonec39f762019-07-10 12:28:17 -07001762 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001763 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001764 }
Qu Wenruo524272602017-03-08 10:25:52 +08001765 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001766 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1767 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001768 return ret;
1769}
1770
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001771void btrfs_split_delalloc_extent(struct inode *inode,
1772 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001773{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001774 u64 size;
1775
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001776 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001777 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001778 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001779
Josef Bacikdcab6a32015-02-11 15:08:59 -05001780 size = orig->end - orig->start + 1;
1781 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001782 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001783 u64 new_size;
1784
1785 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001786 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001787 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001788 */
1789 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001790 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001791 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001792 num_extents += count_max_extents(new_size);
1793 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001794 return;
1795 }
1796
Josef Bacik9e0baf62011-07-15 15:16:44 +00001797 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001798 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001799 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001800}
1801
1802/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001803 * Handle merged delayed allocation extents so we can keep track of new extents
1804 * that are just merged onto old extents, such as when we are doing sequential
1805 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001806 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001807void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1808 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001809{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001810 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001811 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001812
Josef Bacik9ed74f22009-09-11 16:12:44 -04001813 /* not delalloc, ignore it */
1814 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001815 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001816
Josef Bacik8461a3d2015-03-13 15:12:08 -04001817 if (new->start > other->start)
1818 new_size = new->end - other->start + 1;
1819 else
1820 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001821
1822 /* we're not bigger than the max, unreserve the space and go */
1823 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1824 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001825 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001826 spin_unlock(&BTRFS_I(inode)->lock);
1827 return;
1828 }
1829
1830 /*
Josef Bacikba117212015-03-13 15:01:24 -04001831 * We have to add up either side to figure out how many extents were
1832 * accounted for before we merged into one big extent. If the number of
1833 * extents we accounted for is <= the amount we need for the new range
1834 * then we can return, otherwise drop. Think of it like this
1835 *
1836 * [ 4k][MAX_SIZE]
1837 *
1838 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1839 * need 2 outstanding extents, on one side we have 1 and the other side
1840 * we have 1 so they are == and we can return. But in this case
1841 *
1842 * [MAX_SIZE+4k][MAX_SIZE+4k]
1843 *
1844 * Each range on their own accounts for 2 extents, but merged together
1845 * they are only 3 extents worth of accounting, so we need to drop in
1846 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001847 */
Josef Bacikba117212015-03-13 15:01:24 -04001848 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001849 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001850 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001851 num_extents += count_max_extents(old_size);
1852 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001853 return;
1854
Josef Bacik9e0baf62011-07-15 15:16:44 +00001855 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001856 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001857 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001858}
1859
Miao Xieeb73c1b2013-05-15 07:48:22 +00001860static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1861 struct inode *inode)
1862{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001863 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1864
Miao Xieeb73c1b2013-05-15 07:48:22 +00001865 spin_lock(&root->delalloc_lock);
1866 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1867 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1868 &root->delalloc_inodes);
1869 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1870 &BTRFS_I(inode)->runtime_flags);
1871 root->nr_delalloc_inodes++;
1872 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001874 BUG_ON(!list_empty(&root->delalloc_root));
1875 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001876 &fs_info->delalloc_roots);
1877 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001878 }
1879 }
1880 spin_unlock(&root->delalloc_lock);
1881}
1882
Nikolay Borisov2b877332018-04-27 12:21:51 +03001883
1884void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1885 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001886{
David Sterba3ffbd682018-06-29 10:56:42 +02001887 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001888
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001889 if (!list_empty(&inode->delalloc_inodes)) {
1890 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001891 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001892 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001893 root->nr_delalloc_inodes--;
1894 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001895 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001896 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001897 BUG_ON(list_empty(&root->delalloc_root));
1898 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001899 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001900 }
1901 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001902}
1903
1904static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1905 struct btrfs_inode *inode)
1906{
1907 spin_lock(&root->delalloc_lock);
1908 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001909 spin_unlock(&root->delalloc_lock);
1910}
1911
Chris Masond352ac62008-09-29 15:18:18 -04001912/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001913 * Properly track delayed allocation bytes in the inode and to maintain the
1914 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001915 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001916void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1917 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001918{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001919 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1920
Wang Shilong47059d92014-07-03 18:22:07 +08001921 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1922 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001923 /*
1924 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001925 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001926 * bit, which is only set or cleared with irqs on
1927 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001928 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001929 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001930 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001931 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001932 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001933
Josef Bacik8b62f872017-10-19 14:15:55 -04001934 spin_lock(&BTRFS_I(inode)->lock);
1935 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1936 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001937
Josef Bacik6a3891c2015-03-16 17:38:52 -04001938 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001939 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001940 return;
1941
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001942 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1943 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001944 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001945 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001946 if (*bits & EXTENT_DEFRAG)
1947 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001948 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001949 &BTRFS_I(inode)->runtime_flags))
1950 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001951 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001952 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001953
1954 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1955 (*bits & EXTENT_DELALLOC_NEW)) {
1956 spin_lock(&BTRFS_I(inode)->lock);
1957 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1958 state->start;
1959 spin_unlock(&BTRFS_I(inode)->lock);
1960 }
Chris Mason291d6732008-01-29 15:55:23 -05001961}
1962
Chris Masond352ac62008-09-29 15:18:18 -04001963/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001964 * Once a range is no longer delalloc this function ensures that proper
1965 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001966 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001967void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1968 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001969{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001970 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1971 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001972 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001973 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001974
Filipe Manana4a4b9642017-07-27 19:52:55 +01001975 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1976 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001977 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001978 spin_unlock(&inode->lock);
1979 }
Wang Shilong47059d92014-07-03 18:22:07 +08001980
Chris Mason75eff682008-12-15 15:54:40 -05001981 /*
1982 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001983 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001984 * bit, which is only set or cleared with irqs on
1985 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001986 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001987 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001988 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001989
Josef Bacik8b62f872017-10-19 14:15:55 -04001990 spin_lock(&inode->lock);
1991 btrfs_mod_outstanding_extents(inode, -num_extents);
1992 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001993
Josef Bacikb6d08f02013-09-27 14:57:43 -04001994 /*
1995 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001996 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001997 * error.
1998 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001999 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002000 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002001 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002002
Josef Bacik6a3891c2015-03-16 17:38:52 -04002003 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002004 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002005 return;
2006
Filipe Mananaa315e682017-03-06 23:04:20 +00002007 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2008 do_list && !(state->state & EXTENT_NORESERVE) &&
2009 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002010 btrfs_free_reserved_data_space_noquota(
2011 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08002012 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002013
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002014 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2015 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002016 spin_lock(&inode->lock);
2017 inode->delalloc_bytes -= len;
2018 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002019 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002020 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002021 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002022 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002023 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002024
2025 if ((state->state & EXTENT_DELALLOC_NEW) &&
2026 (*bits & EXTENT_DELALLOC_NEW)) {
2027 spin_lock(&inode->lock);
2028 ASSERT(inode->new_delalloc_bytes >= len);
2029 inode->new_delalloc_bytes -= len;
2030 spin_unlock(&inode->lock);
2031 }
Chris Mason291d6732008-01-29 15:55:23 -05002032}
2033
Chris Masond352ac62008-09-29 15:18:18 -04002034/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002035 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2036 * in a chunk's stripe. This function ensures that bios do not span a
2037 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002038 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002039 * @page - The page we are about to add to the bio
2040 * @size - size we want to add to the bio
2041 * @bio - bio we want to ensure is smaller than a stripe
2042 * @bio_flags - flags of the bio
2043 *
2044 * return 1 if page cannot be added to the bio
2045 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002046 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002047 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002048int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2049 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002050{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002051 struct inode *inode = page->mapping->host;
2052 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002053 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002054 u64 length = 0;
2055 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002056 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002057 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002058
Chris Mason771ed682008-11-06 22:02:51 -05002059 if (bio_flags & EXTENT_BIO_COMPRESSED)
2060 return 0;
2061
Kent Overstreet4f024f32013-10-11 15:44:27 -07002062 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002063 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002064 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2065 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002066 if (ret < 0)
2067 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002068
2069 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002070 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002071 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002072}
2073
Chris Masond352ac62008-09-29 15:18:18 -04002074/*
2075 * in order to insert checksums into the metadata in large chunks,
2076 * we wait until bio submission time. All the pages in the bio are
2077 * checksummed and sums are attached onto the ordered extent record.
2078 *
2079 * At IO completion time the cums attached on the ordered extent record
2080 * are inserted into the btree
2081 */
David Sterbad0ee3932018-03-08 14:35:48 +01002082static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002083 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002084{
Josef Bacikc6100a42017-05-05 11:57:13 -04002085 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002086 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002087
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002088 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002089 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002090 return 0;
2091}
Chris Masone0156402008-04-16 11:15:20 -04002092
Chris Mason4a69a412008-11-06 22:03:00 -05002093/*
Chris Masoncad321a2008-12-17 14:51:42 -05002094 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002095 * on write, or reading the csums from the tree before a read.
2096 *
2097 * Rules about async/sync submit,
2098 * a) read: sync submit
2099 *
2100 * b) write without checksum: sync submit
2101 *
2102 * c) write with checksum:
2103 * c-1) if bio is issued by fsync: sync submit
2104 * (sync_writers != 0)
2105 *
2106 * c-2) if root is reloc root: sync submit
2107 * (only in case of buffered IO)
2108 *
2109 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002110 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002111static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002112 int mirror_num,
2113 unsigned long bio_flags)
2114
Chris Mason44b8bd72008-04-16 11:14:51 -04002115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002116 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002117 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302118 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002119 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002120 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002121 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002122
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002123 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002124
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002125 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302126 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002127
Mike Christie37226b22016-06-05 14:31:52 -05002128 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002129 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002130 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002131 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002132
Chris Masond20f7042008-12-08 16:58:54 -05002133 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002134 ret = btrfs_submit_compressed_read(inode, bio,
2135 mirror_num,
2136 bio_flags);
2137 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002138 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002139 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002140 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002141 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002142 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002143 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002144 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002145 /* csum items have already been cloned */
2146 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2147 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002148 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002149 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002150 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002151 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002152 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002153 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002154 if (ret)
2155 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002156 }
2157
Chris Mason0b86a832008-03-24 15:01:56 -04002158mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002159 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002160
2161out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002162 if (ret) {
2163 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002164 bio_endio(bio);
2165 }
Stefan Behrens61891922012-11-05 18:51:52 +01002166 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002167}
Chris Mason6885f302008-02-20 16:11:05 -05002168
Chris Masond352ac62008-09-29 15:18:18 -04002169/*
2170 * given a list of ordered sums record them in the inode. This happens
2171 * at IO completion time based on sums calculated at bio submission time.
2172 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002173static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002174 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002175{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002176 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002177 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002178
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002179 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002180 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002181 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002182 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002183 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002184 if (ret)
2185 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002186 }
2187 return 0;
2188}
2189
Josef Bacik2ac55d42010-02-03 19:33:23 +00002190int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002191 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002192 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002193{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002194 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002195 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002196 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002197}
2198
Chris Masond352ac62008-09-29 15:18:18 -04002199/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002200struct btrfs_writepage_fixup {
2201 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002202 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002203 struct btrfs_work work;
2204};
2205
Christoph Hellwigb2950862008-12-02 09:54:17 -05002206static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002207{
2208 struct btrfs_writepage_fixup *fixup;
2209 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002210 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002211 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002212 struct page *page;
2213 struct inode *inode;
2214 u64 page_start;
2215 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002216 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002217 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002218
2219 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2220 page = fixup->page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002221 inode = fixup->inode;
2222 page_start = page_offset(page);
2223 page_end = page_offset(page) + PAGE_SIZE - 1;
2224
2225 /*
2226 * This is similar to page_mkwrite, we need to reserve the space before
2227 * we take the page lock.
2228 */
2229 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2230 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002231again:
Chris Mason247e7432008-07-17 12:53:51 -04002232 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002233
Chris Mason25f3c502020-01-21 11:51:42 -05002234 /*
2235 * Before we queued this fixup, we took a reference on the page.
2236 * page->mapping may go NULL, but it shouldn't be moved to a different
2237 * address space.
2238 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002239 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2240 /*
2241 * Unfortunately this is a little tricky, either
2242 *
2243 * 1) We got here and our page had already been dealt with and
2244 * we reserved our space, thus ret == 0, so we need to just
2245 * drop our space reservation and bail. This can happen the
2246 * first time we come into the fixup worker, or could happen
2247 * while waiting for the ordered extent.
2248 * 2) Our page was already dealt with, but we happened to get an
2249 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2250 * this case we obviously don't have anything to release, but
2251 * because the page was already dealt with we don't want to
2252 * mark the page with an error, so make sure we're resetting
2253 * ret to 0. This is why we have this check _before_ the ret
2254 * check, because we do not want to have a surprise ENOSPC
2255 * when the page was already properly dealt with.
2256 */
2257 if (!ret) {
2258 btrfs_delalloc_release_extents(BTRFS_I(inode),
2259 PAGE_SIZE);
2260 btrfs_delalloc_release_space(inode, data_reserved,
2261 page_start, PAGE_SIZE,
2262 true);
2263 }
2264 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002265 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002266 }
Chris Mason25f3c502020-01-21 11:51:42 -05002267
2268 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002269 * We can't mess with the page state unless it is locked, so now that
2270 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002271 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002272 if (ret)
2273 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002274
David Sterbaff13db42015-12-03 14:30:40 +01002275 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002276 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002277
2278 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002279 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002280 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002281
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002282 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002283 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002284 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002285 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002286 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002287 unlock_page(page);
2288 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002289 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002290 goto again;
2291 }
Chris Mason247e7432008-07-17 12:53:51 -04002292
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002293 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002294 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002295 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002296 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002297
Chris Mason25f3c502020-01-21 11:51:42 -05002298 /*
2299 * Everything went as planned, we're now the owner of a dirty page with
2300 * delayed allocation bits set and space reserved for our COW
2301 * destination.
2302 *
2303 * The page was dirty when we started, nothing should have cleaned it.
2304 */
2305 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002306 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002307out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002308 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002309 if (free_delalloc_space)
Filipe Manana53687002019-10-09 17:43:59 +01002310 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2311 PAGE_SIZE, true);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002312 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002313 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002314out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002315 if (ret) {
2316 /*
2317 * We hit ENOSPC or other errors. Update the mapping and page
2318 * to reflect the errors and clean the page.
2319 */
2320 mapping_set_error(page->mapping, ret);
2321 end_extent_writepage(page, ret, page_start, page_end);
2322 clear_page_dirty_for_io(page);
2323 SetPageError(page);
2324 }
2325 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002326 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002327 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002328 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002329 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002330 /*
2331 * As a precaution, do a delayed iput in case it would be the last iput
2332 * that could need flushing space. Recursing back to fixup worker would
2333 * deadlock.
2334 */
2335 btrfs_add_delayed_iput(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002336}
2337
2338/*
2339 * There are a few paths in the higher layers of the kernel that directly
2340 * set the page dirty bit without asking the filesystem if it is a
2341 * good idea. This causes problems because we want to make sure COW
2342 * properly happens and the data=ordered rules are followed.
2343 *
Chris Masonc8b97812008-10-29 14:49:59 -04002344 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002345 * hasn't been properly setup for IO. We kick off an async process
2346 * to fix it up. The async helper will wait for ordered extents, set
2347 * the delalloc bit and make it safe to write the page.
2348 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002349int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002350{
2351 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002352 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002353 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002354
Chris Mason8b62b722009-09-02 16:53:46 -04002355 /* this page is properly in the ordered list */
2356 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002357 return 0;
2358
Chris Mason25f3c502020-01-21 11:51:42 -05002359 /*
2360 * PageChecked is set below when we create a fixup worker for this page,
2361 * don't try to create another one if we're already PageChecked()
2362 *
2363 * The extent_io writepage code will redirty the page if we send back
2364 * EAGAIN.
2365 */
Chris Mason247e7432008-07-17 12:53:51 -04002366 if (PageChecked(page))
2367 return -EAGAIN;
2368
2369 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2370 if (!fixup)
2371 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002372
Josef Bacikf4b13632020-01-21 14:34:52 -05002373 /*
2374 * We are already holding a reference to this inode from
2375 * write_cache_pages. We need to hold it because the space reservation
2376 * takes place outside of the page lock, and we can't trust
2377 * page->mapping outside of the page lock.
2378 */
2379 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002380 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002381 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002382 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002383 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002384 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002385 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002386
2387 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002388}
2389
Yan Zhengd899e052008-10-30 14:25:28 -04002390static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2391 struct inode *inode, u64 file_pos,
2392 u64 disk_bytenr, u64 disk_num_bytes,
2393 u64 num_bytes, u64 ram_bytes,
2394 u8 compression, u8 encryption,
2395 u16 other_encoding, int extent_type)
2396{
2397 struct btrfs_root *root = BTRFS_I(inode)->root;
2398 struct btrfs_file_extent_item *fi;
2399 struct btrfs_path *path;
2400 struct extent_buffer *leaf;
2401 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002402 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002403 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002404 int ret;
2405
2406 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002407 if (!path)
2408 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002409
Chris Masona1ed8352009-09-11 12:27:37 -04002410 /*
2411 * we may be replacing one extent in the tree with another.
2412 * The new extent is pinned in the extent map, and we don't want
2413 * to drop it from the cache until it is completely in the btree.
2414 *
2415 * So, tell btrfs_drop_extents to leave this extent in the cache.
2416 * the caller is expected to unpin it and allow it to be merged
2417 * with the others.
2418 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002419 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2420 file_pos + num_bytes, NULL, 0,
2421 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002422 if (ret)
2423 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002424
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002425 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002426 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002427 ins.offset = file_pos;
2428 ins.type = BTRFS_EXTENT_DATA_KEY;
2429
2430 path->leave_spinning = 1;
2431 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2432 sizeof(*fi));
2433 if (ret)
2434 goto out;
2435 }
Yan Zhengd899e052008-10-30 14:25:28 -04002436 leaf = path->nodes[0];
2437 fi = btrfs_item_ptr(leaf, path->slots[0],
2438 struct btrfs_file_extent_item);
2439 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2440 btrfs_set_file_extent_type(leaf, fi, extent_type);
2441 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2442 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2443 btrfs_set_file_extent_offset(leaf, fi, 0);
2444 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2445 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2446 btrfs_set_file_extent_compression(leaf, fi, compression);
2447 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2448 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002449
Yan Zhengd899e052008-10-30 14:25:28 -04002450 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002451 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002452
2453 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002454
2455 ins.objectid = disk_bytenr;
2456 ins.offset = disk_num_bytes;
2457 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002458
Josef Bacik9ddc9592020-01-17 09:02:22 -05002459 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), file_pos,
2460 ram_bytes);
2461 if (ret)
2462 goto out;
2463
Qu Wenruo297d7502015-09-08 17:08:37 +08002464 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002465 * Release the reserved range from inode dirty range map, as it is
2466 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002467 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002468 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2469 if (ret < 0)
2470 goto out;
2471 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002472 ret = btrfs_alloc_reserved_file_extent(trans, root,
2473 btrfs_ino(BTRFS_I(inode)),
2474 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002475out:
Yan Zhengd899e052008-10-30 14:25:28 -04002476 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002477
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002478 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002479}
2480
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002481static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002482 u64 start, u64 len)
2483{
David Sterba32da53862019-10-29 19:20:18 +01002484 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002485
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002487 ASSERT(cache);
2488
2489 spin_lock(&cache->lock);
2490 cache->delalloc_bytes -= len;
2491 spin_unlock(&cache->lock);
2492
2493 btrfs_put_block_group(cache);
2494}
2495
Chris Masond352ac62008-09-29 15:18:18 -04002496/* as ordered data IO finishes, this gets called so we can finish
2497 * an ordered extent if the range of bytes in the file it covers are
2498 * fully written.
2499 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002500static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002501{
Josef Bacik5fd02042012-05-02 14:00:54 -04002502 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002503 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002504 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002505 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002506 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002507 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002508 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002509 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002510 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002511 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002512 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002513 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002514 bool range_locked = false;
2515 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002516 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002517 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002518
Omar Sandovalbffe6332019-12-02 17:34:19 -08002519 start = ordered_extent->file_offset;
2520 end = start + ordered_extent->num_bytes - 1;
2521
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002522 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2523 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2524 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2525 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002526
Nikolay Borisov8d510122019-10-08 20:43:06 +03002527 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002528
Josef Bacik5fd02042012-05-02 14:00:54 -04002529 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2530 ret = -EIO;
2531 goto out;
2532 }
2533
Omar Sandovalbffe6332019-12-02 17:34:19 -08002534 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002535
Josef Bacik77cef2e2013-08-29 13:57:21 -04002536 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2537 truncated = true;
2538 logical_len = ordered_extent->truncated_len;
2539 /* Truncated the entire extent, don't bother adding */
2540 if (!logical_len)
2541 goto out;
2542 }
2543
Yan, Zhengc2167752009-11-12 09:34:21 +00002544 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002545 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002546
2547 /*
2548 * For mwrite(mmap + memset to write) case, we still reserve
2549 * space for NOCOW range.
2550 * As NOCOW won't cause a new delayed ref, just free the space
2551 */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002552 btrfs_qgroup_free_data(inode, NULL, start,
2553 ordered_extent->num_bytes);
Josef Bacikd923afe2020-01-17 09:02:23 -05002554 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002555 if (freespace_inode)
2556 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002557 else
2558 trans = btrfs_join_transaction(root);
2559 if (IS_ERR(trans)) {
2560 ret = PTR_ERR(trans);
2561 trans = NULL;
2562 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002563 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002564 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002565 ret = btrfs_update_inode_fallback(trans, root, inode);
2566 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002567 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002568 goto out;
2569 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002570
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002571 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002572 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002573
Nikolay Borisov8d510122019-10-08 20:43:06 +03002574 if (freespace_inode)
2575 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002576 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002577 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002578 if (IS_ERR(trans)) {
2579 ret = PTR_ERR(trans);
2580 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002581 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002582 }
Chris Masona79b7d42014-05-22 16:18:52 -07002583
Josef Bacik69fe2d72017-10-19 14:15:57 -04002584 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002585
Chris Masonc8b97812008-10-29 14:49:59 -04002586 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002587 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002588 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002589 BUG_ON(compress_type);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002590 btrfs_qgroup_free_data(inode, NULL, start,
2591 ordered_extent->num_bytes);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002592 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002593 ordered_extent->file_offset,
2594 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002595 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002596 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002597 BUG_ON(root == fs_info->tree_root);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002598 ret = insert_reserved_file_extent(trans, inode, start,
2599 ordered_extent->disk_bytenr,
2600 ordered_extent->disk_num_bytes,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002601 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002602 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002603 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04002604 if (!ret) {
2605 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002606 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002607 ordered_extent->disk_bytenr,
2608 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002609 }
Yan Zhengd899e052008-10-30 14:25:28 -04002610 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002611 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002612 ordered_extent->file_offset,
2613 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002614 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002615 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002616 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002617 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002618
Nikolay Borisovac01f262018-01-08 10:59:43 +02002619 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2620 if (ret) {
2621 btrfs_abort_transaction(trans, ret);
2622 goto out;
2623 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002624
Josef Bacikd923afe2020-01-17 09:02:23 -05002625 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002626 ret = btrfs_update_inode_fallback(trans, root, inode);
2627 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002628 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002629 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002630 }
2631 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002632out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002633 clear_bits = EXTENT_DEFRAG;
2634 if (range_locked)
2635 clear_bits |= EXTENT_LOCKED;
2636 if (clear_new_delalloc_bytes)
2637 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002638 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2639 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002640 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002641
Miao Xiea698d0752012-09-20 01:51:59 -06002642 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002643 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002644
Josef Bacik77cef2e2013-08-29 13:57:21 -04002645 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002646 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002647
2648 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002649 unwritten_start += logical_len;
2650 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002651
2652 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002653 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002654
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002655 /*
2656 * If the ordered extent had an IOERR or something else went
2657 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002658 * back to the allocator. We only free the extent in the
2659 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002660 *
2661 * If we made it past insert_reserved_file_extent before we
2662 * errored out then we don't need to do this as the accounting
2663 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002664 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002665 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002666 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002667 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002668 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2669 /*
2670 * Discard the range before returning it back to the
2671 * free space pool
2672 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002673 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002674 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002675 ordered_extent->disk_bytenr,
2676 ordered_extent->disk_num_bytes,
2677 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002678 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002679 ordered_extent->disk_bytenr,
2680 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002681 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002682 }
2683
Josef Bacik5fd02042012-05-02 14:00:54 -04002684 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002685 * This needs to be done to make sure anybody waiting knows we are done
2686 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002687 */
2688 btrfs_remove_ordered_extent(inode, ordered_extent);
2689
Chris Masone6dcd2d2008-07-17 12:53:50 -04002690 /* once for us */
2691 btrfs_put_ordered_extent(ordered_extent);
2692 /* once for the tree */
2693 btrfs_put_ordered_extent(ordered_extent);
2694
Josef Bacik5fd02042012-05-02 14:00:54 -04002695 return ret;
2696}
2697
2698static void finish_ordered_fn(struct btrfs_work *work)
2699{
2700 struct btrfs_ordered_extent *ordered_extent;
2701 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2702 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002703}
2704
Nikolay Borisovc6297322018-11-08 10:18:08 +02002705void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2706 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002707{
Josef Bacik5fd02042012-05-02 14:00:54 -04002708 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002709 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002710 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002711 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002712
liubo1abe9b82011-03-24 11:18:59 +00002713 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2714
Chris Mason8b62b722009-09-02 16:53:46 -04002715 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002716 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2717 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002718 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002719
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002720 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002721 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002722 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002723 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002724
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002725 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002726 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002727}
2728
Omar Sandoval47df7762020-04-16 14:46:17 -07002729static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2730 int icsum, struct page *page, int pgoff, u64 start,
2731 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002732{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2734 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002735 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002736 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2737 u8 *csum_expected;
2738 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002739
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002740 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002741
2742 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002743 shash->tfm = fs_info->csum_shash;
2744
2745 crypto_shash_init(shash);
2746 crypto_shash_update(shash, kaddr + pgoff, len);
2747 crypto_shash_final(shash, csum);
2748
2749 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002750 goto zeroit;
2751
2752 kunmap_atomic(kaddr);
2753 return 0;
2754zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002755 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2756 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002757 memset(kaddr + pgoff, 1, len);
2758 flush_dcache_page(page);
2759 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002760 return -EIO;
2761}
2762
Chris Masond352ac62008-09-29 15:18:18 -04002763/*
Chris Masond352ac62008-09-29 15:18:18 -04002764 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002765 * if there's a match, we allow the bio to finish. If not, the code in
2766 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002767 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002768static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2769 u64 phy_offset, struct page *page,
2770 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002771{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002772 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002773 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002774 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002775 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002776
Chris Masond20f7042008-12-08 16:58:54 -05002777 if (PageChecked(page)) {
2778 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002779 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002780 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002781
2782 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002783 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002784
Yan Zheng17d217f2008-12-12 10:03:38 -05002785 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002786 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002787 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002788 return 0;
2789 }
2790
Miao Xiefacc8a222013-07-25 19:22:34 +08002791 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002792 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2793 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002794}
Chris Masonb888db22007-08-27 16:49:44 -04002795
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002796/*
2797 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2798 *
2799 * @inode: The inode we want to perform iput on
2800 *
2801 * This function uses the generic vfs_inode::i_count to track whether we should
2802 * just decrement it (in case it's > 1) or if this is the last iput then link
2803 * the inode to the delayed iput machinery. Delayed iputs are processed at
2804 * transaction commit time/superblock commit/cleaner kthread.
2805 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002806void btrfs_add_delayed_iput(struct inode *inode)
2807{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002808 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002809 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002810
2811 if (atomic_add_unless(&inode->i_count, -1, 1))
2812 return;
2813
Josef Bacik034f7842018-12-03 11:06:52 -05002814 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002815 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002816 ASSERT(list_empty(&binode->delayed_iput));
2817 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002818 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002819 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2820 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002821}
2822
Josef Bacik63611e72019-06-18 10:59:18 -04002823static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2824 struct btrfs_inode *inode)
2825{
2826 list_del_init(&inode->delayed_iput);
2827 spin_unlock(&fs_info->delayed_iput_lock);
2828 iput(&inode->vfs_inode);
2829 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2830 wake_up(&fs_info->delayed_iputs_wait);
2831 spin_lock(&fs_info->delayed_iput_lock);
2832}
2833
2834static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2835 struct btrfs_inode *inode)
2836{
2837 if (!list_empty(&inode->delayed_iput)) {
2838 spin_lock(&fs_info->delayed_iput_lock);
2839 if (!list_empty(&inode->delayed_iput))
2840 run_delayed_iput_locked(fs_info, inode);
2841 spin_unlock(&fs_info->delayed_iput_lock);
2842 }
2843}
2844
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002845void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002846{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002847
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002848 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002849 while (!list_empty(&fs_info->delayed_iputs)) {
2850 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002851
David Sterba8089fe62015-11-19 14:15:51 +01002852 inode = list_first_entry(&fs_info->delayed_iputs,
2853 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002854 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002855 }
David Sterba8089fe62015-11-19 14:15:51 +01002856 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002857}
2858
Josef Bacik034f7842018-12-03 11:06:52 -05002859/**
2860 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2861 * @fs_info - the fs_info for this fs
2862 * @return - EINTR if we were killed, 0 if nothing's pending
2863 *
2864 * This will wait on any delayed iputs that are currently running with KILLABLE
2865 * set. Once they are all done running we will return, unless we are killed in
2866 * which case we return EINTR. This helps in user operations like fallocate etc
2867 * that might get blocked on the iputs.
2868 */
2869int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2870{
2871 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2872 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2873 if (ret)
2874 return -EINTR;
2875 return 0;
2876}
2877
Yan, Zhengd68fc572010-05-16 10:49:58 -04002878/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002879 * This creates an orphan entry for the given inode in case something goes wrong
2880 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002881 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002882int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002883 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002884{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002885 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002886
Omar Sandoval27919062018-05-11 13:13:37 -07002887 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2888 if (ret && ret != -EEXIST) {
2889 btrfs_abort_transaction(trans, ret);
2890 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002891 }
2892
Yan, Zhengd68fc572010-05-16 10:49:58 -04002893 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002894}
2895
2896/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002897 * We have done the delete so we can go ahead and remove the orphan item for
2898 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002899 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002900static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002901 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002902{
Omar Sandoval27919062018-05-11 13:13:37 -07002903 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002904}
2905
2906/*
2907 * this cleans up any orphans that may be left on the list from the last use
2908 * of this root.
2909 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002910int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002911{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002912 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002913 struct btrfs_path *path;
2914 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002915 struct btrfs_key key, found_key;
2916 struct btrfs_trans_handle *trans;
2917 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002918 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002919 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002920
Yan, Zhengd68fc572010-05-16 10:49:58 -04002921 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002922 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002923
2924 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002925 if (!path) {
2926 ret = -ENOMEM;
2927 goto out;
2928 }
David Sterbae4058b52015-11-27 16:31:35 +01002929 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002930
2931 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002932 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04002933 key.offset = (u64)-1;
2934
Josef Bacik7b128762008-07-24 12:17:14 -04002935 while (1) {
2936 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002937 if (ret < 0)
2938 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002939
2940 /*
2941 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002942 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002943 * find the key and see if we have stuff that matches
2944 */
2945 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002946 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002947 if (path->slots[0] == 0)
2948 break;
2949 path->slots[0]--;
2950 }
2951
2952 /* pull out the item */
2953 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002954 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2955
2956 /* make sure the item matches what we want */
2957 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2958 break;
David Sterba962a2982014-06-04 18:41:45 +02002959 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04002960 break;
2961
2962 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002963 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002964
2965 /*
2966 * this is where we are basically btrfs_lookup, without the
2967 * crossing root thing. we store the inode number in the
2968 * offset of the orphan item.
2969 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002970
2971 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002972 btrfs_err(fs_info,
2973 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002974 ret = -EINVAL;
2975 goto out;
2976 }
2977
2978 last_objectid = found_key.offset;
2979
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002980 found_key.objectid = found_key.offset;
2981 found_key.type = BTRFS_INODE_ITEM_KEY;
2982 found_key.offset = 0;
David Sterba4c66e0d2019-10-03 19:09:35 +02002983 inode = btrfs_iget(fs_info->sb, &found_key, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09302984 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01002985 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002986 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002987
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002988 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002989 struct btrfs_root *dead_root;
2990 struct btrfs_fs_info *fs_info = root->fs_info;
2991 int is_dead_root = 0;
2992
2993 /*
2994 * this is an orphan in the tree root. Currently these
2995 * could come from 2 sources:
2996 * a) a snapshot deletion in progress
2997 * b) a free space cache inode
2998 * We need to distinguish those two, as the snapshot
2999 * orphan must not get deleted.
3000 * find_dead_roots already ran before us, so if this
3001 * is a snapshot deletion, we should find the root
3002 * in the dead_roots list
3003 */
3004 spin_lock(&fs_info->trans_lock);
3005 list_for_each_entry(dead_root, &fs_info->dead_roots,
3006 root_list) {
3007 if (dead_root->root_key.objectid ==
3008 found_key.objectid) {
3009 is_dead_root = 1;
3010 break;
3011 }
3012 }
3013 spin_unlock(&fs_info->trans_lock);
3014 if (is_dead_root) {
3015 /* prevent this orphan from being found again */
3016 key.offset = found_key.objectid - 1;
3017 continue;
3018 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003019
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003020 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003021
Josef Bacika8c9e572011-09-21 16:55:59 -04003022 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003023 * If we have an inode with links, there are a couple of
3024 * possibilities. Old kernels (before v3.12) used to create an
3025 * orphan item for truncate indicating that there were possibly
3026 * extent items past i_size that needed to be deleted. In v3.12,
3027 * truncate was changed to update i_size in sync with the extent
3028 * items, but the (useless) orphan item was still created. Since
3029 * v4.18, we don't create the orphan item for truncate at all.
3030 *
3031 * So, this item could mean that we need to do a truncate, but
3032 * only if this filesystem was last used on a pre-v3.12 kernel
3033 * and was not cleanly unmounted. The odds of that are quite
3034 * slim, and it's a pain to do the truncate now, so just delete
3035 * the orphan item.
3036 *
3037 * It's also possible that this orphan item was supposed to be
3038 * deleted but wasn't. The inode number may have been reused,
3039 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003040 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003041 if (ret == -ENOENT || inode->i_nlink) {
3042 if (!ret)
3043 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003044 trans = btrfs_start_transaction(root, 1);
3045 if (IS_ERR(trans)) {
3046 ret = PTR_ERR(trans);
3047 goto out;
3048 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003049 btrfs_debug(fs_info, "auto deleting %Lu",
3050 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003051 ret = btrfs_del_orphan_item(trans, root,
3052 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003053 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003054 if (ret)
3055 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003056 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003057 }
Josef Bacik7b128762008-07-24 12:17:14 -04003058
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003059 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003060
3061 /* this will do delete_inode and everything for us */
3062 iput(inode);
3063 }
Miao Xie3254c872011-11-10 20:45:05 -05003064 /* release the path since we're done with it */
3065 btrfs_release_path(path);
3066
Yan, Zhengd68fc572010-05-16 10:49:58 -04003067 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3068
Omar Sandovala575cee2018-05-11 13:13:38 -07003069 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003070 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003071 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003072 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003073 }
Josef Bacik7b128762008-07-24 12:17:14 -04003074
3075 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003076 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003077
3078out:
3079 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003080 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003081 btrfs_free_path(path);
3082 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003083}
3084
Chris Masond352ac62008-09-29 15:18:18 -04003085/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003086 * very simple check to peek ahead in the leaf looking for xattrs. If we
3087 * don't find any xattrs, we know there can't be any acls.
3088 *
3089 * slot is the slot the inode is in, objectid is the objectid of the inode
3090 */
3091static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003092 int slot, u64 objectid,
3093 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003094{
3095 u32 nritems = btrfs_header_nritems(leaf);
3096 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003097 static u64 xattr_access = 0;
3098 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003099 int scanned = 0;
3100
Josef Bacikf23b5a52013-06-19 10:16:26 -04003101 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003102 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3103 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3104 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3105 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003106 }
3107
Chris Mason46a53cc2009-04-27 11:47:50 -04003108 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003109 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003110 while (slot < nritems) {
3111 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3112
3113 /* we found a different objectid, there must not be acls */
3114 if (found_key.objectid != objectid)
3115 return 0;
3116
3117 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003118 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003119 if (*first_xattr_slot == -1)
3120 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003121 if (found_key.offset == xattr_access ||
3122 found_key.offset == xattr_default)
3123 return 1;
3124 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003125
3126 /*
3127 * we found a key greater than an xattr key, there can't
3128 * be any acls later on
3129 */
3130 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3131 return 0;
3132
3133 slot++;
3134 scanned++;
3135
3136 /*
3137 * it goes inode, inode backrefs, xattrs, extents,
3138 * so if there are a ton of hard links to an inode there can
3139 * be a lot of backrefs. Don't waste time searching too hard,
3140 * this is just an optimization
3141 */
3142 if (scanned >= 8)
3143 break;
3144 }
3145 /* we hit the end of the leaf before we found an xattr or
3146 * something larger than an xattr. We have to assume the inode
3147 * has acls
3148 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003149 if (*first_xattr_slot == -1)
3150 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003151 return 1;
3152}
3153
3154/*
Chris Masond352ac62008-09-29 15:18:18 -04003155 * read an inode from the btree into the in-memory inode
3156 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003157static int btrfs_read_locked_inode(struct inode *inode,
3158 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003160 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003161 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003162 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003163 struct btrfs_inode_item *inode_item;
3164 struct btrfs_root *root = BTRFS_I(inode)->root;
3165 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003166 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003167 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003168 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003169 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003170 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003171 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003172
3173 ret = btrfs_fill_inode(inode, &rdev);
3174 if (!ret)
3175 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003176
Filipe Manana4222ea72018-10-24 10:13:03 +01003177 if (!path) {
3178 path = btrfs_alloc_path();
3179 if (!path)
3180 return -ENOMEM;
3181 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003182
Chris Mason39279cc2007-06-12 06:35:45 -04003183 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003184
Chris Mason39279cc2007-06-12 06:35:45 -04003185 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003186 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003187 if (path != in_path)
3188 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003189 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003190 }
Chris Mason39279cc2007-06-12 06:35:45 -04003191
Chris Mason5f39d392007-10-15 16:14:19 -04003192 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003193
3194 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003195 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003196
Chris Mason5f39d392007-10-15 16:14:19 -04003197 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3198 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003199 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003200 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003201 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3202 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003203 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003204 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3205 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003206
David Sterbaa937b972014-12-12 17:39:12 +01003207 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3208 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003209
David Sterbaa937b972014-12-12 17:39:12 +01003210 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3211 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003212
David Sterbaa937b972014-12-12 17:39:12 +01003213 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3214 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003215
chandan r9cc97d62012-07-04 12:48:07 +05303216 BTRFS_I(inode)->i_otime.tv_sec =
3217 btrfs_timespec_sec(leaf, &inode_item->otime);
3218 BTRFS_I(inode)->i_otime.tv_nsec =
3219 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003220
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003221 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003222 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003223 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3224
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003225 inode_set_iversion_queried(inode,
3226 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003227 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003228 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003229 rdev = btrfs_inode_rdev(leaf, inode_item);
3230
Josef Bacikaec74772008-07-24 12:12:38 -04003231 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003232 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003233
3234cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003235 /*
3236 * If we were modified in the current generation and evicted from memory
3237 * and then re-read we need to do a full sync since we don't have any
3238 * idea about which extents were modified before we were evicted from
3239 * cache.
3240 *
3241 * This is required for both inode re-read from disk and delayed inode
3242 * in delayed_nodes_tree.
3243 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003244 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003245 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3246 &BTRFS_I(inode)->runtime_flags);
3247
Filipe Mananabde6c242015-07-24 00:00:19 +01003248 /*
3249 * We don't persist the id of the transaction where an unlink operation
3250 * against the inode was last made. So here we assume the inode might
3251 * have been evicted, and therefore the exact value of last_unlink_trans
3252 * lost, and set it to last_trans to avoid metadata inconsistencies
3253 * between the inode and its parent if the inode is fsync'ed and the log
3254 * replayed. For example, in the scenario:
3255 *
3256 * touch mydir/foo
3257 * ln mydir/foo mydir/bar
3258 * sync
3259 * unlink mydir/bar
3260 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3261 * xfs_io -c fsync mydir/foo
3262 * <power failure>
3263 * mount fs, triggers fsync log replay
3264 *
3265 * We must make sure that when we fsync our inode foo we also log its
3266 * parent inode, otherwise after log replay the parent still has the
3267 * dentry with the "bar" name but our inode foo has a link count of 1
3268 * and doesn't have an inode ref with the name "bar" anymore.
3269 *
3270 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003271 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003272 * transaction commits on fsync if our inode is a directory, or if our
3273 * inode is not a directory, logging its parent unnecessarily.
3274 */
3275 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3276
Miao Xie67de1172013-12-26 13:07:06 +08003277 path->slots[0]++;
3278 if (inode->i_nlink != 1 ||
3279 path->slots[0] >= btrfs_header_nritems(leaf))
3280 goto cache_acl;
3281
3282 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003283 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003284 goto cache_acl;
3285
3286 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3287 if (location.type == BTRFS_INODE_REF_KEY) {
3288 struct btrfs_inode_ref *ref;
3289
3290 ref = (struct btrfs_inode_ref *)ptr;
3291 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3292 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3293 struct btrfs_inode_extref *extref;
3294
3295 extref = (struct btrfs_inode_extref *)ptr;
3296 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3297 extref);
3298 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003299cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003300 /*
3301 * try to precache a NULL acl entry for files that don't have
3302 * any xattrs or acls
3303 */
Li Zefan33345d012011-04-20 10:31:50 +08003304 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003305 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003306 if (first_xattr_slot != -1) {
3307 path->slots[0] = first_xattr_slot;
3308 ret = btrfs_load_inode_props(inode, path);
3309 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003310 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003311 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003312 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003313 root->root_key.objectid, ret);
3314 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003315 if (path != in_path)
3316 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003317
Al Viro72c04902009-06-24 16:58:48 -04003318 if (!maybe_acls)
3319 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003320
Chris Mason39279cc2007-06-12 06:35:45 -04003321 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003322 case S_IFREG:
3323 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003324 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003325 inode->i_fop = &btrfs_file_operations;
3326 inode->i_op = &btrfs_file_inode_operations;
3327 break;
3328 case S_IFDIR:
3329 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003330 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003331 break;
3332 case S_IFLNK:
3333 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003334 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003335 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003336 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003337 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003338 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003339 init_special_inode(inode, inode->i_mode, rdev);
3340 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003341 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003342
David Sterba7b6a2212018-03-26 18:40:21 +02003343 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003344 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003345}
3346
Chris Masond352ac62008-09-29 15:18:18 -04003347/*
3348 * given a leaf and an inode, copy the inode fields into the leaf
3349 */
Chris Masone02119d2008-09-05 16:13:11 -04003350static void fill_inode_item(struct btrfs_trans_handle *trans,
3351 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003352 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003353 struct inode *inode)
3354{
Liu Bo51fab692012-12-27 09:01:21 +00003355 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003356
David Sterbac82f8232019-08-09 17:48:21 +02003357 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003358
Liu Bo51fab692012-12-27 09:01:21 +00003359 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3360 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3361 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3362 &token);
3363 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3364 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003365
David Sterbaa937b972014-12-12 17:39:12 +01003366 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003367 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003368 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003369 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003370
David Sterbaa937b972014-12-12 17:39:12 +01003371 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003372 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003373 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003374 inode->i_mtime.tv_nsec, &token);
3375
David Sterbaa937b972014-12-12 17:39:12 +01003376 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003377 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003378 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003379 inode->i_ctime.tv_nsec, &token);
3380
chandan r9cc97d62012-07-04 12:48:07 +05303381 btrfs_set_token_timespec_sec(leaf, &item->otime,
3382 BTRFS_I(inode)->i_otime.tv_sec, &token);
3383 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3384 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3385
Liu Bo51fab692012-12-27 09:01:21 +00003386 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3387 &token);
3388 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3389 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003390 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3391 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003392 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3393 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3394 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3395 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003396}
3397
Chris Masond352ac62008-09-29 15:18:18 -04003398/*
3399 * copy everything in the in-memory inode into the btree.
3400 */
Chris Mason21151332011-11-10 20:39:08 -05003401static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003402 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003403{
3404 struct btrfs_inode_item *inode_item;
3405 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003406 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003407 int ret;
3408
3409 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003410 if (!path)
3411 return -ENOMEM;
3412
Chris Masonb9473432009-03-13 11:00:37 -04003413 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003414 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3415 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003416 if (ret) {
3417 if (ret > 0)
3418 ret = -ENOENT;
3419 goto failed;
3420 }
3421
Chris Mason5f39d392007-10-15 16:14:19 -04003422 leaf = path->nodes[0];
3423 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003424 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003425
Chris Masone02119d2008-09-05 16:13:11 -04003426 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003427 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003428 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003429 ret = 0;
3430failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003431 btrfs_free_path(path);
3432 return ret;
3433}
3434
Chris Masond352ac62008-09-29 15:18:18 -04003435/*
Chris Mason21151332011-11-10 20:39:08 -05003436 * copy everything in the in-memory inode into the btree.
3437 */
3438noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3439 struct btrfs_root *root, struct inode *inode)
3440{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003441 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003442 int ret;
3443
3444 /*
3445 * If the inode is a free space inode, we can deadlock during commit
3446 * if we put it into the delayed code.
3447 *
3448 * The data relocation inode should also be directly updated
3449 * without delay
3450 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003451 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003452 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003453 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003454 btrfs_update_root_times(trans, root);
3455
Chris Mason21151332011-11-10 20:39:08 -05003456 ret = btrfs_delayed_update_inode(trans, root, inode);
3457 if (!ret)
3458 btrfs_set_inode_last_trans(trans, inode);
3459 return ret;
3460 }
3461
3462 return btrfs_update_inode_item(trans, root, inode);
3463}
3464
Josef Bacikbe6aef62012-10-22 15:43:12 -04003465noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3466 struct btrfs_root *root,
3467 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003468{
3469 int ret;
3470
3471 ret = btrfs_update_inode(trans, root, inode);
3472 if (ret == -ENOSPC)
3473 return btrfs_update_inode_item(trans, root, inode);
3474 return ret;
3475}
3476
3477/*
Chris Masond352ac62008-09-29 15:18:18 -04003478 * unlink helper that gets used here in inode.c and in the tree logging
3479 * recovery code. It remove a link in a directory with a given name, and
3480 * also drops the back refs in the inode to the directory
3481 */
Al Viro92986792011-03-04 17:14:37 +00003482static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3483 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003484 struct btrfs_inode *dir,
3485 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003486 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003487{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003488 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003489 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003490 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003491 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003492 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003493 u64 ino = btrfs_ino(inode);
3494 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003495
3496 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003497 if (!path) {
3498 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003499 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003500 }
3501
Chris Masonb9473432009-03-13 11:00:37 -04003502 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003503 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003504 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003505 if (IS_ERR_OR_NULL(di)) {
3506 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003507 goto err;
3508 }
Chris Mason39279cc2007-06-12 06:35:45 -04003509 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003510 if (ret)
3511 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003512 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003513
Miao Xie67de1172013-12-26 13:07:06 +08003514 /*
3515 * If we don't have dir index, we have to get it by looking up
3516 * the inode ref, since we get the inode ref, remove it directly,
3517 * it is unnecessary to do delayed deletion.
3518 *
3519 * But if we have dir index, needn't search inode ref to get it.
3520 * Since the inode ref is close to the inode item, it is better
3521 * that we delay to delete it, and just do this deletion when
3522 * we update the inode item.
3523 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003524 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003525 ret = btrfs_delayed_delete_inode_ref(inode);
3526 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003527 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003528 goto skip_backref;
3529 }
3530 }
3531
Li Zefan33345d012011-04-20 10:31:50 +08003532 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3533 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003534 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003535 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003536 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003537 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003538 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003539 goto err;
3540 }
Miao Xie67de1172013-12-26 13:07:06 +08003541skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003542 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003543 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003544 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003545 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003546 }
Chris Mason39279cc2007-06-12 06:35:45 -04003547
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003548 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3549 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003550 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003551 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003552 goto err;
3553 }
Chris Masone02119d2008-09-05 16:13:11 -04003554
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003555 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3556 index);
Chris Mason6418c962010-10-30 07:34:24 -04003557 if (ret == -ENOENT)
3558 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003559 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003560 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003561
3562 /*
3563 * If we have a pending delayed iput we could end up with the final iput
3564 * being run in btrfs-cleaner context. If we have enough of these built
3565 * up we can end up burning a lot of time in btrfs-cleaner without any
3566 * way to throttle the unlinks. Since we're currently holding a ref on
3567 * the inode we can run the delayed iput here without any issues as the
3568 * final iput won't be done until after we drop the ref we're currently
3569 * holding.
3570 */
3571 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003572err:
3573 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003574 if (ret)
3575 goto out;
3576
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003577 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003578 inode_inc_iversion(&inode->vfs_inode);
3579 inode_inc_iversion(&dir->vfs_inode);
3580 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3581 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3582 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003583out:
Chris Mason39279cc2007-06-12 06:35:45 -04003584 return ret;
3585}
3586
Al Viro92986792011-03-04 17:14:37 +00003587int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3588 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003589 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003590 const char *name, int name_len)
3591{
3592 int ret;
3593 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3594 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003595 drop_nlink(&inode->vfs_inode);
3596 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003597 }
3598 return ret;
3599}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003600
3601/*
3602 * helper to start transaction for unlink and rmdir.
3603 *
Josef Bacikd52be812013-05-29 14:54:47 -04003604 * unlink and rmdir are special in btrfs, they do not always free space, so
3605 * if we cannot make our reservations the normal way try and see if there is
3606 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3607 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003608 */
Josef Bacikd52be812013-05-29 14:54:47 -04003609static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003610{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003611 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003612
Josef Bacike70bea52011-10-11 14:18:24 -04003613 /*
3614 * 1 for the possible orphan item
3615 * 1 for the dir item
3616 * 1 for the dir index
3617 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003618 * 1 for the inode
3619 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003620 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003621}
3622
Chris Mason39279cc2007-06-12 06:35:45 -04003623static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3624{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003625 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003626 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003627 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003628 int ret;
3629
Josef Bacikd52be812013-05-29 14:54:47 -04003630 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003631 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003632 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003633
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003634 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3635 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003636
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003637 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3638 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3639 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003640 if (ret)
3641 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003642
Yan, Zhenga22285a2010-05-16 10:48:46 -04003643 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003644 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003645 if (ret)
3646 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003647 }
Josef Bacik7b128762008-07-24 12:17:14 -04003648
Tsutomu Itohb5324022011-07-19 07:27:20 +00003649out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003650 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003651 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003652 return ret;
3653}
3654
Misono Tomohirof60a2362018-04-18 11:34:52 +09003655static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003656 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003657{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003658 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003659 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003660 struct btrfs_path *path;
3661 struct extent_buffer *leaf;
3662 struct btrfs_dir_item *di;
3663 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003664 const char *name = dentry->d_name.name;
3665 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003666 u64 index;
3667 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003668 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003669 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003670
Josef Bacik045d3962019-12-18 17:20:27 -05003671 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3672 objectid = inode->root->root_key.objectid;
3673 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3674 objectid = inode->location.objectid;
3675 } else {
3676 WARN_ON(1);
3677 return -EINVAL;
3678 }
3679
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003680 path = btrfs_alloc_path();
3681 if (!path)
3682 return -ENOMEM;
3683
Li Zefan33345d012011-04-20 10:31:50 +08003684 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003685 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003686 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003687 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003688 goto out;
3689 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003690
3691 leaf = path->nodes[0];
3692 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3693 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3694 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003695 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003696 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003697 goto out;
3698 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003699 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003700
Josef Bacikd49d3282019-12-18 17:20:28 -05003701 /*
3702 * This is a placeholder inode for a subvolume we didn't have a
3703 * reference to at the time of the snapshot creation. In the meantime
3704 * we could have renamed the real subvol link into our snapshot, so
3705 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3706 * Instead simply lookup the dir_index_item for this entry so we can
3707 * remove it. Otherwise we know we have a ref to the root and we can
3708 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3709 */
3710 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003711 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003712 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003713 if (IS_ERR_OR_NULL(di)) {
3714 if (!di)
3715 ret = -ENOENT;
3716 else
3717 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003718 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003719 goto out;
3720 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003721
3722 leaf = path->nodes[0];
3723 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003724 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003725 btrfs_release_path(path);
3726 } else {
3727 ret = btrfs_del_root_ref(trans, objectid,
3728 root->root_key.objectid, dir_ino,
3729 &index, name, name_len);
3730 if (ret) {
3731 btrfs_abort_transaction(trans, ret);
3732 goto out;
3733 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003734 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003735
Lu Fengqi9add2942018-08-01 11:32:26 +08003736 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003737 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003738 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003739 goto out;
3740 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003741
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003742 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003743 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003744 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003745 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003746 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003747 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003748out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003749 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003750 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003751}
3752
Misono Tomohiroec42f162018-04-18 11:34:13 +09003753/*
3754 * Helper to check if the subvolume references other subvolumes or if it's
3755 * default.
3756 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003757static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003758{
3759 struct btrfs_fs_info *fs_info = root->fs_info;
3760 struct btrfs_path *path;
3761 struct btrfs_dir_item *di;
3762 struct btrfs_key key;
3763 u64 dir_id;
3764 int ret;
3765
3766 path = btrfs_alloc_path();
3767 if (!path)
3768 return -ENOMEM;
3769
3770 /* Make sure this root isn't set as the default subvol */
3771 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3772 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3773 dir_id, "default", 7, 0);
3774 if (di && !IS_ERR(di)) {
3775 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3776 if (key.objectid == root->root_key.objectid) {
3777 ret = -EPERM;
3778 btrfs_err(fs_info,
3779 "deleting default subvolume %llu is not allowed",
3780 key.objectid);
3781 goto out;
3782 }
3783 btrfs_release_path(path);
3784 }
3785
3786 key.objectid = root->root_key.objectid;
3787 key.type = BTRFS_ROOT_REF_KEY;
3788 key.offset = (u64)-1;
3789
3790 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3791 if (ret < 0)
3792 goto out;
3793 BUG_ON(ret == 0);
3794
3795 ret = 0;
3796 if (path->slots[0] > 0) {
3797 path->slots[0]--;
3798 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3799 if (key.objectid == root->root_key.objectid &&
3800 key.type == BTRFS_ROOT_REF_KEY)
3801 ret = -ENOTEMPTY;
3802 }
3803out:
3804 btrfs_free_path(path);
3805 return ret;
3806}
3807
Nikolay Borisov20a68002018-04-27 14:36:24 +03003808/* Delete all dentries for inodes belonging to the root */
3809static void btrfs_prune_dentries(struct btrfs_root *root)
3810{
3811 struct btrfs_fs_info *fs_info = root->fs_info;
3812 struct rb_node *node;
3813 struct rb_node *prev;
3814 struct btrfs_inode *entry;
3815 struct inode *inode;
3816 u64 objectid = 0;
3817
3818 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3819 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3820
3821 spin_lock(&root->inode_lock);
3822again:
3823 node = root->inode_tree.rb_node;
3824 prev = NULL;
3825 while (node) {
3826 prev = node;
3827 entry = rb_entry(node, struct btrfs_inode, rb_node);
3828
David Sterba37508512018-06-29 10:56:40 +02003829 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003830 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003831 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003832 node = node->rb_right;
3833 else
3834 break;
3835 }
3836 if (!node) {
3837 while (prev) {
3838 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003839 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003840 node = prev;
3841 break;
3842 }
3843 prev = rb_next(prev);
3844 }
3845 }
3846 while (node) {
3847 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003848 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003849 inode = igrab(&entry->vfs_inode);
3850 if (inode) {
3851 spin_unlock(&root->inode_lock);
3852 if (atomic_read(&inode->i_count) > 1)
3853 d_prune_aliases(inode);
3854 /*
3855 * btrfs_drop_inode will have it removed from the inode
3856 * cache when its usage count hits zero.
3857 */
3858 iput(inode);
3859 cond_resched();
3860 spin_lock(&root->inode_lock);
3861 goto again;
3862 }
3863
3864 if (cond_resched_lock(&root->inode_lock))
3865 goto again;
3866
3867 node = rb_next(node);
3868 }
3869 spin_unlock(&root->inode_lock);
3870}
3871
Misono Tomohirof60a2362018-04-18 11:34:52 +09003872int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3873{
3874 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3875 struct btrfs_root *root = BTRFS_I(dir)->root;
3876 struct inode *inode = d_inode(dentry);
3877 struct btrfs_root *dest = BTRFS_I(inode)->root;
3878 struct btrfs_trans_handle *trans;
3879 struct btrfs_block_rsv block_rsv;
3880 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003881 int ret;
3882 int err;
3883
3884 /*
3885 * Don't allow to delete a subvolume with send in progress. This is
3886 * inside the inode lock so the error handling that has to drop the bit
3887 * again is not run concurrently.
3888 */
3889 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003890 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003891 spin_unlock(&dest->root_item_lock);
3892 btrfs_warn(fs_info,
3893 "attempt to delete subvolume %llu during send",
3894 dest->root_key.objectid);
3895 return -EPERM;
3896 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003897 root_flags = btrfs_root_flags(&dest->root_item);
3898 btrfs_set_root_flags(&dest->root_item,
3899 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3900 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003901
3902 down_write(&fs_info->subvol_sem);
3903
3904 err = may_destroy_subvol(dest);
3905 if (err)
3906 goto out_up_write;
3907
3908 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3909 /*
3910 * One for dir inode,
3911 * two for dir entries,
3912 * two for root ref/backref.
3913 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003914 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003915 if (err)
3916 goto out_up_write;
3917
3918 trans = btrfs_start_transaction(root, 0);
3919 if (IS_ERR(trans)) {
3920 err = PTR_ERR(trans);
3921 goto out_release;
3922 }
3923 trans->block_rsv = &block_rsv;
3924 trans->bytes_reserved = block_rsv.size;
3925
3926 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3927
Josef Bacik045d3962019-12-18 17:20:27 -05003928 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003929 if (ret) {
3930 err = ret;
3931 btrfs_abort_transaction(trans, ret);
3932 goto out_end_trans;
3933 }
3934
3935 btrfs_record_root_in_trans(trans, dest);
3936
3937 memset(&dest->root_item.drop_progress, 0,
3938 sizeof(dest->root_item.drop_progress));
3939 dest->root_item.drop_level = 0;
3940 btrfs_set_root_refs(&dest->root_item, 0);
3941
3942 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
3943 ret = btrfs_insert_orphan_item(trans,
3944 fs_info->tree_root,
3945 dest->root_key.objectid);
3946 if (ret) {
3947 btrfs_abort_transaction(trans, ret);
3948 err = ret;
3949 goto out_end_trans;
3950 }
3951 }
3952
Lu Fengqid1957792018-05-29 15:01:54 +08003953 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09003954 BTRFS_UUID_KEY_SUBVOL,
3955 dest->root_key.objectid);
3956 if (ret && ret != -ENOENT) {
3957 btrfs_abort_transaction(trans, ret);
3958 err = ret;
3959 goto out_end_trans;
3960 }
3961 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08003962 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09003963 dest->root_item.received_uuid,
3964 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3965 dest->root_key.objectid);
3966 if (ret && ret != -ENOENT) {
3967 btrfs_abort_transaction(trans, ret);
3968 err = ret;
3969 goto out_end_trans;
3970 }
3971 }
3972
3973out_end_trans:
3974 trans->block_rsv = NULL;
3975 trans->bytes_reserved = 0;
3976 ret = btrfs_end_transaction(trans);
3977 if (ret && !err)
3978 err = ret;
3979 inode->i_flags |= S_DEAD;
3980out_release:
3981 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
3982out_up_write:
3983 up_write(&fs_info->subvol_sem);
3984 if (err) {
3985 spin_lock(&dest->root_item_lock);
3986 root_flags = btrfs_root_flags(&dest->root_item);
3987 btrfs_set_root_flags(&dest->root_item,
3988 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
3989 spin_unlock(&dest->root_item_lock);
3990 } else {
3991 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03003992 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003993 ASSERT(dest->send_in_progress == 0);
3994
3995 /* the last ref */
3996 if (dest->ino_cache_inode) {
3997 iput(dest->ino_cache_inode);
3998 dest->ino_cache_inode = NULL;
3999 }
4000 }
4001
4002 return err;
4003}
4004
Chris Mason39279cc2007-06-12 06:35:45 -04004005static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4006{
David Howells2b0143b2015-03-17 22:25:59 +00004007 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004008 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004009 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004010 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004011 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004012
David Sterbab3ae2442012-09-13 16:04:34 -06004013 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004014 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004015 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004016 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004017
Josef Bacikd52be812013-05-29 14:54:47 -04004018 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004019 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004020 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004021
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004022 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004023 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004024 goto out;
4025 }
4026
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004027 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004028 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004029 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004030
Filipe Manana44f714d2016-06-06 16:11:13 +01004031 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4032
Chris Mason39279cc2007-06-12 06:35:45 -04004033 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004034 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4035 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4036 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004037 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004038 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004039 /*
4040 * Propagate the last_unlink_trans value of the deleted dir to
4041 * its parent directory. This is to prevent an unrecoverable
4042 * log tree in the case we do something like this:
4043 * 1) create dir foo
4044 * 2) create snapshot under dir foo
4045 * 3) delete the snapshot
4046 * 4) rmdir foo
4047 * 5) mkdir foo
4048 * 6) fsync foo or some file inside foo
4049 */
4050 if (last_unlink_trans >= trans->transid)
4051 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4052 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004053out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004054 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004055 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004056
Chris Mason39279cc2007-06-12 06:35:45 -04004057 return err;
4058}
4059
Josef Bacikddfae632017-10-19 14:16:02 -04004060/*
4061 * Return this if we need to call truncate_block for the last bit of the
4062 * truncate.
4063 */
4064#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004065
Chris Mason323ac952008-10-01 19:05:46 -04004066/*
Chris Mason39279cc2007-06-12 06:35:45 -04004067 * this can truncate away extent items, csum items and directory items.
4068 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004069 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004070 *
4071 * csum items that cross the new i_size are truncated to the new size
4072 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004073 *
4074 * min_type is the minimum key type to truncate down to. If set to 0, this
4075 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004076 */
Yan, Zheng80825102009-11-12 09:35:36 +00004077int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4078 struct btrfs_root *root,
4079 struct inode *inode,
4080 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004082 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004083 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004084 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004085 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004086 struct btrfs_key key;
4087 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004088 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004089 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004090 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004091 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004092 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004093 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004094 int found_extent;
4095 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004096 int pending_del_nr = 0;
4097 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004098 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004099 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004100 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004101 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004102 bool be_nice = false;
4103 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004104 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4105 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004106
4107 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004108
Chris Mason28ed1342014-12-17 09:41:04 -08004109 /*
4110 * for non-free space inodes and ref cows, we want to back off from
4111 * time to time
4112 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004113 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004114 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004115 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004116
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004117 path = btrfs_alloc_path();
4118 if (!path)
4119 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004120 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004121
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004122 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4123 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4124 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004125
Josef Bacik5dc562c2012-08-17 13:14:17 -04004126 /*
4127 * We want to drop from the next block forward in case this new size is
4128 * not block aligned since we will be keeping the last block of the
4129 * extent just the way it is.
4130 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004131 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004132 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004133 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004134 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004135 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004136
Miao Xie16cdcec2011-04-22 18:12:22 +08004137 /*
4138 * This function is also used to drop the items in the log tree before
4139 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004140 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004141 * items.
4142 */
4143 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004144 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004145
Li Zefan33345d012011-04-20 10:31:50 +08004146 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004147 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004148 key.type = (u8)-1;
4149
Chris Mason85e21ba2008-01-29 15:11:36 -05004150search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004151 /*
4152 * with a 16K leaf size and 128MB extents, you can actually queue
4153 * up a huge file in a single leaf. Most of the time that
4154 * bytes_deleted is > 0, it will be huge by the time we get here
4155 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004156 if (be_nice && bytes_deleted > SZ_32M &&
4157 btrfs_should_end_transaction(trans)) {
4158 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004159 goto out;
4160 }
Chris Masond3977122009-01-05 21:25:51 -05004161
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004162 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4163 if (ret < 0)
4164 goto out;
4165
Chris Mason85e21ba2008-01-29 15:11:36 -05004166 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004167 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004168 /* there are no items in the tree for us to truncate, we're
4169 * done
4170 */
Yan, Zheng80825102009-11-12 09:35:36 +00004171 if (path->slots[0] == 0)
4172 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004173 path->slots[0]--;
4174 }
4175
Chris Masond3977122009-01-05 21:25:51 -05004176 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004177 u64 clear_start = 0, clear_len = 0;
4178
Chris Mason39279cc2007-06-12 06:35:45 -04004179 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004180 leaf = path->nodes[0];
4181 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004182 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004183
Li Zefan33345d012011-04-20 10:31:50 +08004184 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004185 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004186
Chris Mason85e21ba2008-01-29 15:11:36 -05004187 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004188 break;
4189
Chris Mason5f39d392007-10-15 16:14:19 -04004190 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004191 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004192 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004193 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004194 extent_type = btrfs_file_extent_type(leaf, fi);
4195 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004196 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004197 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004198
4199 trace_btrfs_truncate_show_fi_regular(
4200 BTRFS_I(inode), leaf, fi,
4201 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004202 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004203 item_end += btrfs_file_extent_ram_bytes(leaf,
4204 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004205
4206 trace_btrfs_truncate_show_fi_inline(
4207 BTRFS_I(inode), leaf, fi, path->slots[0],
4208 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004209 }
Yan008630c2007-11-07 13:31:09 -05004210 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004211 }
Yan, Zheng80825102009-11-12 09:35:36 +00004212 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004213 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004214 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004215 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004216 break;
4217 if (found_key.offset >= new_size)
4218 del_item = 1;
4219 else
4220 del_item = 0;
4221 }
Chris Mason39279cc2007-06-12 06:35:45 -04004222 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004223 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004224 if (found_type != BTRFS_EXTENT_DATA_KEY)
4225 goto delete;
4226
4227 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004228 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004229
4230 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004231 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004232 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004233 u64 orig_num_bytes =
4234 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004235 extent_num_bytes = ALIGN(new_size -
4236 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004237 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004238 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004239 btrfs_set_file_extent_num_bytes(leaf, fi,
4240 extent_num_bytes);
4241 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004242 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004243 if (test_bit(BTRFS_ROOT_REF_COWS,
4244 &root->state) &&
4245 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004246 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004247 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004248 } else {
Chris Masondb945352007-10-15 16:15:53 -04004249 extent_num_bytes =
4250 btrfs_file_extent_disk_num_bytes(leaf,
4251 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004252 extent_offset = found_key.offset -
4253 btrfs_file_extent_offset(leaf, fi);
4254
Chris Mason39279cc2007-06-12 06:35:45 -04004255 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004256 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004257 if (extent_start != 0) {
4258 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004259 if (test_bit(BTRFS_ROOT_REF_COWS,
4260 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004261 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004262 }
4263 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004264 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004265 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004266 /*
4267 * we can't truncate inline items that have had
4268 * special encodings
4269 */
4270 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004271 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004272 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4273 btrfs_file_extent_compression(leaf, fi) == 0) {
4274 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004275
Josef Bacikddfae632017-10-19 14:16:02 -04004276 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4277 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004278 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004279 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004280 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004281 * We have to bail so the last_size is set to
4282 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004283 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004284 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004285 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004286 } else {
4287 /*
4288 * Inline extents are special, we just treat
4289 * them as a full sector worth in the file
4290 * extent tree just for simplicity sake.
4291 */
4292 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004293 }
Josef Bacikddfae632017-10-19 14:16:02 -04004294
4295 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4296 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004297 }
Chris Mason179e29e2007-11-01 11:28:41 -04004298delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004299 /*
4300 * We use btrfs_truncate_inode_items() to clean up log trees for
4301 * multiple fsyncs, and in this case we don't want to clear the
4302 * file extent range because it's just the log.
4303 */
4304 if (root == BTRFS_I(inode)->root) {
4305 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4306 clear_start, clear_len);
4307 if (ret) {
4308 btrfs_abort_transaction(trans, ret);
4309 break;
4310 }
4311 }
4312
Josef Bacikddfae632017-10-19 14:16:02 -04004313 if (del_item)
4314 last_size = found_key.offset;
4315 else
4316 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004317 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004318 if (!pending_del_nr) {
4319 /* no pending yet, add ourselves */
4320 pending_del_slot = path->slots[0];
4321 pending_del_nr = 1;
4322 } else if (pending_del_nr &&
4323 path->slots[0] + 1 == pending_del_slot) {
4324 /* hop on the pending chunk */
4325 pending_del_nr++;
4326 pending_del_slot = path->slots[0];
4327 } else {
Chris Masond3977122009-01-05 21:25:51 -05004328 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004329 }
Chris Mason39279cc2007-06-12 06:35:45 -04004330 } else {
4331 break;
4332 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004333 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004334
Miao Xie27cdeb72014-04-02 19:51:05 +08004335 if (found_extent &&
4336 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004337 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004338 struct btrfs_ref ref = { 0 };
4339
Chris Mason28ed1342014-12-17 09:41:04 -08004340 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004341
4342 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4343 extent_start, extent_num_bytes, 0);
4344 ref.real_root = root->root_key.objectid;
4345 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4346 ino, extent_offset);
4347 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004348 if (ret) {
4349 btrfs_abort_transaction(trans, ret);
4350 break;
4351 }
Chris Mason28f75a02015-02-04 06:59:29 -08004352 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004353 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004354 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004355 }
Chris Mason39279cc2007-06-12 06:35:45 -04004356 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004357
Yan, Zheng80825102009-11-12 09:35:36 +00004358 if (found_type == BTRFS_INODE_ITEM_KEY)
4359 break;
4360
4361 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004362 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004363 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004364 if (pending_del_nr) {
4365 ret = btrfs_del_items(trans, root, path,
4366 pending_del_slot,
4367 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004368 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004369 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004370 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004371 }
Yan, Zheng80825102009-11-12 09:35:36 +00004372 pending_del_nr = 0;
4373 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004374 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004375
Chris Mason28f75a02015-02-04 06:59:29 -08004376 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004377 * We can generate a lot of delayed refs, so we need to
4378 * throttle every once and a while and make sure we're
4379 * adding enough space to keep up with the work we are
4380 * generating. Since we hold a transaction here we
4381 * can't flush, and we don't want to FLUSH_LIMIT because
4382 * we could have generated too many delayed refs to
4383 * actually allocate, so just bail if we're short and
4384 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004385 */
Josef Bacik28bad212018-12-03 10:20:38 -05004386 if (should_throttle) {
4387 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4388 BTRFS_RESERVE_NO_FLUSH);
4389 if (ret) {
4390 ret = -EAGAIN;
4391 break;
4392 }
Chris Mason28f75a02015-02-04 06:59:29 -08004393 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004394 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004395 } else {
4396 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004397 }
Chris Mason39279cc2007-06-12 06:35:45 -04004398 }
Yan, Zheng80825102009-11-12 09:35:36 +00004399out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004400 if (ret >= 0 && pending_del_nr) {
4401 int err;
4402
4403 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004404 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004405 if (err) {
4406 btrfs_abort_transaction(trans, err);
4407 ret = err;
4408 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004409 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004410 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4411 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004412 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004413 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004414 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004415 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4416 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004417 }
Chris Mason28ed1342014-12-17 09:41:04 -08004418
Chris Mason39279cc2007-06-12 06:35:45 -04004419 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004420 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004421}
4422
Chris Masona52d9a82007-08-27 16:49:44 -04004423/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304424 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004425 * @inode - inode that we're zeroing
4426 * @from - the offset to start zeroing
4427 * @len - the length to zero, 0 to zero the entire range respective to the
4428 * offset
4429 * @front - zero up to the offset instead of from the offset on
4430 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304431 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004432 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004433 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304434int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004435 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004436{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004437 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004438 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004439 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4440 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004441 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004442 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004443 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004444 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004445 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304446 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004447 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004448 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004449 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304450 u64 block_start;
4451 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004452
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004453 if (IS_ALIGNED(offset, blocksize) &&
4454 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004455 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304456
Josef Bacik8b62f872017-10-19 14:15:55 -04004457 block_start = round_down(from, blocksize);
4458 block_end = block_start + blocksize - 1;
4459
Qu Wenruo364ecf32017-02-27 15:10:38 +08004460 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004461 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004462 if (ret)
4463 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004464
Chris Mason211c17f2008-05-15 09:13:45 -04004465again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004466 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004467 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004468 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004469 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004470 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004471 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004472 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004473 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004474
Chris Masona52d9a82007-08-27 16:49:44 -04004475 if (!PageUptodate(page)) {
4476 ret = btrfs_readpage(NULL, page);
4477 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004478 if (page->mapping != mapping) {
4479 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004480 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004481 goto again;
4482 }
Chris Masona52d9a82007-08-27 16:49:44 -04004483 if (!PageUptodate(page)) {
4484 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004485 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004486 }
4487 }
Chris Mason211c17f2008-05-15 09:13:45 -04004488 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004489
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304490 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004491 set_page_extent_mapped(page);
4492
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304493 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004494 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304495 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004496 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004497 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004498 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004499 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004500 btrfs_put_ordered_extent(ordered);
4501 goto again;
4502 }
4503
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304504 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004505 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4506 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004507
Filipe Mananae3b8a482017-11-04 00:16:59 +00004508 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004509 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004510 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304511 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004512 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004513 goto out_unlock;
4514 }
4515
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304516 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004517 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304518 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004519 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004520 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304521 memset(kaddr + (block_start - page_offset(page)),
4522 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004523 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304524 memset(kaddr + (block_start - page_offset(page)) + offset,
4525 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004526 flush_dcache_page(page);
4527 kunmap(page);
4528 }
Chris Mason247e7432008-07-17 12:53:51 -04004529 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004530 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004531 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004532
Chris Mason89642222008-07-24 09:41:53 -04004533out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004534 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004535 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004536 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004537 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004538 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004539 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004540out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004541 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004542 return ret;
4543}
4544
Josef Bacik16e75492013-10-22 12:18:51 -04004545static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4546 u64 offset, u64 len)
4547{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004548 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004549 struct btrfs_trans_handle *trans;
4550 int ret;
4551
4552 /*
4553 * Still need to make sure the inode looks like it's been updated so
4554 * that any holes get logged if we fsync.
4555 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004556 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4557 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004558 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4559 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4560 return 0;
4561 }
4562
4563 /*
4564 * 1 - for the one we're dropping
4565 * 1 - for the one we're adding
4566 * 1 - for updating the inode.
4567 */
4568 trans = btrfs_start_transaction(root, 3);
4569 if (IS_ERR(trans))
4570 return PTR_ERR(trans);
4571
4572 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4573 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004574 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004575 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004576 return ret;
4577 }
4578
David Sterbaf85b7372017-01-20 14:54:07 +01004579 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4580 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004581 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004582 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004583 else
4584 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004585 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004586 return ret;
4587}
4588
Josef Bacik695a0d02011-03-04 15:46:53 -05004589/*
4590 * This function puts in dummy file extents for the area we're creating a hole
4591 * for. So if we are truncating this file to a larger size we need to insert
4592 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4593 * the range between oldsize and size
4594 */
Josef Bacika41ad392011-01-31 15:30:16 -05004595int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004596{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004597 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004598 struct btrfs_root *root = BTRFS_I(inode)->root;
4599 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004600 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004601 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004602 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004603 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4604 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004605 u64 last_byte;
4606 u64 cur_offset;
4607 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004608 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004609
Josef Bacika71754f2013-06-17 17:14:39 -04004610 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304611 * If our size started in the middle of a block we need to zero out the
4612 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004613 * expose stale data.
4614 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304615 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004616 if (err)
4617 return err;
4618
Yan Zheng9036c102008-10-30 14:19:41 -04004619 if (size <= hole_start)
4620 return 0;
4621
David Sterbab272ae22020-02-05 19:09:33 +01004622 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004623 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004624 cur_offset = hole_start;
4625 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004626 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004627 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004628 if (IS_ERR(em)) {
4629 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004630 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004631 break;
4632 }
Yan Zheng9036c102008-10-30 14:19:41 -04004633 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004634 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004635 hole_size = last_byte - cur_offset;
4636
Yan, Zheng80825102009-11-12 09:35:36 +00004637 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004638 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004639
Josef Bacik16e75492013-10-22 12:18:51 -04004640 err = maybe_insert_hole(root, inode, cur_offset,
4641 hole_size);
4642 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004643 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004644
4645 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4646 cur_offset, hole_size);
4647 if (err)
4648 break;
4649
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004650 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004651 cur_offset + hole_size - 1, 0);
4652 hole_em = alloc_extent_map();
4653 if (!hole_em) {
4654 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4655 &BTRFS_I(inode)->runtime_flags);
4656 goto next;
4657 }
4658 hole_em->start = cur_offset;
4659 hole_em->len = hole_size;
4660 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004661
Josef Bacik5dc562c2012-08-17 13:14:17 -04004662 hole_em->block_start = EXTENT_MAP_HOLE;
4663 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004664 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004665 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004666 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004667 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004668
4669 while (1) {
4670 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004671 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004672 write_unlock(&em_tree->lock);
4673 if (err != -EEXIST)
4674 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004675 btrfs_drop_extent_cache(BTRFS_I(inode),
4676 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004677 cur_offset +
4678 hole_size - 1, 0);
4679 }
4680 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004681 } else {
4682 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4683 cur_offset, hole_size);
4684 if (err)
4685 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004686 }
Josef Bacik16e75492013-10-22 12:18:51 -04004687next:
Yan Zheng9036c102008-10-30 14:19:41 -04004688 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004689 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004690 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004691 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004692 break;
4693 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004694 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004695 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004696 return err;
4697}
4698
Eric Sandeen3972f262013-01-12 02:57:22 +00004699static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004700{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004701 struct btrfs_root *root = BTRFS_I(inode)->root;
4702 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004703 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004704 loff_t newsize = attr->ia_size;
4705 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004706 int ret;
4707
Eric Sandeen3972f262013-01-12 02:57:22 +00004708 /*
4709 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4710 * special case where we need to update the times despite not having
4711 * these flags set. For all other operations the VFS set these flags
4712 * explicitly if it wants a timestamp update.
4713 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004714 if (newsize != oldsize) {
4715 inode_inc_iversion(inode);
4716 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4717 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004718 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004719 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004720
Josef Bacika41ad392011-01-31 15:30:16 -05004721 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004722 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004723 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004724 * This is to ensure the snapshot captures a fully consistent
4725 * state of this file - if the snapshot captures this expanding
4726 * truncation, it must capture all writes that happened before
4727 * this truncation.
4728 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004729 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004730 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004731 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004732 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004733 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004734 }
Yan, Zheng80825102009-11-12 09:35:36 +00004735
Miao Xief4a2f4c2011-12-14 20:12:01 -05004736 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004737 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004738 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004739 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004740 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004741
4742 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004743 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304744 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004745 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004746 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004747 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004748 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004749
Josef Bacika41ad392011-01-31 15:30:16 -05004750 /*
4751 * We're truncating a file that used to have good data down to
4752 * zero. Make sure it gets into the ordered flush list so that
4753 * any new writes get down to disk quickly.
4754 */
4755 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004756 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4757 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004758
Josef Bacika41ad392011-01-31 15:30:16 -05004759 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004760
Andrea Gelmini52042d82018-11-28 12:05:13 +01004761 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02004762 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004763 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02004764 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004765
Filipe Manana213e8c52018-02-06 20:40:31 +00004766 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004767 if (ret && inode->i_nlink) {
4768 int err;
4769
4770 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004771 * Truncate failed, so fix up the in-memory size. We
4772 * adjusted disk_i_size down as we removed extents, so
4773 * wait for disk_i_size to be stable and then update the
4774 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004775 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004776 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004777 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004778 return err;
4779 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004780 }
Yan, Zheng80825102009-11-12 09:35:36 +00004781 }
4782
Josef Bacika41ad392011-01-31 15:30:16 -05004783 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004784}
4785
Chris Mason39279cc2007-06-12 06:35:45 -04004786static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4787{
David Howells2b0143b2015-03-17 22:25:59 +00004788 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004789 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004790 int err;
4791
Li Zefanb83cc962010-12-20 16:04:08 +08004792 if (btrfs_root_readonly(root))
4793 return -EROFS;
4794
Jan Kara31051c82016-05-26 16:55:18 +02004795 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004796 if (err)
4797 return err;
4798
Chris Mason5a3f23d2009-03-31 13:27:11 -04004799 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004800 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004801 if (err)
4802 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004803 }
Yan Zheng9036c102008-10-30 14:19:41 -04004804
Christoph Hellwig10257742010-06-04 11:30:02 +02004805 if (attr->ia_valid) {
4806 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004807 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004808 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004809
Josef Bacik22c44fe2011-11-30 10:45:38 -05004810 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004811 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004812 }
4813
Chris Mason39279cc2007-06-12 06:35:45 -04004814 return err;
4815}
Chris Mason61295eb2008-01-14 16:24:38 -05004816
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004817/*
4818 * While truncating the inode pages during eviction, we get the VFS calling
4819 * btrfs_invalidatepage() against each page of the inode. This is slow because
4820 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4821 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4822 * extent_state structures over and over, wasting lots of time.
4823 *
4824 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4825 * those expensive operations on a per page basis and do only the ordered io
4826 * finishing, while we release here the extent_map and extent_state structures,
4827 * without the excessive merging and splitting.
4828 */
4829static void evict_inode_truncate_pages(struct inode *inode)
4830{
4831 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4832 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4833 struct rb_node *node;
4834
4835 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004836 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004837
4838 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004839 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004840 struct extent_map *em;
4841
Liu Bo07e1ce02018-08-23 03:51:52 +08004842 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004843 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004844 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4845 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004846 remove_extent_mapping(map_tree, em);
4847 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004848 if (need_resched()) {
4849 write_unlock(&map_tree->lock);
4850 cond_resched();
4851 write_lock(&map_tree->lock);
4852 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004853 }
4854 write_unlock(&map_tree->lock);
4855
Filipe Manana6ca07092015-05-26 00:55:42 +01004856 /*
4857 * Keep looping until we have no more ranges in the io tree.
4858 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004859 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
4860 * still in progress (unlocked the pages in the bio but did not yet
4861 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004862 * ranges can still be locked and eviction started because before
4863 * submitting those bios, which are executed by a separate task (work
4864 * queue kthread), inode references (inode->i_count) were not taken
4865 * (which would be dropped in the end io callback of each bio).
4866 * Therefore here we effectively end up waiting for those bios and
4867 * anyone else holding locked ranges without having bumped the inode's
4868 * reference count - if we don't do it, when they access the inode's
4869 * io_tree to unlock a range it may be too late, leading to an
4870 * use-after-free issue.
4871 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004872 spin_lock(&io_tree->lock);
4873 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4874 struct extent_state *state;
4875 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004876 u64 start;
4877 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004878 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004879
4880 node = rb_first(&io_tree->state);
4881 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004882 start = state->start;
4883 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004884 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004885 spin_unlock(&io_tree->lock);
4886
David Sterbaff13db42015-12-03 14:30:40 +01004887 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004888
4889 /*
4890 * If still has DELALLOC flag, the extent didn't reach disk,
4891 * and its reserved space won't be freed by delayed_ref.
4892 * So we need to free its reserved space here.
4893 * (Refer to comment in btrfs_invalidatepage, case 2)
4894 *
4895 * Note, end is the bytenr of last byte, so we need + 1 here.
4896 */
Filipe Manana421f0922018-10-12 13:02:48 +01004897 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004898 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004899
Filipe Manana6ca07092015-05-26 00:55:42 +01004900 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004901 EXTENT_LOCKED | EXTENT_DELALLOC |
4902 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4903 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004904
Filipe Manana7064dd52014-08-08 02:47:05 +01004905 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004906 spin_lock(&io_tree->lock);
4907 }
4908 spin_unlock(&io_tree->lock);
4909}
4910
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004911static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04004912 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004913{
4914 struct btrfs_fs_info *fs_info = root->fs_info;
4915 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04004916 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04004917 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04004918 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004919
Josef Bacikd3984c92019-08-01 18:19:37 -04004920 /*
4921 * Eviction should be taking place at some place safe because of our
4922 * delayed iputs. However the normal flushing code will run delayed
4923 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
4924 *
4925 * We reserve the delayed_refs_extra here again because we can't use
4926 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
4927 * above. We reserve our extra bit here because we generate a ton of
4928 * delayed refs activity by truncating.
4929 *
4930 * If we cannot make our reservation we'll attempt to steal from the
4931 * global reserve, because we really want to be able to free up space.
4932 */
4933 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
4934 BTRFS_RESERVE_FLUSH_EVICT);
4935 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004936 /*
4937 * Try to steal from the global reserve if there is space for
4938 * it.
4939 */
Josef Bacikd3984c92019-08-01 18:19:37 -04004940 if (btrfs_check_space_for_delayed_refs(fs_info) ||
4941 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
4942 btrfs_warn(fs_info,
4943 "could not allocate space for delete; will truncate on mount");
4944 return ERR_PTR(-ENOSPC);
4945 }
4946 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004947 }
Josef Bacikd3984c92019-08-01 18:19:37 -04004948
4949 trans = btrfs_join_transaction(root);
4950 if (IS_ERR(trans))
4951 return trans;
4952
4953 if (delayed_refs_extra) {
4954 trans->block_rsv = &fs_info->trans_block_rsv;
4955 trans->bytes_reserved = delayed_refs_extra;
4956 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
4957 delayed_refs_extra, 1);
4958 }
4959 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004960}
4961
Al Virobd555972010-06-07 11:35:40 -04004962void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004963{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004964 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04004965 struct btrfs_trans_handle *trans;
4966 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004967 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04004968 int ret;
4969
liubo1abe9b82011-03-24 11:18:59 +00004970 trace_btrfs_inode_evict(inode);
4971
Nikolay Borisov3d48d982016-06-29 09:46:41 +03004972 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07004973 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03004974 return;
4975 }
4976
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004977 evict_inode_truncate_pages(inode);
4978
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004979 if (inode->i_nlink &&
4980 ((btrfs_root_refs(&root->root_item) != 0 &&
4981 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004982 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04004983 goto no_delete;
4984
Omar Sandoval27919062018-05-11 13:13:37 -07004985 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04004986 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04004987
Nikolay Borisov7ab79562017-02-20 13:50:57 +02004988 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08004989
Omar Sandoval7b40b692018-05-11 13:13:33 -07004990 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00004991 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00004992
Yan, Zheng76dda932009-09-21 16:00:26 -04004993 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004994 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4995 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004996 goto no_delete;
4997 }
4998
Nikolay Borisovaa790212017-01-10 20:35:40 +02004999 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005000 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005001 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005002
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005003 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005004 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005005 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005006 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005007 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005008
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005009 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005010
Yan, Zheng80825102009-11-12 09:35:36 +00005011 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005012 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005013 if (IS_ERR(trans))
5014 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005015
5016 trans->block_rsv = rsv;
5017
Yan, Zhengd68fc572010-05-16 10:49:58 -04005018 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005019 trans->block_rsv = &fs_info->trans_block_rsv;
5020 btrfs_end_transaction(trans);
5021 btrfs_btree_balance_dirty(fs_info);
5022 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5023 goto free_rsv;
5024 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005025 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005026 }
5027
Josef Bacik4ef31a42013-08-13 14:10:08 -04005028 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005029 * Errors here aren't a big deal, it just means we leave orphan items in
5030 * the tree. They will be cleaned up on the next mount. If the inode
5031 * number gets reused, cleanup deletes the orphan item without doing
5032 * anything, and unlink reuses the existing orphan item.
5033 *
5034 * If it turns out that we are dropping too many of these, we might want
5035 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005036 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005037 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005038 if (!IS_ERR(trans)) {
5039 trans->block_rsv = rsv;
5040 btrfs_orphan_del(trans, BTRFS_I(inode));
5041 trans->block_rsv = &fs_info->trans_block_rsv;
5042 btrfs_end_transaction(trans);
5043 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005044
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005045 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005046 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005047 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005048
Omar Sandoval27919062018-05-11 13:13:37 -07005049free_rsv:
5050 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005051no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005052 /*
5053 * If we didn't successfully delete, the orphan item will still be in
5054 * the tree and we'll retry on the next mount. Again, we might also want
5055 * to retry these periodically in the future.
5056 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005057 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005058 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005059}
5060
5061/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005062 * Return the key found in the dir entry in the location pointer, fill @type
5063 * with BTRFS_FT_*, and return 0.
5064 *
Su Yue005d6712018-03-05 17:13:37 +08005065 * If no dir entries were found, returns -ENOENT.
5066 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005067 */
5068static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005069 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005070{
5071 const char *name = dentry->d_name.name;
5072 int namelen = dentry->d_name.len;
5073 struct btrfs_dir_item *di;
5074 struct btrfs_path *path;
5075 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005076 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005077
5078 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005079 if (!path)
5080 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005081
David Sterbaf85b7372017-01-20 14:54:07 +01005082 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5083 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005084 if (IS_ERR_OR_NULL(di)) {
5085 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005086 goto out;
5087 }
Chris Masond3977122009-01-05 21:25:51 -05005088
Chris Mason5f39d392007-10-15 16:14:19 -04005089 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005090 if (location->type != BTRFS_INODE_ITEM_KEY &&
5091 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005092 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005093 btrfs_warn(root->fs_info,
5094"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5095 __func__, name, btrfs_ino(BTRFS_I(dir)),
5096 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005097 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005098 if (!ret)
5099 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005100out:
Chris Mason39279cc2007-06-12 06:35:45 -04005101 btrfs_free_path(path);
5102 return ret;
5103}
5104
5105/*
5106 * when we hit a tree root in a directory, the btrfs part of the inode
5107 * needs to be changed to reflect the root directory of the tree root. This
5108 * is kind of like crossing a mount point.
5109 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005110static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005111 struct inode *dir,
5112 struct dentry *dentry,
5113 struct btrfs_key *location,
5114 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005115{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005116 struct btrfs_path *path;
5117 struct btrfs_root *new_root;
5118 struct btrfs_root_ref *ref;
5119 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005120 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005121 int ret;
5122 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005123
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005124 path = btrfs_alloc_path();
5125 if (!path) {
5126 err = -ENOMEM;
5127 goto out;
5128 }
Chris Mason39279cc2007-06-12 06:35:45 -04005129
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005130 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005131 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5132 key.type = BTRFS_ROOT_REF_KEY;
5133 key.offset = location->objectid;
5134
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005135 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005136 if (ret) {
5137 if (ret < 0)
5138 err = ret;
5139 goto out;
5140 }
Chris Mason39279cc2007-06-12 06:35:45 -04005141
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005142 leaf = path->nodes[0];
5143 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005144 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005145 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5146 goto out;
5147
5148 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5149 (unsigned long)(ref + 1),
5150 dentry->d_name.len);
5151 if (ret)
5152 goto out;
5153
David Sterbab3b4aa72011-04-21 01:20:15 +02005154 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005155
Josef Bacik3619c942020-01-24 09:32:24 -05005156 new_root = btrfs_get_fs_root(fs_info, location, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005157 if (IS_ERR(new_root)) {
5158 err = PTR_ERR(new_root);
5159 goto out;
5160 }
5161
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005162 *sub_root = new_root;
5163 location->objectid = btrfs_root_dirid(&new_root->root_item);
5164 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005165 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005166 err = 0;
5167out:
5168 btrfs_free_path(path);
5169 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005170}
5171
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005172static void inode_tree_add(struct inode *inode)
5173{
5174 struct btrfs_root *root = BTRFS_I(inode)->root;
5175 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005176 struct rb_node **p;
5177 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005178 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005179 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005180
Al Viro1d3382cb2010-10-23 15:19:20 -04005181 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005182 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005183 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005184 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005185 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005186 while (*p) {
5187 parent = *p;
5188 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5189
David Sterba37508512018-06-29 10:56:40 +02005190 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005191 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005192 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005193 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005194 else {
5195 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005196 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005197 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005198 RB_CLEAR_NODE(parent);
5199 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005200 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005201 }
5202 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005203 rb_link_node(new, parent, p);
5204 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005205 spin_unlock(&root->inode_lock);
5206}
5207
5208static void inode_tree_del(struct inode *inode)
5209{
5210 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005211 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005212
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005213 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005214 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005215 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005216 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005217 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005218 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005219 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005220
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005221 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005222 spin_lock(&root->inode_lock);
5223 empty = RB_EMPTY_ROOT(&root->inode_tree);
5224 spin_unlock(&root->inode_lock);
5225 if (empty)
5226 btrfs_add_dead_root(root);
5227 }
5228}
5229
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005230
Chris Masone02119d2008-09-05 16:13:11 -04005231static int btrfs_init_locked_inode(struct inode *inode, void *p)
5232{
5233 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005234 inode->i_ino = args->location->objectid;
5235 memcpy(&BTRFS_I(inode)->location, args->location,
5236 sizeof(*args->location));
Josef Bacik5c8fd992020-02-14 16:11:43 -05005237 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5238 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005239 return 0;
5240}
5241
5242static int btrfs_find_actor(struct inode *inode, void *opaque)
5243{
5244 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005245 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005246 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005247}
5248
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005249static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005250 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005251 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005252{
5253 struct inode *inode;
5254 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005255 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005256
Chris Mason90d3e592014-01-09 17:28:00 -08005257 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005258 args.root = root;
5259
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005260 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005261 btrfs_init_locked_inode,
5262 (void *)&args);
5263 return inode;
5264}
5265
David Sterba4c66e0d2019-10-03 19:09:35 +02005266/*
5267 * Get an inode object given its location and corresponding root.
5268 * Path can be preallocated to prevent recursing back to iget through
5269 * allocator. NULL is also valid but may require an additional allocation
5270 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305271 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005272struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005273 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305274{
5275 struct inode *inode;
5276
Chris Mason90d3e592014-01-09 17:28:00 -08005277 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305278 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005279 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305280
5281 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005282 int ret;
5283
Filipe Manana4222ea72018-10-24 10:13:03 +01005284 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005285 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005286 inode_tree_add(inode);
5287 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005288 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005289 iget_failed(inode);
5290 /*
5291 * ret > 0 can come from btrfs_search_slot called by
5292 * btrfs_read_locked_inode, this means the inode item
5293 * was not found.
5294 */
5295 if (ret > 0)
5296 ret = -ENOENT;
5297 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005298 }
5299 }
5300
Balaji Rao1a54ef82008-07-21 02:01:04 +05305301 return inode;
5302}
5303
Filipe Manana4222ea72018-10-24 10:13:03 +01005304struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005305 struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005306{
David Sterba4c66e0d2019-10-03 19:09:35 +02005307 return btrfs_iget_path(s, location, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005308}
5309
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005310static struct inode *new_simple_dir(struct super_block *s,
5311 struct btrfs_key *key,
5312 struct btrfs_root *root)
5313{
5314 struct inode *inode = new_inode(s);
5315
5316 if (!inode)
5317 return ERR_PTR(-ENOMEM);
5318
Josef Bacik5c8fd992020-02-14 16:11:43 -05005319 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005320 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005321 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005322
5323 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005324 /*
5325 * We only need lookup, the rest is read-only and there's no inode
5326 * associated with the dentry
5327 */
5328 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005329 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005330 inode->i_fop = &simple_dir_operations;
5331 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005332 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305333 inode->i_atime = inode->i_mtime;
5334 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005335 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005336
5337 return inode;
5338}
5339
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005340static inline u8 btrfs_inode_type(struct inode *inode)
5341{
5342 /*
5343 * Compile-time asserts that generic FT_* types still match
5344 * BTRFS_FT_* types
5345 */
5346 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5347 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5348 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5349 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5350 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5351 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5352 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5353 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5354
5355 return fs_umode_to_ftype(inode->i_mode);
5356}
5357
Chris Mason3de45862008-11-17 21:02:50 -05005358struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005359{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005360 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005361 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005362 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005363 struct btrfs_root *sub_root = root;
5364 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005365 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005366 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005367
5368 if (dentry->d_name.len > BTRFS_NAME_LEN)
5369 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005370
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005371 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005372 if (ret < 0)
5373 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005374
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005375 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba4c66e0d2019-10-03 19:09:35 +02005376 inode = btrfs_iget(dir->i_sb, &location, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005377 if (IS_ERR(inode))
5378 return inode;
5379
5380 /* Do extra check against inode mode with di_type */
5381 if (btrfs_inode_type(inode) != di_type) {
5382 btrfs_crit(fs_info,
5383"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5384 inode->i_mode, btrfs_inode_type(inode),
5385 di_type);
5386 iput(inode);
5387 return ERR_PTR(-EUCLEAN);
5388 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005389 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005390 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005391
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005392 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005393 &location, &sub_root);
5394 if (ret < 0) {
5395 if (ret != -ENOENT)
5396 inode = ERR_PTR(ret);
5397 else
5398 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5399 } else {
David Sterba4c66e0d2019-10-03 19:09:35 +02005400 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005401 }
Josef Bacik87270022020-01-24 09:32:31 -05005402 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005403 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005404
Julia Lawall34d19ba2011-01-24 19:55:19 +00005405 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005406 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005407 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005408 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005410 if (ret) {
5411 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005412 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005413 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005414 }
5415
Chris Mason3de45862008-11-17 21:02:50 -05005416 return inode;
5417}
5418
Nick Pigginfe15ce42011-01-07 17:49:23 +11005419static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005420{
5421 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005422 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005423
Li Zefan848cce02012-02-21 17:04:28 +08005424 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005425 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005426
Li Zefan848cce02012-02-21 17:04:28 +08005427 if (inode) {
5428 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005429 if (btrfs_root_refs(&root->root_item) == 0)
5430 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005431
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005432 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005433 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005434 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005435 return 0;
5436}
5437
Chris Mason3de45862008-11-17 21:02:50 -05005438static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005439 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005440{
Al Viro3837d202018-10-10 16:38:27 -04005441 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005442
Al Viro3837d202018-10-10 16:38:27 -04005443 if (inode == ERR_PTR(-ENOENT))
5444 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005445 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005446}
5447
Josef Bacik23b5ec72017-07-24 15:14:25 -04005448/*
5449 * All this infrastructure exists because dir_emit can fault, and we are holding
5450 * the tree lock when doing readdir. For now just allocate a buffer and copy
5451 * our information into that, and then dir_emit from the buffer. This is
5452 * similar to what NFS does, only we don't keep the buffer around in pagecache
5453 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5454 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5455 * tree lock.
5456 */
5457static int btrfs_opendir(struct inode *inode, struct file *file)
5458{
5459 struct btrfs_file_private *private;
5460
5461 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5462 if (!private)
5463 return -ENOMEM;
5464 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5465 if (!private->filldir_buf) {
5466 kfree(private);
5467 return -ENOMEM;
5468 }
5469 file->private_data = private;
5470 return 0;
5471}
5472
5473struct dir_entry {
5474 u64 ino;
5475 u64 offset;
5476 unsigned type;
5477 int name_len;
5478};
5479
5480static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5481{
5482 while (entries--) {
5483 struct dir_entry *entry = addr;
5484 char *name = (char *)(entry + 1);
5485
David Sterba92d32172018-04-16 21:10:14 +02005486 ctx->pos = get_unaligned(&entry->offset);
5487 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5488 get_unaligned(&entry->ino),
5489 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005490 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005491 addr += sizeof(struct dir_entry) +
5492 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005493 ctx->pos++;
5494 }
5495 return 0;
5496}
5497
Al Viro9cdda8d2013-05-22 16:48:09 -04005498static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005499{
Al Viro9cdda8d2013-05-22 16:48:09 -04005500 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005501 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005502 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005503 struct btrfs_dir_item *di;
5504 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005505 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005506 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005507 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005508 struct list_head ins_list;
5509 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005510 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005511 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005512 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005513 char *name_ptr;
5514 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005515 int entries = 0;
5516 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005517 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005518 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005519
Al Viro9cdda8d2013-05-22 16:48:09 -04005520 if (!dir_emit_dots(file, ctx))
5521 return 0;
5522
David Woodhouse49593bf2008-08-17 17:08:36 +01005523 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005524 if (!path)
5525 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005526
Josef Bacik23b5ec72017-07-24 15:14:25 -04005527 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005528 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005529
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005530 INIT_LIST_HEAD(&ins_list);
5531 INIT_LIST_HEAD(&del_list);
5532 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005533
Josef Bacik23b5ec72017-07-24 15:14:25 -04005534again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005535 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005536 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005537 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005538
Chris Mason39279cc2007-06-12 06:35:45 -04005539 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5540 if (ret < 0)
5541 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005542
5543 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005544 struct dir_entry *entry;
5545
Chris Mason5f39d392007-10-15 16:14:19 -04005546 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005547 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005548 if (slot >= btrfs_header_nritems(leaf)) {
5549 ret = btrfs_next_leaf(root, path);
5550 if (ret < 0)
5551 goto err;
5552 else if (ret > 0)
5553 break;
5554 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005555 }
Chris Mason3de45862008-11-17 21:02:50 -05005556
Chris Mason5f39d392007-10-15 16:14:19 -04005557 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5558
5559 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005560 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005561 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005562 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005563 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005564 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005565 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005566 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005567 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005568 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005569 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5570 PAGE_SIZE) {
5571 btrfs_release_path(path);
5572 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5573 if (ret)
5574 goto nopos;
5575 addr = private->filldir_buf;
5576 entries = 0;
5577 total_len = 0;
5578 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005579 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005580
5581 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005582 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005583 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005584 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5585 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005586 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005587 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005588 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005589 put_unaligned(location.objectid, &entry->ino);
5590 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005591 entries++;
5592 addr += sizeof(struct dir_entry) + name_len;
5593 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005594next:
5595 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005596 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005597 btrfs_release_path(path);
5598
5599 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5600 if (ret)
5601 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005602
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005603 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005604 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005605 goto nopos;
5606
Zach Browndb62efb2013-07-11 16:19:42 -07005607 /*
5608 * Stop new entries from being returned after we return the last
5609 * entry.
5610 *
5611 * New directory entries are assigned a strictly increasing
5612 * offset. This means that new entries created during readdir
5613 * are *guaranteed* to be seen in the future by that readdir.
5614 * This has broken buggy programs which operate on names as
5615 * they're returned by readdir. Until we re-use freed offsets
5616 * we have this hack to stop new entries from being returned
5617 * under the assumption that they'll never reach this huge
5618 * offset.
5619 *
5620 * This is being careful not to overflow 32bit loff_t unless the
5621 * last entry requires it because doing so has broken 32bit apps
5622 * in the past.
5623 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005624 if (ctx->pos >= INT_MAX)
5625 ctx->pos = LLONG_MAX;
5626 else
5627 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005628nopos:
5629 ret = 0;
5630err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005631 if (put)
5632 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005633 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005634 return ret;
5635}
5636
Chris Mason39279cc2007-06-12 06:35:45 -04005637/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005638 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005639 * inode changes. But, it is most likely to find the inode in cache.
5640 * FIXME, needs more benchmarking...there are no reasons other than performance
5641 * to keep or drop this code.
5642 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005643static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005644{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005645 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005646 struct btrfs_root *root = BTRFS_I(inode)->root;
5647 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005648 int ret;
5649
Josef Bacik72ac3c02012-05-23 14:13:11 -04005650 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005651 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005652
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005653 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005654 if (IS_ERR(trans))
5655 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005656
5657 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005658 if (ret && ret == -ENOSPC) {
5659 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005660 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005661 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005662 if (IS_ERR(trans))
5663 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005664
Chris Mason94b60442010-05-26 11:02:00 -04005665 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005666 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005667 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005668 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005669 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005670
5671 return ret;
5672}
5673
5674/*
5675 * This is a copy of file_update_time. We need this so we can return error on
5676 * ENOSPC for updating the inode in the case of file write and mmap writes.
5677 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005678static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005679 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005680{
Alexander Block2bc5565282012-06-15 09:49:33 +02005681 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005682 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005683
5684 if (btrfs_root_readonly(root))
5685 return -EROFS;
5686
Josef Bacike41f9412012-03-26 09:46:47 -04005687 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005688 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005689 if (flags & S_CTIME)
5690 inode->i_ctime = *now;
5691 if (flags & S_MTIME)
5692 inode->i_mtime = *now;
5693 if (flags & S_ATIME)
5694 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005695 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005696}
5697
Chris Masond352ac62008-09-29 15:18:18 -04005698/*
5699 * find the highest existing sequence number in a directory
5700 * and then set the in-memory index_cnt variable to reflect
5701 * free sequence numbers
5702 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005703static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005704{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005705 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005706 struct btrfs_key key, found_key;
5707 struct btrfs_path *path;
5708 struct extent_buffer *leaf;
5709 int ret;
5710
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005711 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005712 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005713 key.offset = (u64)-1;
5714
5715 path = btrfs_alloc_path();
5716 if (!path)
5717 return -ENOMEM;
5718
5719 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5720 if (ret < 0)
5721 goto out;
5722 /* FIXME: we should be able to handle this */
5723 if (ret == 0)
5724 goto out;
5725 ret = 0;
5726
5727 /*
5728 * MAGIC NUMBER EXPLANATION:
5729 * since we search a directory based on f_pos we have to start at 2
5730 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5731 * else has to start at 2
5732 */
5733 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005734 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005735 goto out;
5736 }
5737
5738 path->slots[0]--;
5739
5740 leaf = path->nodes[0];
5741 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5742
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005743 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005744 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005745 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005746 goto out;
5747 }
5748
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005749 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005750out:
5751 btrfs_free_path(path);
5752 return ret;
5753}
5754
Chris Masond352ac62008-09-29 15:18:18 -04005755/*
5756 * helper to find a free sequence number in a given directory. This current
5757 * code is very simple, later versions will do smarter things in the btree
5758 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005759int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005760{
5761 int ret = 0;
5762
Nikolay Borisov877574e2017-02-20 13:50:33 +02005763 if (dir->index_cnt == (u64)-1) {
5764 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005765 if (ret) {
5766 ret = btrfs_set_inode_index_count(dir);
5767 if (ret)
5768 return ret;
5769 }
Josef Bacikaec74772008-07-24 12:12:38 -04005770 }
5771
Nikolay Borisov877574e2017-02-20 13:50:33 +02005772 *index = dir->index_cnt;
5773 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005774
5775 return ret;
5776}
5777
Chris Masonb0d5d102014-09-08 13:08:51 -07005778static int btrfs_insert_inode_locked(struct inode *inode)
5779{
5780 struct btrfs_iget_args args;
5781 args.location = &BTRFS_I(inode)->location;
5782 args.root = BTRFS_I(inode)->root;
5783
5784 return insert_inode_locked4(inode,
5785 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5786 btrfs_find_actor, &args);
5787}
5788
Anand Jain19aee8d2017-07-18 17:37:05 +08005789/*
5790 * Inherit flags from the parent inode.
5791 *
5792 * Currently only the compression flags and the cow flags are inherited.
5793 */
5794static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5795{
5796 unsigned int flags;
5797
5798 if (!dir)
5799 return;
5800
5801 flags = BTRFS_I(dir)->flags;
5802
5803 if (flags & BTRFS_INODE_NOCOMPRESS) {
5804 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5805 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5806 } else if (flags & BTRFS_INODE_COMPRESS) {
5807 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5808 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5809 }
5810
5811 if (flags & BTRFS_INODE_NODATACOW) {
5812 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5813 if (S_ISREG(inode->i_mode))
5814 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5815 }
5816
David Sterba7b6a2212018-03-26 18:40:21 +02005817 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005818}
5819
Chris Mason39279cc2007-06-12 06:35:45 -04005820static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5821 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005822 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005823 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005824 u64 ref_objectid, u64 objectid,
5825 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005826{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005827 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005828 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005829 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005830 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005831 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005832 struct btrfs_inode_ref *ref;
5833 struct btrfs_key key[2];
5834 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005835 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005836 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005837 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005838 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005839
Chris Mason5f39d392007-10-15 16:14:19 -04005840 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005841 if (!path)
5842 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005843
Josef Bacik11a19a92019-09-09 10:12:04 -04005844 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005845 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005846 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005847 if (!inode) {
5848 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005849 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005850 }
Chris Mason39279cc2007-06-12 06:35:45 -04005851
Li Zefan581bb052011-04-20 10:06:11 +08005852 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005853 * O_TMPFILE, set link count to 0, so that after this point,
5854 * we fill in an inode item with the correct link count.
5855 */
5856 if (!name)
5857 set_nlink(inode, 0);
5858
5859 /*
Li Zefan581bb052011-04-20 10:06:11 +08005860 * we have to initialize this early, so we can reclaim the inode
5861 * number if we fail afterwards in this function.
5862 */
5863 inode->i_ino = objectid;
5864
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005865 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005866 trace_btrfs_inode_request(dir);
5867
Nikolay Borisov877574e2017-02-20 13:50:33 +02005868 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005869 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005870 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005871 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005872 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005873 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005874 } else if (dir) {
5875 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005876 }
5877 /*
5878 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005879 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005880 * number
5881 */
5882 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005883 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005884 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005885 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005886 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005887
Josef Bacik5dc562c2012-08-17 13:14:17 -04005888 /*
5889 * We could have gotten an inode number from somebody who was fsynced
5890 * and then removed in this same transaction, so let's just set full
5891 * sync since it will be a full sync anyway and this will blow away the
5892 * old info in the log.
5893 */
5894 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5895
Chris Mason9c583092008-01-29 15:15:18 -05005896 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005897 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005898 key[0].offset = 0;
5899
Chris Mason9c583092008-01-29 15:15:18 -05005900 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005901
5902 if (name) {
5903 /*
5904 * Start new inodes with an inode_ref. This is slightly more
5905 * efficient for small numbers of hard links since they will
5906 * be packed into one item. Extended refs will kick in if we
5907 * add more hard links than can fit in the ref item.
5908 */
5909 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005910 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005911 key[1].offset = ref_objectid;
5912
5913 sizes[1] = name_len + sizeof(*ref);
5914 }
Chris Mason9c583092008-01-29 15:15:18 -05005915
Chris Masonb0d5d102014-09-08 13:08:51 -07005916 location = &BTRFS_I(inode)->location;
5917 location->objectid = objectid;
5918 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005919 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005920
5921 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04005922 if (ret < 0) {
5923 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005924 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04005925 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005926
Chris Masonb9473432009-03-13 11:00:37 -04005927 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005928 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005929 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005930 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005931
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005932 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005933 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05305934
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005935 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305936 inode->i_atime = inode->i_mtime;
5937 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005938 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05305939
Chris Mason5f39d392007-10-15 16:14:19 -04005940 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5941 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01005942 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06005943 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005944 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005945
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005946 if (name) {
5947 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5948 struct btrfs_inode_ref);
5949 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5950 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5951 ptr = (unsigned long)(ref + 1);
5952 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5953 }
Chris Mason9c583092008-01-29 15:15:18 -05005954
Chris Mason5f39d392007-10-15 16:14:19 -04005955 btrfs_mark_buffer_dirty(path->nodes[0]);
5956 btrfs_free_path(path);
5957
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005958 btrfs_inherit_iflags(inode, dir);
5959
Al Viro569254b2011-07-24 17:08:40 -04005960 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005961 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04005962 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005963 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005964 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5965 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005966 }
5967
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005968 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005969
5970 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005971 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005972
Alexander Block8ea05e32012-07-25 17:35:53 +02005973 btrfs_update_root_times(trans, root);
5974
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005975 ret = btrfs_inode_inherit_props(trans, inode, dir);
5976 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005977 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005978 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01005979 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005980
Chris Mason39279cc2007-06-12 06:35:45 -04005981 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005982
5983fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04005984 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005985fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005986 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005987 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005988 btrfs_free_path(path);
5989 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005990}
5991
Chris Masond352ac62008-09-29 15:18:18 -04005992/*
5993 * utility function to add 'inode' into 'parent_inode' with
5994 * a give name and a given sequence number.
5995 * if 'add_backref' is true, also insert a backref from the
5996 * inode to the parent directory.
5997 */
Chris Masone02119d2008-09-05 16:13:11 -04005998int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005999 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006000 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006001{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006002 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006003 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006004 struct btrfs_root *root = parent_inode->root;
6005 u64 ino = btrfs_ino(inode);
6006 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006007
Li Zefan33345d012011-04-20 10:31:50 +08006008 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006009 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006010 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006011 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006012 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006013 key.offset = 0;
6014 }
Chris Mason39279cc2007-06-12 06:35:45 -04006015
Li Zefan33345d012011-04-20 10:31:50 +08006016 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006017 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006018 root->root_key.objectid, parent_ino,
6019 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006020 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006021 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6022 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006023 }
6024
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006025 /* Nothing to clean up yet */
6026 if (ret)
6027 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006028
Lu Fengqi684572d2018-08-04 21:10:57 +08006029 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006030 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006031 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006032 goto fail_dir_item;
6033 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006034 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006035 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006036 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006037
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006038 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006039 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006040 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006041 /*
6042 * If we are replaying a log tree, we do not want to update the mtime
6043 * and ctime of the parent directory with the current time, since the
6044 * log replay procedure is responsible for setting them to their correct
6045 * values (the ones it had when the fsync was done).
6046 */
6047 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6048 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6049
6050 parent_inode->vfs_inode.i_mtime = now;
6051 parent_inode->vfs_inode.i_ctime = now;
6052 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006053 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006054 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006055 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006056 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006057
6058fail_dir_item:
6059 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6060 u64 local_index;
6061 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006062 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006063 root->root_key.objectid, parent_ino,
6064 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006065 if (err)
6066 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006067 } else if (add_backref) {
6068 u64 local_index;
6069 int err;
6070
6071 err = btrfs_del_inode_ref(trans, root, name, name_len,
6072 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006073 if (err)
6074 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006075 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006076
6077 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006078 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006079}
6080
6081static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006082 struct btrfs_inode *dir, struct dentry *dentry,
6083 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006084{
Josef Bacika1b075d2010-11-19 20:36:11 +00006085 int err = btrfs_add_link(trans, dir, inode,
6086 dentry->d_name.name, dentry->d_name.len,
6087 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006088 if (err > 0)
6089 err = -EEXIST;
6090 return err;
6091}
6092
Josef Bacik618e21d2007-07-11 10:18:17 -04006093static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006094 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006095{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006096 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006097 struct btrfs_trans_handle *trans;
6098 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006099 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006100 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006101 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006102 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006103
Josef Bacik9ed74f22009-09-11 16:12:44 -04006104 /*
6105 * 2 for inode item and ref
6106 * 2 for dir items
6107 * 1 for xattr if selinux is on
6108 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006109 trans = btrfs_start_transaction(root, 5);
6110 if (IS_ERR(trans))
6111 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006112
Li Zefan581bb052011-04-20 10:06:11 +08006113 err = btrfs_find_free_ino(root, &objectid);
6114 if (err)
6115 goto out_unlock;
6116
Josef Bacikaec74772008-07-24 12:12:38 -04006117 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006118 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6119 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006120 if (IS_ERR(inode)) {
6121 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006122 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006123 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006124 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006125
Casey Schauflerad19db72011-12-15 10:09:07 -05006126 /*
6127 * If the active LSM wants to access the inode during
6128 * d_instantiate it needs these. Smack checks to see
6129 * if the filesystem supports xattrs by looking at the
6130 * ops vector.
6131 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006132 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006133 init_special_inode(inode, inode->i_mode, rdev);
6134
6135 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006136 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006137 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006138
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006139 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6140 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006141 if (err)
6142 goto out_unlock;
6143
6144 btrfs_update_inode(trans, root, inode);
6145 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006146
Josef Bacik618e21d2007-07-11 10:18:17 -04006147out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006148 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006149 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006150 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006151 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006152 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006153 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006154 return err;
6155}
6156
Chris Mason39279cc2007-06-12 06:35:45 -04006157static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006158 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006159{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006160 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006161 struct btrfs_trans_handle *trans;
6162 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006163 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006164 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006165 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006166 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006167
Josef Bacik9ed74f22009-09-11 16:12:44 -04006168 /*
6169 * 2 for inode item and ref
6170 * 2 for dir items
6171 * 1 for xattr if selinux is on
6172 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006173 trans = btrfs_start_transaction(root, 5);
6174 if (IS_ERR(trans))
6175 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006176
Li Zefan581bb052011-04-20 10:06:11 +08006177 err = btrfs_find_free_ino(root, &objectid);
6178 if (err)
6179 goto out_unlock;
6180
Josef Bacikaec74772008-07-24 12:12:38 -04006181 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006182 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6183 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006184 if (IS_ERR(inode)) {
6185 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006186 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006187 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006188 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006189 /*
6190 * If the active LSM wants to access the inode during
6191 * d_instantiate it needs these. Smack checks to see
6192 * if the filesystem supports xattrs by looking at the
6193 * ops vector.
6194 */
6195 inode->i_fop = &btrfs_file_operations;
6196 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006197 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006198
6199 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6200 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006201 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006202
6203 err = btrfs_update_inode(trans, root, inode);
6204 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006205 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006206
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006207 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6208 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006209 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006210 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006211
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006212 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006213 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006214
Chris Mason39279cc2007-06-12 06:35:45 -04006215out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006216 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006217 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006218 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006219 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006220 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006221 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006222 return err;
6223}
6224
6225static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6226 struct dentry *dentry)
6227{
Filipe Manana271dba452016-01-05 16:24:05 +00006228 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006229 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006230 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006232 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006233 int err;
6234 int drop_inode = 0;
6235
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006236 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006237 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006238 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006239
Mark Fashehf1863732012-08-08 11:32:27 -07006240 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006241 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006242
Nikolay Borisov877574e2017-02-20 13:50:33 +02006243 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006244 if (err)
6245 goto fail;
6246
Yan, Zhenga22285a2010-05-16 10:48:46 -04006247 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006248 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006249 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006250 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006251 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006252 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006253 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006254 if (IS_ERR(trans)) {
6255 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006256 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006257 goto fail;
6258 }
Chris Mason5f39d392007-10-15 16:14:19 -04006259
Miao Xie67de1172013-12-26 13:07:06 +08006260 /* There are several dir indexes for this inode, clear the cache. */
6261 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006262 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006263 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006264 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006265 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006266 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006267
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006268 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6269 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006270
Yan, Zhenga5719522009-09-24 09:17:31 -04006271 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006272 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006273 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006274 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006275 int ret;
6276
Yan, Zhenga5719522009-09-24 09:17:31 -04006277 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006278 if (err)
6279 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006280 if (inode->i_nlink == 1) {
6281 /*
6282 * If new hard link count is 1, it's a file created
6283 * with open(2) O_TMPFILE flag.
6284 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006285 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006286 if (err)
6287 goto fail;
6288 }
Al Viro08c422c2011-12-23 07:58:13 -05006289 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006290 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6291 true, NULL);
6292 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6293 err = btrfs_commit_transaction(trans);
6294 trans = NULL;
6295 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006296 }
Chris Mason39279cc2007-06-12 06:35:45 -04006297
Chris Mason1832a6d2007-12-21 16:27:21 -05006298fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006299 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006300 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006301 if (drop_inode) {
6302 inode_dec_link_count(inode);
6303 iput(inode);
6304 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006305 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006306 return err;
6307}
6308
Al Viro18bb1db2011-07-26 01:41:39 -04006309static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006310{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006311 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006312 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006313 struct btrfs_trans_handle *trans;
6314 struct btrfs_root *root = BTRFS_I(dir)->root;
6315 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006316 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006317 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006318
Josef Bacik9ed74f22009-09-11 16:12:44 -04006319 /*
6320 * 2 items for inode and ref
6321 * 2 items for dir items
6322 * 1 for xattr if selinux is on
6323 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006324 trans = btrfs_start_transaction(root, 5);
6325 if (IS_ERR(trans))
6326 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006327
Li Zefan581bb052011-04-20 10:06:11 +08006328 err = btrfs_find_free_ino(root, &objectid);
6329 if (err)
6330 goto out_fail;
6331
Josef Bacikaec74772008-07-24 12:12:38 -04006332 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006333 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6334 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006335 if (IS_ERR(inode)) {
6336 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006337 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006338 goto out_fail;
6339 }
Chris Mason5f39d392007-10-15 16:14:19 -04006340
Chris Masonb0d5d102014-09-08 13:08:51 -07006341 /* these must be set before we unlock the inode */
6342 inode->i_op = &btrfs_dir_inode_operations;
6343 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006344
Eric Paris2a7dba32011-02-01 11:05:39 -05006345 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006346 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006347 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006348
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006349 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006350 err = btrfs_update_inode(trans, root, inode);
6351 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006352 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006353
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006354 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6355 dentry->d_name.name,
6356 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006357 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006358 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006359
Al Viro1e2e5472018-05-04 08:23:01 -04006360 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006361
6362out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006363 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006364 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006365 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006366 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006367 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006368 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006369 return err;
6370}
6371
Chris Masonc8b97812008-10-29 14:49:59 -04006372static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006373 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006374 size_t pg_offset, u64 extent_offset,
6375 struct btrfs_file_extent_item *item)
6376{
6377 int ret;
6378 struct extent_buffer *leaf = path->nodes[0];
6379 char *tmp;
6380 size_t max_size;
6381 unsigned long inline_size;
6382 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006383 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006384
6385 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006386 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006387 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6388 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006389 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006390 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006391 if (!tmp)
6392 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006393 ptr = btrfs_file_extent_inline_start(item);
6394
6395 read_extent_buffer(leaf, tmp, ptr, inline_size);
6396
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006397 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006398 ret = btrfs_decompress(compress_type, tmp, page,
6399 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006400
6401 /*
6402 * decompression code contains a memset to fill in any space between the end
6403 * of the uncompressed data and the end of max_size in case the decompressed
6404 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6405 * the end of an inline extent and the beginning of the next block, so we
6406 * cover that region here.
6407 */
6408
6409 if (max_size + pg_offset < PAGE_SIZE) {
6410 char *map = kmap(page);
6411 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6412 kunmap(page);
6413 }
Chris Masonc8b97812008-10-29 14:49:59 -04006414 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006415 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006416}
6417
Omar Sandoval39b07b52019-12-02 17:34:23 -08006418/**
6419 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6420 * @inode: file to search in
6421 * @page: page to read extent data into if the extent is inline
6422 * @pg_offset: offset into @page to copy to
6423 * @start: file offset
6424 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006425 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006426 * This returns the first &struct extent_map which overlaps with the given
6427 * range, reading it from the B-tree and caching it if necessary. Note that
6428 * there may be more extents which overlap the given range after the returned
6429 * extent_map.
6430 *
6431 * If @page is not NULL and the extent is inline, this also reads the extent
6432 * data directly into the page and marks the extent up to date in the io_tree.
6433 *
6434 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006435 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006436struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006437 struct page *page, size_t pg_offset,
6438 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006439{
David Sterba3ffbd682018-06-29 10:56:42 +02006440 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006441 int ret;
6442 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006443 u64 extent_start = 0;
6444 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006445 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006446 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006447 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006448 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006449 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006450 struct extent_buffer *leaf;
6451 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006452 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006453 struct extent_map_tree *em_tree = &inode->extent_tree;
6454 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006455
Chris Mason890871b2009-09-02 16:24:52 -04006456 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006457 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006458 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006459
Chris Masona52d9a82007-08-27 16:49:44 -04006460 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006461 if (em->start > start || em->start + em->len <= start)
6462 free_extent_map(em);
6463 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006464 free_extent_map(em);
6465 else
6466 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006467 }
David Sterba172ddd62011-04-21 00:48:27 +02006468 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006469 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006470 err = -ENOMEM;
6471 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006472 }
Chris Masond1310b22008-01-24 16:13:08 -05006473 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006474 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006475 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006476 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006477
Liu Bobee6ec82018-08-17 05:05:28 +08006478 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006479 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006480 err = -ENOMEM;
6481 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006482 }
6483
Liu Bobee6ec82018-08-17 05:05:28 +08006484 /* Chances are we'll be called again, so go ahead and do readahead */
6485 path->reada = READA_FORWARD;
6486
Liu Boe49aabd2018-08-25 13:47:09 +08006487 /*
6488 * Unless we're going to uncompress the inline extent, no sleep would
6489 * happen.
6490 */
6491 path->leave_spinning = 1;
6492
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006493 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006494 if (ret < 0) {
6495 err = ret;
6496 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006497 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006498 if (path->slots[0] == 0)
6499 goto not_found;
6500 path->slots[0]--;
6501 }
6502
Chris Mason5f39d392007-10-15 16:14:19 -04006503 leaf = path->nodes[0];
6504 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006505 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006506 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006507 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006508 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006509 /*
6510 * If we backup past the first extent we want to move forward
6511 * and see if there is an extent in front of us, otherwise we'll
6512 * say there is a hole for our whole search range which can
6513 * cause problems.
6514 */
6515 extent_end = start;
6516 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006517 }
6518
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006519 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006520 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006521 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006522 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6523 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006524 /* Only regular file could have regular/prealloc extent */
6525 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6526 ret = -EUCLEAN;
6527 btrfs_crit(fs_info,
6528 "regular/prealloc extent found for non-regular inode %llu",
6529 btrfs_ino(inode));
6530 goto out;
6531 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006532 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6533 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006534 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006535 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6536 path->slots[0],
6537 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006538 }
Josef Bacik25a50342013-10-14 12:08:38 -04006539next:
Yan Zheng9036c102008-10-30 14:19:41 -04006540 if (start >= extent_end) {
6541 path->slots[0]++;
6542 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6543 ret = btrfs_next_leaf(root, path);
6544 if (ret < 0) {
6545 err = ret;
6546 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006547 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006548 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006549 }
Yan Zheng9036c102008-10-30 14:19:41 -04006550 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006551 }
Yan Zheng9036c102008-10-30 14:19:41 -04006552 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6553 if (found_key.objectid != objectid ||
6554 found_key.type != BTRFS_EXTENT_DATA_KEY)
6555 goto not_found;
6556 if (start + len <= found_key.offset)
6557 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006558 if (start > found_key.offset)
6559 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006560
6561 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006562 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006563 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006564 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006565 em->block_start = EXTENT_MAP_HOLE;
6566 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006567 }
6568
Omar Sandoval39b07b52019-12-02 17:34:23 -08006569 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006570
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006571 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6572 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006573 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006574 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006575 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006576 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006577 size_t size;
6578 size_t extent_offset;
6579 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006580
Omar Sandoval39b07b52019-12-02 17:34:23 -08006581 if (!page)
Yan689f9342007-10-29 11:41:07 -04006582 goto out;
Yan689f9342007-10-29 11:41:07 -04006583
Qu Wenruoe41ca582018-06-06 15:41:49 +08006584 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006585 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006586 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6587 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006588 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006589 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006590 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006591 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006592 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006593
6594 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006595 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006596 if (btrfs_file_extent_compression(leaf, item) !=
6597 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006598 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006599 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006600 if (ret) {
6601 err = ret;
6602 goto out;
6603 }
Chris Masonc8b97812008-10-29 14:49:59 -04006604 } else {
6605 map = kmap(page);
6606 read_extent_buffer(leaf, map + pg_offset, ptr,
6607 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006608 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006609 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006610 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006611 copy_size);
6612 }
Chris Masonc8b97812008-10-29 14:49:59 -04006613 kunmap(page);
6614 }
Chris Mason179e29e2007-11-01 11:28:41 -04006615 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006616 }
Chris Masond1310b22008-01-24 16:13:08 -05006617 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006618 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006619 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006620 }
6621not_found:
6622 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006623 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006624 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006625 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006626insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006627 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006628 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006629 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006630 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6631 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006632 err = -EIO;
6633 goto out;
6634 }
Chris Masond1310b22008-01-24 16:13:08 -05006635
6636 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006637 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006638 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006639 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006640out:
Liu Boc6414282018-08-23 07:36:17 +08006641 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006642
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006643 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006644
Chris Masona52d9a82007-08-27 16:49:44 -04006645 if (err) {
6646 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006647 return ERR_PTR(err);
6648 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006649 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006650 return em;
6651}
6652
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006653struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006654 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006655{
6656 struct extent_map *em;
6657 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006658 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006659 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006660 u64 delalloc_len;
6661 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006662 int err = 0;
6663
Omar Sandoval39b07b52019-12-02 17:34:23 -08006664 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006665 if (IS_ERR(em))
6666 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006667 /*
6668 * If our em maps to:
6669 * - a hole or
6670 * - a pre-alloc extent,
6671 * there might actually be delalloc bytes behind it.
6672 */
6673 if (em->block_start != EXTENT_MAP_HOLE &&
6674 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6675 return em;
6676 else
6677 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006678
6679 /* check to see if we've wrapped (len == -1 or similar) */
6680 end = start + len;
6681 if (end < start)
6682 end = (u64)-1;
6683 else
6684 end -= 1;
6685
6686 em = NULL;
6687
6688 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006689 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006690 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006691 delalloc_end = delalloc_start + delalloc_len;
6692 if (delalloc_end < delalloc_start)
6693 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006694
6695 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006696 * We didn't find anything useful, return the original results from
6697 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006698 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006699 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006700 em = hole_em;
6701 hole_em = NULL;
6702 goto out;
6703 }
6704
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006705 /*
6706 * Adjust the delalloc_start to make sure it doesn't go backwards from
6707 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006708 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006709 delalloc_start = max(start, delalloc_start);
6710 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006711
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006712 if (delalloc_len > 0) {
6713 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006714 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006715 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006716
David Sterba172ddd62011-04-21 00:48:27 +02006717 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006718 if (!em) {
6719 err = -ENOMEM;
6720 goto out;
6721 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006722
6723 ASSERT(hole_em);
6724 /*
6725 * When btrfs_get_extent can't find anything it returns one
6726 * huge hole
6727 *
6728 * Make sure what it found really fits our range, and adjust to
6729 * make sure it is based on the start from the caller
6730 */
6731 if (hole_end <= start || hole_em->start > end) {
6732 free_extent_map(hole_em);
6733 hole_em = NULL;
6734 } else {
6735 hole_start = max(hole_em->start, start);
6736 hole_len = hole_end - hole_start;
6737 }
6738
6739 if (hole_em && delalloc_start > hole_start) {
6740 /*
6741 * Our hole starts before our delalloc, so we have to
6742 * return just the parts of the hole that go until the
6743 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006744 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006745 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006746 em->start = hole_start;
6747 em->orig_start = hole_start;
6748 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006749 * Don't adjust block start at all, it is fixed at
6750 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006751 */
6752 em->block_start = hole_em->block_start;
6753 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006754 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6755 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006756 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006757 /*
6758 * Hole is out of passed range or it starts after
6759 * delalloc range
6760 */
6761 em->start = delalloc_start;
6762 em->len = delalloc_len;
6763 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006764 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006765 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006766 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006767 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006768 return hole_em;
6769 }
6770out:
6771
6772 free_extent_map(hole_em);
6773 if (err) {
6774 free_extent_map(em);
6775 return ERR_PTR(err);
6776 }
6777 return em;
6778}
6779
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006780static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
6781 const u64 start,
6782 const u64 len,
6783 const u64 orig_start,
6784 const u64 block_start,
6785 const u64 block_len,
6786 const u64 orig_block_len,
6787 const u64 ram_bytes,
6788 const int type)
6789{
6790 struct extent_map *em = NULL;
6791 int ret;
6792
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006793 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08006794 em = create_io_em(inode, start, len, orig_start,
6795 block_start, block_len, orig_block_len,
6796 ram_bytes,
6797 BTRFS_COMPRESS_NONE, /* compress_type */
6798 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006799 if (IS_ERR(em))
6800 goto out;
6801 }
6802 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
6803 len, block_len, type);
6804 if (ret) {
6805 if (em) {
6806 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02006807 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006808 start + len - 1, 0);
6809 }
6810 em = ERR_PTR(ret);
6811 }
6812 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006813
6814 return em;
6815}
6816
Josef Bacik4b46fce2010-05-23 11:00:55 -04006817static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6818 u64 start, u64 len)
6819{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006820 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006821 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006822 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006823 struct btrfs_key ins;
6824 u64 alloc_hint;
6825 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006826
Josef Bacik4b46fce2010-05-23 11:00:55 -04006827 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006828 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006829 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006830 if (ret)
6831 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006832
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006833 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
6834 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006835 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006836 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006837 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006838 btrfs_free_reserved_extent(fs_info, ins.objectid,
6839 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006840
Josef Bacik4b46fce2010-05-23 11:00:55 -04006841 return em;
6842}
6843
Chris Mason46bfbb52010-05-26 11:04:10 -04006844/*
6845 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6846 * block must be cow'd
6847 */
Josef Bacik00361582013-08-14 14:02:47 -04006848noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006849 u64 *orig_start, u64 *orig_block_len,
6850 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006851{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006852 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006853 struct btrfs_path *path;
6854 int ret;
6855 struct extent_buffer *leaf;
6856 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006857 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006858 struct btrfs_file_extent_item *fi;
6859 struct btrfs_key key;
6860 u64 disk_bytenr;
6861 u64 backref_offset;
6862 u64 extent_end;
6863 u64 num_bytes;
6864 int slot;
6865 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006866 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006867
Chris Mason46bfbb52010-05-26 11:04:10 -04006868 path = btrfs_alloc_path();
6869 if (!path)
6870 return -ENOMEM;
6871
David Sterbaf85b7372017-01-20 14:54:07 +01006872 ret = btrfs_lookup_file_extent(NULL, root, path,
6873 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006874 if (ret < 0)
6875 goto out;
6876
6877 slot = path->slots[0];
6878 if (ret == 1) {
6879 if (slot == 0) {
6880 /* can't find the item, must cow */
6881 ret = 0;
6882 goto out;
6883 }
6884 slot--;
6885 }
6886 ret = 0;
6887 leaf = path->nodes[0];
6888 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006889 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006890 key.type != BTRFS_EXTENT_DATA_KEY) {
6891 /* not our file or wrong item type, must cow */
6892 goto out;
6893 }
6894
6895 if (key.offset > offset) {
6896 /* Wrong offset, must cow */
6897 goto out;
6898 }
6899
6900 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6901 found_type = btrfs_file_extent_type(leaf, fi);
6902 if (found_type != BTRFS_FILE_EXTENT_REG &&
6903 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6904 /* not a regular extent, must cow */
6905 goto out;
6906 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006907
6908 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6909 goto out;
6910
Miao Xiee77751a2013-12-27 21:11:50 +08006911 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6912 if (extent_end <= offset)
6913 goto out;
6914
Chris Mason46bfbb52010-05-26 11:04:10 -04006915 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006916 if (disk_bytenr == 0)
6917 goto out;
6918
6919 if (btrfs_file_extent_compression(leaf, fi) ||
6920 btrfs_file_extent_encryption(leaf, fi) ||
6921 btrfs_file_extent_other_encoding(leaf, fi))
6922 goto out;
6923
Ethan Lien78d42952018-05-17 14:58:29 +08006924 /*
6925 * Do the same check as in btrfs_cross_ref_exist but without the
6926 * unnecessary search.
6927 */
6928 if (btrfs_file_extent_generation(leaf, fi) <=
6929 btrfs_root_last_snapshot(&root->root_item))
6930 goto out;
6931
Chris Mason46bfbb52010-05-26 11:04:10 -04006932 backref_offset = btrfs_file_extent_offset(leaf, fi);
6933
Josef Bacik7ee9e442013-06-21 16:37:03 -04006934 if (orig_start) {
6935 *orig_start = key.offset - backref_offset;
6936 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6937 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6938 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006939
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006940 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04006941 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006942
6943 num_bytes = min(offset + *len, extent_end) - offset;
6944 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6945 u64 range_end;
6946
Jeff Mahoneyda170662016-06-15 09:22:56 -04006947 range_end = round_up(offset + num_bytes,
6948 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08006949 ret = test_range_bit(io_tree, offset, range_end,
6950 EXTENT_DELALLOC, 0, NULL);
6951 if (ret) {
6952 ret = -EAGAIN;
6953 goto out;
6954 }
6955 }
6956
Josef Bacik1bda19e2013-10-18 12:10:36 -04006957 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006958
6959 /*
6960 * look for other files referencing this extent, if we
6961 * find any we must cow
6962 */
Josef Bacik00361582013-08-14 14:02:47 -04006963
Liu Boe4c3b2d2017-01-30 12:25:28 -08006964 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04006965 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04006966 if (ret) {
6967 ret = 0;
6968 goto out;
6969 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006970
6971 /*
6972 * adjust disk_bytenr and num_bytes to cover just the bytes
6973 * in this extent we are about to write. If there
6974 * are any csums in that range we have to cow in order
6975 * to keep the csums correct
6976 */
6977 disk_bytenr += backref_offset;
6978 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006979 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
6980 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04006981 /*
6982 * all of the above have passed, it is safe to overwrite this extent
6983 * without cow
6984 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006985 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006986 ret = 1;
6987out:
6988 btrfs_free_path(path);
6989 return ret;
6990}
6991
Josef Bacikeb838e72012-07-31 16:28:48 -04006992static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6993 struct extent_state **cached_state, int writing)
6994{
6995 struct btrfs_ordered_extent *ordered;
6996 int ret = 0;
6997
6998 while (1) {
6999 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007000 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007001 /*
7002 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007003 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007004 * extents in this range.
7005 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007006 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007007 lockend - lockstart + 1);
7008
7009 /*
7010 * We need to make sure there are no buffered pages in this
7011 * range either, we could have raced between the invalidate in
7012 * generic_file_direct_write and locking the extent. The
7013 * invalidate needs to happen so that reads after a write do not
7014 * get stale data.
7015 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007016 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007017 (!writing || !filemap_range_has_page(inode->i_mapping,
7018 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007019 break;
7020
7021 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007022 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007023
7024 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007025 /*
7026 * If we are doing a DIO read and the ordered extent we
7027 * found is for a buffered write, we can not wait for it
7028 * to complete and retry, because if we do so we can
7029 * deadlock with concurrent buffered writes on page
7030 * locks. This happens only if our DIO read covers more
7031 * than one extent map, if at this point has already
7032 * created an ordered extent for a previous extent map
7033 * and locked its range in the inode's io tree, and a
7034 * concurrent write against that previous extent map's
7035 * range and this range started (we unlock the ranges
7036 * in the io tree only when the bios complete and
7037 * buffered writes always lock pages before attempting
7038 * to lock range in the io tree).
7039 */
7040 if (writing ||
7041 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7042 btrfs_start_ordered_extent(inode, ordered, 1);
7043 else
7044 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007045 btrfs_put_ordered_extent(ordered);
7046 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007047 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007048 * We could trigger writeback for this range (and wait
7049 * for it to complete) and then invalidate the pages for
7050 * this range (through invalidate_inode_pages2_range()),
7051 * but that can lead us to a deadlock with a concurrent
7052 * call to readpages() (a buffered read or a defrag call
7053 * triggered a readahead) on a page lock due to an
7054 * ordered dio extent we created before but did not have
7055 * yet a corresponding bio submitted (whence it can not
7056 * complete), which makes readpages() wait for that
7057 * ordered extent to complete while holding a lock on
7058 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007059 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007060 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007061 }
7062
Filipe Mananaade77022016-02-18 14:28:55 +00007063 if (ret)
7064 break;
7065
Josef Bacikeb838e72012-07-31 16:28:48 -04007066 cond_resched();
7067 }
7068
7069 return ret;
7070}
7071
Liu Bo6f9994d2017-01-31 07:50:22 -08007072/* The callers of this must take lock_extent() */
7073static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7074 u64 orig_start, u64 block_start,
7075 u64 block_len, u64 orig_block_len,
7076 u64 ram_bytes, int compress_type,
7077 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007078{
7079 struct extent_map_tree *em_tree;
7080 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007081 int ret;
7082
Liu Bo6f9994d2017-01-31 07:50:22 -08007083 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7084 type == BTRFS_ORDERED_COMPRESSED ||
7085 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007086 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007087
Josef Bacik69ffb542012-09-11 15:40:07 -04007088 em_tree = &BTRFS_I(inode)->extent_tree;
7089 em = alloc_extent_map();
7090 if (!em)
7091 return ERR_PTR(-ENOMEM);
7092
7093 em->start = start;
7094 em->orig_start = orig_start;
7095 em->len = len;
7096 em->block_len = block_len;
7097 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007098 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007099 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007100 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007101 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007102 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007103 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007104 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007105 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7106 em->compress_type = compress_type;
7107 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007108
7109 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007110 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007111 em->start + em->len - 1, 0);
7112 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007113 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007114 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007115 /*
7116 * The caller has taken lock_extent(), who could race with us
7117 * to add em?
7118 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007119 } while (ret == -EEXIST);
7120
7121 if (ret) {
7122 free_extent_map(em);
7123 return ERR_PTR(ret);
7124 }
7125
Liu Bo6f9994d2017-01-31 07:50:22 -08007126 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007127 return em;
7128}
7129
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007130
7131static int btrfs_get_blocks_direct_read(struct extent_map *em,
7132 struct buffer_head *bh_result,
7133 struct inode *inode,
7134 u64 start, u64 len)
7135{
David Sterba8530c372019-08-30 15:42:07 +02007136 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7137
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007138 if (em->block_start == EXTENT_MAP_HOLE ||
7139 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7140 return -ENOENT;
7141
7142 len = min(len, em->len - (start - em->start));
7143
7144 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7145 inode->i_blkbits;
7146 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007147 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007148 set_buffer_mapped(bh_result);
7149
7150 return 0;
7151}
7152
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007153static int btrfs_get_blocks_direct_write(struct extent_map **map,
7154 struct buffer_head *bh_result,
7155 struct inode *inode,
7156 struct btrfs_dio_data *dio_data,
7157 u64 start, u64 len)
7158{
7159 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7160 struct extent_map *em = *map;
7161 int ret = 0;
7162
7163 /*
7164 * We don't allocate a new extent in the following cases
7165 *
7166 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7167 * existing extent.
7168 * 2) The extent is marked as PREALLOC. We're good to go here and can
7169 * just use the extent.
7170 *
7171 */
7172 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7173 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7174 em->block_start != EXTENT_MAP_HOLE)) {
7175 int type;
7176 u64 block_start, orig_start, orig_block_len, ram_bytes;
7177
7178 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7179 type = BTRFS_ORDERED_PREALLOC;
7180 else
7181 type = BTRFS_ORDERED_NOCOW;
7182 len = min(len, em->len - (start - em->start));
7183 block_start = em->block_start + (start - em->start);
7184
7185 if (can_nocow_extent(inode, start, &len, &orig_start,
7186 &orig_block_len, &ram_bytes) == 1 &&
7187 btrfs_inc_nocow_writers(fs_info, block_start)) {
7188 struct extent_map *em2;
7189
7190 em2 = btrfs_create_dio_extent(inode, start, len,
7191 orig_start, block_start,
7192 len, orig_block_len,
7193 ram_bytes, type);
7194 btrfs_dec_nocow_writers(fs_info, block_start);
7195 if (type == BTRFS_ORDERED_PREALLOC) {
7196 free_extent_map(em);
7197 *map = em = em2;
7198 }
7199
7200 if (em2 && IS_ERR(em2)) {
7201 ret = PTR_ERR(em2);
7202 goto out;
7203 }
7204 /*
7205 * For inode marked NODATACOW or extent marked PREALLOC,
7206 * use the existing or preallocated extent, so does not
7207 * need to adjust btrfs_space_info's bytes_may_use.
7208 */
7209 btrfs_free_reserved_data_space_noquota(inode, start,
7210 len);
7211 goto skip_cow;
7212 }
7213 }
7214
7215 /* this will cow the extent */
7216 len = bh_result->b_size;
7217 free_extent_map(em);
7218 *map = em = btrfs_new_extent_direct(inode, start, len);
7219 if (IS_ERR(em)) {
7220 ret = PTR_ERR(em);
7221 goto out;
7222 }
7223
7224 len = min(len, em->len - (start - em->start));
7225
7226skip_cow:
7227 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7228 inode->i_blkbits;
7229 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007230 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007231 set_buffer_mapped(bh_result);
7232
7233 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7234 set_buffer_new(bh_result);
7235
7236 /*
7237 * Need to update the i_size under the extent lock so buffered
7238 * readers will get the updated i_size when we unlock.
7239 */
7240 if (!dio_data->overwrite && start + len > i_size_read(inode))
7241 i_size_write(inode, start + len);
7242
7243 WARN_ON(dio_data->reserve < len);
7244 dio_data->reserve -= len;
7245 dio_data->unsubmitted_oe_range_end = start + len;
7246 current->journal_info = dio_data;
7247out:
7248 return ret;
7249}
7250
Josef Bacik4b46fce2010-05-23 11:00:55 -04007251static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7252 struct buffer_head *bh_result, int create)
7253{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007254 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007255 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007256 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307257 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007258 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007259 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007260 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007261 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007262
Omar Sandovale1821632019-08-15 14:04:04 -07007263 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007264 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007265
Josef Bacikc3298612012-08-03 16:49:19 -04007266 lockstart = start;
7267 lockend = start + len - 1;
7268
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007269 if (current->journal_info) {
7270 /*
7271 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007272 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007273 * confused.
7274 */
chandan50745b02015-08-28 21:10:13 +05307275 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007276 current->journal_info = NULL;
7277 }
7278
Josef Bacikeb838e72012-07-31 16:28:48 -04007279 /*
7280 * If this errors out it's because we couldn't invalidate pagecache for
7281 * this range and we need to fallback to buffered.
7282 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007283 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7284 create)) {
7285 ret = -ENOTBLK;
7286 goto err;
7287 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007288
Omar Sandoval39b07b52019-12-02 17:34:23 -08007289 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007290 if (IS_ERR(em)) {
7291 ret = PTR_ERR(em);
7292 goto unlock_err;
7293 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007294
7295 /*
7296 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7297 * io. INLINE is special, and we could probably kludge it in here, but
7298 * it's still buffered so for safety lets just fall back to the generic
7299 * buffered path.
7300 *
7301 * For COMPRESSED we _have_ to read the entire extent in so we can
7302 * decompress it, so there will be buffering required no matter what we
7303 * do, so go ahead and fallback to buffered.
7304 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007305 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007306 * to buffered IO. Don't blame me, this is the price we pay for using
7307 * the generic code.
7308 */
7309 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7310 em->block_start == EXTENT_MAP_INLINE) {
7311 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007312 ret = -ENOTBLK;
7313 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007314 }
7315
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007316 if (create) {
7317 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7318 dio_data, start, len);
7319 if (ret < 0)
7320 goto unlock_err;
7321
Omar Sandovale1821632019-08-15 14:04:04 -07007322 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7323 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007324 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007325 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7326 start, len);
7327 /* Can be negative only if we read from a hole */
7328 if (ret < 0) {
7329 ret = 0;
7330 free_extent_map(em);
7331 goto unlock_err;
7332 }
7333 /*
7334 * We need to unlock only the end area that we aren't using.
7335 * The rest is going to be unlocked by the endio routine.
7336 */
7337 lockstart = start + bh_result->b_size;
7338 if (lockstart < lockend) {
Omar Sandovale1821632019-08-15 14:04:04 -07007339 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7340 lockstart, lockend, &cached_state);
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007341 } else {
7342 free_extent_state(cached_state);
7343 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007344 }
7345
Josef Bacik4b46fce2010-05-23 11:00:55 -04007346 free_extent_map(em);
7347
7348 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007349
7350unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007351 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7352 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007353err:
chandan50745b02015-08-28 21:10:13 +05307354 if (dio_data)
7355 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007356 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007357}
7358
Omar Sandoval58efbc92017-08-22 23:45:59 -07007359static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7360 struct bio *bio,
7361 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007362{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007363 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007364 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007365
Mike Christie37226b22016-06-05 14:31:52 -05007366 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007367
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007368 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007369 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007370 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007371
Chris Mason08635ba2019-07-10 12:28:14 -07007372 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007373
Miao Xie8b110e32014-09-12 18:44:03 +08007374 return ret;
7375}
7376
7377static int btrfs_check_dio_repairable(struct inode *inode,
7378 struct bio *failed_bio,
7379 struct io_failure_record *failrec,
7380 int failed_mirror)
7381{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007382 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007383 int num_copies;
7384
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007385 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007386 if (num_copies == 1) {
7387 /*
7388 * we only have a single copy of the data, so don't bother with
7389 * all the retry and error correction code that follows. no
7390 * matter what the error is, it is very likely to persist.
7391 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007392 btrfs_debug(fs_info,
7393 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7394 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007395 return 0;
7396 }
7397
7398 failrec->failed_mirror = failed_mirror;
7399 failrec->this_mirror++;
7400 if (failrec->this_mirror == failed_mirror)
7401 failrec->this_mirror++;
7402
7403 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007404 btrfs_debug(fs_info,
7405 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7406 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007407 return 0;
7408 }
7409
7410 return 1;
7411}
7412
Omar Sandoval58efbc92017-08-22 23:45:59 -07007413static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7414 struct page *page, unsigned int pgoff,
7415 u64 start, u64 end, int failed_mirror,
7416 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007417{
7418 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007419 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7420 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007421 struct bio *bio;
7422 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007423 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007424 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007425 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007426 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007427 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007428
Mike Christie37226b22016-06-05 14:31:52 -05007429 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007430
7431 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7432 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007433 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007434
7435 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7436 failed_mirror);
7437 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007438 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007439 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007440 }
7441
Liu Bo17347ce2017-05-15 15:33:27 -07007442 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007443 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007444 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007445 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007446 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007447
7448 isector = start - btrfs_io_bio(failed_bio)->logical;
7449 isector >>= inode->i_sb->s_blocksize_bits;
7450 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307451 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007452 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007453
7454 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007455 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007456 read_mode, failrec->this_mirror, failrec->in_validation);
7457
Omar Sandoval58efbc92017-08-22 23:45:59 -07007458 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7459 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007460 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007461 bio_put(bio);
7462 }
7463
Omar Sandoval58efbc92017-08-22 23:45:59 -07007464 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007465}
7466
7467struct btrfs_retry_complete {
7468 struct completion done;
7469 struct inode *inode;
7470 u64 start;
7471 int uptodate;
7472};
7473
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007474static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007475{
7476 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007477 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007478 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007479 struct extent_io_tree *io_tree, *failure_tree;
Ming Lei6dc4f102019-02-15 19:13:19 +08007480 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007481
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007482 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007483 goto end;
7484
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307485 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007486 io_tree = &BTRFS_I(inode)->io_tree;
7487 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007488 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307489
Miao Xie8b110e32014-09-12 18:44:03 +08007490 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007491 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007492 bio_for_each_segment_all(bvec, bio, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007493 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7494 io_tree, done->start, bvec->bv_page,
7495 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007496end:
7497 complete(&done->done);
7498 bio_put(bio);
7499}
7500
Omar Sandoval58efbc92017-08-22 23:45:59 -07007501static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7502 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007503{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307504 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007505 struct bio_vec bvec;
7506 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007507 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007508 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307509 unsigned int pgoff;
7510 u32 sectorsize;
7511 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007512 blk_status_t ret;
7513 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007514
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307515 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007516 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307517
Miao Xiec1dc0892014-09-12 18:43:56 +08007518 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007519 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007520 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007521
Liu Bo17347ce2017-05-15 15:33:27 -07007522 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7523 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7524 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307525
7526next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007527 done.uptodate = 0;
7528 done.start = start;
7529 init_completion(&done.done);
7530
Liu Bo17347ce2017-05-15 15:33:27 -07007531 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307532 pgoff, start, start + sectorsize - 1,
7533 io_bio->mirror_num,
7534 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007535 if (ret) {
7536 err = ret;
7537 goto next;
7538 }
Miao Xie8b110e32014-09-12 18:44:03 +08007539
David Sterba9c17f6c2017-07-19 19:26:45 +02007540 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007541
7542 if (!done.uptodate) {
7543 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307544 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007545 }
7546
Liu Bo629ebf42017-05-15 17:20:07 -07007547next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307548 start += sectorsize;
7549
Liu Bo97bf5a52017-04-07 13:11:10 -07007550 nr_sectors--;
7551 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307552 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007553 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307554 goto next_block_or_try_again;
7555 }
Miao Xie8b110e32014-09-12 18:44:03 +08007556 }
7557
Liu Bo629ebf42017-05-15 17:20:07 -07007558 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007559}
7560
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007561static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007562{
7563 struct btrfs_retry_complete *done = bio->bi_private;
7564 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007565 struct extent_io_tree *io_tree, *failure_tree;
7566 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007567 struct bio_vec *bvec;
7568 int uptodate;
7569 int ret;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007570 int i = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +08007571 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007572
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007573 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007574 goto end;
7575
7576 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307577
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307578 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007579 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307580
Josef Bacik7870d082017-05-05 11:57:15 -04007581 io_tree = &BTRFS_I(inode)->io_tree;
7582 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7583
David Sterbac09abff2017-07-13 18:10:07 +02007584 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007585 bio_for_each_segment_all(bvec, bio, iter_all) {
Omar Sandoval47df7762020-04-16 14:46:17 -07007586 ret = check_data_csum(inode, io_bio, i, bvec->bv_page,
7587 bvec->bv_offset, done->start,
7588 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007589 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007590 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7591 failure_tree, io_tree, done->start,
7592 bvec->bv_page,
7593 btrfs_ino(BTRFS_I(inode)),
7594 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007595 else
7596 uptodate = 0;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007597 i++;
Miao Xie8b110e32014-09-12 18:44:03 +08007598 }
7599
7600 done->uptodate = uptodate;
7601end:
7602 complete(&done->done);
7603 bio_put(bio);
7604}
7605
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007606static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7607 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007608{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307609 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007610 struct bio_vec bvec;
7611 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007612 struct btrfs_retry_complete done;
7613 u64 start;
7614 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307615 u32 sectorsize;
7616 int nr_sectors;
7617 unsigned int pgoff;
7618 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007619 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007620 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007621 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007622
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307623 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007624 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307625
Omar Sandoval58efbc92017-08-22 23:45:59 -07007626 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007627 start = io_bio->logical;
7628 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007629 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007630
Liu Bo17347ce2017-05-15 15:33:27 -07007631 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7632 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307633
Liu Bo17347ce2017-05-15 15:33:27 -07007634 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307635next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07007636 if (uptodate) {
7637 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
Omar Sandoval47df7762020-04-16 14:46:17 -07007638 ret = check_data_csum(inode, io_bio, csum_pos,
7639 bvec.bv_page, pgoff, start,
7640 sectorsize);
Liu Boef7cdac12017-04-13 18:11:48 -07007641 if (likely(!ret))
7642 goto next;
7643 }
Miao Xie8b110e32014-09-12 18:44:03 +08007644try_again:
7645 done.uptodate = 0;
7646 done.start = start;
7647 init_completion(&done.done);
7648
Omar Sandoval58efbc92017-08-22 23:45:59 -07007649 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
7650 pgoff, start, start + sectorsize - 1,
7651 io_bio->mirror_num, btrfs_retry_endio,
7652 &done);
7653 if (status) {
7654 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08007655 goto next;
7656 }
7657
David Sterba9c17f6c2017-07-19 19:26:45 +02007658 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007659
7660 if (!done.uptodate) {
7661 /* We might have another mirror, so try again */
7662 goto try_again;
7663 }
7664next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307665 offset += sectorsize;
7666 start += sectorsize;
7667
7668 ASSERT(nr_sectors);
7669
Liu Bo97bf5a52017-04-07 13:11:10 -07007670 nr_sectors--;
7671 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307672 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007673 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307674 goto next_block;
7675 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007676 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007677
7678 return err;
7679}
7680
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007681static blk_status_t btrfs_subio_endio_read(struct inode *inode,
7682 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007683{
7684 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7685
7686 if (skip_csum) {
7687 if (unlikely(err))
7688 return __btrfs_correct_data_nocsum(inode, io_bio);
7689 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07007690 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007691 } else {
7692 return __btrfs_subio_endio_read(inode, io_bio, err);
7693 }
7694}
7695
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007696static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08007697{
7698 struct btrfs_dio_private *dip = bio->bi_private;
7699 struct inode *inode = dip->inode;
7700 struct bio *dio_bio;
7701 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007702 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08007703
Liu Bo99c4e3b92017-09-15 15:06:51 -06007704 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08007705 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007706
Josef Bacik4b46fce2010-05-23 11:00:55 -04007707 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007708 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007709 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007710
Josef Bacik4b46fce2010-05-23 11:00:55 -04007711 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007712
Liu Bo99c4e3b92017-09-15 15:06:51 -06007713 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02007714 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01007715 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007716 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007717}
7718
Qu Wenruo524272602017-03-08 10:25:52 +08007719static void __endio_write_update_ordered(struct inode *inode,
7720 const u64 offset, const u64 bytes,
7721 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007722{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007723 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007724 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007725 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007726 u64 ordered_offset = offset;
7727 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007728 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007729
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007730 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08007731 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007732 else
Qu Wenruo524272602017-03-08 10:25:52 +08007733 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007734
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007735 while (ordered_offset < offset + bytes) {
7736 last_offset = ordered_offset;
7737 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
7738 &ordered_offset,
7739 ordered_bytes,
7740 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007741 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7742 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007743 btrfs_queue_work(wq, &ordered->work);
7744 }
7745 /*
7746 * If btrfs_dec_test_ordered_pending does not find any ordered
7747 * extent in the range, we can exit.
7748 */
7749 if (ordered_offset == last_offset)
7750 return;
7751 /*
7752 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007753 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007754 */
7755 if (ordered_offset < offset + bytes) {
7756 ordered_bytes = offset + bytes - ordered_offset;
7757 ordered = NULL;
7758 }
Chris Mason163cf092010-11-28 19:56:33 -05007759 }
Filipe Manana14543772015-11-24 16:23:54 +00007760}
7761
7762static void btrfs_endio_direct_write(struct bio *bio)
7763{
7764 struct btrfs_dio_private *dip = bio->bi_private;
7765 struct bio *dio_bio = dip->dio_bio;
7766
Qu Wenruo524272602017-03-08 10:25:52 +08007767 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007768 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007769
Josef Bacik4b46fce2010-05-23 11:00:55 -04007770 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007771
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007772 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02007773 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007774 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007775}
7776
David Sterbad0ee3932018-03-08 14:35:48 +01007777static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007778 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007779{
Josef Bacikc6100a42017-05-05 11:57:13 -04007780 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007781 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007782 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007783 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007784 return 0;
7785}
7786
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007787static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007788{
7789 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007790 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007791
Miao Xie8b110e32014-09-12 18:44:03 +08007792 if (err)
7793 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007794 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007795 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7796 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007797 (unsigned long long)bio->bi_iter.bi_sector,
7798 bio->bi_iter.bi_size, err);
7799
7800 if (dip->subio_endio)
7801 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007802
7803 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007804 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02007805 * We want to perceive the errors flag being set before
7806 * decrementing the reference count. We don't need a barrier
7807 * since atomic operations with a return value are fully
7808 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00007809 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02007810 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00007811 }
7812
7813 /* if there are more bios still pending for this dio, just exit */
Omar Sandovale3b318d2020-04-16 14:46:20 -07007814 if (!refcount_dec_and_test(&dip->refs))
Miao Xiee65e1532010-11-22 03:04:43 +00007815 goto out;
7816
Chris Mason9be33952013-05-17 18:30:14 -04007817 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007818 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007819 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08007820 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007821 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007822 }
7823out:
7824 bio_put(bio);
7825}
7826
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007827static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08007828 struct btrfs_dio_private *dip,
7829 struct bio *bio,
7830 u64 file_offset)
7831{
7832 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7833 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Omar Sandovale7a04892020-03-02 14:02:49 -08007834 u16 csum_size;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007835 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08007836
7837 /*
7838 * We load all the csum data we need when we submit
7839 * the first bio to reduce the csum tree search and
7840 * contention.
7841 */
7842 if (dip->logical_offset == file_offset) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08007843 ret = btrfs_lookup_bio_sums(inode, dip->orig_bio, file_offset,
7844 NULL);
Miao Xiec1dc0892014-09-12 18:43:56 +08007845 if (ret)
7846 return ret;
7847 }
7848
7849 if (bio == dip->orig_bio)
7850 return 0;
7851
7852 file_offset -= dip->logical_offset;
7853 file_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandovale7a04892020-03-02 14:02:49 -08007854 csum_size = btrfs_super_csum_size(btrfs_sb(inode->i_sb)->super_copy);
7855 io_bio->csum = orig_io_bio->csum + csum_size * file_offset;
Miao Xiec1dc0892014-09-12 18:43:56 +08007856
7857 return 0;
7858}
7859
David Sterbad0ee3932018-03-08 14:35:48 +01007860static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7861 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007862{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007863 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007864 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007865 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007866 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007867
Liu Bo4c274bc2017-11-01 17:19:27 -06007868 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007869 if (async_submit)
7870 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7871
Josef Bacik5fd02042012-05-02 14:00:54 -04007872 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007873 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007874 if (ret)
7875 goto err;
7876 }
Miao Xiee65e1532010-11-22 03:04:43 +00007877
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007878 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007879 goto map;
7880
7881 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007882 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7883 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007884 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007885 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007886 } else if (write) {
7887 /*
7888 * If we aren't doing async submit, calculate the csum of the
7889 * bio now.
7890 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007891 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007892 if (ret)
7893 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007894 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007895 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08007896 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007897 if (ret)
7898 goto err;
7899 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007900map:
Chris Mason08635ba2019-07-10 12:28:14 -07007901 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007902err:
Miao Xiee65e1532010-11-22 03:04:43 +00007903 return ret;
7904}
7905
Omar Sandovalc36cac22020-04-16 14:46:13 -07007906/*
7907 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7908 * or ordered extents whether or not we submit any bios.
7909 */
7910static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7911 struct inode *inode,
7912 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007913{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007914 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
7915 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007916 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007917
7918 dip = kzalloc(sizeof(*dip), GFP_NOFS);
7919 if (!dip)
7920 return NULL;
7921
7922 bio = btrfs_bio_clone(dio_bio);
7923 bio->bi_private = dip;
7924 btrfs_io_bio(bio)->logical = file_offset;
7925
Omar Sandovalc36cac22020-04-16 14:46:13 -07007926 dip->inode = inode;
7927 dip->logical_offset = file_offset;
7928 dip->bytes = dio_bio->bi_iter.bi_size;
7929 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
7930 dip->orig_bio = bio;
7931 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007932 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007933
7934 if (write) {
7935 struct btrfs_dio_data *dio_data = current->journal_info;
7936
7937 /*
7938 * Setting range start and end to the same value means that
7939 * no cleanup will happen in btrfs_direct_IO
7940 */
7941 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7942 dip->bytes;
7943 dio_data->unsubmitted_oe_range_start =
7944 dio_data->unsubmitted_oe_range_end;
7945
7946 bio->bi_end_io = btrfs_endio_direct_write;
7947 } else {
7948 bio->bi_end_io = btrfs_endio_direct_read;
7949 dip->subio_endio = btrfs_subio_endio_read;
7950 }
7951 return dip;
7952}
7953
7954static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7955 loff_t file_offset)
7956{
7957 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
7958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7959 struct btrfs_dio_private *dip;
7960 struct bio *bio;
7961 struct bio *orig_bio;
7962 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007963 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007964 u64 submit_len;
7965 int clone_offset = 0;
7966 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307967 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007968 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007969 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007970
Omar Sandovalc36cac22020-04-16 14:46:13 -07007971 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7972 if (!dip) {
7973 if (!write) {
7974 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7975 file_offset + dio_bio->bi_iter.bi_size - 1);
7976 }
7977 dio_bio->bi_status = BLK_STS_RESOURCE;
7978 dio_end_io(dio_bio);
7979 return;
7980 }
7981
7982 orig_bio = dip->orig_bio;
7983 start_sector = orig_bio->bi_iter.bi_sector;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007984 submit_len = orig_bio->bi_iter.bi_size;
7985 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
7986 start_sector << 9, submit_len, &geom);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007987 if (ret)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007988 goto out_err;
Miao Xiefacc8a222013-07-25 19:22:34 +08007989
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007990 if (geom.len >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007991 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007992 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007993 goto submit;
7994 }
7995
David Woodhouse53b381b2013-01-29 18:40:14 -05007996 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04007997 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05007998 async_submit = 0;
7999 else
8000 async_submit = 1;
8001
Liu Bo725130b2017-05-16 09:51:39 -07008002 /* bio split */
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008003 ASSERT(geom.len <= INT_MAX);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008004 do {
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008005 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04008006
Liu Bo725130b2017-05-16 09:51:39 -07008007 /*
8008 * This will never fail as it's passing GPF_NOFS and
8009 * the allocation is backed by btrfs_bioset.
8010 */
Liu Boe4770942017-05-16 10:57:14 -07008011 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07008012 clone_len);
8013 bio->bi_private = dip;
8014 bio->bi_end_io = btrfs_end_dio_bio;
8015 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00008016
Liu Bo725130b2017-05-16 09:51:39 -07008017 ASSERT(submit_len >= clone_len);
8018 submit_len -= clone_len;
8019 if (submit_len == 0)
8020 break;
Miao Xiee65e1532010-11-22 03:04:43 +00008021
Liu Bo725130b2017-05-16 09:51:39 -07008022 /*
8023 * Increase the count before we submit the bio so we know
8024 * the end IO handler won't happen before we increase the
8025 * count. Otherwise, the dip might get freed before we're
8026 * done setting it up.
8027 */
Omar Sandovale3b318d2020-04-16 14:46:20 -07008028 refcount_inc(&dip->refs);
Miao Xiee65e1532010-11-22 03:04:43 +00008029
David Sterbad0ee3932018-03-08 14:35:48 +01008030 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008031 async_submit);
8032 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008033 bio_put(bio);
Omar Sandovale3b318d2020-04-16 14:46:20 -07008034 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07008035 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008036 }
Liu Bo725130b2017-05-16 09:51:39 -07008037
8038 clone_offset += clone_len;
8039 start_sector += clone_len >> 9;
8040 file_offset += clone_len;
8041
Nikolay Borisov89b798a2019-06-03 12:05:05 +03008042 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
8043 start_sector << 9, submit_len, &geom);
Liu Bo725130b2017-05-16 09:51:39 -07008044 if (ret)
8045 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008046 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00008047
Josef Bacik02f57c72011-04-06 14:25:44 -04008048submit:
David Sterbad0ee3932018-03-08 14:35:48 +01008049 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07008050 if (!status)
Omar Sandovalc36cac22020-04-16 14:46:13 -07008051 return;
Miao Xiee65e1532010-11-22 03:04:43 +00008052
Omar Sandoval6d3113a2020-04-16 14:46:12 -07008053 if (bio != orig_bio)
8054 bio_put(bio);
Miao Xiee65e1532010-11-22 03:04:43 +00008055out_err:
8056 dip->errors = 1;
8057 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008058 * Before atomic variable goto zero, we must make sure dip->errors is
8059 * perceived to be set. This ordering is ensured by the fact that an
8060 * atomic operations with a return value are fully ordered as per
8061 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008062 */
Omar Sandovale3b318d2020-04-16 14:46:20 -07008063 if (refcount_dec_and_test(&dip->refs))
Miao Xiee65e1532010-11-22 03:04:43 +00008064 bio_io_error(dip->orig_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008065}
8066
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008067static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008068 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008069{
8070 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008071 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008072 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008073 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008074
8075 if (offset & blocksize_mask)
8076 goto out;
8077
Al Viro28060d52014-03-22 05:15:17 -04008078 if (iov_iter_alignment(iter) & blocksize_mask)
8079 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008080
Al Viro28060d52014-03-22 05:15:17 -04008081 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008082 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008083 return 0;
8084 /*
8085 * Check to make sure we don't have duplicate iov_base's in this
8086 * iovec, if so return EINVAL, otherwise we'll get csum errors
8087 * when reading back.
8088 */
8089 for (seg = 0; seg < iter->nr_segs; seg++) {
8090 for (i = seg + 1; i < iter->nr_segs; i++) {
8091 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008092 goto out;
8093 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008094 }
8095 retval = 0;
8096out:
8097 return retval;
8098}
Josef Bacikeb838e72012-07-31 16:28:48 -04008099
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008100static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008101{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008102 struct file *file = iocb->ki_filp;
8103 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008104 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308105 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008106 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008107 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008108 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008109 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008110 bool wakeup = true;
8111 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008112 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008113
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008114 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008115 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008116
Jens Axboefe0f07d2015-04-15 17:05:48 -06008117 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008118
Josef Bacik0e267c42013-07-02 10:38:02 -04008119 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008120 * The generic stuff only does filemap_write_and_wait_range, which
8121 * isn't enough if we've written compressed pages to this area, so
8122 * we need to flush the dirty pages again to make absolutely sure
8123 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008124 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008125 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008126 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8127 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008128 filemap_fdatawrite_range(inode->i_mapping, offset,
8129 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008130
Omar Sandoval6f673762015-03-16 04:33:52 -07008131 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008132 /*
8133 * If the write DIO is beyond the EOF, we need update
8134 * the isize, but it is protected by i_mutex. So we can
8135 * not unlock the i_mutex at this case.
8136 */
8137 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008138 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008139 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008140 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008141 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8142 ret = -EAGAIN;
8143 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008144 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008145 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8146 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008147 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008148 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008149
8150 /*
8151 * We need to know how many extents we reserved so that we can
8152 * do the accounting properly if we go over the number we
8153 * originally calculated. Abuse current->journal_info for this.
8154 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008155 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008156 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008157 dio_data.unsubmitted_oe_range_start = (u64)offset;
8158 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308159 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308160 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008161 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8162 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008163 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008164 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8165 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008166 }
8167
Omar Sandoval17f8c842015-03-16 04:33:50 -07008168 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008169 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008170 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008171 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008172 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308173 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008174 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008175 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308176 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008177 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008178 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008179 /*
8180 * On error we might have left some ordered extents
8181 * without submitting corresponding bios for them, so
8182 * cleanup them up to avoid other tasks getting them
8183 * and waiting for them to complete forever.
8184 */
8185 if (dio_data.unsubmitted_oe_range_start <
8186 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008187 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008188 dio_data.unsubmitted_oe_range_start,
8189 dio_data.unsubmitted_oe_range_end -
8190 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008191 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008192 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008193 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008194 offset, count - (size_t)ret, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08008195 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008196 }
Miao Xie38851cc2013-02-08 07:04:11 +00008197out:
Miao Xie2e60a512013-02-08 07:01:08 +00008198 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008199 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008200 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008201 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008202
Qu Wenruo364ecf32017-02-27 15:10:38 +08008203 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008204 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008205}
8206
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008207#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8208
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008209static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8210 __u64 start, __u64 len)
8211{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008212 int ret;
8213
8214 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8215 if (ret)
8216 return ret;
8217
David Sterba2135fb92017-06-23 04:09:57 +02008218 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008219}
8220
Chris Mason9ebefb182007-06-15 13:50:00 -04008221int btrfs_readpage(struct file *file, struct page *page)
8222{
David Sterba71ad38b2020-02-05 19:09:35 +01008223 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008224}
Chris Mason1832a6d2007-12-21 16:27:21 -05008225
Chris Mason39279cc2007-06-12 06:35:45 -04008226static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8227{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008228 struct inode *inode = page->mapping->host;
8229 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008230
8231 if (current->flags & PF_MEMALLOC) {
8232 redirty_page_for_writepage(wbc, page);
8233 unlock_page(page);
8234 return 0;
8235 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008236
8237 /*
8238 * If we are under memory pressure we will call this directly from the
8239 * VM, we need to make sure we have the inode referenced for the ordered
8240 * extent. If not just return like we didn't do anything.
8241 */
8242 if (!igrab(inode)) {
8243 redirty_page_for_writepage(wbc, page);
8244 return AOP_WRITEPAGE_ACTIVATE;
8245 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008246 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008247 btrfs_add_delayed_iput(inode);
8248 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008249}
Chris Mason39279cc2007-06-12 06:35:45 -04008250
Eric Sandeen48a3b632013-04-25 20:41:01 +00008251static int btrfs_writepages(struct address_space *mapping,
8252 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008253{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008254 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008255}
8256
Chris Mason3ab2fb52007-11-08 10:59:22 -05008257static int
8258btrfs_readpages(struct file *file, struct address_space *mapping,
8259 struct list_head *pages, unsigned nr_pages)
8260{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008261 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008262}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008263
Chris Masone6dcd2d2008-07-17 12:53:50 -04008264static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008265{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008266 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008267 if (ret == 1) {
8268 ClearPagePrivate(page);
8269 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008270 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008271 }
8272 return ret;
8273}
Chris Mason39279cc2007-06-12 06:35:45 -04008274
Chris Masone6dcd2d2008-07-17 12:53:50 -04008275static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8276{
Chris Mason98509cf2008-09-11 15:51:43 -04008277 if (PageWriteback(page) || PageDirty(page))
8278 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008279 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008280}
8281
Roman Gushchinf8e66082020-03-04 16:57:35 -08008282#ifdef CONFIG_MIGRATION
8283static int btrfs_migratepage(struct address_space *mapping,
8284 struct page *newpage, struct page *page,
8285 enum migrate_mode mode)
8286{
8287 int ret;
8288
8289 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8290 if (ret != MIGRATEPAGE_SUCCESS)
8291 return ret;
8292
8293 if (page_has_private(page)) {
8294 ClearPagePrivate(page);
8295 get_page(newpage);
8296 set_page_private(newpage, page_private(page));
8297 set_page_private(page, 0);
8298 put_page(page);
8299 SetPagePrivate(newpage);
8300 }
8301
8302 if (PagePrivate2(page)) {
8303 ClearPagePrivate2(page);
8304 SetPagePrivate2(newpage);
8305 }
8306
8307 if (mode != MIGRATE_SYNC_NO_COPY)
8308 migrate_page_copy(newpage, page);
8309 else
8310 migrate_page_states(newpage, page);
8311 return MIGRATEPAGE_SUCCESS;
8312}
8313#endif
8314
Lukas Czernerd47992f2013-05-21 23:17:23 -04008315static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8316 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008317{
Josef Bacik5fd02042012-05-02 14:00:54 -04008318 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008319 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008320 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008321 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008322 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008323 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308324 u64 start;
8325 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008326 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008327
Chris Mason8b62b722009-09-02 16:53:46 -04008328 /*
8329 * we have the page locked, so new writeback can't start,
8330 * and the dirty bit won't be cleared while we are here.
8331 *
8332 * Wait for IO on this page so that we can safely clear
8333 * the PagePrivate2 bit and do ordered accounting
8334 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008335 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008336
Josef Bacik5fd02042012-05-02 14:00:54 -04008337 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008338 if (offset) {
8339 btrfs_releasepage(page, GFP_NOFS);
8340 return;
8341 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008342
8343 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008344 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308345again:
8346 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008347 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308348 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008349 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008350 end = min(page_end,
8351 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008352 /*
8353 * IO on this page will never be started, so we need
8354 * to account for any ordered extents now
8355 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008356 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308357 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008358 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008359 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008360 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008361 /*
8362 * whoever cleared the private bit is responsible
8363 * for the finish_ordered_io
8364 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008365 if (TestClearPagePrivate2(page)) {
8366 struct btrfs_ordered_inode_tree *tree;
8367 u64 new_len;
8368
8369 tree = &BTRFS_I(inode)->ordered_tree;
8370
8371 spin_lock_irq(&tree->lock);
8372 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308373 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008374 if (new_len < ordered->truncated_len)
8375 ordered->truncated_len = new_len;
8376 spin_unlock_irq(&tree->lock);
8377
8378 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308379 start,
8380 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008381 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008382 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008383 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008384 if (!inode_evicting) {
8385 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308386 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008387 &cached_state);
8388 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308389
8390 start = end + 1;
8391 if (start < page_end)
8392 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008393 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008394
Qu Wenruob9d0b382015-09-29 10:35:16 +08008395 /*
8396 * Qgroup reserved space handler
8397 * Page here will be either
8398 * 1) Already written to disk
8399 * In this case, its reserved space is released from data rsv map
8400 * and will be freed by delayed_ref handler finally.
8401 * So even we call qgroup_free_data(), it won't decrease reserved
8402 * space.
8403 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008404 * This means the reserved space should be freed here. However,
8405 * if a truncate invalidates the page (by clearing PageDirty)
8406 * and the page is accounted for while allocating extent
8407 * in btrfs_check_data_free_space() we let delayed_ref to
8408 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008409 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008410 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008411 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008412 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008413 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008414 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8415 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008416 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008417
8418 __btrfs_releasepage(page, GFP_NOFS);
8419 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008420
Chris Mason4a096752008-07-21 10:29:44 -04008421 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008422 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008423 ClearPagePrivate(page);
8424 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008425 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008426 }
Chris Mason39279cc2007-06-12 06:35:45 -04008427}
8428
Chris Mason9ebefb182007-06-15 13:50:00 -04008429/*
8430 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8431 * called from a page fault handler when a page is first dirtied. Hence we must
8432 * be careful to check for EOF conditions here. We set the page up correctly
8433 * for a written page which means we get ENOSPC checking when writing into
8434 * holes and correct delalloc and unwritten extent mapping on filesystems that
8435 * support these features.
8436 *
8437 * We are not allowed to take the i_mutex here so we have to play games to
8438 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008439 * truncate_setsize() writes the inode size before removing pages, once we have
8440 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008441 * beyond EOF, then the page is guaranteed safe against truncation until we
8442 * unlock the page.
8443 */
Souptick Joardera528a242018-06-06 19:54:44 +05308444vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008445{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008446 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008447 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008448 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008449 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8450 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008451 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008452 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008453 char *kaddr;
8454 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008455 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308456 vm_fault_t ret;
8457 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008458 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308459 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008460 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008461 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308462 u64 end;
8463
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008464 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008465
Jan Karab2b5ef52012-06-12 16:20:45 +02008466 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008467 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008468 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308469 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008470
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308471 /*
8472 * Reserving delalloc space after obtaining the page lock can lead to
8473 * deadlock. For example, if a dirty page is locked by this function
8474 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8475 * dirty page write out, then the btrfs_writepage() function could
8476 * end up waiting indefinitely to get a lock on the page currently
8477 * being processed by btrfs_page_mkwrite() function.
8478 */
Souptick Joardera528a242018-06-06 19:54:44 +05308479 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308480 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308481 if (!ret2) {
8482 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008483 reserved = 1;
8484 }
Souptick Joardera528a242018-06-06 19:54:44 +05308485 if (ret2) {
8486 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008487 if (reserved)
8488 goto out;
8489 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008490 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008491
Nick Piggin56a76f82009-03-31 15:23:23 -07008492 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008493again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008494 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008495 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008496
Chris Mason9ebefb182007-06-15 13:50:00 -04008497 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008498 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008499 /* page got truncated out from underneath us */
8500 goto out_unlock;
8501 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008502 wait_on_page_writeback(page);
8503
David Sterbaff13db42015-12-03 14:30:40 +01008504 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008505 set_page_extent_mapped(page);
8506
Chris Masoneb84ae02008-07-17 13:53:27 -04008507 /*
8508 * we can't set the delalloc bits if there are pending ordered
8509 * extents. Drop our locks and wait for them to finish
8510 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008511 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8512 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008513 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008514 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008515 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008516 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008517 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008518 btrfs_put_ordered_extent(ordered);
8519 goto again;
8520 }
8521
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008522 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008523 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008524 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008525 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308526 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008527 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008528 page_start, PAGE_SIZE - reserved_space,
8529 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308530 }
8531 }
8532
Josef Bacikfbf19082009-10-01 17:10:23 -04008533 /*
Liu Bo54160342016-12-13 12:15:19 -08008534 * page_mkwrite gets called when the page is firstly dirtied after it's
8535 * faulted in, but write(2) could also dirty a page and set delalloc
8536 * bits, thus in this case for space account reason, we still need to
8537 * clear any delalloc bits within this page range since we have to
8538 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008539 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308540 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008541 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8542 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008543
Souptick Joardera528a242018-06-06 19:54:44 +05308544 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008545 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308546 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008547 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008548 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008549 ret = VM_FAULT_SIGBUS;
8550 goto out_unlock;
8551 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008552
8553 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008554 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008555 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008556 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008557 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008558
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008559 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008560 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008561 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008562 flush_dcache_page(page);
8563 kunmap(page);
8564 }
Chris Mason247e7432008-07-17 12:53:51 -04008565 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008566 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008567 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008568
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008569 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008570 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008571 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008572
David Sterbae43bbe52017-12-12 21:43:52 +01008573 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008574
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008575 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8576 sb_end_pagefault(inode->i_sb);
8577 extent_changeset_free(data_reserved);
8578 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008579
8580out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008581 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008582out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008583 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008584 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008585 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008586out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008587 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008588 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008589 return ret;
8590}
8591
Filipe Manana213e8c52018-02-06 20:40:31 +00008592static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008593{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008594 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008595 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008596 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008597 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008598 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008599 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008600 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008601
Filipe Manana213e8c52018-02-06 20:40:31 +00008602 if (!skip_writeback) {
8603 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8604 (u64)-1);
8605 if (ret)
8606 return ret;
8607 }
Chris Mason39279cc2007-06-12 06:35:45 -04008608
Josef Bacikfcb80c22011-05-03 10:40:22 -04008609 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008610 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8611 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008612 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008613 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008614 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008615 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008616 * be free'd up by the truncate operation, but also have some slack
8617 * space reserved in case it uses space during the truncate (thank you
8618 * very much snapshotting).
8619 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008620 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008621 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008622 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008623 * doesn't end up using space reserved for updating the inode. We also
8624 * need to be able to stop the transaction and start a new one, which
8625 * means we need to be able to update the inode several times, and we
8626 * have no idea of knowing how many times that will be, so we can't just
8627 * reserve 1 item for the entirety of the operation, so that has to be
8628 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008629 *
8630 * So that leaves us with
8631 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008632 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008633 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008634 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008635 * updating the inode.
8636 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008637 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008638 if (!rsv)
8639 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008640 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008641 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008642
Josef Bacik907cbce2011-08-08 13:46:15 -04008643 /*
Josef Bacik07127182011-08-19 10:29:59 -04008644 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008645 * 1 for updating the inode.
8646 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008647 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008648 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008649 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008650 goto out;
8651 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008652
Josef Bacik907cbce2011-08-08 13:46:15 -04008653 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008654 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008655 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008656 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008657
Chris Mason5a3f23d2009-03-31 13:27:11 -04008658 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008659 * So if we truncate and then write and fsync we normally would just
8660 * write the extents that changed, which is a problem if we need to
8661 * first truncate that entire inode. So set this flag so we write out
8662 * all of the extents in the inode to the sync log so we're completely
8663 * safe.
8664 */
8665 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008666 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008667
Yan, Zheng80825102009-11-12 09:35:36 +00008668 while (1) {
8669 ret = btrfs_truncate_inode_items(trans, root, inode,
8670 inode->i_size,
8671 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008672 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008673 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008674 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008675
Yan, Zheng80825102009-11-12 09:35:36 +00008676 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008677 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008678 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008679
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008680 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008681 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008682
8683 trans = btrfs_start_transaction(root, 2);
8684 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008685 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008686 trans = NULL;
8687 break;
8688 }
8689
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008690 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008691 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008692 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008693 BUG_ON(ret); /* shouldn't happen */
8694 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008695 }
8696
Josef Bacikddfae632017-10-19 14:16:02 -04008697 /*
8698 * We can't call btrfs_truncate_block inside a trans handle as we could
8699 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8700 * we've truncated everything except the last little bit, and can do
8701 * btrfs_truncate_block and then update the disk_i_size.
8702 */
8703 if (ret == NEED_TRUNCATE_BLOCK) {
8704 btrfs_end_transaction(trans);
8705 btrfs_btree_balance_dirty(fs_info);
8706
8707 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8708 if (ret)
8709 goto out;
8710 trans = btrfs_start_transaction(root, 1);
8711 if (IS_ERR(trans)) {
8712 ret = PTR_ERR(trans);
8713 goto out;
8714 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008715 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008716 }
8717
Chris Mason917c16b2011-11-08 14:49:59 -05008718 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008719 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008720
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008721 trans->block_rsv = &fs_info->trans_block_rsv;
8722 ret2 = btrfs_update_inode(trans, root, inode);
8723 if (ret2 && !ret)
8724 ret = ret2;
8725
8726 ret2 = btrfs_end_transaction(trans);
8727 if (ret2 && !ret)
8728 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008729 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008730 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008731out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008732 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008733
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008734 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008735}
8736
Sven Wegener3b963622008-06-09 21:57:42 -04008737/*
Chris Masond352ac62008-09-29 15:18:18 -04008738 * create a new subvolume directory/inode (helper for the ioctl).
8739 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008740int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008741 struct btrfs_root *new_root,
8742 struct btrfs_root *parent_root,
8743 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008744{
Chris Mason39279cc2007-06-12 06:35:45 -04008745 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008746 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008747 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008748
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008749 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8750 new_dirid, new_dirid,
8751 S_IFDIR | (~current_umask() & S_IRWXUGO),
8752 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008753 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008754 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008755 inode->i_op = &btrfs_dir_inode_operations;
8756 inode->i_fop = &btrfs_dir_file_operations;
8757
Miklos Szeredibfe86842011-10-28 14:13:29 +02008758 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008759 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008760 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008761
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008762 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8763 if (err)
8764 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008765 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008766 new_root->root_key.objectid, err);
8767
Yan, Zheng76dda932009-09-21 16:00:26 -04008768 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008769
Yan, Zheng76dda932009-09-21 16:00:26 -04008770 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008771 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008772}
8773
Chris Mason39279cc2007-06-12 06:35:45 -04008774struct inode *btrfs_alloc_inode(struct super_block *sb)
8775{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008776 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008777 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008778 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008779
David Sterba712e36c2017-10-31 17:08:27 +01008780 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008781 if (!ei)
8782 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008783
8784 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008785 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008786 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008787 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008788 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008789 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008790 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008791 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008792 ei->disk_i_size = 0;
8793 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008794 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008795 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008796 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008797 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008798 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008799
Josef Bacik9e0baf62011-07-15 15:16:44 +00008800 spin_lock_init(&ei->lock);
8801 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008802 if (sb->s_magic != BTRFS_TEST_MAGIC)
8803 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8804 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008805 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008806 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008807 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008808
Miao Xie16cdcec2011-04-22 18:12:22 +08008809 ei->delayed_node = NULL;
8810
chandan r9cc97d62012-07-04 12:48:07 +05308811 ei->i_otime.tv_sec = 0;
8812 ei->i_otime.tv_nsec = 0;
8813
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008814 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008815 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008816 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8817 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8818 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008819 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8820 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008821 ei->io_tree.track_uptodate = true;
8822 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008823 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008824 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008825 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008826 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008827 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008828 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008829 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008830
8831 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008832}
8833
Josef Bacikaaedb552013-10-11 14:44:09 -04008834#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8835void btrfs_test_destroy_inode(struct inode *inode)
8836{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008837 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008838 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8839}
8840#endif
8841
Al Viro26602ca2019-04-10 15:14:41 -04008842void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008843{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008844 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8845}
8846
Chris Mason39279cc2007-06-12 06:35:45 -04008847void btrfs_destroy_inode(struct inode *inode)
8848{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008849 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008850 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008851 struct btrfs_root *root = BTRFS_I(inode)->root;
8852
Al Virob3d9b7a2012-06-09 13:51:19 -04008853 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008854 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008855 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8856 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008857 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008858 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008859 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008860 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008861 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008862
Chris Mason5a3f23d2009-03-31 13:27:11 -04008863 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008864 * This can happen where we create an inode, but somebody else also
8865 * created the same inode and we need to destroy the one we already
8866 * created.
8867 */
8868 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008869 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008870
Chris Masond3977122009-01-05 21:25:51 -05008871 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008872 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8873 if (!ordered)
8874 break;
8875 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008876 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008877 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008878 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008879 btrfs_remove_ordered_extent(inode, ordered);
8880 btrfs_put_ordered_extent(ordered);
8881 btrfs_put_ordered_extent(ordered);
8882 }
8883 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008884 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008885 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008886 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008887 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008888 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008889}
8890
Al Viro45321ac2010-06-07 13:43:19 -04008891int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008892{
8893 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008894
Naohiro Aota6379ef92013-06-06 09:56:34 +00008895 if (root == NULL)
8896 return 1;
8897
Liu Bofa6ac872013-02-20 14:10:23 +00008898 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008899 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008900 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008901 else
Al Viro45321ac2010-06-07 13:43:19 -04008902 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008903}
8904
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008905static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008906{
8907 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8908
8909 inode_init_once(&ei->vfs_inode);
8910}
8911
David Sterbae67c7182018-02-19 17:24:18 +01008912void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008913{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008914 /*
8915 * Make sure all delayed rcu free inodes are flushed before we
8916 * destroy cache.
8917 */
8918 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008919 kmem_cache_destroy(btrfs_inode_cachep);
8920 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008921 kmem_cache_destroy(btrfs_path_cachep);
8922 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008923 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008924}
8925
Liu Bof5c29bd2017-11-02 17:21:50 -06008926int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008927{
David Sterba837e1972012-09-07 03:00:48 -06008928 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008929 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008930 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8931 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008932 if (!btrfs_inode_cachep)
8933 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008934
David Sterba837e1972012-09-07 03:00:48 -06008935 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008936 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008937 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008938 if (!btrfs_trans_handle_cachep)
8939 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008940
David Sterba837e1972012-09-07 03:00:48 -06008941 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008942 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008943 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008944 if (!btrfs_path_cachep)
8945 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008946
David Sterba837e1972012-09-07 03:00:48 -06008947 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008948 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008949 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008950 if (!btrfs_free_space_cachep)
8951 goto fail;
8952
Christophe Leroy3acd4852019-08-21 15:05:55 +00008953 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8954 PAGE_SIZE, PAGE_SIZE,
8955 SLAB_RED_ZONE, NULL);
8956 if (!btrfs_free_space_bitmap_cachep)
8957 goto fail;
8958
Chris Mason39279cc2007-06-12 06:35:45 -04008959 return 0;
8960fail:
8961 btrfs_destroy_cachep();
8962 return -ENOMEM;
8963}
8964
David Howellsa528d352017-01-31 16:46:22 +00008965static int btrfs_getattr(const struct path *path, struct kstat *stat,
8966 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008967{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008968 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008969 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008970 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008971 u32 bi_flags = BTRFS_I(inode)->flags;
8972
8973 stat->result_mask |= STATX_BTIME;
8974 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8975 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8976 if (bi_flags & BTRFS_INODE_APPEND)
8977 stat->attributes |= STATX_ATTR_APPEND;
8978 if (bi_flags & BTRFS_INODE_COMPRESS)
8979 stat->attributes |= STATX_ATTR_COMPRESSED;
8980 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8981 stat->attributes |= STATX_ATTR_IMMUTABLE;
8982 if (bi_flags & BTRFS_INODE_NODUMP)
8983 stat->attributes |= STATX_ATTR_NODUMP;
8984
8985 stat->attributes_mask |= (STATX_ATTR_APPEND |
8986 STATX_ATTR_COMPRESSED |
8987 STATX_ATTR_IMMUTABLE |
8988 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008989
Chris Mason39279cc2007-06-12 06:35:45 -04008990 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008991 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008992
8993 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008994 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008995 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008996 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008997 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008998 return 0;
8999}
9000
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009001static int btrfs_rename_exchange(struct inode *old_dir,
9002 struct dentry *old_dentry,
9003 struct inode *new_dir,
9004 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009005{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009006 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009007 struct btrfs_trans_handle *trans;
9008 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009009 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009010 struct inode *new_inode = new_dentry->d_inode;
9011 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009012 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009013 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009014 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9015 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009016 u64 old_idx = 0;
9017 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009018 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009019 bool root_log_pinned = false;
9020 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009021 struct btrfs_log_ctx ctx_root;
9022 struct btrfs_log_ctx ctx_dest;
9023 bool sync_log_root = false;
9024 bool sync_log_dest = false;
9025 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009026
9027 /* we only allow rename subvolume link between subvolumes */
9028 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9029 return -EXDEV;
9030
Filipe Mananad4682ba2018-06-11 19:24:28 +01009031 btrfs_init_log_ctx(&ctx_root, old_inode);
9032 btrfs_init_log_ctx(&ctx_dest, new_inode);
9033
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009034 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009035 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9036 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009037 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009038
9039 /*
9040 * We want to reserve the absolute worst case amount of items. So if
9041 * both inodes are subvols and we need to unlink them then that would
9042 * require 4 item modifications, but if they are both normal inodes it
9043 * would require 5 item modifications, so we'll assume their normal
9044 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9045 * should cover the worst case number of items we'll modify.
9046 */
9047 trans = btrfs_start_transaction(root, 12);
9048 if (IS_ERR(trans)) {
9049 ret = PTR_ERR(trans);
9050 goto out_notrans;
9051 }
9052
Josef Bacik3e174092019-11-15 15:43:06 -05009053 if (dest != root)
9054 btrfs_record_root_in_trans(trans, dest);
9055
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009056 /*
9057 * We need to find a free sequence number both in the source and
9058 * in the destination directory for the exchange.
9059 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009060 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009061 if (ret)
9062 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009063 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009064 if (ret)
9065 goto out_fail;
9066
9067 BTRFS_I(old_inode)->dir_index = 0ULL;
9068 BTRFS_I(new_inode)->dir_index = 0ULL;
9069
9070 /* Reference for the source. */
9071 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9072 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009073 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009074 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009075 btrfs_pin_log_trans(root);
9076 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009077 ret = btrfs_insert_inode_ref(trans, dest,
9078 new_dentry->d_name.name,
9079 new_dentry->d_name.len,
9080 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009081 btrfs_ino(BTRFS_I(new_dir)),
9082 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009083 if (ret)
9084 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009085 }
9086
9087 /* And now for the dest. */
9088 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9089 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009090 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009091 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009092 btrfs_pin_log_trans(dest);
9093 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009094 ret = btrfs_insert_inode_ref(trans, root,
9095 old_dentry->d_name.name,
9096 old_dentry->d_name.len,
9097 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009098 btrfs_ino(BTRFS_I(old_dir)),
9099 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009100 if (ret)
9101 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009102 }
9103
9104 /* Update inode version and ctime/mtime. */
9105 inode_inc_iversion(old_dir);
9106 inode_inc_iversion(new_dir);
9107 inode_inc_iversion(old_inode);
9108 inode_inc_iversion(new_inode);
9109 old_dir->i_ctime = old_dir->i_mtime = ctime;
9110 new_dir->i_ctime = new_dir->i_mtime = ctime;
9111 old_inode->i_ctime = ctime;
9112 new_inode->i_ctime = ctime;
9113
9114 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009115 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9116 BTRFS_I(old_inode), 1);
9117 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9118 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009119 }
9120
9121 /* src is a subvolume */
9122 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009123 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009124 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009125 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9126 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009127 old_dentry->d_name.name,
9128 old_dentry->d_name.len);
9129 if (!ret)
9130 ret = btrfs_update_inode(trans, root, old_inode);
9131 }
9132 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009133 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009134 goto out_fail;
9135 }
9136
9137 /* dest is a subvolume */
9138 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009139 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009140 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009141 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9142 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009143 new_dentry->d_name.name,
9144 new_dentry->d_name.len);
9145 if (!ret)
9146 ret = btrfs_update_inode(trans, dest, new_inode);
9147 }
9148 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009149 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009150 goto out_fail;
9151 }
9152
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009153 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009154 new_dentry->d_name.name,
9155 new_dentry->d_name.len, 0, old_idx);
9156 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009157 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009158 goto out_fail;
9159 }
9160
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009161 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009162 old_dentry->d_name.name,
9163 old_dentry->d_name.len, 0, new_idx);
9164 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009165 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009166 goto out_fail;
9167 }
9168
9169 if (old_inode->i_nlink == 1)
9170 BTRFS_I(old_inode)->dir_index = old_idx;
9171 if (new_inode->i_nlink == 1)
9172 BTRFS_I(new_inode)->dir_index = new_idx;
9173
Filipe Manana86e8aa02016-05-05 02:02:27 +01009174 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009175 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009176 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9177 BTRFS_I(old_dir), parent,
9178 false, &ctx_root);
9179 if (ret == BTRFS_NEED_LOG_SYNC)
9180 sync_log_root = true;
9181 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9182 commit_transaction = true;
9183 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009184 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009185 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009186 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009187 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009188 if (!commit_transaction) {
9189 parent = old_dentry->d_parent;
9190 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9191 BTRFS_I(new_dir), parent,
9192 false, &ctx_dest);
9193 if (ret == BTRFS_NEED_LOG_SYNC)
9194 sync_log_dest = true;
9195 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9196 commit_transaction = true;
9197 ret = 0;
9198 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009199 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009200 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009201 }
9202out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009203 /*
9204 * If we have pinned a log and an error happened, we unpin tasks
9205 * trying to sync the log and force them to fallback to a transaction
9206 * commit if the log currently contains any of the inodes involved in
9207 * this rename operation (to ensure we do not persist a log with an
9208 * inconsistent state for any of these inodes or leading to any
9209 * inconsistencies when replayed). If the transaction was aborted, the
9210 * abortion reason is propagated to userspace when attempting to commit
9211 * the transaction. If the log does not contain any of these inodes, we
9212 * allow the tasks to sync it.
9213 */
9214 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009215 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9216 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9217 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009218 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009219 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009220 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009221
9222 if (root_log_pinned) {
9223 btrfs_end_log_trans(root);
9224 root_log_pinned = false;
9225 }
9226 if (dest_log_pinned) {
9227 btrfs_end_log_trans(dest);
9228 dest_log_pinned = false;
9229 }
9230 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009231 if (!ret && sync_log_root && !commit_transaction) {
9232 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9233 &ctx_root);
9234 if (ret)
9235 commit_transaction = true;
9236 }
9237 if (!ret && sync_log_dest && !commit_transaction) {
9238 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9239 &ctx_dest);
9240 if (ret)
9241 commit_transaction = true;
9242 }
9243 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009244 /*
9245 * We may have set commit_transaction when logging the new name
9246 * in the destination root, in which case we left the source
9247 * root context in the list of log contextes. So make sure we
9248 * remove it to avoid invalid memory accesses, since the context
9249 * was allocated in our stack frame.
9250 */
9251 if (sync_log_root) {
9252 mutex_lock(&root->log_mutex);
9253 list_del_init(&ctx_root.list);
9254 mutex_unlock(&root->log_mutex);
9255 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009256 ret = btrfs_commit_transaction(trans);
9257 } else {
9258 int ret2;
9259
9260 ret2 = btrfs_end_transaction(trans);
9261 ret = ret ? ret : ret2;
9262 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009263out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009264 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9265 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009266 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009267
Filipe Mananae6c61712019-11-08 16:11:56 +00009268 ASSERT(list_empty(&ctx_root.list));
9269 ASSERT(list_empty(&ctx_dest.list));
9270
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009271 return ret;
9272}
9273
9274static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9275 struct btrfs_root *root,
9276 struct inode *dir,
9277 struct dentry *dentry)
9278{
9279 int ret;
9280 struct inode *inode;
9281 u64 objectid;
9282 u64 index;
9283
9284 ret = btrfs_find_free_ino(root, &objectid);
9285 if (ret)
9286 return ret;
9287
9288 inode = btrfs_new_inode(trans, root, dir,
9289 dentry->d_name.name,
9290 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009291 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009292 objectid,
9293 S_IFCHR | WHITEOUT_MODE,
9294 &index);
9295
9296 if (IS_ERR(inode)) {
9297 ret = PTR_ERR(inode);
9298 return ret;
9299 }
9300
9301 inode->i_op = &btrfs_special_inode_operations;
9302 init_special_inode(inode, inode->i_mode,
9303 WHITEOUT_DEV);
9304
9305 ret = btrfs_init_inode_security(trans, inode, dir,
9306 &dentry->d_name);
9307 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009308 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009309
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009310 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9311 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009312 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009313 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009314
9315 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009316out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009317 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009318 if (ret)
9319 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009320 iput(inode);
9321
Filipe Mananac9901612016-05-05 01:41:57 +01009322 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009323}
9324
Chris Mason39279cc2007-06-12 06:35:45 -04009325static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009326 struct inode *new_dir, struct dentry *new_dentry,
9327 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009328{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009329 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009330 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009331 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009332 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9333 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009334 struct inode *new_inode = d_inode(new_dentry);
9335 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009336 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009337 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009338 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009339 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009340 struct btrfs_log_ctx ctx;
9341 bool sync_log = false;
9342 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009343
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009344 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009345 return -EPERM;
9346
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009347 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009348 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009349 return -EXDEV;
9350
Li Zefan33345d012011-04-20 10:31:50 +08009351 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009352 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009353 return -ENOTEMPTY;
9354
Chris Mason39279cc2007-06-12 06:35:45 -04009355 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009356 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009357 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009358
9359
9360 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009361 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009362 new_dentry->d_name.name,
9363 new_dentry->d_name.len);
9364
9365 if (ret) {
9366 if (ret == -EEXIST) {
9367 /* we shouldn't get
9368 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309369 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009370 return ret;
9371 }
9372 } else {
9373 /* maybe -EOVERFLOW */
9374 return ret;
9375 }
9376 }
9377 ret = 0;
9378
Chris Mason5a3f23d2009-03-31 13:27:11 -04009379 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009380 * we're using rename to replace one file with another. Start IO on it
9381 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009382 */
Chris Mason8d875f92014-08-12 10:47:42 -07009383 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009384 filemap_flush(old_inode->i_mapping);
9385
Yan, Zheng76dda932009-09-21 16:00:26 -04009386 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009387 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009388 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009389 /*
9390 * We want to reserve the absolute worst case amount of items. So if
9391 * both inodes are subvols and we need to unlink them then that would
9392 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009393 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009394 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9395 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009396 * If our rename has the whiteout flag, we need more 5 units for the
9397 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9398 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009399 */
Filipe Manana5062af32016-05-05 10:26:26 +01009400 trans_num_items = 11;
9401 if (flags & RENAME_WHITEOUT)
9402 trans_num_items += 5;
9403 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009404 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009405 ret = PTR_ERR(trans);
9406 goto out_notrans;
9407 }
Chris Mason5f39d392007-10-15 16:14:19 -04009408
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009409 if (dest != root)
9410 btrfs_record_root_in_trans(trans, dest);
9411
Nikolay Borisov877574e2017-02-20 13:50:33 +02009412 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009413 if (ret)
9414 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009415
Miao Xie67de1172013-12-26 13:07:06 +08009416 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009417 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009418 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009419 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009420 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009421 btrfs_pin_log_trans(root);
9422 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009423 ret = btrfs_insert_inode_ref(trans, dest,
9424 new_dentry->d_name.name,
9425 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009426 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009427 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009428 if (ret)
9429 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009430 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009431
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009432 inode_inc_iversion(old_dir);
9433 inode_inc_iversion(new_dir);
9434 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009435 old_dir->i_ctime = old_dir->i_mtime =
9436 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009437 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009438
Chris Mason12fcfd22009-03-24 10:24:20 -04009439 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009440 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9441 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009442
Li Zefan33345d012011-04-20 10:31:50 +08009443 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009444 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009445 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009446 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9447 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009448 old_dentry->d_name.name,
9449 old_dentry->d_name.len);
9450 if (!ret)
9451 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009452 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009453 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009454 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009455 goto out_fail;
9456 }
Chris Mason39279cc2007-06-12 06:35:45 -04009457
9458 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009459 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009460 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009461 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009462 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009463 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009464 BUG_ON(new_inode->i_nlink == 0);
9465 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009466 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9467 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009468 new_dentry->d_name.name,
9469 new_dentry->d_name.len);
9470 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009471 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009472 ret = btrfs_orphan_add(trans,
9473 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009474 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009475 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009476 goto out_fail;
9477 }
Chris Mason39279cc2007-06-12 06:35:45 -04009478 }
Josef Bacikaec74772008-07-24 12:12:38 -04009479
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009480 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009481 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009482 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009483 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009484 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009485 goto out_fail;
9486 }
Chris Mason39279cc2007-06-12 06:35:45 -04009487
Miao Xie67de1172013-12-26 13:07:06 +08009488 if (old_inode->i_nlink == 1)
9489 BTRFS_I(old_inode)->dir_index = index;
9490
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009491 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009492 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009493
Filipe Mananad4682ba2018-06-11 19:24:28 +01009494 btrfs_init_log_ctx(&ctx, old_inode);
9495 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9496 BTRFS_I(old_dir), parent,
9497 false, &ctx);
9498 if (ret == BTRFS_NEED_LOG_SYNC)
9499 sync_log = true;
9500 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9501 commit_transaction = true;
9502 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009503 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009504 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009505 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009506
9507 if (flags & RENAME_WHITEOUT) {
9508 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9509 old_dentry);
9510
9511 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009512 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009513 goto out_fail;
9514 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009515 }
Chris Mason39279cc2007-06-12 06:35:45 -04009516out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009517 /*
9518 * If we have pinned the log and an error happened, we unpin tasks
9519 * trying to sync the log and force them to fallback to a transaction
9520 * commit if the log currently contains any of the inodes involved in
9521 * this rename operation (to ensure we do not persist a log with an
9522 * inconsistent state for any of these inodes or leading to any
9523 * inconsistencies when replayed). If the transaction was aborted, the
9524 * abortion reason is propagated to userspace when attempting to commit
9525 * the transaction. If the log does not contain any of these inodes, we
9526 * allow the tasks to sync it.
9527 */
9528 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009529 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9530 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9531 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009532 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009533 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009534 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009535
9536 btrfs_end_log_trans(root);
9537 log_pinned = false;
9538 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009539 if (!ret && sync_log) {
9540 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9541 if (ret)
9542 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009543 } else if (sync_log) {
9544 mutex_lock(&root->log_mutex);
9545 list_del(&ctx.list);
9546 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009547 }
9548 if (commit_transaction) {
9549 ret = btrfs_commit_transaction(trans);
9550 } else {
9551 int ret2;
9552
9553 ret2 = btrfs_end_transaction(trans);
9554 ret = ret ? ret : ret2;
9555 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009556out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009557 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009558 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009559
Chris Mason39279cc2007-06-12 06:35:45 -04009560 return ret;
9561}
9562
Miklos Szeredi80ace852014-07-23 15:15:32 +02009563static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9564 struct inode *new_dir, struct dentry *new_dentry,
9565 unsigned int flags)
9566{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009567 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009568 return -EINVAL;
9569
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009570 if (flags & RENAME_EXCHANGE)
9571 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9572 new_dentry);
9573
9574 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009575}
9576
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009577struct btrfs_delalloc_work {
9578 struct inode *inode;
9579 struct completion completion;
9580 struct list_head list;
9581 struct btrfs_work work;
9582};
9583
Miao Xie8ccf6f192012-10-25 09:28:04 +00009584static void btrfs_run_delalloc_work(struct btrfs_work *work)
9585{
9586 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009587 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009588
9589 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9590 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009591 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009592 filemap_flush(inode->i_mapping);
9593 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9594 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009595 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009596
Nikolay Borisov076da912018-04-23 10:54:16 +03009597 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009598 complete(&delalloc_work->completion);
9599}
9600
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009601static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009602{
9603 struct btrfs_delalloc_work *work;
9604
David Sterba100d5702015-12-08 14:39:32 +01009605 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009606 if (!work)
9607 return NULL;
9608
9609 init_completion(&work->completion);
9610 INIT_LIST_HEAD(&work->list);
9611 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009612 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009613
9614 return work;
9615}
9616
Chris Masond352ac62008-09-29 15:18:18 -04009617/*
9618 * some fairly slow code that needs optimization. This walks the list
9619 * of all the inodes with pending delalloc and forces them to disk.
9620 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009621static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009622{
Chris Masonea8c2812008-08-04 23:17:27 -04009623 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009624 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009625 struct btrfs_delalloc_work *work, *next;
9626 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009627 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009628 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009629
Miao Xie8ccf6f192012-10-25 09:28:04 +00009630 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009631 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009632
Miao Xie573bfb72014-03-06 13:55:03 +08009633 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009634 spin_lock(&root->delalloc_lock);
9635 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009636 while (!list_empty(&splice)) {
9637 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009638 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009639
Miao Xieeb73c1b2013-05-15 07:48:22 +00009640 list_move_tail(&binode->delalloc_inodes,
9641 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009642 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009643 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009644 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009645 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009646 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009647 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009648
Ethan Lien3cd24c62018-11-01 14:49:03 +08009649 if (snapshot)
9650 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9651 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009652 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009653 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009654 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009655 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009656 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009657 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009658 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009659 btrfs_queue_work(root->fs_info->flush_workers,
9660 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009661 ret++;
9662 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009663 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009664 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009665 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009666 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009667 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009668
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009669out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009670 list_for_each_entry_safe(work, next, &works, list) {
9671 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009672 wait_for_completion(&work->completion);
9673 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009674 }
9675
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009676 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009677 spin_lock(&root->delalloc_lock);
9678 list_splice_tail(&splice, &root->delalloc_inodes);
9679 spin_unlock(&root->delalloc_lock);
9680 }
Miao Xie573bfb72014-03-06 13:55:03 +08009681 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009682 return ret;
9683}
9684
Ethan Lien3cd24c62018-11-01 14:49:03 +08009685int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009686{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009687 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009688 int ret;
9689
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009690 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009691 return -EROFS;
9692
Ethan Lien3cd24c62018-11-01 14:49:03 +08009693 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009694 if (ret > 0)
9695 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009696 return ret;
9697}
9698
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009699int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009700{
9701 struct btrfs_root *root;
9702 struct list_head splice;
9703 int ret;
9704
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009705 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009706 return -EROFS;
9707
9708 INIT_LIST_HEAD(&splice);
9709
Miao Xie573bfb72014-03-06 13:55:03 +08009710 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009711 spin_lock(&fs_info->delalloc_root_lock);
9712 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009713 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009714 root = list_first_entry(&splice, struct btrfs_root,
9715 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009716 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009717 BUG_ON(!root);
9718 list_move_tail(&root->delalloc_root,
9719 &fs_info->delalloc_roots);
9720 spin_unlock(&fs_info->delalloc_root_lock);
9721
Ethan Lien3cd24c62018-11-01 14:49:03 +08009722 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009723 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009724 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009725 goto out;
9726
Miao Xie6c255e62014-03-06 13:55:01 +08009727 if (nr != -1) {
9728 nr -= ret;
9729 WARN_ON(nr < 0);
9730 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009731 spin_lock(&fs_info->delalloc_root_lock);
9732 }
9733 spin_unlock(&fs_info->delalloc_root_lock);
9734
Miao Xie6c255e62014-03-06 13:55:01 +08009735 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009736out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009737 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009738 spin_lock(&fs_info->delalloc_root_lock);
9739 list_splice_tail(&splice, &fs_info->delalloc_roots);
9740 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009741 }
Miao Xie573bfb72014-03-06 13:55:03 +08009742 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009743 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009744}
9745
Chris Mason39279cc2007-06-12 06:35:45 -04009746static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9747 const char *symname)
9748{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009749 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009750 struct btrfs_trans_handle *trans;
9751 struct btrfs_root *root = BTRFS_I(dir)->root;
9752 struct btrfs_path *path;
9753 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009754 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009755 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009756 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309757 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009758 int name_len;
9759 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009760 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009761 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009762 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009763
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009764 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009765 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009766 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009767
Josef Bacik9ed74f22009-09-11 16:12:44 -04009768 /*
9769 * 2 items for inode item and ref
9770 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009771 * 1 item for updating parent inode item
9772 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009773 * 1 item for xattr if selinux is on
9774 */
Filipe Manana9269d122015-12-31 18:16:29 +00009775 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009776 if (IS_ERR(trans))
9777 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009778
Li Zefan581bb052011-04-20 10:06:11 +08009779 err = btrfs_find_free_ino(root, &objectid);
9780 if (err)
9781 goto out_unlock;
9782
Josef Bacikaec74772008-07-24 12:12:38 -04009783 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009784 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9785 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009786 if (IS_ERR(inode)) {
9787 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009788 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009789 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009790 }
Chris Mason39279cc2007-06-12 06:35:45 -04009791
Casey Schauflerad19db72011-12-15 10:09:07 -05009792 /*
9793 * If the active LSM wants to access the inode during
9794 * d_instantiate it needs these. Smack checks to see
9795 * if the filesystem supports xattrs by looking at the
9796 * ops vector.
9797 */
9798 inode->i_fop = &btrfs_file_operations;
9799 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009800 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009801 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9802
9803 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9804 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009805 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009806
Chris Mason39279cc2007-06-12 06:35:45 -04009807 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009808 if (!path) {
9809 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009810 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009811 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009812 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009813 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009814 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009815 datasize = btrfs_file_extent_calc_inline_size(name_len);
9816 err = btrfs_insert_empty_item(trans, root, path, &key,
9817 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009818 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009819 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009820 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009821 }
Chris Mason5f39d392007-10-15 16:14:19 -04009822 leaf = path->nodes[0];
9823 ei = btrfs_item_ptr(leaf, path->slots[0],
9824 struct btrfs_file_extent_item);
9825 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9826 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009827 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009828 btrfs_set_file_extent_encryption(leaf, ei, 0);
9829 btrfs_set_file_extent_compression(leaf, ei, 0);
9830 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9831 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9832
Chris Mason39279cc2007-06-12 06:35:45 -04009833 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009834 write_extent_buffer(leaf, symname, ptr, name_len);
9835 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009836 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009837
Chris Mason39279cc2007-06-12 06:35:45 -04009838 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009839 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009840 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009841 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009842 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009843 /*
9844 * Last step, add directory indexes for our symlink inode. This is the
9845 * last step to avoid extra cleanup of these indexes if an error happens
9846 * elsewhere above.
9847 */
9848 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009849 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9850 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009851 if (err)
9852 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009853
Al Viro1e2e5472018-05-04 08:23:01 -04009854 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009855
9856out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009857 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009858 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009859 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009860 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009861 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009862 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009863 return err;
9864}
Chris Mason16432982008-04-10 10:23:21 -04009865
Josef Bacik0af3d002010-06-21 14:48:16 -04009866static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9867 u64 start, u64 num_bytes, u64 min_size,
9868 loff_t actual_len, u64 *alloc_hint,
9869 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009870{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009871 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009872 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9873 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009874 struct btrfs_root *root = BTRFS_I(inode)->root;
9875 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009876 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009877 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009878 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009879 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009880 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009881 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009882 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009883 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009884
Josef Bacik0af3d002010-06-21 14:48:16 -04009885 if (trans)
9886 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009887 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009888 if (own_trans) {
9889 trans = btrfs_start_transaction(root, 3);
9890 if (IS_ERR(trans)) {
9891 ret = PTR_ERR(trans);
9892 break;
9893 }
Yan Zhengd899e052008-10-30 14:25:28 -04009894 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009895
Byongho Leeee221842015-12-15 01:42:10 +09009896 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009897 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009898 /*
9899 * If we are severely fragmented we could end up with really
9900 * small allocations, so if the allocator is returning small
9901 * chunks lets make its job easier by only searching for those
9902 * sized chunks.
9903 */
9904 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009905 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9906 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009907 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009908 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009909 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009910 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009911 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009912
9913 /*
9914 * We've reserved this space, and thus converted it from
9915 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9916 * from here on out we will only need to clear our reservation
9917 * for the remaining unreserved area, so advance our
9918 * clear_offset by our extent size.
9919 */
9920 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009921 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009922
Josef Bacik0b670dc2015-09-23 17:11:16 -04009923 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04009924 ret = insert_reserved_file_extent(trans, inode,
9925 cur_offset, ins.objectid,
9926 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009927 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009928 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009929 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009930 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009931 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009932 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009933 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009934 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009935 break;
9936 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009937
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009938 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009939 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009940
Josef Bacik5dc562c2012-08-17 13:14:17 -04009941 em = alloc_extent_map();
9942 if (!em) {
9943 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9944 &BTRFS_I(inode)->runtime_flags);
9945 goto next;
9946 }
9947
9948 em->start = cur_offset;
9949 em->orig_start = cur_offset;
9950 em->len = ins.offset;
9951 em->block_start = ins.objectid;
9952 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009953 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009954 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009955 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9956 em->generation = trans->transid;
9957
9958 while (1) {
9959 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009960 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009961 write_unlock(&em_tree->lock);
9962 if (ret != -EEXIST)
9963 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009964 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009965 cur_offset + ins.offset - 1,
9966 0);
9967 }
9968 free_extent_map(em);
9969next:
Yan Zhengd899e052008-10-30 14:25:28 -04009970 num_bytes -= ins.offset;
9971 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009972 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009973
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009974 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009975 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009976 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009977 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009978 (actual_len > inode->i_size) &&
9979 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009980 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009981 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009982 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009983 i_size = cur_offset;
9984 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009985 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009986 }
9987
Yan Zhengd899e052008-10-30 14:25:28 -04009988 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009989
9990 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009991 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009992 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009993 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009994 break;
9995 }
Yan Zhengd899e052008-10-30 14:25:28 -04009996
Josef Bacik0af3d002010-06-21 14:48:16 -04009997 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009998 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009999 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010000 if (clear_offset < end)
10001 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
10002 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010003 return ret;
10004}
10005
Josef Bacik0af3d002010-06-21 14:48:16 -040010006int btrfs_prealloc_file_range(struct inode *inode, int mode,
10007 u64 start, u64 num_bytes, u64 min_size,
10008 loff_t actual_len, u64 *alloc_hint)
10009{
10010 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10011 min_size, actual_len, alloc_hint,
10012 NULL);
10013}
10014
10015int btrfs_prealloc_file_range_trans(struct inode *inode,
10016 struct btrfs_trans_handle *trans, int mode,
10017 u64 start, u64 num_bytes, u64 min_size,
10018 loff_t actual_len, u64 *alloc_hint)
10019{
10020 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10021 min_size, actual_len, alloc_hint, trans);
10022}
10023
Chris Masone6dcd2d2008-07-17 12:53:50 -040010024static int btrfs_set_page_dirty(struct page *page)
10025{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010026 return __set_page_dirty_nobuffers(page);
10027}
10028
Al Viro10556cb22011-06-20 19:28:19 -040010029static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010030{
Li Zefanb83cc962010-12-20 16:04:08 +080010031 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010032 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010033
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010034 if (mask & MAY_WRITE &&
10035 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10036 if (btrfs_root_readonly(root))
10037 return -EROFS;
10038 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10039 return -EACCES;
10040 }
Al Viro2830ba72011-06-20 19:16:29 -040010041 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010042}
Chris Mason39279cc2007-06-12 06:35:45 -040010043
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010044static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10045{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010046 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010047 struct btrfs_trans_handle *trans;
10048 struct btrfs_root *root = BTRFS_I(dir)->root;
10049 struct inode *inode = NULL;
10050 u64 objectid;
10051 u64 index;
10052 int ret = 0;
10053
10054 /*
10055 * 5 units required for adding orphan entry
10056 */
10057 trans = btrfs_start_transaction(root, 5);
10058 if (IS_ERR(trans))
10059 return PTR_ERR(trans);
10060
10061 ret = btrfs_find_free_ino(root, &objectid);
10062 if (ret)
10063 goto out;
10064
10065 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010066 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010067 if (IS_ERR(inode)) {
10068 ret = PTR_ERR(inode);
10069 inode = NULL;
10070 goto out;
10071 }
10072
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010073 inode->i_fop = &btrfs_file_operations;
10074 inode->i_op = &btrfs_file_inode_operations;
10075
10076 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010077 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10078
Chris Masonb0d5d102014-09-08 13:08:51 -070010079 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10080 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010081 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010082
10083 ret = btrfs_update_inode(trans, root, inode);
10084 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010085 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010086 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010087 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010088 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010089
Filipe Manana5762b5c2014-08-01 00:10:32 +010010090 /*
10091 * We set number of links to 0 in btrfs_new_inode(), and here we set
10092 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10093 * through:
10094 *
10095 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10096 */
10097 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010098 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010099 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010100 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010101out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010102 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010103 if (ret && inode)
10104 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010105 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010106 return ret;
10107}
10108
David Sterba5cdc84b2018-07-18 20:32:52 +020010109void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010110{
David Sterba5cdc84b2018-07-18 20:32:52 +020010111 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010112 unsigned long index = start >> PAGE_SHIFT;
10113 unsigned long end_index = end >> PAGE_SHIFT;
10114 struct page *page;
10115
10116 while (index <= end_index) {
10117 page = find_get_page(inode->i_mapping, index);
10118 ASSERT(page); /* Pages should be in the extent_io_tree */
10119 set_page_writeback(page);
10120 put_page(page);
10121 index++;
10122 }
10123}
10124
Omar Sandovaled46ff32016-11-03 10:28:14 -070010125#ifdef CONFIG_SWAP
10126/*
10127 * Add an entry indicating a block group or device which is pinned by a
10128 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10129 * negative errno on failure.
10130 */
10131static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10132 bool is_block_group)
10133{
10134 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10135 struct btrfs_swapfile_pin *sp, *entry;
10136 struct rb_node **p;
10137 struct rb_node *parent = NULL;
10138
10139 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10140 if (!sp)
10141 return -ENOMEM;
10142 sp->ptr = ptr;
10143 sp->inode = inode;
10144 sp->is_block_group = is_block_group;
10145
10146 spin_lock(&fs_info->swapfile_pins_lock);
10147 p = &fs_info->swapfile_pins.rb_node;
10148 while (*p) {
10149 parent = *p;
10150 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10151 if (sp->ptr < entry->ptr ||
10152 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10153 p = &(*p)->rb_left;
10154 } else if (sp->ptr > entry->ptr ||
10155 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10156 p = &(*p)->rb_right;
10157 } else {
10158 spin_unlock(&fs_info->swapfile_pins_lock);
10159 kfree(sp);
10160 return 1;
10161 }
10162 }
10163 rb_link_node(&sp->node, parent, p);
10164 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10165 spin_unlock(&fs_info->swapfile_pins_lock);
10166 return 0;
10167}
10168
10169/* Free all of the entries pinned by this swapfile. */
10170static void btrfs_free_swapfile_pins(struct inode *inode)
10171{
10172 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10173 struct btrfs_swapfile_pin *sp;
10174 struct rb_node *node, *next;
10175
10176 spin_lock(&fs_info->swapfile_pins_lock);
10177 node = rb_first(&fs_info->swapfile_pins);
10178 while (node) {
10179 next = rb_next(node);
10180 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10181 if (sp->inode == inode) {
10182 rb_erase(&sp->node, &fs_info->swapfile_pins);
10183 if (sp->is_block_group)
10184 btrfs_put_block_group(sp->ptr);
10185 kfree(sp);
10186 }
10187 node = next;
10188 }
10189 spin_unlock(&fs_info->swapfile_pins_lock);
10190}
10191
10192struct btrfs_swap_info {
10193 u64 start;
10194 u64 block_start;
10195 u64 block_len;
10196 u64 lowest_ppage;
10197 u64 highest_ppage;
10198 unsigned long nr_pages;
10199 int nr_extents;
10200};
10201
10202static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10203 struct btrfs_swap_info *bsi)
10204{
10205 unsigned long nr_pages;
10206 u64 first_ppage, first_ppage_reported, next_ppage;
10207 int ret;
10208
10209 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10210 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10211 PAGE_SIZE) >> PAGE_SHIFT;
10212
10213 if (first_ppage >= next_ppage)
10214 return 0;
10215 nr_pages = next_ppage - first_ppage;
10216
10217 first_ppage_reported = first_ppage;
10218 if (bsi->start == 0)
10219 first_ppage_reported++;
10220 if (bsi->lowest_ppage > first_ppage_reported)
10221 bsi->lowest_ppage = first_ppage_reported;
10222 if (bsi->highest_ppage < (next_ppage - 1))
10223 bsi->highest_ppage = next_ppage - 1;
10224
10225 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10226 if (ret < 0)
10227 return ret;
10228 bsi->nr_extents += ret;
10229 bsi->nr_pages += nr_pages;
10230 return 0;
10231}
10232
10233static void btrfs_swap_deactivate(struct file *file)
10234{
10235 struct inode *inode = file_inode(file);
10236
10237 btrfs_free_swapfile_pins(inode);
10238 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10239}
10240
10241static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10242 sector_t *span)
10243{
10244 struct inode *inode = file_inode(file);
10245 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10246 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10247 struct extent_state *cached_state = NULL;
10248 struct extent_map *em = NULL;
10249 struct btrfs_device *device = NULL;
10250 struct btrfs_swap_info bsi = {
10251 .lowest_ppage = (sector_t)-1ULL,
10252 };
10253 int ret = 0;
10254 u64 isize;
10255 u64 start;
10256
10257 /*
10258 * If the swap file was just created, make sure delalloc is done. If the
10259 * file changes again after this, the user is doing something stupid and
10260 * we don't really care.
10261 */
10262 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10263 if (ret)
10264 return ret;
10265
10266 /*
10267 * The inode is locked, so these flags won't change after we check them.
10268 */
10269 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10270 btrfs_warn(fs_info, "swapfile must not be compressed");
10271 return -EINVAL;
10272 }
10273 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10274 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10275 return -EINVAL;
10276 }
10277 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10278 btrfs_warn(fs_info, "swapfile must not be checksummed");
10279 return -EINVAL;
10280 }
10281
10282 /*
10283 * Balance or device remove/replace/resize can move stuff around from
10284 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10285 * concurrently while we are mapping the swap extents, and
10286 * fs_info->swapfile_pins prevents them from running while the swap file
10287 * is active and moving the extents. Note that this also prevents a
10288 * concurrent device add which isn't actually necessary, but it's not
10289 * really worth the trouble to allow it.
10290 */
10291 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10292 btrfs_warn(fs_info,
10293 "cannot activate swapfile while exclusive operation is running");
10294 return -EBUSY;
10295 }
10296 /*
10297 * Snapshots can create extents which require COW even if NODATACOW is
10298 * set. We use this counter to prevent snapshots. We must increment it
10299 * before walking the extents because we don't want a concurrent
10300 * snapshot to run after we've already checked the extents.
10301 */
10302 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10303
10304 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10305
10306 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10307 start = 0;
10308 while (start < isize) {
10309 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010310 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010311 u64 len = isize - start;
10312
Omar Sandoval39b07b52019-12-02 17:34:23 -080010313 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010314 if (IS_ERR(em)) {
10315 ret = PTR_ERR(em);
10316 goto out;
10317 }
10318
10319 if (em->block_start == EXTENT_MAP_HOLE) {
10320 btrfs_warn(fs_info, "swapfile must not have holes");
10321 ret = -EINVAL;
10322 goto out;
10323 }
10324 if (em->block_start == EXTENT_MAP_INLINE) {
10325 /*
10326 * It's unlikely we'll ever actually find ourselves
10327 * here, as a file small enough to fit inline won't be
10328 * big enough to store more than the swap header, but in
10329 * case something changes in the future, let's catch it
10330 * here rather than later.
10331 */
10332 btrfs_warn(fs_info, "swapfile must not be inline");
10333 ret = -EINVAL;
10334 goto out;
10335 }
10336 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10337 btrfs_warn(fs_info, "swapfile must not be compressed");
10338 ret = -EINVAL;
10339 goto out;
10340 }
10341
10342 logical_block_start = em->block_start + (start - em->start);
10343 len = min(len, em->len - (start - em->start));
10344 free_extent_map(em);
10345 em = NULL;
10346
10347 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10348 if (ret < 0) {
10349 goto out;
10350 } else if (ret) {
10351 ret = 0;
10352 } else {
10353 btrfs_warn(fs_info,
10354 "swapfile must not be copy-on-write");
10355 ret = -EINVAL;
10356 goto out;
10357 }
10358
10359 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10360 if (IS_ERR(em)) {
10361 ret = PTR_ERR(em);
10362 goto out;
10363 }
10364
10365 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10366 btrfs_warn(fs_info,
10367 "swapfile must have single data profile");
10368 ret = -EINVAL;
10369 goto out;
10370 }
10371
10372 if (device == NULL) {
10373 device = em->map_lookup->stripes[0].dev;
10374 ret = btrfs_add_swapfile_pin(inode, device, false);
10375 if (ret == 1)
10376 ret = 0;
10377 else if (ret)
10378 goto out;
10379 } else if (device != em->map_lookup->stripes[0].dev) {
10380 btrfs_warn(fs_info, "swapfile must be on one device");
10381 ret = -EINVAL;
10382 goto out;
10383 }
10384
10385 physical_block_start = (em->map_lookup->stripes[0].physical +
10386 (logical_block_start - em->start));
10387 len = min(len, em->len - (logical_block_start - em->start));
10388 free_extent_map(em);
10389 em = NULL;
10390
10391 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10392 if (!bg) {
10393 btrfs_warn(fs_info,
10394 "could not find block group containing swapfile");
10395 ret = -EINVAL;
10396 goto out;
10397 }
10398
10399 ret = btrfs_add_swapfile_pin(inode, bg, true);
10400 if (ret) {
10401 btrfs_put_block_group(bg);
10402 if (ret == 1)
10403 ret = 0;
10404 else
10405 goto out;
10406 }
10407
10408 if (bsi.block_len &&
10409 bsi.block_start + bsi.block_len == physical_block_start) {
10410 bsi.block_len += len;
10411 } else {
10412 if (bsi.block_len) {
10413 ret = btrfs_add_swap_extent(sis, &bsi);
10414 if (ret)
10415 goto out;
10416 }
10417 bsi.start = start;
10418 bsi.block_start = physical_block_start;
10419 bsi.block_len = len;
10420 }
10421
10422 start += len;
10423 }
10424
10425 if (bsi.block_len)
10426 ret = btrfs_add_swap_extent(sis, &bsi);
10427
10428out:
10429 if (!IS_ERR_OR_NULL(em))
10430 free_extent_map(em);
10431
10432 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10433
10434 if (ret)
10435 btrfs_swap_deactivate(file);
10436
10437 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10438
10439 if (ret)
10440 return ret;
10441
10442 if (device)
10443 sis->bdev = device->bdev;
10444 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10445 sis->max = bsi.nr_pages;
10446 sis->pages = bsi.nr_pages - 1;
10447 sis->highest_bit = bsi.nr_pages - 1;
10448 return bsi.nr_extents;
10449}
10450#else
10451static void btrfs_swap_deactivate(struct file *file)
10452{
10453}
10454
10455static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10456 sector_t *span)
10457{
10458 return -EOPNOTSUPP;
10459}
10460#endif
10461
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010462static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010463 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010464 .lookup = btrfs_lookup,
10465 .create = btrfs_create,
10466 .unlink = btrfs_unlink,
10467 .link = btrfs_link,
10468 .mkdir = btrfs_mkdir,
10469 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010470 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010471 .symlink = btrfs_symlink,
10472 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010473 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010474 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010475 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010476 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010477 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010478 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010479 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010480};
Yan, Zheng76dda932009-09-21 16:00:26 -040010481
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010482static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010483 .llseek = generic_file_llseek,
10484 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010485 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010486 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010487 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010488#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010489 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010490#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010491 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010492 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010493};
10494
David Sterba20e55062015-11-19 11:42:28 +010010495static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010496 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010497 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010498 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10499};
10500
Chris Mason35054392009-01-21 13:11:13 -050010501/*
10502 * btrfs doesn't support the bmap operation because swapfiles
10503 * use bmap to make a mapping of extents in the file. They assume
10504 * these extents won't change over the life of the file and they
10505 * use the bmap result to do IO directly to the drive.
10506 *
10507 * the btrfs bmap call would return logical addresses that aren't
10508 * suitable for IO and they also will change frequently as COW
10509 * operations happen. So, swapfile + btrfs == corruption.
10510 *
10511 * For now we're avoiding this by dropping bmap.
10512 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010513static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010514 .readpage = btrfs_readpage,
10515 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010516 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010517 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010518 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010519 .invalidatepage = btrfs_invalidatepage,
10520 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010521#ifdef CONFIG_MIGRATION
10522 .migratepage = btrfs_migratepage,
10523#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010524 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010525 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010526 .swap_activate = btrfs_swap_activate,
10527 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010528};
10529
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010530static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010531 .getattr = btrfs_getattr,
10532 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010533 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010534 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010535 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010536 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010537 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010538 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010539};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010540static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010541 .getattr = btrfs_getattr,
10542 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010543 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010544 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010545 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010546 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010547 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010548};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010549static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010550 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010551 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010552 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010553 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010554 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010555 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010556};
Yan, Zheng76dda932009-09-21 16:00:26 -040010557
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010558const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010559 .d_delete = btrfs_dentry_delete,
10560};