blob: 08c02bd148a6961e440a98a649924d8773b0aa50 [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 Masonb888db2b2007-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 Masonb888db2b2007-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 Masonb888db2b2007-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 Masonb888db2b2007-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
Eric Biggersfd080012020-04-30 23:51:59 -07002745 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002746
2747 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002748 goto zeroit;
2749
2750 kunmap_atomic(kaddr);
2751 return 0;
2752zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002753 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2754 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002755 memset(kaddr + pgoff, 1, len);
2756 flush_dcache_page(page);
2757 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002758 return -EIO;
2759}
2760
Chris Masond352ac62008-09-29 15:18:18 -04002761/*
Chris Masond352ac62008-09-29 15:18:18 -04002762 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002763 * if there's a match, we allow the bio to finish. If not, the code in
2764 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002765 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002766static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2767 u64 phy_offset, struct page *page,
2768 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002769{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002770 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002771 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002772 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002773 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002774
Chris Masond20f7042008-12-08 16:58:54 -05002775 if (PageChecked(page)) {
2776 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002777 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002778 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002779
2780 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002781 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002782
Yan Zheng17d217f2008-12-12 10:03:38 -05002783 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002784 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002785 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002786 return 0;
2787 }
2788
Miao Xiefacc8a222013-07-25 19:22:34 +08002789 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002790 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2791 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002792}
Chris Masonb888db2b2007-08-27 16:49:44 -04002793
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002794/*
2795 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2796 *
2797 * @inode: The inode we want to perform iput on
2798 *
2799 * This function uses the generic vfs_inode::i_count to track whether we should
2800 * just decrement it (in case it's > 1) or if this is the last iput then link
2801 * the inode to the delayed iput machinery. Delayed iputs are processed at
2802 * transaction commit time/superblock commit/cleaner kthread.
2803 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002804void btrfs_add_delayed_iput(struct inode *inode)
2805{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002806 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002807 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002808
2809 if (atomic_add_unless(&inode->i_count, -1, 1))
2810 return;
2811
Josef Bacik034f7842018-12-03 11:06:52 -05002812 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002813 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002814 ASSERT(list_empty(&binode->delayed_iput));
2815 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002816 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002817 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2818 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002819}
2820
Josef Bacik63611e72019-06-18 10:59:18 -04002821static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2822 struct btrfs_inode *inode)
2823{
2824 list_del_init(&inode->delayed_iput);
2825 spin_unlock(&fs_info->delayed_iput_lock);
2826 iput(&inode->vfs_inode);
2827 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2828 wake_up(&fs_info->delayed_iputs_wait);
2829 spin_lock(&fs_info->delayed_iput_lock);
2830}
2831
2832static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2833 struct btrfs_inode *inode)
2834{
2835 if (!list_empty(&inode->delayed_iput)) {
2836 spin_lock(&fs_info->delayed_iput_lock);
2837 if (!list_empty(&inode->delayed_iput))
2838 run_delayed_iput_locked(fs_info, inode);
2839 spin_unlock(&fs_info->delayed_iput_lock);
2840 }
2841}
2842
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002843void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002844{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002845
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002846 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002847 while (!list_empty(&fs_info->delayed_iputs)) {
2848 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002849
David Sterba8089fe62015-11-19 14:15:51 +01002850 inode = list_first_entry(&fs_info->delayed_iputs,
2851 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002852 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002853 }
David Sterba8089fe62015-11-19 14:15:51 +01002854 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002855}
2856
Josef Bacik034f7842018-12-03 11:06:52 -05002857/**
2858 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2859 * @fs_info - the fs_info for this fs
2860 * @return - EINTR if we were killed, 0 if nothing's pending
2861 *
2862 * This will wait on any delayed iputs that are currently running with KILLABLE
2863 * set. Once they are all done running we will return, unless we are killed in
2864 * which case we return EINTR. This helps in user operations like fallocate etc
2865 * that might get blocked on the iputs.
2866 */
2867int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2868{
2869 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2870 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2871 if (ret)
2872 return -EINTR;
2873 return 0;
2874}
2875
Yan, Zhengd68fc572010-05-16 10:49:58 -04002876/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002877 * This creates an orphan entry for the given inode in case something goes wrong
2878 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002879 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002880int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002881 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002882{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002883 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002884
Omar Sandoval27919062018-05-11 13:13:37 -07002885 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2886 if (ret && ret != -EEXIST) {
2887 btrfs_abort_transaction(trans, ret);
2888 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002889 }
2890
Yan, Zhengd68fc572010-05-16 10:49:58 -04002891 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002892}
2893
2894/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002895 * We have done the delete so we can go ahead and remove the orphan item for
2896 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002897 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002898static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002899 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002900{
Omar Sandoval27919062018-05-11 13:13:37 -07002901 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002902}
2903
2904/*
2905 * this cleans up any orphans that may be left on the list from the last use
2906 * of this root.
2907 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002908int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002909{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002910 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002911 struct btrfs_path *path;
2912 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002913 struct btrfs_key key, found_key;
2914 struct btrfs_trans_handle *trans;
2915 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002916 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002917 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002918
Yan, Zhengd68fc572010-05-16 10:49:58 -04002919 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002920 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002921
2922 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002923 if (!path) {
2924 ret = -ENOMEM;
2925 goto out;
2926 }
David Sterbae4058b52015-11-27 16:31:35 +01002927 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002928
2929 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002930 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04002931 key.offset = (u64)-1;
2932
Josef Bacik7b128762008-07-24 12:17:14 -04002933 while (1) {
2934 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002935 if (ret < 0)
2936 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002937
2938 /*
2939 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002940 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002941 * find the key and see if we have stuff that matches
2942 */
2943 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002944 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002945 if (path->slots[0] == 0)
2946 break;
2947 path->slots[0]--;
2948 }
2949
2950 /* pull out the item */
2951 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002952 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2953
2954 /* make sure the item matches what we want */
2955 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2956 break;
David Sterba962a2982014-06-04 18:41:45 +02002957 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04002958 break;
2959
2960 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002961 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002962
2963 /*
2964 * this is where we are basically btrfs_lookup, without the
2965 * crossing root thing. we store the inode number in the
2966 * offset of the orphan item.
2967 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002968
2969 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002970 btrfs_err(fs_info,
2971 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002972 ret = -EINVAL;
2973 goto out;
2974 }
2975
2976 last_objectid = found_key.offset;
2977
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002978 found_key.objectid = found_key.offset;
2979 found_key.type = BTRFS_INODE_ITEM_KEY;
2980 found_key.offset = 0;
David Sterba4c66e0d2019-10-03 19:09:35 +02002981 inode = btrfs_iget(fs_info->sb, &found_key, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09302982 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01002983 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002984 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002985
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002986 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002987 struct btrfs_root *dead_root;
2988 struct btrfs_fs_info *fs_info = root->fs_info;
2989 int is_dead_root = 0;
2990
2991 /*
2992 * this is an orphan in the tree root. Currently these
2993 * could come from 2 sources:
2994 * a) a snapshot deletion in progress
2995 * b) a free space cache inode
2996 * We need to distinguish those two, as the snapshot
2997 * orphan must not get deleted.
2998 * find_dead_roots already ran before us, so if this
2999 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003000 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003001 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003002
3003 spin_lock(&fs_info->fs_roots_radix_lock);
3004 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3005 (unsigned long)found_key.objectid);
3006 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3007 is_dead_root = 1;
3008 spin_unlock(&fs_info->fs_roots_radix_lock);
3009
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003010 if (is_dead_root) {
3011 /* prevent this orphan from being found again */
3012 key.offset = found_key.objectid - 1;
3013 continue;
3014 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003015
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003016 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003017
Josef Bacika8c9e572011-09-21 16:55:59 -04003018 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003019 * If we have an inode with links, there are a couple of
3020 * possibilities. Old kernels (before v3.12) used to create an
3021 * orphan item for truncate indicating that there were possibly
3022 * extent items past i_size that needed to be deleted. In v3.12,
3023 * truncate was changed to update i_size in sync with the extent
3024 * items, but the (useless) orphan item was still created. Since
3025 * v4.18, we don't create the orphan item for truncate at all.
3026 *
3027 * So, this item could mean that we need to do a truncate, but
3028 * only if this filesystem was last used on a pre-v3.12 kernel
3029 * and was not cleanly unmounted. The odds of that are quite
3030 * slim, and it's a pain to do the truncate now, so just delete
3031 * the orphan item.
3032 *
3033 * It's also possible that this orphan item was supposed to be
3034 * deleted but wasn't. The inode number may have been reused,
3035 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003036 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003037 if (ret == -ENOENT || inode->i_nlink) {
3038 if (!ret)
3039 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003040 trans = btrfs_start_transaction(root, 1);
3041 if (IS_ERR(trans)) {
3042 ret = PTR_ERR(trans);
3043 goto out;
3044 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003045 btrfs_debug(fs_info, "auto deleting %Lu",
3046 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003047 ret = btrfs_del_orphan_item(trans, root,
3048 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003049 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003050 if (ret)
3051 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003052 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003053 }
Josef Bacik7b128762008-07-24 12:17:14 -04003054
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003055 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003056
3057 /* this will do delete_inode and everything for us */
3058 iput(inode);
3059 }
Miao Xie3254c872011-11-10 20:45:05 -05003060 /* release the path since we're done with it */
3061 btrfs_release_path(path);
3062
Yan, Zhengd68fc572010-05-16 10:49:58 -04003063 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3064
Omar Sandovala575cee2018-05-11 13:13:38 -07003065 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003066 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003067 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003068 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003069 }
Josef Bacik7b128762008-07-24 12:17:14 -04003070
3071 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003072 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003073
3074out:
3075 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003076 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003077 btrfs_free_path(path);
3078 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003079}
3080
Chris Masond352ac62008-09-29 15:18:18 -04003081/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003082 * very simple check to peek ahead in the leaf looking for xattrs. If we
3083 * don't find any xattrs, we know there can't be any acls.
3084 *
3085 * slot is the slot the inode is in, objectid is the objectid of the inode
3086 */
3087static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003088 int slot, u64 objectid,
3089 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003090{
3091 u32 nritems = btrfs_header_nritems(leaf);
3092 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003093 static u64 xattr_access = 0;
3094 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003095 int scanned = 0;
3096
Josef Bacikf23b5a52013-06-19 10:16:26 -04003097 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003098 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3099 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3100 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3101 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003102 }
3103
Chris Mason46a53cc2009-04-27 11:47:50 -04003104 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003105 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003106 while (slot < nritems) {
3107 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3108
3109 /* we found a different objectid, there must not be acls */
3110 if (found_key.objectid != objectid)
3111 return 0;
3112
3113 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003114 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003115 if (*first_xattr_slot == -1)
3116 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003117 if (found_key.offset == xattr_access ||
3118 found_key.offset == xattr_default)
3119 return 1;
3120 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003121
3122 /*
3123 * we found a key greater than an xattr key, there can't
3124 * be any acls later on
3125 */
3126 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3127 return 0;
3128
3129 slot++;
3130 scanned++;
3131
3132 /*
3133 * it goes inode, inode backrefs, xattrs, extents,
3134 * so if there are a ton of hard links to an inode there can
3135 * be a lot of backrefs. Don't waste time searching too hard,
3136 * this is just an optimization
3137 */
3138 if (scanned >= 8)
3139 break;
3140 }
3141 /* we hit the end of the leaf before we found an xattr or
3142 * something larger than an xattr. We have to assume the inode
3143 * has acls
3144 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003145 if (*first_xattr_slot == -1)
3146 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003147 return 1;
3148}
3149
3150/*
Chris Masond352ac62008-09-29 15:18:18 -04003151 * read an inode from the btree into the in-memory inode
3152 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003153static int btrfs_read_locked_inode(struct inode *inode,
3154 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003155{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003156 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003157 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003158 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003159 struct btrfs_inode_item *inode_item;
3160 struct btrfs_root *root = BTRFS_I(inode)->root;
3161 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003162 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003163 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003164 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003165 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003166 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003167 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003168
3169 ret = btrfs_fill_inode(inode, &rdev);
3170 if (!ret)
3171 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003172
Filipe Manana4222ea72018-10-24 10:13:03 +01003173 if (!path) {
3174 path = btrfs_alloc_path();
3175 if (!path)
3176 return -ENOMEM;
3177 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003178
Chris Mason39279cc2007-06-12 06:35:45 -04003179 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003180
Chris Mason39279cc2007-06-12 06:35:45 -04003181 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003182 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003183 if (path != in_path)
3184 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003185 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003186 }
Chris Mason39279cc2007-06-12 06:35:45 -04003187
Chris Mason5f39d392007-10-15 16:14:19 -04003188 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003189
3190 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003191 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003192
Chris Mason5f39d392007-10-15 16:14:19 -04003193 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3194 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003195 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003196 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003197 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3198 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003199 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003200 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3201 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003202
David Sterbaa937b972014-12-12 17:39:12 +01003203 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3204 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003205
David Sterbaa937b972014-12-12 17:39:12 +01003206 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3207 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003208
David Sterbaa937b972014-12-12 17:39:12 +01003209 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3210 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003211
chandan r9cc97d62012-07-04 12:48:07 +05303212 BTRFS_I(inode)->i_otime.tv_sec =
3213 btrfs_timespec_sec(leaf, &inode_item->otime);
3214 BTRFS_I(inode)->i_otime.tv_nsec =
3215 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003216
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003217 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003218 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003219 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3220
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003221 inode_set_iversion_queried(inode,
3222 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003223 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003224 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003225 rdev = btrfs_inode_rdev(leaf, inode_item);
3226
Josef Bacikaec74772008-07-24 12:12:38 -04003227 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003228 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003229
3230cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003231 /*
3232 * If we were modified in the current generation and evicted from memory
3233 * and then re-read we need to do a full sync since we don't have any
3234 * idea about which extents were modified before we were evicted from
3235 * cache.
3236 *
3237 * This is required for both inode re-read from disk and delayed inode
3238 * in delayed_nodes_tree.
3239 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003240 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003241 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3242 &BTRFS_I(inode)->runtime_flags);
3243
Filipe Mananabde6c242015-07-24 00:00:19 +01003244 /*
3245 * We don't persist the id of the transaction where an unlink operation
3246 * against the inode was last made. So here we assume the inode might
3247 * have been evicted, and therefore the exact value of last_unlink_trans
3248 * lost, and set it to last_trans to avoid metadata inconsistencies
3249 * between the inode and its parent if the inode is fsync'ed and the log
3250 * replayed. For example, in the scenario:
3251 *
3252 * touch mydir/foo
3253 * ln mydir/foo mydir/bar
3254 * sync
3255 * unlink mydir/bar
3256 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3257 * xfs_io -c fsync mydir/foo
3258 * <power failure>
3259 * mount fs, triggers fsync log replay
3260 *
3261 * We must make sure that when we fsync our inode foo we also log its
3262 * parent inode, otherwise after log replay the parent still has the
3263 * dentry with the "bar" name but our inode foo has a link count of 1
3264 * and doesn't have an inode ref with the name "bar" anymore.
3265 *
3266 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003267 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003268 * transaction commits on fsync if our inode is a directory, or if our
3269 * inode is not a directory, logging its parent unnecessarily.
3270 */
3271 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3272
Miao Xie67de1172013-12-26 13:07:06 +08003273 path->slots[0]++;
3274 if (inode->i_nlink != 1 ||
3275 path->slots[0] >= btrfs_header_nritems(leaf))
3276 goto cache_acl;
3277
3278 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003279 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003280 goto cache_acl;
3281
3282 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3283 if (location.type == BTRFS_INODE_REF_KEY) {
3284 struct btrfs_inode_ref *ref;
3285
3286 ref = (struct btrfs_inode_ref *)ptr;
3287 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3288 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3289 struct btrfs_inode_extref *extref;
3290
3291 extref = (struct btrfs_inode_extref *)ptr;
3292 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3293 extref);
3294 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003295cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003296 /*
3297 * try to precache a NULL acl entry for files that don't have
3298 * any xattrs or acls
3299 */
Li Zefan33345d012011-04-20 10:31:50 +08003300 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003301 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003302 if (first_xattr_slot != -1) {
3303 path->slots[0] = first_xattr_slot;
3304 ret = btrfs_load_inode_props(inode, path);
3305 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003306 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003307 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003308 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003309 root->root_key.objectid, ret);
3310 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003311 if (path != in_path)
3312 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003313
Al Viro72c04902009-06-24 16:58:48 -04003314 if (!maybe_acls)
3315 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003316
Chris Mason39279cc2007-06-12 06:35:45 -04003317 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003318 case S_IFREG:
3319 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003320 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003321 inode->i_fop = &btrfs_file_operations;
3322 inode->i_op = &btrfs_file_inode_operations;
3323 break;
3324 case S_IFDIR:
3325 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003326 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003327 break;
3328 case S_IFLNK:
3329 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003330 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003331 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003332 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003333 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003334 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003335 init_special_inode(inode, inode->i_mode, rdev);
3336 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003337 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003338
David Sterba7b6a2212018-03-26 18:40:21 +02003339 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003340 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003341}
3342
Chris Masond352ac62008-09-29 15:18:18 -04003343/*
3344 * given a leaf and an inode, copy the inode fields into the leaf
3345 */
Chris Masone02119d2008-09-05 16:13:11 -04003346static void fill_inode_item(struct btrfs_trans_handle *trans,
3347 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003348 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003349 struct inode *inode)
3350{
Liu Bo51fab692012-12-27 09:01:21 +00003351 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003352
David Sterbac82f8232019-08-09 17:48:21 +02003353 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003354
David Sterbacc4c13d2020-04-29 02:15:56 +02003355 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3356 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3357 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3358 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3359 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003360
David Sterbacc4c13d2020-04-29 02:15:56 +02003361 btrfs_set_token_timespec_sec(&token, &item->atime,
3362 inode->i_atime.tv_sec);
3363 btrfs_set_token_timespec_nsec(&token, &item->atime,
3364 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003365
David Sterbacc4c13d2020-04-29 02:15:56 +02003366 btrfs_set_token_timespec_sec(&token, &item->mtime,
3367 inode->i_mtime.tv_sec);
3368 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3369 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003370
David Sterbacc4c13d2020-04-29 02:15:56 +02003371 btrfs_set_token_timespec_sec(&token, &item->ctime,
3372 inode->i_ctime.tv_sec);
3373 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3374 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003375
David Sterbacc4c13d2020-04-29 02:15:56 +02003376 btrfs_set_token_timespec_sec(&token, &item->otime,
3377 BTRFS_I(inode)->i_otime.tv_sec);
3378 btrfs_set_token_timespec_nsec(&token, &item->otime,
3379 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303380
David Sterbacc4c13d2020-04-29 02:15:56 +02003381 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3382 btrfs_set_token_inode_generation(&token, item,
3383 BTRFS_I(inode)->generation);
3384 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3385 btrfs_set_token_inode_transid(&token, item, trans->transid);
3386 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3387 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3388 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003389}
3390
Chris Masond352ac62008-09-29 15:18:18 -04003391/*
3392 * copy everything in the in-memory inode into the btree.
3393 */
Chris Mason21151332011-11-10 20:39:08 -05003394static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003395 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003396{
3397 struct btrfs_inode_item *inode_item;
3398 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003399 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003400 int ret;
3401
3402 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003403 if (!path)
3404 return -ENOMEM;
3405
Chris Masonb9473432009-03-13 11:00:37 -04003406 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003407 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3408 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003409 if (ret) {
3410 if (ret > 0)
3411 ret = -ENOENT;
3412 goto failed;
3413 }
3414
Chris Mason5f39d392007-10-15 16:14:19 -04003415 leaf = path->nodes[0];
3416 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003417 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003418
Chris Masone02119d2008-09-05 16:13:11 -04003419 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003420 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003421 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003422 ret = 0;
3423failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003424 btrfs_free_path(path);
3425 return ret;
3426}
3427
Chris Masond352ac62008-09-29 15:18:18 -04003428/*
Chris Mason21151332011-11-10 20:39:08 -05003429 * copy everything in the in-memory inode into the btree.
3430 */
3431noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3432 struct btrfs_root *root, struct inode *inode)
3433{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003434 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003435 int ret;
3436
3437 /*
3438 * If the inode is a free space inode, we can deadlock during commit
3439 * if we put it into the delayed code.
3440 *
3441 * The data relocation inode should also be directly updated
3442 * without delay
3443 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003444 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003445 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003446 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003447 btrfs_update_root_times(trans, root);
3448
Chris Mason21151332011-11-10 20:39:08 -05003449 ret = btrfs_delayed_update_inode(trans, root, inode);
3450 if (!ret)
3451 btrfs_set_inode_last_trans(trans, inode);
3452 return ret;
3453 }
3454
3455 return btrfs_update_inode_item(trans, root, inode);
3456}
3457
Josef Bacikbe6aef62012-10-22 15:43:12 -04003458noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3459 struct btrfs_root *root,
3460 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003461{
3462 int ret;
3463
3464 ret = btrfs_update_inode(trans, root, inode);
3465 if (ret == -ENOSPC)
3466 return btrfs_update_inode_item(trans, root, inode);
3467 return ret;
3468}
3469
3470/*
Chris Masond352ac62008-09-29 15:18:18 -04003471 * unlink helper that gets used here in inode.c and in the tree logging
3472 * recovery code. It remove a link in a directory with a given name, and
3473 * also drops the back refs in the inode to the directory
3474 */
Al Viro92986792011-03-04 17:14:37 +00003475static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3476 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003477 struct btrfs_inode *dir,
3478 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003479 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003480{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003481 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003482 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003483 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003484 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003485 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003486 u64 ino = btrfs_ino(inode);
3487 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003488
3489 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003490 if (!path) {
3491 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003492 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003493 }
3494
Chris Masonb9473432009-03-13 11:00:37 -04003495 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003496 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003497 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003498 if (IS_ERR_OR_NULL(di)) {
3499 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003500 goto err;
3501 }
Chris Mason39279cc2007-06-12 06:35:45 -04003502 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003503 if (ret)
3504 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003505 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003506
Miao Xie67de1172013-12-26 13:07:06 +08003507 /*
3508 * If we don't have dir index, we have to get it by looking up
3509 * the inode ref, since we get the inode ref, remove it directly,
3510 * it is unnecessary to do delayed deletion.
3511 *
3512 * But if we have dir index, needn't search inode ref to get it.
3513 * Since the inode ref is close to the inode item, it is better
3514 * that we delay to delete it, and just do this deletion when
3515 * we update the inode item.
3516 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003517 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003518 ret = btrfs_delayed_delete_inode_ref(inode);
3519 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003520 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003521 goto skip_backref;
3522 }
3523 }
3524
Li Zefan33345d012011-04-20 10:31:50 +08003525 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3526 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003527 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003528 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003529 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003530 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003531 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003532 goto err;
3533 }
Miao Xie67de1172013-12-26 13:07:06 +08003534skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003535 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003536 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003537 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003538 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003539 }
Chris Mason39279cc2007-06-12 06:35:45 -04003540
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003541 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3542 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003543 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003544 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003545 goto err;
3546 }
Chris Masone02119d2008-09-05 16:13:11 -04003547
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003548 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3549 index);
Chris Mason6418c962010-10-30 07:34:24 -04003550 if (ret == -ENOENT)
3551 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003552 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003553 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003554
3555 /*
3556 * If we have a pending delayed iput we could end up with the final iput
3557 * being run in btrfs-cleaner context. If we have enough of these built
3558 * up we can end up burning a lot of time in btrfs-cleaner without any
3559 * way to throttle the unlinks. Since we're currently holding a ref on
3560 * the inode we can run the delayed iput here without any issues as the
3561 * final iput won't be done until after we drop the ref we're currently
3562 * holding.
3563 */
3564 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003565err:
3566 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003567 if (ret)
3568 goto out;
3569
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003570 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003571 inode_inc_iversion(&inode->vfs_inode);
3572 inode_inc_iversion(&dir->vfs_inode);
3573 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3574 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3575 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003576out:
Chris Mason39279cc2007-06-12 06:35:45 -04003577 return ret;
3578}
3579
Al Viro92986792011-03-04 17:14:37 +00003580int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3581 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003582 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003583 const char *name, int name_len)
3584{
3585 int ret;
3586 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3587 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003588 drop_nlink(&inode->vfs_inode);
3589 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003590 }
3591 return ret;
3592}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003593
3594/*
3595 * helper to start transaction for unlink and rmdir.
3596 *
Josef Bacikd52be812013-05-29 14:54:47 -04003597 * unlink and rmdir are special in btrfs, they do not always free space, so
3598 * if we cannot make our reservations the normal way try and see if there is
3599 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3600 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003601 */
Josef Bacikd52be812013-05-29 14:54:47 -04003602static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003603{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003604 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003605
Josef Bacike70bea52011-10-11 14:18:24 -04003606 /*
3607 * 1 for the possible orphan item
3608 * 1 for the dir item
3609 * 1 for the dir index
3610 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003611 * 1 for the inode
3612 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003613 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003614}
3615
Chris Mason39279cc2007-06-12 06:35:45 -04003616static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3617{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003618 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003619 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003620 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003621 int ret;
3622
Josef Bacikd52be812013-05-29 14:54:47 -04003623 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003624 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003625 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003626
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003627 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3628 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003629
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003630 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3631 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3632 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003633 if (ret)
3634 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003635
Yan, Zhenga22285a2010-05-16 10:48:46 -04003636 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003637 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003638 if (ret)
3639 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003640 }
Josef Bacik7b128762008-07-24 12:17:14 -04003641
Tsutomu Itohb5324022011-07-19 07:27:20 +00003642out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003643 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003644 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003645 return ret;
3646}
3647
Misono Tomohirof60a2362018-04-18 11:34:52 +09003648static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003649 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003650{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003651 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003652 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003653 struct btrfs_path *path;
3654 struct extent_buffer *leaf;
3655 struct btrfs_dir_item *di;
3656 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003657 const char *name = dentry->d_name.name;
3658 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003659 u64 index;
3660 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003661 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003662 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003663
Josef Bacik045d3962019-12-18 17:20:27 -05003664 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3665 objectid = inode->root->root_key.objectid;
3666 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3667 objectid = inode->location.objectid;
3668 } else {
3669 WARN_ON(1);
3670 return -EINVAL;
3671 }
3672
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003673 path = btrfs_alloc_path();
3674 if (!path)
3675 return -ENOMEM;
3676
Li Zefan33345d012011-04-20 10:31:50 +08003677 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003678 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003679 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003680 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003681 goto out;
3682 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003683
3684 leaf = path->nodes[0];
3685 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3686 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3687 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003688 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003689 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003690 goto out;
3691 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003692 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003693
Josef Bacikd49d3282019-12-18 17:20:28 -05003694 /*
3695 * This is a placeholder inode for a subvolume we didn't have a
3696 * reference to at the time of the snapshot creation. In the meantime
3697 * we could have renamed the real subvol link into our snapshot, so
3698 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3699 * Instead simply lookup the dir_index_item for this entry so we can
3700 * remove it. Otherwise we know we have a ref to the root and we can
3701 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3702 */
3703 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003704 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003705 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003706 if (IS_ERR_OR_NULL(di)) {
3707 if (!di)
3708 ret = -ENOENT;
3709 else
3710 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003711 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003712 goto out;
3713 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003714
3715 leaf = path->nodes[0];
3716 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003717 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003718 btrfs_release_path(path);
3719 } else {
3720 ret = btrfs_del_root_ref(trans, objectid,
3721 root->root_key.objectid, dir_ino,
3722 &index, name, name_len);
3723 if (ret) {
3724 btrfs_abort_transaction(trans, ret);
3725 goto out;
3726 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003727 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003728
Lu Fengqi9add2942018-08-01 11:32:26 +08003729 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003730 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003731 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003732 goto out;
3733 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003734
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003735 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003736 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003737 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003738 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003739 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003740 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003741out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003742 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003743 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003744}
3745
Misono Tomohiroec42f162018-04-18 11:34:13 +09003746/*
3747 * Helper to check if the subvolume references other subvolumes or if it's
3748 * default.
3749 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003750static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003751{
3752 struct btrfs_fs_info *fs_info = root->fs_info;
3753 struct btrfs_path *path;
3754 struct btrfs_dir_item *di;
3755 struct btrfs_key key;
3756 u64 dir_id;
3757 int ret;
3758
3759 path = btrfs_alloc_path();
3760 if (!path)
3761 return -ENOMEM;
3762
3763 /* Make sure this root isn't set as the default subvol */
3764 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3765 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3766 dir_id, "default", 7, 0);
3767 if (di && !IS_ERR(di)) {
3768 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3769 if (key.objectid == root->root_key.objectid) {
3770 ret = -EPERM;
3771 btrfs_err(fs_info,
3772 "deleting default subvolume %llu is not allowed",
3773 key.objectid);
3774 goto out;
3775 }
3776 btrfs_release_path(path);
3777 }
3778
3779 key.objectid = root->root_key.objectid;
3780 key.type = BTRFS_ROOT_REF_KEY;
3781 key.offset = (u64)-1;
3782
3783 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3784 if (ret < 0)
3785 goto out;
3786 BUG_ON(ret == 0);
3787
3788 ret = 0;
3789 if (path->slots[0] > 0) {
3790 path->slots[0]--;
3791 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3792 if (key.objectid == root->root_key.objectid &&
3793 key.type == BTRFS_ROOT_REF_KEY)
3794 ret = -ENOTEMPTY;
3795 }
3796out:
3797 btrfs_free_path(path);
3798 return ret;
3799}
3800
Nikolay Borisov20a68002018-04-27 14:36:24 +03003801/* Delete all dentries for inodes belonging to the root */
3802static void btrfs_prune_dentries(struct btrfs_root *root)
3803{
3804 struct btrfs_fs_info *fs_info = root->fs_info;
3805 struct rb_node *node;
3806 struct rb_node *prev;
3807 struct btrfs_inode *entry;
3808 struct inode *inode;
3809 u64 objectid = 0;
3810
3811 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3812 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3813
3814 spin_lock(&root->inode_lock);
3815again:
3816 node = root->inode_tree.rb_node;
3817 prev = NULL;
3818 while (node) {
3819 prev = node;
3820 entry = rb_entry(node, struct btrfs_inode, rb_node);
3821
David Sterba37508512018-06-29 10:56:40 +02003822 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003823 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003824 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003825 node = node->rb_right;
3826 else
3827 break;
3828 }
3829 if (!node) {
3830 while (prev) {
3831 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003832 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003833 node = prev;
3834 break;
3835 }
3836 prev = rb_next(prev);
3837 }
3838 }
3839 while (node) {
3840 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003841 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003842 inode = igrab(&entry->vfs_inode);
3843 if (inode) {
3844 spin_unlock(&root->inode_lock);
3845 if (atomic_read(&inode->i_count) > 1)
3846 d_prune_aliases(inode);
3847 /*
3848 * btrfs_drop_inode will have it removed from the inode
3849 * cache when its usage count hits zero.
3850 */
3851 iput(inode);
3852 cond_resched();
3853 spin_lock(&root->inode_lock);
3854 goto again;
3855 }
3856
3857 if (cond_resched_lock(&root->inode_lock))
3858 goto again;
3859
3860 node = rb_next(node);
3861 }
3862 spin_unlock(&root->inode_lock);
3863}
3864
Misono Tomohirof60a2362018-04-18 11:34:52 +09003865int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3866{
3867 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3868 struct btrfs_root *root = BTRFS_I(dir)->root;
3869 struct inode *inode = d_inode(dentry);
3870 struct btrfs_root *dest = BTRFS_I(inode)->root;
3871 struct btrfs_trans_handle *trans;
3872 struct btrfs_block_rsv block_rsv;
3873 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003874 int ret;
3875 int err;
3876
3877 /*
3878 * Don't allow to delete a subvolume with send in progress. This is
3879 * inside the inode lock so the error handling that has to drop the bit
3880 * again is not run concurrently.
3881 */
3882 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003883 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003884 spin_unlock(&dest->root_item_lock);
3885 btrfs_warn(fs_info,
3886 "attempt to delete subvolume %llu during send",
3887 dest->root_key.objectid);
3888 return -EPERM;
3889 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003890 root_flags = btrfs_root_flags(&dest->root_item);
3891 btrfs_set_root_flags(&dest->root_item,
3892 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3893 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003894
3895 down_write(&fs_info->subvol_sem);
3896
3897 err = may_destroy_subvol(dest);
3898 if (err)
3899 goto out_up_write;
3900
3901 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3902 /*
3903 * One for dir inode,
3904 * two for dir entries,
3905 * two for root ref/backref.
3906 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003907 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003908 if (err)
3909 goto out_up_write;
3910
3911 trans = btrfs_start_transaction(root, 0);
3912 if (IS_ERR(trans)) {
3913 err = PTR_ERR(trans);
3914 goto out_release;
3915 }
3916 trans->block_rsv = &block_rsv;
3917 trans->bytes_reserved = block_rsv.size;
3918
3919 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3920
Josef Bacik045d3962019-12-18 17:20:27 -05003921 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003922 if (ret) {
3923 err = ret;
3924 btrfs_abort_transaction(trans, ret);
3925 goto out_end_trans;
3926 }
3927
3928 btrfs_record_root_in_trans(trans, dest);
3929
3930 memset(&dest->root_item.drop_progress, 0,
3931 sizeof(dest->root_item.drop_progress));
3932 dest->root_item.drop_level = 0;
3933 btrfs_set_root_refs(&dest->root_item, 0);
3934
3935 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
3936 ret = btrfs_insert_orphan_item(trans,
3937 fs_info->tree_root,
3938 dest->root_key.objectid);
3939 if (ret) {
3940 btrfs_abort_transaction(trans, ret);
3941 err = ret;
3942 goto out_end_trans;
3943 }
3944 }
3945
Lu Fengqid1957792018-05-29 15:01:54 +08003946 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09003947 BTRFS_UUID_KEY_SUBVOL,
3948 dest->root_key.objectid);
3949 if (ret && ret != -ENOENT) {
3950 btrfs_abort_transaction(trans, ret);
3951 err = ret;
3952 goto out_end_trans;
3953 }
3954 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08003955 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09003956 dest->root_item.received_uuid,
3957 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3958 dest->root_key.objectid);
3959 if (ret && ret != -ENOENT) {
3960 btrfs_abort_transaction(trans, ret);
3961 err = ret;
3962 goto out_end_trans;
3963 }
3964 }
3965
3966out_end_trans:
3967 trans->block_rsv = NULL;
3968 trans->bytes_reserved = 0;
3969 ret = btrfs_end_transaction(trans);
3970 if (ret && !err)
3971 err = ret;
3972 inode->i_flags |= S_DEAD;
3973out_release:
3974 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
3975out_up_write:
3976 up_write(&fs_info->subvol_sem);
3977 if (err) {
3978 spin_lock(&dest->root_item_lock);
3979 root_flags = btrfs_root_flags(&dest->root_item);
3980 btrfs_set_root_flags(&dest->root_item,
3981 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
3982 spin_unlock(&dest->root_item_lock);
3983 } else {
3984 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03003985 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003986 ASSERT(dest->send_in_progress == 0);
3987
3988 /* the last ref */
3989 if (dest->ino_cache_inode) {
3990 iput(dest->ino_cache_inode);
3991 dest->ino_cache_inode = NULL;
3992 }
3993 }
3994
3995 return err;
3996}
3997
Chris Mason39279cc2007-06-12 06:35:45 -04003998static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3999{
David Howells2b0143b2015-03-17 22:25:59 +00004000 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004001 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004002 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004003 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004004 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004005
David Sterbab3ae2442012-09-13 16:04:34 -06004006 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004007 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004008 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004009 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004010
Josef Bacikd52be812013-05-29 14:54:47 -04004011 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004012 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004013 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004014
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004015 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004016 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004017 goto out;
4018 }
4019
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004020 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004021 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004022 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004023
Filipe Manana44f714d2016-06-06 16:11:13 +01004024 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4025
Chris Mason39279cc2007-06-12 06:35:45 -04004026 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004027 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4028 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4029 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004030 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004031 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004032 /*
4033 * Propagate the last_unlink_trans value of the deleted dir to
4034 * its parent directory. This is to prevent an unrecoverable
4035 * log tree in the case we do something like this:
4036 * 1) create dir foo
4037 * 2) create snapshot under dir foo
4038 * 3) delete the snapshot
4039 * 4) rmdir foo
4040 * 5) mkdir foo
4041 * 6) fsync foo or some file inside foo
4042 */
4043 if (last_unlink_trans >= trans->transid)
4044 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4045 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004046out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004047 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004048 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004049
Chris Mason39279cc2007-06-12 06:35:45 -04004050 return err;
4051}
4052
Josef Bacikddfae632017-10-19 14:16:02 -04004053/*
4054 * Return this if we need to call truncate_block for the last bit of the
4055 * truncate.
4056 */
4057#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004058
Chris Mason323ac952008-10-01 19:05:46 -04004059/*
Chris Mason39279cc2007-06-12 06:35:45 -04004060 * this can truncate away extent items, csum items and directory items.
4061 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004062 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004063 *
4064 * csum items that cross the new i_size are truncated to the new size
4065 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004066 *
4067 * min_type is the minimum key type to truncate down to. If set to 0, this
4068 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004069 */
Yan, Zheng80825102009-11-12 09:35:36 +00004070int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4071 struct btrfs_root *root,
4072 struct inode *inode,
4073 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004074{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004075 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004076 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004077 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004078 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004079 struct btrfs_key key;
4080 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004081 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004082 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004083 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004084 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004085 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004086 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004087 int found_extent;
4088 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004089 int pending_del_nr = 0;
4090 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004091 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004092 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004093 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004094 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004095 bool be_nice = false;
4096 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004097 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4098 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004099
4100 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004101
Chris Mason28ed1342014-12-17 09:41:04 -08004102 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004103 * For non-free space inodes and non-shareable roots, we want to back
4104 * off from time to time. This means all inodes in subvolume roots,
4105 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004106 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004107 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004108 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004109 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004110
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004111 path = btrfs_alloc_path();
4112 if (!path)
4113 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004114 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004115
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004116 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4117 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4118 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004119
Josef Bacik5dc562c2012-08-17 13:14:17 -04004120 /*
4121 * We want to drop from the next block forward in case this new size is
4122 * not block aligned since we will be keeping the last block of the
4123 * extent just the way it is.
4124 */
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004125 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004126 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004127 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004128 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004129 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004130
Miao Xie16cdcec2011-04-22 18:12:22 +08004131 /*
4132 * This function is also used to drop the items in the log tree before
4133 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004134 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004135 * items.
4136 */
4137 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004138 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004139
Li Zefan33345d012011-04-20 10:31:50 +08004140 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004141 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004142 key.type = (u8)-1;
4143
Chris Mason85e21ba2008-01-29 15:11:36 -05004144search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004145 /*
4146 * with a 16K leaf size and 128MB extents, you can actually queue
4147 * up a huge file in a single leaf. Most of the time that
4148 * bytes_deleted is > 0, it will be huge by the time we get here
4149 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004150 if (be_nice && bytes_deleted > SZ_32M &&
4151 btrfs_should_end_transaction(trans)) {
4152 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004153 goto out;
4154 }
Chris Masond3977122009-01-05 21:25:51 -05004155
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004156 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4157 if (ret < 0)
4158 goto out;
4159
Chris Mason85e21ba2008-01-29 15:11:36 -05004160 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004161 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004162 /* there are no items in the tree for us to truncate, we're
4163 * done
4164 */
Yan, Zheng80825102009-11-12 09:35:36 +00004165 if (path->slots[0] == 0)
4166 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004167 path->slots[0]--;
4168 }
4169
Chris Masond3977122009-01-05 21:25:51 -05004170 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004171 u64 clear_start = 0, clear_len = 0;
4172
Chris Mason39279cc2007-06-12 06:35:45 -04004173 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004174 leaf = path->nodes[0];
4175 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004176 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004177
Li Zefan33345d012011-04-20 10:31:50 +08004178 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004179 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004180
Chris Mason85e21ba2008-01-29 15:11:36 -05004181 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004182 break;
4183
Chris Mason5f39d392007-10-15 16:14:19 -04004184 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004185 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004186 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004187 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004188 extent_type = btrfs_file_extent_type(leaf, fi);
4189 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004190 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004191 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004192
4193 trace_btrfs_truncate_show_fi_regular(
4194 BTRFS_I(inode), leaf, fi,
4195 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004196 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004197 item_end += btrfs_file_extent_ram_bytes(leaf,
4198 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004199
4200 trace_btrfs_truncate_show_fi_inline(
4201 BTRFS_I(inode), leaf, fi, path->slots[0],
4202 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004203 }
Yan008630c2007-11-07 13:31:09 -05004204 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004205 }
Yan, Zheng80825102009-11-12 09:35:36 +00004206 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004207 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004208 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004209 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004210 break;
4211 if (found_key.offset >= new_size)
4212 del_item = 1;
4213 else
4214 del_item = 0;
4215 }
Chris Mason39279cc2007-06-12 06:35:45 -04004216 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004217 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004218 if (found_type != BTRFS_EXTENT_DATA_KEY)
4219 goto delete;
4220
4221 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004222 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004223
4224 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004225 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004226 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004227 u64 orig_num_bytes =
4228 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004229 extent_num_bytes = ALIGN(new_size -
4230 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004231 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004232 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004233 btrfs_set_file_extent_num_bytes(leaf, fi,
4234 extent_num_bytes);
4235 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004236 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004237 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004238 &root->state) &&
4239 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004240 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004241 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004242 } else {
Chris Masondb945352007-10-15 16:15:53 -04004243 extent_num_bytes =
4244 btrfs_file_extent_disk_num_bytes(leaf,
4245 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004246 extent_offset = found_key.offset -
4247 btrfs_file_extent_offset(leaf, fi);
4248
Chris Mason39279cc2007-06-12 06:35:45 -04004249 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004250 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004251 if (extent_start != 0) {
4252 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004253 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004254 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004255 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004256 }
4257 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004258 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004259 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004260 /*
4261 * we can't truncate inline items that have had
4262 * special encodings
4263 */
4264 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004265 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004266 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4267 btrfs_file_extent_compression(leaf, fi) == 0) {
4268 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004269
Josef Bacikddfae632017-10-19 14:16:02 -04004270 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4271 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004272 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004273 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004274 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004275 * We have to bail so the last_size is set to
4276 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004277 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004278 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004279 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004280 } else {
4281 /*
4282 * Inline extents are special, we just treat
4283 * them as a full sector worth in the file
4284 * extent tree just for simplicity sake.
4285 */
4286 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004287 }
Josef Bacikddfae632017-10-19 14:16:02 -04004288
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004289 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004290 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004291 }
Chris Mason179e29e2007-11-01 11:28:41 -04004292delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004293 /*
4294 * We use btrfs_truncate_inode_items() to clean up log trees for
4295 * multiple fsyncs, and in this case we don't want to clear the
4296 * file extent range because it's just the log.
4297 */
4298 if (root == BTRFS_I(inode)->root) {
4299 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4300 clear_start, clear_len);
4301 if (ret) {
4302 btrfs_abort_transaction(trans, ret);
4303 break;
4304 }
4305 }
4306
Josef Bacikddfae632017-10-19 14:16:02 -04004307 if (del_item)
4308 last_size = found_key.offset;
4309 else
4310 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004311 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004312 if (!pending_del_nr) {
4313 /* no pending yet, add ourselves */
4314 pending_del_slot = path->slots[0];
4315 pending_del_nr = 1;
4316 } else if (pending_del_nr &&
4317 path->slots[0] + 1 == pending_del_slot) {
4318 /* hop on the pending chunk */
4319 pending_del_nr++;
4320 pending_del_slot = path->slots[0];
4321 } else {
Chris Masond3977122009-01-05 21:25:51 -05004322 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004323 }
Chris Mason39279cc2007-06-12 06:35:45 -04004324 } else {
4325 break;
4326 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004327 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004328
Miao Xie27cdeb72014-04-02 19:51:05 +08004329 if (found_extent &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004330 (test_bit(BTRFS_ROOT_SHAREABLE, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004331 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004332 struct btrfs_ref ref = { 0 };
4333
Chris Mason28ed1342014-12-17 09:41:04 -08004334 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004335
4336 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4337 extent_start, extent_num_bytes, 0);
4338 ref.real_root = root->root_key.objectid;
4339 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4340 ino, extent_offset);
4341 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004342 if (ret) {
4343 btrfs_abort_transaction(trans, ret);
4344 break;
4345 }
Chris Mason28f75a02015-02-04 06:59:29 -08004346 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004347 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004348 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004349 }
Chris Mason39279cc2007-06-12 06:35:45 -04004350 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004351
Yan, Zheng80825102009-11-12 09:35:36 +00004352 if (found_type == BTRFS_INODE_ITEM_KEY)
4353 break;
4354
4355 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004356 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004357 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004358 if (pending_del_nr) {
4359 ret = btrfs_del_items(trans, root, path,
4360 pending_del_slot,
4361 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004362 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004363 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004364 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004365 }
Yan, Zheng80825102009-11-12 09:35:36 +00004366 pending_del_nr = 0;
4367 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004368 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004369
Chris Mason28f75a02015-02-04 06:59:29 -08004370 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004371 * We can generate a lot of delayed refs, so we need to
4372 * throttle every once and a while and make sure we're
4373 * adding enough space to keep up with the work we are
4374 * generating. Since we hold a transaction here we
4375 * can't flush, and we don't want to FLUSH_LIMIT because
4376 * we could have generated too many delayed refs to
4377 * actually allocate, so just bail if we're short and
4378 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004379 */
Josef Bacik28bad212018-12-03 10:20:38 -05004380 if (should_throttle) {
4381 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4382 BTRFS_RESERVE_NO_FLUSH);
4383 if (ret) {
4384 ret = -EAGAIN;
4385 break;
4386 }
Chris Mason28f75a02015-02-04 06:59:29 -08004387 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004388 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004389 } else {
4390 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004391 }
Chris Mason39279cc2007-06-12 06:35:45 -04004392 }
Yan, Zheng80825102009-11-12 09:35:36 +00004393out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004394 if (ret >= 0 && pending_del_nr) {
4395 int err;
4396
4397 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004398 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004399 if (err) {
4400 btrfs_abort_transaction(trans, err);
4401 ret = err;
4402 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004403 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004404 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4405 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004406 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004407 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004408 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004409 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4410 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004411 }
Chris Mason28ed1342014-12-17 09:41:04 -08004412
Chris Mason39279cc2007-06-12 06:35:45 -04004413 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004414 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004415}
4416
Chris Masona52d9a82007-08-27 16:49:44 -04004417/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304418 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004419 * @inode - inode that we're zeroing
4420 * @from - the offset to start zeroing
4421 * @len - the length to zero, 0 to zero the entire range respective to the
4422 * offset
4423 * @front - zero up to the offset instead of from the offset on
4424 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304425 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004426 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004427 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304428int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004429 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004430{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004431 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004432 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004433 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4434 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004435 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004436 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004437 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004438 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004439 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304440 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004441 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004442 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004443 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304444 u64 block_start;
4445 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004446
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004447 if (IS_ALIGNED(offset, blocksize) &&
4448 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004449 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304450
Josef Bacik8b62f872017-10-19 14:15:55 -04004451 block_start = round_down(from, blocksize);
4452 block_end = block_start + blocksize - 1;
4453
Qu Wenruo364ecf32017-02-27 15:10:38 +08004454 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004455 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004456 if (ret)
4457 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004458
Chris Mason211c17f2008-05-15 09:13:45 -04004459again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004460 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004461 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004462 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004463 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004464 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004465 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004466 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004467 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004468
Chris Masona52d9a82007-08-27 16:49:44 -04004469 if (!PageUptodate(page)) {
4470 ret = btrfs_readpage(NULL, page);
4471 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004472 if (page->mapping != mapping) {
4473 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004474 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004475 goto again;
4476 }
Chris Masona52d9a82007-08-27 16:49:44 -04004477 if (!PageUptodate(page)) {
4478 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004479 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004480 }
4481 }
Chris Mason211c17f2008-05-15 09:13:45 -04004482 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004483
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304484 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004485 set_page_extent_mapped(page);
4486
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304487 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004488 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304489 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004490 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004491 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004492 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004493 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004494 btrfs_put_ordered_extent(ordered);
4495 goto again;
4496 }
4497
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304498 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004499 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4500 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004501
Filipe Mananae3b8a482017-11-04 00:16:59 +00004502 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004503 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004504 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304505 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004506 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004507 goto out_unlock;
4508 }
4509
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304510 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004511 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304512 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004513 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004514 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304515 memset(kaddr + (block_start - page_offset(page)),
4516 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004517 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304518 memset(kaddr + (block_start - page_offset(page)) + offset,
4519 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004520 flush_dcache_page(page);
4521 kunmap(page);
4522 }
Chris Mason247e7432008-07-17 12:53:51 -04004523 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004524 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004525 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004526
Chris Mason89642222008-07-24 09:41:53 -04004527out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004528 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004529 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004530 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004531 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004532 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004533 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004534out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004535 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004536 return ret;
4537}
4538
Josef Bacik16e75492013-10-22 12:18:51 -04004539static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4540 u64 offset, u64 len)
4541{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004542 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004543 struct btrfs_trans_handle *trans;
4544 int ret;
4545
4546 /*
4547 * Still need to make sure the inode looks like it's been updated so
4548 * that any holes get logged if we fsync.
4549 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004550 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4551 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004552 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4553 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4554 return 0;
4555 }
4556
4557 /*
4558 * 1 - for the one we're dropping
4559 * 1 - for the one we're adding
4560 * 1 - for updating the inode.
4561 */
4562 trans = btrfs_start_transaction(root, 3);
4563 if (IS_ERR(trans))
4564 return PTR_ERR(trans);
4565
4566 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4567 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004568 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004569 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004570 return ret;
4571 }
4572
David Sterbaf85b7372017-01-20 14:54:07 +01004573 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4574 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004575 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004576 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004577 else
4578 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004579 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004580 return ret;
4581}
4582
Josef Bacik695a0d02011-03-04 15:46:53 -05004583/*
4584 * This function puts in dummy file extents for the area we're creating a hole
4585 * for. So if we are truncating this file to a larger size we need to insert
4586 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4587 * the range between oldsize and size
4588 */
Josef Bacika41ad392011-01-31 15:30:16 -05004589int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004590{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004591 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004592 struct btrfs_root *root = BTRFS_I(inode)->root;
4593 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004594 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004595 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004596 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004597 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4598 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004599 u64 last_byte;
4600 u64 cur_offset;
4601 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004602 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004603
Josef Bacika71754f2013-06-17 17:14:39 -04004604 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304605 * If our size started in the middle of a block we need to zero out the
4606 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004607 * expose stale data.
4608 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304609 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004610 if (err)
4611 return err;
4612
Yan Zheng9036c102008-10-30 14:19:41 -04004613 if (size <= hole_start)
4614 return 0;
4615
David Sterbab272ae22020-02-05 19:09:33 +01004616 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004617 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004618 cur_offset = hole_start;
4619 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004620 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004621 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004622 if (IS_ERR(em)) {
4623 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004624 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004625 break;
4626 }
Yan Zheng9036c102008-10-30 14:19:41 -04004627 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004628 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004629 hole_size = last_byte - cur_offset;
4630
Yan, Zheng80825102009-11-12 09:35:36 +00004631 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004632 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004633
Josef Bacik16e75492013-10-22 12:18:51 -04004634 err = maybe_insert_hole(root, inode, cur_offset,
4635 hole_size);
4636 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004637 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004638
4639 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4640 cur_offset, hole_size);
4641 if (err)
4642 break;
4643
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004644 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004645 cur_offset + hole_size - 1, 0);
4646 hole_em = alloc_extent_map();
4647 if (!hole_em) {
4648 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4649 &BTRFS_I(inode)->runtime_flags);
4650 goto next;
4651 }
4652 hole_em->start = cur_offset;
4653 hole_em->len = hole_size;
4654 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004655
Josef Bacik5dc562c2012-08-17 13:14:17 -04004656 hole_em->block_start = EXTENT_MAP_HOLE;
4657 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004658 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004659 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004660 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004661 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004662
4663 while (1) {
4664 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004665 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004666 write_unlock(&em_tree->lock);
4667 if (err != -EEXIST)
4668 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004669 btrfs_drop_extent_cache(BTRFS_I(inode),
4670 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004671 cur_offset +
4672 hole_size - 1, 0);
4673 }
4674 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004675 } else {
4676 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4677 cur_offset, hole_size);
4678 if (err)
4679 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004680 }
Josef Bacik16e75492013-10-22 12:18:51 -04004681next:
Yan Zheng9036c102008-10-30 14:19:41 -04004682 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004683 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004684 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004685 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004686 break;
4687 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004688 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004689 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004690 return err;
4691}
4692
Eric Sandeen3972f262013-01-12 02:57:22 +00004693static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004694{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004695 struct btrfs_root *root = BTRFS_I(inode)->root;
4696 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004697 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004698 loff_t newsize = attr->ia_size;
4699 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004700 int ret;
4701
Eric Sandeen3972f262013-01-12 02:57:22 +00004702 /*
4703 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4704 * special case where we need to update the times despite not having
4705 * these flags set. For all other operations the VFS set these flags
4706 * explicitly if it wants a timestamp update.
4707 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004708 if (newsize != oldsize) {
4709 inode_inc_iversion(inode);
4710 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4711 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004712 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004713 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004714
Josef Bacika41ad392011-01-31 15:30:16 -05004715 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004716 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004717 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004718 * This is to ensure the snapshot captures a fully consistent
4719 * state of this file - if the snapshot captures this expanding
4720 * truncation, it must capture all writes that happened before
4721 * this truncation.
4722 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004723 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004724 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004725 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004726 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004727 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004728 }
Yan, Zheng80825102009-11-12 09:35:36 +00004729
Miao Xief4a2f4c2011-12-14 20:12:01 -05004730 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004731 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004732 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004733 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004734 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004735
4736 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004737 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304738 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004739 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004740 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004741 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004742 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004743
Josef Bacika41ad392011-01-31 15:30:16 -05004744 /*
4745 * We're truncating a file that used to have good data down to
4746 * zero. Make sure it gets into the ordered flush list so that
4747 * any new writes get down to disk quickly.
4748 */
4749 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004750 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4751 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004752
Josef Bacika41ad392011-01-31 15:30:16 -05004753 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004754
Andrea Gelmini52042d82018-11-28 12:05:13 +01004755 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02004756 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004757 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02004758 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004759
Filipe Manana213e8c52018-02-06 20:40:31 +00004760 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004761 if (ret && inode->i_nlink) {
4762 int err;
4763
4764 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004765 * Truncate failed, so fix up the in-memory size. We
4766 * adjusted disk_i_size down as we removed extents, so
4767 * wait for disk_i_size to be stable and then update the
4768 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004769 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004770 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004771 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004772 return err;
4773 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004774 }
Yan, Zheng80825102009-11-12 09:35:36 +00004775 }
4776
Josef Bacika41ad392011-01-31 15:30:16 -05004777 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004778}
4779
Chris Mason39279cc2007-06-12 06:35:45 -04004780static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4781{
David Howells2b0143b2015-03-17 22:25:59 +00004782 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004783 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004784 int err;
4785
Li Zefanb83cc962010-12-20 16:04:08 +08004786 if (btrfs_root_readonly(root))
4787 return -EROFS;
4788
Jan Kara31051c82016-05-26 16:55:18 +02004789 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004790 if (err)
4791 return err;
4792
Chris Mason5a3f23d2009-03-31 13:27:11 -04004793 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004794 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004795 if (err)
4796 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004797 }
Yan Zheng9036c102008-10-30 14:19:41 -04004798
Christoph Hellwig10257742010-06-04 11:30:02 +02004799 if (attr->ia_valid) {
4800 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004801 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004802 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004803
Josef Bacik22c44fe2011-11-30 10:45:38 -05004804 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004805 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004806 }
4807
Chris Mason39279cc2007-06-12 06:35:45 -04004808 return err;
4809}
Chris Mason61295eb2008-01-14 16:24:38 -05004810
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004811/*
4812 * While truncating the inode pages during eviction, we get the VFS calling
4813 * btrfs_invalidatepage() against each page of the inode. This is slow because
4814 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4815 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4816 * extent_state structures over and over, wasting lots of time.
4817 *
4818 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4819 * those expensive operations on a per page basis and do only the ordered io
4820 * finishing, while we release here the extent_map and extent_state structures,
4821 * without the excessive merging and splitting.
4822 */
4823static void evict_inode_truncate_pages(struct inode *inode)
4824{
4825 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4826 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4827 struct rb_node *node;
4828
4829 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004830 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004831
4832 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004833 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004834 struct extent_map *em;
4835
Liu Bo07e1ce02018-08-23 03:51:52 +08004836 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004837 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004838 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4839 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004840 remove_extent_mapping(map_tree, em);
4841 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004842 if (need_resched()) {
4843 write_unlock(&map_tree->lock);
4844 cond_resched();
4845 write_lock(&map_tree->lock);
4846 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004847 }
4848 write_unlock(&map_tree->lock);
4849
Filipe Manana6ca07092015-05-26 00:55:42 +01004850 /*
4851 * Keep looping until we have no more ranges in the io tree.
4852 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004853 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
4854 * still in progress (unlocked the pages in the bio but did not yet
4855 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004856 * ranges can still be locked and eviction started because before
4857 * submitting those bios, which are executed by a separate task (work
4858 * queue kthread), inode references (inode->i_count) were not taken
4859 * (which would be dropped in the end io callback of each bio).
4860 * Therefore here we effectively end up waiting for those bios and
4861 * anyone else holding locked ranges without having bumped the inode's
4862 * reference count - if we don't do it, when they access the inode's
4863 * io_tree to unlock a range it may be too late, leading to an
4864 * use-after-free issue.
4865 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004866 spin_lock(&io_tree->lock);
4867 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4868 struct extent_state *state;
4869 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004870 u64 start;
4871 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004872 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004873
4874 node = rb_first(&io_tree->state);
4875 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004876 start = state->start;
4877 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004878 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004879 spin_unlock(&io_tree->lock);
4880
David Sterbaff13db42015-12-03 14:30:40 +01004881 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004882
4883 /*
4884 * If still has DELALLOC flag, the extent didn't reach disk,
4885 * and its reserved space won't be freed by delayed_ref.
4886 * So we need to free its reserved space here.
4887 * (Refer to comment in btrfs_invalidatepage, case 2)
4888 *
4889 * Note, end is the bytenr of last byte, so we need + 1 here.
4890 */
Filipe Manana421f0922018-10-12 13:02:48 +01004891 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004892 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004893
Filipe Manana6ca07092015-05-26 00:55:42 +01004894 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004895 EXTENT_LOCKED | EXTENT_DELALLOC |
4896 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4897 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004898
Filipe Manana7064dd52014-08-08 02:47:05 +01004899 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004900 spin_lock(&io_tree->lock);
4901 }
4902 spin_unlock(&io_tree->lock);
4903}
4904
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004905static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04004906 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004907{
4908 struct btrfs_fs_info *fs_info = root->fs_info;
4909 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04004910 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04004911 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04004912 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004913
Josef Bacikd3984c92019-08-01 18:19:37 -04004914 /*
4915 * Eviction should be taking place at some place safe because of our
4916 * delayed iputs. However the normal flushing code will run delayed
4917 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
4918 *
4919 * We reserve the delayed_refs_extra here again because we can't use
4920 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
4921 * above. We reserve our extra bit here because we generate a ton of
4922 * delayed refs activity by truncating.
4923 *
4924 * If we cannot make our reservation we'll attempt to steal from the
4925 * global reserve, because we really want to be able to free up space.
4926 */
4927 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
4928 BTRFS_RESERVE_FLUSH_EVICT);
4929 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004930 /*
4931 * Try to steal from the global reserve if there is space for
4932 * it.
4933 */
Josef Bacikd3984c92019-08-01 18:19:37 -04004934 if (btrfs_check_space_for_delayed_refs(fs_info) ||
4935 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
4936 btrfs_warn(fs_info,
4937 "could not allocate space for delete; will truncate on mount");
4938 return ERR_PTR(-ENOSPC);
4939 }
4940 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004941 }
Josef Bacikd3984c92019-08-01 18:19:37 -04004942
4943 trans = btrfs_join_transaction(root);
4944 if (IS_ERR(trans))
4945 return trans;
4946
4947 if (delayed_refs_extra) {
4948 trans->block_rsv = &fs_info->trans_block_rsv;
4949 trans->bytes_reserved = delayed_refs_extra;
4950 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
4951 delayed_refs_extra, 1);
4952 }
4953 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004954}
4955
Al Virobd555972010-06-07 11:35:40 -04004956void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004957{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04004959 struct btrfs_trans_handle *trans;
4960 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004961 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04004962 int ret;
4963
liubo1abe9b82011-03-24 11:18:59 +00004964 trace_btrfs_inode_evict(inode);
4965
Nikolay Borisov3d48d982016-06-29 09:46:41 +03004966 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07004967 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03004968 return;
4969 }
4970
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004971 evict_inode_truncate_pages(inode);
4972
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004973 if (inode->i_nlink &&
4974 ((btrfs_root_refs(&root->root_item) != 0 &&
4975 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004976 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04004977 goto no_delete;
4978
Omar Sandoval27919062018-05-11 13:13:37 -07004979 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04004980 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04004981
Nikolay Borisov7ab79562017-02-20 13:50:57 +02004982 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08004983
Omar Sandoval7b40b692018-05-11 13:13:33 -07004984 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00004985 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00004986
Yan, Zheng76dda932009-09-21 16:00:26 -04004987 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004988 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4989 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004990 goto no_delete;
4991 }
4992
Nikolay Borisovaa790212017-01-10 20:35:40 +02004993 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07004994 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00004995 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00004996
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004997 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07004998 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04004999 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005000 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005001 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005002
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005003 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005004
Yan, Zheng80825102009-11-12 09:35:36 +00005005 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005006 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005007 if (IS_ERR(trans))
5008 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005009
5010 trans->block_rsv = rsv;
5011
Yan, Zhengd68fc572010-05-16 10:49:58 -04005012 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005013 trans->block_rsv = &fs_info->trans_block_rsv;
5014 btrfs_end_transaction(trans);
5015 btrfs_btree_balance_dirty(fs_info);
5016 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5017 goto free_rsv;
5018 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005019 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005020 }
5021
Josef Bacik4ef31a42013-08-13 14:10:08 -04005022 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005023 * Errors here aren't a big deal, it just means we leave orphan items in
5024 * the tree. They will be cleaned up on the next mount. If the inode
5025 * number gets reused, cleanup deletes the orphan item without doing
5026 * anything, and unlink reuses the existing orphan item.
5027 *
5028 * If it turns out that we are dropping too many of these, we might want
5029 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005030 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005031 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005032 if (!IS_ERR(trans)) {
5033 trans->block_rsv = rsv;
5034 btrfs_orphan_del(trans, BTRFS_I(inode));
5035 trans->block_rsv = &fs_info->trans_block_rsv;
5036 btrfs_end_transaction(trans);
5037 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005038
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005039 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005040 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005041 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005042
Omar Sandoval27919062018-05-11 13:13:37 -07005043free_rsv:
5044 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005045no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005046 /*
5047 * If we didn't successfully delete, the orphan item will still be in
5048 * the tree and we'll retry on the next mount. Again, we might also want
5049 * to retry these periodically in the future.
5050 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005051 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005052 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005053}
5054
5055/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005056 * Return the key found in the dir entry in the location pointer, fill @type
5057 * with BTRFS_FT_*, and return 0.
5058 *
Su Yue005d6712018-03-05 17:13:37 +08005059 * If no dir entries were found, returns -ENOENT.
5060 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005061 */
5062static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005063 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005064{
5065 const char *name = dentry->d_name.name;
5066 int namelen = dentry->d_name.len;
5067 struct btrfs_dir_item *di;
5068 struct btrfs_path *path;
5069 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005070 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005071
5072 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005073 if (!path)
5074 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005075
David Sterbaf85b7372017-01-20 14:54:07 +01005076 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5077 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005078 if (IS_ERR_OR_NULL(di)) {
5079 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005080 goto out;
5081 }
Chris Masond3977122009-01-05 21:25:51 -05005082
Chris Mason5f39d392007-10-15 16:14:19 -04005083 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005084 if (location->type != BTRFS_INODE_ITEM_KEY &&
5085 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005086 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005087 btrfs_warn(root->fs_info,
5088"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5089 __func__, name, btrfs_ino(BTRFS_I(dir)),
5090 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005091 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005092 if (!ret)
5093 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005094out:
Chris Mason39279cc2007-06-12 06:35:45 -04005095 btrfs_free_path(path);
5096 return ret;
5097}
5098
5099/*
5100 * when we hit a tree root in a directory, the btrfs part of the inode
5101 * needs to be changed to reflect the root directory of the tree root. This
5102 * is kind of like crossing a mount point.
5103 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005104static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005105 struct inode *dir,
5106 struct dentry *dentry,
5107 struct btrfs_key *location,
5108 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005109{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005110 struct btrfs_path *path;
5111 struct btrfs_root *new_root;
5112 struct btrfs_root_ref *ref;
5113 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005114 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005115 int ret;
5116 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005117
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005118 path = btrfs_alloc_path();
5119 if (!path) {
5120 err = -ENOMEM;
5121 goto out;
5122 }
Chris Mason39279cc2007-06-12 06:35:45 -04005123
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005124 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005125 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5126 key.type = BTRFS_ROOT_REF_KEY;
5127 key.offset = location->objectid;
5128
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005129 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005130 if (ret) {
5131 if (ret < 0)
5132 err = ret;
5133 goto out;
5134 }
Chris Mason39279cc2007-06-12 06:35:45 -04005135
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005136 leaf = path->nodes[0];
5137 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005138 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005139 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5140 goto out;
5141
5142 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5143 (unsigned long)(ref + 1),
5144 dentry->d_name.len);
5145 if (ret)
5146 goto out;
5147
David Sterbab3b4aa72011-04-21 01:20:15 +02005148 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005149
Josef Bacik3619c942020-01-24 09:32:24 -05005150 new_root = btrfs_get_fs_root(fs_info, location, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005151 if (IS_ERR(new_root)) {
5152 err = PTR_ERR(new_root);
5153 goto out;
5154 }
5155
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005156 *sub_root = new_root;
5157 location->objectid = btrfs_root_dirid(&new_root->root_item);
5158 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005159 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005160 err = 0;
5161out:
5162 btrfs_free_path(path);
5163 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005164}
5165
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005166static void inode_tree_add(struct inode *inode)
5167{
5168 struct btrfs_root *root = BTRFS_I(inode)->root;
5169 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005170 struct rb_node **p;
5171 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005172 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005173 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005174
Al Viro1d3382cb2010-10-23 15:19:20 -04005175 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005176 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005177 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005178 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005179 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005180 while (*p) {
5181 parent = *p;
5182 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5183
David Sterba37508512018-06-29 10:56:40 +02005184 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005185 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005186 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005187 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005188 else {
5189 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005190 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005191 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005192 RB_CLEAR_NODE(parent);
5193 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005194 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005195 }
5196 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005197 rb_link_node(new, parent, p);
5198 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005199 spin_unlock(&root->inode_lock);
5200}
5201
5202static void inode_tree_del(struct inode *inode)
5203{
5204 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005205 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005206
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005207 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005208 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005209 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005210 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005211 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005212 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005213 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005214
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005215 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005216 spin_lock(&root->inode_lock);
5217 empty = RB_EMPTY_ROOT(&root->inode_tree);
5218 spin_unlock(&root->inode_lock);
5219 if (empty)
5220 btrfs_add_dead_root(root);
5221 }
5222}
5223
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005224
Chris Masone02119d2008-09-05 16:13:11 -04005225static int btrfs_init_locked_inode(struct inode *inode, void *p)
5226{
5227 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005228 inode->i_ino = args->location->objectid;
5229 memcpy(&BTRFS_I(inode)->location, args->location,
5230 sizeof(*args->location));
Josef Bacik5c8fd992020-02-14 16:11:43 -05005231 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5232 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005233 return 0;
5234}
5235
5236static int btrfs_find_actor(struct inode *inode, void *opaque)
5237{
5238 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005239 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005240 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005241}
5242
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005243static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005244 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005245 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005246{
5247 struct inode *inode;
5248 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005249 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005250
Chris Mason90d3e592014-01-09 17:28:00 -08005251 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005252 args.root = root;
5253
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005254 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005255 btrfs_init_locked_inode,
5256 (void *)&args);
5257 return inode;
5258}
5259
David Sterba4c66e0d2019-10-03 19:09:35 +02005260/*
5261 * Get an inode object given its location and corresponding root.
5262 * Path can be preallocated to prevent recursing back to iget through
5263 * allocator. NULL is also valid but may require an additional allocation
5264 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305265 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005266struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005267 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305268{
5269 struct inode *inode;
5270
Chris Mason90d3e592014-01-09 17:28:00 -08005271 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305272 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005273 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305274
5275 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005276 int ret;
5277
Filipe Manana4222ea72018-10-24 10:13:03 +01005278 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005279 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005280 inode_tree_add(inode);
5281 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005282 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005283 iget_failed(inode);
5284 /*
5285 * ret > 0 can come from btrfs_search_slot called by
5286 * btrfs_read_locked_inode, this means the inode item
5287 * was not found.
5288 */
5289 if (ret > 0)
5290 ret = -ENOENT;
5291 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005292 }
5293 }
5294
Balaji Rao1a54ef82008-07-21 02:01:04 +05305295 return inode;
5296}
5297
Filipe Manana4222ea72018-10-24 10:13:03 +01005298struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005299 struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005300{
David Sterba4c66e0d2019-10-03 19:09:35 +02005301 return btrfs_iget_path(s, location, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005302}
5303
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005304static struct inode *new_simple_dir(struct super_block *s,
5305 struct btrfs_key *key,
5306 struct btrfs_root *root)
5307{
5308 struct inode *inode = new_inode(s);
5309
5310 if (!inode)
5311 return ERR_PTR(-ENOMEM);
5312
Josef Bacik5c8fd992020-02-14 16:11:43 -05005313 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005314 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005315 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005316
5317 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005318 /*
5319 * We only need lookup, the rest is read-only and there's no inode
5320 * associated with the dentry
5321 */
5322 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005323 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005324 inode->i_fop = &simple_dir_operations;
5325 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005326 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305327 inode->i_atime = inode->i_mtime;
5328 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005329 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005330
5331 return inode;
5332}
5333
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005334static inline u8 btrfs_inode_type(struct inode *inode)
5335{
5336 /*
5337 * Compile-time asserts that generic FT_* types still match
5338 * BTRFS_FT_* types
5339 */
5340 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5341 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5342 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5343 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5344 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5345 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5346 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5347 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5348
5349 return fs_umode_to_ftype(inode->i_mode);
5350}
5351
Chris Mason3de45862008-11-17 21:02:50 -05005352struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005353{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005354 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005355 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005356 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005357 struct btrfs_root *sub_root = root;
5358 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005359 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005360 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005361
5362 if (dentry->d_name.len > BTRFS_NAME_LEN)
5363 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005364
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005365 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005366 if (ret < 0)
5367 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005368
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005369 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba4c66e0d2019-10-03 19:09:35 +02005370 inode = btrfs_iget(dir->i_sb, &location, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005371 if (IS_ERR(inode))
5372 return inode;
5373
5374 /* Do extra check against inode mode with di_type */
5375 if (btrfs_inode_type(inode) != di_type) {
5376 btrfs_crit(fs_info,
5377"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5378 inode->i_mode, btrfs_inode_type(inode),
5379 di_type);
5380 iput(inode);
5381 return ERR_PTR(-EUCLEAN);
5382 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005383 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005384 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005385
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005386 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005387 &location, &sub_root);
5388 if (ret < 0) {
5389 if (ret != -ENOENT)
5390 inode = ERR_PTR(ret);
5391 else
5392 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5393 } else {
David Sterba4c66e0d2019-10-03 19:09:35 +02005394 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005395 }
Josef Bacik87270022020-01-24 09:32:31 -05005396 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005397 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005398
Julia Lawall34d19ba2011-01-24 19:55:19 +00005399 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005400 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005401 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005402 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005403 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005404 if (ret) {
5405 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005406 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005407 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005408 }
5409
Chris Mason3de45862008-11-17 21:02:50 -05005410 return inode;
5411}
5412
Nick Pigginfe15ce42011-01-07 17:49:23 +11005413static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005414{
5415 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005416 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005417
Li Zefan848cce02012-02-21 17:04:28 +08005418 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005419 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005420
Li Zefan848cce02012-02-21 17:04:28 +08005421 if (inode) {
5422 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005423 if (btrfs_root_refs(&root->root_item) == 0)
5424 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005425
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005426 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005427 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005428 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005429 return 0;
5430}
5431
Chris Mason3de45862008-11-17 21:02:50 -05005432static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005433 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005434{
Al Viro3837d202018-10-10 16:38:27 -04005435 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005436
Al Viro3837d202018-10-10 16:38:27 -04005437 if (inode == ERR_PTR(-ENOENT))
5438 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005439 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005440}
5441
Josef Bacik23b5ec72017-07-24 15:14:25 -04005442/*
5443 * All this infrastructure exists because dir_emit can fault, and we are holding
5444 * the tree lock when doing readdir. For now just allocate a buffer and copy
5445 * our information into that, and then dir_emit from the buffer. This is
5446 * similar to what NFS does, only we don't keep the buffer around in pagecache
5447 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5448 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5449 * tree lock.
5450 */
5451static int btrfs_opendir(struct inode *inode, struct file *file)
5452{
5453 struct btrfs_file_private *private;
5454
5455 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5456 if (!private)
5457 return -ENOMEM;
5458 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5459 if (!private->filldir_buf) {
5460 kfree(private);
5461 return -ENOMEM;
5462 }
5463 file->private_data = private;
5464 return 0;
5465}
5466
5467struct dir_entry {
5468 u64 ino;
5469 u64 offset;
5470 unsigned type;
5471 int name_len;
5472};
5473
5474static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5475{
5476 while (entries--) {
5477 struct dir_entry *entry = addr;
5478 char *name = (char *)(entry + 1);
5479
David Sterba92d32172018-04-16 21:10:14 +02005480 ctx->pos = get_unaligned(&entry->offset);
5481 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5482 get_unaligned(&entry->ino),
5483 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005484 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005485 addr += sizeof(struct dir_entry) +
5486 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005487 ctx->pos++;
5488 }
5489 return 0;
5490}
5491
Al Viro9cdda8d2013-05-22 16:48:09 -04005492static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005493{
Al Viro9cdda8d2013-05-22 16:48:09 -04005494 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005495 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005496 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005497 struct btrfs_dir_item *di;
5498 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005499 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005500 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005501 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005502 struct list_head ins_list;
5503 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005504 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005505 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005506 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005507 char *name_ptr;
5508 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005509 int entries = 0;
5510 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005511 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005512 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005513
Al Viro9cdda8d2013-05-22 16:48:09 -04005514 if (!dir_emit_dots(file, ctx))
5515 return 0;
5516
David Woodhouse49593bf2008-08-17 17:08:36 +01005517 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005518 if (!path)
5519 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005520
Josef Bacik23b5ec72017-07-24 15:14:25 -04005521 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005522 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005523
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005524 INIT_LIST_HEAD(&ins_list);
5525 INIT_LIST_HEAD(&del_list);
5526 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005527
Josef Bacik23b5ec72017-07-24 15:14:25 -04005528again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005529 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005530 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005531 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005532
Chris Mason39279cc2007-06-12 06:35:45 -04005533 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5534 if (ret < 0)
5535 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005536
5537 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005538 struct dir_entry *entry;
5539
Chris Mason5f39d392007-10-15 16:14:19 -04005540 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005541 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005542 if (slot >= btrfs_header_nritems(leaf)) {
5543 ret = btrfs_next_leaf(root, path);
5544 if (ret < 0)
5545 goto err;
5546 else if (ret > 0)
5547 break;
5548 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005549 }
Chris Mason3de45862008-11-17 21:02:50 -05005550
Chris Mason5f39d392007-10-15 16:14:19 -04005551 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5552
5553 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005554 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005555 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005556 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005557 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005558 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005559 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005560 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005561 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005562 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005563 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5564 PAGE_SIZE) {
5565 btrfs_release_path(path);
5566 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5567 if (ret)
5568 goto nopos;
5569 addr = private->filldir_buf;
5570 entries = 0;
5571 total_len = 0;
5572 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005573 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005574
5575 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005576 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005577 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005578 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5579 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005580 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005581 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005582 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005583 put_unaligned(location.objectid, &entry->ino);
5584 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005585 entries++;
5586 addr += sizeof(struct dir_entry) + name_len;
5587 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005588next:
5589 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005590 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005591 btrfs_release_path(path);
5592
5593 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5594 if (ret)
5595 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005596
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005597 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005598 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005599 goto nopos;
5600
Zach Browndb62efb2013-07-11 16:19:42 -07005601 /*
5602 * Stop new entries from being returned after we return the last
5603 * entry.
5604 *
5605 * New directory entries are assigned a strictly increasing
5606 * offset. This means that new entries created during readdir
5607 * are *guaranteed* to be seen in the future by that readdir.
5608 * This has broken buggy programs which operate on names as
5609 * they're returned by readdir. Until we re-use freed offsets
5610 * we have this hack to stop new entries from being returned
5611 * under the assumption that they'll never reach this huge
5612 * offset.
5613 *
5614 * This is being careful not to overflow 32bit loff_t unless the
5615 * last entry requires it because doing so has broken 32bit apps
5616 * in the past.
5617 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005618 if (ctx->pos >= INT_MAX)
5619 ctx->pos = LLONG_MAX;
5620 else
5621 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005622nopos:
5623 ret = 0;
5624err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005625 if (put)
5626 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005627 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005628 return ret;
5629}
5630
Chris Mason39279cc2007-06-12 06:35:45 -04005631/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005632 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005633 * inode changes. But, it is most likely to find the inode in cache.
5634 * FIXME, needs more benchmarking...there are no reasons other than performance
5635 * to keep or drop this code.
5636 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005637static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005638{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005639 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005640 struct btrfs_root *root = BTRFS_I(inode)->root;
5641 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005642 int ret;
5643
Josef Bacik72ac3c02012-05-23 14:13:11 -04005644 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005645 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005646
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005647 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005648 if (IS_ERR(trans))
5649 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005650
5651 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005652 if (ret && ret == -ENOSPC) {
5653 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005654 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005655 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005656 if (IS_ERR(trans))
5657 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005658
Chris Mason94b60442010-05-26 11:02:00 -04005659 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005660 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005661 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005662 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005663 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005664
5665 return ret;
5666}
5667
5668/*
5669 * This is a copy of file_update_time. We need this so we can return error on
5670 * ENOSPC for updating the inode in the case of file write and mmap writes.
5671 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005672static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005673 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005674{
Alexander Block2bc5565282012-06-15 09:49:33 +02005675 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005676 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005677
5678 if (btrfs_root_readonly(root))
5679 return -EROFS;
5680
Josef Bacike41f9412012-03-26 09:46:47 -04005681 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005682 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005683 if (flags & S_CTIME)
5684 inode->i_ctime = *now;
5685 if (flags & S_MTIME)
5686 inode->i_mtime = *now;
5687 if (flags & S_ATIME)
5688 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005689 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005690}
5691
Chris Masond352ac62008-09-29 15:18:18 -04005692/*
5693 * find the highest existing sequence number in a directory
5694 * and then set the in-memory index_cnt variable to reflect
5695 * free sequence numbers
5696 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005697static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005698{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005699 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005700 struct btrfs_key key, found_key;
5701 struct btrfs_path *path;
5702 struct extent_buffer *leaf;
5703 int ret;
5704
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005705 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005706 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005707 key.offset = (u64)-1;
5708
5709 path = btrfs_alloc_path();
5710 if (!path)
5711 return -ENOMEM;
5712
5713 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5714 if (ret < 0)
5715 goto out;
5716 /* FIXME: we should be able to handle this */
5717 if (ret == 0)
5718 goto out;
5719 ret = 0;
5720
5721 /*
5722 * MAGIC NUMBER EXPLANATION:
5723 * since we search a directory based on f_pos we have to start at 2
5724 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5725 * else has to start at 2
5726 */
5727 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005728 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005729 goto out;
5730 }
5731
5732 path->slots[0]--;
5733
5734 leaf = path->nodes[0];
5735 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5736
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005737 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005738 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005739 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005740 goto out;
5741 }
5742
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005743 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005744out:
5745 btrfs_free_path(path);
5746 return ret;
5747}
5748
Chris Masond352ac62008-09-29 15:18:18 -04005749/*
5750 * helper to find a free sequence number in a given directory. This current
5751 * code is very simple, later versions will do smarter things in the btree
5752 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005753int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005754{
5755 int ret = 0;
5756
Nikolay Borisov877574e2017-02-20 13:50:33 +02005757 if (dir->index_cnt == (u64)-1) {
5758 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005759 if (ret) {
5760 ret = btrfs_set_inode_index_count(dir);
5761 if (ret)
5762 return ret;
5763 }
Josef Bacikaec74772008-07-24 12:12:38 -04005764 }
5765
Nikolay Borisov877574e2017-02-20 13:50:33 +02005766 *index = dir->index_cnt;
5767 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005768
5769 return ret;
5770}
5771
Chris Masonb0d5d102014-09-08 13:08:51 -07005772static int btrfs_insert_inode_locked(struct inode *inode)
5773{
5774 struct btrfs_iget_args args;
5775 args.location = &BTRFS_I(inode)->location;
5776 args.root = BTRFS_I(inode)->root;
5777
5778 return insert_inode_locked4(inode,
5779 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5780 btrfs_find_actor, &args);
5781}
5782
Anand Jain19aee8d2017-07-18 17:37:05 +08005783/*
5784 * Inherit flags from the parent inode.
5785 *
5786 * Currently only the compression flags and the cow flags are inherited.
5787 */
5788static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5789{
5790 unsigned int flags;
5791
5792 if (!dir)
5793 return;
5794
5795 flags = BTRFS_I(dir)->flags;
5796
5797 if (flags & BTRFS_INODE_NOCOMPRESS) {
5798 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5799 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5800 } else if (flags & BTRFS_INODE_COMPRESS) {
5801 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5802 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5803 }
5804
5805 if (flags & BTRFS_INODE_NODATACOW) {
5806 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5807 if (S_ISREG(inode->i_mode))
5808 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5809 }
5810
David Sterba7b6a2212018-03-26 18:40:21 +02005811 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005812}
5813
Chris Mason39279cc2007-06-12 06:35:45 -04005814static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5815 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005816 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005817 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005818 u64 ref_objectid, u64 objectid,
5819 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005820{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005821 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005822 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005823 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005824 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005825 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005826 struct btrfs_inode_ref *ref;
5827 struct btrfs_key key[2];
5828 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005829 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005830 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005831 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005832 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005833
Chris Mason5f39d392007-10-15 16:14:19 -04005834 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005835 if (!path)
5836 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005837
Josef Bacik11a19a92019-09-09 10:12:04 -04005838 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005839 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005840 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005841 if (!inode) {
5842 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005843 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005844 }
Chris Mason39279cc2007-06-12 06:35:45 -04005845
Li Zefan581bb052011-04-20 10:06:11 +08005846 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005847 * O_TMPFILE, set link count to 0, so that after this point,
5848 * we fill in an inode item with the correct link count.
5849 */
5850 if (!name)
5851 set_nlink(inode, 0);
5852
5853 /*
Li Zefan581bb052011-04-20 10:06:11 +08005854 * we have to initialize this early, so we can reclaim the inode
5855 * number if we fail afterwards in this function.
5856 */
5857 inode->i_ino = objectid;
5858
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005859 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005860 trace_btrfs_inode_request(dir);
5861
Nikolay Borisov877574e2017-02-20 13:50:33 +02005862 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005863 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005864 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005865 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005866 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005867 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005868 } else if (dir) {
5869 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005870 }
5871 /*
5872 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005873 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005874 * number
5875 */
5876 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005877 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005878 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005879 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005880 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04005881
Josef Bacik5dc562c2012-08-17 13:14:17 -04005882 /*
5883 * We could have gotten an inode number from somebody who was fsynced
5884 * and then removed in this same transaction, so let's just set full
5885 * sync since it will be a full sync anyway and this will blow away the
5886 * old info in the log.
5887 */
5888 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5889
Chris Mason9c583092008-01-29 15:15:18 -05005890 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005891 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005892 key[0].offset = 0;
5893
Chris Mason9c583092008-01-29 15:15:18 -05005894 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005895
5896 if (name) {
5897 /*
5898 * Start new inodes with an inode_ref. This is slightly more
5899 * efficient for small numbers of hard links since they will
5900 * be packed into one item. Extended refs will kick in if we
5901 * add more hard links than can fit in the ref item.
5902 */
5903 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005904 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005905 key[1].offset = ref_objectid;
5906
5907 sizes[1] = name_len + sizeof(*ref);
5908 }
Chris Mason9c583092008-01-29 15:15:18 -05005909
Chris Masonb0d5d102014-09-08 13:08:51 -07005910 location = &BTRFS_I(inode)->location;
5911 location->objectid = objectid;
5912 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005913 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005914
5915 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04005916 if (ret < 0) {
5917 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005918 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04005919 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005920
Chris Masonb9473432009-03-13 11:00:37 -04005921 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005922 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005923 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005924 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005925
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005926 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005927 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05305928
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005929 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305930 inode->i_atime = inode->i_mtime;
5931 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005932 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05305933
Chris Mason5f39d392007-10-15 16:14:19 -04005934 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5935 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01005936 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06005937 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005938 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005939
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005940 if (name) {
5941 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5942 struct btrfs_inode_ref);
5943 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5944 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5945 ptr = (unsigned long)(ref + 1);
5946 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5947 }
Chris Mason9c583092008-01-29 15:15:18 -05005948
Chris Mason5f39d392007-10-15 16:14:19 -04005949 btrfs_mark_buffer_dirty(path->nodes[0]);
5950 btrfs_free_path(path);
5951
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005952 btrfs_inherit_iflags(inode, dir);
5953
Al Viro569254b2011-07-24 17:08:40 -04005954 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005955 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04005956 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005957 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005958 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5959 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005960 }
5961
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005962 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005963
5964 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005965 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005966
Alexander Block8ea05e32012-07-25 17:35:53 +02005967 btrfs_update_root_times(trans, root);
5968
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005969 ret = btrfs_inode_inherit_props(trans, inode, dir);
5970 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005971 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005972 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01005973 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005974
Chris Mason39279cc2007-06-12 06:35:45 -04005975 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005976
5977fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04005978 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005979fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005980 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005981 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005982 btrfs_free_path(path);
5983 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005984}
5985
Chris Masond352ac62008-09-29 15:18:18 -04005986/*
5987 * utility function to add 'inode' into 'parent_inode' with
5988 * a give name and a given sequence number.
5989 * if 'add_backref' is true, also insert a backref from the
5990 * inode to the parent directory.
5991 */
Chris Masone02119d2008-09-05 16:13:11 -04005992int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005993 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04005994 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005995{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005996 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005997 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005998 struct btrfs_root *root = parent_inode->root;
5999 u64 ino = btrfs_ino(inode);
6000 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006001
Li Zefan33345d012011-04-20 10:31:50 +08006002 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006003 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006004 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006005 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006006 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006007 key.offset = 0;
6008 }
Chris Mason39279cc2007-06-12 06:35:45 -04006009
Li Zefan33345d012011-04-20 10:31:50 +08006010 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006011 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006012 root->root_key.objectid, parent_ino,
6013 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006014 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006015 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6016 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006017 }
6018
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006019 /* Nothing to clean up yet */
6020 if (ret)
6021 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006022
Lu Fengqi684572d2018-08-04 21:10:57 +08006023 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006024 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006025 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006026 goto fail_dir_item;
6027 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006028 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006029 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006030 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006031
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006032 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006033 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006034 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006035 /*
6036 * If we are replaying a log tree, we do not want to update the mtime
6037 * and ctime of the parent directory with the current time, since the
6038 * log replay procedure is responsible for setting them to their correct
6039 * values (the ones it had when the fsync was done).
6040 */
6041 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6042 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6043
6044 parent_inode->vfs_inode.i_mtime = now;
6045 parent_inode->vfs_inode.i_ctime = now;
6046 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006047 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006048 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006049 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006050 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006051
6052fail_dir_item:
6053 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6054 u64 local_index;
6055 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006056 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006057 root->root_key.objectid, parent_ino,
6058 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006059 if (err)
6060 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006061 } else if (add_backref) {
6062 u64 local_index;
6063 int err;
6064
6065 err = btrfs_del_inode_ref(trans, root, name, name_len,
6066 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006067 if (err)
6068 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006069 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006070
6071 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006072 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006073}
6074
6075static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006076 struct btrfs_inode *dir, struct dentry *dentry,
6077 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006078{
Josef Bacika1b075d2010-11-19 20:36:11 +00006079 int err = btrfs_add_link(trans, dir, inode,
6080 dentry->d_name.name, dentry->d_name.len,
6081 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006082 if (err > 0)
6083 err = -EEXIST;
6084 return err;
6085}
6086
Josef Bacik618e21d2007-07-11 10:18:17 -04006087static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006088 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006089{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006090 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006091 struct btrfs_trans_handle *trans;
6092 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006093 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006094 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006095 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006096 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006097
Josef Bacik9ed74f22009-09-11 16:12:44 -04006098 /*
6099 * 2 for inode item and ref
6100 * 2 for dir items
6101 * 1 for xattr if selinux is on
6102 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006103 trans = btrfs_start_transaction(root, 5);
6104 if (IS_ERR(trans))
6105 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006106
Li Zefan581bb052011-04-20 10:06:11 +08006107 err = btrfs_find_free_ino(root, &objectid);
6108 if (err)
6109 goto out_unlock;
6110
Josef Bacikaec74772008-07-24 12:12:38 -04006111 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006112 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6113 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006114 if (IS_ERR(inode)) {
6115 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006116 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006117 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006118 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006119
Casey Schauflerad19db72011-12-15 10:09:07 -05006120 /*
6121 * If the active LSM wants to access the inode during
6122 * d_instantiate it needs these. Smack checks to see
6123 * if the filesystem supports xattrs by looking at the
6124 * ops vector.
6125 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006126 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006127 init_special_inode(inode, inode->i_mode, rdev);
6128
6129 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006130 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006131 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006132
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006133 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6134 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006135 if (err)
6136 goto out_unlock;
6137
6138 btrfs_update_inode(trans, root, inode);
6139 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006140
Josef Bacik618e21d2007-07-11 10:18:17 -04006141out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006142 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006143 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006144 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006145 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006146 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006147 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006148 return err;
6149}
6150
Chris Mason39279cc2007-06-12 06:35:45 -04006151static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006152 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006153{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006154 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006155 struct btrfs_trans_handle *trans;
6156 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006157 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006158 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006159 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006160 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006161
Josef Bacik9ed74f22009-09-11 16:12:44 -04006162 /*
6163 * 2 for inode item and ref
6164 * 2 for dir items
6165 * 1 for xattr if selinux is on
6166 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006167 trans = btrfs_start_transaction(root, 5);
6168 if (IS_ERR(trans))
6169 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006170
Li Zefan581bb052011-04-20 10:06:11 +08006171 err = btrfs_find_free_ino(root, &objectid);
6172 if (err)
6173 goto out_unlock;
6174
Josef Bacikaec74772008-07-24 12:12:38 -04006175 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006176 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6177 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006178 if (IS_ERR(inode)) {
6179 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006180 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006181 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006182 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006183 /*
6184 * If the active LSM wants to access the inode during
6185 * d_instantiate it needs these. Smack checks to see
6186 * if the filesystem supports xattrs by looking at the
6187 * ops vector.
6188 */
6189 inode->i_fop = &btrfs_file_operations;
6190 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006191 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006192
6193 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6194 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006195 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006196
6197 err = btrfs_update_inode(trans, root, inode);
6198 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006199 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006200
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006201 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6202 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006203 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006204 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006205
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006206 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006207 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006208
Chris Mason39279cc2007-06-12 06:35:45 -04006209out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006210 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006211 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006212 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006213 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006214 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006215 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006216 return err;
6217}
6218
6219static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6220 struct dentry *dentry)
6221{
Filipe Manana271dba452016-01-05 16:24:05 +00006222 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006223 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006224 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006225 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006226 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006227 int err;
6228 int drop_inode = 0;
6229
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006230 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006231 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006232 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006233
Mark Fashehf1863732012-08-08 11:32:27 -07006234 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006235 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006236
Nikolay Borisov877574e2017-02-20 13:50:33 +02006237 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006238 if (err)
6239 goto fail;
6240
Yan, Zhenga22285a2010-05-16 10:48:46 -04006241 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006242 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006243 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006244 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006245 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006246 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006247 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006248 if (IS_ERR(trans)) {
6249 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006250 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006251 goto fail;
6252 }
Chris Mason5f39d392007-10-15 16:14:19 -04006253
Miao Xie67de1172013-12-26 13:07:06 +08006254 /* There are several dir indexes for this inode, clear the cache. */
6255 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006256 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006257 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006258 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006259 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006260 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006261
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006262 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6263 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006264
Yan, Zhenga5719522009-09-24 09:17:31 -04006265 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006266 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006267 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006268 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006269 int ret;
6270
Yan, Zhenga5719522009-09-24 09:17:31 -04006271 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006272 if (err)
6273 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006274 if (inode->i_nlink == 1) {
6275 /*
6276 * If new hard link count is 1, it's a file created
6277 * with open(2) O_TMPFILE flag.
6278 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006279 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006280 if (err)
6281 goto fail;
6282 }
Al Viro08c422c2011-12-23 07:58:13 -05006283 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006284 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6285 true, NULL);
6286 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6287 err = btrfs_commit_transaction(trans);
6288 trans = NULL;
6289 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006290 }
Chris Mason39279cc2007-06-12 06:35:45 -04006291
Chris Mason1832a6d2007-12-21 16:27:21 -05006292fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006293 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006294 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006295 if (drop_inode) {
6296 inode_dec_link_count(inode);
6297 iput(inode);
6298 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006299 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006300 return err;
6301}
6302
Al Viro18bb1db2011-07-26 01:41:39 -04006303static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006304{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006305 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006306 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006307 struct btrfs_trans_handle *trans;
6308 struct btrfs_root *root = BTRFS_I(dir)->root;
6309 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006310 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006311 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006312
Josef Bacik9ed74f22009-09-11 16:12:44 -04006313 /*
6314 * 2 items for inode and ref
6315 * 2 items for dir items
6316 * 1 for xattr if selinux is on
6317 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006318 trans = btrfs_start_transaction(root, 5);
6319 if (IS_ERR(trans))
6320 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006321
Li Zefan581bb052011-04-20 10:06:11 +08006322 err = btrfs_find_free_ino(root, &objectid);
6323 if (err)
6324 goto out_fail;
6325
Josef Bacikaec74772008-07-24 12:12:38 -04006326 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006327 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6328 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006329 if (IS_ERR(inode)) {
6330 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006331 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006332 goto out_fail;
6333 }
Chris Mason5f39d392007-10-15 16:14:19 -04006334
Chris Masonb0d5d102014-09-08 13:08:51 -07006335 /* these must be set before we unlock the inode */
6336 inode->i_op = &btrfs_dir_inode_operations;
6337 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006338
Eric Paris2a7dba32011-02-01 11:05:39 -05006339 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006340 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006341 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006342
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006343 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006344 err = btrfs_update_inode(trans, root, inode);
6345 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006346 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006347
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006348 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6349 dentry->d_name.name,
6350 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006351 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006352 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006353
Al Viro1e2e5472018-05-04 08:23:01 -04006354 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006355
6356out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006357 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006358 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006359 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006360 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006361 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006362 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006363 return err;
6364}
6365
Chris Masonc8b97812008-10-29 14:49:59 -04006366static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006367 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006368 size_t pg_offset, u64 extent_offset,
6369 struct btrfs_file_extent_item *item)
6370{
6371 int ret;
6372 struct extent_buffer *leaf = path->nodes[0];
6373 char *tmp;
6374 size_t max_size;
6375 unsigned long inline_size;
6376 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006377 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006378
6379 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006380 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006381 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6382 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006383 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006384 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006385 if (!tmp)
6386 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006387 ptr = btrfs_file_extent_inline_start(item);
6388
6389 read_extent_buffer(leaf, tmp, ptr, inline_size);
6390
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006391 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006392 ret = btrfs_decompress(compress_type, tmp, page,
6393 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006394
6395 /*
6396 * decompression code contains a memset to fill in any space between the end
6397 * of the uncompressed data and the end of max_size in case the decompressed
6398 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6399 * the end of an inline extent and the beginning of the next block, so we
6400 * cover that region here.
6401 */
6402
6403 if (max_size + pg_offset < PAGE_SIZE) {
6404 char *map = kmap(page);
6405 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6406 kunmap(page);
6407 }
Chris Masonc8b97812008-10-29 14:49:59 -04006408 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006409 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006410}
6411
Omar Sandoval39b07b52019-12-02 17:34:23 -08006412/**
6413 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6414 * @inode: file to search in
6415 * @page: page to read extent data into if the extent is inline
6416 * @pg_offset: offset into @page to copy to
6417 * @start: file offset
6418 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006419 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006420 * This returns the first &struct extent_map which overlaps with the given
6421 * range, reading it from the B-tree and caching it if necessary. Note that
6422 * there may be more extents which overlap the given range after the returned
6423 * extent_map.
6424 *
6425 * If @page is not NULL and the extent is inline, this also reads the extent
6426 * data directly into the page and marks the extent up to date in the io_tree.
6427 *
6428 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006429 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006430struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006431 struct page *page, size_t pg_offset,
6432 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006433{
David Sterba3ffbd682018-06-29 10:56:42 +02006434 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006435 int ret;
6436 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006437 u64 extent_start = 0;
6438 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006439 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006440 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006441 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006442 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006443 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006444 struct extent_buffer *leaf;
6445 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006446 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006447 struct extent_map_tree *em_tree = &inode->extent_tree;
6448 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006449
Chris Mason890871b2009-09-02 16:24:52 -04006450 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006451 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006452 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006453
Chris Masona52d9a82007-08-27 16:49:44 -04006454 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006455 if (em->start > start || em->start + em->len <= start)
6456 free_extent_map(em);
6457 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006458 free_extent_map(em);
6459 else
6460 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006461 }
David Sterba172ddd62011-04-21 00:48:27 +02006462 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006463 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006464 err = -ENOMEM;
6465 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006466 }
Chris Masond1310b22008-01-24 16:13:08 -05006467 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006468 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006469 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006470 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006471
Liu Bobee6ec82018-08-17 05:05:28 +08006472 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006473 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006474 err = -ENOMEM;
6475 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006476 }
6477
Liu Bobee6ec82018-08-17 05:05:28 +08006478 /* Chances are we'll be called again, so go ahead and do readahead */
6479 path->reada = READA_FORWARD;
6480
Liu Boe49aabd2018-08-25 13:47:09 +08006481 /*
6482 * Unless we're going to uncompress the inline extent, no sleep would
6483 * happen.
6484 */
6485 path->leave_spinning = 1;
6486
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006487 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006488 if (ret < 0) {
6489 err = ret;
6490 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006491 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006492 if (path->slots[0] == 0)
6493 goto not_found;
6494 path->slots[0]--;
6495 }
6496
Chris Mason5f39d392007-10-15 16:14:19 -04006497 leaf = path->nodes[0];
6498 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006499 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006500 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006501 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006502 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006503 /*
6504 * If we backup past the first extent we want to move forward
6505 * and see if there is an extent in front of us, otherwise we'll
6506 * say there is a hole for our whole search range which can
6507 * cause problems.
6508 */
6509 extent_end = start;
6510 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006511 }
6512
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006513 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006514 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006515 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006516 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6517 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006518 /* Only regular file could have regular/prealloc extent */
6519 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6520 ret = -EUCLEAN;
6521 btrfs_crit(fs_info,
6522 "regular/prealloc extent found for non-regular inode %llu",
6523 btrfs_ino(inode));
6524 goto out;
6525 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006526 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6527 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006528 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006529 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6530 path->slots[0],
6531 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006532 }
Josef Bacik25a50342013-10-14 12:08:38 -04006533next:
Yan Zheng9036c102008-10-30 14:19:41 -04006534 if (start >= extent_end) {
6535 path->slots[0]++;
6536 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6537 ret = btrfs_next_leaf(root, path);
6538 if (ret < 0) {
6539 err = ret;
6540 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006541 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006542 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006543 }
Yan Zheng9036c102008-10-30 14:19:41 -04006544 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006545 }
Yan Zheng9036c102008-10-30 14:19:41 -04006546 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6547 if (found_key.objectid != objectid ||
6548 found_key.type != BTRFS_EXTENT_DATA_KEY)
6549 goto not_found;
6550 if (start + len <= found_key.offset)
6551 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006552 if (start > found_key.offset)
6553 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006554
6555 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006556 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006557 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006558 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006559 em->block_start = EXTENT_MAP_HOLE;
6560 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006561 }
6562
Omar Sandoval39b07b52019-12-02 17:34:23 -08006563 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006564
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006565 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6566 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006567 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006568 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006569 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006570 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006571 size_t size;
6572 size_t extent_offset;
6573 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006574
Omar Sandoval39b07b52019-12-02 17:34:23 -08006575 if (!page)
Yan689f9342007-10-29 11:41:07 -04006576 goto out;
Yan689f9342007-10-29 11:41:07 -04006577
Qu Wenruoe41ca582018-06-06 15:41:49 +08006578 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006579 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006580 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6581 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006582 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006583 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006584 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006585 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006586 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006587
6588 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006589 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006590 if (btrfs_file_extent_compression(leaf, item) !=
6591 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006592 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006593 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006594 if (ret) {
6595 err = ret;
6596 goto out;
6597 }
Chris Masonc8b97812008-10-29 14:49:59 -04006598 } else {
6599 map = kmap(page);
6600 read_extent_buffer(leaf, map + pg_offset, ptr,
6601 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006602 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006603 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006604 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006605 copy_size);
6606 }
Chris Masonc8b97812008-10-29 14:49:59 -04006607 kunmap(page);
6608 }
Chris Mason179e29e2007-11-01 11:28:41 -04006609 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006610 }
Chris Masond1310b22008-01-24 16:13:08 -05006611 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006612 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006613 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006614 }
6615not_found:
6616 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006617 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006618 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006619 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006620insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006621 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006622 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006623 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006624 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6625 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006626 err = -EIO;
6627 goto out;
6628 }
Chris Masond1310b22008-01-24 16:13:08 -05006629
6630 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006631 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006632 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006633 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006634out:
Liu Boc6414282018-08-23 07:36:17 +08006635 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006636
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006637 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006638
Chris Masona52d9a82007-08-27 16:49:44 -04006639 if (err) {
6640 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006641 return ERR_PTR(err);
6642 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006643 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006644 return em;
6645}
6646
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006647struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006648 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006649{
6650 struct extent_map *em;
6651 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006652 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006653 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006654 u64 delalloc_len;
6655 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006656 int err = 0;
6657
Omar Sandoval39b07b52019-12-02 17:34:23 -08006658 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006659 if (IS_ERR(em))
6660 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006661 /*
6662 * If our em maps to:
6663 * - a hole or
6664 * - a pre-alloc extent,
6665 * there might actually be delalloc bytes behind it.
6666 */
6667 if (em->block_start != EXTENT_MAP_HOLE &&
6668 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6669 return em;
6670 else
6671 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006672
6673 /* check to see if we've wrapped (len == -1 or similar) */
6674 end = start + len;
6675 if (end < start)
6676 end = (u64)-1;
6677 else
6678 end -= 1;
6679
6680 em = NULL;
6681
6682 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006683 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006684 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006685 delalloc_end = delalloc_start + delalloc_len;
6686 if (delalloc_end < delalloc_start)
6687 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006688
6689 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006690 * We didn't find anything useful, return the original results from
6691 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006692 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006693 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006694 em = hole_em;
6695 hole_em = NULL;
6696 goto out;
6697 }
6698
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006699 /*
6700 * Adjust the delalloc_start to make sure it doesn't go backwards from
6701 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006702 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006703 delalloc_start = max(start, delalloc_start);
6704 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006705
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006706 if (delalloc_len > 0) {
6707 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006708 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006709 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006710
David Sterba172ddd62011-04-21 00:48:27 +02006711 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006712 if (!em) {
6713 err = -ENOMEM;
6714 goto out;
6715 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006716
6717 ASSERT(hole_em);
6718 /*
6719 * When btrfs_get_extent can't find anything it returns one
6720 * huge hole
6721 *
6722 * Make sure what it found really fits our range, and adjust to
6723 * make sure it is based on the start from the caller
6724 */
6725 if (hole_end <= start || hole_em->start > end) {
6726 free_extent_map(hole_em);
6727 hole_em = NULL;
6728 } else {
6729 hole_start = max(hole_em->start, start);
6730 hole_len = hole_end - hole_start;
6731 }
6732
6733 if (hole_em && delalloc_start > hole_start) {
6734 /*
6735 * Our hole starts before our delalloc, so we have to
6736 * return just the parts of the hole that go until the
6737 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006738 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006739 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006740 em->start = hole_start;
6741 em->orig_start = hole_start;
6742 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006743 * Don't adjust block start at all, it is fixed at
6744 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006745 */
6746 em->block_start = hole_em->block_start;
6747 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006748 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6749 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006750 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006751 /*
6752 * Hole is out of passed range or it starts after
6753 * delalloc range
6754 */
6755 em->start = delalloc_start;
6756 em->len = delalloc_len;
6757 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006758 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006759 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006760 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006761 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006762 return hole_em;
6763 }
6764out:
6765
6766 free_extent_map(hole_em);
6767 if (err) {
6768 free_extent_map(em);
6769 return ERR_PTR(err);
6770 }
6771 return em;
6772}
6773
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006774static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
6775 const u64 start,
6776 const u64 len,
6777 const u64 orig_start,
6778 const u64 block_start,
6779 const u64 block_len,
6780 const u64 orig_block_len,
6781 const u64 ram_bytes,
6782 const int type)
6783{
6784 struct extent_map *em = NULL;
6785 int ret;
6786
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006787 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08006788 em = create_io_em(inode, start, len, orig_start,
6789 block_start, block_len, orig_block_len,
6790 ram_bytes,
6791 BTRFS_COMPRESS_NONE, /* compress_type */
6792 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006793 if (IS_ERR(em))
6794 goto out;
6795 }
6796 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
6797 len, block_len, type);
6798 if (ret) {
6799 if (em) {
6800 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02006801 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006802 start + len - 1, 0);
6803 }
6804 em = ERR_PTR(ret);
6805 }
6806 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006807
6808 return em;
6809}
6810
Josef Bacik4b46fce2010-05-23 11:00:55 -04006811static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6812 u64 start, u64 len)
6813{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006814 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006815 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006816 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006817 struct btrfs_key ins;
6818 u64 alloc_hint;
6819 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006820
Josef Bacik4b46fce2010-05-23 11:00:55 -04006821 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006822 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006823 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006824 if (ret)
6825 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006826
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006827 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
6828 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006829 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006830 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006831 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006832 btrfs_free_reserved_extent(fs_info, ins.objectid,
6833 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006834
Josef Bacik4b46fce2010-05-23 11:00:55 -04006835 return em;
6836}
6837
Chris Mason46bfbb52010-05-26 11:04:10 -04006838/*
6839 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6840 * block must be cow'd
6841 */
Josef Bacik00361582013-08-14 14:02:47 -04006842noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006843 u64 *orig_start, u64 *orig_block_len,
6844 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006845{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006846 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006847 struct btrfs_path *path;
6848 int ret;
6849 struct extent_buffer *leaf;
6850 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006851 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006852 struct btrfs_file_extent_item *fi;
6853 struct btrfs_key key;
6854 u64 disk_bytenr;
6855 u64 backref_offset;
6856 u64 extent_end;
6857 u64 num_bytes;
6858 int slot;
6859 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006860 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006861
Chris Mason46bfbb52010-05-26 11:04:10 -04006862 path = btrfs_alloc_path();
6863 if (!path)
6864 return -ENOMEM;
6865
David Sterbaf85b7372017-01-20 14:54:07 +01006866 ret = btrfs_lookup_file_extent(NULL, root, path,
6867 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006868 if (ret < 0)
6869 goto out;
6870
6871 slot = path->slots[0];
6872 if (ret == 1) {
6873 if (slot == 0) {
6874 /* can't find the item, must cow */
6875 ret = 0;
6876 goto out;
6877 }
6878 slot--;
6879 }
6880 ret = 0;
6881 leaf = path->nodes[0];
6882 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006883 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006884 key.type != BTRFS_EXTENT_DATA_KEY) {
6885 /* not our file or wrong item type, must cow */
6886 goto out;
6887 }
6888
6889 if (key.offset > offset) {
6890 /* Wrong offset, must cow */
6891 goto out;
6892 }
6893
6894 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6895 found_type = btrfs_file_extent_type(leaf, fi);
6896 if (found_type != BTRFS_FILE_EXTENT_REG &&
6897 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6898 /* not a regular extent, must cow */
6899 goto out;
6900 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006901
6902 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6903 goto out;
6904
Miao Xiee77751a2013-12-27 21:11:50 +08006905 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6906 if (extent_end <= offset)
6907 goto out;
6908
Chris Mason46bfbb52010-05-26 11:04:10 -04006909 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006910 if (disk_bytenr == 0)
6911 goto out;
6912
6913 if (btrfs_file_extent_compression(leaf, fi) ||
6914 btrfs_file_extent_encryption(leaf, fi) ||
6915 btrfs_file_extent_other_encoding(leaf, fi))
6916 goto out;
6917
Ethan Lien78d42952018-05-17 14:58:29 +08006918 /*
6919 * Do the same check as in btrfs_cross_ref_exist but without the
6920 * unnecessary search.
6921 */
6922 if (btrfs_file_extent_generation(leaf, fi) <=
6923 btrfs_root_last_snapshot(&root->root_item))
6924 goto out;
6925
Chris Mason46bfbb52010-05-26 11:04:10 -04006926 backref_offset = btrfs_file_extent_offset(leaf, fi);
6927
Josef Bacik7ee9e442013-06-21 16:37:03 -04006928 if (orig_start) {
6929 *orig_start = key.offset - backref_offset;
6930 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6931 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6932 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006933
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006934 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04006935 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006936
6937 num_bytes = min(offset + *len, extent_end) - offset;
6938 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6939 u64 range_end;
6940
Jeff Mahoneyda170662016-06-15 09:22:56 -04006941 range_end = round_up(offset + num_bytes,
6942 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08006943 ret = test_range_bit(io_tree, offset, range_end,
6944 EXTENT_DELALLOC, 0, NULL);
6945 if (ret) {
6946 ret = -EAGAIN;
6947 goto out;
6948 }
6949 }
6950
Josef Bacik1bda19e2013-10-18 12:10:36 -04006951 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006952
6953 /*
6954 * look for other files referencing this extent, if we
6955 * find any we must cow
6956 */
Josef Bacik00361582013-08-14 14:02:47 -04006957
Liu Boe4c3b2d2017-01-30 12:25:28 -08006958 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04006959 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04006960 if (ret) {
6961 ret = 0;
6962 goto out;
6963 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006964
6965 /*
6966 * adjust disk_bytenr and num_bytes to cover just the bytes
6967 * in this extent we are about to write. If there
6968 * are any csums in that range we have to cow in order
6969 * to keep the csums correct
6970 */
6971 disk_bytenr += backref_offset;
6972 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006973 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
6974 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04006975 /*
6976 * all of the above have passed, it is safe to overwrite this extent
6977 * without cow
6978 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006979 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006980 ret = 1;
6981out:
6982 btrfs_free_path(path);
6983 return ret;
6984}
6985
Josef Bacikeb838e72012-07-31 16:28:48 -04006986static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6987 struct extent_state **cached_state, int writing)
6988{
6989 struct btrfs_ordered_extent *ordered;
6990 int ret = 0;
6991
6992 while (1) {
6993 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01006994 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04006995 /*
6996 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04006997 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04006998 * extents in this range.
6999 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007000 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007001 lockend - lockstart + 1);
7002
7003 /*
7004 * We need to make sure there are no buffered pages in this
7005 * range either, we could have raced between the invalidate in
7006 * generic_file_direct_write and locking the extent. The
7007 * invalidate needs to happen so that reads after a write do not
7008 * get stale data.
7009 */
Alex Gartrellfc4adbf2014-05-20 13:07:56 -07007010 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007011 (!writing || !filemap_range_has_page(inode->i_mapping,
7012 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007013 break;
7014
7015 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007016 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007017
7018 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007019 /*
7020 * If we are doing a DIO read and the ordered extent we
7021 * found is for a buffered write, we can not wait for it
7022 * to complete and retry, because if we do so we can
7023 * deadlock with concurrent buffered writes on page
7024 * locks. This happens only if our DIO read covers more
7025 * than one extent map, if at this point has already
7026 * created an ordered extent for a previous extent map
7027 * and locked its range in the inode's io tree, and a
7028 * concurrent write against that previous extent map's
7029 * range and this range started (we unlock the ranges
7030 * in the io tree only when the bios complete and
7031 * buffered writes always lock pages before attempting
7032 * to lock range in the io tree).
7033 */
7034 if (writing ||
7035 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7036 btrfs_start_ordered_extent(inode, ordered, 1);
7037 else
7038 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007039 btrfs_put_ordered_extent(ordered);
7040 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007041 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007042 * We could trigger writeback for this range (and wait
7043 * for it to complete) and then invalidate the pages for
7044 * this range (through invalidate_inode_pages2_range()),
7045 * but that can lead us to a deadlock with a concurrent
7046 * call to readpages() (a buffered read or a defrag call
7047 * triggered a readahead) on a page lock due to an
7048 * ordered dio extent we created before but did not have
7049 * yet a corresponding bio submitted (whence it can not
7050 * complete), which makes readpages() wait for that
7051 * ordered extent to complete while holding a lock on
7052 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007053 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007054 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007055 }
7056
Filipe Mananaade77022016-02-18 14:28:55 +00007057 if (ret)
7058 break;
7059
Josef Bacikeb838e72012-07-31 16:28:48 -04007060 cond_resched();
7061 }
7062
7063 return ret;
7064}
7065
Liu Bo6f9994d2017-01-31 07:50:22 -08007066/* The callers of this must take lock_extent() */
7067static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7068 u64 orig_start, u64 block_start,
7069 u64 block_len, u64 orig_block_len,
7070 u64 ram_bytes, int compress_type,
7071 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007072{
7073 struct extent_map_tree *em_tree;
7074 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007075 int ret;
7076
Liu Bo6f9994d2017-01-31 07:50:22 -08007077 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7078 type == BTRFS_ORDERED_COMPRESSED ||
7079 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007080 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007081
Josef Bacik69ffb542012-09-11 15:40:07 -04007082 em_tree = &BTRFS_I(inode)->extent_tree;
7083 em = alloc_extent_map();
7084 if (!em)
7085 return ERR_PTR(-ENOMEM);
7086
7087 em->start = start;
7088 em->orig_start = orig_start;
7089 em->len = len;
7090 em->block_len = block_len;
7091 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007092 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007093 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007094 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007095 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007096 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007097 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007098 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007099 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7100 em->compress_type = compress_type;
7101 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007102
7103 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007104 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007105 em->start + em->len - 1, 0);
7106 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007107 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007108 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007109 /*
7110 * The caller has taken lock_extent(), who could race with us
7111 * to add em?
7112 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007113 } while (ret == -EEXIST);
7114
7115 if (ret) {
7116 free_extent_map(em);
7117 return ERR_PTR(ret);
7118 }
7119
Liu Bo6f9994d2017-01-31 07:50:22 -08007120 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007121 return em;
7122}
7123
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007124
7125static int btrfs_get_blocks_direct_read(struct extent_map *em,
7126 struct buffer_head *bh_result,
7127 struct inode *inode,
7128 u64 start, u64 len)
7129{
David Sterba8530c372019-08-30 15:42:07 +02007130 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7131
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007132 if (em->block_start == EXTENT_MAP_HOLE ||
7133 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7134 return -ENOENT;
7135
7136 len = min(len, em->len - (start - em->start));
7137
7138 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7139 inode->i_blkbits;
7140 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007141 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007142 set_buffer_mapped(bh_result);
7143
7144 return 0;
7145}
7146
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007147static int btrfs_get_blocks_direct_write(struct extent_map **map,
7148 struct buffer_head *bh_result,
7149 struct inode *inode,
7150 struct btrfs_dio_data *dio_data,
7151 u64 start, u64 len)
7152{
7153 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7154 struct extent_map *em = *map;
7155 int ret = 0;
7156
7157 /*
7158 * We don't allocate a new extent in the following cases
7159 *
7160 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7161 * existing extent.
7162 * 2) The extent is marked as PREALLOC. We're good to go here and can
7163 * just use the extent.
7164 *
7165 */
7166 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7167 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7168 em->block_start != EXTENT_MAP_HOLE)) {
7169 int type;
7170 u64 block_start, orig_start, orig_block_len, ram_bytes;
7171
7172 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7173 type = BTRFS_ORDERED_PREALLOC;
7174 else
7175 type = BTRFS_ORDERED_NOCOW;
7176 len = min(len, em->len - (start - em->start));
7177 block_start = em->block_start + (start - em->start);
7178
7179 if (can_nocow_extent(inode, start, &len, &orig_start,
7180 &orig_block_len, &ram_bytes) == 1 &&
7181 btrfs_inc_nocow_writers(fs_info, block_start)) {
7182 struct extent_map *em2;
7183
7184 em2 = btrfs_create_dio_extent(inode, start, len,
7185 orig_start, block_start,
7186 len, orig_block_len,
7187 ram_bytes, type);
7188 btrfs_dec_nocow_writers(fs_info, block_start);
7189 if (type == BTRFS_ORDERED_PREALLOC) {
7190 free_extent_map(em);
7191 *map = em = em2;
7192 }
7193
7194 if (em2 && IS_ERR(em2)) {
7195 ret = PTR_ERR(em2);
7196 goto out;
7197 }
7198 /*
7199 * For inode marked NODATACOW or extent marked PREALLOC,
7200 * use the existing or preallocated extent, so does not
7201 * need to adjust btrfs_space_info's bytes_may_use.
7202 */
7203 btrfs_free_reserved_data_space_noquota(inode, start,
7204 len);
7205 goto skip_cow;
7206 }
7207 }
7208
7209 /* this will cow the extent */
7210 len = bh_result->b_size;
7211 free_extent_map(em);
7212 *map = em = btrfs_new_extent_direct(inode, start, len);
7213 if (IS_ERR(em)) {
7214 ret = PTR_ERR(em);
7215 goto out;
7216 }
7217
7218 len = min(len, em->len - (start - em->start));
7219
7220skip_cow:
7221 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7222 inode->i_blkbits;
7223 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007224 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007225 set_buffer_mapped(bh_result);
7226
7227 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7228 set_buffer_new(bh_result);
7229
7230 /*
7231 * Need to update the i_size under the extent lock so buffered
7232 * readers will get the updated i_size when we unlock.
7233 */
7234 if (!dio_data->overwrite && start + len > i_size_read(inode))
7235 i_size_write(inode, start + len);
7236
7237 WARN_ON(dio_data->reserve < len);
7238 dio_data->reserve -= len;
7239 dio_data->unsubmitted_oe_range_end = start + len;
7240 current->journal_info = dio_data;
7241out:
7242 return ret;
7243}
7244
Josef Bacik4b46fce2010-05-23 11:00:55 -04007245static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7246 struct buffer_head *bh_result, int create)
7247{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007249 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007250 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307251 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007252 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007253 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007254 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007255 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007256
Omar Sandovale1821632019-08-15 14:04:04 -07007257 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007258 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007259
Josef Bacikc3298612012-08-03 16:49:19 -04007260 lockstart = start;
7261 lockend = start + len - 1;
7262
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007263 if (current->journal_info) {
7264 /*
7265 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007266 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007267 * confused.
7268 */
chandan50745b02015-08-28 21:10:13 +05307269 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007270 current->journal_info = NULL;
7271 }
7272
Josef Bacikeb838e72012-07-31 16:28:48 -04007273 /*
7274 * If this errors out it's because we couldn't invalidate pagecache for
7275 * this range and we need to fallback to buffered.
7276 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007277 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7278 create)) {
7279 ret = -ENOTBLK;
7280 goto err;
7281 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007282
Omar Sandoval39b07b52019-12-02 17:34:23 -08007283 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007284 if (IS_ERR(em)) {
7285 ret = PTR_ERR(em);
7286 goto unlock_err;
7287 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007288
7289 /*
7290 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7291 * io. INLINE is special, and we could probably kludge it in here, but
7292 * it's still buffered so for safety lets just fall back to the generic
7293 * buffered path.
7294 *
7295 * For COMPRESSED we _have_ to read the entire extent in so we can
7296 * decompress it, so there will be buffering required no matter what we
7297 * do, so go ahead and fallback to buffered.
7298 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007299 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007300 * to buffered IO. Don't blame me, this is the price we pay for using
7301 * the generic code.
7302 */
7303 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7304 em->block_start == EXTENT_MAP_INLINE) {
7305 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007306 ret = -ENOTBLK;
7307 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007308 }
7309
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007310 if (create) {
7311 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7312 dio_data, start, len);
7313 if (ret < 0)
7314 goto unlock_err;
7315
Omar Sandovale1821632019-08-15 14:04:04 -07007316 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7317 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007318 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007319 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7320 start, len);
7321 /* Can be negative only if we read from a hole */
7322 if (ret < 0) {
7323 ret = 0;
7324 free_extent_map(em);
7325 goto unlock_err;
7326 }
7327 /*
7328 * We need to unlock only the end area that we aren't using.
7329 * The rest is going to be unlocked by the endio routine.
7330 */
7331 lockstart = start + bh_result->b_size;
7332 if (lockstart < lockend) {
Omar Sandovale1821632019-08-15 14:04:04 -07007333 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7334 lockstart, lockend, &cached_state);
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007335 } else {
7336 free_extent_state(cached_state);
7337 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007338 }
7339
Josef Bacik4b46fce2010-05-23 11:00:55 -04007340 free_extent_map(em);
7341
7342 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007343
7344unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007345 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7346 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007347err:
chandan50745b02015-08-28 21:10:13 +05307348 if (dio_data)
7349 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007350 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007351}
7352
Omar Sandoval769b4f22020-04-16 14:46:22 -07007353static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7354{
7355 /*
7356 * This implies a barrier so that stores to dio_bio->bi_status before
7357 * this and loads of dio_bio->bi_status after this are fully ordered.
7358 */
7359 if (!refcount_dec_and_test(&dip->refs))
7360 return;
7361
7362 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
7363 __endio_write_update_ordered(dip->inode, dip->logical_offset,
7364 dip->bytes,
7365 !dip->dio_bio->bi_status);
7366 } else {
7367 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7368 dip->logical_offset,
7369 dip->logical_offset + dip->bytes - 1);
7370 }
7371
7372 dio_end_io(dip->dio_bio);
7373 kfree(dip);
7374}
7375
Omar Sandoval77d5d682020-04-16 14:46:25 -07007376static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7377 int mirror_num,
7378 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007379{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007380 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007381 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007382 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007383
Mike Christie37226b22016-06-05 14:31:52 -05007384 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007385
Omar Sandoval5c047a62020-04-16 14:46:24 -07007386 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007387 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007388 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007389
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007390 refcount_inc(&dip->refs);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007391 ret = btrfs_map_bio(fs_info, bio, mirror_num);
7392 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007393 refcount_dec(&dip->refs);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007394 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007395}
7396
Omar Sandoval769b4f22020-04-16 14:46:22 -07007397static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7398 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007399 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007400{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007401 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7402 const u32 sectorsize = fs_info->sectorsize;
7403 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
7404 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7405 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7406 struct bio_vec bvec;
7407 struct bvec_iter iter;
7408 u64 start = io_bio->logical;
7409 int icsum = 0;
7410 blk_status_t err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007411
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007412 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7413 unsigned int i, nr_sectors, pgoff;
7414
7415 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7416 pgoff = bvec.bv_offset;
7417 for (i = 0; i < nr_sectors; i++) {
7418 ASSERT(pgoff < PAGE_SIZE);
7419 if (uptodate &&
7420 (!csum || !check_data_csum(inode, io_bio, icsum,
7421 bvec.bv_page, pgoff,
7422 start, sectorsize))) {
7423 clean_io_failure(fs_info, failure_tree, io_tree,
7424 start, bvec.bv_page,
7425 btrfs_ino(BTRFS_I(inode)),
7426 pgoff);
7427 } else {
7428 blk_status_t status;
7429
Omar Sandoval77d5d682020-04-16 14:46:25 -07007430 status = btrfs_submit_read_repair(inode,
7431 &io_bio->bio,
7432 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007433 bvec.bv_page, pgoff,
7434 start,
7435 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007436 io_bio->mirror_num,
7437 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007438 if (status)
7439 err = status;
7440 }
7441 start += sectorsize;
7442 icsum++;
7443 pgoff += sectorsize;
7444 }
Miao Xie8b110e32014-09-12 18:44:03 +08007445 }
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007446 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007447}
7448
Qu Wenruo524272602017-03-08 10:25:52 +08007449static void __endio_write_update_ordered(struct inode *inode,
7450 const u64 offset, const u64 bytes,
7451 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007452{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007453 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007454 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007455 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007456 u64 ordered_offset = offset;
7457 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007458 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007459
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007460 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08007461 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007462 else
Qu Wenruo524272602017-03-08 10:25:52 +08007463 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007464
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007465 while (ordered_offset < offset + bytes) {
7466 last_offset = ordered_offset;
7467 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
7468 &ordered_offset,
7469 ordered_bytes,
7470 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007471 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7472 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007473 btrfs_queue_work(wq, &ordered->work);
7474 }
7475 /*
7476 * If btrfs_dec_test_ordered_pending does not find any ordered
7477 * extent in the range, we can exit.
7478 */
7479 if (ordered_offset == last_offset)
7480 return;
7481 /*
7482 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007483 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007484 */
7485 if (ordered_offset < offset + bytes) {
7486 ordered_bytes = offset + bytes - ordered_offset;
7487 ordered = NULL;
7488 }
Chris Mason163cf092010-11-28 19:56:33 -05007489 }
Filipe Manana14543772015-11-24 16:23:54 +00007490}
7491
David Sterbad0ee3932018-03-08 14:35:48 +01007492static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007493 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007494{
Josef Bacikc6100a42017-05-05 11:57:13 -04007495 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007496 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007497 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007498 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007499 return 0;
7500}
7501
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007502static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007503{
7504 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007505 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007506
Miao Xie8b110e32014-09-12 18:44:03 +08007507 if (err)
7508 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007509 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007510 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7511 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007512 (unsigned long long)bio->bi_iter.bi_sector,
7513 bio->bi_iter.bi_size, err);
7514
Omar Sandoval769b4f22020-04-16 14:46:22 -07007515 if (bio_op(bio) == REQ_OP_READ) {
7516 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007517 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007518 }
7519
Omar Sandoval769b4f22020-04-16 14:46:22 -07007520 if (err)
7521 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007522
Miao Xiee65e1532010-11-22 03:04:43 +00007523 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007524 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007525}
7526
David Sterbad0ee3932018-03-08 14:35:48 +01007527static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7528 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007529{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007530 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007531 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007532 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007533 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007534
Liu Bo4c274bc2017-11-01 17:19:27 -06007535 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007536 if (async_submit)
7537 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7538
Josef Bacik5fd02042012-05-02 14:00:54 -04007539 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007540 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007541 if (ret)
7542 goto err;
7543 }
Miao Xiee65e1532010-11-22 03:04:43 +00007544
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007545 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007546 goto map;
7547
7548 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007549 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7550 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007551 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007552 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007553 } else if (write) {
7554 /*
7555 * If we aren't doing async submit, calculate the csum of the
7556 * bio now.
7557 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007558 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007559 if (ret)
7560 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007561 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007562 u64 csum_offset;
7563
7564 csum_offset = file_offset - dip->logical_offset;
7565 csum_offset >>= inode->i_sb->s_blocksize_bits;
7566 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7567 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007568 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007569map:
Chris Mason08635ba2019-07-10 12:28:14 -07007570 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007571err:
Miao Xiee65e1532010-11-22 03:04:43 +00007572 return ret;
7573}
7574
Omar Sandovalc36cac22020-04-16 14:46:13 -07007575/*
7576 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7577 * or ordered extents whether or not we submit any bios.
7578 */
7579static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7580 struct inode *inode,
7581 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007582{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007583 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007584 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7585 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007586 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007587
Omar Sandoval85879572020-04-16 14:46:21 -07007588 dip_size = sizeof(*dip);
7589 if (!write && csum) {
7590 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7591 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7592 size_t nblocks;
7593
7594 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7595 dip_size += csum_size * nblocks;
7596 }
7597
7598 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007599 if (!dip)
7600 return NULL;
7601
Omar Sandovalc36cac22020-04-16 14:46:13 -07007602 dip->inode = inode;
7603 dip->logical_offset = file_offset;
7604 dip->bytes = dio_bio->bi_iter.bi_size;
7605 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007606 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007607 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007608
7609 if (write) {
7610 struct btrfs_dio_data *dio_data = current->journal_info;
7611
7612 /*
7613 * Setting range start and end to the same value means that
7614 * no cleanup will happen in btrfs_direct_IO
7615 */
7616 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7617 dip->bytes;
7618 dio_data->unsubmitted_oe_range_start =
7619 dio_data->unsubmitted_oe_range_end;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007620 }
7621 return dip;
7622}
7623
7624static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7625 loff_t file_offset)
7626{
7627 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007628 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007629 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007630 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7631 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007632 struct btrfs_dio_private *dip;
7633 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007634 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007635 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007636 u64 submit_len;
7637 int clone_offset = 0;
7638 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307639 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007640 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007641 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007642
Omar Sandovalc36cac22020-04-16 14:46:13 -07007643 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7644 if (!dip) {
7645 if (!write) {
7646 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7647 file_offset + dio_bio->bi_iter.bi_size - 1);
7648 }
7649 dio_bio->bi_status = BLK_STS_RESOURCE;
7650 dio_end_io(dio_bio);
7651 return;
7652 }
7653
Omar Sandoval85879572020-04-16 14:46:21 -07007654 if (!write && csum) {
7655 /*
7656 * Load the csums up front to reduce csum tree searches and
7657 * contention when submitting bios.
7658 */
7659 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7660 dip->csums);
7661 if (status != BLK_STS_OK)
7662 goto out_err;
7663 }
7664
Omar Sandoval769b4f22020-04-16 14:46:22 -07007665 start_sector = dio_bio->bi_iter.bi_sector;
7666 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiefacc8a222013-07-25 19:22:34 +08007667
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007668 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007669 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7670 start_sector << 9, submit_len,
7671 &geom);
7672 if (ret) {
7673 status = errno_to_blk_status(ret);
7674 goto out_err;
7675 }
7676 ASSERT(geom.len <= INT_MAX);
7677
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007678 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007679
Liu Bo725130b2017-05-16 09:51:39 -07007680 /*
7681 * This will never fail as it's passing GPF_NOFS and
7682 * the allocation is backed by btrfs_bioset.
7683 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007684 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007685 bio->bi_private = dip;
7686 bio->bi_end_io = btrfs_end_dio_bio;
7687 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007688
Liu Bo725130b2017-05-16 09:51:39 -07007689 ASSERT(submit_len >= clone_len);
7690 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007691
Liu Bo725130b2017-05-16 09:51:39 -07007692 /*
7693 * Increase the count before we submit the bio so we know
7694 * the end IO handler won't happen before we increase the
7695 * count. Otherwise, the dip might get freed before we're
7696 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007697 *
7698 * We transfer the initial reference to the last bio, so we
7699 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007700 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007701 if (submit_len > 0) {
7702 refcount_inc(&dip->refs);
7703 /*
7704 * If we are submitting more than one bio, submit them
7705 * all asynchronously. The exception is RAID 5 or 6, as
7706 * asynchronous checksums make it difficult to collect
7707 * full stripe writes.
7708 */
7709 if (!raid56)
7710 async_submit = 1;
7711 }
Miao Xiee65e1532010-11-22 03:04:43 +00007712
David Sterbad0ee3932018-03-08 14:35:48 +01007713 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007714 async_submit);
7715 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007716 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007717 if (submit_len > 0)
7718 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007719 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007720 }
Liu Bo725130b2017-05-16 09:51:39 -07007721
7722 clone_offset += clone_len;
7723 start_sector += clone_len >> 9;
7724 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007725 } while (submit_len > 0);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007726 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007727
Miao Xiee65e1532010-11-22 03:04:43 +00007728out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007729 dip->dio_bio->bi_status = status;
7730 btrfs_dio_private_put(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007731}
7732
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007733static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007734 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007735{
7736 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007737 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007738 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007739 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007740
7741 if (offset & blocksize_mask)
7742 goto out;
7743
Al Viro28060d52014-03-22 05:15:17 -04007744 if (iov_iter_alignment(iter) & blocksize_mask)
7745 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007746
Al Viro28060d52014-03-22 05:15:17 -04007747 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04007748 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04007749 return 0;
7750 /*
7751 * Check to make sure we don't have duplicate iov_base's in this
7752 * iovec, if so return EINVAL, otherwise we'll get csum errors
7753 * when reading back.
7754 */
7755 for (seg = 0; seg < iter->nr_segs; seg++) {
7756 for (i = seg + 1; i < iter->nr_segs; i++) {
7757 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007758 goto out;
7759 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007760 }
7761 retval = 0;
7762out:
7763 return retval;
7764}
Josef Bacikeb838e72012-07-31 16:28:48 -04007765
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07007766static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04007767{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007768 struct file *file = iocb->ki_filp;
7769 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007770 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05307771 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08007772 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07007773 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00007774 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007775 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007776 bool wakeup = true;
7777 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007778 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007779
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03007780 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007781 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007782
Jens Axboefe0f07d2015-04-15 17:05:48 -06007783 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007784
Josef Bacik0e267c42013-07-02 10:38:02 -04007785 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007786 * The generic stuff only does filemap_write_and_wait_range, which
7787 * isn't enough if we've written compressed pages to this area, so
7788 * we need to flush the dirty pages again to make absolutely sure
7789 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007790 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007791 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007792 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7793 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007794 filemap_fdatawrite_range(inode->i_mapping, offset,
7795 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007796
Omar Sandoval6f673762015-03-16 04:33:52 -07007797 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007798 /*
7799 * If the write DIO is beyond the EOF, we need update
7800 * the isize, but it is protected by i_mutex. So we can
7801 * not unlock the i_mutex at this case.
7802 */
7803 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08007804 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05007805 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007806 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05007807 } else if (iocb->ki_flags & IOCB_NOWAIT) {
7808 ret = -EAGAIN;
7809 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00007810 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08007811 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
7812 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00007813 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007814 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007815
7816 /*
7817 * We need to know how many extents we reserved so that we can
7818 * do the accounting properly if we go over the number we
7819 * originally calculated. Abuse current->journal_info for this.
7820 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04007821 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007822 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00007823 dio_data.unsubmitted_oe_range_start = (u64)offset;
7824 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05307825 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05307826 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02007827 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7828 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06007829 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007830 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7831 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007832 }
7833
Omar Sandoval17f8c842015-03-16 04:33:50 -07007834 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007835 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07007836 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07007837 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07007838 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05307839 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007840 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08007841 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05307842 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08007843 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08007844 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00007845 /*
7846 * On error we might have left some ordered extents
7847 * without submitting corresponding bios for them, so
7848 * cleanup them up to avoid other tasks getting them
7849 * and waiting for them to complete forever.
7850 */
7851 if (dio_data.unsubmitted_oe_range_start <
7852 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08007853 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00007854 dio_data.unsubmitted_oe_range_start,
7855 dio_data.unsubmitted_oe_range_end -
7856 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08007857 false);
Liu Boddba1bf2015-06-17 16:59:58 +08007858 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08007859 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08007860 offset, count - (size_t)ret, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08007861 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00007862 }
Miao Xie38851cc2013-02-08 07:04:11 +00007863out:
Miao Xie2e60a512013-02-08 07:01:08 +00007864 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06007865 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007866 if (relock)
Al Viro59551022016-01-22 15:40:57 -05007867 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00007868
Qu Wenruo364ecf32017-02-27 15:10:38 +08007869 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00007870 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007871}
7872
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007873#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7874
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007875static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7876 __u64 start, __u64 len)
7877{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007878 int ret;
7879
7880 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7881 if (ret)
7882 return ret;
7883
David Sterba2135fb92017-06-23 04:09:57 +02007884 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007885}
7886
Chris Mason9ebefb182007-06-15 13:50:00 -04007887int btrfs_readpage(struct file *file, struct page *page)
7888{
David Sterba71ad38b2020-02-05 19:09:35 +01007889 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007890}
Chris Mason1832a6d2007-12-21 16:27:21 -05007891
Chris Mason39279cc2007-06-12 06:35:45 -04007892static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7893{
Josef Bacikbe7bd732015-10-22 15:05:09 -04007894 struct inode *inode = page->mapping->host;
7895 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04007896
7897 if (current->flags & PF_MEMALLOC) {
7898 redirty_page_for_writepage(wbc, page);
7899 unlock_page(page);
7900 return 0;
7901 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04007902
7903 /*
7904 * If we are under memory pressure we will call this directly from the
7905 * VM, we need to make sure we have the inode referenced for the ordered
7906 * extent. If not just return like we didn't do anything.
7907 */
7908 if (!igrab(inode)) {
7909 redirty_page_for_writepage(wbc, page);
7910 return AOP_WRITEPAGE_ACTIVATE;
7911 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02007912 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04007913 btrfs_add_delayed_iput(inode);
7914 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04007915}
Chris Mason39279cc2007-06-12 06:35:45 -04007916
Eric Sandeen48a3b632013-04-25 20:41:01 +00007917static int btrfs_writepages(struct address_space *mapping,
7918 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007919{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03007920 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04007921}
7922
Chris Mason3ab2fb52007-11-08 10:59:22 -05007923static int
7924btrfs_readpages(struct file *file, struct address_space *mapping,
7925 struct list_head *pages, unsigned nr_pages)
7926{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03007927 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05007928}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03007929
Chris Masone6dcd2d2008-07-17 12:53:50 -04007930static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007931{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03007932 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007933 if (ret == 1) {
7934 ClearPagePrivate(page);
7935 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007936 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007937 }
7938 return ret;
7939}
Chris Mason39279cc2007-06-12 06:35:45 -04007940
Chris Masone6dcd2d2008-07-17 12:53:50 -04007941static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7942{
Chris Mason98509cf2008-09-11 15:51:43 -04007943 if (PageWriteback(page) || PageDirty(page))
7944 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01007945 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007946}
7947
Roman Gushchinf8e66082020-03-04 16:57:35 -08007948#ifdef CONFIG_MIGRATION
7949static int btrfs_migratepage(struct address_space *mapping,
7950 struct page *newpage, struct page *page,
7951 enum migrate_mode mode)
7952{
7953 int ret;
7954
7955 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
7956 if (ret != MIGRATEPAGE_SUCCESS)
7957 return ret;
7958
7959 if (page_has_private(page)) {
7960 ClearPagePrivate(page);
7961 get_page(newpage);
7962 set_page_private(newpage, page_private(page));
7963 set_page_private(page, 0);
7964 put_page(page);
7965 SetPagePrivate(newpage);
7966 }
7967
7968 if (PagePrivate2(page)) {
7969 ClearPagePrivate2(page);
7970 SetPagePrivate2(newpage);
7971 }
7972
7973 if (mode != MIGRATE_SYNC_NO_COPY)
7974 migrate_page_copy(newpage, page);
7975 else
7976 migrate_page_states(newpage, page);
7977 return MIGRATEPAGE_SUCCESS;
7978}
7979#endif
7980
Lukas Czernerd47992f2013-05-21 23:17:23 -04007981static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7982 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007983{
Josef Bacik5fd02042012-05-02 14:00:54 -04007984 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007985 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007986 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007987 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007988 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007989 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05307990 u64 start;
7991 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007992 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007993
Chris Mason8b62b722009-09-02 16:53:46 -04007994 /*
7995 * we have the page locked, so new writeback can't start,
7996 * and the dirty bit won't be cleared while we are here.
7997 *
7998 * Wait for IO on this page so that we can safely clear
7999 * the PagePrivate2 bit and do ordered accounting
8000 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008001 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008002
Josef Bacik5fd02042012-05-02 14:00:54 -04008003 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008004 if (offset) {
8005 btrfs_releasepage(page, GFP_NOFS);
8006 return;
8007 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008008
8009 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008010 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308011again:
8012 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008013 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308014 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008015 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008016 end = min(page_end,
8017 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008018 /*
8019 * IO on this page will never be started, so we need
8020 * to account for any ordered extents now
8021 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008022 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308023 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008024 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008025 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008026 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008027 /*
8028 * whoever cleared the private bit is responsible
8029 * for the finish_ordered_io
8030 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008031 if (TestClearPagePrivate2(page)) {
8032 struct btrfs_ordered_inode_tree *tree;
8033 u64 new_len;
8034
8035 tree = &BTRFS_I(inode)->ordered_tree;
8036
8037 spin_lock_irq(&tree->lock);
8038 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308039 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008040 if (new_len < ordered->truncated_len)
8041 ordered->truncated_len = new_len;
8042 spin_unlock_irq(&tree->lock);
8043
8044 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308045 start,
8046 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008047 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008048 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008049 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008050 if (!inode_evicting) {
8051 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308052 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008053 &cached_state);
8054 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308055
8056 start = end + 1;
8057 if (start < page_end)
8058 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008059 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008060
Qu Wenruob9d0b382015-09-29 10:35:16 +08008061 /*
8062 * Qgroup reserved space handler
8063 * Page here will be either
8064 * 1) Already written to disk
8065 * In this case, its reserved space is released from data rsv map
8066 * and will be freed by delayed_ref handler finally.
8067 * So even we call qgroup_free_data(), it won't decrease reserved
8068 * space.
8069 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008070 * This means the reserved space should be freed here. However,
8071 * if a truncate invalidates the page (by clearing PageDirty)
8072 * and the page is accounted for while allocating extent
8073 * in btrfs_check_data_free_space() we let delayed_ref to
8074 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008075 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008076 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008077 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008078 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008079 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008080 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8081 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008082 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008083
8084 __btrfs_releasepage(page, GFP_NOFS);
8085 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008086
Chris Mason4a096752008-07-21 10:29:44 -04008087 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008088 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008089 ClearPagePrivate(page);
8090 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008091 put_page(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04008092 }
Chris Mason39279cc2007-06-12 06:35:45 -04008093}
8094
Chris Mason9ebefb182007-06-15 13:50:00 -04008095/*
8096 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8097 * called from a page fault handler when a page is first dirtied. Hence we must
8098 * be careful to check for EOF conditions here. We set the page up correctly
8099 * for a written page which means we get ENOSPC checking when writing into
8100 * holes and correct delalloc and unwritten extent mapping on filesystems that
8101 * support these features.
8102 *
8103 * We are not allowed to take the i_mutex here so we have to play games to
8104 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008105 * truncate_setsize() writes the inode size before removing pages, once we have
8106 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008107 * beyond EOF, then the page is guaranteed safe against truncation until we
8108 * unlock the page.
8109 */
Souptick Joardera528a242018-06-06 19:54:44 +05308110vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008111{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008112 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008113 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008114 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008115 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8116 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008117 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008118 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008119 char *kaddr;
8120 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008121 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308122 vm_fault_t ret;
8123 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008124 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308125 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008126 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008127 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308128 u64 end;
8129
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008130 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008131
Jan Karab2b5ef52012-06-12 16:20:45 +02008132 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008133 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008134 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308135 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008136
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308137 /*
8138 * Reserving delalloc space after obtaining the page lock can lead to
8139 * deadlock. For example, if a dirty page is locked by this function
8140 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8141 * dirty page write out, then the btrfs_writepage() function could
8142 * end up waiting indefinitely to get a lock on the page currently
8143 * being processed by btrfs_page_mkwrite() function.
8144 */
Souptick Joardera528a242018-06-06 19:54:44 +05308145 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308146 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308147 if (!ret2) {
8148 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008149 reserved = 1;
8150 }
Souptick Joardera528a242018-06-06 19:54:44 +05308151 if (ret2) {
8152 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008153 if (reserved)
8154 goto out;
8155 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008156 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008157
Nick Piggin56a76f82009-03-31 15:23:23 -07008158 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008159again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008160 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008161 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008162
Chris Mason9ebefb182007-06-15 13:50:00 -04008163 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008164 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008165 /* page got truncated out from underneath us */
8166 goto out_unlock;
8167 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008168 wait_on_page_writeback(page);
8169
David Sterbaff13db42015-12-03 14:30:40 +01008170 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008171 set_page_extent_mapped(page);
8172
Chris Masoneb84ae02008-07-17 13:53:27 -04008173 /*
8174 * we can't set the delalloc bits if there are pending ordered
8175 * extents. Drop our locks and wait for them to finish
8176 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008177 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8178 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008179 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008180 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008181 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008182 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008183 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008184 btrfs_put_ordered_extent(ordered);
8185 goto again;
8186 }
8187
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008188 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008189 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008190 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008191 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308192 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008193 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008194 page_start, PAGE_SIZE - reserved_space,
8195 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308196 }
8197 }
8198
Josef Bacikfbf19082009-10-01 17:10:23 -04008199 /*
Liu Bo54160342016-12-13 12:15:19 -08008200 * page_mkwrite gets called when the page is firstly dirtied after it's
8201 * faulted in, but write(2) could also dirty a page and set delalloc
8202 * bits, thus in this case for space account reason, we still need to
8203 * clear any delalloc bits within this page range since we have to
8204 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008205 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308206 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008207 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8208 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008209
Souptick Joardera528a242018-06-06 19:54:44 +05308210 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008211 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308212 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008213 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008214 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008215 ret = VM_FAULT_SIGBUS;
8216 goto out_unlock;
8217 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008218
8219 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008220 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008221 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008222 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008223 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008224
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008225 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008226 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008227 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008228 flush_dcache_page(page);
8229 kunmap(page);
8230 }
Chris Mason247e7432008-07-17 12:53:51 -04008231 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008232 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008233 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008234
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008235 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008236 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008237 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008238
David Sterbae43bbe52017-12-12 21:43:52 +01008239 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008240
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008241 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8242 sb_end_pagefault(inode->i_sb);
8243 extent_changeset_free(data_reserved);
8244 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008245
8246out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008247 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008248out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008249 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008250 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008251 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008252out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008253 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008254 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008255 return ret;
8256}
8257
Filipe Manana213e8c52018-02-06 20:40:31 +00008258static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008259{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008260 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008261 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008262 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008263 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008264 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008265 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008266 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008267
Filipe Manana213e8c52018-02-06 20:40:31 +00008268 if (!skip_writeback) {
8269 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8270 (u64)-1);
8271 if (ret)
8272 return ret;
8273 }
Chris Mason39279cc2007-06-12 06:35:45 -04008274
Josef Bacikfcb80c22011-05-03 10:40:22 -04008275 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008276 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8277 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008278 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008279 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008280 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008281 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008282 * be free'd up by the truncate operation, but also have some slack
8283 * space reserved in case it uses space during the truncate (thank you
8284 * very much snapshotting).
8285 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008286 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008287 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008288 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008289 * doesn't end up using space reserved for updating the inode. We also
8290 * need to be able to stop the transaction and start a new one, which
8291 * means we need to be able to update the inode several times, and we
8292 * have no idea of knowing how many times that will be, so we can't just
8293 * reserve 1 item for the entirety of the operation, so that has to be
8294 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008295 *
8296 * So that leaves us with
8297 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008298 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008299 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008300 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008301 * updating the inode.
8302 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008303 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008304 if (!rsv)
8305 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008306 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008307 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008308
Josef Bacik907cbce2011-08-08 13:46:15 -04008309 /*
Josef Bacik07127182011-08-19 10:29:59 -04008310 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008311 * 1 for updating the inode.
8312 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008313 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008314 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008315 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008316 goto out;
8317 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008318
Josef Bacik907cbce2011-08-08 13:46:15 -04008319 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008320 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008321 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008322 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008323
Chris Mason5a3f23d2009-03-31 13:27:11 -04008324 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008325 * So if we truncate and then write and fsync we normally would just
8326 * write the extents that changed, which is a problem if we need to
8327 * first truncate that entire inode. So set this flag so we write out
8328 * all of the extents in the inode to the sync log so we're completely
8329 * safe.
8330 */
8331 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008332 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008333
Yan, Zheng80825102009-11-12 09:35:36 +00008334 while (1) {
8335 ret = btrfs_truncate_inode_items(trans, root, inode,
8336 inode->i_size,
8337 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008338 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008339 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008340 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008341
Yan, Zheng80825102009-11-12 09:35:36 +00008342 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008343 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008344 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008345
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008346 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008347 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008348
8349 trans = btrfs_start_transaction(root, 2);
8350 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008351 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008352 trans = NULL;
8353 break;
8354 }
8355
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008356 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008357 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008358 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008359 BUG_ON(ret); /* shouldn't happen */
8360 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008361 }
8362
Josef Bacikddfae632017-10-19 14:16:02 -04008363 /*
8364 * We can't call btrfs_truncate_block inside a trans handle as we could
8365 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8366 * we've truncated everything except the last little bit, and can do
8367 * btrfs_truncate_block and then update the disk_i_size.
8368 */
8369 if (ret == NEED_TRUNCATE_BLOCK) {
8370 btrfs_end_transaction(trans);
8371 btrfs_btree_balance_dirty(fs_info);
8372
8373 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8374 if (ret)
8375 goto out;
8376 trans = btrfs_start_transaction(root, 1);
8377 if (IS_ERR(trans)) {
8378 ret = PTR_ERR(trans);
8379 goto out;
8380 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008381 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008382 }
8383
Chris Mason917c16b2011-11-08 14:49:59 -05008384 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008385 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008386
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008387 trans->block_rsv = &fs_info->trans_block_rsv;
8388 ret2 = btrfs_update_inode(trans, root, inode);
8389 if (ret2 && !ret)
8390 ret = ret2;
8391
8392 ret2 = btrfs_end_transaction(trans);
8393 if (ret2 && !ret)
8394 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008395 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008396 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008397out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008398 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008399
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008400 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008401}
8402
Sven Wegener3b963622008-06-09 21:57:42 -04008403/*
Chris Masond352ac62008-09-29 15:18:18 -04008404 * create a new subvolume directory/inode (helper for the ioctl).
8405 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008406int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008407 struct btrfs_root *new_root,
8408 struct btrfs_root *parent_root,
8409 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008410{
Chris Mason39279cc2007-06-12 06:35:45 -04008411 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008412 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008413 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008414
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008415 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8416 new_dirid, new_dirid,
8417 S_IFDIR | (~current_umask() & S_IRWXUGO),
8418 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008419 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008420 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008421 inode->i_op = &btrfs_dir_inode_operations;
8422 inode->i_fop = &btrfs_dir_file_operations;
8423
Miklos Szeredibfe86842011-10-28 14:13:29 +02008424 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008425 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008426 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008427
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008428 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8429 if (err)
8430 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008431 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008432 new_root->root_key.objectid, err);
8433
Yan, Zheng76dda932009-09-21 16:00:26 -04008434 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008435
Yan, Zheng76dda932009-09-21 16:00:26 -04008436 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008437 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008438}
8439
Chris Mason39279cc2007-06-12 06:35:45 -04008440struct inode *btrfs_alloc_inode(struct super_block *sb)
8441{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008442 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008443 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008444 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008445
David Sterba712e36c2017-10-31 17:08:27 +01008446 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008447 if (!ei)
8448 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008449
8450 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008451 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008452 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008453 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008454 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008455 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008456 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008457 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008458 ei->disk_i_size = 0;
8459 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008460 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008461 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008462 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008463 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008464 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008465
Josef Bacik9e0baf62011-07-15 15:16:44 +00008466 spin_lock_init(&ei->lock);
8467 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008468 if (sb->s_magic != BTRFS_TEST_MAGIC)
8469 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8470 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008471 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008472 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008473 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008474
Miao Xie16cdcec2011-04-22 18:12:22 +08008475 ei->delayed_node = NULL;
8476
chandan r9cc97d62012-07-04 12:48:07 +05308477 ei->i_otime.tv_sec = 0;
8478 ei->i_otime.tv_nsec = 0;
8479
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008480 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008481 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008482 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8483 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8484 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008485 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8486 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008487 ei->io_tree.track_uptodate = true;
8488 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008489 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008490 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008491 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008492 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008493 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008494 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008495 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008496
8497 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008498}
8499
Josef Bacikaaedb552013-10-11 14:44:09 -04008500#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8501void btrfs_test_destroy_inode(struct inode *inode)
8502{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008503 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008504 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8505}
8506#endif
8507
Al Viro26602ca2019-04-10 15:14:41 -04008508void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008509{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008510 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8511}
8512
Chris Mason39279cc2007-06-12 06:35:45 -04008513void btrfs_destroy_inode(struct inode *inode)
8514{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008515 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008516 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008517 struct btrfs_root *root = BTRFS_I(inode)->root;
8518
Al Virob3d9b7a2012-06-09 13:51:19 -04008519 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008520 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008521 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8522 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008523 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008524 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008525 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008526 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008527 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008528
Chris Mason5a3f23d2009-03-31 13:27:11 -04008529 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008530 * This can happen where we create an inode, but somebody else also
8531 * created the same inode and we need to destroy the one we already
8532 * created.
8533 */
8534 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008535 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008536
Chris Masond3977122009-01-05 21:25:51 -05008537 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008538 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8539 if (!ordered)
8540 break;
8541 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008542 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008543 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008544 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008545 btrfs_remove_ordered_extent(inode, ordered);
8546 btrfs_put_ordered_extent(ordered);
8547 btrfs_put_ordered_extent(ordered);
8548 }
8549 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008550 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008551 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008552 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008553 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008554 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008555}
8556
Al Viro45321ac2010-06-07 13:43:19 -04008557int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008558{
8559 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008560
Naohiro Aota6379ef92013-06-06 09:56:34 +00008561 if (root == NULL)
8562 return 1;
8563
Liu Bofa6ac872013-02-20 14:10:23 +00008564 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008565 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008566 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008567 else
Al Viro45321ac2010-06-07 13:43:19 -04008568 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008569}
8570
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008571static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008572{
8573 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8574
8575 inode_init_once(&ei->vfs_inode);
8576}
8577
David Sterbae67c7182018-02-19 17:24:18 +01008578void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008579{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008580 /*
8581 * Make sure all delayed rcu free inodes are flushed before we
8582 * destroy cache.
8583 */
8584 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008585 kmem_cache_destroy(btrfs_inode_cachep);
8586 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008587 kmem_cache_destroy(btrfs_path_cachep);
8588 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008589 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008590}
8591
Liu Bof5c29bd2017-11-02 17:21:50 -06008592int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008593{
David Sterba837e1972012-09-07 03:00:48 -06008594 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008595 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008596 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8597 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008598 if (!btrfs_inode_cachep)
8599 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008600
David Sterba837e1972012-09-07 03:00:48 -06008601 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008602 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008603 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008604 if (!btrfs_trans_handle_cachep)
8605 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008606
David Sterba837e1972012-09-07 03:00:48 -06008607 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008608 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008609 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008610 if (!btrfs_path_cachep)
8611 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008612
David Sterba837e1972012-09-07 03:00:48 -06008613 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008614 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008615 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008616 if (!btrfs_free_space_cachep)
8617 goto fail;
8618
Christophe Leroy3acd4852019-08-21 15:05:55 +00008619 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8620 PAGE_SIZE, PAGE_SIZE,
8621 SLAB_RED_ZONE, NULL);
8622 if (!btrfs_free_space_bitmap_cachep)
8623 goto fail;
8624
Chris Mason39279cc2007-06-12 06:35:45 -04008625 return 0;
8626fail:
8627 btrfs_destroy_cachep();
8628 return -ENOMEM;
8629}
8630
David Howellsa528d352017-01-31 16:46:22 +00008631static int btrfs_getattr(const struct path *path, struct kstat *stat,
8632 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008633{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008634 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008635 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008636 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008637 u32 bi_flags = BTRFS_I(inode)->flags;
8638
8639 stat->result_mask |= STATX_BTIME;
8640 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8641 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8642 if (bi_flags & BTRFS_INODE_APPEND)
8643 stat->attributes |= STATX_ATTR_APPEND;
8644 if (bi_flags & BTRFS_INODE_COMPRESS)
8645 stat->attributes |= STATX_ATTR_COMPRESSED;
8646 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8647 stat->attributes |= STATX_ATTR_IMMUTABLE;
8648 if (bi_flags & BTRFS_INODE_NODUMP)
8649 stat->attributes |= STATX_ATTR_NODUMP;
8650
8651 stat->attributes_mask |= (STATX_ATTR_APPEND |
8652 STATX_ATTR_COMPRESSED |
8653 STATX_ATTR_IMMUTABLE |
8654 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008655
Chris Mason39279cc2007-06-12 06:35:45 -04008656 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008657 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008658
8659 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008660 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008661 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008662 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008663 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008664 return 0;
8665}
8666
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008667static int btrfs_rename_exchange(struct inode *old_dir,
8668 struct dentry *old_dentry,
8669 struct inode *new_dir,
8670 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008671{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008672 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008673 struct btrfs_trans_handle *trans;
8674 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008675 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008676 struct inode *new_inode = new_dentry->d_inode;
8677 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008678 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008679 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008680 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8681 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008682 u64 old_idx = 0;
8683 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008684 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008685 bool root_log_pinned = false;
8686 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008687 struct btrfs_log_ctx ctx_root;
8688 struct btrfs_log_ctx ctx_dest;
8689 bool sync_log_root = false;
8690 bool sync_log_dest = false;
8691 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008692
8693 /* we only allow rename subvolume link between subvolumes */
8694 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8695 return -EXDEV;
8696
Filipe Mananad4682ba2018-06-11 19:24:28 +01008697 btrfs_init_log_ctx(&ctx_root, old_inode);
8698 btrfs_init_log_ctx(&ctx_dest, new_inode);
8699
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008700 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008701 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8702 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008703 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008704
8705 /*
8706 * We want to reserve the absolute worst case amount of items. So if
8707 * both inodes are subvols and we need to unlink them then that would
8708 * require 4 item modifications, but if they are both normal inodes it
8709 * would require 5 item modifications, so we'll assume their normal
8710 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8711 * should cover the worst case number of items we'll modify.
8712 */
8713 trans = btrfs_start_transaction(root, 12);
8714 if (IS_ERR(trans)) {
8715 ret = PTR_ERR(trans);
8716 goto out_notrans;
8717 }
8718
Josef Bacik3e174092019-11-15 15:43:06 -05008719 if (dest != root)
8720 btrfs_record_root_in_trans(trans, dest);
8721
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008722 /*
8723 * We need to find a free sequence number both in the source and
8724 * in the destination directory for the exchange.
8725 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008726 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008727 if (ret)
8728 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008729 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008730 if (ret)
8731 goto out_fail;
8732
8733 BTRFS_I(old_inode)->dir_index = 0ULL;
8734 BTRFS_I(new_inode)->dir_index = 0ULL;
8735
8736 /* Reference for the source. */
8737 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8738 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008739 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008740 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008741 btrfs_pin_log_trans(root);
8742 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008743 ret = btrfs_insert_inode_ref(trans, dest,
8744 new_dentry->d_name.name,
8745 new_dentry->d_name.len,
8746 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008747 btrfs_ino(BTRFS_I(new_dir)),
8748 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008749 if (ret)
8750 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008751 }
8752
8753 /* And now for the dest. */
8754 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8755 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008756 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008757 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008758 btrfs_pin_log_trans(dest);
8759 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008760 ret = btrfs_insert_inode_ref(trans, root,
8761 old_dentry->d_name.name,
8762 old_dentry->d_name.len,
8763 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008764 btrfs_ino(BTRFS_I(old_dir)),
8765 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008766 if (ret)
8767 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008768 }
8769
8770 /* Update inode version and ctime/mtime. */
8771 inode_inc_iversion(old_dir);
8772 inode_inc_iversion(new_dir);
8773 inode_inc_iversion(old_inode);
8774 inode_inc_iversion(new_inode);
8775 old_dir->i_ctime = old_dir->i_mtime = ctime;
8776 new_dir->i_ctime = new_dir->i_mtime = ctime;
8777 old_inode->i_ctime = ctime;
8778 new_inode->i_ctime = ctime;
8779
8780 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008781 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8782 BTRFS_I(old_inode), 1);
8783 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8784 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008785 }
8786
8787 /* src is a subvolume */
8788 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008789 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008790 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008791 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8792 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008793 old_dentry->d_name.name,
8794 old_dentry->d_name.len);
8795 if (!ret)
8796 ret = btrfs_update_inode(trans, root, old_inode);
8797 }
8798 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008799 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008800 goto out_fail;
8801 }
8802
8803 /* dest is a subvolume */
8804 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008805 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008806 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008807 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8808 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008809 new_dentry->d_name.name,
8810 new_dentry->d_name.len);
8811 if (!ret)
8812 ret = btrfs_update_inode(trans, dest, new_inode);
8813 }
8814 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008815 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008816 goto out_fail;
8817 }
8818
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008819 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008820 new_dentry->d_name.name,
8821 new_dentry->d_name.len, 0, old_idx);
8822 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008823 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008824 goto out_fail;
8825 }
8826
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008827 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008828 old_dentry->d_name.name,
8829 old_dentry->d_name.len, 0, new_idx);
8830 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008831 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008832 goto out_fail;
8833 }
8834
8835 if (old_inode->i_nlink == 1)
8836 BTRFS_I(old_inode)->dir_index = old_idx;
8837 if (new_inode->i_nlink == 1)
8838 BTRFS_I(new_inode)->dir_index = new_idx;
8839
Filipe Manana86e8aa02016-05-05 02:02:27 +01008840 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008841 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008842 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8843 BTRFS_I(old_dir), parent,
8844 false, &ctx_root);
8845 if (ret == BTRFS_NEED_LOG_SYNC)
8846 sync_log_root = true;
8847 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8848 commit_transaction = true;
8849 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008850 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008851 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008852 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008853 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008854 if (!commit_transaction) {
8855 parent = old_dentry->d_parent;
8856 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8857 BTRFS_I(new_dir), parent,
8858 false, &ctx_dest);
8859 if (ret == BTRFS_NEED_LOG_SYNC)
8860 sync_log_dest = true;
8861 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8862 commit_transaction = true;
8863 ret = 0;
8864 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008865 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008866 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008867 }
8868out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008869 /*
8870 * If we have pinned a log and an error happened, we unpin tasks
8871 * trying to sync the log and force them to fallback to a transaction
8872 * commit if the log currently contains any of the inodes involved in
8873 * this rename operation (to ensure we do not persist a log with an
8874 * inconsistent state for any of these inodes or leading to any
8875 * inconsistencies when replayed). If the transaction was aborted, the
8876 * abortion reason is propagated to userspace when attempting to commit
8877 * the transaction. If the log does not contain any of these inodes, we
8878 * allow the tasks to sync it.
8879 */
8880 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008881 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8882 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8883 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008884 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008885 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008886 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008887
8888 if (root_log_pinned) {
8889 btrfs_end_log_trans(root);
8890 root_log_pinned = false;
8891 }
8892 if (dest_log_pinned) {
8893 btrfs_end_log_trans(dest);
8894 dest_log_pinned = false;
8895 }
8896 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008897 if (!ret && sync_log_root && !commit_transaction) {
8898 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
8899 &ctx_root);
8900 if (ret)
8901 commit_transaction = true;
8902 }
8903 if (!ret && sync_log_dest && !commit_transaction) {
8904 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
8905 &ctx_dest);
8906 if (ret)
8907 commit_transaction = true;
8908 }
8909 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00008910 /*
8911 * We may have set commit_transaction when logging the new name
8912 * in the destination root, in which case we left the source
8913 * root context in the list of log contextes. So make sure we
8914 * remove it to avoid invalid memory accesses, since the context
8915 * was allocated in our stack frame.
8916 */
8917 if (sync_log_root) {
8918 mutex_lock(&root->log_mutex);
8919 list_del_init(&ctx_root.list);
8920 mutex_unlock(&root->log_mutex);
8921 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008922 ret = btrfs_commit_transaction(trans);
8923 } else {
8924 int ret2;
8925
8926 ret2 = btrfs_end_transaction(trans);
8927 ret = ret ? ret : ret2;
8928 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008929out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05008930 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
8931 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008932 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008933
Filipe Mananae6c61712019-11-08 16:11:56 +00008934 ASSERT(list_empty(&ctx_root.list));
8935 ASSERT(list_empty(&ctx_dest.list));
8936
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008937 return ret;
8938}
8939
8940static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
8941 struct btrfs_root *root,
8942 struct inode *dir,
8943 struct dentry *dentry)
8944{
8945 int ret;
8946 struct inode *inode;
8947 u64 objectid;
8948 u64 index;
8949
8950 ret = btrfs_find_free_ino(root, &objectid);
8951 if (ret)
8952 return ret;
8953
8954 inode = btrfs_new_inode(trans, root, dir,
8955 dentry->d_name.name,
8956 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008957 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008958 objectid,
8959 S_IFCHR | WHITEOUT_MODE,
8960 &index);
8961
8962 if (IS_ERR(inode)) {
8963 ret = PTR_ERR(inode);
8964 return ret;
8965 }
8966
8967 inode->i_op = &btrfs_special_inode_operations;
8968 init_special_inode(inode, inode->i_mode,
8969 WHITEOUT_DEV);
8970
8971 ret = btrfs_init_inode_security(trans, inode, dir,
8972 &dentry->d_name);
8973 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01008974 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008975
Nikolay Borisovcef415a2017-02-20 13:51:09 +02008976 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
8977 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008978 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01008979 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008980
8981 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01008982out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008983 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01008984 if (ret)
8985 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008986 iput(inode);
8987
Filipe Mananac9901612016-05-05 01:41:57 +01008988 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008989}
8990
Chris Mason39279cc2007-06-12 06:35:45 -04008991static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008992 struct inode *new_dir, struct dentry *new_dentry,
8993 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008994{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008995 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008996 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01008997 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04008998 struct btrfs_root *root = BTRFS_I(old_dir)->root;
8999 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009000 struct inode *new_inode = d_inode(new_dentry);
9001 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009002 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009003 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009004 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009005 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009006 struct btrfs_log_ctx ctx;
9007 bool sync_log = false;
9008 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009009
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009010 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009011 return -EPERM;
9012
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009013 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009014 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009015 return -EXDEV;
9016
Li Zefan33345d012011-04-20 10:31:50 +08009017 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009018 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009019 return -ENOTEMPTY;
9020
Chris Mason39279cc2007-06-12 06:35:45 -04009021 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009022 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009023 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009024
9025
9026 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009027 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009028 new_dentry->d_name.name,
9029 new_dentry->d_name.len);
9030
9031 if (ret) {
9032 if (ret == -EEXIST) {
9033 /* we shouldn't get
9034 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309035 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009036 return ret;
9037 }
9038 } else {
9039 /* maybe -EOVERFLOW */
9040 return ret;
9041 }
9042 }
9043 ret = 0;
9044
Chris Mason5a3f23d2009-03-31 13:27:11 -04009045 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009046 * we're using rename to replace one file with another. Start IO on it
9047 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009048 */
Chris Mason8d875f92014-08-12 10:47:42 -07009049 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009050 filemap_flush(old_inode->i_mapping);
9051
Yan, Zheng76dda932009-09-21 16:00:26 -04009052 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009053 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009054 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009055 /*
9056 * We want to reserve the absolute worst case amount of items. So if
9057 * both inodes are subvols and we need to unlink them then that would
9058 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009059 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009060 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9061 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009062 * If our rename has the whiteout flag, we need more 5 units for the
9063 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9064 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009065 */
Filipe Manana5062af32016-05-05 10:26:26 +01009066 trans_num_items = 11;
9067 if (flags & RENAME_WHITEOUT)
9068 trans_num_items += 5;
9069 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009070 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009071 ret = PTR_ERR(trans);
9072 goto out_notrans;
9073 }
Chris Mason5f39d392007-10-15 16:14:19 -04009074
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009075 if (dest != root)
9076 btrfs_record_root_in_trans(trans, dest);
9077
Nikolay Borisov877574e2017-02-20 13:50:33 +02009078 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009079 if (ret)
9080 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009081
Miao Xie67de1172013-12-26 13:07:06 +08009082 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009083 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009084 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009085 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009086 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009087 btrfs_pin_log_trans(root);
9088 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009089 ret = btrfs_insert_inode_ref(trans, dest,
9090 new_dentry->d_name.name,
9091 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009092 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009093 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009094 if (ret)
9095 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009096 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009097
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009098 inode_inc_iversion(old_dir);
9099 inode_inc_iversion(new_dir);
9100 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009101 old_dir->i_ctime = old_dir->i_mtime =
9102 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009103 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009104
Chris Mason12fcfd22009-03-24 10:24:20 -04009105 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009106 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9107 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009108
Li Zefan33345d012011-04-20 10:31:50 +08009109 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009110 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009111 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009112 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9113 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009114 old_dentry->d_name.name,
9115 old_dentry->d_name.len);
9116 if (!ret)
9117 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009118 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009119 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009120 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009121 goto out_fail;
9122 }
Chris Mason39279cc2007-06-12 06:35:45 -04009123
9124 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009125 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009126 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009127 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009128 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009129 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009130 BUG_ON(new_inode->i_nlink == 0);
9131 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009132 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9133 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009134 new_dentry->d_name.name,
9135 new_dentry->d_name.len);
9136 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009137 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009138 ret = btrfs_orphan_add(trans,
9139 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009140 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009141 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009142 goto out_fail;
9143 }
Chris Mason39279cc2007-06-12 06:35:45 -04009144 }
Josef Bacikaec74772008-07-24 12:12:38 -04009145
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009146 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009147 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009148 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009149 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009150 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009151 goto out_fail;
9152 }
Chris Mason39279cc2007-06-12 06:35:45 -04009153
Miao Xie67de1172013-12-26 13:07:06 +08009154 if (old_inode->i_nlink == 1)
9155 BTRFS_I(old_inode)->dir_index = index;
9156
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009157 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009158 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009159
Filipe Mananad4682ba2018-06-11 19:24:28 +01009160 btrfs_init_log_ctx(&ctx, old_inode);
9161 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9162 BTRFS_I(old_dir), parent,
9163 false, &ctx);
9164 if (ret == BTRFS_NEED_LOG_SYNC)
9165 sync_log = true;
9166 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9167 commit_transaction = true;
9168 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009169 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009170 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009171 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009172
9173 if (flags & RENAME_WHITEOUT) {
9174 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9175 old_dentry);
9176
9177 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009178 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009179 goto out_fail;
9180 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009181 }
Chris Mason39279cc2007-06-12 06:35:45 -04009182out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009183 /*
9184 * If we have pinned the log and an error happened, we unpin tasks
9185 * trying to sync the log and force them to fallback to a transaction
9186 * commit if the log currently contains any of the inodes involved in
9187 * this rename operation (to ensure we do not persist a log with an
9188 * inconsistent state for any of these inodes or leading to any
9189 * inconsistencies when replayed). If the transaction was aborted, the
9190 * abortion reason is propagated to userspace when attempting to commit
9191 * the transaction. If the log does not contain any of these inodes, we
9192 * allow the tasks to sync it.
9193 */
9194 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009195 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9196 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9197 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009198 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009199 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009200 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009201
9202 btrfs_end_log_trans(root);
9203 log_pinned = false;
9204 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009205 if (!ret && sync_log) {
9206 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9207 if (ret)
9208 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009209 } else if (sync_log) {
9210 mutex_lock(&root->log_mutex);
9211 list_del(&ctx.list);
9212 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009213 }
9214 if (commit_transaction) {
9215 ret = btrfs_commit_transaction(trans);
9216 } else {
9217 int ret2;
9218
9219 ret2 = btrfs_end_transaction(trans);
9220 ret = ret ? ret : ret2;
9221 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009222out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009223 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009224 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009225
Chris Mason39279cc2007-06-12 06:35:45 -04009226 return ret;
9227}
9228
Miklos Szeredi80ace852014-07-23 15:15:32 +02009229static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9230 struct inode *new_dir, struct dentry *new_dentry,
9231 unsigned int flags)
9232{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009233 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009234 return -EINVAL;
9235
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009236 if (flags & RENAME_EXCHANGE)
9237 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9238 new_dentry);
9239
9240 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009241}
9242
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009243struct btrfs_delalloc_work {
9244 struct inode *inode;
9245 struct completion completion;
9246 struct list_head list;
9247 struct btrfs_work work;
9248};
9249
Miao Xie8ccf6f192012-10-25 09:28:04 +00009250static void btrfs_run_delalloc_work(struct btrfs_work *work)
9251{
9252 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009253 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009254
9255 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9256 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009257 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009258 filemap_flush(inode->i_mapping);
9259 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9260 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009261 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009262
Nikolay Borisov076da912018-04-23 10:54:16 +03009263 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009264 complete(&delalloc_work->completion);
9265}
9266
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009267static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009268{
9269 struct btrfs_delalloc_work *work;
9270
David Sterba100d5702015-12-08 14:39:32 +01009271 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009272 if (!work)
9273 return NULL;
9274
9275 init_completion(&work->completion);
9276 INIT_LIST_HEAD(&work->list);
9277 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009278 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009279
9280 return work;
9281}
9282
Chris Masond352ac62008-09-29 15:18:18 -04009283/*
9284 * some fairly slow code that needs optimization. This walks the list
9285 * of all the inodes with pending delalloc and forces them to disk.
9286 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009287static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009288{
Chris Masonea8c2812008-08-04 23:17:27 -04009289 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009290 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009291 struct btrfs_delalloc_work *work, *next;
9292 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009293 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009294 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009295
Miao Xie8ccf6f192012-10-25 09:28:04 +00009296 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009297 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009298
Miao Xie573bfb72014-03-06 13:55:03 +08009299 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009300 spin_lock(&root->delalloc_lock);
9301 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009302 while (!list_empty(&splice)) {
9303 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009304 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009305
Miao Xieeb73c1b2013-05-15 07:48:22 +00009306 list_move_tail(&binode->delalloc_inodes,
9307 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009308 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009309 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009310 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009311 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009312 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009313 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009314
Ethan Lien3cd24c62018-11-01 14:49:03 +08009315 if (snapshot)
9316 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9317 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009318 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009319 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009320 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009321 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009322 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009323 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009324 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009325 btrfs_queue_work(root->fs_info->flush_workers,
9326 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009327 ret++;
9328 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009329 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009330 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009331 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009332 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009333 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009334
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009335out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009336 list_for_each_entry_safe(work, next, &works, list) {
9337 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009338 wait_for_completion(&work->completion);
9339 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009340 }
9341
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009342 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009343 spin_lock(&root->delalloc_lock);
9344 list_splice_tail(&splice, &root->delalloc_inodes);
9345 spin_unlock(&root->delalloc_lock);
9346 }
Miao Xie573bfb72014-03-06 13:55:03 +08009347 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009348 return ret;
9349}
9350
Ethan Lien3cd24c62018-11-01 14:49:03 +08009351int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009352{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009353 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009354 int ret;
9355
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009356 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009357 return -EROFS;
9358
Ethan Lien3cd24c62018-11-01 14:49:03 +08009359 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009360 if (ret > 0)
9361 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009362 return ret;
9363}
9364
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009365int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009366{
9367 struct btrfs_root *root;
9368 struct list_head splice;
9369 int ret;
9370
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009371 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009372 return -EROFS;
9373
9374 INIT_LIST_HEAD(&splice);
9375
Miao Xie573bfb72014-03-06 13:55:03 +08009376 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009377 spin_lock(&fs_info->delalloc_root_lock);
9378 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009379 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009380 root = list_first_entry(&splice, struct btrfs_root,
9381 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009382 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009383 BUG_ON(!root);
9384 list_move_tail(&root->delalloc_root,
9385 &fs_info->delalloc_roots);
9386 spin_unlock(&fs_info->delalloc_root_lock);
9387
Ethan Lien3cd24c62018-11-01 14:49:03 +08009388 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009389 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009390 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009391 goto out;
9392
Miao Xie6c255e62014-03-06 13:55:01 +08009393 if (nr != -1) {
9394 nr -= ret;
9395 WARN_ON(nr < 0);
9396 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009397 spin_lock(&fs_info->delalloc_root_lock);
9398 }
9399 spin_unlock(&fs_info->delalloc_root_lock);
9400
Miao Xie6c255e62014-03-06 13:55:01 +08009401 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009402out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009403 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009404 spin_lock(&fs_info->delalloc_root_lock);
9405 list_splice_tail(&splice, &fs_info->delalloc_roots);
9406 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009407 }
Miao Xie573bfb72014-03-06 13:55:03 +08009408 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009409 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009410}
9411
Chris Mason39279cc2007-06-12 06:35:45 -04009412static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9413 const char *symname)
9414{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009415 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009416 struct btrfs_trans_handle *trans;
9417 struct btrfs_root *root = BTRFS_I(dir)->root;
9418 struct btrfs_path *path;
9419 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009420 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009421 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009422 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309423 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009424 int name_len;
9425 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009426 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009427 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009428 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009429
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009430 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009431 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009432 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009433
Josef Bacik9ed74f22009-09-11 16:12:44 -04009434 /*
9435 * 2 items for inode item and ref
9436 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009437 * 1 item for updating parent inode item
9438 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009439 * 1 item for xattr if selinux is on
9440 */
Filipe Manana9269d122015-12-31 18:16:29 +00009441 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009442 if (IS_ERR(trans))
9443 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009444
Li Zefan581bb052011-04-20 10:06:11 +08009445 err = btrfs_find_free_ino(root, &objectid);
9446 if (err)
9447 goto out_unlock;
9448
Josef Bacikaec74772008-07-24 12:12:38 -04009449 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009450 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9451 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009452 if (IS_ERR(inode)) {
9453 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009454 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009455 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009456 }
Chris Mason39279cc2007-06-12 06:35:45 -04009457
Casey Schauflerad19db72011-12-15 10:09:07 -05009458 /*
9459 * If the active LSM wants to access the inode during
9460 * d_instantiate it needs these. Smack checks to see
9461 * if the filesystem supports xattrs by looking at the
9462 * ops vector.
9463 */
9464 inode->i_fop = &btrfs_file_operations;
9465 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009466 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009467 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9468
9469 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9470 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009471 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009472
Chris Mason39279cc2007-06-12 06:35:45 -04009473 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009474 if (!path) {
9475 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009476 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009477 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009478 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009479 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009480 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009481 datasize = btrfs_file_extent_calc_inline_size(name_len);
9482 err = btrfs_insert_empty_item(trans, root, path, &key,
9483 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009484 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009485 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009486 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009487 }
Chris Mason5f39d392007-10-15 16:14:19 -04009488 leaf = path->nodes[0];
9489 ei = btrfs_item_ptr(leaf, path->slots[0],
9490 struct btrfs_file_extent_item);
9491 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9492 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009493 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009494 btrfs_set_file_extent_encryption(leaf, ei, 0);
9495 btrfs_set_file_extent_compression(leaf, ei, 0);
9496 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9497 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9498
Chris Mason39279cc2007-06-12 06:35:45 -04009499 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009500 write_extent_buffer(leaf, symname, ptr, name_len);
9501 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009502 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009503
Chris Mason39279cc2007-06-12 06:35:45 -04009504 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009505 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009506 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009507 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009508 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009509 /*
9510 * Last step, add directory indexes for our symlink inode. This is the
9511 * last step to avoid extra cleanup of these indexes if an error happens
9512 * elsewhere above.
9513 */
9514 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009515 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9516 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009517 if (err)
9518 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009519
Al Viro1e2e5472018-05-04 08:23:01 -04009520 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009521
9522out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009523 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009524 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009525 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009526 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009527 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009528 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009529 return err;
9530}
Chris Mason16432982008-04-10 10:23:21 -04009531
Josef Bacik0af3d002010-06-21 14:48:16 -04009532static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9533 u64 start, u64 num_bytes, u64 min_size,
9534 loff_t actual_len, u64 *alloc_hint,
9535 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009536{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009537 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009538 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9539 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009540 struct btrfs_root *root = BTRFS_I(inode)->root;
9541 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009542 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009543 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009544 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009545 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009546 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009547 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009548 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009549 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009550
Josef Bacik0af3d002010-06-21 14:48:16 -04009551 if (trans)
9552 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009553 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009554 if (own_trans) {
9555 trans = btrfs_start_transaction(root, 3);
9556 if (IS_ERR(trans)) {
9557 ret = PTR_ERR(trans);
9558 break;
9559 }
Yan Zhengd899e052008-10-30 14:25:28 -04009560 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009561
Byongho Leeee221842015-12-15 01:42:10 +09009562 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009563 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009564 /*
9565 * If we are severely fragmented we could end up with really
9566 * small allocations, so if the allocator is returning small
9567 * chunks lets make its job easier by only searching for those
9568 * sized chunks.
9569 */
9570 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009571 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9572 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009573 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009574 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009575 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009576 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009577 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009578
9579 /*
9580 * We've reserved this space, and thus converted it from
9581 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9582 * from here on out we will only need to clear our reservation
9583 * for the remaining unreserved area, so advance our
9584 * clear_offset by our extent size.
9585 */
9586 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009587 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009588
Josef Bacik0b670dc2015-09-23 17:11:16 -04009589 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04009590 ret = insert_reserved_file_extent(trans, inode,
9591 cur_offset, ins.objectid,
9592 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009593 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009594 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009595 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009596 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009597 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009598 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009599 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009600 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009601 break;
9602 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009603
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009604 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009605 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009606
Josef Bacik5dc562c2012-08-17 13:14:17 -04009607 em = alloc_extent_map();
9608 if (!em) {
9609 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9610 &BTRFS_I(inode)->runtime_flags);
9611 goto next;
9612 }
9613
9614 em->start = cur_offset;
9615 em->orig_start = cur_offset;
9616 em->len = ins.offset;
9617 em->block_start = ins.objectid;
9618 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009619 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009620 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009621 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9622 em->generation = trans->transid;
9623
9624 while (1) {
9625 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009626 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009627 write_unlock(&em_tree->lock);
9628 if (ret != -EEXIST)
9629 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009630 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009631 cur_offset + ins.offset - 1,
9632 0);
9633 }
9634 free_extent_map(em);
9635next:
Yan Zhengd899e052008-10-30 14:25:28 -04009636 num_bytes -= ins.offset;
9637 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009638 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009639
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009640 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009641 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009642 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009643 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009644 (actual_len > inode->i_size) &&
9645 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009646 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009647 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009648 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009649 i_size = cur_offset;
9650 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009651 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009652 }
9653
Yan Zhengd899e052008-10-30 14:25:28 -04009654 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009655
9656 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009657 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009658 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009659 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009660 break;
9661 }
Yan Zhengd899e052008-10-30 14:25:28 -04009662
Josef Bacik0af3d002010-06-21 14:48:16 -04009663 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009664 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009665 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009666 if (clear_offset < end)
9667 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
9668 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009669 return ret;
9670}
9671
Josef Bacik0af3d002010-06-21 14:48:16 -04009672int btrfs_prealloc_file_range(struct inode *inode, int mode,
9673 u64 start, u64 num_bytes, u64 min_size,
9674 loff_t actual_len, u64 *alloc_hint)
9675{
9676 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9677 min_size, actual_len, alloc_hint,
9678 NULL);
9679}
9680
9681int btrfs_prealloc_file_range_trans(struct inode *inode,
9682 struct btrfs_trans_handle *trans, int mode,
9683 u64 start, u64 num_bytes, u64 min_size,
9684 loff_t actual_len, u64 *alloc_hint)
9685{
9686 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9687 min_size, actual_len, alloc_hint, trans);
9688}
9689
Chris Masone6dcd2d2008-07-17 12:53:50 -04009690static int btrfs_set_page_dirty(struct page *page)
9691{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009692 return __set_page_dirty_nobuffers(page);
9693}
9694
Al Viro10556cb2011-06-20 19:28:19 -04009695static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009696{
Li Zefanb83cc962010-12-20 16:04:08 +08009697 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009698 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009699
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009700 if (mask & MAY_WRITE &&
9701 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9702 if (btrfs_root_readonly(root))
9703 return -EROFS;
9704 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9705 return -EACCES;
9706 }
Al Viro2830ba72011-06-20 19:16:29 -04009707 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009708}
Chris Mason39279cc2007-06-12 06:35:45 -04009709
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009710static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9711{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009712 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009713 struct btrfs_trans_handle *trans;
9714 struct btrfs_root *root = BTRFS_I(dir)->root;
9715 struct inode *inode = NULL;
9716 u64 objectid;
9717 u64 index;
9718 int ret = 0;
9719
9720 /*
9721 * 5 units required for adding orphan entry
9722 */
9723 trans = btrfs_start_transaction(root, 5);
9724 if (IS_ERR(trans))
9725 return PTR_ERR(trans);
9726
9727 ret = btrfs_find_free_ino(root, &objectid);
9728 if (ret)
9729 goto out;
9730
9731 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009732 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009733 if (IS_ERR(inode)) {
9734 ret = PTR_ERR(inode);
9735 inode = NULL;
9736 goto out;
9737 }
9738
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009739 inode->i_fop = &btrfs_file_operations;
9740 inode->i_op = &btrfs_file_inode_operations;
9741
9742 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009743 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9744
Chris Masonb0d5d102014-09-08 13:08:51 -07009745 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9746 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009747 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009748
9749 ret = btrfs_update_inode(trans, root, inode);
9750 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009751 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009752 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009753 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009754 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009755
Filipe Manana5762b5c2014-08-01 00:10:32 +01009756 /*
9757 * We set number of links to 0 in btrfs_new_inode(), and here we set
9758 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9759 * through:
9760 *
9761 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9762 */
9763 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009764 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009765 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009766 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009767out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009768 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009769 if (ret && inode)
9770 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009771 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009772 return ret;
9773}
9774
David Sterba5cdc84b2018-07-18 20:32:52 +02009775void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009776{
David Sterba5cdc84b2018-07-18 20:32:52 +02009777 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009778 unsigned long index = start >> PAGE_SHIFT;
9779 unsigned long end_index = end >> PAGE_SHIFT;
9780 struct page *page;
9781
9782 while (index <= end_index) {
9783 page = find_get_page(inode->i_mapping, index);
9784 ASSERT(page); /* Pages should be in the extent_io_tree */
9785 set_page_writeback(page);
9786 put_page(page);
9787 index++;
9788 }
9789}
9790
Omar Sandovaled46ff32016-11-03 10:28:14 -07009791#ifdef CONFIG_SWAP
9792/*
9793 * Add an entry indicating a block group or device which is pinned by a
9794 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9795 * negative errno on failure.
9796 */
9797static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9798 bool is_block_group)
9799{
9800 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9801 struct btrfs_swapfile_pin *sp, *entry;
9802 struct rb_node **p;
9803 struct rb_node *parent = NULL;
9804
9805 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9806 if (!sp)
9807 return -ENOMEM;
9808 sp->ptr = ptr;
9809 sp->inode = inode;
9810 sp->is_block_group = is_block_group;
9811
9812 spin_lock(&fs_info->swapfile_pins_lock);
9813 p = &fs_info->swapfile_pins.rb_node;
9814 while (*p) {
9815 parent = *p;
9816 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9817 if (sp->ptr < entry->ptr ||
9818 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9819 p = &(*p)->rb_left;
9820 } else if (sp->ptr > entry->ptr ||
9821 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9822 p = &(*p)->rb_right;
9823 } else {
9824 spin_unlock(&fs_info->swapfile_pins_lock);
9825 kfree(sp);
9826 return 1;
9827 }
9828 }
9829 rb_link_node(&sp->node, parent, p);
9830 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9831 spin_unlock(&fs_info->swapfile_pins_lock);
9832 return 0;
9833}
9834
9835/* Free all of the entries pinned by this swapfile. */
9836static void btrfs_free_swapfile_pins(struct inode *inode)
9837{
9838 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9839 struct btrfs_swapfile_pin *sp;
9840 struct rb_node *node, *next;
9841
9842 spin_lock(&fs_info->swapfile_pins_lock);
9843 node = rb_first(&fs_info->swapfile_pins);
9844 while (node) {
9845 next = rb_next(node);
9846 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9847 if (sp->inode == inode) {
9848 rb_erase(&sp->node, &fs_info->swapfile_pins);
9849 if (sp->is_block_group)
9850 btrfs_put_block_group(sp->ptr);
9851 kfree(sp);
9852 }
9853 node = next;
9854 }
9855 spin_unlock(&fs_info->swapfile_pins_lock);
9856}
9857
9858struct btrfs_swap_info {
9859 u64 start;
9860 u64 block_start;
9861 u64 block_len;
9862 u64 lowest_ppage;
9863 u64 highest_ppage;
9864 unsigned long nr_pages;
9865 int nr_extents;
9866};
9867
9868static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9869 struct btrfs_swap_info *bsi)
9870{
9871 unsigned long nr_pages;
9872 u64 first_ppage, first_ppage_reported, next_ppage;
9873 int ret;
9874
9875 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9876 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9877 PAGE_SIZE) >> PAGE_SHIFT;
9878
9879 if (first_ppage >= next_ppage)
9880 return 0;
9881 nr_pages = next_ppage - first_ppage;
9882
9883 first_ppage_reported = first_ppage;
9884 if (bsi->start == 0)
9885 first_ppage_reported++;
9886 if (bsi->lowest_ppage > first_ppage_reported)
9887 bsi->lowest_ppage = first_ppage_reported;
9888 if (bsi->highest_ppage < (next_ppage - 1))
9889 bsi->highest_ppage = next_ppage - 1;
9890
9891 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
9892 if (ret < 0)
9893 return ret;
9894 bsi->nr_extents += ret;
9895 bsi->nr_pages += nr_pages;
9896 return 0;
9897}
9898
9899static void btrfs_swap_deactivate(struct file *file)
9900{
9901 struct inode *inode = file_inode(file);
9902
9903 btrfs_free_swapfile_pins(inode);
9904 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
9905}
9906
9907static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
9908 sector_t *span)
9909{
9910 struct inode *inode = file_inode(file);
9911 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9912 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9913 struct extent_state *cached_state = NULL;
9914 struct extent_map *em = NULL;
9915 struct btrfs_device *device = NULL;
9916 struct btrfs_swap_info bsi = {
9917 .lowest_ppage = (sector_t)-1ULL,
9918 };
9919 int ret = 0;
9920 u64 isize;
9921 u64 start;
9922
9923 /*
9924 * If the swap file was just created, make sure delalloc is done. If the
9925 * file changes again after this, the user is doing something stupid and
9926 * we don't really care.
9927 */
9928 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
9929 if (ret)
9930 return ret;
9931
9932 /*
9933 * The inode is locked, so these flags won't change after we check them.
9934 */
9935 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
9936 btrfs_warn(fs_info, "swapfile must not be compressed");
9937 return -EINVAL;
9938 }
9939 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
9940 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
9941 return -EINVAL;
9942 }
9943 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
9944 btrfs_warn(fs_info, "swapfile must not be checksummed");
9945 return -EINVAL;
9946 }
9947
9948 /*
9949 * Balance or device remove/replace/resize can move stuff around from
9950 * under us. The EXCL_OP flag makes sure they aren't running/won't run
9951 * concurrently while we are mapping the swap extents, and
9952 * fs_info->swapfile_pins prevents them from running while the swap file
9953 * is active and moving the extents. Note that this also prevents a
9954 * concurrent device add which isn't actually necessary, but it's not
9955 * really worth the trouble to allow it.
9956 */
9957 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
9958 btrfs_warn(fs_info,
9959 "cannot activate swapfile while exclusive operation is running");
9960 return -EBUSY;
9961 }
9962 /*
9963 * Snapshots can create extents which require COW even if NODATACOW is
9964 * set. We use this counter to prevent snapshots. We must increment it
9965 * before walking the extents because we don't want a concurrent
9966 * snapshot to run after we've already checked the extents.
9967 */
9968 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
9969
9970 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
9971
9972 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
9973 start = 0;
9974 while (start < isize) {
9975 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +01009976 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -07009977 u64 len = isize - start;
9978
Omar Sandoval39b07b52019-12-02 17:34:23 -08009979 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -07009980 if (IS_ERR(em)) {
9981 ret = PTR_ERR(em);
9982 goto out;
9983 }
9984
9985 if (em->block_start == EXTENT_MAP_HOLE) {
9986 btrfs_warn(fs_info, "swapfile must not have holes");
9987 ret = -EINVAL;
9988 goto out;
9989 }
9990 if (em->block_start == EXTENT_MAP_INLINE) {
9991 /*
9992 * It's unlikely we'll ever actually find ourselves
9993 * here, as a file small enough to fit inline won't be
9994 * big enough to store more than the swap header, but in
9995 * case something changes in the future, let's catch it
9996 * here rather than later.
9997 */
9998 btrfs_warn(fs_info, "swapfile must not be inline");
9999 ret = -EINVAL;
10000 goto out;
10001 }
10002 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10003 btrfs_warn(fs_info, "swapfile must not be compressed");
10004 ret = -EINVAL;
10005 goto out;
10006 }
10007
10008 logical_block_start = em->block_start + (start - em->start);
10009 len = min(len, em->len - (start - em->start));
10010 free_extent_map(em);
10011 em = NULL;
10012
10013 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10014 if (ret < 0) {
10015 goto out;
10016 } else if (ret) {
10017 ret = 0;
10018 } else {
10019 btrfs_warn(fs_info,
10020 "swapfile must not be copy-on-write");
10021 ret = -EINVAL;
10022 goto out;
10023 }
10024
10025 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10026 if (IS_ERR(em)) {
10027 ret = PTR_ERR(em);
10028 goto out;
10029 }
10030
10031 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10032 btrfs_warn(fs_info,
10033 "swapfile must have single data profile");
10034 ret = -EINVAL;
10035 goto out;
10036 }
10037
10038 if (device == NULL) {
10039 device = em->map_lookup->stripes[0].dev;
10040 ret = btrfs_add_swapfile_pin(inode, device, false);
10041 if (ret == 1)
10042 ret = 0;
10043 else if (ret)
10044 goto out;
10045 } else if (device != em->map_lookup->stripes[0].dev) {
10046 btrfs_warn(fs_info, "swapfile must be on one device");
10047 ret = -EINVAL;
10048 goto out;
10049 }
10050
10051 physical_block_start = (em->map_lookup->stripes[0].physical +
10052 (logical_block_start - em->start));
10053 len = min(len, em->len - (logical_block_start - em->start));
10054 free_extent_map(em);
10055 em = NULL;
10056
10057 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10058 if (!bg) {
10059 btrfs_warn(fs_info,
10060 "could not find block group containing swapfile");
10061 ret = -EINVAL;
10062 goto out;
10063 }
10064
10065 ret = btrfs_add_swapfile_pin(inode, bg, true);
10066 if (ret) {
10067 btrfs_put_block_group(bg);
10068 if (ret == 1)
10069 ret = 0;
10070 else
10071 goto out;
10072 }
10073
10074 if (bsi.block_len &&
10075 bsi.block_start + bsi.block_len == physical_block_start) {
10076 bsi.block_len += len;
10077 } else {
10078 if (bsi.block_len) {
10079 ret = btrfs_add_swap_extent(sis, &bsi);
10080 if (ret)
10081 goto out;
10082 }
10083 bsi.start = start;
10084 bsi.block_start = physical_block_start;
10085 bsi.block_len = len;
10086 }
10087
10088 start += len;
10089 }
10090
10091 if (bsi.block_len)
10092 ret = btrfs_add_swap_extent(sis, &bsi);
10093
10094out:
10095 if (!IS_ERR_OR_NULL(em))
10096 free_extent_map(em);
10097
10098 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10099
10100 if (ret)
10101 btrfs_swap_deactivate(file);
10102
10103 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10104
10105 if (ret)
10106 return ret;
10107
10108 if (device)
10109 sis->bdev = device->bdev;
10110 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10111 sis->max = bsi.nr_pages;
10112 sis->pages = bsi.nr_pages - 1;
10113 sis->highest_bit = bsi.nr_pages - 1;
10114 return bsi.nr_extents;
10115}
10116#else
10117static void btrfs_swap_deactivate(struct file *file)
10118{
10119}
10120
10121static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10122 sector_t *span)
10123{
10124 return -EOPNOTSUPP;
10125}
10126#endif
10127
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010128static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010129 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010130 .lookup = btrfs_lookup,
10131 .create = btrfs_create,
10132 .unlink = btrfs_unlink,
10133 .link = btrfs_link,
10134 .mkdir = btrfs_mkdir,
10135 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010136 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010137 .symlink = btrfs_symlink,
10138 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010139 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010140 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010141 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010142 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010143 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010144 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010145 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010146};
Yan, Zheng76dda932009-09-21 16:00:26 -040010147
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010148static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010149 .llseek = generic_file_llseek,
10150 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010151 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010152 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010153 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010154#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010155 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010156#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010157 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010158 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010159};
10160
David Sterba20e55062015-11-19 11:42:28 +010010161static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010162 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010163 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010164 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10165};
10166
Chris Mason35054392009-01-21 13:11:13 -050010167/*
10168 * btrfs doesn't support the bmap operation because swapfiles
10169 * use bmap to make a mapping of extents in the file. They assume
10170 * these extents won't change over the life of the file and they
10171 * use the bmap result to do IO directly to the drive.
10172 *
10173 * the btrfs bmap call would return logical addresses that aren't
10174 * suitable for IO and they also will change frequently as COW
10175 * operations happen. So, swapfile + btrfs == corruption.
10176 *
10177 * For now we're avoiding this by dropping bmap.
10178 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010179static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010180 .readpage = btrfs_readpage,
10181 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010182 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010183 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010184 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010185 .invalidatepage = btrfs_invalidatepage,
10186 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010187#ifdef CONFIG_MIGRATION
10188 .migratepage = btrfs_migratepage,
10189#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010190 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010191 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010192 .swap_activate = btrfs_swap_activate,
10193 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010194};
10195
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010196static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010197 .getattr = btrfs_getattr,
10198 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010199 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010200 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010201 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010202 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010203 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010204 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010205};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010206static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010207 .getattr = btrfs_getattr,
10208 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010209 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010210 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010211 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010212 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010213 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010214};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010215static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010216 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010217 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010218 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010219 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010220 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010221 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010222};
Yan, Zheng76dda932009-09-21 16:00:26 -040010223
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010224const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010225 .d_delete = btrfs_dentry_delete,
10226};