blob: 3ea694ee1c901f9136cd1c3e75ea4defbdcf57bb [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Mason8f18cf12008-04-25 16:53:30 -04006#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05007#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -04008#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080031#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030032#include <linux/sched/mm.h>
David Sterba92d32172018-04-16 21:10:14 +020033#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020034#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040035#include "ctree.h"
36#include "disk-io.h"
37#include "transaction.h"
38#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040040#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040041#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040042#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020043#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040044#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050045#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050046#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080047#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000048#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080049#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040050#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040051#include "block-group.h"
Chris Mason39279cc2007-06-12 06:35:45 -040052
53struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080054 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040055 struct btrfs_root *root;
56};
57
Filipe Mananaf28a4922015-12-08 19:23:20 +000058struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000059 u64 reserve;
60 u64 unsubmitted_oe_range_start;
61 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080062 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000063};
64
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070065static const struct inode_operations btrfs_dir_inode_operations;
66static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070067static const struct inode_operations btrfs_special_inode_operations;
68static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070069static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070070static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010071static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
74struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050076struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000077struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078
Eric Sandeen3972f262013-01-12 02:57:22 +000079static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000080static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040081static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050082static noinline int cow_file_range(struct inode *inode,
83 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030084 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030085 unsigned long *nr_written, int unlock);
Liu Bo6f9994d2017-01-31 07:50:22 -080086static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
87 u64 orig_start, u64 block_start,
88 u64 block_len, u64 orig_block_len,
89 u64 ram_bytes, int compress_type,
90 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040091
Qu Wenruo524272602017-03-08 10:25:52 +080092static void __endio_write_update_ordered(struct inode *inode,
93 const u64 offset, const u64 bytes,
94 const bool uptodate);
95
96/*
97 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +010098 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +080099 *
100 * NOTE: caller must ensure that when an error happens, it can not call
101 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
102 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
103 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200104 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800105 */
106static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200107 struct page *locked_page,
108 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800109{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900110 unsigned long index = offset >> PAGE_SHIFT;
111 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200112 u64 page_start = page_offset(locked_page);
113 u64 page_end = page_start + PAGE_SIZE - 1;
114
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900115 struct page *page;
116
117 while (index <= end_index) {
118 page = find_get_page(inode->i_mapping, index);
119 index++;
120 if (!page)
121 continue;
122 ClearPagePrivate2(page);
123 put_page(page);
124 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200125
126 /*
127 * In case this page belongs to the delalloc range being instantiated
128 * then skip it, since the first page of a range is going to be
129 * properly cleaned up by the caller of run_delalloc_range
130 */
131 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
132 offset += PAGE_SIZE;
133 bytes -= PAGE_SIZE;
134 }
135
136 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800137}
138
Eric Sandeen48a3b632013-04-25 20:41:01 +0000139static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400140
Josef Bacik6a3891c2015-03-16 17:38:52 -0400141#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
142void btrfs_test_inode_set_ops(struct inode *inode)
143{
144 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
145}
146#endif
147
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000148static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500149 struct inode *inode, struct inode *dir,
150 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500151{
152 int err;
153
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000154 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500155 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500156 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500157 return err;
158}
159
Chris Masond352ac62008-09-29 15:18:18 -0400160/*
Chris Masonc8b97812008-10-29 14:49:59 -0400161 * this does all the hard work for inserting an inline extent into
162 * the btree. The caller should have done a btrfs_drop_extents so that
163 * no overlapping inline items exist in the btree
164 */
Chris Mason40f76582014-05-21 13:35:51 -0700165static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000166 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400167 struct btrfs_root *root, struct inode *inode,
168 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000169 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400170 struct page **compressed_pages)
171{
Chris Masonc8b97812008-10-29 14:49:59 -0400172 struct extent_buffer *leaf;
173 struct page *page = NULL;
174 char *kaddr;
175 unsigned long ptr;
176 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400177 int ret;
178 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400179 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400180
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800181 ASSERT((compressed_size > 0 && compressed_pages) ||
182 (compressed_size == 0 && !compressed_pages));
183
Li Zefanfe3f5662011-03-28 08:30:38 +0000184 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400185 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400186
Chris Masonc8b97812008-10-29 14:49:59 -0400187 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000188
189 if (!extent_inserted) {
190 struct btrfs_key key;
191 size_t datasize;
192
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200193 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000194 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200195 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000196
197 datasize = btrfs_file_extent_calc_inline_size(cur_size);
198 path->leave_spinning = 1;
199 ret = btrfs_insert_empty_item(trans, root, path, &key,
200 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200201 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000202 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400203 }
204 leaf = path->nodes[0];
205 ei = btrfs_item_ptr(leaf, path->slots[0],
206 struct btrfs_file_extent_item);
207 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
208 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
209 btrfs_set_file_extent_encryption(leaf, ei, 0);
210 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
211 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
212 ptr = btrfs_file_extent_inline_start(ei);
213
Li Zefan261507a02010-12-17 14:21:50 +0800214 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400215 struct page *cpage;
216 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500217 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400218 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500219 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300220 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400221
Cong Wang7ac687d2011-11-25 23:14:28 +0800222 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400223 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800224 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400225
226 i++;
227 ptr += cur_size;
228 compressed_size -= cur_size;
229 }
230 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800231 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400232 } else {
233 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300234 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800236 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100237 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800239 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300240 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 }
242 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000243 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400244
Yan, Zhengc2167752009-11-12 09:34:21 +0000245 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500246 * We align size to sectorsize for inline extents just for simplicity
247 * sake.
248 */
249 size = ALIGN(size, root->fs_info->sectorsize);
250 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
251 if (ret)
252 goto fail;
253
254 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000255 * we're an inline extent, so nobody can
256 * extend the file past i_size without locking
257 * a page we already have locked.
258 *
259 * We must do any isize and inode updates
260 * before we unlock the pages. Otherwise we
261 * could end up racing with unlink.
262 */
Chris Masonc8b97812008-10-29 14:49:59 -0400263 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100264 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000265
Chris Masonc8b97812008-10-29 14:49:59 -0400266fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200267 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400268}
269
270
271/*
272 * conditionally insert an inline extent into the file. This
273 * does the checks required to make sure the data is small enough
274 * to fit as an inline extent.
275 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200276static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400277 u64 end, size_t compressed_size,
278 int compress_type,
279 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400280{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200281 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400282 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400283 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400284 u64 isize = i_size_read(inode);
285 u64 actual_end = min(end + 1, isize);
286 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400287 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400288 u64 data_len = inline_len;
289 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000290 struct btrfs_path *path;
291 int extent_inserted = 0;
292 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400293
294 if (compressed_size)
295 data_len = compressed_size;
296
297 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400298 actual_end > fs_info->sectorsize ||
299 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400300 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400302 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400303 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400304 return 1;
305 }
306
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000307 path = btrfs_alloc_path();
308 if (!path)
309 return -ENOMEM;
310
Josef Bacik00361582013-08-14 14:02:47 -0400311 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000312 if (IS_ERR(trans)) {
313 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400314 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400316 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400317
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 if (compressed_size && compressed_pages)
319 extent_item_size = btrfs_file_extent_calc_inline_size(
320 compressed_size);
321 else
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 inline_len);
324
325 ret = __btrfs_drop_extents(trans, root, inode, path,
326 start, aligned_end, NULL,
327 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400328 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400329 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400330 goto out;
331 }
Chris Masonc8b97812008-10-29 14:49:59 -0400332
333 if (isize > actual_end)
334 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000335 ret = insert_inline_extent(trans, path, extent_inserted,
336 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400337 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000338 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400339 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400340 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400341 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400343 ret = 1;
344 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100345 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400346
Josef Bacikbdc20e62013-02-28 13:23:38 -0500347 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200348 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400349out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800350 /*
351 * Don't forget to free the reserved space, as for inlined extent
352 * it won't count as data extent, free them directly here.
353 * And at reserve time, it's always aligned to page size, so
354 * just free one page here.
355 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800356 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000357 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400358 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400359 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400360}
361
Chris Mason771ed682008-11-06 22:02:51 -0500362struct async_extent {
363 u64 start;
364 u64 ram_size;
365 u64 compressed_size;
366 struct page **pages;
367 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800368 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500369 struct list_head list;
370};
371
Nikolay Borisov97db1202019-03-12 17:20:24 +0200372struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500373 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500374 struct page *locked_page;
375 u64 start;
376 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600377 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500378 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700379 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500380 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200381 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500382};
383
Nikolay Borisov97db1202019-03-12 17:20:24 +0200384struct async_cow {
385 /* Number of chunks in flight; must be first in the structure */
386 atomic_t num_chunks;
387 struct async_chunk chunks[];
388};
389
390static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500391 u64 start, u64 ram_size,
392 u64 compressed_size,
393 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800394 unsigned long nr_pages,
395 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500396{
397 struct async_extent *async_extent;
398
399 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100400 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500401 async_extent->start = start;
402 async_extent->ram_size = ram_size;
403 async_extent->compressed_size = compressed_size;
404 async_extent->pages = pages;
405 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800406 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500407 list_add_tail(&async_extent->list, &cow->extents);
408 return 0;
409}
410
Qu Wenruo42c16da2019-07-01 05:12:46 +0000411/*
412 * Check if the inode has flags compatible with compression
413 */
414static inline bool inode_can_compress(struct inode *inode)
415{
416 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
417 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
418 return false;
419 return true;
420}
421
422/*
423 * Check if the inode needs to be submitted to compression, based on mount
424 * options, defragmentation, properties or heuristics.
425 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300426static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800427{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400428 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800429
Qu Wenruo42c16da2019-07-01 05:12:46 +0000430 if (!inode_can_compress(inode)) {
431 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
432 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
433 btrfs_ino(BTRFS_I(inode)));
434 return 0;
435 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800436 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400437 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800438 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200439 /* defrag ioctl */
440 if (BTRFS_I(inode)->defrag_compress)
441 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800442 /* bad compression ratios */
443 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
444 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400445 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800446 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200447 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300448 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800449 return 0;
450}
451
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200452static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800453 u64 start, u64 end, u64 num_bytes, u64 small_write)
454{
455 /* If this is a small write inside eof, kick off a defrag */
456 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200457 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800458 btrfs_add_inode_defrag(NULL, inode);
459}
460
Chris Masonc8b97812008-10-29 14:49:59 -0400461/*
Chris Mason771ed682008-11-06 22:02:51 -0500462 * we create compressed extents in two phases. The first
463 * phase compresses a range of pages that have already been
464 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400465 *
Chris Mason771ed682008-11-06 22:02:51 -0500466 * This is done inside an ordered work queue, and the compression
467 * is spread across many cpus. The actual IO submission is step
468 * two, and the ordered work queue takes care of making sure that
469 * happens in the same order things were put onto the queue by
470 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400471 *
Chris Mason771ed682008-11-06 22:02:51 -0500472 * If this code finds it can't get good compression, it puts an
473 * entry onto the work queue to write the uncompressed bytes. This
474 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300475 * are written in the same order that the flusher thread sent them
476 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400477 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300478static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400479{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200480 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400481 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400482 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200483 u64 start = async_chunk->start;
484 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400485 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400486 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400487 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400488 struct page **pages = NULL;
489 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400490 unsigned long total_compressed = 0;
491 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400492 int i;
493 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400494 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300495 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400496 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400497
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200498 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
499 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400500
Josef Bacikd98da492019-10-11 09:03:54 -0400501 /*
502 * We need to save i_size before now because it could change in between
503 * us evaluating the size and assigning it. This is because we lock and
504 * unlock the page in truncate and fallocate, and then modify the i_size
505 * later on.
506 *
507 * The barriers are to emulate READ_ONCE, remove that once i_size_read
508 * does that for us.
509 */
510 barrier();
511 i_size = i_size_read(inode);
512 barrier();
513 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400514again:
515 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300516 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100517 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
518 nr_pages = min_t(unsigned long, nr_pages,
519 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400520
Chris Masonf03d9301f2009-02-04 09:31:06 -0500521 /*
522 * we don't want to send crud past the end of i_size through
523 * compression, that's just a waste of CPU time. So, if the
524 * end of the file is before the start of our current
525 * requested range of bytes, we bail out to the uncompressed
526 * cleanup code that can deal with all of this.
527 *
528 * It isn't really the fastest way to fix things, but this is a
529 * very uncommon corner.
530 */
531 if (actual_end <= start)
532 goto cleanup_and_bail_uncompressed;
533
Chris Masonc8b97812008-10-29 14:49:59 -0400534 total_compressed = actual_end - start;
535
Shilong Wang4bcbb332014-10-07 18:44:35 -0400536 /*
537 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400538 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400539 */
540 if (total_compressed <= blocksize &&
541 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
542 goto cleanup_and_bail_uncompressed;
543
David Sterba069eac72017-02-14 19:36:54 +0100544 total_compressed = min_t(unsigned long, total_compressed,
545 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400546 total_in = 0;
547 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400548
Chris Mason771ed682008-11-06 22:02:51 -0500549 /*
550 * we do compression for mount -o compress and when the
551 * inode has not been flagged as nocompress. This flag can
552 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400553 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300554 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400555 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100556 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800557 if (!pages) {
558 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100559 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800560 goto cont;
561 }
Chris Mason179e29e2007-11-01 11:28:41 -0400562
David Sterbaeec63c62017-07-17 19:41:31 +0200563 if (BTRFS_I(inode)->defrag_compress)
564 compress_type = BTRFS_I(inode)->defrag_compress;
565 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200566 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800567
Chris Mason4adaa612013-03-26 13:07:00 -0400568 /*
569 * we need to call clear_page_dirty_for_io on each
570 * page in the range. Otherwise applications with the file
571 * mmap'd can wander in and change the page contents while
572 * we are compressing them.
573 *
574 * If the compression fails for any reason, we set the pages
575 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300576 *
577 * Note that the remaining part is redirtied, the start pointer
578 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400579 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300580 if (!redirty) {
581 extent_range_clear_dirty_for_io(inode, start, end);
582 redirty = 1;
583 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200584
585 /* Compression level is applied here and only here */
586 ret = btrfs_compress_pages(
587 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800588 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100589 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100590 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800591 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100592 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400593
594 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100595 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100596 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400597 char *kaddr;
598
599 /* zero the tail end of the last page, we might be
600 * sending it down to disk
601 */
602 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800603 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400604 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300605 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800606 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400607 }
608 will_compress = 1;
609 }
610 }
Li Zefan560f7d72011-09-08 10:22:01 +0800611cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400612 if (start == 0) {
613 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300614 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400615 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500616 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400617 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200618 ret = cow_file_range_inline(inode, start, end, 0,
619 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400620 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500621 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200622 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000623 total_compressed,
624 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400625 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100626 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400627 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400628 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
629 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100630 unsigned long page_error_op;
631
Filipe Mananae6eb4312014-10-10 10:45:12 +0100632 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400633
Chris Mason771ed682008-11-06 22:02:51 -0500634 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100635 * inline extent creation worked or returned error,
636 * we don't need to create any more async work items.
637 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400638 *
639 * We use DO_ACCOUNTING here because we need the
640 * delalloc_release_metadata to be done _after_ we drop
641 * our outstanding extent for clearing delalloc for this
642 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500643 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300644 extent_clear_unlock_delalloc(inode, start, end, NULL,
645 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800646 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400647 PAGE_CLEAR_DIRTY |
648 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100649 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400650 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300651
652 for (i = 0; i < nr_pages; i++) {
653 WARN_ON(pages[i]->mapping);
654 put_page(pages[i]);
655 }
656 kfree(pages);
657
658 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400659 }
660 }
661
662 if (will_compress) {
663 /*
664 * we aren't doing an inline extent round the compressed size
665 * up to a block size boundary so the allocator does sane
666 * things
667 */
Qu Wenruofda28322013-02-26 08:10:22 +0000668 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400669
670 /*
671 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300672 * win, compare the page count read with the blocks on disk,
673 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400674 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300675 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300676 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300677 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700678
679 /*
680 * The async work queues will take care of doing actual
681 * allocation on disk for these compressed pages, and
682 * will submit them to the elevator.
683 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200684 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100685 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700686 compress_type);
687
Timofey Titovets11708622017-10-03 18:06:01 +0300688 if (start + total_in < end) {
689 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700690 pages = NULL;
691 cond_resched();
692 goto again;
693 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300694 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400695 }
696 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700697 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400698 /*
699 * the compression code ran but failed to make things smaller,
700 * free any pages it allocated and our page pointer array
701 */
David Sterba4d3a8002017-02-14 19:04:07 +0100702 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400703 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300704 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400705 }
706 kfree(pages);
707 pages = NULL;
708 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100709 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400710
711 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400712 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200713 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500714 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500715 }
Chris Masonc8b97812008-10-29 14:49:59 -0400716 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500717cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700718 /*
719 * No compression, but we still need to write the pages in the file
720 * we've been given so far. redirty the locked page if it corresponds
721 * to our extent and set things up for the async work queue to run
722 * cow_file_range to do the normal delalloc dance.
723 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700724 if (async_chunk->locked_page &&
725 (page_offset(async_chunk->locked_page) >= start &&
726 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200727 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700728 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700729 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700730
731 if (redirty)
732 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200733 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700734 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300735 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500736
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300737 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500738}
Chris Mason771ed682008-11-06 22:02:51 -0500739
Filipe Manana40ae8372014-10-06 22:14:24 +0100740static void free_async_extent_pages(struct async_extent *async_extent)
741{
742 int i;
743
744 if (!async_extent->pages)
745 return;
746
747 for (i = 0; i < async_extent->nr_pages; i++) {
748 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300749 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100750 }
751 kfree(async_extent->pages);
752 async_extent->nr_pages = 0;
753 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500754}
755
756/*
757 * phase two of compressed writeback. This is the ordered portion
758 * of the code, which only gets called in the order the work was
759 * queued. We walk all the async extents created by compress_file_range
760 * and send them down to the disk.
761 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200762static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500763{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200764 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400765 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500766 struct async_extent *async_extent;
767 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500768 struct btrfs_key ins;
769 struct extent_map *em;
770 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200771 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500772 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500773
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500774again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200775 while (!list_empty(&async_chunk->extents)) {
776 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500777 struct async_extent, list);
778 list_del(&async_extent->list);
779
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500780retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200781 lock_extent(io_tree, async_extent->start,
782 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500783 /* did the compression code fall back to uncompressed IO? */
784 if (!async_extent->pages) {
785 int page_started = 0;
786 unsigned long nr_written = 0;
787
Chris Mason771ed682008-11-06 22:02:51 -0500788 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200789 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500790 async_extent->start,
791 async_extent->start +
792 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300793 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500794
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100795 /* JDM XXX */
796
Chris Mason771ed682008-11-06 22:02:51 -0500797 /*
798 * if page_started, cow_file_range inserted an
799 * inline extent and took care of all the unlocking
800 * and IO for us. Otherwise, we need to submit
801 * all those pages down to the drive.
802 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500803 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200804 extent_write_locked_range(inode,
805 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500806 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500807 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500808 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700809 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200810 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500811 kfree(async_extent);
812 cond_resched();
813 continue;
814 }
815
Wang Xiaoguang18513092016-07-25 15:51:40 +0800816 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500817 async_extent->compressed_size,
818 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800819 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500820 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100821 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500822
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400823 if (ret == -ENOSPC) {
824 unlock_extent(io_tree, async_extent->start,
825 async_extent->start +
826 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800827
828 /*
829 * we need to redirty the pages if we decide to
830 * fallback to uncompressed IO, otherwise we
831 * will not submit these pages down to lower
832 * layers.
833 */
834 extent_range_redirty_for_io(inode,
835 async_extent->start,
836 async_extent->start +
837 async_extent->ram_size - 1);
838
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100839 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400840 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500841 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500842 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000843 /*
844 * here we're doing allocation and writeback of the
845 * compressed pages
846 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800847 em = create_io_em(inode, async_extent->start,
848 async_extent->ram_size, /* len */
849 async_extent->start, /* orig_start */
850 ins.objectid, /* block_start */
851 ins.offset, /* block_len */
852 ins.offset, /* orig_block_len */
853 async_extent->ram_size, /* ram_bytes */
854 async_extent->compress_type,
855 BTRFS_ORDERED_COMPRESSED);
856 if (IS_ERR(em))
857 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500858 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800859 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500860
Li Zefan261507a02010-12-17 14:21:50 +0800861 ret = btrfs_add_ordered_extent_compress(inode,
862 async_extent->start,
863 ins.objectid,
864 async_extent->ram_size,
865 ins.offset,
866 BTRFS_ORDERED_COMPRESSED,
867 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100868 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200869 btrfs_drop_extent_cache(BTRFS_I(inode),
870 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100871 async_extent->start +
872 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500873 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100874 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400875 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500876
Chris Mason771ed682008-11-06 22:02:51 -0500877 /*
878 * clear dirty, set writeback and unlock the pages.
879 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400880 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400881 async_extent->start +
882 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400883 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
884 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400885 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200886 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500887 async_extent->start,
888 async_extent->ram_size,
889 ins.objectid,
890 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600891 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700892 async_chunk->write_flags,
893 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100894 struct page *p = async_extent->pages[0];
895 const u64 start = async_extent->start;
896 const u64 end = start + async_extent->ram_size - 1;
897
898 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200899 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200900
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100901 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300902 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800903 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100904 PAGE_END_WRITEBACK |
905 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100906 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100907 }
Chris Mason771ed682008-11-06 22:02:51 -0500908 alloc_hint = ins.objectid + ins.offset;
909 kfree(async_extent);
910 cond_resched();
911 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100912 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500913out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400914 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400915 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100916out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400917 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500918 async_extent->start +
919 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400920 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100921 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400922 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
923 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100924 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
925 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100926 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100927 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500928 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500929}
930
Josef Bacik4b46fce2010-05-23 11:00:55 -0400931static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
932 u64 num_bytes)
933{
934 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
935 struct extent_map *em;
936 u64 alloc_hint = 0;
937
938 read_lock(&em_tree->lock);
939 em = search_extent_mapping(em_tree, start, num_bytes);
940 if (em) {
941 /*
942 * if block start isn't an actual block number then find the
943 * first block in this inode and use that as a hint. If that
944 * block is also bogus then just don't worry about it.
945 */
946 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
947 free_extent_map(em);
948 em = search_extent_mapping(em_tree, 0, 0);
949 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
950 alloc_hint = em->block_start;
951 if (em)
952 free_extent_map(em);
953 } else {
954 alloc_hint = em->block_start;
955 free_extent_map(em);
956 }
957 }
958 read_unlock(&em_tree->lock);
959
960 return alloc_hint;
961}
962
Chris Mason771ed682008-11-06 22:02:51 -0500963/*
964 * when extent_io.c finds a delayed allocation range in the file,
965 * the call backs end up in this code. The basic idea is to
966 * allocate extents on disk for the range, and create ordered data structs
967 * in ram to track those extents.
968 *
969 * locked_page is the page that writepage had locked already. We use
970 * it to make sure we don't do extra locks or unlocks.
971 *
972 * *page_started is set to one if we unlock locked_page and do everything
973 * required to start IO on it. It may be clean and already done with
974 * IO when we return.
975 */
Josef Bacik00361582013-08-14 14:02:47 -0400976static noinline int cow_file_range(struct inode *inode,
977 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300978 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300979 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500980{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400981 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400982 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500983 u64 alloc_hint = 0;
984 u64 num_bytes;
985 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000986 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400987 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500988 struct btrfs_key ins;
989 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000990 unsigned clear_bits;
991 unsigned long page_ops;
992 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500993 int ret = 0;
994
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200995 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400996 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500997 ret = -EINVAL;
998 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400999 }
Chris Mason771ed682008-11-06 22:02:51 -05001000
Qu Wenruofda28322013-02-26 08:10:22 +00001001 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001002 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001003 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001004
Nikolay Borisov6158e1c2017-02-20 13:50:43 +02001005 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001006
Chris Mason771ed682008-11-06 22:02:51 -05001007 if (start == 0) {
1008 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001009 ret = cow_file_range_inline(inode, start, end, 0,
1010 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001011 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001012 /*
1013 * We use DO_ACCOUNTING here because we need the
1014 * delalloc_release_metadata to be run _after_ we drop
1015 * our outstanding extent for clearing delalloc for this
1016 * range.
1017 */
Nikolay Borisov74e91942019-07-17 16:18:16 +03001018 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001019 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001020 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1021 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001022 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1023 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001024 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001025 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001026 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001027 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001028 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001029 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001030 }
1031 }
Chris Masonc8b97812008-10-29 14:49:59 -04001032
Josef Bacik4b46fce2010-05-23 11:00:55 -04001033 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001034 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1035 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001036
Anand Jain3752d222018-02-15 12:29:38 +08001037 while (num_bytes > 0) {
1038 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001039 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001040 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001041 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001042 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001043 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001044 cur_alloc_size = ins.offset;
1045 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001046
Chris Mason771ed682008-11-06 22:02:51 -05001047 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001048 em = create_io_em(inode, start, ins.offset, /* len */
1049 start, /* orig_start */
1050 ins.objectid, /* block_start */
1051 ins.offset, /* block_len */
1052 ins.offset, /* orig_block_len */
1053 ram_size, /* ram_bytes */
1054 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001055 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001056 if (IS_ERR(em)) {
1057 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001058 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001059 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001060 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001061
Chris Masone6dcd2d2008-07-17 12:53:50 -04001062 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001063 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001064 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001065 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001066
Yan Zheng17d217f2008-12-12 10:03:38 -05001067 if (root->root_key.objectid ==
1068 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1069 ret = btrfs_reloc_clone_csums(inode, start,
1070 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001071 /*
1072 * Only drop cache here, and process as normal.
1073 *
1074 * We must not allow extent_clear_unlock_delalloc()
1075 * at out_unlock label to free meta of this ordered
1076 * extent, as its meta should be freed by
1077 * btrfs_finish_ordered_io().
1078 *
1079 * So we must continue until @start is increased to
1080 * skip current ordered extent.
1081 */
Josef Bacik00361582013-08-14 14:02:47 -04001082 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001083 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1084 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001085 }
1086
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001087 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001088
Chris Masonc8b97812008-10-29 14:49:59 -04001089 /* we're not doing compressed IO, don't unlock the first
1090 * page (which the caller expects to stay locked), don't
1091 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001092 *
1093 * Do set the Private2 bit so we know this page was properly
1094 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001095 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001096 page_ops = unlock ? PAGE_UNLOCK : 0;
1097 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001098
Josef Bacikc2790a22013-07-29 11:20:47 -04001099 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001100 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001101 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001102 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001103 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001104 if (num_bytes < cur_alloc_size)
1105 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001106 else
Anand Jain3752d222018-02-15 12:29:38 +08001107 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001108 alloc_hint = ins.objectid + ins.offset;
1109 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001110 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001111
1112 /*
1113 * btrfs_reloc_clone_csums() error, since start is increased
1114 * extent_clear_unlock_delalloc() at out_unlock label won't
1115 * free metadata of current ordered extent, we're OK to exit.
1116 */
1117 if (ret)
1118 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001119 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001120out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001121 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001122
Filipe Mananad9f85962014-08-25 10:43:00 +01001123out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001124 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001125out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001126 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001127 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001128out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001129 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1130 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001131 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1132 PAGE_END_WRITEBACK;
1133 /*
1134 * If we reserved an extent for our delalloc range (or a subrange) and
1135 * failed to create the respective ordered extent, then it means that
1136 * when we reserved the extent we decremented the extent's size from
1137 * the data space_info's bytes_may_use counter and incremented the
1138 * space_info's bytes_reserved counter by the same amount. We must make
1139 * sure extent_clear_unlock_delalloc() does not try to decrement again
1140 * the data space_info's bytes_may_use counter, therefore we do not pass
1141 * it the flag EXTENT_CLEAR_DATA_RESV.
1142 */
1143 if (extent_reserved) {
1144 extent_clear_unlock_delalloc(inode, start,
1145 start + cur_alloc_size,
Filipe Mananaa315e682017-03-06 23:04:20 +00001146 locked_page,
1147 clear_bits,
1148 page_ops);
1149 start += cur_alloc_size;
1150 if (start >= end)
1151 goto out;
1152 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001153 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001154 clear_bits | EXTENT_CLEAR_DATA_RESV,
1155 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001156 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001157}
Chris Masonc8b97812008-10-29 14:49:59 -04001158
Chris Mason771ed682008-11-06 22:02:51 -05001159/*
1160 * work queue call back to started compression on a file and pages
1161 */
1162static noinline void async_cow_start(struct btrfs_work *work)
1163{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001164 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001165 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001166
Nikolay Borisovb5326272019-03-12 17:20:25 +02001167 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001168
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001169 compressed_extents = compress_file_range(async_chunk);
1170 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001171 btrfs_add_delayed_iput(async_chunk->inode);
1172 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001173 }
Chris Mason771ed682008-11-06 22:02:51 -05001174}
1175
1176/*
1177 * work queue call back to submit previously compressed pages
1178 */
1179static noinline void async_cow_submit(struct btrfs_work *work)
1180{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001181 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1182 work);
1183 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001184 unsigned long nr_pages;
1185
Nikolay Borisovb5326272019-03-12 17:20:25 +02001186 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001187 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001188
David Sterba093258e2018-02-26 16:15:17 +01001189 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001190 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001191 5 * SZ_1M)
1192 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001193
Nikolay Borisov4546d172019-01-03 10:50:03 +02001194 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001195 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001196 * in which case we don't have anything to submit, yet we need to
1197 * always adjust ->async_delalloc_pages as its paired with the init
1198 * happening in cow_file_range_async
1199 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001200 if (async_chunk->inode)
1201 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001202}
Chris Masonc8b97812008-10-29 14:49:59 -04001203
Chris Mason771ed682008-11-06 22:02:51 -05001204static noinline void async_cow_free(struct btrfs_work *work)
1205{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001206 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001207
Nikolay Borisovb5326272019-03-12 17:20:25 +02001208 async_chunk = container_of(work, struct async_chunk, work);
1209 if (async_chunk->inode)
1210 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001211 if (async_chunk->blkcg_css)
1212 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001213 /*
1214 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001215 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001216 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001217 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001218 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001219}
1220
Chris Masonec39f762019-07-10 12:28:17 -07001221static int cow_file_range_async(struct inode *inode,
1222 struct writeback_control *wbc,
1223 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001224 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001225 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001226{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001227 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonec39f762019-07-10 12:28:17 -07001228 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001229 struct async_cow *ctx;
1230 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001231 unsigned long nr_pages;
1232 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001233 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1234 int i;
1235 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001236 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001237 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001238
Nikolay Borisov69684c52019-03-12 17:20:28 +02001239 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001240
1241 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1242 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1243 num_chunks = 1;
1244 should_compress = false;
1245 } else {
1246 should_compress = true;
1247 }
1248
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001249 nofs_flag = memalloc_nofs_save();
1250 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1251 memalloc_nofs_restore(nofs_flag);
1252
Nikolay Borisov97db1202019-03-12 17:20:24 +02001253 if (!ctx) {
1254 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1255 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1256 EXTENT_DO_ACCOUNTING;
1257 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1258 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1259 PAGE_SET_ERROR;
1260
Nikolay Borisov74e91942019-07-17 16:18:16 +03001261 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001262 clear_bits, page_ops);
1263 return -ENOMEM;
1264 }
1265
1266 async_chunk = ctx->chunks;
1267 atomic_set(&ctx->num_chunks, num_chunks);
1268
1269 for (i = 0; i < num_chunks; i++) {
1270 if (should_compress)
1271 cur_end = min(end, start + SZ_512K - 1);
1272 else
1273 cur_end = end;
1274
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001275 /*
1276 * igrab is called higher up in the call chain, take only the
1277 * lightweight reference for the callback lifetime
1278 */
1279 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001280 async_chunk[i].pending = &ctx->num_chunks;
1281 async_chunk[i].inode = inode;
1282 async_chunk[i].start = start;
1283 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001284 async_chunk[i].write_flags = write_flags;
1285 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001286
Chris Mason1d53c9e2019-07-10 12:28:16 -07001287 /*
1288 * The locked_page comes all the way from writepage and its
1289 * the original page we were actually given. As we spread
1290 * this large delalloc region across multiple async_chunk
1291 * structs, only the first struct needs a pointer to locked_page
1292 *
1293 * This way we don't need racey decisions about who is supposed
1294 * to unlock it.
1295 */
1296 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001297 /*
1298 * Depending on the compressibility, the pages might or
1299 * might not go through async. We want all of them to
1300 * be accounted against wbc once. Let's do it here
1301 * before the paths diverge. wbc accounting is used
1302 * only for foreign writeback detection and doesn't
1303 * need full accuracy. Just account the whole thing
1304 * against the first page.
1305 */
1306 wbc_account_cgroup_owner(wbc, locked_page,
1307 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001308 async_chunk[i].locked_page = locked_page;
1309 locked_page = NULL;
1310 } else {
1311 async_chunk[i].locked_page = NULL;
1312 }
1313
Chris Masonec39f762019-07-10 12:28:17 -07001314 if (blkcg_css != blkcg_root_css) {
1315 css_get(blkcg_css);
1316 async_chunk[i].blkcg_css = blkcg_css;
1317 } else {
1318 async_chunk[i].blkcg_css = NULL;
1319 }
1320
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001321 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1322 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001323
Nikolay Borisov97db1202019-03-12 17:20:24 +02001324 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001325 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001326
Nikolay Borisov97db1202019-03-12 17:20:24 +02001327 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001328
Chris Mason771ed682008-11-06 22:02:51 -05001329 *nr_written += nr_pages;
1330 start = cur_end + 1;
1331 }
1332 *page_started = 1;
1333 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001334}
1335
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001336static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001337 u64 bytenr, u64 num_bytes)
1338{
1339 int ret;
1340 struct btrfs_ordered_sum *sums;
1341 LIST_HEAD(list);
1342
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001343 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001344 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001345 if (ret == 0 && list_empty(&list))
1346 return 0;
1347
1348 while (!list_empty(&list)) {
1349 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1350 list_del(&sums->list);
1351 kfree(sums);
1352 }
Liu Bo58113752018-01-31 17:09:13 -07001353 if (ret < 0)
1354 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001355 return 1;
1356}
1357
Chris Masond352ac62008-09-29 15:18:18 -04001358/*
1359 * when nowcow writeback call back. This checks for snapshots or COW copies
1360 * of the extents that exist in the file, and COWs the file as required.
1361 *
1362 * If no cow copies or snapshots exist, we write directly to the existing
1363 * blocks on disk
1364 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001365static noinline int run_delalloc_nocow(struct inode *inode,
1366 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001367 const u64 start, const u64 end,
1368 int *page_started, int force,
1369 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001370{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001371 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001372 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001373 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001374 u64 cow_start = (u64)-1;
1375 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001376 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001377 bool check_prev = true;
1378 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001379 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001380 bool nocow = false;
1381 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001382
1383 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001384 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001385 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001386 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001387 EXTENT_DO_ACCOUNTING |
1388 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001389 PAGE_CLEAR_DIRTY |
1390 PAGE_SET_WRITEBACK |
1391 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001392 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001393 }
Li Zefan82d59022011-04-20 10:33:24 +08001394
Yan Zheng80ff3852008-10-30 14:20:02 -04001395 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001396 struct btrfs_key found_key;
1397 struct btrfs_file_extent_item *fi;
1398 struct extent_buffer *leaf;
1399 u64 extent_end;
1400 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001401 u64 num_bytes = 0;
1402 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001403 u64 ram_bytes;
1404 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001405
1406 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001407
Liu Boe4c3b2d2017-01-30 12:25:28 -08001408 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001409 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001410 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001411 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001412
1413 /*
1414 * If there is no extent for our range when doing the initial
1415 * search, then go back to the previous slot as it will be the
1416 * one containing the search offset
1417 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001418 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1419 leaf = path->nodes[0];
1420 btrfs_item_key_to_cpu(leaf, &found_key,
1421 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001422 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001423 found_key.type == BTRFS_EXTENT_DATA_KEY)
1424 path->slots[0]--;
1425 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001426 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001427next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001428 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001429 leaf = path->nodes[0];
1430 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1431 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001432 if (ret < 0) {
1433 if (cow_start != (u64)-1)
1434 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001435 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001436 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001437 if (ret > 0)
1438 break;
1439 leaf = path->nodes[0];
1440 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001441
Yan Zheng80ff3852008-10-30 14:20:02 -04001442 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001443
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001444 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001445 if (found_key.objectid > ino)
1446 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001447 /*
1448 * Keep searching until we find an EXTENT_ITEM or there are no
1449 * more extents for this inode
1450 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001451 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1452 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1453 path->slots[0]++;
1454 goto next_slot;
1455 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001456
1457 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001458 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001459 found_key.offset > end)
1460 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001461
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001462 /*
1463 * If the found extent starts after requested offset, then
1464 * adjust extent_end to be right before this extent begins
1465 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001466 if (found_key.offset > cur_offset) {
1467 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001468 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001469 goto out_check;
1470 }
Chris Masonc31f8832008-01-08 15:46:31 -05001471
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001472 /*
1473 * Found extent which begins before our range and potentially
1474 * intersect it
1475 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001476 fi = btrfs_item_ptr(leaf, path->slots[0],
1477 struct btrfs_file_extent_item);
1478 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001479
Josef Bacikcc95bef2013-04-04 14:31:27 -04001480 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001481 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1482 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001483 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001484 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001485 extent_end = found_key.offset +
1486 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001487 disk_num_bytes =
1488 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001489 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001490 * If the extent we got ends before our current offset,
1491 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001492 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001493 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001494 path->slots[0]++;
1495 goto next_slot;
1496 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001497 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001498 if (disk_bytenr == 0)
1499 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001500 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001501 if (btrfs_file_extent_compression(leaf, fi) ||
1502 btrfs_file_extent_encryption(leaf, fi) ||
1503 btrfs_file_extent_other_encoding(leaf, fi))
1504 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001505 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001506 * If extent is created before the last volume's snapshot
1507 * this implies the extent is shared, hence we can't do
1508 * nocow. This is the same check as in
1509 * btrfs_cross_ref_exist but without calling
1510 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001511 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001512 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001513 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001514 btrfs_root_last_snapshot(&root->root_item))
1515 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001516 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1517 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001518 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001519 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001521 ret = btrfs_cross_ref_exist(root, ino,
1522 found_key.offset -
1523 extent_offset, disk_bytenr);
1524 if (ret) {
1525 /*
1526 * ret could be -EIO if the above fails to read
1527 * metadata.
1528 */
1529 if (ret < 0) {
1530 if (cow_start != (u64)-1)
1531 cur_offset = cow_start;
1532 goto error;
1533 }
1534
Nikolay Borisov3e024842019-08-21 10:42:03 +03001535 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001536 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001537 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001538 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001539 disk_bytenr += cur_offset - found_key.offset;
1540 num_bytes = min(end + 1, extent_end) - cur_offset;
1541 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001542 * If there are pending snapshots for this root, we
1543 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001544 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001545 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001546 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001547 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001548 * force cow if csum exists in the range.
1549 * this ensure that csum for a given extent are
1550 * either valid or do not exist.
1551 */
Liu Bo58113752018-01-31 17:09:13 -07001552 ret = csum_exist_in_range(fs_info, disk_bytenr,
1553 num_bytes);
1554 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001555 /*
1556 * ret could be -EIO if the above fails to read
1557 * metadata.
1558 */
1559 if (ret < 0) {
1560 if (cow_start != (u64)-1)
1561 cur_offset = cow_start;
1562 goto error;
1563 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001564 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001565 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001566 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001567 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001568 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001569 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001570 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001571 extent_end = found_key.offset + ram_bytes;
1572 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001573 /* Skip extents outside of our requested range */
1574 if (extent_end <= start) {
1575 path->slots[0]++;
1576 goto next_slot;
1577 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001578 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001579 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001580 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001581 }
1582out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001583 /*
1584 * If nocow is false then record the beginning of the range
1585 * that needs to be COWed
1586 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001587 if (!nocow) {
1588 if (cow_start == (u64)-1)
1589 cow_start = cur_offset;
1590 cur_offset = extent_end;
1591 if (cur_offset > end)
1592 break;
1593 path->slots[0]++;
1594 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001595 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001596
David Sterbab3b4aa72011-04-21 01:20:15 +02001597 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001598
1599 /*
1600 * COW range from cow_start to found_key.offset - 1. As the key
1601 * will contain the beginning of the first extent that can be
1602 * NOCOW, following one which needs to be COW'ed
1603 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001604 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001605 ret = cow_file_range(inode, locked_page,
1606 cow_start, found_key.offset - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001607 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001608 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001609 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001610 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001611 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001612 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001613 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001614 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001615 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001616
Yan Zhengd899e052008-10-30 14:25:28 -04001617 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001618 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001619 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001620
1621 em = create_io_em(inode, cur_offset, num_bytes,
1622 orig_start,
1623 disk_bytenr, /* block_start */
1624 num_bytes, /* block_len */
1625 disk_num_bytes, /* orig_block_len */
1626 ram_bytes, BTRFS_COMPRESS_NONE,
1627 BTRFS_ORDERED_PREALLOC);
1628 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001629 if (nocow)
1630 btrfs_dec_nocow_writers(fs_info,
1631 disk_bytenr);
1632 ret = PTR_ERR(em);
1633 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001634 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001635 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001636 ret = btrfs_add_ordered_extent(inode, cur_offset,
1637 disk_bytenr, num_bytes,
1638 num_bytes,
1639 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001640 if (ret) {
1641 btrfs_drop_extent_cache(BTRFS_I(inode),
1642 cur_offset,
1643 cur_offset + num_bytes - 1,
1644 0);
1645 goto error;
1646 }
Yan Zhengd899e052008-10-30 14:25:28 -04001647 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001648 ret = btrfs_add_ordered_extent(inode, cur_offset,
1649 disk_bytenr, num_bytes,
1650 num_bytes,
1651 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001652 if (ret)
1653 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001654 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001655
Filipe Mananaf78c4362016-05-09 13:15:41 +01001656 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001657 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001658 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001659
Yan, Zhengefa56462010-05-16 10:49:59 -04001660 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001661 BTRFS_DATA_RELOC_TREE_OBJECTID)
1662 /*
1663 * Error handled later, as we must prevent
1664 * extent_clear_unlock_delalloc() in error handler
1665 * from freeing metadata of created ordered extent.
1666 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001667 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1668 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001669
Josef Bacikc2790a22013-07-29 11:20:47 -04001670 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001671 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001672 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001673 EXTENT_DELALLOC |
1674 EXTENT_CLEAR_DATA_RESV,
1675 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1676
Yan Zheng80ff3852008-10-30 14:20:02 -04001677 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001678
1679 /*
1680 * btrfs_reloc_clone_csums() error, now we're OK to call error
1681 * handler, as metadata for created ordered extent will only
1682 * be freed by btrfs_finish_ordered_io().
1683 */
1684 if (ret)
1685 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001686 if (cur_offset > end)
1687 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001688 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001689 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001690
Robbie Ko506481b2018-10-30 18:04:04 +08001691 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001692 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001693
Yan Zheng80ff3852008-10-30 14:20:02 -04001694 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001695 cur_offset = end;
Nikolay Borisov74e91942019-07-17 16:18:16 +03001696 ret = cow_file_range(inode, locked_page, cow_start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001697 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001698 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001699 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001700 }
1701
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001702error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001703 if (nocow)
1704 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1705
Josef Bacik17ca04a2012-05-31 15:58:55 -04001706 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001707 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001708 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001709 EXTENT_DELALLOC | EXTENT_DEFRAG |
1710 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1711 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001712 PAGE_SET_WRITEBACK |
1713 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001714 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001715 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001716}
1717
Wang Shilong47059d92014-07-03 18:22:07 +08001718static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1719{
1720
1721 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1722 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1723 return 0;
1724
1725 /*
1726 * @defrag_bytes is a hint value, no spinlock held here,
1727 * if is not zero, it means the file is defragging.
1728 * Force cow if given extent needs to be defragged.
1729 */
1730 if (BTRFS_I(inode)->defrag_bytes &&
1731 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1732 EXTENT_DEFRAG, 0, NULL))
1733 return 1;
1734
1735 return 0;
1736}
1737
Chris Masond352ac62008-09-29 15:18:18 -04001738/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001739 * Function to process delayed allocation (create CoW) for ranges which are
1740 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001741 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001742int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001743 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1744 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001745{
Chris Masonbe20aa92007-12-17 20:14:01 -05001746 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001747 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001748
Wang Shilong47059d92014-07-03 18:22:07 +08001749 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001750 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001751 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001752 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001753 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001754 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001755 } else if (!inode_can_compress(inode) ||
1756 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001757 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001758 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001759 } else {
1760 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1761 &BTRFS_I(inode)->runtime_flags);
Chris Masonec39f762019-07-10 12:28:17 -07001762 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001763 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001764 }
Qu Wenruo524272602017-03-08 10:25:52 +08001765 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001766 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1767 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001768 return ret;
1769}
1770
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001771void btrfs_split_delalloc_extent(struct inode *inode,
1772 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001773{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001774 u64 size;
1775
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001776 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001777 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001778 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001779
Josef Bacikdcab6a32015-02-11 15:08:59 -05001780 size = orig->end - orig->start + 1;
1781 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001782 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001783 u64 new_size;
1784
1785 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001786 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001787 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001788 */
1789 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001790 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001791 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001792 num_extents += count_max_extents(new_size);
1793 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001794 return;
1795 }
1796
Josef Bacik9e0baf62011-07-15 15:16:44 +00001797 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001798 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001799 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001800}
1801
1802/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001803 * Handle merged delayed allocation extents so we can keep track of new extents
1804 * that are just merged onto old extents, such as when we are doing sequential
1805 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001806 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001807void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1808 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001809{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001810 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001811 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001812
Josef Bacik9ed74f22009-09-11 16:12:44 -04001813 /* not delalloc, ignore it */
1814 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001815 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001816
Josef Bacik8461a3d2015-03-13 15:12:08 -04001817 if (new->start > other->start)
1818 new_size = new->end - other->start + 1;
1819 else
1820 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001821
1822 /* we're not bigger than the max, unreserve the space and go */
1823 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1824 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001825 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001826 spin_unlock(&BTRFS_I(inode)->lock);
1827 return;
1828 }
1829
1830 /*
Josef Bacikba117212015-03-13 15:01:24 -04001831 * We have to add up either side to figure out how many extents were
1832 * accounted for before we merged into one big extent. If the number of
1833 * extents we accounted for is <= the amount we need for the new range
1834 * then we can return, otherwise drop. Think of it like this
1835 *
1836 * [ 4k][MAX_SIZE]
1837 *
1838 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1839 * need 2 outstanding extents, on one side we have 1 and the other side
1840 * we have 1 so they are == and we can return. But in this case
1841 *
1842 * [MAX_SIZE+4k][MAX_SIZE+4k]
1843 *
1844 * Each range on their own accounts for 2 extents, but merged together
1845 * they are only 3 extents worth of accounting, so we need to drop in
1846 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001847 */
Josef Bacikba117212015-03-13 15:01:24 -04001848 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001849 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001850 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001851 num_extents += count_max_extents(old_size);
1852 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001853 return;
1854
Josef Bacik9e0baf62011-07-15 15:16:44 +00001855 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001856 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001857 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001858}
1859
Miao Xieeb73c1b2013-05-15 07:48:22 +00001860static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1861 struct inode *inode)
1862{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001863 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1864
Miao Xieeb73c1b2013-05-15 07:48:22 +00001865 spin_lock(&root->delalloc_lock);
1866 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1867 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1868 &root->delalloc_inodes);
1869 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1870 &BTRFS_I(inode)->runtime_flags);
1871 root->nr_delalloc_inodes++;
1872 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001873 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001874 BUG_ON(!list_empty(&root->delalloc_root));
1875 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001876 &fs_info->delalloc_roots);
1877 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001878 }
1879 }
1880 spin_unlock(&root->delalloc_lock);
1881}
1882
Nikolay Borisov2b877332018-04-27 12:21:51 +03001883
1884void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1885 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001886{
David Sterba3ffbd682018-06-29 10:56:42 +02001887 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001888
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001889 if (!list_empty(&inode->delalloc_inodes)) {
1890 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001891 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001892 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001893 root->nr_delalloc_inodes--;
1894 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001895 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001896 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001897 BUG_ON(list_empty(&root->delalloc_root));
1898 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001899 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001900 }
1901 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001902}
1903
1904static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1905 struct btrfs_inode *inode)
1906{
1907 spin_lock(&root->delalloc_lock);
1908 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001909 spin_unlock(&root->delalloc_lock);
1910}
1911
Chris Masond352ac62008-09-29 15:18:18 -04001912/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001913 * Properly track delayed allocation bytes in the inode and to maintain the
1914 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001915 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001916void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1917 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001918{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001919 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1920
Wang Shilong47059d92014-07-03 18:22:07 +08001921 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1922 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001923 /*
1924 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001925 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001926 * bit, which is only set or cleared with irqs on
1927 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001928 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001929 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001930 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001931 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001932 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001933
Josef Bacik8b62f872017-10-19 14:15:55 -04001934 spin_lock(&BTRFS_I(inode)->lock);
1935 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1936 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001937
Josef Bacik6a3891c2015-03-16 17:38:52 -04001938 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001939 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001940 return;
1941
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001942 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1943 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001944 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001945 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001946 if (*bits & EXTENT_DEFRAG)
1947 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001948 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001949 &BTRFS_I(inode)->runtime_flags))
1950 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001951 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001952 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001953
1954 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1955 (*bits & EXTENT_DELALLOC_NEW)) {
1956 spin_lock(&BTRFS_I(inode)->lock);
1957 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1958 state->start;
1959 spin_unlock(&BTRFS_I(inode)->lock);
1960 }
Chris Mason291d6732008-01-29 15:55:23 -05001961}
1962
Chris Masond352ac62008-09-29 15:18:18 -04001963/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001964 * Once a range is no longer delalloc this function ensures that proper
1965 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001966 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001967void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1968 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001969{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001970 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1971 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001972 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001973 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001974
Filipe Manana4a4b9642017-07-27 19:52:55 +01001975 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1976 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001977 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001978 spin_unlock(&inode->lock);
1979 }
Wang Shilong47059d92014-07-03 18:22:07 +08001980
Chris Mason75eff682008-12-15 15:54:40 -05001981 /*
1982 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001983 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001984 * bit, which is only set or cleared with irqs on
1985 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001986 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001987 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001988 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001989
Josef Bacik8b62f872017-10-19 14:15:55 -04001990 spin_lock(&inode->lock);
1991 btrfs_mod_outstanding_extents(inode, -num_extents);
1992 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001993
Josef Bacikb6d08f02013-09-27 14:57:43 -04001994 /*
1995 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001996 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001997 * error.
1998 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001999 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002000 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002001 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002002
Josef Bacik6a3891c2015-03-16 17:38:52 -04002003 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002004 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002005 return;
2006
Filipe Mananaa315e682017-03-06 23:04:20 +00002007 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2008 do_list && !(state->state & EXTENT_NORESERVE) &&
2009 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002010 btrfs_free_reserved_data_space_noquota(
2011 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08002012 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002013
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002014 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2015 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002016 spin_lock(&inode->lock);
2017 inode->delalloc_bytes -= len;
2018 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002019 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002020 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002021 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002022 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002023 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002024
2025 if ((state->state & EXTENT_DELALLOC_NEW) &&
2026 (*bits & EXTENT_DELALLOC_NEW)) {
2027 spin_lock(&inode->lock);
2028 ASSERT(inode->new_delalloc_bytes >= len);
2029 inode->new_delalloc_bytes -= len;
2030 spin_unlock(&inode->lock);
2031 }
Chris Mason291d6732008-01-29 15:55:23 -05002032}
2033
Chris Masond352ac62008-09-29 15:18:18 -04002034/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002035 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2036 * in a chunk's stripe. This function ensures that bios do not span a
2037 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002038 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002039 * @page - The page we are about to add to the bio
2040 * @size - size we want to add to the bio
2041 * @bio - bio we want to ensure is smaller than a stripe
2042 * @bio_flags - flags of the bio
2043 *
2044 * return 1 if page cannot be added to the bio
2045 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002046 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002047 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002048int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2049 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002050{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002051 struct inode *inode = page->mapping->host;
2052 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002053 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002054 u64 length = 0;
2055 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002056 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002057 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002058
Chris Mason771ed682008-11-06 22:02:51 -05002059 if (bio_flags & EXTENT_BIO_COMPRESSED)
2060 return 0;
2061
Kent Overstreet4f024f32013-10-11 15:44:27 -07002062 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002063 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002064 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2065 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002066 if (ret < 0)
2067 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002068
2069 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002070 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002071 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002072}
2073
Chris Masond352ac62008-09-29 15:18:18 -04002074/*
2075 * in order to insert checksums into the metadata in large chunks,
2076 * we wait until bio submission time. All the pages in the bio are
2077 * checksummed and sums are attached onto the ordered extent record.
2078 *
2079 * At IO completion time the cums attached on the ordered extent record
2080 * are inserted into the btree
2081 */
David Sterbad0ee3932018-03-08 14:35:48 +01002082static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002083 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002084{
Josef Bacikc6100a42017-05-05 11:57:13 -04002085 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002086 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002087
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002088 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002089 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002090 return 0;
2091}
Chris Masone0156402008-04-16 11:15:20 -04002092
Chris Mason4a69a412008-11-06 22:03:00 -05002093/*
Chris Masoncad321a2008-12-17 14:51:42 -05002094 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002095 * on write, or reading the csums from the tree before a read.
2096 *
2097 * Rules about async/sync submit,
2098 * a) read: sync submit
2099 *
2100 * b) write without checksum: sync submit
2101 *
2102 * c) write with checksum:
2103 * c-1) if bio is issued by fsync: sync submit
2104 * (sync_writers != 0)
2105 *
2106 * c-2) if root is reloc root: sync submit
2107 * (only in case of buffered IO)
2108 *
2109 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002110 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002111static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002112 int mirror_num,
2113 unsigned long bio_flags)
2114
Chris Mason44b8bd72008-04-16 11:14:51 -04002115{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002116 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002117 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302118 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002119 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002120 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002121 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002122
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002123 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002124
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002125 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302126 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002127
Mike Christie37226b22016-06-05 14:31:52 -05002128 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002129 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002130 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002131 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002132
Chris Masond20f7042008-12-08 16:58:54 -05002133 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002134 ret = btrfs_submit_compressed_read(inode, bio,
2135 mirror_num,
2136 bio_flags);
2137 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002138 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002139 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002140 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002141 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002142 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002143 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002144 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002145 /* csum items have already been cloned */
2146 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2147 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002148 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002149 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002150 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002151 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002152 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002153 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002154 if (ret)
2155 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002156 }
2157
Chris Mason0b86a832008-03-24 15:01:56 -04002158mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002159 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002160
2161out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002162 if (ret) {
2163 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002164 bio_endio(bio);
2165 }
Stefan Behrens61891922012-11-05 18:51:52 +01002166 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002167}
Chris Mason6885f302008-02-20 16:11:05 -05002168
Chris Masond352ac62008-09-29 15:18:18 -04002169/*
2170 * given a list of ordered sums record them in the inode. This happens
2171 * at IO completion time based on sums calculated at bio submission time.
2172 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002173static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002174 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002175{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002176 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002177 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002178
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002179 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002180 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002181 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002182 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002183 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002184 if (ret)
2185 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002186 }
2187 return 0;
2188}
2189
Josef Bacik2ac55d42010-02-03 19:33:23 +00002190int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002191 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002192 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002193{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002194 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002195 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002196 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002197}
2198
Chris Masond352ac62008-09-29 15:18:18 -04002199/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002200struct btrfs_writepage_fixup {
2201 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002202 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002203 struct btrfs_work work;
2204};
2205
Christoph Hellwigb2950862008-12-02 09:54:17 -05002206static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002207{
2208 struct btrfs_writepage_fixup *fixup;
2209 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002210 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002211 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002212 struct page *page;
2213 struct inode *inode;
2214 u64 page_start;
2215 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002216 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002217 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002218
2219 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2220 page = fixup->page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002221 inode = fixup->inode;
2222 page_start = page_offset(page);
2223 page_end = page_offset(page) + PAGE_SIZE - 1;
2224
2225 /*
2226 * This is similar to page_mkwrite, we need to reserve the space before
2227 * we take the page lock.
2228 */
2229 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2230 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002231again:
Chris Mason247e7432008-07-17 12:53:51 -04002232 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002233
Chris Mason25f3c502020-01-21 11:51:42 -05002234 /*
2235 * Before we queued this fixup, we took a reference on the page.
2236 * page->mapping may go NULL, but it shouldn't be moved to a different
2237 * address space.
2238 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002239 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2240 /*
2241 * Unfortunately this is a little tricky, either
2242 *
2243 * 1) We got here and our page had already been dealt with and
2244 * we reserved our space, thus ret == 0, so we need to just
2245 * drop our space reservation and bail. This can happen the
2246 * first time we come into the fixup worker, or could happen
2247 * while waiting for the ordered extent.
2248 * 2) Our page was already dealt with, but we happened to get an
2249 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2250 * this case we obviously don't have anything to release, but
2251 * because the page was already dealt with we don't want to
2252 * mark the page with an error, so make sure we're resetting
2253 * ret to 0. This is why we have this check _before_ the ret
2254 * check, because we do not want to have a surprise ENOSPC
2255 * when the page was already properly dealt with.
2256 */
2257 if (!ret) {
2258 btrfs_delalloc_release_extents(BTRFS_I(inode),
2259 PAGE_SIZE);
2260 btrfs_delalloc_release_space(inode, data_reserved,
2261 page_start, PAGE_SIZE,
2262 true);
2263 }
2264 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002265 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002266 }
Chris Mason25f3c502020-01-21 11:51:42 -05002267
2268 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002269 * We can't mess with the page state unless it is locked, so now that
2270 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002271 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002272 if (ret)
2273 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002274
David Sterbaff13db42015-12-03 14:30:40 +01002275 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002276 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002277
2278 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002279 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002280 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002281
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002282 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002283 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002284 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002285 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002286 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002287 unlock_page(page);
2288 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002289 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002290 goto again;
2291 }
Chris Mason247e7432008-07-17 12:53:51 -04002292
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002293 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002294 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002295 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002296 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002297
Chris Mason25f3c502020-01-21 11:51:42 -05002298 /*
2299 * Everything went as planned, we're now the owner of a dirty page with
2300 * delayed allocation bits set and space reserved for our COW
2301 * destination.
2302 *
2303 * The page was dirty when we started, nothing should have cleaned it.
2304 */
2305 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002306 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002307out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002308 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002309 if (free_delalloc_space)
Filipe Manana53687002019-10-09 17:43:59 +01002310 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2311 PAGE_SIZE, true);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002312 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002313 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002314out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002315 if (ret) {
2316 /*
2317 * We hit ENOSPC or other errors. Update the mapping and page
2318 * to reflect the errors and clean the page.
2319 */
2320 mapping_set_error(page->mapping, ret);
2321 end_extent_writepage(page, ret, page_start, page_end);
2322 clear_page_dirty_for_io(page);
2323 SetPageError(page);
2324 }
2325 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002326 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002327 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002328 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002329 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002330 /*
2331 * As a precaution, do a delayed iput in case it would be the last iput
2332 * that could need flushing space. Recursing back to fixup worker would
2333 * deadlock.
2334 */
2335 btrfs_add_delayed_iput(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002336}
2337
2338/*
2339 * There are a few paths in the higher layers of the kernel that directly
2340 * set the page dirty bit without asking the filesystem if it is a
2341 * good idea. This causes problems because we want to make sure COW
2342 * properly happens and the data=ordered rules are followed.
2343 *
Chris Masonc8b97812008-10-29 14:49:59 -04002344 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002345 * hasn't been properly setup for IO. We kick off an async process
2346 * to fix it up. The async helper will wait for ordered extents, set
2347 * the delalloc bit and make it safe to write the page.
2348 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002349int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002350{
2351 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002352 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002353 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002354
Chris Mason8b62b722009-09-02 16:53:46 -04002355 /* this page is properly in the ordered list */
2356 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002357 return 0;
2358
Chris Mason25f3c502020-01-21 11:51:42 -05002359 /*
2360 * PageChecked is set below when we create a fixup worker for this page,
2361 * don't try to create another one if we're already PageChecked()
2362 *
2363 * The extent_io writepage code will redirty the page if we send back
2364 * EAGAIN.
2365 */
Chris Mason247e7432008-07-17 12:53:51 -04002366 if (PageChecked(page))
2367 return -EAGAIN;
2368
2369 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2370 if (!fixup)
2371 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002372
Josef Bacikf4b13632020-01-21 14:34:52 -05002373 /*
2374 * We are already holding a reference to this inode from
2375 * write_cache_pages. We need to hold it because the space reservation
2376 * takes place outside of the page lock, and we can't trust
2377 * page->mapping outside of the page lock.
2378 */
2379 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002380 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002381 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002382 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002383 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002384 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002385 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002386
2387 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002388}
2389
Yan Zhengd899e052008-10-30 14:25:28 -04002390static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2391 struct inode *inode, u64 file_pos,
2392 u64 disk_bytenr, u64 disk_num_bytes,
2393 u64 num_bytes, u64 ram_bytes,
2394 u8 compression, u8 encryption,
2395 u16 other_encoding, int extent_type)
2396{
2397 struct btrfs_root *root = BTRFS_I(inode)->root;
2398 struct btrfs_file_extent_item *fi;
2399 struct btrfs_path *path;
2400 struct extent_buffer *leaf;
2401 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002402 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002403 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002404 int ret;
2405
2406 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002407 if (!path)
2408 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002409
Chris Masona1ed8352009-09-11 12:27:37 -04002410 /*
2411 * we may be replacing one extent in the tree with another.
2412 * The new extent is pinned in the extent map, and we don't want
2413 * to drop it from the cache until it is completely in the btree.
2414 *
2415 * So, tell btrfs_drop_extents to leave this extent in the cache.
2416 * the caller is expected to unpin it and allow it to be merged
2417 * with the others.
2418 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002419 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2420 file_pos + num_bytes, NULL, 0,
2421 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002422 if (ret)
2423 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002424
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002425 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002426 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002427 ins.offset = file_pos;
2428 ins.type = BTRFS_EXTENT_DATA_KEY;
2429
2430 path->leave_spinning = 1;
2431 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2432 sizeof(*fi));
2433 if (ret)
2434 goto out;
2435 }
Yan Zhengd899e052008-10-30 14:25:28 -04002436 leaf = path->nodes[0];
2437 fi = btrfs_item_ptr(leaf, path->slots[0],
2438 struct btrfs_file_extent_item);
2439 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2440 btrfs_set_file_extent_type(leaf, fi, extent_type);
2441 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2442 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2443 btrfs_set_file_extent_offset(leaf, fi, 0);
2444 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2445 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2446 btrfs_set_file_extent_compression(leaf, fi, compression);
2447 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2448 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002449
Yan Zhengd899e052008-10-30 14:25:28 -04002450 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002451 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002452
2453 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002454
2455 ins.objectid = disk_bytenr;
2456 ins.offset = disk_num_bytes;
2457 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002458
Josef Bacik9ddc9592020-01-17 09:02:22 -05002459 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), file_pos,
2460 ram_bytes);
2461 if (ret)
2462 goto out;
2463
Qu Wenruo297d7502015-09-08 17:08:37 +08002464 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002465 * Release the reserved range from inode dirty range map, as it is
2466 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002467 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002468 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2469 if (ret < 0)
2470 goto out;
2471 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002472 ret = btrfs_alloc_reserved_file_extent(trans, root,
2473 btrfs_ino(BTRFS_I(inode)),
2474 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002475out:
Yan Zhengd899e052008-10-30 14:25:28 -04002476 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002477
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002478 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002479}
2480
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002481static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002482 u64 start, u64 len)
2483{
David Sterba32da53862019-10-29 19:20:18 +01002484 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002485
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002486 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002487 ASSERT(cache);
2488
2489 spin_lock(&cache->lock);
2490 cache->delalloc_bytes -= len;
2491 spin_unlock(&cache->lock);
2492
2493 btrfs_put_block_group(cache);
2494}
2495
Chris Masond352ac62008-09-29 15:18:18 -04002496/* as ordered data IO finishes, this gets called so we can finish
2497 * an ordered extent if the range of bytes in the file it covers are
2498 * fully written.
2499 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002500static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002501{
Josef Bacik5fd02042012-05-02 14:00:54 -04002502 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002503 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002504 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002505 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002506 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002507 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002508 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002509 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002510 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002511 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002512 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002513 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002514 bool range_locked = false;
2515 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002516 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002517 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002518
Omar Sandovalbffe6332019-12-02 17:34:19 -08002519 start = ordered_extent->file_offset;
2520 end = start + ordered_extent->num_bytes - 1;
2521
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002522 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2523 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2524 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2525 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002526
Nikolay Borisov8d510122019-10-08 20:43:06 +03002527 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002528
Josef Bacik5fd02042012-05-02 14:00:54 -04002529 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2530 ret = -EIO;
2531 goto out;
2532 }
2533
Omar Sandovalbffe6332019-12-02 17:34:19 -08002534 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002535
Josef Bacik77cef2e2013-08-29 13:57:21 -04002536 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2537 truncated = true;
2538 logical_len = ordered_extent->truncated_len;
2539 /* Truncated the entire extent, don't bother adding */
2540 if (!logical_len)
2541 goto out;
2542 }
2543
Yan, Zhengc2167752009-11-12 09:34:21 +00002544 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002545 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002546
2547 /*
2548 * For mwrite(mmap + memset to write) case, we still reserve
2549 * space for NOCOW range.
2550 * As NOCOW won't cause a new delayed ref, just free the space
2551 */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002552 btrfs_qgroup_free_data(inode, NULL, start,
2553 ordered_extent->num_bytes);
Josef Bacikd923afe2020-01-17 09:02:23 -05002554 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002555 if (freespace_inode)
2556 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002557 else
2558 trans = btrfs_join_transaction(root);
2559 if (IS_ERR(trans)) {
2560 ret = PTR_ERR(trans);
2561 trans = NULL;
2562 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002563 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002564 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002565 ret = btrfs_update_inode_fallback(trans, root, inode);
2566 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002567 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002568 goto out;
2569 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002570
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002571 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002572 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002573
Nikolay Borisov8d510122019-10-08 20:43:06 +03002574 if (freespace_inode)
2575 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002576 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002577 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002578 if (IS_ERR(trans)) {
2579 ret = PTR_ERR(trans);
2580 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002581 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002582 }
Chris Masona79b7d42014-05-22 16:18:52 -07002583
Josef Bacik69fe2d72017-10-19 14:15:57 -04002584 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002585
Chris Masonc8b97812008-10-29 14:49:59 -04002586 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002587 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002588 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002589 BUG_ON(compress_type);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002590 btrfs_qgroup_free_data(inode, NULL, start,
2591 ordered_extent->num_bytes);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002592 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002593 ordered_extent->file_offset,
2594 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002595 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002596 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002597 BUG_ON(root == fs_info->tree_root);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002598 ret = insert_reserved_file_extent(trans, inode, start,
2599 ordered_extent->disk_bytenr,
2600 ordered_extent->disk_num_bytes,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002601 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002602 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002603 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04002604 if (!ret) {
2605 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002606 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002607 ordered_extent->disk_bytenr,
2608 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002609 }
Yan Zhengd899e052008-10-30 14:25:28 -04002610 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002611 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002612 ordered_extent->file_offset,
2613 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002614 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002615 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002616 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002617 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002618
Nikolay Borisovac01f262018-01-08 10:59:43 +02002619 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2620 if (ret) {
2621 btrfs_abort_transaction(trans, ret);
2622 goto out;
2623 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002624
Josef Bacikd923afe2020-01-17 09:02:23 -05002625 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002626 ret = btrfs_update_inode_fallback(trans, root, inode);
2627 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002628 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002629 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002630 }
2631 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002632out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002633 clear_bits = EXTENT_DEFRAG;
2634 if (range_locked)
2635 clear_bits |= EXTENT_LOCKED;
2636 if (clear_new_delalloc_bytes)
2637 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002638 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2639 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002640 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002641
Miao Xiea698d0752012-09-20 01:51:59 -06002642 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002643 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002644
Josef Bacik77cef2e2013-08-29 13:57:21 -04002645 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002646 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002647
2648 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002649 unwritten_start += logical_len;
2650 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002651
2652 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002653 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002654
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002655 /*
2656 * If the ordered extent had an IOERR or something else went
2657 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002658 * back to the allocator. We only free the extent in the
2659 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002660 *
2661 * If we made it past insert_reserved_file_extent before we
2662 * errored out then we don't need to do this as the accounting
2663 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002664 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002665 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002666 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002667 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002668 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2669 /*
2670 * Discard the range before returning it back to the
2671 * free space pool
2672 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002673 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002674 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002675 ordered_extent->disk_bytenr,
2676 ordered_extent->disk_num_bytes,
2677 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002678 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002679 ordered_extent->disk_bytenr,
2680 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002681 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002682 }
2683
Josef Bacik5fd02042012-05-02 14:00:54 -04002684 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002685 * This needs to be done to make sure anybody waiting knows we are done
2686 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002687 */
2688 btrfs_remove_ordered_extent(inode, ordered_extent);
2689
Chris Masone6dcd2d2008-07-17 12:53:50 -04002690 /* once for us */
2691 btrfs_put_ordered_extent(ordered_extent);
2692 /* once for the tree */
2693 btrfs_put_ordered_extent(ordered_extent);
2694
Josef Bacik5fd02042012-05-02 14:00:54 -04002695 return ret;
2696}
2697
2698static void finish_ordered_fn(struct btrfs_work *work)
2699{
2700 struct btrfs_ordered_extent *ordered_extent;
2701 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2702 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002703}
2704
Nikolay Borisovc6297322018-11-08 10:18:08 +02002705void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2706 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002707{
Josef Bacik5fd02042012-05-02 14:00:54 -04002708 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002709 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002710 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002711 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002712
liubo1abe9b82011-03-24 11:18:59 +00002713 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2714
Chris Mason8b62b722009-09-02 16:53:46 -04002715 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002716 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2717 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002718 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002719
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002720 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002721 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002722 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002723 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002724
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002725 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002726 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002727}
2728
Omar Sandoval47df7762020-04-16 14:46:17 -07002729static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2730 int icsum, struct page *page, int pgoff, u64 start,
2731 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002732{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2734 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002735 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002736 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2737 u8 *csum_expected;
2738 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002739
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002740 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002741
2742 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002743 shash->tfm = fs_info->csum_shash;
2744
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 Masonb888db22007-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 /*
4103 * for non-free space inodes and ref cows, we want to back off from
4104 * time to time
4105 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004106 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004107 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004108 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004109
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004110 path = btrfs_alloc_path();
4111 if (!path)
4112 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004113 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004114
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004115 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4116 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4117 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004118
Josef Bacik5dc562c2012-08-17 13:14:17 -04004119 /*
4120 * We want to drop from the next block forward in case this new size is
4121 * not block aligned since we will be keeping the last block of the
4122 * extent just the way it is.
4123 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004124 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004125 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004126 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004127 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004128 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004129
Miao Xie16cdcec2011-04-22 18:12:22 +08004130 /*
4131 * This function is also used to drop the items in the log tree before
4132 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004133 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004134 * items.
4135 */
4136 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004137 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004138
Li Zefan33345d012011-04-20 10:31:50 +08004139 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004140 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004141 key.type = (u8)-1;
4142
Chris Mason85e21ba2008-01-29 15:11:36 -05004143search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004144 /*
4145 * with a 16K leaf size and 128MB extents, you can actually queue
4146 * up a huge file in a single leaf. Most of the time that
4147 * bytes_deleted is > 0, it will be huge by the time we get here
4148 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004149 if (be_nice && bytes_deleted > SZ_32M &&
4150 btrfs_should_end_transaction(trans)) {
4151 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004152 goto out;
4153 }
Chris Masond3977122009-01-05 21:25:51 -05004154
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004155 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4156 if (ret < 0)
4157 goto out;
4158
Chris Mason85e21ba2008-01-29 15:11:36 -05004159 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004160 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004161 /* there are no items in the tree for us to truncate, we're
4162 * done
4163 */
Yan, Zheng80825102009-11-12 09:35:36 +00004164 if (path->slots[0] == 0)
4165 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004166 path->slots[0]--;
4167 }
4168
Chris Masond3977122009-01-05 21:25:51 -05004169 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004170 u64 clear_start = 0, clear_len = 0;
4171
Chris Mason39279cc2007-06-12 06:35:45 -04004172 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004173 leaf = path->nodes[0];
4174 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004175 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004176
Li Zefan33345d012011-04-20 10:31:50 +08004177 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004178 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004179
Chris Mason85e21ba2008-01-29 15:11:36 -05004180 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004181 break;
4182
Chris Mason5f39d392007-10-15 16:14:19 -04004183 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004184 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004185 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004186 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004187 extent_type = btrfs_file_extent_type(leaf, fi);
4188 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004189 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004190 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004191
4192 trace_btrfs_truncate_show_fi_regular(
4193 BTRFS_I(inode), leaf, fi,
4194 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004195 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004196 item_end += btrfs_file_extent_ram_bytes(leaf,
4197 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004198
4199 trace_btrfs_truncate_show_fi_inline(
4200 BTRFS_I(inode), leaf, fi, path->slots[0],
4201 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004202 }
Yan008630c2007-11-07 13:31:09 -05004203 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004204 }
Yan, Zheng80825102009-11-12 09:35:36 +00004205 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004206 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004207 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004208 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004209 break;
4210 if (found_key.offset >= new_size)
4211 del_item = 1;
4212 else
4213 del_item = 0;
4214 }
Chris Mason39279cc2007-06-12 06:35:45 -04004215 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004216 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004217 if (found_type != BTRFS_EXTENT_DATA_KEY)
4218 goto delete;
4219
4220 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004221 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004222
4223 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004224 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004225 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004226 u64 orig_num_bytes =
4227 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004228 extent_num_bytes = ALIGN(new_size -
4229 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004230 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004231 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004232 btrfs_set_file_extent_num_bytes(leaf, fi,
4233 extent_num_bytes);
4234 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004235 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004236 if (test_bit(BTRFS_ROOT_REF_COWS,
4237 &root->state) &&
4238 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004239 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004240 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004241 } else {
Chris Masondb945352007-10-15 16:15:53 -04004242 extent_num_bytes =
4243 btrfs_file_extent_disk_num_bytes(leaf,
4244 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004245 extent_offset = found_key.offset -
4246 btrfs_file_extent_offset(leaf, fi);
4247
Chris Mason39279cc2007-06-12 06:35:45 -04004248 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004249 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004250 if (extent_start != 0) {
4251 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004252 if (test_bit(BTRFS_ROOT_REF_COWS,
4253 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004254 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004255 }
4256 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004257 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004258 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004259 /*
4260 * we can't truncate inline items that have had
4261 * special encodings
4262 */
4263 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004264 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004265 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4266 btrfs_file_extent_compression(leaf, fi) == 0) {
4267 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004268
Josef Bacikddfae632017-10-19 14:16:02 -04004269 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4270 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004271 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004272 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004273 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004274 * We have to bail so the last_size is set to
4275 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004276 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004277 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004278 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004279 } else {
4280 /*
4281 * Inline extents are special, we just treat
4282 * them as a full sector worth in the file
4283 * extent tree just for simplicity sake.
4284 */
4285 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004286 }
Josef Bacikddfae632017-10-19 14:16:02 -04004287
4288 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4289 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004290 }
Chris Mason179e29e2007-11-01 11:28:41 -04004291delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004292 /*
4293 * We use btrfs_truncate_inode_items() to clean up log trees for
4294 * multiple fsyncs, and in this case we don't want to clear the
4295 * file extent range because it's just the log.
4296 */
4297 if (root == BTRFS_I(inode)->root) {
4298 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4299 clear_start, clear_len);
4300 if (ret) {
4301 btrfs_abort_transaction(trans, ret);
4302 break;
4303 }
4304 }
4305
Josef Bacikddfae632017-10-19 14:16:02 -04004306 if (del_item)
4307 last_size = found_key.offset;
4308 else
4309 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004310 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004311 if (!pending_del_nr) {
4312 /* no pending yet, add ourselves */
4313 pending_del_slot = path->slots[0];
4314 pending_del_nr = 1;
4315 } else if (pending_del_nr &&
4316 path->slots[0] + 1 == pending_del_slot) {
4317 /* hop on the pending chunk */
4318 pending_del_nr++;
4319 pending_del_slot = path->slots[0];
4320 } else {
Chris Masond3977122009-01-05 21:25:51 -05004321 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004322 }
Chris Mason39279cc2007-06-12 06:35:45 -04004323 } else {
4324 break;
4325 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004326 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004327
Miao Xie27cdeb72014-04-02 19:51:05 +08004328 if (found_extent &&
4329 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004330 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004331 struct btrfs_ref ref = { 0 };
4332
Chris Mason28ed1342014-12-17 09:41:04 -08004333 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004334
4335 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4336 extent_start, extent_num_bytes, 0);
4337 ref.real_root = root->root_key.objectid;
4338 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4339 ino, extent_offset);
4340 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004341 if (ret) {
4342 btrfs_abort_transaction(trans, ret);
4343 break;
4344 }
Chris Mason28f75a02015-02-04 06:59:29 -08004345 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004346 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004347 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004348 }
Chris Mason39279cc2007-06-12 06:35:45 -04004349 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004350
Yan, Zheng80825102009-11-12 09:35:36 +00004351 if (found_type == BTRFS_INODE_ITEM_KEY)
4352 break;
4353
4354 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004355 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004356 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004357 if (pending_del_nr) {
4358 ret = btrfs_del_items(trans, root, path,
4359 pending_del_slot,
4360 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004361 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004362 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004363 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004364 }
Yan, Zheng80825102009-11-12 09:35:36 +00004365 pending_del_nr = 0;
4366 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004367 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004368
Chris Mason28f75a02015-02-04 06:59:29 -08004369 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004370 * We can generate a lot of delayed refs, so we need to
4371 * throttle every once and a while and make sure we're
4372 * adding enough space to keep up with the work we are
4373 * generating. Since we hold a transaction here we
4374 * can't flush, and we don't want to FLUSH_LIMIT because
4375 * we could have generated too many delayed refs to
4376 * actually allocate, so just bail if we're short and
4377 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004378 */
Josef Bacik28bad212018-12-03 10:20:38 -05004379 if (should_throttle) {
4380 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4381 BTRFS_RESERVE_NO_FLUSH);
4382 if (ret) {
4383 ret = -EAGAIN;
4384 break;
4385 }
Chris Mason28f75a02015-02-04 06:59:29 -08004386 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004387 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004388 } else {
4389 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004390 }
Chris Mason39279cc2007-06-12 06:35:45 -04004391 }
Yan, Zheng80825102009-11-12 09:35:36 +00004392out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004393 if (ret >= 0 && pending_del_nr) {
4394 int err;
4395
4396 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004397 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004398 if (err) {
4399 btrfs_abort_transaction(trans, err);
4400 ret = err;
4401 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004402 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004403 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4404 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004405 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004406 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004407 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004408 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4409 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004410 }
Chris Mason28ed1342014-12-17 09:41:04 -08004411
Chris Mason39279cc2007-06-12 06:35:45 -04004412 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004413 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004414}
4415
Chris Masona52d9a82007-08-27 16:49:44 -04004416/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304417 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004418 * @inode - inode that we're zeroing
4419 * @from - the offset to start zeroing
4420 * @len - the length to zero, 0 to zero the entire range respective to the
4421 * offset
4422 * @front - zero up to the offset instead of from the offset on
4423 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304424 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004425 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004426 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304427int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004428 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004429{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004430 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004431 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004432 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4433 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004434 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004435 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004436 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004437 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004438 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304439 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004440 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004441 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004442 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304443 u64 block_start;
4444 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004445
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004446 if (IS_ALIGNED(offset, blocksize) &&
4447 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004448 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304449
Josef Bacik8b62f872017-10-19 14:15:55 -04004450 block_start = round_down(from, blocksize);
4451 block_end = block_start + blocksize - 1;
4452
Qu Wenruo364ecf32017-02-27 15:10:38 +08004453 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004454 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004455 if (ret)
4456 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004457
Chris Mason211c17f2008-05-15 09:13:45 -04004458again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004459 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004460 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004461 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004462 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004463 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004464 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004465 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004466 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004467
Chris Masona52d9a82007-08-27 16:49:44 -04004468 if (!PageUptodate(page)) {
4469 ret = btrfs_readpage(NULL, page);
4470 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004471 if (page->mapping != mapping) {
4472 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004473 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004474 goto again;
4475 }
Chris Masona52d9a82007-08-27 16:49:44 -04004476 if (!PageUptodate(page)) {
4477 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004478 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004479 }
4480 }
Chris Mason211c17f2008-05-15 09:13:45 -04004481 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004482
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304483 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004484 set_page_extent_mapped(page);
4485
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304486 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004487 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304488 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004489 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004490 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004491 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004492 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004493 btrfs_put_ordered_extent(ordered);
4494 goto again;
4495 }
4496
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304497 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004498 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4499 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004500
Filipe Mananae3b8a482017-11-04 00:16:59 +00004501 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004502 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004503 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304504 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004505 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004506 goto out_unlock;
4507 }
4508
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304509 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004510 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304511 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004512 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004513 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304514 memset(kaddr + (block_start - page_offset(page)),
4515 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004516 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304517 memset(kaddr + (block_start - page_offset(page)) + offset,
4518 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004519 flush_dcache_page(page);
4520 kunmap(page);
4521 }
Chris Mason247e7432008-07-17 12:53:51 -04004522 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004523 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004524 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004525
Chris Mason89642222008-07-24 09:41:53 -04004526out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004527 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004528 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004529 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004530 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004531 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004532 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004533out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004534 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004535 return ret;
4536}
4537
Josef Bacik16e75492013-10-22 12:18:51 -04004538static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4539 u64 offset, u64 len)
4540{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004541 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004542 struct btrfs_trans_handle *trans;
4543 int ret;
4544
4545 /*
4546 * Still need to make sure the inode looks like it's been updated so
4547 * that any holes get logged if we fsync.
4548 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004549 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4550 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004551 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4552 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4553 return 0;
4554 }
4555
4556 /*
4557 * 1 - for the one we're dropping
4558 * 1 - for the one we're adding
4559 * 1 - for updating the inode.
4560 */
4561 trans = btrfs_start_transaction(root, 3);
4562 if (IS_ERR(trans))
4563 return PTR_ERR(trans);
4564
4565 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4566 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004567 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004568 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004569 return ret;
4570 }
4571
David Sterbaf85b7372017-01-20 14:54:07 +01004572 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4573 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004574 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004575 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004576 else
4577 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004578 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004579 return ret;
4580}
4581
Josef Bacik695a0d02011-03-04 15:46:53 -05004582/*
4583 * This function puts in dummy file extents for the area we're creating a hole
4584 * for. So if we are truncating this file to a larger size we need to insert
4585 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4586 * the range between oldsize and size
4587 */
Josef Bacika41ad392011-01-31 15:30:16 -05004588int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004589{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004590 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004591 struct btrfs_root *root = BTRFS_I(inode)->root;
4592 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004593 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004594 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004595 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004596 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4597 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004598 u64 last_byte;
4599 u64 cur_offset;
4600 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004601 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004602
Josef Bacika71754f2013-06-17 17:14:39 -04004603 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304604 * If our size started in the middle of a block we need to zero out the
4605 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004606 * expose stale data.
4607 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304608 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004609 if (err)
4610 return err;
4611
Yan Zheng9036c102008-10-30 14:19:41 -04004612 if (size <= hole_start)
4613 return 0;
4614
David Sterbab272ae22020-02-05 19:09:33 +01004615 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004616 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004617 cur_offset = hole_start;
4618 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004619 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004620 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004621 if (IS_ERR(em)) {
4622 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004623 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004624 break;
4625 }
Yan Zheng9036c102008-10-30 14:19:41 -04004626 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004627 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004628 hole_size = last_byte - cur_offset;
4629
Yan, Zheng80825102009-11-12 09:35:36 +00004630 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004631 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004632
Josef Bacik16e75492013-10-22 12:18:51 -04004633 err = maybe_insert_hole(root, inode, cur_offset,
4634 hole_size);
4635 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004636 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004637
4638 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4639 cur_offset, hole_size);
4640 if (err)
4641 break;
4642
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004643 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004644 cur_offset + hole_size - 1, 0);
4645 hole_em = alloc_extent_map();
4646 if (!hole_em) {
4647 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4648 &BTRFS_I(inode)->runtime_flags);
4649 goto next;
4650 }
4651 hole_em->start = cur_offset;
4652 hole_em->len = hole_size;
4653 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004654
Josef Bacik5dc562c2012-08-17 13:14:17 -04004655 hole_em->block_start = EXTENT_MAP_HOLE;
4656 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004657 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004658 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004659 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004660 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004661
4662 while (1) {
4663 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004664 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004665 write_unlock(&em_tree->lock);
4666 if (err != -EEXIST)
4667 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004668 btrfs_drop_extent_cache(BTRFS_I(inode),
4669 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004670 cur_offset +
4671 hole_size - 1, 0);
4672 }
4673 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004674 } else {
4675 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4676 cur_offset, hole_size);
4677 if (err)
4678 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004679 }
Josef Bacik16e75492013-10-22 12:18:51 -04004680next:
Yan Zheng9036c102008-10-30 14:19:41 -04004681 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004682 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004683 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004684 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004685 break;
4686 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004687 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004688 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004689 return err;
4690}
4691
Eric Sandeen3972f262013-01-12 02:57:22 +00004692static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004693{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004694 struct btrfs_root *root = BTRFS_I(inode)->root;
4695 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004696 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004697 loff_t newsize = attr->ia_size;
4698 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004699 int ret;
4700
Eric Sandeen3972f262013-01-12 02:57:22 +00004701 /*
4702 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4703 * special case where we need to update the times despite not having
4704 * these flags set. For all other operations the VFS set these flags
4705 * explicitly if it wants a timestamp update.
4706 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004707 if (newsize != oldsize) {
4708 inode_inc_iversion(inode);
4709 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4710 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004711 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004712 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004713
Josef Bacika41ad392011-01-31 15:30:16 -05004714 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004715 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004716 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004717 * This is to ensure the snapshot captures a fully consistent
4718 * state of this file - if the snapshot captures this expanding
4719 * truncation, it must capture all writes that happened before
4720 * this truncation.
4721 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004722 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004723 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004724 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004725 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004726 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004727 }
Yan, Zheng80825102009-11-12 09:35:36 +00004728
Miao Xief4a2f4c2011-12-14 20:12:01 -05004729 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004730 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004731 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004732 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004733 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004734
4735 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004736 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304737 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004738 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004739 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004740 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004741 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004742
Josef Bacika41ad392011-01-31 15:30:16 -05004743 /*
4744 * We're truncating a file that used to have good data down to
4745 * zero. Make sure it gets into the ordered flush list so that
4746 * any new writes get down to disk quickly.
4747 */
4748 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004749 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4750 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004751
Josef Bacika41ad392011-01-31 15:30:16 -05004752 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004753
Andrea Gelmini52042d82018-11-28 12:05:13 +01004754 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02004755 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004756 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02004757 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004758
Filipe Manana213e8c52018-02-06 20:40:31 +00004759 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004760 if (ret && inode->i_nlink) {
4761 int err;
4762
4763 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004764 * Truncate failed, so fix up the in-memory size. We
4765 * adjusted disk_i_size down as we removed extents, so
4766 * wait for disk_i_size to be stable and then update the
4767 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004768 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004769 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004770 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004771 return err;
4772 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004773 }
Yan, Zheng80825102009-11-12 09:35:36 +00004774 }
4775
Josef Bacika41ad392011-01-31 15:30:16 -05004776 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004777}
4778
Chris Mason39279cc2007-06-12 06:35:45 -04004779static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4780{
David Howells2b0143b2015-03-17 22:25:59 +00004781 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004782 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004783 int err;
4784
Li Zefanb83cc962010-12-20 16:04:08 +08004785 if (btrfs_root_readonly(root))
4786 return -EROFS;
4787
Jan Kara31051c82016-05-26 16:55:18 +02004788 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004789 if (err)
4790 return err;
4791
Chris Mason5a3f23d2009-03-31 13:27:11 -04004792 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004793 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004794 if (err)
4795 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004796 }
Yan Zheng9036c102008-10-30 14:19:41 -04004797
Christoph Hellwig10257742010-06-04 11:30:02 +02004798 if (attr->ia_valid) {
4799 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004800 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004801 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004802
Josef Bacik22c44fe2011-11-30 10:45:38 -05004803 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004804 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004805 }
4806
Chris Mason39279cc2007-06-12 06:35:45 -04004807 return err;
4808}
Chris Mason61295eb2008-01-14 16:24:38 -05004809
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004810/*
4811 * While truncating the inode pages during eviction, we get the VFS calling
4812 * btrfs_invalidatepage() against each page of the inode. This is slow because
4813 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4814 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4815 * extent_state structures over and over, wasting lots of time.
4816 *
4817 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4818 * those expensive operations on a per page basis and do only the ordered io
4819 * finishing, while we release here the extent_map and extent_state structures,
4820 * without the excessive merging and splitting.
4821 */
4822static void evict_inode_truncate_pages(struct inode *inode)
4823{
4824 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4825 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4826 struct rb_node *node;
4827
4828 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004829 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004830
4831 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004832 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004833 struct extent_map *em;
4834
Liu Bo07e1ce02018-08-23 03:51:52 +08004835 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004836 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004837 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4838 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004839 remove_extent_mapping(map_tree, em);
4840 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004841 if (need_resched()) {
4842 write_unlock(&map_tree->lock);
4843 cond_resched();
4844 write_lock(&map_tree->lock);
4845 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004846 }
4847 write_unlock(&map_tree->lock);
4848
Filipe Manana6ca07092015-05-26 00:55:42 +01004849 /*
4850 * Keep looping until we have no more ranges in the io tree.
4851 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004852 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
4853 * still in progress (unlocked the pages in the bio but did not yet
4854 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004855 * ranges can still be locked and eviction started because before
4856 * submitting those bios, which are executed by a separate task (work
4857 * queue kthread), inode references (inode->i_count) were not taken
4858 * (which would be dropped in the end io callback of each bio).
4859 * Therefore here we effectively end up waiting for those bios and
4860 * anyone else holding locked ranges without having bumped the inode's
4861 * reference count - if we don't do it, when they access the inode's
4862 * io_tree to unlock a range it may be too late, leading to an
4863 * use-after-free issue.
4864 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004865 spin_lock(&io_tree->lock);
4866 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4867 struct extent_state *state;
4868 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004869 u64 start;
4870 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004871 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004872
4873 node = rb_first(&io_tree->state);
4874 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004875 start = state->start;
4876 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004877 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004878 spin_unlock(&io_tree->lock);
4879
David Sterbaff13db42015-12-03 14:30:40 +01004880 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004881
4882 /*
4883 * If still has DELALLOC flag, the extent didn't reach disk,
4884 * and its reserved space won't be freed by delayed_ref.
4885 * So we need to free its reserved space here.
4886 * (Refer to comment in btrfs_invalidatepage, case 2)
4887 *
4888 * Note, end is the bytenr of last byte, so we need + 1 here.
4889 */
Filipe Manana421f0922018-10-12 13:02:48 +01004890 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004891 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004892
Filipe Manana6ca07092015-05-26 00:55:42 +01004893 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004894 EXTENT_LOCKED | EXTENT_DELALLOC |
4895 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4896 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004897
Filipe Manana7064dd52014-08-08 02:47:05 +01004898 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004899 spin_lock(&io_tree->lock);
4900 }
4901 spin_unlock(&io_tree->lock);
4902}
4903
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004904static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04004905 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004906{
4907 struct btrfs_fs_info *fs_info = root->fs_info;
4908 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04004909 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04004910 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04004911 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004912
Josef Bacikd3984c92019-08-01 18:19:37 -04004913 /*
4914 * Eviction should be taking place at some place safe because of our
4915 * delayed iputs. However the normal flushing code will run delayed
4916 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
4917 *
4918 * We reserve the delayed_refs_extra here again because we can't use
4919 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
4920 * above. We reserve our extra bit here because we generate a ton of
4921 * delayed refs activity by truncating.
4922 *
4923 * If we cannot make our reservation we'll attempt to steal from the
4924 * global reserve, because we really want to be able to free up space.
4925 */
4926 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
4927 BTRFS_RESERVE_FLUSH_EVICT);
4928 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004929 /*
4930 * Try to steal from the global reserve if there is space for
4931 * it.
4932 */
Josef Bacikd3984c92019-08-01 18:19:37 -04004933 if (btrfs_check_space_for_delayed_refs(fs_info) ||
4934 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
4935 btrfs_warn(fs_info,
4936 "could not allocate space for delete; will truncate on mount");
4937 return ERR_PTR(-ENOSPC);
4938 }
4939 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004940 }
Josef Bacikd3984c92019-08-01 18:19:37 -04004941
4942 trans = btrfs_join_transaction(root);
4943 if (IS_ERR(trans))
4944 return trans;
4945
4946 if (delayed_refs_extra) {
4947 trans->block_rsv = &fs_info->trans_block_rsv;
4948 trans->bytes_reserved = delayed_refs_extra;
4949 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
4950 delayed_refs_extra, 1);
4951 }
4952 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004953}
4954
Al Virobd555972010-06-07 11:35:40 -04004955void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004956{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004957 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04004958 struct btrfs_trans_handle *trans;
4959 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004960 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04004961 int ret;
4962
liubo1abe9b82011-03-24 11:18:59 +00004963 trace_btrfs_inode_evict(inode);
4964
Nikolay Borisov3d48d982016-06-29 09:46:41 +03004965 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07004966 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03004967 return;
4968 }
4969
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004970 evict_inode_truncate_pages(inode);
4971
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004972 if (inode->i_nlink &&
4973 ((btrfs_root_refs(&root->root_item) != 0 &&
4974 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004975 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04004976 goto no_delete;
4977
Omar Sandoval27919062018-05-11 13:13:37 -07004978 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04004979 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04004980
Nikolay Borisov7ab79562017-02-20 13:50:57 +02004981 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08004982
Omar Sandoval7b40b692018-05-11 13:13:33 -07004983 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00004984 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00004985
Yan, Zheng76dda932009-09-21 16:00:26 -04004986 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004987 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4988 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004989 goto no_delete;
4990 }
4991
Nikolay Borisovaa790212017-01-10 20:35:40 +02004992 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07004993 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00004994 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00004995
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004996 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07004997 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04004998 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04004999 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005000 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005001
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005002 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005003
Yan, Zheng80825102009-11-12 09:35:36 +00005004 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005005 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005006 if (IS_ERR(trans))
5007 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005008
5009 trans->block_rsv = rsv;
5010
Yan, Zhengd68fc572010-05-16 10:49:58 -04005011 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005012 trans->block_rsv = &fs_info->trans_block_rsv;
5013 btrfs_end_transaction(trans);
5014 btrfs_btree_balance_dirty(fs_info);
5015 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5016 goto free_rsv;
5017 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005018 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005019 }
5020
Josef Bacik4ef31a42013-08-13 14:10:08 -04005021 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005022 * Errors here aren't a big deal, it just means we leave orphan items in
5023 * the tree. They will be cleaned up on the next mount. If the inode
5024 * number gets reused, cleanup deletes the orphan item without doing
5025 * anything, and unlink reuses the existing orphan item.
5026 *
5027 * If it turns out that we are dropping too many of these, we might want
5028 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005029 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005030 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005031 if (!IS_ERR(trans)) {
5032 trans->block_rsv = rsv;
5033 btrfs_orphan_del(trans, BTRFS_I(inode));
5034 trans->block_rsv = &fs_info->trans_block_rsv;
5035 btrfs_end_transaction(trans);
5036 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005037
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005038 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005039 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005040 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005041
Omar Sandoval27919062018-05-11 13:13:37 -07005042free_rsv:
5043 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005044no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005045 /*
5046 * If we didn't successfully delete, the orphan item will still be in
5047 * the tree and we'll retry on the next mount. Again, we might also want
5048 * to retry these periodically in the future.
5049 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005050 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005051 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005052}
5053
5054/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005055 * Return the key found in the dir entry in the location pointer, fill @type
5056 * with BTRFS_FT_*, and return 0.
5057 *
Su Yue005d6712018-03-05 17:13:37 +08005058 * If no dir entries were found, returns -ENOENT.
5059 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005060 */
5061static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005062 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005063{
5064 const char *name = dentry->d_name.name;
5065 int namelen = dentry->d_name.len;
5066 struct btrfs_dir_item *di;
5067 struct btrfs_path *path;
5068 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005069 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005070
5071 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005072 if (!path)
5073 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005074
David Sterbaf85b7372017-01-20 14:54:07 +01005075 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5076 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005077 if (IS_ERR_OR_NULL(di)) {
5078 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005079 goto out;
5080 }
Chris Masond3977122009-01-05 21:25:51 -05005081
Chris Mason5f39d392007-10-15 16:14:19 -04005082 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005083 if (location->type != BTRFS_INODE_ITEM_KEY &&
5084 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005085 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005086 btrfs_warn(root->fs_info,
5087"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5088 __func__, name, btrfs_ino(BTRFS_I(dir)),
5089 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005090 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005091 if (!ret)
5092 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005093out:
Chris Mason39279cc2007-06-12 06:35:45 -04005094 btrfs_free_path(path);
5095 return ret;
5096}
5097
5098/*
5099 * when we hit a tree root in a directory, the btrfs part of the inode
5100 * needs to be changed to reflect the root directory of the tree root. This
5101 * is kind of like crossing a mount point.
5102 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005103static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005104 struct inode *dir,
5105 struct dentry *dentry,
5106 struct btrfs_key *location,
5107 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005108{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005109 struct btrfs_path *path;
5110 struct btrfs_root *new_root;
5111 struct btrfs_root_ref *ref;
5112 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005113 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005114 int ret;
5115 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005116
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005117 path = btrfs_alloc_path();
5118 if (!path) {
5119 err = -ENOMEM;
5120 goto out;
5121 }
Chris Mason39279cc2007-06-12 06:35:45 -04005122
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005123 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005124 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5125 key.type = BTRFS_ROOT_REF_KEY;
5126 key.offset = location->objectid;
5127
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005128 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005129 if (ret) {
5130 if (ret < 0)
5131 err = ret;
5132 goto out;
5133 }
Chris Mason39279cc2007-06-12 06:35:45 -04005134
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005135 leaf = path->nodes[0];
5136 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005137 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005138 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5139 goto out;
5140
5141 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5142 (unsigned long)(ref + 1),
5143 dentry->d_name.len);
5144 if (ret)
5145 goto out;
5146
David Sterbab3b4aa72011-04-21 01:20:15 +02005147 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005148
Josef Bacik3619c942020-01-24 09:32:24 -05005149 new_root = btrfs_get_fs_root(fs_info, location, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005150 if (IS_ERR(new_root)) {
5151 err = PTR_ERR(new_root);
5152 goto out;
5153 }
5154
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005155 *sub_root = new_root;
5156 location->objectid = btrfs_root_dirid(&new_root->root_item);
5157 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005158 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005159 err = 0;
5160out:
5161 btrfs_free_path(path);
5162 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005163}
5164
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005165static void inode_tree_add(struct inode *inode)
5166{
5167 struct btrfs_root *root = BTRFS_I(inode)->root;
5168 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005169 struct rb_node **p;
5170 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005171 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005172 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005173
Al Viro1d3382cb2010-10-23 15:19:20 -04005174 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005175 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005176 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005177 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005178 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005179 while (*p) {
5180 parent = *p;
5181 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5182
David Sterba37508512018-06-29 10:56:40 +02005183 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005184 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005185 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005186 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005187 else {
5188 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005189 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005190 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005191 RB_CLEAR_NODE(parent);
5192 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005193 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005194 }
5195 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005196 rb_link_node(new, parent, p);
5197 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005198 spin_unlock(&root->inode_lock);
5199}
5200
5201static void inode_tree_del(struct inode *inode)
5202{
5203 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005204 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005205
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005206 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005207 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005208 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005209 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005210 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005211 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005212 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005213
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005214 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005215 spin_lock(&root->inode_lock);
5216 empty = RB_EMPTY_ROOT(&root->inode_tree);
5217 spin_unlock(&root->inode_lock);
5218 if (empty)
5219 btrfs_add_dead_root(root);
5220 }
5221}
5222
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005223
Chris Masone02119d2008-09-05 16:13:11 -04005224static int btrfs_init_locked_inode(struct inode *inode, void *p)
5225{
5226 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005227 inode->i_ino = args->location->objectid;
5228 memcpy(&BTRFS_I(inode)->location, args->location,
5229 sizeof(*args->location));
Josef Bacik5c8fd992020-02-14 16:11:43 -05005230 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5231 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005232 return 0;
5233}
5234
5235static int btrfs_find_actor(struct inode *inode, void *opaque)
5236{
5237 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005238 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005239 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005240}
5241
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005242static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005243 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005244 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005245{
5246 struct inode *inode;
5247 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005248 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005249
Chris Mason90d3e592014-01-09 17:28:00 -08005250 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005251 args.root = root;
5252
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005253 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005254 btrfs_init_locked_inode,
5255 (void *)&args);
5256 return inode;
5257}
5258
David Sterba4c66e0d2019-10-03 19:09:35 +02005259/*
5260 * Get an inode object given its location and corresponding root.
5261 * Path can be preallocated to prevent recursing back to iget through
5262 * allocator. NULL is also valid but may require an additional allocation
5263 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305264 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005265struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005266 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305267{
5268 struct inode *inode;
5269
Chris Mason90d3e592014-01-09 17:28:00 -08005270 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305271 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005272 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305273
5274 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005275 int ret;
5276
Filipe Manana4222ea72018-10-24 10:13:03 +01005277 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005278 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005279 inode_tree_add(inode);
5280 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005281 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005282 iget_failed(inode);
5283 /*
5284 * ret > 0 can come from btrfs_search_slot called by
5285 * btrfs_read_locked_inode, this means the inode item
5286 * was not found.
5287 */
5288 if (ret > 0)
5289 ret = -ENOENT;
5290 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005291 }
5292 }
5293
Balaji Rao1a54ef82008-07-21 02:01:04 +05305294 return inode;
5295}
5296
Filipe Manana4222ea72018-10-24 10:13:03 +01005297struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005298 struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005299{
David Sterba4c66e0d2019-10-03 19:09:35 +02005300 return btrfs_iget_path(s, location, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005301}
5302
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005303static struct inode *new_simple_dir(struct super_block *s,
5304 struct btrfs_key *key,
5305 struct btrfs_root *root)
5306{
5307 struct inode *inode = new_inode(s);
5308
5309 if (!inode)
5310 return ERR_PTR(-ENOMEM);
5311
Josef Bacik5c8fd992020-02-14 16:11:43 -05005312 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005313 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005314 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005315
5316 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005317 /*
5318 * We only need lookup, the rest is read-only and there's no inode
5319 * associated with the dentry
5320 */
5321 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005322 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005323 inode->i_fop = &simple_dir_operations;
5324 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005325 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305326 inode->i_atime = inode->i_mtime;
5327 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005328 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005329
5330 return inode;
5331}
5332
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005333static inline u8 btrfs_inode_type(struct inode *inode)
5334{
5335 /*
5336 * Compile-time asserts that generic FT_* types still match
5337 * BTRFS_FT_* types
5338 */
5339 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5340 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5341 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5342 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5343 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5344 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5345 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5346 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5347
5348 return fs_umode_to_ftype(inode->i_mode);
5349}
5350
Chris Mason3de45862008-11-17 21:02:50 -05005351struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005352{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005353 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005354 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005355 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005356 struct btrfs_root *sub_root = root;
5357 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005358 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005359 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005360
5361 if (dentry->d_name.len > BTRFS_NAME_LEN)
5362 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005363
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005364 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005365 if (ret < 0)
5366 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005367
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005368 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba4c66e0d2019-10-03 19:09:35 +02005369 inode = btrfs_iget(dir->i_sb, &location, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005370 if (IS_ERR(inode))
5371 return inode;
5372
5373 /* Do extra check against inode mode with di_type */
5374 if (btrfs_inode_type(inode) != di_type) {
5375 btrfs_crit(fs_info,
5376"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5377 inode->i_mode, btrfs_inode_type(inode),
5378 di_type);
5379 iput(inode);
5380 return ERR_PTR(-EUCLEAN);
5381 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005382 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005383 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005384
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005385 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005386 &location, &sub_root);
5387 if (ret < 0) {
5388 if (ret != -ENOENT)
5389 inode = ERR_PTR(ret);
5390 else
5391 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5392 } else {
David Sterba4c66e0d2019-10-03 19:09:35 +02005393 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005394 }
Josef Bacik87270022020-01-24 09:32:31 -05005395 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005396 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005397
Julia Lawall34d19ba2011-01-24 19:55:19 +00005398 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005399 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005400 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005401 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005402 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005403 if (ret) {
5404 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005405 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005406 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005407 }
5408
Chris Mason3de45862008-11-17 21:02:50 -05005409 return inode;
5410}
5411
Nick Pigginfe15ce42011-01-07 17:49:23 +11005412static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005413{
5414 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005415 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005416
Li Zefan848cce02012-02-21 17:04:28 +08005417 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005418 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005419
Li Zefan848cce02012-02-21 17:04:28 +08005420 if (inode) {
5421 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005422 if (btrfs_root_refs(&root->root_item) == 0)
5423 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005424
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005425 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005426 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005427 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005428 return 0;
5429}
5430
Chris Mason3de45862008-11-17 21:02:50 -05005431static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005432 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005433{
Al Viro3837d202018-10-10 16:38:27 -04005434 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005435
Al Viro3837d202018-10-10 16:38:27 -04005436 if (inode == ERR_PTR(-ENOENT))
5437 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005438 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005439}
5440
Josef Bacik23b5ec72017-07-24 15:14:25 -04005441/*
5442 * All this infrastructure exists because dir_emit can fault, and we are holding
5443 * the tree lock when doing readdir. For now just allocate a buffer and copy
5444 * our information into that, and then dir_emit from the buffer. This is
5445 * similar to what NFS does, only we don't keep the buffer around in pagecache
5446 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5447 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5448 * tree lock.
5449 */
5450static int btrfs_opendir(struct inode *inode, struct file *file)
5451{
5452 struct btrfs_file_private *private;
5453
5454 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5455 if (!private)
5456 return -ENOMEM;
5457 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5458 if (!private->filldir_buf) {
5459 kfree(private);
5460 return -ENOMEM;
5461 }
5462 file->private_data = private;
5463 return 0;
5464}
5465
5466struct dir_entry {
5467 u64 ino;
5468 u64 offset;
5469 unsigned type;
5470 int name_len;
5471};
5472
5473static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5474{
5475 while (entries--) {
5476 struct dir_entry *entry = addr;
5477 char *name = (char *)(entry + 1);
5478
David Sterba92d32172018-04-16 21:10:14 +02005479 ctx->pos = get_unaligned(&entry->offset);
5480 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5481 get_unaligned(&entry->ino),
5482 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005483 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005484 addr += sizeof(struct dir_entry) +
5485 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005486 ctx->pos++;
5487 }
5488 return 0;
5489}
5490
Al Viro9cdda8d2013-05-22 16:48:09 -04005491static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005492{
Al Viro9cdda8d2013-05-22 16:48:09 -04005493 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005494 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005495 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005496 struct btrfs_dir_item *di;
5497 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005498 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005499 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005500 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005501 struct list_head ins_list;
5502 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005503 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005504 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005505 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005506 char *name_ptr;
5507 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005508 int entries = 0;
5509 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005510 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005511 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005512
Al Viro9cdda8d2013-05-22 16:48:09 -04005513 if (!dir_emit_dots(file, ctx))
5514 return 0;
5515
David Woodhouse49593bf2008-08-17 17:08:36 +01005516 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005517 if (!path)
5518 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005519
Josef Bacik23b5ec72017-07-24 15:14:25 -04005520 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005521 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005522
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005523 INIT_LIST_HEAD(&ins_list);
5524 INIT_LIST_HEAD(&del_list);
5525 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005526
Josef Bacik23b5ec72017-07-24 15:14:25 -04005527again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005528 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005529 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005530 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005531
Chris Mason39279cc2007-06-12 06:35:45 -04005532 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5533 if (ret < 0)
5534 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005535
5536 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005537 struct dir_entry *entry;
5538
Chris Mason5f39d392007-10-15 16:14:19 -04005539 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005540 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005541 if (slot >= btrfs_header_nritems(leaf)) {
5542 ret = btrfs_next_leaf(root, path);
5543 if (ret < 0)
5544 goto err;
5545 else if (ret > 0)
5546 break;
5547 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005548 }
Chris Mason3de45862008-11-17 21:02:50 -05005549
Chris Mason5f39d392007-10-15 16:14:19 -04005550 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5551
5552 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005553 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005554 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005555 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005556 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005557 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005558 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005559 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005560 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005561 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005562 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5563 PAGE_SIZE) {
5564 btrfs_release_path(path);
5565 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5566 if (ret)
5567 goto nopos;
5568 addr = private->filldir_buf;
5569 entries = 0;
5570 total_len = 0;
5571 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005572 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005573
5574 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005575 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005576 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005577 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5578 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005579 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005580 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005581 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005582 put_unaligned(location.objectid, &entry->ino);
5583 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005584 entries++;
5585 addr += sizeof(struct dir_entry) + name_len;
5586 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005587next:
5588 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005589 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005590 btrfs_release_path(path);
5591
5592 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5593 if (ret)
5594 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005595
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005596 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005597 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005598 goto nopos;
5599
Zach Browndb62efb2013-07-11 16:19:42 -07005600 /*
5601 * Stop new entries from being returned after we return the last
5602 * entry.
5603 *
5604 * New directory entries are assigned a strictly increasing
5605 * offset. This means that new entries created during readdir
5606 * are *guaranteed* to be seen in the future by that readdir.
5607 * This has broken buggy programs which operate on names as
5608 * they're returned by readdir. Until we re-use freed offsets
5609 * we have this hack to stop new entries from being returned
5610 * under the assumption that they'll never reach this huge
5611 * offset.
5612 *
5613 * This is being careful not to overflow 32bit loff_t unless the
5614 * last entry requires it because doing so has broken 32bit apps
5615 * in the past.
5616 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005617 if (ctx->pos >= INT_MAX)
5618 ctx->pos = LLONG_MAX;
5619 else
5620 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005621nopos:
5622 ret = 0;
5623err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005624 if (put)
5625 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005626 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005627 return ret;
5628}
5629
Chris Mason39279cc2007-06-12 06:35:45 -04005630/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005631 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005632 * inode changes. But, it is most likely to find the inode in cache.
5633 * FIXME, needs more benchmarking...there are no reasons other than performance
5634 * to keep or drop this code.
5635 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005636static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005637{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005638 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005639 struct btrfs_root *root = BTRFS_I(inode)->root;
5640 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005641 int ret;
5642
Josef Bacik72ac3c02012-05-23 14:13:11 -04005643 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005644 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005645
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005646 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005647 if (IS_ERR(trans))
5648 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005649
5650 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005651 if (ret && ret == -ENOSPC) {
5652 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005653 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005654 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005655 if (IS_ERR(trans))
5656 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005657
Chris Mason94b60442010-05-26 11:02:00 -04005658 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005659 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005660 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005661 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005662 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005663
5664 return ret;
5665}
5666
5667/*
5668 * This is a copy of file_update_time. We need this so we can return error on
5669 * ENOSPC for updating the inode in the case of file write and mmap writes.
5670 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005671static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005672 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005673{
Alexander Block2bc5565282012-06-15 09:49:33 +02005674 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005675 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005676
5677 if (btrfs_root_readonly(root))
5678 return -EROFS;
5679
Josef Bacike41f9412012-03-26 09:46:47 -04005680 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005681 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005682 if (flags & S_CTIME)
5683 inode->i_ctime = *now;
5684 if (flags & S_MTIME)
5685 inode->i_mtime = *now;
5686 if (flags & S_ATIME)
5687 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005688 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005689}
5690
Chris Masond352ac62008-09-29 15:18:18 -04005691/*
5692 * find the highest existing sequence number in a directory
5693 * and then set the in-memory index_cnt variable to reflect
5694 * free sequence numbers
5695 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005696static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005697{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005698 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005699 struct btrfs_key key, found_key;
5700 struct btrfs_path *path;
5701 struct extent_buffer *leaf;
5702 int ret;
5703
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005704 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005705 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005706 key.offset = (u64)-1;
5707
5708 path = btrfs_alloc_path();
5709 if (!path)
5710 return -ENOMEM;
5711
5712 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5713 if (ret < 0)
5714 goto out;
5715 /* FIXME: we should be able to handle this */
5716 if (ret == 0)
5717 goto out;
5718 ret = 0;
5719
5720 /*
5721 * MAGIC NUMBER EXPLANATION:
5722 * since we search a directory based on f_pos we have to start at 2
5723 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5724 * else has to start at 2
5725 */
5726 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005727 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005728 goto out;
5729 }
5730
5731 path->slots[0]--;
5732
5733 leaf = path->nodes[0];
5734 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5735
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005736 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005737 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005738 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005739 goto out;
5740 }
5741
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005742 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005743out:
5744 btrfs_free_path(path);
5745 return ret;
5746}
5747
Chris Masond352ac62008-09-29 15:18:18 -04005748/*
5749 * helper to find a free sequence number in a given directory. This current
5750 * code is very simple, later versions will do smarter things in the btree
5751 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005752int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005753{
5754 int ret = 0;
5755
Nikolay Borisov877574e2017-02-20 13:50:33 +02005756 if (dir->index_cnt == (u64)-1) {
5757 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005758 if (ret) {
5759 ret = btrfs_set_inode_index_count(dir);
5760 if (ret)
5761 return ret;
5762 }
Josef Bacikaec74772008-07-24 12:12:38 -04005763 }
5764
Nikolay Borisov877574e2017-02-20 13:50:33 +02005765 *index = dir->index_cnt;
5766 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005767
5768 return ret;
5769}
5770
Chris Masonb0d5d102014-09-08 13:08:51 -07005771static int btrfs_insert_inode_locked(struct inode *inode)
5772{
5773 struct btrfs_iget_args args;
5774 args.location = &BTRFS_I(inode)->location;
5775 args.root = BTRFS_I(inode)->root;
5776
5777 return insert_inode_locked4(inode,
5778 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5779 btrfs_find_actor, &args);
5780}
5781
Anand Jain19aee8d2017-07-18 17:37:05 +08005782/*
5783 * Inherit flags from the parent inode.
5784 *
5785 * Currently only the compression flags and the cow flags are inherited.
5786 */
5787static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5788{
5789 unsigned int flags;
5790
5791 if (!dir)
5792 return;
5793
5794 flags = BTRFS_I(dir)->flags;
5795
5796 if (flags & BTRFS_INODE_NOCOMPRESS) {
5797 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5798 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5799 } else if (flags & BTRFS_INODE_COMPRESS) {
5800 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5801 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5802 }
5803
5804 if (flags & BTRFS_INODE_NODATACOW) {
5805 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5806 if (S_ISREG(inode->i_mode))
5807 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5808 }
5809
David Sterba7b6a2212018-03-26 18:40:21 +02005810 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005811}
5812
Chris Mason39279cc2007-06-12 06:35:45 -04005813static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5814 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005815 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005816 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005817 u64 ref_objectid, u64 objectid,
5818 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005819{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005820 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005821 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005822 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005823 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005824 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005825 struct btrfs_inode_ref *ref;
5826 struct btrfs_key key[2];
5827 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005828 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005829 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005830 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005831 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005832
Chris Mason5f39d392007-10-15 16:14:19 -04005833 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005834 if (!path)
5835 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005836
Josef Bacik11a19a92019-09-09 10:12:04 -04005837 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005838 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005839 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005840 if (!inode) {
5841 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005842 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005843 }
Chris Mason39279cc2007-06-12 06:35:45 -04005844
Li Zefan581bb052011-04-20 10:06:11 +08005845 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005846 * O_TMPFILE, set link count to 0, so that after this point,
5847 * we fill in an inode item with the correct link count.
5848 */
5849 if (!name)
5850 set_nlink(inode, 0);
5851
5852 /*
Li Zefan581bb052011-04-20 10:06:11 +08005853 * we have to initialize this early, so we can reclaim the inode
5854 * number if we fail afterwards in this function.
5855 */
5856 inode->i_ino = objectid;
5857
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005858 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005859 trace_btrfs_inode_request(dir);
5860
Nikolay Borisov877574e2017-02-20 13:50:33 +02005861 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005862 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005863 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005864 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005865 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005866 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005867 } else if (dir) {
5868 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005869 }
5870 /*
5871 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005872 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005873 * number
5874 */
5875 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005876 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005877 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005878 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005879 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005880
Josef Bacik5dc562c2012-08-17 13:14:17 -04005881 /*
5882 * We could have gotten an inode number from somebody who was fsynced
5883 * and then removed in this same transaction, so let's just set full
5884 * sync since it will be a full sync anyway and this will blow away the
5885 * old info in the log.
5886 */
5887 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5888
Chris Mason9c583092008-01-29 15:15:18 -05005889 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005890 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005891 key[0].offset = 0;
5892
Chris Mason9c583092008-01-29 15:15:18 -05005893 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005894
5895 if (name) {
5896 /*
5897 * Start new inodes with an inode_ref. This is slightly more
5898 * efficient for small numbers of hard links since they will
5899 * be packed into one item. Extended refs will kick in if we
5900 * add more hard links than can fit in the ref item.
5901 */
5902 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005903 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005904 key[1].offset = ref_objectid;
5905
5906 sizes[1] = name_len + sizeof(*ref);
5907 }
Chris Mason9c583092008-01-29 15:15:18 -05005908
Chris Masonb0d5d102014-09-08 13:08:51 -07005909 location = &BTRFS_I(inode)->location;
5910 location->objectid = objectid;
5911 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005912 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005913
5914 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04005915 if (ret < 0) {
5916 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005917 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04005918 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005919
Chris Masonb9473432009-03-13 11:00:37 -04005920 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005921 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005922 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005923 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005924
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005925 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005926 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05305927
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005928 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305929 inode->i_atime = inode->i_mtime;
5930 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005931 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05305932
Chris Mason5f39d392007-10-15 16:14:19 -04005933 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5934 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01005935 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06005936 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005937 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005938
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005939 if (name) {
5940 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5941 struct btrfs_inode_ref);
5942 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5943 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5944 ptr = (unsigned long)(ref + 1);
5945 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5946 }
Chris Mason9c583092008-01-29 15:15:18 -05005947
Chris Mason5f39d392007-10-15 16:14:19 -04005948 btrfs_mark_buffer_dirty(path->nodes[0]);
5949 btrfs_free_path(path);
5950
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005951 btrfs_inherit_iflags(inode, dir);
5952
Al Viro569254b2011-07-24 17:08:40 -04005953 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005954 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04005955 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005956 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005957 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5958 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005959 }
5960
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005961 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005962
5963 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005964 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005965
Alexander Block8ea05e32012-07-25 17:35:53 +02005966 btrfs_update_root_times(trans, root);
5967
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005968 ret = btrfs_inode_inherit_props(trans, inode, dir);
5969 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005970 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005971 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01005972 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005973
Chris Mason39279cc2007-06-12 06:35:45 -04005974 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005975
5976fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04005977 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005978fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005979 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005980 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005981 btrfs_free_path(path);
5982 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005983}
5984
Chris Masond352ac62008-09-29 15:18:18 -04005985/*
5986 * utility function to add 'inode' into 'parent_inode' with
5987 * a give name and a given sequence number.
5988 * if 'add_backref' is true, also insert a backref from the
5989 * inode to the parent directory.
5990 */
Chris Masone02119d2008-09-05 16:13:11 -04005991int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005992 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04005993 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04005994{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005995 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005996 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005997 struct btrfs_root *root = parent_inode->root;
5998 u64 ino = btrfs_ino(inode);
5999 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006000
Li Zefan33345d012011-04-20 10:31:50 +08006001 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006002 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006003 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006004 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006005 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006006 key.offset = 0;
6007 }
Chris Mason39279cc2007-06-12 06:35:45 -04006008
Li Zefan33345d012011-04-20 10:31:50 +08006009 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006010 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006011 root->root_key.objectid, parent_ino,
6012 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006013 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006014 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6015 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006016 }
6017
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006018 /* Nothing to clean up yet */
6019 if (ret)
6020 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006021
Lu Fengqi684572d2018-08-04 21:10:57 +08006022 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006023 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006024 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006025 goto fail_dir_item;
6026 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006027 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006028 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006029 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006030
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006031 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006032 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006033 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006034 /*
6035 * If we are replaying a log tree, we do not want to update the mtime
6036 * and ctime of the parent directory with the current time, since the
6037 * log replay procedure is responsible for setting them to their correct
6038 * values (the ones it had when the fsync was done).
6039 */
6040 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6041 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6042
6043 parent_inode->vfs_inode.i_mtime = now;
6044 parent_inode->vfs_inode.i_ctime = now;
6045 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006046 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006047 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006048 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006049 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006050
6051fail_dir_item:
6052 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6053 u64 local_index;
6054 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006055 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006056 root->root_key.objectid, parent_ino,
6057 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006058 if (err)
6059 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006060 } else if (add_backref) {
6061 u64 local_index;
6062 int err;
6063
6064 err = btrfs_del_inode_ref(trans, root, name, name_len,
6065 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006066 if (err)
6067 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006068 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006069
6070 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006071 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006072}
6073
6074static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006075 struct btrfs_inode *dir, struct dentry *dentry,
6076 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006077{
Josef Bacika1b075d2010-11-19 20:36:11 +00006078 int err = btrfs_add_link(trans, dir, inode,
6079 dentry->d_name.name, dentry->d_name.len,
6080 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006081 if (err > 0)
6082 err = -EEXIST;
6083 return err;
6084}
6085
Josef Bacik618e21d2007-07-11 10:18:17 -04006086static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006087 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006088{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006089 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006090 struct btrfs_trans_handle *trans;
6091 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006092 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006093 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006094 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006095 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006096
Josef Bacik9ed74f22009-09-11 16:12:44 -04006097 /*
6098 * 2 for inode item and ref
6099 * 2 for dir items
6100 * 1 for xattr if selinux is on
6101 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006102 trans = btrfs_start_transaction(root, 5);
6103 if (IS_ERR(trans))
6104 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006105
Li Zefan581bb052011-04-20 10:06:11 +08006106 err = btrfs_find_free_ino(root, &objectid);
6107 if (err)
6108 goto out_unlock;
6109
Josef Bacikaec74772008-07-24 12:12:38 -04006110 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006111 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6112 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006113 if (IS_ERR(inode)) {
6114 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006115 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006116 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006117 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006118
Casey Schauflerad19db72011-12-15 10:09:07 -05006119 /*
6120 * If the active LSM wants to access the inode during
6121 * d_instantiate it needs these. Smack checks to see
6122 * if the filesystem supports xattrs by looking at the
6123 * ops vector.
6124 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006125 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006126 init_special_inode(inode, inode->i_mode, rdev);
6127
6128 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006129 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006130 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006131
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006132 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6133 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006134 if (err)
6135 goto out_unlock;
6136
6137 btrfs_update_inode(trans, root, inode);
6138 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006139
Josef Bacik618e21d2007-07-11 10:18:17 -04006140out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006141 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006142 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006143 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006144 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006145 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006146 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006147 return err;
6148}
6149
Chris Mason39279cc2007-06-12 06:35:45 -04006150static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006151 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006152{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006153 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006154 struct btrfs_trans_handle *trans;
6155 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006156 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006157 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006158 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006159 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006160
Josef Bacik9ed74f22009-09-11 16:12:44 -04006161 /*
6162 * 2 for inode item and ref
6163 * 2 for dir items
6164 * 1 for xattr if selinux is on
6165 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006166 trans = btrfs_start_transaction(root, 5);
6167 if (IS_ERR(trans))
6168 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006169
Li Zefan581bb052011-04-20 10:06:11 +08006170 err = btrfs_find_free_ino(root, &objectid);
6171 if (err)
6172 goto out_unlock;
6173
Josef Bacikaec74772008-07-24 12:12:38 -04006174 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006175 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6176 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006177 if (IS_ERR(inode)) {
6178 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006179 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006180 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006181 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006182 /*
6183 * If the active LSM wants to access the inode during
6184 * d_instantiate it needs these. Smack checks to see
6185 * if the filesystem supports xattrs by looking at the
6186 * ops vector.
6187 */
6188 inode->i_fop = &btrfs_file_operations;
6189 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006190 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006191
6192 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6193 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006194 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006195
6196 err = btrfs_update_inode(trans, root, inode);
6197 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006198 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006199
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006200 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6201 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006202 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006203 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006204
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006205 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006206 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006207
Chris Mason39279cc2007-06-12 06:35:45 -04006208out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006209 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006210 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006211 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006212 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006213 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006214 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006215 return err;
6216}
6217
6218static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6219 struct dentry *dentry)
6220{
Filipe Manana271dba452016-01-05 16:24:05 +00006221 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006222 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006223 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006224 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006225 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006226 int err;
6227 int drop_inode = 0;
6228
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006229 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006230 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006231 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006232
Mark Fashehf1863732012-08-08 11:32:27 -07006233 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006234 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006235
Nikolay Borisov877574e2017-02-20 13:50:33 +02006236 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006237 if (err)
6238 goto fail;
6239
Yan, Zhenga22285a2010-05-16 10:48:46 -04006240 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006241 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006242 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006243 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006244 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006245 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006246 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006247 if (IS_ERR(trans)) {
6248 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006249 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006250 goto fail;
6251 }
Chris Mason5f39d392007-10-15 16:14:19 -04006252
Miao Xie67de1172013-12-26 13:07:06 +08006253 /* There are several dir indexes for this inode, clear the cache. */
6254 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006255 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006256 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006257 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006258 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006259 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006260
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006261 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6262 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006263
Yan, Zhenga5719522009-09-24 09:17:31 -04006264 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006265 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006266 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006267 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006268 int ret;
6269
Yan, Zhenga5719522009-09-24 09:17:31 -04006270 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006271 if (err)
6272 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006273 if (inode->i_nlink == 1) {
6274 /*
6275 * If new hard link count is 1, it's a file created
6276 * with open(2) O_TMPFILE flag.
6277 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006278 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006279 if (err)
6280 goto fail;
6281 }
Al Viro08c422c2011-12-23 07:58:13 -05006282 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006283 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6284 true, NULL);
6285 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6286 err = btrfs_commit_transaction(trans);
6287 trans = NULL;
6288 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006289 }
Chris Mason39279cc2007-06-12 06:35:45 -04006290
Chris Mason1832a6d2007-12-21 16:27:21 -05006291fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006292 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006293 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006294 if (drop_inode) {
6295 inode_dec_link_count(inode);
6296 iput(inode);
6297 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006298 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006299 return err;
6300}
6301
Al Viro18bb1db2011-07-26 01:41:39 -04006302static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006303{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006304 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006305 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006306 struct btrfs_trans_handle *trans;
6307 struct btrfs_root *root = BTRFS_I(dir)->root;
6308 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006309 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006310 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006311
Josef Bacik9ed74f22009-09-11 16:12:44 -04006312 /*
6313 * 2 items for inode and ref
6314 * 2 items for dir items
6315 * 1 for xattr if selinux is on
6316 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006317 trans = btrfs_start_transaction(root, 5);
6318 if (IS_ERR(trans))
6319 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006320
Li Zefan581bb052011-04-20 10:06:11 +08006321 err = btrfs_find_free_ino(root, &objectid);
6322 if (err)
6323 goto out_fail;
6324
Josef Bacikaec74772008-07-24 12:12:38 -04006325 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006326 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6327 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006328 if (IS_ERR(inode)) {
6329 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006330 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006331 goto out_fail;
6332 }
Chris Mason5f39d392007-10-15 16:14:19 -04006333
Chris Masonb0d5d102014-09-08 13:08:51 -07006334 /* these must be set before we unlock the inode */
6335 inode->i_op = &btrfs_dir_inode_operations;
6336 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006337
Eric Paris2a7dba32011-02-01 11:05:39 -05006338 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006339 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006340 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006341
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006342 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006343 err = btrfs_update_inode(trans, root, inode);
6344 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006345 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006346
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006347 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6348 dentry->d_name.name,
6349 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006350 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006351 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006352
Al Viro1e2e5472018-05-04 08:23:01 -04006353 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006354
6355out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006356 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006357 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006358 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006359 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006360 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006361 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006362 return err;
6363}
6364
Chris Masonc8b97812008-10-29 14:49:59 -04006365static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006366 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006367 size_t pg_offset, u64 extent_offset,
6368 struct btrfs_file_extent_item *item)
6369{
6370 int ret;
6371 struct extent_buffer *leaf = path->nodes[0];
6372 char *tmp;
6373 size_t max_size;
6374 unsigned long inline_size;
6375 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006376 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006377
6378 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006379 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006380 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6381 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006382 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006383 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006384 if (!tmp)
6385 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006386 ptr = btrfs_file_extent_inline_start(item);
6387
6388 read_extent_buffer(leaf, tmp, ptr, inline_size);
6389
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006390 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006391 ret = btrfs_decompress(compress_type, tmp, page,
6392 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006393
6394 /*
6395 * decompression code contains a memset to fill in any space between the end
6396 * of the uncompressed data and the end of max_size in case the decompressed
6397 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6398 * the end of an inline extent and the beginning of the next block, so we
6399 * cover that region here.
6400 */
6401
6402 if (max_size + pg_offset < PAGE_SIZE) {
6403 char *map = kmap(page);
6404 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6405 kunmap(page);
6406 }
Chris Masonc8b97812008-10-29 14:49:59 -04006407 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006408 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006409}
6410
Omar Sandoval39b07b52019-12-02 17:34:23 -08006411/**
6412 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6413 * @inode: file to search in
6414 * @page: page to read extent data into if the extent is inline
6415 * @pg_offset: offset into @page to copy to
6416 * @start: file offset
6417 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006418 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006419 * This returns the first &struct extent_map which overlaps with the given
6420 * range, reading it from the B-tree and caching it if necessary. Note that
6421 * there may be more extents which overlap the given range after the returned
6422 * extent_map.
6423 *
6424 * If @page is not NULL and the extent is inline, this also reads the extent
6425 * data directly into the page and marks the extent up to date in the io_tree.
6426 *
6427 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006428 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006429struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006430 struct page *page, size_t pg_offset,
6431 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006432{
David Sterba3ffbd682018-06-29 10:56:42 +02006433 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006434 int ret;
6435 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006436 u64 extent_start = 0;
6437 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006438 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006439 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006440 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006441 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006442 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006443 struct extent_buffer *leaf;
6444 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006445 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006446 struct extent_map_tree *em_tree = &inode->extent_tree;
6447 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006448
Chris Mason890871b2009-09-02 16:24:52 -04006449 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006450 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006451 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006452
Chris Masona52d9a82007-08-27 16:49:44 -04006453 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006454 if (em->start > start || em->start + em->len <= start)
6455 free_extent_map(em);
6456 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006457 free_extent_map(em);
6458 else
6459 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006460 }
David Sterba172ddd62011-04-21 00:48:27 +02006461 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006462 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006463 err = -ENOMEM;
6464 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006465 }
Chris Masond1310b22008-01-24 16:13:08 -05006466 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006467 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006468 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006469 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006470
Liu Bobee6ec82018-08-17 05:05:28 +08006471 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006472 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006473 err = -ENOMEM;
6474 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006475 }
6476
Liu Bobee6ec82018-08-17 05:05:28 +08006477 /* Chances are we'll be called again, so go ahead and do readahead */
6478 path->reada = READA_FORWARD;
6479
Liu Boe49aabd2018-08-25 13:47:09 +08006480 /*
6481 * Unless we're going to uncompress the inline extent, no sleep would
6482 * happen.
6483 */
6484 path->leave_spinning = 1;
6485
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006486 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006487 if (ret < 0) {
6488 err = ret;
6489 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006490 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006491 if (path->slots[0] == 0)
6492 goto not_found;
6493 path->slots[0]--;
6494 }
6495
Chris Mason5f39d392007-10-15 16:14:19 -04006496 leaf = path->nodes[0];
6497 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006498 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006499 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006500 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006501 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006502 /*
6503 * If we backup past the first extent we want to move forward
6504 * and see if there is an extent in front of us, otherwise we'll
6505 * say there is a hole for our whole search range which can
6506 * cause problems.
6507 */
6508 extent_end = start;
6509 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006510 }
6511
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006512 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006513 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006514 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006515 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6516 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006517 /* Only regular file could have regular/prealloc extent */
6518 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6519 ret = -EUCLEAN;
6520 btrfs_crit(fs_info,
6521 "regular/prealloc extent found for non-regular inode %llu",
6522 btrfs_ino(inode));
6523 goto out;
6524 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006525 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6526 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006527 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006528 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6529 path->slots[0],
6530 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006531 }
Josef Bacik25a50342013-10-14 12:08:38 -04006532next:
Yan Zheng9036c102008-10-30 14:19:41 -04006533 if (start >= extent_end) {
6534 path->slots[0]++;
6535 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6536 ret = btrfs_next_leaf(root, path);
6537 if (ret < 0) {
6538 err = ret;
6539 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006540 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006541 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006542 }
Yan Zheng9036c102008-10-30 14:19:41 -04006543 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006544 }
Yan Zheng9036c102008-10-30 14:19:41 -04006545 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6546 if (found_key.objectid != objectid ||
6547 found_key.type != BTRFS_EXTENT_DATA_KEY)
6548 goto not_found;
6549 if (start + len <= found_key.offset)
6550 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006551 if (start > found_key.offset)
6552 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006553
6554 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006555 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006556 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006557 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006558 em->block_start = EXTENT_MAP_HOLE;
6559 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006560 }
6561
Omar Sandoval39b07b52019-12-02 17:34:23 -08006562 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006563
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006564 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6565 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006566 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006567 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006568 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006569 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006570 size_t size;
6571 size_t extent_offset;
6572 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006573
Omar Sandoval39b07b52019-12-02 17:34:23 -08006574 if (!page)
Yan689f9342007-10-29 11:41:07 -04006575 goto out;
Yan689f9342007-10-29 11:41:07 -04006576
Qu Wenruoe41ca582018-06-06 15:41:49 +08006577 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006578 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006579 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6580 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006581 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006582 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006583 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006584 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006585 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006586
6587 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006588 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006589 if (btrfs_file_extent_compression(leaf, item) !=
6590 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006591 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006592 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006593 if (ret) {
6594 err = ret;
6595 goto out;
6596 }
Chris Masonc8b97812008-10-29 14:49:59 -04006597 } else {
6598 map = kmap(page);
6599 read_extent_buffer(leaf, map + pg_offset, ptr,
6600 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006601 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006602 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006603 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006604 copy_size);
6605 }
Chris Masonc8b97812008-10-29 14:49:59 -04006606 kunmap(page);
6607 }
Chris Mason179e29e2007-11-01 11:28:41 -04006608 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006609 }
Chris Masond1310b22008-01-24 16:13:08 -05006610 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006611 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006612 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006613 }
6614not_found:
6615 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006616 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006617 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006618 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006619insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006620 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006621 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006622 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006623 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6624 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006625 err = -EIO;
6626 goto out;
6627 }
Chris Masond1310b22008-01-24 16:13:08 -05006628
6629 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006630 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006631 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006632 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006633out:
Liu Boc6414282018-08-23 07:36:17 +08006634 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006635
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006636 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006637
Chris Masona52d9a82007-08-27 16:49:44 -04006638 if (err) {
6639 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006640 return ERR_PTR(err);
6641 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006642 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006643 return em;
6644}
6645
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006646struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006647 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006648{
6649 struct extent_map *em;
6650 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006651 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006652 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006653 u64 delalloc_len;
6654 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006655 int err = 0;
6656
Omar Sandoval39b07b52019-12-02 17:34:23 -08006657 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006658 if (IS_ERR(em))
6659 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006660 /*
6661 * If our em maps to:
6662 * - a hole or
6663 * - a pre-alloc extent,
6664 * there might actually be delalloc bytes behind it.
6665 */
6666 if (em->block_start != EXTENT_MAP_HOLE &&
6667 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6668 return em;
6669 else
6670 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006671
6672 /* check to see if we've wrapped (len == -1 or similar) */
6673 end = start + len;
6674 if (end < start)
6675 end = (u64)-1;
6676 else
6677 end -= 1;
6678
6679 em = NULL;
6680
6681 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006682 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006683 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006684 delalloc_end = delalloc_start + delalloc_len;
6685 if (delalloc_end < delalloc_start)
6686 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006687
6688 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006689 * We didn't find anything useful, return the original results from
6690 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006691 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006692 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006693 em = hole_em;
6694 hole_em = NULL;
6695 goto out;
6696 }
6697
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006698 /*
6699 * Adjust the delalloc_start to make sure it doesn't go backwards from
6700 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006701 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006702 delalloc_start = max(start, delalloc_start);
6703 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006704
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006705 if (delalloc_len > 0) {
6706 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006707 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006708 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006709
David Sterba172ddd62011-04-21 00:48:27 +02006710 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006711 if (!em) {
6712 err = -ENOMEM;
6713 goto out;
6714 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006715
6716 ASSERT(hole_em);
6717 /*
6718 * When btrfs_get_extent can't find anything it returns one
6719 * huge hole
6720 *
6721 * Make sure what it found really fits our range, and adjust to
6722 * make sure it is based on the start from the caller
6723 */
6724 if (hole_end <= start || hole_em->start > end) {
6725 free_extent_map(hole_em);
6726 hole_em = NULL;
6727 } else {
6728 hole_start = max(hole_em->start, start);
6729 hole_len = hole_end - hole_start;
6730 }
6731
6732 if (hole_em && delalloc_start > hole_start) {
6733 /*
6734 * Our hole starts before our delalloc, so we have to
6735 * return just the parts of the hole that go until the
6736 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006737 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006738 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006739 em->start = hole_start;
6740 em->orig_start = hole_start;
6741 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006742 * Don't adjust block start at all, it is fixed at
6743 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006744 */
6745 em->block_start = hole_em->block_start;
6746 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006747 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6748 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006749 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006750 /*
6751 * Hole is out of passed range or it starts after
6752 * delalloc range
6753 */
6754 em->start = delalloc_start;
6755 em->len = delalloc_len;
6756 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006757 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006758 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006759 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006760 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006761 return hole_em;
6762 }
6763out:
6764
6765 free_extent_map(hole_em);
6766 if (err) {
6767 free_extent_map(em);
6768 return ERR_PTR(err);
6769 }
6770 return em;
6771}
6772
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006773static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
6774 const u64 start,
6775 const u64 len,
6776 const u64 orig_start,
6777 const u64 block_start,
6778 const u64 block_len,
6779 const u64 orig_block_len,
6780 const u64 ram_bytes,
6781 const int type)
6782{
6783 struct extent_map *em = NULL;
6784 int ret;
6785
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006786 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08006787 em = create_io_em(inode, start, len, orig_start,
6788 block_start, block_len, orig_block_len,
6789 ram_bytes,
6790 BTRFS_COMPRESS_NONE, /* compress_type */
6791 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006792 if (IS_ERR(em))
6793 goto out;
6794 }
6795 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
6796 len, block_len, type);
6797 if (ret) {
6798 if (em) {
6799 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02006800 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006801 start + len - 1, 0);
6802 }
6803 em = ERR_PTR(ret);
6804 }
6805 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006806
6807 return em;
6808}
6809
Josef Bacik4b46fce2010-05-23 11:00:55 -04006810static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6811 u64 start, u64 len)
6812{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006813 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006814 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006815 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006816 struct btrfs_key ins;
6817 u64 alloc_hint;
6818 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006819
Josef Bacik4b46fce2010-05-23 11:00:55 -04006820 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006821 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006822 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006823 if (ret)
6824 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006825
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006826 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
6827 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006828 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006829 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006830 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006831 btrfs_free_reserved_extent(fs_info, ins.objectid,
6832 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006833
Josef Bacik4b46fce2010-05-23 11:00:55 -04006834 return em;
6835}
6836
Chris Mason46bfbb52010-05-26 11:04:10 -04006837/*
6838 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6839 * block must be cow'd
6840 */
Josef Bacik00361582013-08-14 14:02:47 -04006841noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006842 u64 *orig_start, u64 *orig_block_len,
6843 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006844{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006845 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006846 struct btrfs_path *path;
6847 int ret;
6848 struct extent_buffer *leaf;
6849 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006850 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006851 struct btrfs_file_extent_item *fi;
6852 struct btrfs_key key;
6853 u64 disk_bytenr;
6854 u64 backref_offset;
6855 u64 extent_end;
6856 u64 num_bytes;
6857 int slot;
6858 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006859 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006860
Chris Mason46bfbb52010-05-26 11:04:10 -04006861 path = btrfs_alloc_path();
6862 if (!path)
6863 return -ENOMEM;
6864
David Sterbaf85b7372017-01-20 14:54:07 +01006865 ret = btrfs_lookup_file_extent(NULL, root, path,
6866 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006867 if (ret < 0)
6868 goto out;
6869
6870 slot = path->slots[0];
6871 if (ret == 1) {
6872 if (slot == 0) {
6873 /* can't find the item, must cow */
6874 ret = 0;
6875 goto out;
6876 }
6877 slot--;
6878 }
6879 ret = 0;
6880 leaf = path->nodes[0];
6881 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006882 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006883 key.type != BTRFS_EXTENT_DATA_KEY) {
6884 /* not our file or wrong item type, must cow */
6885 goto out;
6886 }
6887
6888 if (key.offset > offset) {
6889 /* Wrong offset, must cow */
6890 goto out;
6891 }
6892
6893 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6894 found_type = btrfs_file_extent_type(leaf, fi);
6895 if (found_type != BTRFS_FILE_EXTENT_REG &&
6896 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6897 /* not a regular extent, must cow */
6898 goto out;
6899 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006900
6901 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6902 goto out;
6903
Miao Xiee77751a2013-12-27 21:11:50 +08006904 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6905 if (extent_end <= offset)
6906 goto out;
6907
Chris Mason46bfbb52010-05-26 11:04:10 -04006908 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006909 if (disk_bytenr == 0)
6910 goto out;
6911
6912 if (btrfs_file_extent_compression(leaf, fi) ||
6913 btrfs_file_extent_encryption(leaf, fi) ||
6914 btrfs_file_extent_other_encoding(leaf, fi))
6915 goto out;
6916
Ethan Lien78d42952018-05-17 14:58:29 +08006917 /*
6918 * Do the same check as in btrfs_cross_ref_exist but without the
6919 * unnecessary search.
6920 */
6921 if (btrfs_file_extent_generation(leaf, fi) <=
6922 btrfs_root_last_snapshot(&root->root_item))
6923 goto out;
6924
Chris Mason46bfbb52010-05-26 11:04:10 -04006925 backref_offset = btrfs_file_extent_offset(leaf, fi);
6926
Josef Bacik7ee9e442013-06-21 16:37:03 -04006927 if (orig_start) {
6928 *orig_start = key.offset - backref_offset;
6929 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6930 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6931 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006932
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006933 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04006934 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006935
6936 num_bytes = min(offset + *len, extent_end) - offset;
6937 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6938 u64 range_end;
6939
Jeff Mahoneyda170662016-06-15 09:22:56 -04006940 range_end = round_up(offset + num_bytes,
6941 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08006942 ret = test_range_bit(io_tree, offset, range_end,
6943 EXTENT_DELALLOC, 0, NULL);
6944 if (ret) {
6945 ret = -EAGAIN;
6946 goto out;
6947 }
6948 }
6949
Josef Bacik1bda19e2013-10-18 12:10:36 -04006950 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006951
6952 /*
6953 * look for other files referencing this extent, if we
6954 * find any we must cow
6955 */
Josef Bacik00361582013-08-14 14:02:47 -04006956
Liu Boe4c3b2d2017-01-30 12:25:28 -08006957 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04006958 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04006959 if (ret) {
6960 ret = 0;
6961 goto out;
6962 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006963
6964 /*
6965 * adjust disk_bytenr and num_bytes to cover just the bytes
6966 * in this extent we are about to write. If there
6967 * are any csums in that range we have to cow in order
6968 * to keep the csums correct
6969 */
6970 disk_bytenr += backref_offset;
6971 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006972 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
6973 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04006974 /*
6975 * all of the above have passed, it is safe to overwrite this extent
6976 * without cow
6977 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006978 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006979 ret = 1;
6980out:
6981 btrfs_free_path(path);
6982 return ret;
6983}
6984
Josef Bacikeb838e72012-07-31 16:28:48 -04006985static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
6986 struct extent_state **cached_state, int writing)
6987{
6988 struct btrfs_ordered_extent *ordered;
6989 int ret = 0;
6990
6991 while (1) {
6992 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01006993 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04006994 /*
6995 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04006996 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04006997 * extents in this range.
6998 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02006999 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007000 lockend - lockstart + 1);
7001
7002 /*
7003 * We need to make sure there are no buffered pages in this
7004 * range either, we could have raced between the invalidate in
7005 * generic_file_direct_write and locking the extent. The
7006 * invalidate needs to happen so that reads after a write do not
7007 * get stale data.
7008 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007009 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007010 (!writing || !filemap_range_has_page(inode->i_mapping,
7011 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007012 break;
7013
7014 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007015 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007016
7017 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007018 /*
7019 * If we are doing a DIO read and the ordered extent we
7020 * found is for a buffered write, we can not wait for it
7021 * to complete and retry, because if we do so we can
7022 * deadlock with concurrent buffered writes on page
7023 * locks. This happens only if our DIO read covers more
7024 * than one extent map, if at this point has already
7025 * created an ordered extent for a previous extent map
7026 * and locked its range in the inode's io tree, and a
7027 * concurrent write against that previous extent map's
7028 * range and this range started (we unlock the ranges
7029 * in the io tree only when the bios complete and
7030 * buffered writes always lock pages before attempting
7031 * to lock range in the io tree).
7032 */
7033 if (writing ||
7034 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7035 btrfs_start_ordered_extent(inode, ordered, 1);
7036 else
7037 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007038 btrfs_put_ordered_extent(ordered);
7039 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007040 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007041 * We could trigger writeback for this range (and wait
7042 * for it to complete) and then invalidate the pages for
7043 * this range (through invalidate_inode_pages2_range()),
7044 * but that can lead us to a deadlock with a concurrent
7045 * call to readpages() (a buffered read or a defrag call
7046 * triggered a readahead) on a page lock due to an
7047 * ordered dio extent we created before but did not have
7048 * yet a corresponding bio submitted (whence it can not
7049 * complete), which makes readpages() wait for that
7050 * ordered extent to complete while holding a lock on
7051 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007052 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007053 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007054 }
7055
Filipe Mananaade77022016-02-18 14:28:55 +00007056 if (ret)
7057 break;
7058
Josef Bacikeb838e72012-07-31 16:28:48 -04007059 cond_resched();
7060 }
7061
7062 return ret;
7063}
7064
Liu Bo6f9994d2017-01-31 07:50:22 -08007065/* The callers of this must take lock_extent() */
7066static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7067 u64 orig_start, u64 block_start,
7068 u64 block_len, u64 orig_block_len,
7069 u64 ram_bytes, int compress_type,
7070 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007071{
7072 struct extent_map_tree *em_tree;
7073 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007074 int ret;
7075
Liu Bo6f9994d2017-01-31 07:50:22 -08007076 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7077 type == BTRFS_ORDERED_COMPRESSED ||
7078 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007079 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007080
Josef Bacik69ffb542012-09-11 15:40:07 -04007081 em_tree = &BTRFS_I(inode)->extent_tree;
7082 em = alloc_extent_map();
7083 if (!em)
7084 return ERR_PTR(-ENOMEM);
7085
7086 em->start = start;
7087 em->orig_start = orig_start;
7088 em->len = len;
7089 em->block_len = block_len;
7090 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007091 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007092 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007093 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007094 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007095 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007096 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007097 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007098 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7099 em->compress_type = compress_type;
7100 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007101
7102 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007103 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007104 em->start + em->len - 1, 0);
7105 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007106 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007107 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007108 /*
7109 * The caller has taken lock_extent(), who could race with us
7110 * to add em?
7111 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007112 } while (ret == -EEXIST);
7113
7114 if (ret) {
7115 free_extent_map(em);
7116 return ERR_PTR(ret);
7117 }
7118
Liu Bo6f9994d2017-01-31 07:50:22 -08007119 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007120 return em;
7121}
7122
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007123
7124static int btrfs_get_blocks_direct_read(struct extent_map *em,
7125 struct buffer_head *bh_result,
7126 struct inode *inode,
7127 u64 start, u64 len)
7128{
David Sterba8530c372019-08-30 15:42:07 +02007129 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7130
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007131 if (em->block_start == EXTENT_MAP_HOLE ||
7132 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7133 return -ENOENT;
7134
7135 len = min(len, em->len - (start - em->start));
7136
7137 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7138 inode->i_blkbits;
7139 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007140 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007141 set_buffer_mapped(bh_result);
7142
7143 return 0;
7144}
7145
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007146static int btrfs_get_blocks_direct_write(struct extent_map **map,
7147 struct buffer_head *bh_result,
7148 struct inode *inode,
7149 struct btrfs_dio_data *dio_data,
7150 u64 start, u64 len)
7151{
7152 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7153 struct extent_map *em = *map;
7154 int ret = 0;
7155
7156 /*
7157 * We don't allocate a new extent in the following cases
7158 *
7159 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7160 * existing extent.
7161 * 2) The extent is marked as PREALLOC. We're good to go here and can
7162 * just use the extent.
7163 *
7164 */
7165 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7166 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7167 em->block_start != EXTENT_MAP_HOLE)) {
7168 int type;
7169 u64 block_start, orig_start, orig_block_len, ram_bytes;
7170
7171 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7172 type = BTRFS_ORDERED_PREALLOC;
7173 else
7174 type = BTRFS_ORDERED_NOCOW;
7175 len = min(len, em->len - (start - em->start));
7176 block_start = em->block_start + (start - em->start);
7177
7178 if (can_nocow_extent(inode, start, &len, &orig_start,
7179 &orig_block_len, &ram_bytes) == 1 &&
7180 btrfs_inc_nocow_writers(fs_info, block_start)) {
7181 struct extent_map *em2;
7182
7183 em2 = btrfs_create_dio_extent(inode, start, len,
7184 orig_start, block_start,
7185 len, orig_block_len,
7186 ram_bytes, type);
7187 btrfs_dec_nocow_writers(fs_info, block_start);
7188 if (type == BTRFS_ORDERED_PREALLOC) {
7189 free_extent_map(em);
7190 *map = em = em2;
7191 }
7192
7193 if (em2 && IS_ERR(em2)) {
7194 ret = PTR_ERR(em2);
7195 goto out;
7196 }
7197 /*
7198 * For inode marked NODATACOW or extent marked PREALLOC,
7199 * use the existing or preallocated extent, so does not
7200 * need to adjust btrfs_space_info's bytes_may_use.
7201 */
7202 btrfs_free_reserved_data_space_noquota(inode, start,
7203 len);
7204 goto skip_cow;
7205 }
7206 }
7207
7208 /* this will cow the extent */
7209 len = bh_result->b_size;
7210 free_extent_map(em);
7211 *map = em = btrfs_new_extent_direct(inode, start, len);
7212 if (IS_ERR(em)) {
7213 ret = PTR_ERR(em);
7214 goto out;
7215 }
7216
7217 len = min(len, em->len - (start - em->start));
7218
7219skip_cow:
7220 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7221 inode->i_blkbits;
7222 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007223 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007224 set_buffer_mapped(bh_result);
7225
7226 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7227 set_buffer_new(bh_result);
7228
7229 /*
7230 * Need to update the i_size under the extent lock so buffered
7231 * readers will get the updated i_size when we unlock.
7232 */
7233 if (!dio_data->overwrite && start + len > i_size_read(inode))
7234 i_size_write(inode, start + len);
7235
7236 WARN_ON(dio_data->reserve < len);
7237 dio_data->reserve -= len;
7238 dio_data->unsubmitted_oe_range_end = start + len;
7239 current->journal_info = dio_data;
7240out:
7241 return ret;
7242}
7243
Josef Bacik4b46fce2010-05-23 11:00:55 -04007244static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7245 struct buffer_head *bh_result, int create)
7246{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007247 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007248 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007249 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307250 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007251 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007252 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007253 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007254 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007255
Omar Sandovale1821632019-08-15 14:04:04 -07007256 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007257 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007258
Josef Bacikc3298612012-08-03 16:49:19 -04007259 lockstart = start;
7260 lockend = start + len - 1;
7261
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007262 if (current->journal_info) {
7263 /*
7264 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007265 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007266 * confused.
7267 */
chandan50745b02015-08-28 21:10:13 +05307268 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007269 current->journal_info = NULL;
7270 }
7271
Josef Bacikeb838e72012-07-31 16:28:48 -04007272 /*
7273 * If this errors out it's because we couldn't invalidate pagecache for
7274 * this range and we need to fallback to buffered.
7275 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007276 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7277 create)) {
7278 ret = -ENOTBLK;
7279 goto err;
7280 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007281
Omar Sandoval39b07b52019-12-02 17:34:23 -08007282 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007283 if (IS_ERR(em)) {
7284 ret = PTR_ERR(em);
7285 goto unlock_err;
7286 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007287
7288 /*
7289 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7290 * io. INLINE is special, and we could probably kludge it in here, but
7291 * it's still buffered so for safety lets just fall back to the generic
7292 * buffered path.
7293 *
7294 * For COMPRESSED we _have_ to read the entire extent in so we can
7295 * decompress it, so there will be buffering required no matter what we
7296 * do, so go ahead and fallback to buffered.
7297 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007298 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007299 * to buffered IO. Don't blame me, this is the price we pay for using
7300 * the generic code.
7301 */
7302 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7303 em->block_start == EXTENT_MAP_INLINE) {
7304 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007305 ret = -ENOTBLK;
7306 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007307 }
7308
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007309 if (create) {
7310 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7311 dio_data, start, len);
7312 if (ret < 0)
7313 goto unlock_err;
7314
Omar Sandovale1821632019-08-15 14:04:04 -07007315 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7316 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007317 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007318 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7319 start, len);
7320 /* Can be negative only if we read from a hole */
7321 if (ret < 0) {
7322 ret = 0;
7323 free_extent_map(em);
7324 goto unlock_err;
7325 }
7326 /*
7327 * We need to unlock only the end area that we aren't using.
7328 * The rest is going to be unlocked by the endio routine.
7329 */
7330 lockstart = start + bh_result->b_size;
7331 if (lockstart < lockend) {
Omar Sandovale1821632019-08-15 14:04:04 -07007332 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7333 lockstart, lockend, &cached_state);
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007334 } else {
7335 free_extent_state(cached_state);
7336 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007337 }
7338
Josef Bacik4b46fce2010-05-23 11:00:55 -04007339 free_extent_map(em);
7340
7341 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007342
7343unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007344 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7345 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007346err:
chandan50745b02015-08-28 21:10:13 +05307347 if (dio_data)
7348 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007349 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007350}
7351
Omar Sandoval769b4f22020-04-16 14:46:22 -07007352static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7353{
7354 /*
7355 * This implies a barrier so that stores to dio_bio->bi_status before
7356 * this and loads of dio_bio->bi_status after this are fully ordered.
7357 */
7358 if (!refcount_dec_and_test(&dip->refs))
7359 return;
7360
7361 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
7362 __endio_write_update_ordered(dip->inode, dip->logical_offset,
7363 dip->bytes,
7364 !dip->dio_bio->bi_status);
7365 } else {
7366 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7367 dip->logical_offset,
7368 dip->logical_offset + dip->bytes - 1);
7369 }
7370
7371 dio_end_io(dip->dio_bio);
7372 kfree(dip);
7373}
7374
Omar Sandoval77d5d682020-04-16 14:46:25 -07007375static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7376 int mirror_num,
7377 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007378{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007379 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007380 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007381 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007382
Mike Christie37226b22016-06-05 14:31:52 -05007383 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007384
Omar Sandoval5c047a62020-04-16 14:46:24 -07007385 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007386 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007387 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007388
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007389 refcount_inc(&dip->refs);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007390 ret = btrfs_map_bio(fs_info, bio, mirror_num);
7391 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007392 refcount_dec(&dip->refs);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007393 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007394}
7395
Omar Sandoval769b4f22020-04-16 14:46:22 -07007396static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7397 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007398 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007399{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007400 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7401 const u32 sectorsize = fs_info->sectorsize;
7402 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
7403 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7404 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7405 struct bio_vec bvec;
7406 struct bvec_iter iter;
7407 u64 start = io_bio->logical;
7408 int icsum = 0;
7409 blk_status_t err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007410
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007411 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7412 unsigned int i, nr_sectors, pgoff;
7413
7414 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7415 pgoff = bvec.bv_offset;
7416 for (i = 0; i < nr_sectors; i++) {
7417 ASSERT(pgoff < PAGE_SIZE);
7418 if (uptodate &&
7419 (!csum || !check_data_csum(inode, io_bio, icsum,
7420 bvec.bv_page, pgoff,
7421 start, sectorsize))) {
7422 clean_io_failure(fs_info, failure_tree, io_tree,
7423 start, bvec.bv_page,
7424 btrfs_ino(BTRFS_I(inode)),
7425 pgoff);
7426 } else {
7427 blk_status_t status;
7428
Omar Sandoval77d5d682020-04-16 14:46:25 -07007429 status = btrfs_submit_read_repair(inode,
7430 &io_bio->bio,
7431 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007432 bvec.bv_page, pgoff,
7433 start,
7434 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007435 io_bio->mirror_num,
7436 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007437 if (status)
7438 err = status;
7439 }
7440 start += sectorsize;
7441 icsum++;
7442 pgoff += sectorsize;
7443 }
Miao Xie8b110e32014-09-12 18:44:03 +08007444 }
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007445 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007446}
7447
Qu Wenruo524272602017-03-08 10:25:52 +08007448static void __endio_write_update_ordered(struct inode *inode,
7449 const u64 offset, const u64 bytes,
7450 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007451{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007452 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007453 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007454 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007455 u64 ordered_offset = offset;
7456 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007457 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007458
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007459 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08007460 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007461 else
Qu Wenruo524272602017-03-08 10:25:52 +08007462 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007463
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007464 while (ordered_offset < offset + bytes) {
7465 last_offset = ordered_offset;
7466 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
7467 &ordered_offset,
7468 ordered_bytes,
7469 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007470 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7471 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007472 btrfs_queue_work(wq, &ordered->work);
7473 }
7474 /*
7475 * If btrfs_dec_test_ordered_pending does not find any ordered
7476 * extent in the range, we can exit.
7477 */
7478 if (ordered_offset == last_offset)
7479 return;
7480 /*
7481 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007482 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007483 */
7484 if (ordered_offset < offset + bytes) {
7485 ordered_bytes = offset + bytes - ordered_offset;
7486 ordered = NULL;
7487 }
Chris Mason163cf092010-11-28 19:56:33 -05007488 }
Filipe Manana14543772015-11-24 16:23:54 +00007489}
7490
David Sterbad0ee3932018-03-08 14:35:48 +01007491static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007492 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007493{
Josef Bacikc6100a42017-05-05 11:57:13 -04007494 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007495 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007496 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007497 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007498 return 0;
7499}
7500
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007501static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007502{
7503 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007504 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007505
Miao Xie8b110e32014-09-12 18:44:03 +08007506 if (err)
7507 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007508 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007509 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7510 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007511 (unsigned long long)bio->bi_iter.bi_sector,
7512 bio->bi_iter.bi_size, err);
7513
Omar Sandoval769b4f22020-04-16 14:46:22 -07007514 if (bio_op(bio) == REQ_OP_READ) {
7515 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007516 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007517 }
7518
Omar Sandoval769b4f22020-04-16 14:46:22 -07007519 if (err)
7520 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007521
Miao Xiee65e1532010-11-22 03:04:43 +00007522 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007523 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007524}
7525
David Sterbad0ee3932018-03-08 14:35:48 +01007526static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7527 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007528{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007529 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007530 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007531 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007532 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007533
Liu Bo4c274bc2017-11-01 17:19:27 -06007534 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007535 if (async_submit)
7536 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7537
Josef Bacik5fd02042012-05-02 14:00:54 -04007538 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007539 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007540 if (ret)
7541 goto err;
7542 }
Miao Xiee65e1532010-11-22 03:04:43 +00007543
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007544 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007545 goto map;
7546
7547 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007548 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7549 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007550 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007551 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007552 } else if (write) {
7553 /*
7554 * If we aren't doing async submit, calculate the csum of the
7555 * bio now.
7556 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007557 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007558 if (ret)
7559 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007560 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007561 u64 csum_offset;
7562
7563 csum_offset = file_offset - dip->logical_offset;
7564 csum_offset >>= inode->i_sb->s_blocksize_bits;
7565 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7566 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007567 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007568map:
Chris Mason08635ba2019-07-10 12:28:14 -07007569 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007570err:
Miao Xiee65e1532010-11-22 03:04:43 +00007571 return ret;
7572}
7573
Omar Sandovalc36cac22020-04-16 14:46:13 -07007574/*
7575 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7576 * or ordered extents whether or not we submit any bios.
7577 */
7578static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7579 struct inode *inode,
7580 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007581{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007582 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007583 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7584 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007585 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007586
Omar Sandoval85879572020-04-16 14:46:21 -07007587 dip_size = sizeof(*dip);
7588 if (!write && csum) {
7589 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7590 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7591 size_t nblocks;
7592
7593 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7594 dip_size += csum_size * nblocks;
7595 }
7596
7597 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007598 if (!dip)
7599 return NULL;
7600
Omar Sandovalc36cac22020-04-16 14:46:13 -07007601 dip->inode = inode;
7602 dip->logical_offset = file_offset;
7603 dip->bytes = dio_bio->bi_iter.bi_size;
7604 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007605 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007606 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007607
7608 if (write) {
7609 struct btrfs_dio_data *dio_data = current->journal_info;
7610
7611 /*
7612 * Setting range start and end to the same value means that
7613 * no cleanup will happen in btrfs_direct_IO
7614 */
7615 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7616 dip->bytes;
7617 dio_data->unsubmitted_oe_range_start =
7618 dio_data->unsubmitted_oe_range_end;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007619 }
7620 return dip;
7621}
7622
7623static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7624 loff_t file_offset)
7625{
7626 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007627 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007628 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007629 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7630 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007631 struct btrfs_dio_private *dip;
7632 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007633 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007634 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007635 u64 submit_len;
7636 int clone_offset = 0;
7637 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307638 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007639 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007640 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007641
Omar Sandovalc36cac22020-04-16 14:46:13 -07007642 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7643 if (!dip) {
7644 if (!write) {
7645 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7646 file_offset + dio_bio->bi_iter.bi_size - 1);
7647 }
7648 dio_bio->bi_status = BLK_STS_RESOURCE;
7649 dio_end_io(dio_bio);
7650 return;
7651 }
7652
Omar Sandoval85879572020-04-16 14:46:21 -07007653 if (!write && csum) {
7654 /*
7655 * Load the csums up front to reduce csum tree searches and
7656 * contention when submitting bios.
7657 */
7658 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7659 dip->csums);
7660 if (status != BLK_STS_OK)
7661 goto out_err;
7662 }
7663
Omar Sandoval769b4f22020-04-16 14:46:22 -07007664 start_sector = dio_bio->bi_iter.bi_sector;
7665 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiefacc8a222013-07-25 19:22:34 +08007666
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007667 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007668 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7669 start_sector << 9, submit_len,
7670 &geom);
7671 if (ret) {
7672 status = errno_to_blk_status(ret);
7673 goto out_err;
7674 }
7675 ASSERT(geom.len <= INT_MAX);
7676
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007677 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007678
Liu Bo725130b2017-05-16 09:51:39 -07007679 /*
7680 * This will never fail as it's passing GPF_NOFS and
7681 * the allocation is backed by btrfs_bioset.
7682 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007683 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007684 bio->bi_private = dip;
7685 bio->bi_end_io = btrfs_end_dio_bio;
7686 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007687
Liu Bo725130b2017-05-16 09:51:39 -07007688 ASSERT(submit_len >= clone_len);
7689 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007690
Liu Bo725130b2017-05-16 09:51:39 -07007691 /*
7692 * Increase the count before we submit the bio so we know
7693 * the end IO handler won't happen before we increase the
7694 * count. Otherwise, the dip might get freed before we're
7695 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007696 *
7697 * We transfer the initial reference to the last bio, so we
7698 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007699 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007700 if (submit_len > 0) {
7701 refcount_inc(&dip->refs);
7702 /*
7703 * If we are submitting more than one bio, submit them
7704 * all asynchronously. The exception is RAID 5 or 6, as
7705 * asynchronous checksums make it difficult to collect
7706 * full stripe writes.
7707 */
7708 if (!raid56)
7709 async_submit = 1;
7710 }
Miao Xiee65e1532010-11-22 03:04:43 +00007711
David Sterbad0ee3932018-03-08 14:35:48 +01007712 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007713 async_submit);
7714 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007715 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007716 if (submit_len > 0)
7717 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007718 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007719 }
Liu Bo725130b2017-05-16 09:51:39 -07007720
7721 clone_offset += clone_len;
7722 start_sector += clone_len >> 9;
7723 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007724 } while (submit_len > 0);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007725 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007726
Miao Xiee65e1532010-11-22 03:04:43 +00007727out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007728 dip->dio_bio->bi_status = status;
7729 btrfs_dio_private_put(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007730}
7731
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007732static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007733 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04007734{
7735 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00007736 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007737 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007738 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007739
7740 if (offset & blocksize_mask)
7741 goto out;
7742
Al Viro28060d52014-03-22 05:15:17 -04007743 if (iov_iter_alignment(iter) & blocksize_mask)
7744 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00007745
Al Viro28060d52014-03-22 05:15:17 -04007746 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04007747 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04007748 return 0;
7749 /*
7750 * Check to make sure we don't have duplicate iov_base's in this
7751 * iovec, if so return EINVAL, otherwise we'll get csum errors
7752 * when reading back.
7753 */
7754 for (seg = 0; seg < iter->nr_segs; seg++) {
7755 for (i = seg + 1; i < iter->nr_segs; i++) {
7756 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00007757 goto out;
7758 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04007759 }
7760 retval = 0;
7761out:
7762 return retval;
7763}
Josef Bacikeb838e72012-07-31 16:28:48 -04007764
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07007765static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04007766{
Josef Bacik4b46fce2010-05-23 11:00:55 -04007767 struct file *file = iocb->ki_filp;
7768 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007769 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05307770 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08007771 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07007772 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00007773 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00007774 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00007775 bool wakeup = true;
7776 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00007777 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007778
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03007779 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04007780 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04007781
Jens Axboefe0f07d2015-04-15 17:05:48 -06007782 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007783
Josef Bacik0e267c42013-07-02 10:38:02 -04007784 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08007785 * The generic stuff only does filemap_write_and_wait_range, which
7786 * isn't enough if we've written compressed pages to this area, so
7787 * we need to flush the dirty pages again to make absolutely sure
7788 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04007789 */
Al Viroa6cbcd42014-03-04 22:38:00 -05007790 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08007791 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7792 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08007793 filemap_fdatawrite_range(inode->i_mapping, offset,
7794 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04007795
Omar Sandoval6f673762015-03-16 04:33:52 -07007796 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00007797 /*
7798 * If the write DIO is beyond the EOF, we need update
7799 * the isize, but it is protected by i_mutex. So we can
7800 * not unlock the i_mutex at this case.
7801 */
7802 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08007803 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05007804 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007805 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05007806 } else if (iocb->ki_flags & IOCB_NOWAIT) {
7807 ret = -EAGAIN;
7808 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00007809 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08007810 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
7811 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00007812 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00007813 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007814
7815 /*
7816 * We need to know how many extents we reserved so that we can
7817 * do the accounting properly if we go over the number we
7818 * originally calculated. Abuse current->journal_info for this.
7819 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04007820 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007821 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00007822 dio_data.unsubmitted_oe_range_start = (u64)offset;
7823 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05307824 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05307825 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02007826 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7827 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06007828 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007829 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7830 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00007831 }
7832
Omar Sandoval17f8c842015-03-16 04:33:50 -07007833 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007834 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07007835 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07007836 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07007837 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05307838 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007839 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08007840 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05307841 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08007842 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08007843 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00007844 /*
7845 * On error we might have left some ordered extents
7846 * without submitting corresponding bios for them, so
7847 * cleanup them up to avoid other tasks getting them
7848 * and waiting for them to complete forever.
7849 */
7850 if (dio_data.unsubmitted_oe_range_start <
7851 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08007852 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00007853 dio_data.unsubmitted_oe_range_start,
7854 dio_data.unsubmitted_oe_range_end -
7855 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08007856 false);
Liu Boddba1bf2015-06-17 16:59:58 +08007857 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08007858 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08007859 offset, count - (size_t)ret, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08007860 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00007861 }
Miao Xie38851cc2013-02-08 07:04:11 +00007862out:
Miao Xie2e60a512013-02-08 07:01:08 +00007863 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06007864 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00007865 if (relock)
Al Viro59551022016-01-22 15:40:57 -05007866 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00007867
Qu Wenruo364ecf32017-02-27 15:10:38 +08007868 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00007869 return ret;
Chris Mason16432982008-04-10 10:23:21 -04007870}
7871
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007872#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
7873
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007874static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7875 __u64 start, __u64 len)
7876{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007877 int ret;
7878
7879 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
7880 if (ret)
7881 return ret;
7882
David Sterba2135fb92017-06-23 04:09:57 +02007883 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007884}
7885
Chris Mason9ebefb182007-06-15 13:50:00 -04007886int btrfs_readpage(struct file *file, struct page *page)
7887{
David Sterba71ad38b2020-02-05 19:09:35 +01007888 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007889}
Chris Mason1832a6d2007-12-21 16:27:21 -05007890
Chris Mason39279cc2007-06-12 06:35:45 -04007891static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7892{
Josef Bacikbe7bd732015-10-22 15:05:09 -04007893 struct inode *inode = page->mapping->host;
7894 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04007895
7896 if (current->flags & PF_MEMALLOC) {
7897 redirty_page_for_writepage(wbc, page);
7898 unlock_page(page);
7899 return 0;
7900 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04007901
7902 /*
7903 * If we are under memory pressure we will call this directly from the
7904 * VM, we need to make sure we have the inode referenced for the ordered
7905 * extent. If not just return like we didn't do anything.
7906 */
7907 if (!igrab(inode)) {
7908 redirty_page_for_writepage(wbc, page);
7909 return AOP_WRITEPAGE_ACTIVATE;
7910 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02007911 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04007912 btrfs_add_delayed_iput(inode);
7913 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04007914}
Chris Mason39279cc2007-06-12 06:35:45 -04007915
Eric Sandeen48a3b632013-04-25 20:41:01 +00007916static int btrfs_writepages(struct address_space *mapping,
7917 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007918{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03007919 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04007920}
7921
Chris Mason3ab2fb52007-11-08 10:59:22 -05007922static int
7923btrfs_readpages(struct file *file, struct address_space *mapping,
7924 struct list_head *pages, unsigned nr_pages)
7925{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03007926 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05007927}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03007928
Chris Masone6dcd2d2008-07-17 12:53:50 -04007929static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007930{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03007931 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04007932 if (ret == 1) {
7933 ClearPagePrivate(page);
7934 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007935 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007936 }
7937 return ret;
7938}
Chris Mason39279cc2007-06-12 06:35:45 -04007939
Chris Masone6dcd2d2008-07-17 12:53:50 -04007940static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7941{
Chris Mason98509cf2008-09-11 15:51:43 -04007942 if (PageWriteback(page) || PageDirty(page))
7943 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01007944 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04007945}
7946
Roman Gushchinf8e66082020-03-04 16:57:35 -08007947#ifdef CONFIG_MIGRATION
7948static int btrfs_migratepage(struct address_space *mapping,
7949 struct page *newpage, struct page *page,
7950 enum migrate_mode mode)
7951{
7952 int ret;
7953
7954 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
7955 if (ret != MIGRATEPAGE_SUCCESS)
7956 return ret;
7957
7958 if (page_has_private(page)) {
7959 ClearPagePrivate(page);
7960 get_page(newpage);
7961 set_page_private(newpage, page_private(page));
7962 set_page_private(page, 0);
7963 put_page(page);
7964 SetPagePrivate(newpage);
7965 }
7966
7967 if (PagePrivate2(page)) {
7968 ClearPagePrivate2(page);
7969 SetPagePrivate2(newpage);
7970 }
7971
7972 if (mode != MIGRATE_SYNC_NO_COPY)
7973 migrate_page_copy(newpage, page);
7974 else
7975 migrate_page_states(newpage, page);
7976 return MIGRATEPAGE_SUCCESS;
7977}
7978#endif
7979
Lukas Czernerd47992f2013-05-21 23:17:23 -04007980static void btrfs_invalidatepage(struct page *page, unsigned int offset,
7981 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04007982{
Josef Bacik5fd02042012-05-02 14:00:54 -04007983 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05007984 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007985 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00007986 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04007987 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03007988 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05307989 u64 start;
7990 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00007991 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04007992
Chris Mason8b62b722009-09-02 16:53:46 -04007993 /*
7994 * we have the page locked, so new writeback can't start,
7995 * and the dirty bit won't be cleared while we are here.
7996 *
7997 * Wait for IO on this page so that we can safely clear
7998 * the PagePrivate2 bit and do ordered accounting
7999 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008000 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008001
Josef Bacik5fd02042012-05-02 14:00:54 -04008002 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008003 if (offset) {
8004 btrfs_releasepage(page, GFP_NOFS);
8005 return;
8006 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008007
8008 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008009 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308010again:
8011 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008012 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308013 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008014 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008015 end = min(page_end,
8016 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008017 /*
8018 * IO on this page will never be started, so we need
8019 * to account for any ordered extents now
8020 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008021 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308022 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008023 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008024 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008025 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008026 /*
8027 * whoever cleared the private bit is responsible
8028 * for the finish_ordered_io
8029 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008030 if (TestClearPagePrivate2(page)) {
8031 struct btrfs_ordered_inode_tree *tree;
8032 u64 new_len;
8033
8034 tree = &BTRFS_I(inode)->ordered_tree;
8035
8036 spin_lock_irq(&tree->lock);
8037 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308038 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008039 if (new_len < ordered->truncated_len)
8040 ordered->truncated_len = new_len;
8041 spin_unlock_irq(&tree->lock);
8042
8043 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308044 start,
8045 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008046 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008047 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008048 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008049 if (!inode_evicting) {
8050 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308051 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008052 &cached_state);
8053 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308054
8055 start = end + 1;
8056 if (start < page_end)
8057 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008058 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008059
Qu Wenruob9d0b382015-09-29 10:35:16 +08008060 /*
8061 * Qgroup reserved space handler
8062 * Page here will be either
8063 * 1) Already written to disk
8064 * In this case, its reserved space is released from data rsv map
8065 * and will be freed by delayed_ref handler finally.
8066 * So even we call qgroup_free_data(), it won't decrease reserved
8067 * space.
8068 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008069 * This means the reserved space should be freed here. However,
8070 * if a truncate invalidates the page (by clearing PageDirty)
8071 * and the page is accounted for while allocating extent
8072 * in btrfs_check_data_free_space() we let delayed_ref to
8073 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008074 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008075 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008076 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008077 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008078 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008079 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8080 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008081 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008082
8083 __btrfs_releasepage(page, GFP_NOFS);
8084 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008085
Chris Mason4a096752008-07-21 10:29:44 -04008086 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008087 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008088 ClearPagePrivate(page);
8089 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008090 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008091 }
Chris Mason39279cc2007-06-12 06:35:45 -04008092}
8093
Chris Mason9ebefb182007-06-15 13:50:00 -04008094/*
8095 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8096 * called from a page fault handler when a page is first dirtied. Hence we must
8097 * be careful to check for EOF conditions here. We set the page up correctly
8098 * for a written page which means we get ENOSPC checking when writing into
8099 * holes and correct delalloc and unwritten extent mapping on filesystems that
8100 * support these features.
8101 *
8102 * We are not allowed to take the i_mutex here so we have to play games to
8103 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008104 * truncate_setsize() writes the inode size before removing pages, once we have
8105 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008106 * beyond EOF, then the page is guaranteed safe against truncation until we
8107 * unlock the page.
8108 */
Souptick Joardera528a242018-06-06 19:54:44 +05308109vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008110{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008111 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008112 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008113 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008114 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8115 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008116 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008117 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008118 char *kaddr;
8119 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008120 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308121 vm_fault_t ret;
8122 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008123 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308124 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008125 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008126 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308127 u64 end;
8128
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008129 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008130
Jan Karab2b5ef52012-06-12 16:20:45 +02008131 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008132 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008133 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308134 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008135
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308136 /*
8137 * Reserving delalloc space after obtaining the page lock can lead to
8138 * deadlock. For example, if a dirty page is locked by this function
8139 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8140 * dirty page write out, then the btrfs_writepage() function could
8141 * end up waiting indefinitely to get a lock on the page currently
8142 * being processed by btrfs_page_mkwrite() function.
8143 */
Souptick Joardera528a242018-06-06 19:54:44 +05308144 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308145 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308146 if (!ret2) {
8147 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008148 reserved = 1;
8149 }
Souptick Joardera528a242018-06-06 19:54:44 +05308150 if (ret2) {
8151 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008152 if (reserved)
8153 goto out;
8154 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008155 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008156
Nick Piggin56a76f82009-03-31 15:23:23 -07008157 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008158again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008159 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008160 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008161
Chris Mason9ebefb182007-06-15 13:50:00 -04008162 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008163 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008164 /* page got truncated out from underneath us */
8165 goto out_unlock;
8166 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008167 wait_on_page_writeback(page);
8168
David Sterbaff13db42015-12-03 14:30:40 +01008169 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008170 set_page_extent_mapped(page);
8171
Chris Masoneb84ae02008-07-17 13:53:27 -04008172 /*
8173 * we can't set the delalloc bits if there are pending ordered
8174 * extents. Drop our locks and wait for them to finish
8175 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008176 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8177 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008178 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008179 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008180 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008181 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008182 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008183 btrfs_put_ordered_extent(ordered);
8184 goto again;
8185 }
8186
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008187 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008188 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008189 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008190 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308191 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008192 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008193 page_start, PAGE_SIZE - reserved_space,
8194 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308195 }
8196 }
8197
Josef Bacikfbf19082009-10-01 17:10:23 -04008198 /*
Liu Bo54160342016-12-13 12:15:19 -08008199 * page_mkwrite gets called when the page is firstly dirtied after it's
8200 * faulted in, but write(2) could also dirty a page and set delalloc
8201 * bits, thus in this case for space account reason, we still need to
8202 * clear any delalloc bits within this page range since we have to
8203 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008204 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308205 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008206 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8207 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008208
Souptick Joardera528a242018-06-06 19:54:44 +05308209 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008210 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308211 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008212 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008213 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008214 ret = VM_FAULT_SIGBUS;
8215 goto out_unlock;
8216 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008217
8218 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008219 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008220 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008221 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008222 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008223
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008224 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008225 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008226 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008227 flush_dcache_page(page);
8228 kunmap(page);
8229 }
Chris Mason247e7432008-07-17 12:53:51 -04008230 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008231 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008232 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008233
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008234 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008235 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008236 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008237
David Sterbae43bbe52017-12-12 21:43:52 +01008238 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008239
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008240 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8241 sb_end_pagefault(inode->i_sb);
8242 extent_changeset_free(data_reserved);
8243 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008244
8245out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008246 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008247out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008248 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008249 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008250 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008251out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008252 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008253 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008254 return ret;
8255}
8256
Filipe Manana213e8c52018-02-06 20:40:31 +00008257static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008258{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008259 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008260 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008261 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008262 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008263 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008264 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008265 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008266
Filipe Manana213e8c52018-02-06 20:40:31 +00008267 if (!skip_writeback) {
8268 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8269 (u64)-1);
8270 if (ret)
8271 return ret;
8272 }
Chris Mason39279cc2007-06-12 06:35:45 -04008273
Josef Bacikfcb80c22011-05-03 10:40:22 -04008274 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008275 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8276 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008277 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008278 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008279 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008280 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008281 * be free'd up by the truncate operation, but also have some slack
8282 * space reserved in case it uses space during the truncate (thank you
8283 * very much snapshotting).
8284 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008285 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008286 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008287 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008288 * doesn't end up using space reserved for updating the inode. We also
8289 * need to be able to stop the transaction and start a new one, which
8290 * means we need to be able to update the inode several times, and we
8291 * have no idea of knowing how many times that will be, so we can't just
8292 * reserve 1 item for the entirety of the operation, so that has to be
8293 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008294 *
8295 * So that leaves us with
8296 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008297 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008298 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008299 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008300 * updating the inode.
8301 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008302 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008303 if (!rsv)
8304 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008305 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008306 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008307
Josef Bacik907cbce2011-08-08 13:46:15 -04008308 /*
Josef Bacik07127182011-08-19 10:29:59 -04008309 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008310 * 1 for updating the inode.
8311 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008312 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008313 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008314 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008315 goto out;
8316 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008317
Josef Bacik907cbce2011-08-08 13:46:15 -04008318 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008319 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008320 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008321 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008322
Chris Mason5a3f23d2009-03-31 13:27:11 -04008323 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008324 * So if we truncate and then write and fsync we normally would just
8325 * write the extents that changed, which is a problem if we need to
8326 * first truncate that entire inode. So set this flag so we write out
8327 * all of the extents in the inode to the sync log so we're completely
8328 * safe.
8329 */
8330 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008331 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008332
Yan, Zheng80825102009-11-12 09:35:36 +00008333 while (1) {
8334 ret = btrfs_truncate_inode_items(trans, root, inode,
8335 inode->i_size,
8336 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008337 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008338 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008339 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008340
Yan, Zheng80825102009-11-12 09:35:36 +00008341 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008342 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008343 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008344
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008345 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008346 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008347
8348 trans = btrfs_start_transaction(root, 2);
8349 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008350 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008351 trans = NULL;
8352 break;
8353 }
8354
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008355 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008356 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008357 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008358 BUG_ON(ret); /* shouldn't happen */
8359 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008360 }
8361
Josef Bacikddfae632017-10-19 14:16:02 -04008362 /*
8363 * We can't call btrfs_truncate_block inside a trans handle as we could
8364 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8365 * we've truncated everything except the last little bit, and can do
8366 * btrfs_truncate_block and then update the disk_i_size.
8367 */
8368 if (ret == NEED_TRUNCATE_BLOCK) {
8369 btrfs_end_transaction(trans);
8370 btrfs_btree_balance_dirty(fs_info);
8371
8372 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8373 if (ret)
8374 goto out;
8375 trans = btrfs_start_transaction(root, 1);
8376 if (IS_ERR(trans)) {
8377 ret = PTR_ERR(trans);
8378 goto out;
8379 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008380 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008381 }
8382
Chris Mason917c16b2011-11-08 14:49:59 -05008383 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008384 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008385
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008386 trans->block_rsv = &fs_info->trans_block_rsv;
8387 ret2 = btrfs_update_inode(trans, root, inode);
8388 if (ret2 && !ret)
8389 ret = ret2;
8390
8391 ret2 = btrfs_end_transaction(trans);
8392 if (ret2 && !ret)
8393 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008394 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008395 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008396out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008397 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008398
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008399 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008400}
8401
Sven Wegener3b963622008-06-09 21:57:42 -04008402/*
Chris Masond352ac62008-09-29 15:18:18 -04008403 * create a new subvolume directory/inode (helper for the ioctl).
8404 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008405int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008406 struct btrfs_root *new_root,
8407 struct btrfs_root *parent_root,
8408 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008409{
Chris Mason39279cc2007-06-12 06:35:45 -04008410 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008411 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008412 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008413
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008414 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8415 new_dirid, new_dirid,
8416 S_IFDIR | (~current_umask() & S_IRWXUGO),
8417 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008418 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008419 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008420 inode->i_op = &btrfs_dir_inode_operations;
8421 inode->i_fop = &btrfs_dir_file_operations;
8422
Miklos Szeredibfe86842011-10-28 14:13:29 +02008423 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008424 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008425 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008426
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008427 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8428 if (err)
8429 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008430 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008431 new_root->root_key.objectid, err);
8432
Yan, Zheng76dda932009-09-21 16:00:26 -04008433 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008434
Yan, Zheng76dda932009-09-21 16:00:26 -04008435 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008436 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008437}
8438
Chris Mason39279cc2007-06-12 06:35:45 -04008439struct inode *btrfs_alloc_inode(struct super_block *sb)
8440{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008441 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008442 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008443 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008444
David Sterba712e36c2017-10-31 17:08:27 +01008445 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008446 if (!ei)
8447 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008448
8449 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008450 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008451 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008452 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008453 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008454 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008455 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008456 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008457 ei->disk_i_size = 0;
8458 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008459 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008460 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008461 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008462 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008463 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008464
Josef Bacik9e0baf62011-07-15 15:16:44 +00008465 spin_lock_init(&ei->lock);
8466 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008467 if (sb->s_magic != BTRFS_TEST_MAGIC)
8468 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8469 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008470 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008471 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008472 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008473
Miao Xie16cdcec2011-04-22 18:12:22 +08008474 ei->delayed_node = NULL;
8475
chandan r9cc97d62012-07-04 12:48:07 +05308476 ei->i_otime.tv_sec = 0;
8477 ei->i_otime.tv_nsec = 0;
8478
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008479 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008480 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008481 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8482 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8483 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008484 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8485 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008486 ei->io_tree.track_uptodate = true;
8487 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008488 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008489 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008490 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008491 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008492 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008493 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008494 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008495
8496 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008497}
8498
Josef Bacikaaedb552013-10-11 14:44:09 -04008499#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8500void btrfs_test_destroy_inode(struct inode *inode)
8501{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008502 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008503 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8504}
8505#endif
8506
Al Viro26602ca2019-04-10 15:14:41 -04008507void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008508{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008509 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8510}
8511
Chris Mason39279cc2007-06-12 06:35:45 -04008512void btrfs_destroy_inode(struct inode *inode)
8513{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008514 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008515 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008516 struct btrfs_root *root = BTRFS_I(inode)->root;
8517
Al Virob3d9b7a2012-06-09 13:51:19 -04008518 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008519 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008520 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8521 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008522 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008523 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008524 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008525 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008526 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008527
Chris Mason5a3f23d2009-03-31 13:27:11 -04008528 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008529 * This can happen where we create an inode, but somebody else also
8530 * created the same inode and we need to destroy the one we already
8531 * created.
8532 */
8533 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008534 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008535
Chris Masond3977122009-01-05 21:25:51 -05008536 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008537 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8538 if (!ordered)
8539 break;
8540 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008541 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008542 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008543 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008544 btrfs_remove_ordered_extent(inode, ordered);
8545 btrfs_put_ordered_extent(ordered);
8546 btrfs_put_ordered_extent(ordered);
8547 }
8548 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008549 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008550 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008551 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008552 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008553 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008554}
8555
Al Viro45321ac2010-06-07 13:43:19 -04008556int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008557{
8558 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008559
Naohiro Aota6379ef92013-06-06 09:56:34 +00008560 if (root == NULL)
8561 return 1;
8562
Liu Bofa6ac872013-02-20 14:10:23 +00008563 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008564 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008565 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008566 else
Al Viro45321ac2010-06-07 13:43:19 -04008567 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008568}
8569
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008570static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008571{
8572 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8573
8574 inode_init_once(&ei->vfs_inode);
8575}
8576
David Sterbae67c7182018-02-19 17:24:18 +01008577void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008578{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008579 /*
8580 * Make sure all delayed rcu free inodes are flushed before we
8581 * destroy cache.
8582 */
8583 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008584 kmem_cache_destroy(btrfs_inode_cachep);
8585 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008586 kmem_cache_destroy(btrfs_path_cachep);
8587 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008588 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008589}
8590
Liu Bof5c29bd2017-11-02 17:21:50 -06008591int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008592{
David Sterba837e1972012-09-07 03:00:48 -06008593 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008594 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008595 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8596 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008597 if (!btrfs_inode_cachep)
8598 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008599
David Sterba837e1972012-09-07 03:00:48 -06008600 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008601 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008602 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008603 if (!btrfs_trans_handle_cachep)
8604 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008605
David Sterba837e1972012-09-07 03:00:48 -06008606 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008607 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008608 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008609 if (!btrfs_path_cachep)
8610 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008611
David Sterba837e1972012-09-07 03:00:48 -06008612 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008613 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008614 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008615 if (!btrfs_free_space_cachep)
8616 goto fail;
8617
Christophe Leroy3acd4852019-08-21 15:05:55 +00008618 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8619 PAGE_SIZE, PAGE_SIZE,
8620 SLAB_RED_ZONE, NULL);
8621 if (!btrfs_free_space_bitmap_cachep)
8622 goto fail;
8623
Chris Mason39279cc2007-06-12 06:35:45 -04008624 return 0;
8625fail:
8626 btrfs_destroy_cachep();
8627 return -ENOMEM;
8628}
8629
David Howellsa528d352017-01-31 16:46:22 +00008630static int btrfs_getattr(const struct path *path, struct kstat *stat,
8631 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008632{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008633 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008634 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008635 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008636 u32 bi_flags = BTRFS_I(inode)->flags;
8637
8638 stat->result_mask |= STATX_BTIME;
8639 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8640 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8641 if (bi_flags & BTRFS_INODE_APPEND)
8642 stat->attributes |= STATX_ATTR_APPEND;
8643 if (bi_flags & BTRFS_INODE_COMPRESS)
8644 stat->attributes |= STATX_ATTR_COMPRESSED;
8645 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8646 stat->attributes |= STATX_ATTR_IMMUTABLE;
8647 if (bi_flags & BTRFS_INODE_NODUMP)
8648 stat->attributes |= STATX_ATTR_NODUMP;
8649
8650 stat->attributes_mask |= (STATX_ATTR_APPEND |
8651 STATX_ATTR_COMPRESSED |
8652 STATX_ATTR_IMMUTABLE |
8653 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008654
Chris Mason39279cc2007-06-12 06:35:45 -04008655 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008656 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008657
8658 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008659 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008660 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008661 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008662 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008663 return 0;
8664}
8665
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008666static int btrfs_rename_exchange(struct inode *old_dir,
8667 struct dentry *old_dentry,
8668 struct inode *new_dir,
8669 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008670{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008671 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008672 struct btrfs_trans_handle *trans;
8673 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008674 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008675 struct inode *new_inode = new_dentry->d_inode;
8676 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008677 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008678 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008679 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8680 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008681 u64 old_idx = 0;
8682 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008683 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008684 bool root_log_pinned = false;
8685 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008686 struct btrfs_log_ctx ctx_root;
8687 struct btrfs_log_ctx ctx_dest;
8688 bool sync_log_root = false;
8689 bool sync_log_dest = false;
8690 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008691
8692 /* we only allow rename subvolume link between subvolumes */
8693 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8694 return -EXDEV;
8695
Filipe Mananad4682ba2018-06-11 19:24:28 +01008696 btrfs_init_log_ctx(&ctx_root, old_inode);
8697 btrfs_init_log_ctx(&ctx_dest, new_inode);
8698
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008699 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008700 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8701 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008702 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008703
8704 /*
8705 * We want to reserve the absolute worst case amount of items. So if
8706 * both inodes are subvols and we need to unlink them then that would
8707 * require 4 item modifications, but if they are both normal inodes it
8708 * would require 5 item modifications, so we'll assume their normal
8709 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8710 * should cover the worst case number of items we'll modify.
8711 */
8712 trans = btrfs_start_transaction(root, 12);
8713 if (IS_ERR(trans)) {
8714 ret = PTR_ERR(trans);
8715 goto out_notrans;
8716 }
8717
Josef Bacik3e174092019-11-15 15:43:06 -05008718 if (dest != root)
8719 btrfs_record_root_in_trans(trans, dest);
8720
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008721 /*
8722 * We need to find a free sequence number both in the source and
8723 * in the destination directory for the exchange.
8724 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008725 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008726 if (ret)
8727 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008728 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008729 if (ret)
8730 goto out_fail;
8731
8732 BTRFS_I(old_inode)->dir_index = 0ULL;
8733 BTRFS_I(new_inode)->dir_index = 0ULL;
8734
8735 /* Reference for the source. */
8736 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8737 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008738 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008739 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008740 btrfs_pin_log_trans(root);
8741 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008742 ret = btrfs_insert_inode_ref(trans, dest,
8743 new_dentry->d_name.name,
8744 new_dentry->d_name.len,
8745 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008746 btrfs_ino(BTRFS_I(new_dir)),
8747 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008748 if (ret)
8749 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008750 }
8751
8752 /* And now for the dest. */
8753 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8754 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008755 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008756 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008757 btrfs_pin_log_trans(dest);
8758 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008759 ret = btrfs_insert_inode_ref(trans, root,
8760 old_dentry->d_name.name,
8761 old_dentry->d_name.len,
8762 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008763 btrfs_ino(BTRFS_I(old_dir)),
8764 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008765 if (ret)
8766 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008767 }
8768
8769 /* Update inode version and ctime/mtime. */
8770 inode_inc_iversion(old_dir);
8771 inode_inc_iversion(new_dir);
8772 inode_inc_iversion(old_inode);
8773 inode_inc_iversion(new_inode);
8774 old_dir->i_ctime = old_dir->i_mtime = ctime;
8775 new_dir->i_ctime = new_dir->i_mtime = ctime;
8776 old_inode->i_ctime = ctime;
8777 new_inode->i_ctime = ctime;
8778
8779 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008780 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8781 BTRFS_I(old_inode), 1);
8782 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8783 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008784 }
8785
8786 /* src is a subvolume */
8787 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008788 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008789 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008790 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8791 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008792 old_dentry->d_name.name,
8793 old_dentry->d_name.len);
8794 if (!ret)
8795 ret = btrfs_update_inode(trans, root, old_inode);
8796 }
8797 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008798 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008799 goto out_fail;
8800 }
8801
8802 /* dest is a subvolume */
8803 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008804 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008805 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008806 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8807 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008808 new_dentry->d_name.name,
8809 new_dentry->d_name.len);
8810 if (!ret)
8811 ret = btrfs_update_inode(trans, dest, new_inode);
8812 }
8813 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008814 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008815 goto out_fail;
8816 }
8817
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008818 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008819 new_dentry->d_name.name,
8820 new_dentry->d_name.len, 0, old_idx);
8821 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008822 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008823 goto out_fail;
8824 }
8825
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008826 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008827 old_dentry->d_name.name,
8828 old_dentry->d_name.len, 0, new_idx);
8829 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008830 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008831 goto out_fail;
8832 }
8833
8834 if (old_inode->i_nlink == 1)
8835 BTRFS_I(old_inode)->dir_index = old_idx;
8836 if (new_inode->i_nlink == 1)
8837 BTRFS_I(new_inode)->dir_index = new_idx;
8838
Filipe Manana86e8aa02016-05-05 02:02:27 +01008839 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008840 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008841 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8842 BTRFS_I(old_dir), parent,
8843 false, &ctx_root);
8844 if (ret == BTRFS_NEED_LOG_SYNC)
8845 sync_log_root = true;
8846 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8847 commit_transaction = true;
8848 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008849 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008850 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008851 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008852 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008853 if (!commit_transaction) {
8854 parent = old_dentry->d_parent;
8855 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8856 BTRFS_I(new_dir), parent,
8857 false, &ctx_dest);
8858 if (ret == BTRFS_NEED_LOG_SYNC)
8859 sync_log_dest = true;
8860 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8861 commit_transaction = true;
8862 ret = 0;
8863 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008864 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008865 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008866 }
8867out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008868 /*
8869 * If we have pinned a log and an error happened, we unpin tasks
8870 * trying to sync the log and force them to fallback to a transaction
8871 * commit if the log currently contains any of the inodes involved in
8872 * this rename operation (to ensure we do not persist a log with an
8873 * inconsistent state for any of these inodes or leading to any
8874 * inconsistencies when replayed). If the transaction was aborted, the
8875 * abortion reason is propagated to userspace when attempting to commit
8876 * the transaction. If the log does not contain any of these inodes, we
8877 * allow the tasks to sync it.
8878 */
8879 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008880 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8881 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8882 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008883 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008884 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008885 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008886
8887 if (root_log_pinned) {
8888 btrfs_end_log_trans(root);
8889 root_log_pinned = false;
8890 }
8891 if (dest_log_pinned) {
8892 btrfs_end_log_trans(dest);
8893 dest_log_pinned = false;
8894 }
8895 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008896 if (!ret && sync_log_root && !commit_transaction) {
8897 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
8898 &ctx_root);
8899 if (ret)
8900 commit_transaction = true;
8901 }
8902 if (!ret && sync_log_dest && !commit_transaction) {
8903 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
8904 &ctx_dest);
8905 if (ret)
8906 commit_transaction = true;
8907 }
8908 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00008909 /*
8910 * We may have set commit_transaction when logging the new name
8911 * in the destination root, in which case we left the source
8912 * root context in the list of log contextes. So make sure we
8913 * remove it to avoid invalid memory accesses, since the context
8914 * was allocated in our stack frame.
8915 */
8916 if (sync_log_root) {
8917 mutex_lock(&root->log_mutex);
8918 list_del_init(&ctx_root.list);
8919 mutex_unlock(&root->log_mutex);
8920 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008921 ret = btrfs_commit_transaction(trans);
8922 } else {
8923 int ret2;
8924
8925 ret2 = btrfs_end_transaction(trans);
8926 ret = ret ? ret : ret2;
8927 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008928out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05008929 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
8930 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008931 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008932
Filipe Mananae6c61712019-11-08 16:11:56 +00008933 ASSERT(list_empty(&ctx_root.list));
8934 ASSERT(list_empty(&ctx_dest.list));
8935
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008936 return ret;
8937}
8938
8939static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
8940 struct btrfs_root *root,
8941 struct inode *dir,
8942 struct dentry *dentry)
8943{
8944 int ret;
8945 struct inode *inode;
8946 u64 objectid;
8947 u64 index;
8948
8949 ret = btrfs_find_free_ino(root, &objectid);
8950 if (ret)
8951 return ret;
8952
8953 inode = btrfs_new_inode(trans, root, dir,
8954 dentry->d_name.name,
8955 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008956 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008957 objectid,
8958 S_IFCHR | WHITEOUT_MODE,
8959 &index);
8960
8961 if (IS_ERR(inode)) {
8962 ret = PTR_ERR(inode);
8963 return ret;
8964 }
8965
8966 inode->i_op = &btrfs_special_inode_operations;
8967 init_special_inode(inode, inode->i_mode,
8968 WHITEOUT_DEV);
8969
8970 ret = btrfs_init_inode_security(trans, inode, dir,
8971 &dentry->d_name);
8972 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01008973 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008974
Nikolay Borisovcef415a2017-02-20 13:51:09 +02008975 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
8976 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008977 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01008978 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008979
8980 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01008981out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008982 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01008983 if (ret)
8984 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008985 iput(inode);
8986
Filipe Mananac9901612016-05-05 01:41:57 +01008987 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008988}
8989
Chris Mason39279cc2007-06-12 06:35:45 -04008990static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008991 struct inode *new_dir, struct dentry *new_dentry,
8992 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008993{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008994 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008995 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01008996 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04008997 struct btrfs_root *root = BTRFS_I(old_dir)->root;
8998 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00008999 struct inode *new_inode = d_inode(new_dentry);
9000 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009001 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009002 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009003 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009004 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009005 struct btrfs_log_ctx ctx;
9006 bool sync_log = false;
9007 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009008
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009009 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009010 return -EPERM;
9011
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009012 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009013 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009014 return -EXDEV;
9015
Li Zefan33345d012011-04-20 10:31:50 +08009016 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009017 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009018 return -ENOTEMPTY;
9019
Chris Mason39279cc2007-06-12 06:35:45 -04009020 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009021 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009022 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009023
9024
9025 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009026 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009027 new_dentry->d_name.name,
9028 new_dentry->d_name.len);
9029
9030 if (ret) {
9031 if (ret == -EEXIST) {
9032 /* we shouldn't get
9033 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309034 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009035 return ret;
9036 }
9037 } else {
9038 /* maybe -EOVERFLOW */
9039 return ret;
9040 }
9041 }
9042 ret = 0;
9043
Chris Mason5a3f23d2009-03-31 13:27:11 -04009044 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009045 * we're using rename to replace one file with another. Start IO on it
9046 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009047 */
Chris Mason8d875f92014-08-12 10:47:42 -07009048 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009049 filemap_flush(old_inode->i_mapping);
9050
Yan, Zheng76dda932009-09-21 16:00:26 -04009051 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009052 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009053 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009054 /*
9055 * We want to reserve the absolute worst case amount of items. So if
9056 * both inodes are subvols and we need to unlink them then that would
9057 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009058 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009059 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9060 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009061 * If our rename has the whiteout flag, we need more 5 units for the
9062 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9063 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009064 */
Filipe Manana5062af32016-05-05 10:26:26 +01009065 trans_num_items = 11;
9066 if (flags & RENAME_WHITEOUT)
9067 trans_num_items += 5;
9068 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009069 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009070 ret = PTR_ERR(trans);
9071 goto out_notrans;
9072 }
Chris Mason5f39d392007-10-15 16:14:19 -04009073
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009074 if (dest != root)
9075 btrfs_record_root_in_trans(trans, dest);
9076
Nikolay Borisov877574e2017-02-20 13:50:33 +02009077 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009078 if (ret)
9079 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009080
Miao Xie67de1172013-12-26 13:07:06 +08009081 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009082 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009083 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009084 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009085 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009086 btrfs_pin_log_trans(root);
9087 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009088 ret = btrfs_insert_inode_ref(trans, dest,
9089 new_dentry->d_name.name,
9090 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009091 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009092 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009093 if (ret)
9094 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009095 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009096
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009097 inode_inc_iversion(old_dir);
9098 inode_inc_iversion(new_dir);
9099 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009100 old_dir->i_ctime = old_dir->i_mtime =
9101 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009102 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009103
Chris Mason12fcfd22009-03-24 10:24:20 -04009104 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009105 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9106 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009107
Li Zefan33345d012011-04-20 10:31:50 +08009108 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009109 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009110 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009111 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9112 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009113 old_dentry->d_name.name,
9114 old_dentry->d_name.len);
9115 if (!ret)
9116 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009117 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009118 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009119 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009120 goto out_fail;
9121 }
Chris Mason39279cc2007-06-12 06:35:45 -04009122
9123 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009124 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009125 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009126 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009127 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009128 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009129 BUG_ON(new_inode->i_nlink == 0);
9130 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009131 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9132 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009133 new_dentry->d_name.name,
9134 new_dentry->d_name.len);
9135 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009136 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009137 ret = btrfs_orphan_add(trans,
9138 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009139 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009140 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009141 goto out_fail;
9142 }
Chris Mason39279cc2007-06-12 06:35:45 -04009143 }
Josef Bacikaec74772008-07-24 12:12:38 -04009144
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009145 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009146 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009147 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009148 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009149 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009150 goto out_fail;
9151 }
Chris Mason39279cc2007-06-12 06:35:45 -04009152
Miao Xie67de1172013-12-26 13:07:06 +08009153 if (old_inode->i_nlink == 1)
9154 BTRFS_I(old_inode)->dir_index = index;
9155
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009156 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009157 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009158
Filipe Mananad4682ba2018-06-11 19:24:28 +01009159 btrfs_init_log_ctx(&ctx, old_inode);
9160 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9161 BTRFS_I(old_dir), parent,
9162 false, &ctx);
9163 if (ret == BTRFS_NEED_LOG_SYNC)
9164 sync_log = true;
9165 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9166 commit_transaction = true;
9167 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009168 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009169 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009170 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009171
9172 if (flags & RENAME_WHITEOUT) {
9173 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9174 old_dentry);
9175
9176 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009177 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009178 goto out_fail;
9179 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009180 }
Chris Mason39279cc2007-06-12 06:35:45 -04009181out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009182 /*
9183 * If we have pinned the log and an error happened, we unpin tasks
9184 * trying to sync the log and force them to fallback to a transaction
9185 * commit if the log currently contains any of the inodes involved in
9186 * this rename operation (to ensure we do not persist a log with an
9187 * inconsistent state for any of these inodes or leading to any
9188 * inconsistencies when replayed). If the transaction was aborted, the
9189 * abortion reason is propagated to userspace when attempting to commit
9190 * the transaction. If the log does not contain any of these inodes, we
9191 * allow the tasks to sync it.
9192 */
9193 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009194 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9195 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9196 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009197 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009198 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009199 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009200
9201 btrfs_end_log_trans(root);
9202 log_pinned = false;
9203 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009204 if (!ret && sync_log) {
9205 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9206 if (ret)
9207 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009208 } else if (sync_log) {
9209 mutex_lock(&root->log_mutex);
9210 list_del(&ctx.list);
9211 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009212 }
9213 if (commit_transaction) {
9214 ret = btrfs_commit_transaction(trans);
9215 } else {
9216 int ret2;
9217
9218 ret2 = btrfs_end_transaction(trans);
9219 ret = ret ? ret : ret2;
9220 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009221out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009222 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009223 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009224
Chris Mason39279cc2007-06-12 06:35:45 -04009225 return ret;
9226}
9227
Miklos Szeredi80ace852014-07-23 15:15:32 +02009228static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9229 struct inode *new_dir, struct dentry *new_dentry,
9230 unsigned int flags)
9231{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009232 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009233 return -EINVAL;
9234
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009235 if (flags & RENAME_EXCHANGE)
9236 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9237 new_dentry);
9238
9239 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009240}
9241
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009242struct btrfs_delalloc_work {
9243 struct inode *inode;
9244 struct completion completion;
9245 struct list_head list;
9246 struct btrfs_work work;
9247};
9248
Miao Xie8ccf6f192012-10-25 09:28:04 +00009249static void btrfs_run_delalloc_work(struct btrfs_work *work)
9250{
9251 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009252 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009253
9254 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9255 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009256 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009257 filemap_flush(inode->i_mapping);
9258 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9259 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009260 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009261
Nikolay Borisov076da912018-04-23 10:54:16 +03009262 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009263 complete(&delalloc_work->completion);
9264}
9265
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009266static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009267{
9268 struct btrfs_delalloc_work *work;
9269
David Sterba100d5702015-12-08 14:39:32 +01009270 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009271 if (!work)
9272 return NULL;
9273
9274 init_completion(&work->completion);
9275 INIT_LIST_HEAD(&work->list);
9276 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009277 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009278
9279 return work;
9280}
9281
Chris Masond352ac62008-09-29 15:18:18 -04009282/*
9283 * some fairly slow code that needs optimization. This walks the list
9284 * of all the inodes with pending delalloc and forces them to disk.
9285 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009286static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009287{
Chris Masonea8c2812008-08-04 23:17:27 -04009288 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009289 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009290 struct btrfs_delalloc_work *work, *next;
9291 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009292 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009293 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009294
Miao Xie8ccf6f192012-10-25 09:28:04 +00009295 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009296 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009297
Miao Xie573bfb72014-03-06 13:55:03 +08009298 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009299 spin_lock(&root->delalloc_lock);
9300 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009301 while (!list_empty(&splice)) {
9302 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009303 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009304
Miao Xieeb73c1b2013-05-15 07:48:22 +00009305 list_move_tail(&binode->delalloc_inodes,
9306 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009307 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009308 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009309 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009310 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009311 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009312 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009313
Ethan Lien3cd24c62018-11-01 14:49:03 +08009314 if (snapshot)
9315 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9316 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009317 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009318 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009319 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009320 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009321 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009322 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009323 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009324 btrfs_queue_work(root->fs_info->flush_workers,
9325 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009326 ret++;
9327 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009328 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009329 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009330 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009331 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009332 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009333
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009334out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009335 list_for_each_entry_safe(work, next, &works, list) {
9336 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009337 wait_for_completion(&work->completion);
9338 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009339 }
9340
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009341 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009342 spin_lock(&root->delalloc_lock);
9343 list_splice_tail(&splice, &root->delalloc_inodes);
9344 spin_unlock(&root->delalloc_lock);
9345 }
Miao Xie573bfb72014-03-06 13:55:03 +08009346 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009347 return ret;
9348}
9349
Ethan Lien3cd24c62018-11-01 14:49:03 +08009350int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009351{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009352 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009353 int ret;
9354
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009355 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009356 return -EROFS;
9357
Ethan Lien3cd24c62018-11-01 14:49:03 +08009358 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009359 if (ret > 0)
9360 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009361 return ret;
9362}
9363
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009364int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009365{
9366 struct btrfs_root *root;
9367 struct list_head splice;
9368 int ret;
9369
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009370 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009371 return -EROFS;
9372
9373 INIT_LIST_HEAD(&splice);
9374
Miao Xie573bfb72014-03-06 13:55:03 +08009375 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009376 spin_lock(&fs_info->delalloc_root_lock);
9377 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009378 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009379 root = list_first_entry(&splice, struct btrfs_root,
9380 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009381 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009382 BUG_ON(!root);
9383 list_move_tail(&root->delalloc_root,
9384 &fs_info->delalloc_roots);
9385 spin_unlock(&fs_info->delalloc_root_lock);
9386
Ethan Lien3cd24c62018-11-01 14:49:03 +08009387 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009388 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009389 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009390 goto out;
9391
Miao Xie6c255e62014-03-06 13:55:01 +08009392 if (nr != -1) {
9393 nr -= ret;
9394 WARN_ON(nr < 0);
9395 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009396 spin_lock(&fs_info->delalloc_root_lock);
9397 }
9398 spin_unlock(&fs_info->delalloc_root_lock);
9399
Miao Xie6c255e62014-03-06 13:55:01 +08009400 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009401out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009402 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009403 spin_lock(&fs_info->delalloc_root_lock);
9404 list_splice_tail(&splice, &fs_info->delalloc_roots);
9405 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009406 }
Miao Xie573bfb72014-03-06 13:55:03 +08009407 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009408 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009409}
9410
Chris Mason39279cc2007-06-12 06:35:45 -04009411static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9412 const char *symname)
9413{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009414 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009415 struct btrfs_trans_handle *trans;
9416 struct btrfs_root *root = BTRFS_I(dir)->root;
9417 struct btrfs_path *path;
9418 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009419 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009420 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009421 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309422 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009423 int name_len;
9424 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009425 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009426 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009427 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009428
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009429 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009430 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009431 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009432
Josef Bacik9ed74f22009-09-11 16:12:44 -04009433 /*
9434 * 2 items for inode item and ref
9435 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009436 * 1 item for updating parent inode item
9437 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009438 * 1 item for xattr if selinux is on
9439 */
Filipe Manana9269d122015-12-31 18:16:29 +00009440 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009441 if (IS_ERR(trans))
9442 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009443
Li Zefan581bb052011-04-20 10:06:11 +08009444 err = btrfs_find_free_ino(root, &objectid);
9445 if (err)
9446 goto out_unlock;
9447
Josef Bacikaec74772008-07-24 12:12:38 -04009448 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009449 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9450 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009451 if (IS_ERR(inode)) {
9452 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009453 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009454 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009455 }
Chris Mason39279cc2007-06-12 06:35:45 -04009456
Casey Schauflerad19db72011-12-15 10:09:07 -05009457 /*
9458 * If the active LSM wants to access the inode during
9459 * d_instantiate it needs these. Smack checks to see
9460 * if the filesystem supports xattrs by looking at the
9461 * ops vector.
9462 */
9463 inode->i_fop = &btrfs_file_operations;
9464 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009465 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009466 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9467
9468 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9469 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009470 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009471
Chris Mason39279cc2007-06-12 06:35:45 -04009472 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009473 if (!path) {
9474 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009475 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009476 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009477 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009478 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009479 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009480 datasize = btrfs_file_extent_calc_inline_size(name_len);
9481 err = btrfs_insert_empty_item(trans, root, path, &key,
9482 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009483 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009484 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009485 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009486 }
Chris Mason5f39d392007-10-15 16:14:19 -04009487 leaf = path->nodes[0];
9488 ei = btrfs_item_ptr(leaf, path->slots[0],
9489 struct btrfs_file_extent_item);
9490 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9491 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009492 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009493 btrfs_set_file_extent_encryption(leaf, ei, 0);
9494 btrfs_set_file_extent_compression(leaf, ei, 0);
9495 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9496 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9497
Chris Mason39279cc2007-06-12 06:35:45 -04009498 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009499 write_extent_buffer(leaf, symname, ptr, name_len);
9500 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009501 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009502
Chris Mason39279cc2007-06-12 06:35:45 -04009503 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009504 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009505 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009506 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009507 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009508 /*
9509 * Last step, add directory indexes for our symlink inode. This is the
9510 * last step to avoid extra cleanup of these indexes if an error happens
9511 * elsewhere above.
9512 */
9513 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009514 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9515 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009516 if (err)
9517 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009518
Al Viro1e2e5472018-05-04 08:23:01 -04009519 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009520
9521out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009522 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009523 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009524 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009525 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009526 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009527 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009528 return err;
9529}
Chris Mason16432982008-04-10 10:23:21 -04009530
Josef Bacik0af3d002010-06-21 14:48:16 -04009531static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9532 u64 start, u64 num_bytes, u64 min_size,
9533 loff_t actual_len, u64 *alloc_hint,
9534 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009535{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009536 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009537 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9538 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009539 struct btrfs_root *root = BTRFS_I(inode)->root;
9540 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009541 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009542 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009543 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009544 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009545 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009546 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009547 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009548 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009549
Josef Bacik0af3d002010-06-21 14:48:16 -04009550 if (trans)
9551 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009552 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009553 if (own_trans) {
9554 trans = btrfs_start_transaction(root, 3);
9555 if (IS_ERR(trans)) {
9556 ret = PTR_ERR(trans);
9557 break;
9558 }
Yan Zhengd899e052008-10-30 14:25:28 -04009559 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009560
Byongho Leeee221842015-12-15 01:42:10 +09009561 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009562 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009563 /*
9564 * If we are severely fragmented we could end up with really
9565 * small allocations, so if the allocator is returning small
9566 * chunks lets make its job easier by only searching for those
9567 * sized chunks.
9568 */
9569 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009570 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9571 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009572 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009573 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009574 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009575 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009576 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009577
9578 /*
9579 * We've reserved this space, and thus converted it from
9580 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9581 * from here on out we will only need to clear our reservation
9582 * for the remaining unreserved area, so advance our
9583 * clear_offset by our extent size.
9584 */
9585 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009586 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009587
Josef Bacik0b670dc2015-09-23 17:11:16 -04009588 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04009589 ret = insert_reserved_file_extent(trans, inode,
9590 cur_offset, ins.objectid,
9591 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009592 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009593 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009594 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009595 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009596 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009597 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009598 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009599 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009600 break;
9601 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009602
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009603 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009604 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009605
Josef Bacik5dc562c2012-08-17 13:14:17 -04009606 em = alloc_extent_map();
9607 if (!em) {
9608 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9609 &BTRFS_I(inode)->runtime_flags);
9610 goto next;
9611 }
9612
9613 em->start = cur_offset;
9614 em->orig_start = cur_offset;
9615 em->len = ins.offset;
9616 em->block_start = ins.objectid;
9617 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009618 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009619 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009620 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9621 em->generation = trans->transid;
9622
9623 while (1) {
9624 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009625 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009626 write_unlock(&em_tree->lock);
9627 if (ret != -EEXIST)
9628 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009629 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009630 cur_offset + ins.offset - 1,
9631 0);
9632 }
9633 free_extent_map(em);
9634next:
Yan Zhengd899e052008-10-30 14:25:28 -04009635 num_bytes -= ins.offset;
9636 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009637 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009638
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009639 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009640 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009641 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009642 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009643 (actual_len > inode->i_size) &&
9644 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009645 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009646 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009647 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009648 i_size = cur_offset;
9649 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009650 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009651 }
9652
Yan Zhengd899e052008-10-30 14:25:28 -04009653 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009654
9655 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009656 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009657 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009658 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009659 break;
9660 }
Yan Zhengd899e052008-10-30 14:25:28 -04009661
Josef Bacik0af3d002010-06-21 14:48:16 -04009662 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009663 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009664 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009665 if (clear_offset < end)
9666 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
9667 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009668 return ret;
9669}
9670
Josef Bacik0af3d002010-06-21 14:48:16 -04009671int btrfs_prealloc_file_range(struct inode *inode, int mode,
9672 u64 start, u64 num_bytes, u64 min_size,
9673 loff_t actual_len, u64 *alloc_hint)
9674{
9675 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9676 min_size, actual_len, alloc_hint,
9677 NULL);
9678}
9679
9680int btrfs_prealloc_file_range_trans(struct inode *inode,
9681 struct btrfs_trans_handle *trans, int mode,
9682 u64 start, u64 num_bytes, u64 min_size,
9683 loff_t actual_len, u64 *alloc_hint)
9684{
9685 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9686 min_size, actual_len, alloc_hint, trans);
9687}
9688
Chris Masone6dcd2d2008-07-17 12:53:50 -04009689static int btrfs_set_page_dirty(struct page *page)
9690{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009691 return __set_page_dirty_nobuffers(page);
9692}
9693
Al Viro10556cb22011-06-20 19:28:19 -04009694static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009695{
Li Zefanb83cc962010-12-20 16:04:08 +08009696 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009697 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009698
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009699 if (mask & MAY_WRITE &&
9700 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9701 if (btrfs_root_readonly(root))
9702 return -EROFS;
9703 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9704 return -EACCES;
9705 }
Al Viro2830ba72011-06-20 19:16:29 -04009706 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009707}
Chris Mason39279cc2007-06-12 06:35:45 -04009708
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009709static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9710{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009711 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009712 struct btrfs_trans_handle *trans;
9713 struct btrfs_root *root = BTRFS_I(dir)->root;
9714 struct inode *inode = NULL;
9715 u64 objectid;
9716 u64 index;
9717 int ret = 0;
9718
9719 /*
9720 * 5 units required for adding orphan entry
9721 */
9722 trans = btrfs_start_transaction(root, 5);
9723 if (IS_ERR(trans))
9724 return PTR_ERR(trans);
9725
9726 ret = btrfs_find_free_ino(root, &objectid);
9727 if (ret)
9728 goto out;
9729
9730 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009731 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009732 if (IS_ERR(inode)) {
9733 ret = PTR_ERR(inode);
9734 inode = NULL;
9735 goto out;
9736 }
9737
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009738 inode->i_fop = &btrfs_file_operations;
9739 inode->i_op = &btrfs_file_inode_operations;
9740
9741 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009742 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9743
Chris Masonb0d5d102014-09-08 13:08:51 -07009744 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9745 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009746 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009747
9748 ret = btrfs_update_inode(trans, root, inode);
9749 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009750 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009751 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009752 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009753 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009754
Filipe Manana5762b5c2014-08-01 00:10:32 +01009755 /*
9756 * We set number of links to 0 in btrfs_new_inode(), and here we set
9757 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9758 * through:
9759 *
9760 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9761 */
9762 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009763 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009764 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009765 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009766out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009767 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009768 if (ret && inode)
9769 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009770 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009771 return ret;
9772}
9773
David Sterba5cdc84b2018-07-18 20:32:52 +02009774void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009775{
David Sterba5cdc84b2018-07-18 20:32:52 +02009776 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009777 unsigned long index = start >> PAGE_SHIFT;
9778 unsigned long end_index = end >> PAGE_SHIFT;
9779 struct page *page;
9780
9781 while (index <= end_index) {
9782 page = find_get_page(inode->i_mapping, index);
9783 ASSERT(page); /* Pages should be in the extent_io_tree */
9784 set_page_writeback(page);
9785 put_page(page);
9786 index++;
9787 }
9788}
9789
Omar Sandovaled46ff32016-11-03 10:28:14 -07009790#ifdef CONFIG_SWAP
9791/*
9792 * Add an entry indicating a block group or device which is pinned by a
9793 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9794 * negative errno on failure.
9795 */
9796static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9797 bool is_block_group)
9798{
9799 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9800 struct btrfs_swapfile_pin *sp, *entry;
9801 struct rb_node **p;
9802 struct rb_node *parent = NULL;
9803
9804 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9805 if (!sp)
9806 return -ENOMEM;
9807 sp->ptr = ptr;
9808 sp->inode = inode;
9809 sp->is_block_group = is_block_group;
9810
9811 spin_lock(&fs_info->swapfile_pins_lock);
9812 p = &fs_info->swapfile_pins.rb_node;
9813 while (*p) {
9814 parent = *p;
9815 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9816 if (sp->ptr < entry->ptr ||
9817 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9818 p = &(*p)->rb_left;
9819 } else if (sp->ptr > entry->ptr ||
9820 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9821 p = &(*p)->rb_right;
9822 } else {
9823 spin_unlock(&fs_info->swapfile_pins_lock);
9824 kfree(sp);
9825 return 1;
9826 }
9827 }
9828 rb_link_node(&sp->node, parent, p);
9829 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9830 spin_unlock(&fs_info->swapfile_pins_lock);
9831 return 0;
9832}
9833
9834/* Free all of the entries pinned by this swapfile. */
9835static void btrfs_free_swapfile_pins(struct inode *inode)
9836{
9837 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9838 struct btrfs_swapfile_pin *sp;
9839 struct rb_node *node, *next;
9840
9841 spin_lock(&fs_info->swapfile_pins_lock);
9842 node = rb_first(&fs_info->swapfile_pins);
9843 while (node) {
9844 next = rb_next(node);
9845 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9846 if (sp->inode == inode) {
9847 rb_erase(&sp->node, &fs_info->swapfile_pins);
9848 if (sp->is_block_group)
9849 btrfs_put_block_group(sp->ptr);
9850 kfree(sp);
9851 }
9852 node = next;
9853 }
9854 spin_unlock(&fs_info->swapfile_pins_lock);
9855}
9856
9857struct btrfs_swap_info {
9858 u64 start;
9859 u64 block_start;
9860 u64 block_len;
9861 u64 lowest_ppage;
9862 u64 highest_ppage;
9863 unsigned long nr_pages;
9864 int nr_extents;
9865};
9866
9867static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9868 struct btrfs_swap_info *bsi)
9869{
9870 unsigned long nr_pages;
9871 u64 first_ppage, first_ppage_reported, next_ppage;
9872 int ret;
9873
9874 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9875 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9876 PAGE_SIZE) >> PAGE_SHIFT;
9877
9878 if (first_ppage >= next_ppage)
9879 return 0;
9880 nr_pages = next_ppage - first_ppage;
9881
9882 first_ppage_reported = first_ppage;
9883 if (bsi->start == 0)
9884 first_ppage_reported++;
9885 if (bsi->lowest_ppage > first_ppage_reported)
9886 bsi->lowest_ppage = first_ppage_reported;
9887 if (bsi->highest_ppage < (next_ppage - 1))
9888 bsi->highest_ppage = next_ppage - 1;
9889
9890 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
9891 if (ret < 0)
9892 return ret;
9893 bsi->nr_extents += ret;
9894 bsi->nr_pages += nr_pages;
9895 return 0;
9896}
9897
9898static void btrfs_swap_deactivate(struct file *file)
9899{
9900 struct inode *inode = file_inode(file);
9901
9902 btrfs_free_swapfile_pins(inode);
9903 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
9904}
9905
9906static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
9907 sector_t *span)
9908{
9909 struct inode *inode = file_inode(file);
9910 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9911 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9912 struct extent_state *cached_state = NULL;
9913 struct extent_map *em = NULL;
9914 struct btrfs_device *device = NULL;
9915 struct btrfs_swap_info bsi = {
9916 .lowest_ppage = (sector_t)-1ULL,
9917 };
9918 int ret = 0;
9919 u64 isize;
9920 u64 start;
9921
9922 /*
9923 * If the swap file was just created, make sure delalloc is done. If the
9924 * file changes again after this, the user is doing something stupid and
9925 * we don't really care.
9926 */
9927 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
9928 if (ret)
9929 return ret;
9930
9931 /*
9932 * The inode is locked, so these flags won't change after we check them.
9933 */
9934 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
9935 btrfs_warn(fs_info, "swapfile must not be compressed");
9936 return -EINVAL;
9937 }
9938 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
9939 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
9940 return -EINVAL;
9941 }
9942 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
9943 btrfs_warn(fs_info, "swapfile must not be checksummed");
9944 return -EINVAL;
9945 }
9946
9947 /*
9948 * Balance or device remove/replace/resize can move stuff around from
9949 * under us. The EXCL_OP flag makes sure they aren't running/won't run
9950 * concurrently while we are mapping the swap extents, and
9951 * fs_info->swapfile_pins prevents them from running while the swap file
9952 * is active and moving the extents. Note that this also prevents a
9953 * concurrent device add which isn't actually necessary, but it's not
9954 * really worth the trouble to allow it.
9955 */
9956 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
9957 btrfs_warn(fs_info,
9958 "cannot activate swapfile while exclusive operation is running");
9959 return -EBUSY;
9960 }
9961 /*
9962 * Snapshots can create extents which require COW even if NODATACOW is
9963 * set. We use this counter to prevent snapshots. We must increment it
9964 * before walking the extents because we don't want a concurrent
9965 * snapshot to run after we've already checked the extents.
9966 */
9967 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
9968
9969 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
9970
9971 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
9972 start = 0;
9973 while (start < isize) {
9974 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +01009975 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -07009976 u64 len = isize - start;
9977
Omar Sandoval39b07b52019-12-02 17:34:23 -08009978 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -07009979 if (IS_ERR(em)) {
9980 ret = PTR_ERR(em);
9981 goto out;
9982 }
9983
9984 if (em->block_start == EXTENT_MAP_HOLE) {
9985 btrfs_warn(fs_info, "swapfile must not have holes");
9986 ret = -EINVAL;
9987 goto out;
9988 }
9989 if (em->block_start == EXTENT_MAP_INLINE) {
9990 /*
9991 * It's unlikely we'll ever actually find ourselves
9992 * here, as a file small enough to fit inline won't be
9993 * big enough to store more than the swap header, but in
9994 * case something changes in the future, let's catch it
9995 * here rather than later.
9996 */
9997 btrfs_warn(fs_info, "swapfile must not be inline");
9998 ret = -EINVAL;
9999 goto out;
10000 }
10001 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10002 btrfs_warn(fs_info, "swapfile must not be compressed");
10003 ret = -EINVAL;
10004 goto out;
10005 }
10006
10007 logical_block_start = em->block_start + (start - em->start);
10008 len = min(len, em->len - (start - em->start));
10009 free_extent_map(em);
10010 em = NULL;
10011
10012 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10013 if (ret < 0) {
10014 goto out;
10015 } else if (ret) {
10016 ret = 0;
10017 } else {
10018 btrfs_warn(fs_info,
10019 "swapfile must not be copy-on-write");
10020 ret = -EINVAL;
10021 goto out;
10022 }
10023
10024 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10025 if (IS_ERR(em)) {
10026 ret = PTR_ERR(em);
10027 goto out;
10028 }
10029
10030 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10031 btrfs_warn(fs_info,
10032 "swapfile must have single data profile");
10033 ret = -EINVAL;
10034 goto out;
10035 }
10036
10037 if (device == NULL) {
10038 device = em->map_lookup->stripes[0].dev;
10039 ret = btrfs_add_swapfile_pin(inode, device, false);
10040 if (ret == 1)
10041 ret = 0;
10042 else if (ret)
10043 goto out;
10044 } else if (device != em->map_lookup->stripes[0].dev) {
10045 btrfs_warn(fs_info, "swapfile must be on one device");
10046 ret = -EINVAL;
10047 goto out;
10048 }
10049
10050 physical_block_start = (em->map_lookup->stripes[0].physical +
10051 (logical_block_start - em->start));
10052 len = min(len, em->len - (logical_block_start - em->start));
10053 free_extent_map(em);
10054 em = NULL;
10055
10056 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10057 if (!bg) {
10058 btrfs_warn(fs_info,
10059 "could not find block group containing swapfile");
10060 ret = -EINVAL;
10061 goto out;
10062 }
10063
10064 ret = btrfs_add_swapfile_pin(inode, bg, true);
10065 if (ret) {
10066 btrfs_put_block_group(bg);
10067 if (ret == 1)
10068 ret = 0;
10069 else
10070 goto out;
10071 }
10072
10073 if (bsi.block_len &&
10074 bsi.block_start + bsi.block_len == physical_block_start) {
10075 bsi.block_len += len;
10076 } else {
10077 if (bsi.block_len) {
10078 ret = btrfs_add_swap_extent(sis, &bsi);
10079 if (ret)
10080 goto out;
10081 }
10082 bsi.start = start;
10083 bsi.block_start = physical_block_start;
10084 bsi.block_len = len;
10085 }
10086
10087 start += len;
10088 }
10089
10090 if (bsi.block_len)
10091 ret = btrfs_add_swap_extent(sis, &bsi);
10092
10093out:
10094 if (!IS_ERR_OR_NULL(em))
10095 free_extent_map(em);
10096
10097 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10098
10099 if (ret)
10100 btrfs_swap_deactivate(file);
10101
10102 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10103
10104 if (ret)
10105 return ret;
10106
10107 if (device)
10108 sis->bdev = device->bdev;
10109 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10110 sis->max = bsi.nr_pages;
10111 sis->pages = bsi.nr_pages - 1;
10112 sis->highest_bit = bsi.nr_pages - 1;
10113 return bsi.nr_extents;
10114}
10115#else
10116static void btrfs_swap_deactivate(struct file *file)
10117{
10118}
10119
10120static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10121 sector_t *span)
10122{
10123 return -EOPNOTSUPP;
10124}
10125#endif
10126
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010127static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010128 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010129 .lookup = btrfs_lookup,
10130 .create = btrfs_create,
10131 .unlink = btrfs_unlink,
10132 .link = btrfs_link,
10133 .mkdir = btrfs_mkdir,
10134 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010135 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010136 .symlink = btrfs_symlink,
10137 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010138 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010139 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010140 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010141 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010142 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010143 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010144 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010145};
Yan, Zheng76dda932009-09-21 16:00:26 -040010146
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010147static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010148 .llseek = generic_file_llseek,
10149 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010150 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010151 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010152 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010153#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010154 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010155#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010156 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010157 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010158};
10159
David Sterba20e55062015-11-19 11:42:28 +010010160static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010161 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010162 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010163 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10164};
10165
Chris Mason35054392009-01-21 13:11:13 -050010166/*
10167 * btrfs doesn't support the bmap operation because swapfiles
10168 * use bmap to make a mapping of extents in the file. They assume
10169 * these extents won't change over the life of the file and they
10170 * use the bmap result to do IO directly to the drive.
10171 *
10172 * the btrfs bmap call would return logical addresses that aren't
10173 * suitable for IO and they also will change frequently as COW
10174 * operations happen. So, swapfile + btrfs == corruption.
10175 *
10176 * For now we're avoiding this by dropping bmap.
10177 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010178static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010179 .readpage = btrfs_readpage,
10180 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010181 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010182 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010183 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010184 .invalidatepage = btrfs_invalidatepage,
10185 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010186#ifdef CONFIG_MIGRATION
10187 .migratepage = btrfs_migratepage,
10188#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010189 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010190 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010191 .swap_activate = btrfs_swap_activate,
10192 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010193};
10194
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010195static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010196 .getattr = btrfs_getattr,
10197 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010198 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010199 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010200 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010201 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010202 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010203 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010204};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010205static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010206 .getattr = btrfs_getattr,
10207 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010208 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010209 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010210 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010211 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010212 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010213};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010214static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010215 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010216 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010217 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010218 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010219 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010220 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010221};
Yan, Zheng76dda932009-09-21 16:00:26 -040010222
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010223const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010224 .d_delete = btrfs_dentry_delete,
10225};