blob: 2865de52dc22aac3e6be8ef74db39333cf924f2b [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>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030031#include <linux/sched/mm.h>
David Sterba92d32172018-04-16 21:10:14 +020032#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020033#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040034#include "ctree.h"
35#include "disk-io.h"
36#include "transaction.h"
37#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040038#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040039#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040040#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040041#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020042#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040043#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050044#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050045#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080046#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000047#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080048#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040049#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040050#include "block-group.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051
52struct btrfs_iget_args {
Chris Mason90d3e592014-01-09 17:28:00 -080053 struct btrfs_key *location;
Chris Mason39279cc2007-06-12 06:35:45 -040054 struct btrfs_root *root;
55};
56
Filipe Mananaf28a4922015-12-08 19:23:20 +000057struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000058 u64 reserve;
59 u64 unsubmitted_oe_range_start;
60 u64 unsubmitted_oe_range_end;
Liu Bo4aaedfb2016-12-14 22:36:05 -080061 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000062};
63
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070064static const struct inode_operations btrfs_dir_inode_operations;
65static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070066static const struct inode_operations btrfs_special_inode_operations;
67static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070068static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070069static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010070static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040071
72static struct kmem_cache *btrfs_inode_cachep;
73struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040074struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050075struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000076struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077
Eric Sandeen3972f262013-01-12 02:57:22 +000078static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000079static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040080static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050081static noinline int cow_file_range(struct inode *inode,
82 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030083 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030084 unsigned long *nr_written, int unlock);
Liu Bo6f9994d2017-01-31 07:50:22 -080085static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
86 u64 orig_start, u64 block_start,
87 u64 block_len, u64 orig_block_len,
88 u64 ram_bytes, int compress_type,
89 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040090
Qu Wenruo524272602017-03-08 10:25:52 +080091static void __endio_write_update_ordered(struct inode *inode,
92 const u64 offset, const u64 bytes,
93 const bool uptodate);
94
95/*
96 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +010097 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +080098 *
99 * NOTE: caller must ensure that when an error happens, it can not call
100 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
101 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
102 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200103 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800104 */
105static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200106 struct page *locked_page,
107 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800108{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900109 unsigned long index = offset >> PAGE_SHIFT;
110 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200111 u64 page_start = page_offset(locked_page);
112 u64 page_end = page_start + PAGE_SIZE - 1;
113
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900114 struct page *page;
115
116 while (index <= end_index) {
117 page = find_get_page(inode->i_mapping, index);
118 index++;
119 if (!page)
120 continue;
121 ClearPagePrivate2(page);
122 put_page(page);
123 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200124
125 /*
126 * In case this page belongs to the delalloc range being instantiated
127 * then skip it, since the first page of a range is going to be
128 * properly cleaned up by the caller of run_delalloc_range
129 */
130 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
131 offset += PAGE_SIZE;
132 bytes -= PAGE_SIZE;
133 }
134
135 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800136}
137
Eric Sandeen48a3b632013-04-25 20:41:01 +0000138static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400139
Josef Bacik6a3891c2015-03-16 17:38:52 -0400140#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
141void btrfs_test_inode_set_ops(struct inode *inode)
142{
143 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
144}
145#endif
146
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000147static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500148 struct inode *inode, struct inode *dir,
149 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500150{
151 int err;
152
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000153 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500154 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500155 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500156 return err;
157}
158
Chris Masond352ac62008-09-29 15:18:18 -0400159/*
Chris Masonc8b97812008-10-29 14:49:59 -0400160 * this does all the hard work for inserting an inline extent into
161 * the btree. The caller should have done a btrfs_drop_extents so that
162 * no overlapping inline items exist in the btree
163 */
Chris Mason40f76582014-05-21 13:35:51 -0700164static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000165 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400166 struct btrfs_root *root, struct inode *inode,
167 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000168 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400169 struct page **compressed_pages)
170{
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct extent_buffer *leaf;
172 struct page *page = NULL;
173 char *kaddr;
174 unsigned long ptr;
175 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400176 int ret;
177 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400178 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400179
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800180 ASSERT((compressed_size > 0 && compressed_pages) ||
181 (compressed_size == 0 && !compressed_pages));
182
Li Zefanfe3f5662011-03-28 08:30:38 +0000183 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400184 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400185
Chris Masonc8b97812008-10-29 14:49:59 -0400186 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000187
188 if (!extent_inserted) {
189 struct btrfs_key key;
190 size_t datasize;
191
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200192 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000193 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200194 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000195
196 datasize = btrfs_file_extent_calc_inline_size(cur_size);
197 path->leave_spinning = 1;
198 ret = btrfs_insert_empty_item(trans, root, path, &key,
199 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200200 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000201 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400202 }
203 leaf = path->nodes[0];
204 ei = btrfs_item_ptr(leaf, path->slots[0],
205 struct btrfs_file_extent_item);
206 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
207 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
208 btrfs_set_file_extent_encryption(leaf, ei, 0);
209 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
210 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
211 ptr = btrfs_file_extent_inline_start(ei);
212
Li Zefan261507a02010-12-17 14:21:50 +0800213 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400214 struct page *cpage;
215 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500216 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400217 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500218 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300219 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400220
Cong Wang7ac687d2011-11-25 23:14:28 +0800221 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400222 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800223 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400224
225 i++;
226 ptr += cur_size;
227 compressed_size -= cur_size;
228 }
229 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800230 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400231 } else {
232 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300233 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400234 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800235 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100236 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800238 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300239 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400240 }
241 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000242 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400243
Yan, Zhengc2167752009-11-12 09:34:21 +0000244 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500245 * We align size to sectorsize for inline extents just for simplicity
246 * sake.
247 */
248 size = ALIGN(size, root->fs_info->sectorsize);
249 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
250 if (ret)
251 goto fail;
252
253 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000254 * we're an inline extent, so nobody can
255 * extend the file past i_size without locking
256 * a page we already have locked.
257 *
258 * We must do any isize and inode updates
259 * before we unlock the pages. Otherwise we
260 * could end up racing with unlink.
261 */
Chris Masonc8b97812008-10-29 14:49:59 -0400262 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100263 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000264
Chris Masonc8b97812008-10-29 14:49:59 -0400265fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200266 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400267}
268
269
270/*
271 * conditionally insert an inline extent into the file. This
272 * does the checks required to make sure the data is small enough
273 * to fit as an inline extent.
274 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200275static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400276 u64 end, size_t compressed_size,
277 int compress_type,
278 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400279{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200280 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400281 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400282 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400283 u64 isize = i_size_read(inode);
284 u64 actual_end = min(end + 1, isize);
285 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400286 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400287 u64 data_len = inline_len;
288 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000289 struct btrfs_path *path;
290 int extent_inserted = 0;
291 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400292
293 if (compressed_size)
294 data_len = compressed_size;
295
296 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400297 actual_end > fs_info->sectorsize ||
298 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400299 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400300 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400301 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400302 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400303 return 1;
304 }
305
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000306 path = btrfs_alloc_path();
307 if (!path)
308 return -ENOMEM;
309
Josef Bacik00361582013-08-14 14:02:47 -0400310 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000311 if (IS_ERR(trans)) {
312 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400313 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000314 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400315 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400316
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000317 if (compressed_size && compressed_pages)
318 extent_item_size = btrfs_file_extent_calc_inline_size(
319 compressed_size);
320 else
321 extent_item_size = btrfs_file_extent_calc_inline_size(
322 inline_len);
323
324 ret = __btrfs_drop_extents(trans, root, inode, path,
325 start, aligned_end, NULL,
326 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400327 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400328 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400329 goto out;
330 }
Chris Masonc8b97812008-10-29 14:49:59 -0400331
332 if (isize > actual_end)
333 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000334 ret = insert_inline_extent(trans, path, extent_inserted,
335 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400336 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000337 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400338 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400339 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400340 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400341 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400342 ret = 1;
343 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100344 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400345
Josef Bacikbdc20e62013-02-28 13:23:38 -0500346 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200347 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400348out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800349 /*
350 * Don't forget to free the reserved space, as for inlined extent
351 * it won't count as data extent, free them directly here.
352 * And at reserve time, it's always aligned to page size, so
353 * just free one page here.
354 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800355 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000356 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400357 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400358 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400359}
360
Chris Mason771ed682008-11-06 22:02:51 -0500361struct async_extent {
362 u64 start;
363 u64 ram_size;
364 u64 compressed_size;
365 struct page **pages;
366 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800367 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500368 struct list_head list;
369};
370
Nikolay Borisov97db1202019-03-12 17:20:24 +0200371struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500373 struct page *locked_page;
374 u64 start;
375 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600376 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500377 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700378 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500379 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200380 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500381};
382
Nikolay Borisov97db1202019-03-12 17:20:24 +0200383struct async_cow {
384 /* Number of chunks in flight; must be first in the structure */
385 atomic_t num_chunks;
386 struct async_chunk chunks[];
387};
388
389static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500390 u64 start, u64 ram_size,
391 u64 compressed_size,
392 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800393 unsigned long nr_pages,
394 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500395{
396 struct async_extent *async_extent;
397
398 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100399 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500400 async_extent->start = start;
401 async_extent->ram_size = ram_size;
402 async_extent->compressed_size = compressed_size;
403 async_extent->pages = pages;
404 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800405 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500406 list_add_tail(&async_extent->list, &cow->extents);
407 return 0;
408}
409
Qu Wenruo42c16da2019-07-01 05:12:46 +0000410/*
411 * Check if the inode has flags compatible with compression
412 */
413static inline bool inode_can_compress(struct inode *inode)
414{
415 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
416 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
417 return false;
418 return true;
419}
420
421/*
422 * Check if the inode needs to be submitted to compression, based on mount
423 * options, defragmentation, properties or heuristics.
424 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300425static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800426{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400427 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800428
Qu Wenruo42c16da2019-07-01 05:12:46 +0000429 if (!inode_can_compress(inode)) {
430 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
431 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
432 btrfs_ino(BTRFS_I(inode)));
433 return 0;
434 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800435 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400436 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800437 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200438 /* defrag ioctl */
439 if (BTRFS_I(inode)->defrag_compress)
440 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800441 /* bad compression ratios */
442 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
443 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400444 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800445 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200446 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300447 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800448 return 0;
449}
450
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200451static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800452 u64 start, u64 end, u64 num_bytes, u64 small_write)
453{
454 /* If this is a small write inside eof, kick off a defrag */
455 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200456 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800457 btrfs_add_inode_defrag(NULL, inode);
458}
459
Chris Masonc8b97812008-10-29 14:49:59 -0400460/*
Chris Mason771ed682008-11-06 22:02:51 -0500461 * we create compressed extents in two phases. The first
462 * phase compresses a range of pages that have already been
463 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400464 *
Chris Mason771ed682008-11-06 22:02:51 -0500465 * This is done inside an ordered work queue, and the compression
466 * is spread across many cpus. The actual IO submission is step
467 * two, and the ordered work queue takes care of making sure that
468 * happens in the same order things were put onto the queue by
469 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400470 *
Chris Mason771ed682008-11-06 22:02:51 -0500471 * If this code finds it can't get good compression, it puts an
472 * entry onto the work queue to write the uncompressed bytes. This
473 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300474 * are written in the same order that the flusher thread sent them
475 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400476 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300477static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400478{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200479 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400480 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400481 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200482 u64 start = async_chunk->start;
483 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400484 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400485 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400486 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400487 struct page **pages = NULL;
488 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400489 unsigned long total_compressed = 0;
490 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400491 int i;
492 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400493 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300494 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400495 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400496
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200497 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
498 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400499
Josef Bacikd98da492019-10-11 09:03:54 -0400500 /*
501 * We need to save i_size before now because it could change in between
502 * us evaluating the size and assigning it. This is because we lock and
503 * unlock the page in truncate and fallocate, and then modify the i_size
504 * later on.
505 *
506 * The barriers are to emulate READ_ONCE, remove that once i_size_read
507 * does that for us.
508 */
509 barrier();
510 i_size = i_size_read(inode);
511 barrier();
512 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400513again:
514 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300515 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100516 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
517 nr_pages = min_t(unsigned long, nr_pages,
518 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400519
Chris Masonf03d9301f2009-02-04 09:31:06 -0500520 /*
521 * we don't want to send crud past the end of i_size through
522 * compression, that's just a waste of CPU time. So, if the
523 * end of the file is before the start of our current
524 * requested range of bytes, we bail out to the uncompressed
525 * cleanup code that can deal with all of this.
526 *
527 * It isn't really the fastest way to fix things, but this is a
528 * very uncommon corner.
529 */
530 if (actual_end <= start)
531 goto cleanup_and_bail_uncompressed;
532
Chris Masonc8b97812008-10-29 14:49:59 -0400533 total_compressed = actual_end - start;
534
Shilong Wang4bcbb332014-10-07 18:44:35 -0400535 /*
536 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400537 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400538 */
539 if (total_compressed <= blocksize &&
540 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
541 goto cleanup_and_bail_uncompressed;
542
David Sterba069eac72017-02-14 19:36:54 +0100543 total_compressed = min_t(unsigned long, total_compressed,
544 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400545 total_in = 0;
546 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400547
Chris Mason771ed682008-11-06 22:02:51 -0500548 /*
549 * we do compression for mount -o compress and when the
550 * inode has not been flagged as nocompress. This flag can
551 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400552 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300553 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400554 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100555 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800556 if (!pages) {
557 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100558 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800559 goto cont;
560 }
Chris Mason179e29e2007-11-01 11:28:41 -0400561
David Sterbaeec63c62017-07-17 19:41:31 +0200562 if (BTRFS_I(inode)->defrag_compress)
563 compress_type = BTRFS_I(inode)->defrag_compress;
564 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200565 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800566
Chris Mason4adaa612013-03-26 13:07:00 -0400567 /*
568 * we need to call clear_page_dirty_for_io on each
569 * page in the range. Otherwise applications with the file
570 * mmap'd can wander in and change the page contents while
571 * we are compressing them.
572 *
573 * If the compression fails for any reason, we set the pages
574 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300575 *
576 * Note that the remaining part is redirtied, the start pointer
577 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400578 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300579 if (!redirty) {
580 extent_range_clear_dirty_for_io(inode, start, end);
581 redirty = 1;
582 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200583
584 /* Compression level is applied here and only here */
585 ret = btrfs_compress_pages(
586 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800587 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100588 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100589 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800590 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100591 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400592
593 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100594 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100595 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400596 char *kaddr;
597
598 /* zero the tail end of the last page, we might be
599 * sending it down to disk
600 */
601 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800602 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400603 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300604 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800605 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400606 }
607 will_compress = 1;
608 }
609 }
Li Zefan560f7d72011-09-08 10:22:01 +0800610cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400611 if (start == 0) {
612 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300613 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400614 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500615 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400616 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200617 ret = cow_file_range_inline(inode, start, end, 0,
618 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400619 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500620 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200621 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000622 total_compressed,
623 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400624 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100625 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400626 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400627 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
628 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100629 unsigned long page_error_op;
630
Filipe Mananae6eb4312014-10-10 10:45:12 +0100631 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400632
Chris Mason771ed682008-11-06 22:02:51 -0500633 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100634 * inline extent creation worked or returned error,
635 * we don't need to create any more async work items.
636 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400637 *
638 * We use DO_ACCOUNTING here because we need the
639 * delalloc_release_metadata to be done _after_ we drop
640 * our outstanding extent for clearing delalloc for this
641 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500642 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300643 extent_clear_unlock_delalloc(inode, start, end, NULL,
644 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800645 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400646 PAGE_CLEAR_DIRTY |
647 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100648 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400649 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300650
651 for (i = 0; i < nr_pages; i++) {
652 WARN_ON(pages[i]->mapping);
653 put_page(pages[i]);
654 }
655 kfree(pages);
656
657 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400658 }
659 }
660
661 if (will_compress) {
662 /*
663 * we aren't doing an inline extent round the compressed size
664 * up to a block size boundary so the allocator does sane
665 * things
666 */
Qu Wenruofda28322013-02-26 08:10:22 +0000667 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400668
669 /*
670 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300671 * win, compare the page count read with the blocks on disk,
672 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400673 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300674 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300675 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300676 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700677
678 /*
679 * The async work queues will take care of doing actual
680 * allocation on disk for these compressed pages, and
681 * will submit them to the elevator.
682 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200683 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100684 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700685 compress_type);
686
Timofey Titovets11708622017-10-03 18:06:01 +0300687 if (start + total_in < end) {
688 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700689 pages = NULL;
690 cond_resched();
691 goto again;
692 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300693 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400694 }
695 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700696 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400697 /*
698 * the compression code ran but failed to make things smaller,
699 * free any pages it allocated and our page pointer array
700 */
David Sterba4d3a8002017-02-14 19:04:07 +0100701 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400702 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300703 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400704 }
705 kfree(pages);
706 pages = NULL;
707 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100708 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400709
710 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400711 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200712 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500713 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500714 }
Chris Masonc8b97812008-10-29 14:49:59 -0400715 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500716cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700717 /*
718 * No compression, but we still need to write the pages in the file
719 * we've been given so far. redirty the locked page if it corresponds
720 * to our extent and set things up for the async work queue to run
721 * cow_file_range to do the normal delalloc dance.
722 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700723 if (async_chunk->locked_page &&
724 (page_offset(async_chunk->locked_page) >= start &&
725 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200726 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700727 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700728 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700729
730 if (redirty)
731 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200732 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700733 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300734 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500735
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300736 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500737}
Chris Mason771ed682008-11-06 22:02:51 -0500738
Filipe Manana40ae8372014-10-06 22:14:24 +0100739static void free_async_extent_pages(struct async_extent *async_extent)
740{
741 int i;
742
743 if (!async_extent->pages)
744 return;
745
746 for (i = 0; i < async_extent->nr_pages; i++) {
747 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300748 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100749 }
750 kfree(async_extent->pages);
751 async_extent->nr_pages = 0;
752 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500753}
754
755/*
756 * phase two of compressed writeback. This is the ordered portion
757 * of the code, which only gets called in the order the work was
758 * queued. We walk all the async extents created by compress_file_range
759 * and send them down to the disk.
760 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200761static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500762{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200763 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400764 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500765 struct async_extent *async_extent;
766 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500767 struct btrfs_key ins;
768 struct extent_map *em;
769 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200770 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500771 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500772
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500773again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200774 while (!list_empty(&async_chunk->extents)) {
775 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500776 struct async_extent, list);
777 list_del(&async_extent->list);
778
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500779retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200780 lock_extent(io_tree, async_extent->start,
781 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500782 /* did the compression code fall back to uncompressed IO? */
783 if (!async_extent->pages) {
784 int page_started = 0;
785 unsigned long nr_written = 0;
786
Chris Mason771ed682008-11-06 22:02:51 -0500787 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200788 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500789 async_extent->start,
790 async_extent->start +
791 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300792 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500793
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100794 /* JDM XXX */
795
Chris Mason771ed682008-11-06 22:02:51 -0500796 /*
797 * if page_started, cow_file_range inserted an
798 * inline extent and took care of all the unlocking
799 * and IO for us. Otherwise, we need to submit
800 * all those pages down to the drive.
801 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500802 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200803 extent_write_locked_range(inode,
804 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500805 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500806 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500807 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700808 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200809 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500810 kfree(async_extent);
811 cond_resched();
812 continue;
813 }
814
Wang Xiaoguang18513092016-07-25 15:51:40 +0800815 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500816 async_extent->compressed_size,
817 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800818 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500819 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100820 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500821
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400822 if (ret == -ENOSPC) {
823 unlock_extent(io_tree, async_extent->start,
824 async_extent->start +
825 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800826
827 /*
828 * we need to redirty the pages if we decide to
829 * fallback to uncompressed IO, otherwise we
830 * will not submit these pages down to lower
831 * layers.
832 */
833 extent_range_redirty_for_io(inode,
834 async_extent->start,
835 async_extent->start +
836 async_extent->ram_size - 1);
837
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100838 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400839 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500840 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500841 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000842 /*
843 * here we're doing allocation and writeback of the
844 * compressed pages
845 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800846 em = create_io_em(inode, async_extent->start,
847 async_extent->ram_size, /* len */
848 async_extent->start, /* orig_start */
849 ins.objectid, /* block_start */
850 ins.offset, /* block_len */
851 ins.offset, /* orig_block_len */
852 async_extent->ram_size, /* ram_bytes */
853 async_extent->compress_type,
854 BTRFS_ORDERED_COMPRESSED);
855 if (IS_ERR(em))
856 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500857 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800858 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500859
Li Zefan261507a02010-12-17 14:21:50 +0800860 ret = btrfs_add_ordered_extent_compress(inode,
861 async_extent->start,
862 ins.objectid,
863 async_extent->ram_size,
864 ins.offset,
865 BTRFS_ORDERED_COMPRESSED,
866 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100867 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200868 btrfs_drop_extent_cache(BTRFS_I(inode),
869 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100870 async_extent->start +
871 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100873 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400874 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500875
Chris Mason771ed682008-11-06 22:02:51 -0500876 /*
877 * clear dirty, set writeback and unlock the pages.
878 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400879 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400880 async_extent->start +
881 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400882 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
883 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400884 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200885 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500886 async_extent->start,
887 async_extent->ram_size,
888 ins.objectid,
889 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600890 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700891 async_chunk->write_flags,
892 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100893 struct page *p = async_extent->pages[0];
894 const u64 start = async_extent->start;
895 const u64 end = start + async_extent->ram_size - 1;
896
897 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200898 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200899
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100900 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300901 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800902 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100903 PAGE_END_WRITEBACK |
904 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100905 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100906 }
Chris Mason771ed682008-11-06 22:02:51 -0500907 alloc_hint = ins.objectid + ins.offset;
908 kfree(async_extent);
909 cond_resched();
910 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100911 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500912out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400913 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400914 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100915out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400916 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500917 async_extent->start +
918 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400919 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100920 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400921 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
922 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100923 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
924 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100925 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100926 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500927 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500928}
929
Josef Bacik4b46fce2010-05-23 11:00:55 -0400930static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
931 u64 num_bytes)
932{
933 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
934 struct extent_map *em;
935 u64 alloc_hint = 0;
936
937 read_lock(&em_tree->lock);
938 em = search_extent_mapping(em_tree, start, num_bytes);
939 if (em) {
940 /*
941 * if block start isn't an actual block number then find the
942 * first block in this inode and use that as a hint. If that
943 * block is also bogus then just don't worry about it.
944 */
945 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
946 free_extent_map(em);
947 em = search_extent_mapping(em_tree, 0, 0);
948 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
949 alloc_hint = em->block_start;
950 if (em)
951 free_extent_map(em);
952 } else {
953 alloc_hint = em->block_start;
954 free_extent_map(em);
955 }
956 }
957 read_unlock(&em_tree->lock);
958
959 return alloc_hint;
960}
961
Chris Mason771ed682008-11-06 22:02:51 -0500962/*
963 * when extent_io.c finds a delayed allocation range in the file,
964 * the call backs end up in this code. The basic idea is to
965 * allocate extents on disk for the range, and create ordered data structs
966 * in ram to track those extents.
967 *
968 * locked_page is the page that writepage had locked already. We use
969 * it to make sure we don't do extra locks or unlocks.
970 *
971 * *page_started is set to one if we unlock locked_page and do everything
972 * required to start IO on it. It may be clean and already done with
973 * IO when we return.
974 */
Josef Bacik00361582013-08-14 14:02:47 -0400975static noinline int cow_file_range(struct inode *inode,
976 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300977 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300978 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500979{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400980 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400981 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500982 u64 alloc_hint = 0;
983 u64 num_bytes;
984 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000985 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400986 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500987 struct btrfs_key ins;
988 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000989 unsigned clear_bits;
990 unsigned long page_ops;
991 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500992 int ret = 0;
993
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200994 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400995 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500996 ret = -EINVAL;
997 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400998 }
Chris Mason771ed682008-11-06 22:02:51 -0500999
Qu Wenruofda28322013-02-26 08:10:22 +00001000 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001001 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001002 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001003
Nikolay Borisov6158e1c2017-02-20 13:50:43 +02001004 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001005
Chris Mason771ed682008-11-06 22:02:51 -05001006 if (start == 0) {
1007 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001008 ret = cow_file_range_inline(inode, start, end, 0,
1009 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001010 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001011 /*
1012 * We use DO_ACCOUNTING here because we need the
1013 * delalloc_release_metadata to be run _after_ we drop
1014 * our outstanding extent for clearing delalloc for this
1015 * range.
1016 */
Nikolay Borisov74e91942019-07-17 16:18:16 +03001017 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001018 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001019 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1020 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001021 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1022 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001023 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001024 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001025 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001026 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001027 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001028 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001029 }
1030 }
Chris Masonc8b97812008-10-29 14:49:59 -04001031
Josef Bacik4b46fce2010-05-23 11:00:55 -04001032 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001033 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1034 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001035
Anand Jain3752d222018-02-15 12:29:38 +08001036 while (num_bytes > 0) {
1037 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001038 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001039 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001040 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001041 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001042 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001043 cur_alloc_size = ins.offset;
1044 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001045
Chris Mason771ed682008-11-06 22:02:51 -05001046 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001047 em = create_io_em(inode, start, ins.offset, /* len */
1048 start, /* orig_start */
1049 ins.objectid, /* block_start */
1050 ins.offset, /* block_len */
1051 ins.offset, /* orig_block_len */
1052 ram_size, /* ram_bytes */
1053 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001054 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001055 if (IS_ERR(em)) {
1056 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001057 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001058 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001059 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001060
Chris Masone6dcd2d2008-07-17 12:53:50 -04001061 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001062 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001063 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001064 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001065
Yan Zheng17d217f2008-12-12 10:03:38 -05001066 if (root->root_key.objectid ==
1067 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1068 ret = btrfs_reloc_clone_csums(inode, start,
1069 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001070 /*
1071 * Only drop cache here, and process as normal.
1072 *
1073 * We must not allow extent_clear_unlock_delalloc()
1074 * at out_unlock label to free meta of this ordered
1075 * extent, as its meta should be freed by
1076 * btrfs_finish_ordered_io().
1077 *
1078 * So we must continue until @start is increased to
1079 * skip current ordered extent.
1080 */
Josef Bacik00361582013-08-14 14:02:47 -04001081 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001082 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1083 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001084 }
1085
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001086 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001087
Chris Masonc8b97812008-10-29 14:49:59 -04001088 /* we're not doing compressed IO, don't unlock the first
1089 * page (which the caller expects to stay locked), don't
1090 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001091 *
1092 * Do set the Private2 bit so we know this page was properly
1093 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001094 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001095 page_ops = unlock ? PAGE_UNLOCK : 0;
1096 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001097
Josef Bacikc2790a22013-07-29 11:20:47 -04001098 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001099 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001100 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001101 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001102 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001103 if (num_bytes < cur_alloc_size)
1104 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001105 else
Anand Jain3752d222018-02-15 12:29:38 +08001106 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001107 alloc_hint = ins.objectid + ins.offset;
1108 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001109 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001110
1111 /*
1112 * btrfs_reloc_clone_csums() error, since start is increased
1113 * extent_clear_unlock_delalloc() at out_unlock label won't
1114 * free metadata of current ordered extent, we're OK to exit.
1115 */
1116 if (ret)
1117 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001118 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001119out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001120 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001121
Filipe Mananad9f85962014-08-25 10:43:00 +01001122out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001123 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001124out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001125 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001126 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001127out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001128 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1129 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001130 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1131 PAGE_END_WRITEBACK;
1132 /*
1133 * If we reserved an extent for our delalloc range (or a subrange) and
1134 * failed to create the respective ordered extent, then it means that
1135 * when we reserved the extent we decremented the extent's size from
1136 * the data space_info's bytes_may_use counter and incremented the
1137 * space_info's bytes_reserved counter by the same amount. We must make
1138 * sure extent_clear_unlock_delalloc() does not try to decrement again
1139 * the data space_info's bytes_may_use counter, therefore we do not pass
1140 * it the flag EXTENT_CLEAR_DATA_RESV.
1141 */
1142 if (extent_reserved) {
1143 extent_clear_unlock_delalloc(inode, start,
1144 start + cur_alloc_size,
Filipe Mananaa315e682017-03-06 23:04:20 +00001145 locked_page,
1146 clear_bits,
1147 page_ops);
1148 start += cur_alloc_size;
1149 if (start >= end)
1150 goto out;
1151 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001152 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001153 clear_bits | EXTENT_CLEAR_DATA_RESV,
1154 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001155 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001156}
Chris Masonc8b97812008-10-29 14:49:59 -04001157
Chris Mason771ed682008-11-06 22:02:51 -05001158/*
1159 * work queue call back to started compression on a file and pages
1160 */
1161static noinline void async_cow_start(struct btrfs_work *work)
1162{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001163 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001164 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001165
Nikolay Borisovb5326272019-03-12 17:20:25 +02001166 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001167
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001168 compressed_extents = compress_file_range(async_chunk);
1169 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001170 btrfs_add_delayed_iput(async_chunk->inode);
1171 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001172 }
Chris Mason771ed682008-11-06 22:02:51 -05001173}
1174
1175/*
1176 * work queue call back to submit previously compressed pages
1177 */
1178static noinline void async_cow_submit(struct btrfs_work *work)
1179{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001180 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1181 work);
1182 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001183 unsigned long nr_pages;
1184
Nikolay Borisovb5326272019-03-12 17:20:25 +02001185 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001186 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001187
David Sterba093258e2018-02-26 16:15:17 +01001188 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001189 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001190 5 * SZ_1M)
1191 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001192
Nikolay Borisov4546d172019-01-03 10:50:03 +02001193 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001194 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001195 * in which case we don't have anything to submit, yet we need to
1196 * always adjust ->async_delalloc_pages as its paired with the init
1197 * happening in cow_file_range_async
1198 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001199 if (async_chunk->inode)
1200 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001201}
Chris Masonc8b97812008-10-29 14:49:59 -04001202
Chris Mason771ed682008-11-06 22:02:51 -05001203static noinline void async_cow_free(struct btrfs_work *work)
1204{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001205 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001206
Nikolay Borisovb5326272019-03-12 17:20:25 +02001207 async_chunk = container_of(work, struct async_chunk, work);
1208 if (async_chunk->inode)
1209 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001210 if (async_chunk->blkcg_css)
1211 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001212 /*
1213 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001214 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001215 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001216 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001217 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001218}
1219
Chris Masonec39f762019-07-10 12:28:17 -07001220static int cow_file_range_async(struct inode *inode,
1221 struct writeback_control *wbc,
1222 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001223 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001224 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001225{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001226 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonec39f762019-07-10 12:28:17 -07001227 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001228 struct async_cow *ctx;
1229 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001230 unsigned long nr_pages;
1231 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001232 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1233 int i;
1234 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001235 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001236 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001237
Nikolay Borisov69684c52019-03-12 17:20:28 +02001238 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001239
1240 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1241 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1242 num_chunks = 1;
1243 should_compress = false;
1244 } else {
1245 should_compress = true;
1246 }
1247
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001248 nofs_flag = memalloc_nofs_save();
1249 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1250 memalloc_nofs_restore(nofs_flag);
1251
Nikolay Borisov97db1202019-03-12 17:20:24 +02001252 if (!ctx) {
1253 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1254 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1255 EXTENT_DO_ACCOUNTING;
1256 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1257 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1258 PAGE_SET_ERROR;
1259
Nikolay Borisov74e91942019-07-17 16:18:16 +03001260 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001261 clear_bits, page_ops);
1262 return -ENOMEM;
1263 }
1264
1265 async_chunk = ctx->chunks;
1266 atomic_set(&ctx->num_chunks, num_chunks);
1267
1268 for (i = 0; i < num_chunks; i++) {
1269 if (should_compress)
1270 cur_end = min(end, start + SZ_512K - 1);
1271 else
1272 cur_end = end;
1273
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001274 /*
1275 * igrab is called higher up in the call chain, take only the
1276 * lightweight reference for the callback lifetime
1277 */
1278 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001279 async_chunk[i].pending = &ctx->num_chunks;
1280 async_chunk[i].inode = inode;
1281 async_chunk[i].start = start;
1282 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001283 async_chunk[i].write_flags = write_flags;
1284 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001285
Chris Mason1d53c9e2019-07-10 12:28:16 -07001286 /*
1287 * The locked_page comes all the way from writepage and its
1288 * the original page we were actually given. As we spread
1289 * this large delalloc region across multiple async_chunk
1290 * structs, only the first struct needs a pointer to locked_page
1291 *
1292 * This way we don't need racey decisions about who is supposed
1293 * to unlock it.
1294 */
1295 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001296 /*
1297 * Depending on the compressibility, the pages might or
1298 * might not go through async. We want all of them to
1299 * be accounted against wbc once. Let's do it here
1300 * before the paths diverge. wbc accounting is used
1301 * only for foreign writeback detection and doesn't
1302 * need full accuracy. Just account the whole thing
1303 * against the first page.
1304 */
1305 wbc_account_cgroup_owner(wbc, locked_page,
1306 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001307 async_chunk[i].locked_page = locked_page;
1308 locked_page = NULL;
1309 } else {
1310 async_chunk[i].locked_page = NULL;
1311 }
1312
Chris Masonec39f762019-07-10 12:28:17 -07001313 if (blkcg_css != blkcg_root_css) {
1314 css_get(blkcg_css);
1315 async_chunk[i].blkcg_css = blkcg_css;
1316 } else {
1317 async_chunk[i].blkcg_css = NULL;
1318 }
1319
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001320 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1321 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001322
Nikolay Borisov97db1202019-03-12 17:20:24 +02001323 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001324 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001325
Nikolay Borisov97db1202019-03-12 17:20:24 +02001326 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001327
Chris Mason771ed682008-11-06 22:02:51 -05001328 *nr_written += nr_pages;
1329 start = cur_end + 1;
1330 }
1331 *page_started = 1;
1332 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001333}
1334
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001335static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001336 u64 bytenr, u64 num_bytes)
1337{
1338 int ret;
1339 struct btrfs_ordered_sum *sums;
1340 LIST_HEAD(list);
1341
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001342 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001343 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001344 if (ret == 0 && list_empty(&list))
1345 return 0;
1346
1347 while (!list_empty(&list)) {
1348 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1349 list_del(&sums->list);
1350 kfree(sums);
1351 }
Liu Bo58113752018-01-31 17:09:13 -07001352 if (ret < 0)
1353 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001354 return 1;
1355}
1356
Chris Masond352ac62008-09-29 15:18:18 -04001357/*
1358 * when nowcow writeback call back. This checks for snapshots or COW copies
1359 * of the extents that exist in the file, and COWs the file as required.
1360 *
1361 * If no cow copies or snapshots exist, we write directly to the existing
1362 * blocks on disk
1363 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001364static noinline int run_delalloc_nocow(struct inode *inode,
1365 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001366 const u64 start, const u64 end,
1367 int *page_started, int force,
1368 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001369{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001370 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001371 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001372 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001373 u64 cow_start = (u64)-1;
1374 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001375 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001376 bool check_prev = true;
1377 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001378 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001379 bool nocow = false;
1380 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001381
1382 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001383 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001384 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001385 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001386 EXTENT_DO_ACCOUNTING |
1387 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001388 PAGE_CLEAR_DIRTY |
1389 PAGE_SET_WRITEBACK |
1390 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001391 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001392 }
Li Zefan82d59022011-04-20 10:33:24 +08001393
Yan Zheng80ff3852008-10-30 14:20:02 -04001394 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001395 struct btrfs_key found_key;
1396 struct btrfs_file_extent_item *fi;
1397 struct extent_buffer *leaf;
1398 u64 extent_end;
1399 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001400 u64 num_bytes = 0;
1401 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001402 u64 ram_bytes;
1403 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001404
1405 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001406
Liu Boe4c3b2d2017-01-30 12:25:28 -08001407 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001408 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001409 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001410 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001411
1412 /*
1413 * If there is no extent for our range when doing the initial
1414 * search, then go back to the previous slot as it will be the
1415 * one containing the search offset
1416 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001417 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1418 leaf = path->nodes[0];
1419 btrfs_item_key_to_cpu(leaf, &found_key,
1420 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001421 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001422 found_key.type == BTRFS_EXTENT_DATA_KEY)
1423 path->slots[0]--;
1424 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001425 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001426next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001427 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001428 leaf = path->nodes[0];
1429 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1430 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001431 if (ret < 0) {
1432 if (cow_start != (u64)-1)
1433 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001434 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001435 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001436 if (ret > 0)
1437 break;
1438 leaf = path->nodes[0];
1439 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001440
Yan Zheng80ff3852008-10-30 14:20:02 -04001441 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001442
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001443 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001444 if (found_key.objectid > ino)
1445 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001446 /*
1447 * Keep searching until we find an EXTENT_ITEM or there are no
1448 * more extents for this inode
1449 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001450 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1451 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1452 path->slots[0]++;
1453 goto next_slot;
1454 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001455
1456 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001457 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001458 found_key.offset > end)
1459 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001460
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001461 /*
1462 * If the found extent starts after requested offset, then
1463 * adjust extent_end to be right before this extent begins
1464 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001465 if (found_key.offset > cur_offset) {
1466 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001467 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001468 goto out_check;
1469 }
Chris Masonc31f8832008-01-08 15:46:31 -05001470
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001471 /*
1472 * Found extent which begins before our range and potentially
1473 * intersect it
1474 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001475 fi = btrfs_item_ptr(leaf, path->slots[0],
1476 struct btrfs_file_extent_item);
1477 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001478
Josef Bacikcc95bef2013-04-04 14:31:27 -04001479 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001480 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1481 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001482 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001483 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001484 extent_end = found_key.offset +
1485 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001486 disk_num_bytes =
1487 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001488 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001489 * If the extent we got ends before our current offset,
1490 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001491 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001492 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001493 path->slots[0]++;
1494 goto next_slot;
1495 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001496 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001497 if (disk_bytenr == 0)
1498 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001499 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001500 if (btrfs_file_extent_compression(leaf, fi) ||
1501 btrfs_file_extent_encryption(leaf, fi) ||
1502 btrfs_file_extent_other_encoding(leaf, fi))
1503 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001504 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001505 * If extent is created before the last volume's snapshot
1506 * this implies the extent is shared, hence we can't do
1507 * nocow. This is the same check as in
1508 * btrfs_cross_ref_exist but without calling
1509 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001510 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001511 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001512 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001513 btrfs_root_last_snapshot(&root->root_item))
1514 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001515 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1516 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001517 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001518 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001519 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001520 ret = btrfs_cross_ref_exist(root, ino,
1521 found_key.offset -
1522 extent_offset, disk_bytenr);
1523 if (ret) {
1524 /*
1525 * ret could be -EIO if the above fails to read
1526 * metadata.
1527 */
1528 if (ret < 0) {
1529 if (cow_start != (u64)-1)
1530 cur_offset = cow_start;
1531 goto error;
1532 }
1533
Nikolay Borisov3e024842019-08-21 10:42:03 +03001534 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001535 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001536 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001537 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001538 disk_bytenr += cur_offset - found_key.offset;
1539 num_bytes = min(end + 1, extent_end) - cur_offset;
1540 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001541 * If there are pending snapshots for this root, we
1542 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001543 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001544 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001545 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001546 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001547 * force cow if csum exists in the range.
1548 * this ensure that csum for a given extent are
1549 * either valid or do not exist.
1550 */
Liu Bo58113752018-01-31 17:09:13 -07001551 ret = csum_exist_in_range(fs_info, disk_bytenr,
1552 num_bytes);
1553 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001554 /*
1555 * ret could be -EIO if the above fails to read
1556 * metadata.
1557 */
1558 if (ret < 0) {
1559 if (cow_start != (u64)-1)
1560 cur_offset = cow_start;
1561 goto error;
1562 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001563 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001564 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001565 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001566 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001567 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001568 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001569 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001570 extent_end = found_key.offset + ram_bytes;
1571 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001572 /* Skip extents outside of our requested range */
1573 if (extent_end <= start) {
1574 path->slots[0]++;
1575 goto next_slot;
1576 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001577 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001578 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001579 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001580 }
1581out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001582 /*
1583 * If nocow is false then record the beginning of the range
1584 * that needs to be COWed
1585 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001586 if (!nocow) {
1587 if (cow_start == (u64)-1)
1588 cow_start = cur_offset;
1589 cur_offset = extent_end;
1590 if (cur_offset > end)
1591 break;
1592 path->slots[0]++;
1593 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001594 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001595
David Sterbab3b4aa72011-04-21 01:20:15 +02001596 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001597
1598 /*
1599 * COW range from cow_start to found_key.offset - 1. As the key
1600 * will contain the beginning of the first extent that can be
1601 * NOCOW, following one which needs to be COW'ed
1602 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001603 if (cow_start != (u64)-1) {
Josef Bacik00361582013-08-14 14:02:47 -04001604 ret = cow_file_range(inode, locked_page,
1605 cow_start, found_key.offset - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001606 page_started, nr_written, 1);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001607 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001608 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001609 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001610 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001611 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001612 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001613 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001614 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001615
Yan Zhengd899e052008-10-30 14:25:28 -04001616 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001617 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001618 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001619
1620 em = create_io_em(inode, cur_offset, num_bytes,
1621 orig_start,
1622 disk_bytenr, /* block_start */
1623 num_bytes, /* block_len */
1624 disk_num_bytes, /* orig_block_len */
1625 ram_bytes, BTRFS_COMPRESS_NONE,
1626 BTRFS_ORDERED_PREALLOC);
1627 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001628 if (nocow)
1629 btrfs_dec_nocow_writers(fs_info,
1630 disk_bytenr);
1631 ret = PTR_ERR(em);
1632 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001633 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001634 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001635 ret = btrfs_add_ordered_extent(inode, cur_offset,
1636 disk_bytenr, num_bytes,
1637 num_bytes,
1638 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001639 if (ret) {
1640 btrfs_drop_extent_cache(BTRFS_I(inode),
1641 cur_offset,
1642 cur_offset + num_bytes - 1,
1643 0);
1644 goto error;
1645 }
Yan Zhengd899e052008-10-30 14:25:28 -04001646 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001647 ret = btrfs_add_ordered_extent(inode, cur_offset,
1648 disk_bytenr, num_bytes,
1649 num_bytes,
1650 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001651 if (ret)
1652 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001653 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001654
Filipe Mananaf78c4362016-05-09 13:15:41 +01001655 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001656 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001657 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001658
Yan, Zhengefa56462010-05-16 10:49:59 -04001659 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001660 BTRFS_DATA_RELOC_TREE_OBJECTID)
1661 /*
1662 * Error handled later, as we must prevent
1663 * extent_clear_unlock_delalloc() in error handler
1664 * from freeing metadata of created ordered extent.
1665 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001666 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1667 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001668
Josef Bacikc2790a22013-07-29 11:20:47 -04001669 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001670 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001671 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001672 EXTENT_DELALLOC |
1673 EXTENT_CLEAR_DATA_RESV,
1674 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1675
Yan Zheng80ff3852008-10-30 14:20:02 -04001676 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001677
1678 /*
1679 * btrfs_reloc_clone_csums() error, now we're OK to call error
1680 * handler, as metadata for created ordered extent will only
1681 * be freed by btrfs_finish_ordered_io().
1682 */
1683 if (ret)
1684 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001685 if (cur_offset > end)
1686 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001687 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001688 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001689
Robbie Ko506481b2018-10-30 18:04:04 +08001690 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001691 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001692
Yan Zheng80ff3852008-10-30 14:20:02 -04001693 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001694 cur_offset = end;
Nikolay Borisov74e91942019-07-17 16:18:16 +03001695 ret = cow_file_range(inode, locked_page, cow_start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001696 page_started, nr_written, 1);
Josef Bacikd788a342013-10-25 16:55:08 -04001697 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001698 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001699 }
1700
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001701error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001702 if (nocow)
1703 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1704
Josef Bacik17ca04a2012-05-31 15:58:55 -04001705 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001706 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001707 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001708 EXTENT_DELALLOC | EXTENT_DEFRAG |
1709 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1710 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001711 PAGE_SET_WRITEBACK |
1712 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001713 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001714 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001715}
1716
Wang Shilong47059d92014-07-03 18:22:07 +08001717static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1718{
1719
1720 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1721 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1722 return 0;
1723
1724 /*
1725 * @defrag_bytes is a hint value, no spinlock held here,
1726 * if is not zero, it means the file is defragging.
1727 * Force cow if given extent needs to be defragged.
1728 */
1729 if (BTRFS_I(inode)->defrag_bytes &&
1730 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1731 EXTENT_DEFRAG, 0, NULL))
1732 return 1;
1733
1734 return 0;
1735}
1736
Chris Masond352ac62008-09-29 15:18:18 -04001737/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001738 * Function to process delayed allocation (create CoW) for ranges which are
1739 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001740 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001741int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001742 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1743 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001744{
Chris Masonbe20aa92007-12-17 20:14:01 -05001745 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001746 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001747
Wang Shilong47059d92014-07-03 18:22:07 +08001748 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001749 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001750 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001751 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001752 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001753 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001754 } else if (!inode_can_compress(inode) ||
1755 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001756 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001757 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001758 } else {
1759 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1760 &BTRFS_I(inode)->runtime_flags);
Chris Masonec39f762019-07-10 12:28:17 -07001761 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001762 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001763 }
Qu Wenruo524272602017-03-08 10:25:52 +08001764 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001765 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1766 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001767 return ret;
1768}
1769
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001770void btrfs_split_delalloc_extent(struct inode *inode,
1771 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001772{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001773 u64 size;
1774
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001775 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001776 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001777 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001778
Josef Bacikdcab6a32015-02-11 15:08:59 -05001779 size = orig->end - orig->start + 1;
1780 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001781 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001782 u64 new_size;
1783
1784 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001785 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001786 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001787 */
1788 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001789 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001790 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001791 num_extents += count_max_extents(new_size);
1792 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001793 return;
1794 }
1795
Josef Bacik9e0baf62011-07-15 15:16:44 +00001796 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001797 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001798 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001799}
1800
1801/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001802 * Handle merged delayed allocation extents so we can keep track of new extents
1803 * that are just merged onto old extents, such as when we are doing sequential
1804 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001805 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001806void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1807 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001808{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001809 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001810 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001811
Josef Bacik9ed74f22009-09-11 16:12:44 -04001812 /* not delalloc, ignore it */
1813 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001814 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001815
Josef Bacik8461a3d2015-03-13 15:12:08 -04001816 if (new->start > other->start)
1817 new_size = new->end - other->start + 1;
1818 else
1819 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001820
1821 /* we're not bigger than the max, unreserve the space and go */
1822 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1823 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001824 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001825 spin_unlock(&BTRFS_I(inode)->lock);
1826 return;
1827 }
1828
1829 /*
Josef Bacikba117212015-03-13 15:01:24 -04001830 * We have to add up either side to figure out how many extents were
1831 * accounted for before we merged into one big extent. If the number of
1832 * extents we accounted for is <= the amount we need for the new range
1833 * then we can return, otherwise drop. Think of it like this
1834 *
1835 * [ 4k][MAX_SIZE]
1836 *
1837 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1838 * need 2 outstanding extents, on one side we have 1 and the other side
1839 * we have 1 so they are == and we can return. But in this case
1840 *
1841 * [MAX_SIZE+4k][MAX_SIZE+4k]
1842 *
1843 * Each range on their own accounts for 2 extents, but merged together
1844 * they are only 3 extents worth of accounting, so we need to drop in
1845 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001846 */
Josef Bacikba117212015-03-13 15:01:24 -04001847 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001848 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001849 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001850 num_extents += count_max_extents(old_size);
1851 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001852 return;
1853
Josef Bacik9e0baf62011-07-15 15:16:44 +00001854 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001855 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001856 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001857}
1858
Miao Xieeb73c1b2013-05-15 07:48:22 +00001859static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1860 struct inode *inode)
1861{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001862 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1863
Miao Xieeb73c1b2013-05-15 07:48:22 +00001864 spin_lock(&root->delalloc_lock);
1865 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1866 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1867 &root->delalloc_inodes);
1868 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1869 &BTRFS_I(inode)->runtime_flags);
1870 root->nr_delalloc_inodes++;
1871 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001872 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001873 BUG_ON(!list_empty(&root->delalloc_root));
1874 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001875 &fs_info->delalloc_roots);
1876 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001877 }
1878 }
1879 spin_unlock(&root->delalloc_lock);
1880}
1881
Nikolay Borisov2b877332018-04-27 12:21:51 +03001882
1883void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1884 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001885{
David Sterba3ffbd682018-06-29 10:56:42 +02001886 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001887
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001888 if (!list_empty(&inode->delalloc_inodes)) {
1889 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001890 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001891 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001892 root->nr_delalloc_inodes--;
1893 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001894 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001895 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001896 BUG_ON(list_empty(&root->delalloc_root));
1897 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001898 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001899 }
1900 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001901}
1902
1903static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1904 struct btrfs_inode *inode)
1905{
1906 spin_lock(&root->delalloc_lock);
1907 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001908 spin_unlock(&root->delalloc_lock);
1909}
1910
Chris Masond352ac62008-09-29 15:18:18 -04001911/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001912 * Properly track delayed allocation bytes in the inode and to maintain the
1913 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001914 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001915void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1916 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001917{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001918 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1919
Wang Shilong47059d92014-07-03 18:22:07 +08001920 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1921 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001922 /*
1923 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001924 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001925 * bit, which is only set or cleared with irqs on
1926 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001927 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001928 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001929 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001930 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001931 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001932
Josef Bacik8b62f872017-10-19 14:15:55 -04001933 spin_lock(&BTRFS_I(inode)->lock);
1934 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1935 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001936
Josef Bacik6a3891c2015-03-16 17:38:52 -04001937 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001938 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04001939 return;
1940
Nikolay Borisov104b4e52017-06-20 21:01:20 +03001941 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
1942 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001943 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001944 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08001945 if (*bits & EXTENT_DEFRAG)
1946 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00001947 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00001948 &BTRFS_I(inode)->runtime_flags))
1949 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00001950 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05001951 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001952
1953 if (!(state->state & EXTENT_DELALLOC_NEW) &&
1954 (*bits & EXTENT_DELALLOC_NEW)) {
1955 spin_lock(&BTRFS_I(inode)->lock);
1956 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
1957 state->start;
1958 spin_unlock(&BTRFS_I(inode)->lock);
1959 }
Chris Mason291d6732008-01-29 15:55:23 -05001960}
1961
Chris Masond352ac62008-09-29 15:18:18 -04001962/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001963 * Once a range is no longer delalloc this function ensures that proper
1964 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04001965 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001966void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
1967 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001968{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02001969 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
1970 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08001971 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01001972 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08001973
Filipe Manana4a4b9642017-07-27 19:52:55 +01001974 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
1975 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001976 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01001977 spin_unlock(&inode->lock);
1978 }
Wang Shilong47059d92014-07-03 18:22:07 +08001979
Chris Mason75eff682008-12-15 15:54:40 -05001980 /*
1981 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001982 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001983 * bit, which is only set or cleared with irqs on
1984 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001985 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02001986 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06001987 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04001988
Josef Bacik8b62f872017-10-19 14:15:55 -04001989 spin_lock(&inode->lock);
1990 btrfs_mod_outstanding_extents(inode, -num_extents);
1991 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001992
Josef Bacikb6d08f02013-09-27 14:57:43 -04001993 /*
1994 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01001995 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04001996 * error.
1997 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001998 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001999 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002000 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002001
Josef Bacik6a3891c2015-03-16 17:38:52 -04002002 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002003 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002004 return;
2005
Filipe Mananaa315e682017-03-06 23:04:20 +00002006 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2007 do_list && !(state->state & EXTENT_NORESERVE) &&
2008 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002009 btrfs_free_reserved_data_space_noquota(
2010 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08002011 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002012
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002013 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2014 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002015 spin_lock(&inode->lock);
2016 inode->delalloc_bytes -= len;
2017 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002018 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002019 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002020 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002021 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002022 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002023
2024 if ((state->state & EXTENT_DELALLOC_NEW) &&
2025 (*bits & EXTENT_DELALLOC_NEW)) {
2026 spin_lock(&inode->lock);
2027 ASSERT(inode->new_delalloc_bytes >= len);
2028 inode->new_delalloc_bytes -= len;
2029 spin_unlock(&inode->lock);
2030 }
Chris Mason291d6732008-01-29 15:55:23 -05002031}
2032
Chris Masond352ac62008-09-29 15:18:18 -04002033/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002034 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2035 * in a chunk's stripe. This function ensures that bios do not span a
2036 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002037 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002038 * @page - The page we are about to add to the bio
2039 * @size - size we want to add to the bio
2040 * @bio - bio we want to ensure is smaller than a stripe
2041 * @bio_flags - flags of the bio
2042 *
2043 * return 1 if page cannot be added to the bio
2044 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002045 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002046 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002047int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2048 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002049{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002050 struct inode *inode = page->mapping->host;
2051 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002052 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002053 u64 length = 0;
2054 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002055 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002056 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002057
Chris Mason771ed682008-11-06 22:02:51 -05002058 if (bio_flags & EXTENT_BIO_COMPRESSED)
2059 return 0;
2060
Kent Overstreet4f024f32013-10-11 15:44:27 -07002061 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002062 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002063 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2064 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002065 if (ret < 0)
2066 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002067
2068 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002069 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002070 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002071}
2072
Chris Masond352ac62008-09-29 15:18:18 -04002073/*
2074 * in order to insert checksums into the metadata in large chunks,
2075 * we wait until bio submission time. All the pages in the bio are
2076 * checksummed and sums are attached onto the ordered extent record.
2077 *
2078 * At IO completion time the cums attached on the ordered extent record
2079 * are inserted into the btree
2080 */
David Sterbad0ee3932018-03-08 14:35:48 +01002081static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002082 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002083{
Josef Bacikc6100a42017-05-05 11:57:13 -04002084 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002085 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002086
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002087 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002088 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002089 return 0;
2090}
Chris Masone0156402008-04-16 11:15:20 -04002091
Chris Mason4a69a412008-11-06 22:03:00 -05002092/*
Chris Masoncad321a2008-12-17 14:51:42 -05002093 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002094 * on write, or reading the csums from the tree before a read.
2095 *
2096 * Rules about async/sync submit,
2097 * a) read: sync submit
2098 *
2099 * b) write without checksum: sync submit
2100 *
2101 * c) write with checksum:
2102 * c-1) if bio is issued by fsync: sync submit
2103 * (sync_writers != 0)
2104 *
2105 * c-2) if root is reloc root: sync submit
2106 * (only in case of buffered IO)
2107 *
2108 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002109 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002110static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002111 int mirror_num,
2112 unsigned long bio_flags)
2113
Chris Mason44b8bd72008-04-16 11:14:51 -04002114{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002115 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002116 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302117 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002118 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002119 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002120 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002121
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002122 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002123
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002124 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302125 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002126
Mike Christie37226b22016-06-05 14:31:52 -05002127 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002128 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002129 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002130 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002131
Chris Masond20f7042008-12-08 16:58:54 -05002132 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002133 ret = btrfs_submit_compressed_read(inode, bio,
2134 mirror_num,
2135 bio_flags);
2136 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002137 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002138 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002139 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002140 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002141 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002142 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002143 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002144 /* csum items have already been cloned */
2145 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2146 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002147 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002148 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002149 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002150 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002151 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002152 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002153 if (ret)
2154 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002155 }
2156
Chris Mason0b86a832008-03-24 15:01:56 -04002157mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002158 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002159
2160out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002161 if (ret) {
2162 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002163 bio_endio(bio);
2164 }
Stefan Behrens61891922012-11-05 18:51:52 +01002165 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002166}
Chris Mason6885f302008-02-20 16:11:05 -05002167
Chris Masond352ac62008-09-29 15:18:18 -04002168/*
2169 * given a list of ordered sums record them in the inode. This happens
2170 * at IO completion time based on sums calculated at bio submission time.
2171 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002172static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002173 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002174{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002175 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002176 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002177
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002178 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002179 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002180 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002181 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002182 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002183 if (ret)
2184 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002185 }
2186 return 0;
2187}
2188
Josef Bacik2ac55d42010-02-03 19:33:23 +00002189int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002190 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002191 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002192{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002193 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002194 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002195 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002196}
2197
Chris Masond352ac62008-09-29 15:18:18 -04002198/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002199struct btrfs_writepage_fixup {
2200 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002201 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002202 struct btrfs_work work;
2203};
2204
Christoph Hellwigb2950862008-12-02 09:54:17 -05002205static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002206{
2207 struct btrfs_writepage_fixup *fixup;
2208 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002209 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002210 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002211 struct page *page;
2212 struct inode *inode;
2213 u64 page_start;
2214 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002215 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002216 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002217
2218 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2219 page = fixup->page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002220 inode = fixup->inode;
2221 page_start = page_offset(page);
2222 page_end = page_offset(page) + PAGE_SIZE - 1;
2223
2224 /*
2225 * This is similar to page_mkwrite, we need to reserve the space before
2226 * we take the page lock.
2227 */
2228 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2229 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002230again:
Chris Mason247e7432008-07-17 12:53:51 -04002231 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002232
Chris Mason25f3c502020-01-21 11:51:42 -05002233 /*
2234 * Before we queued this fixup, we took a reference on the page.
2235 * page->mapping may go NULL, but it shouldn't be moved to a different
2236 * address space.
2237 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002238 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2239 /*
2240 * Unfortunately this is a little tricky, either
2241 *
2242 * 1) We got here and our page had already been dealt with and
2243 * we reserved our space, thus ret == 0, so we need to just
2244 * drop our space reservation and bail. This can happen the
2245 * first time we come into the fixup worker, or could happen
2246 * while waiting for the ordered extent.
2247 * 2) Our page was already dealt with, but we happened to get an
2248 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2249 * this case we obviously don't have anything to release, but
2250 * because the page was already dealt with we don't want to
2251 * mark the page with an error, so make sure we're resetting
2252 * ret to 0. This is why we have this check _before_ the ret
2253 * check, because we do not want to have a surprise ENOSPC
2254 * when the page was already properly dealt with.
2255 */
2256 if (!ret) {
2257 btrfs_delalloc_release_extents(BTRFS_I(inode),
2258 PAGE_SIZE);
2259 btrfs_delalloc_release_space(inode, data_reserved,
2260 page_start, PAGE_SIZE,
2261 true);
2262 }
2263 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002264 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002265 }
Chris Mason25f3c502020-01-21 11:51:42 -05002266
2267 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002268 * We can't mess with the page state unless it is locked, so now that
2269 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002270 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002271 if (ret)
2272 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002273
David Sterbaff13db42015-12-03 14:30:40 +01002274 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002275 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002276
2277 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002278 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002279 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002280
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002281 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002282 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002283 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002284 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002285 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002286 unlock_page(page);
2287 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002288 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002289 goto again;
2290 }
Chris Mason247e7432008-07-17 12:53:51 -04002291
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002292 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002293 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002294 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002295 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002296
Chris Mason25f3c502020-01-21 11:51:42 -05002297 /*
2298 * Everything went as planned, we're now the owner of a dirty page with
2299 * delayed allocation bits set and space reserved for our COW
2300 * destination.
2301 *
2302 * The page was dirty when we started, nothing should have cleaned it.
2303 */
2304 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002305 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002306out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002307 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002308 if (free_delalloc_space)
Filipe Manana53687002019-10-09 17:43:59 +01002309 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2310 PAGE_SIZE, true);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002311 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002312 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002313out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002314 if (ret) {
2315 /*
2316 * We hit ENOSPC or other errors. Update the mapping and page
2317 * to reflect the errors and clean the page.
2318 */
2319 mapping_set_error(page->mapping, ret);
2320 end_extent_writepage(page, ret, page_start, page_end);
2321 clear_page_dirty_for_io(page);
2322 SetPageError(page);
2323 }
2324 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002325 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002326 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002327 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002328 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002329 /*
2330 * As a precaution, do a delayed iput in case it would be the last iput
2331 * that could need flushing space. Recursing back to fixup worker would
2332 * deadlock.
2333 */
2334 btrfs_add_delayed_iput(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002335}
2336
2337/*
2338 * There are a few paths in the higher layers of the kernel that directly
2339 * set the page dirty bit without asking the filesystem if it is a
2340 * good idea. This causes problems because we want to make sure COW
2341 * properly happens and the data=ordered rules are followed.
2342 *
Chris Masonc8b97812008-10-29 14:49:59 -04002343 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002344 * hasn't been properly setup for IO. We kick off an async process
2345 * to fix it up. The async helper will wait for ordered extents, set
2346 * the delalloc bit and make it safe to write the page.
2347 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002348int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002349{
2350 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002351 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002352 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002353
Chris Mason8b62b722009-09-02 16:53:46 -04002354 /* this page is properly in the ordered list */
2355 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002356 return 0;
2357
Chris Mason25f3c502020-01-21 11:51:42 -05002358 /*
2359 * PageChecked is set below when we create a fixup worker for this page,
2360 * don't try to create another one if we're already PageChecked()
2361 *
2362 * The extent_io writepage code will redirty the page if we send back
2363 * EAGAIN.
2364 */
Chris Mason247e7432008-07-17 12:53:51 -04002365 if (PageChecked(page))
2366 return -EAGAIN;
2367
2368 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2369 if (!fixup)
2370 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002371
Josef Bacikf4b13632020-01-21 14:34:52 -05002372 /*
2373 * We are already holding a reference to this inode from
2374 * write_cache_pages. We need to hold it because the space reservation
2375 * takes place outside of the page lock, and we can't trust
2376 * page->mapping outside of the page lock.
2377 */
2378 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002379 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002380 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002381 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002382 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002383 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002384 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002385
2386 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002387}
2388
Yan Zhengd899e052008-10-30 14:25:28 -04002389static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2390 struct inode *inode, u64 file_pos,
2391 u64 disk_bytenr, u64 disk_num_bytes,
2392 u64 num_bytes, u64 ram_bytes,
2393 u8 compression, u8 encryption,
2394 u16 other_encoding, int extent_type)
2395{
2396 struct btrfs_root *root = BTRFS_I(inode)->root;
2397 struct btrfs_file_extent_item *fi;
2398 struct btrfs_path *path;
2399 struct extent_buffer *leaf;
2400 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002401 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002402 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002403 int ret;
2404
2405 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002406 if (!path)
2407 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002408
Chris Masona1ed8352009-09-11 12:27:37 -04002409 /*
2410 * we may be replacing one extent in the tree with another.
2411 * The new extent is pinned in the extent map, and we don't want
2412 * to drop it from the cache until it is completely in the btree.
2413 *
2414 * So, tell btrfs_drop_extents to leave this extent in the cache.
2415 * the caller is expected to unpin it and allow it to be merged
2416 * with the others.
2417 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002418 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2419 file_pos + num_bytes, NULL, 0,
2420 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002421 if (ret)
2422 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002423
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002424 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002425 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002426 ins.offset = file_pos;
2427 ins.type = BTRFS_EXTENT_DATA_KEY;
2428
2429 path->leave_spinning = 1;
2430 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2431 sizeof(*fi));
2432 if (ret)
2433 goto out;
2434 }
Yan Zhengd899e052008-10-30 14:25:28 -04002435 leaf = path->nodes[0];
2436 fi = btrfs_item_ptr(leaf, path->slots[0],
2437 struct btrfs_file_extent_item);
2438 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2439 btrfs_set_file_extent_type(leaf, fi, extent_type);
2440 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2441 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2442 btrfs_set_file_extent_offset(leaf, fi, 0);
2443 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2444 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2445 btrfs_set_file_extent_compression(leaf, fi, compression);
2446 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2447 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002448
Yan Zhengd899e052008-10-30 14:25:28 -04002449 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002450 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002451
2452 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002453
2454 ins.objectid = disk_bytenr;
2455 ins.offset = disk_num_bytes;
2456 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002457
Josef Bacik9ddc9592020-01-17 09:02:22 -05002458 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), file_pos,
2459 ram_bytes);
2460 if (ret)
2461 goto out;
2462
Qu Wenruo297d7502015-09-08 17:08:37 +08002463 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002464 * Release the reserved range from inode dirty range map, as it is
2465 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002466 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002467 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2468 if (ret < 0)
2469 goto out;
2470 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002471 ret = btrfs_alloc_reserved_file_extent(trans, root,
2472 btrfs_ino(BTRFS_I(inode)),
2473 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002474out:
Yan Zhengd899e052008-10-30 14:25:28 -04002475 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002476
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002477 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002478}
2479
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002480static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002481 u64 start, u64 len)
2482{
David Sterba32da53862019-10-29 19:20:18 +01002483 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002484
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002485 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002486 ASSERT(cache);
2487
2488 spin_lock(&cache->lock);
2489 cache->delalloc_bytes -= len;
2490 spin_unlock(&cache->lock);
2491
2492 btrfs_put_block_group(cache);
2493}
2494
Chris Masond352ac62008-09-29 15:18:18 -04002495/* as ordered data IO finishes, this gets called so we can finish
2496 * an ordered extent if the range of bytes in the file it covers are
2497 * fully written.
2498 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002499static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002500{
Josef Bacik5fd02042012-05-02 14:00:54 -04002501 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002502 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002503 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002504 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002505 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002506 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002507 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002508 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002509 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002510 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002511 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002512 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002513 bool range_locked = false;
2514 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002515 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002516 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002517
Omar Sandovalbffe6332019-12-02 17:34:19 -08002518 start = ordered_extent->file_offset;
2519 end = start + ordered_extent->num_bytes - 1;
2520
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002521 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2522 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2523 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2524 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002525
Nikolay Borisov8d510122019-10-08 20:43:06 +03002526 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002527
Josef Bacik5fd02042012-05-02 14:00:54 -04002528 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2529 ret = -EIO;
2530 goto out;
2531 }
2532
Omar Sandovalbffe6332019-12-02 17:34:19 -08002533 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002534
Josef Bacik77cef2e2013-08-29 13:57:21 -04002535 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2536 truncated = true;
2537 logical_len = ordered_extent->truncated_len;
2538 /* Truncated the entire extent, don't bother adding */
2539 if (!logical_len)
2540 goto out;
2541 }
2542
Yan, Zhengc2167752009-11-12 09:34:21 +00002543 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002544 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002545
2546 /*
2547 * For mwrite(mmap + memset to write) case, we still reserve
2548 * space for NOCOW range.
2549 * As NOCOW won't cause a new delayed ref, just free the space
2550 */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002551 btrfs_qgroup_free_data(inode, NULL, start,
2552 ordered_extent->num_bytes);
Josef Bacik6c760c02012-11-09 10:53:21 -05002553 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002554 if (freespace_inode)
2555 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002556 else
2557 trans = btrfs_join_transaction(root);
2558 if (IS_ERR(trans)) {
2559 ret = PTR_ERR(trans);
2560 trans = NULL;
2561 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002562 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002563 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002564 ret = btrfs_update_inode_fallback(trans, root, inode);
2565 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002566 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002567 goto out;
2568 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002569
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002570 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002571 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002572
Nikolay Borisov8d510122019-10-08 20:43:06 +03002573 if (freespace_inode)
2574 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002575 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002576 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002577 if (IS_ERR(trans)) {
2578 ret = PTR_ERR(trans);
2579 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002580 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002581 }
Chris Masona79b7d42014-05-22 16:18:52 -07002582
Josef Bacik69fe2d72017-10-19 14:15:57 -04002583 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002584
Chris Masonc8b97812008-10-29 14:49:59 -04002585 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002586 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002587 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002588 BUG_ON(compress_type);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002589 btrfs_qgroup_free_data(inode, NULL, start,
2590 ordered_extent->num_bytes);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002591 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002592 ordered_extent->file_offset,
2593 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002594 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002595 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002596 BUG_ON(root == fs_info->tree_root);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002597 ret = insert_reserved_file_extent(trans, inode, start,
2598 ordered_extent->disk_bytenr,
2599 ordered_extent->disk_num_bytes,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002600 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002601 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002602 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04002603 if (!ret) {
2604 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002605 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002606 ordered_extent->disk_bytenr,
2607 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002608 }
Yan Zhengd899e052008-10-30 14:25:28 -04002609 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002610 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002611 ordered_extent->file_offset,
2612 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002613 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002614 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002615 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002616 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002617
Nikolay Borisovac01f262018-01-08 10:59:43 +02002618 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2619 if (ret) {
2620 btrfs_abort_transaction(trans, ret);
2621 goto out;
2622 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002623
Josef Bacik6c760c02012-11-09 10:53:21 -05002624 btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2625 ret = btrfs_update_inode_fallback(trans, root, inode);
2626 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002627 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002628 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002629 }
2630 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002631out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002632 clear_bits = EXTENT_DEFRAG;
2633 if (range_locked)
2634 clear_bits |= EXTENT_LOCKED;
2635 if (clear_new_delalloc_bytes)
2636 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002637 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2638 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002639 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002640
Miao Xiea698d0752012-09-20 01:51:59 -06002641 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002642 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002643
Josef Bacik77cef2e2013-08-29 13:57:21 -04002644 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002645 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002646
2647 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002648 unwritten_start += logical_len;
2649 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002650
2651 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002652 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002653
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002654 /*
2655 * If the ordered extent had an IOERR or something else went
2656 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002657 * back to the allocator. We only free the extent in the
2658 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002659 *
2660 * If we made it past insert_reserved_file_extent before we
2661 * errored out then we don't need to do this as the accounting
2662 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002663 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002664 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002665 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002666 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002667 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2668 /*
2669 * Discard the range before returning it back to the
2670 * free space pool
2671 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002672 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002673 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002674 ordered_extent->disk_bytenr,
2675 ordered_extent->disk_num_bytes,
2676 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002677 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002678 ordered_extent->disk_bytenr,
2679 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002680 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002681 }
2682
Josef Bacik5fd02042012-05-02 14:00:54 -04002683 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002684 * This needs to be done to make sure anybody waiting knows we are done
2685 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002686 */
2687 btrfs_remove_ordered_extent(inode, ordered_extent);
2688
Chris Masone6dcd2d2008-07-17 12:53:50 -04002689 /* once for us */
2690 btrfs_put_ordered_extent(ordered_extent);
2691 /* once for the tree */
2692 btrfs_put_ordered_extent(ordered_extent);
2693
Josef Bacik5fd02042012-05-02 14:00:54 -04002694 return ret;
2695}
2696
2697static void finish_ordered_fn(struct btrfs_work *work)
2698{
2699 struct btrfs_ordered_extent *ordered_extent;
2700 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2701 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002702}
2703
Nikolay Borisovc6297322018-11-08 10:18:08 +02002704void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2705 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002706{
Josef Bacik5fd02042012-05-02 14:00:54 -04002707 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002708 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002709 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002710 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002711
liubo1abe9b82011-03-24 11:18:59 +00002712 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2713
Chris Mason8b62b722009-09-02 16:53:46 -04002714 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002715 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2716 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002717 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002718
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002719 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002720 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002721 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002722 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002723
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002724 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002725 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002726}
2727
Miao Xiedc380ae2014-09-12 18:43:55 +08002728static int __readpage_endio_check(struct inode *inode,
2729 struct btrfs_io_bio *io_bio,
2730 int icsum, struct page *page,
2731 int pgoff, u64 start, size_t len)
2732{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2734 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002735 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002736 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2737 u8 *csum_expected;
2738 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002739
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002740 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002741
2742 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002743 shash->tfm = fs_info->csum_shash;
2744
2745 crypto_shash_init(shash);
2746 crypto_shash_update(shash, kaddr + pgoff, len);
2747 crypto_shash_final(shash, csum);
2748
2749 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002750 goto zeroit;
2751
2752 kunmap_atomic(kaddr);
2753 return 0;
2754zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002755 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2756 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002757 memset(kaddr + pgoff, 1, len);
2758 flush_dcache_page(page);
2759 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002760 return -EIO;
2761}
2762
Chris Masond352ac62008-09-29 15:18:18 -04002763/*
Chris Masond352ac62008-09-29 15:18:18 -04002764 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002765 * if there's a match, we allow the bio to finish. If not, the code in
2766 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002767 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002768static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2769 u64 phy_offset, struct page *page,
2770 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002771{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002772 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002773 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002774 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002775 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002776
Chris Masond20f7042008-12-08 16:58:54 -05002777 if (PageChecked(page)) {
2778 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002779 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002780 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002781
2782 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002783 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002784
Yan Zheng17d217f2008-12-12 10:03:38 -05002785 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002786 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002787 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002788 return 0;
2789 }
2790
Miao Xiefacc8a222013-07-25 19:22:34 +08002791 phy_offset >>= inode->i_sb->s_blocksize_bits;
Miao Xiedc380ae2014-09-12 18:43:55 +08002792 return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
2793 start, (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002794}
Chris Masonb888db22007-08-27 16:49:44 -04002795
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002796/*
2797 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2798 *
2799 * @inode: The inode we want to perform iput on
2800 *
2801 * This function uses the generic vfs_inode::i_count to track whether we should
2802 * just decrement it (in case it's > 1) or if this is the last iput then link
2803 * the inode to the delayed iput machinery. Delayed iputs are processed at
2804 * transaction commit time/superblock commit/cleaner kthread.
2805 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002806void btrfs_add_delayed_iput(struct inode *inode)
2807{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002808 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002809 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002810
2811 if (atomic_add_unless(&inode->i_count, -1, 1))
2812 return;
2813
Josef Bacik034f7842018-12-03 11:06:52 -05002814 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002815 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002816 ASSERT(list_empty(&binode->delayed_iput));
2817 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002818 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002819 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2820 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002821}
2822
Josef Bacik63611e72019-06-18 10:59:18 -04002823static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2824 struct btrfs_inode *inode)
2825{
2826 list_del_init(&inode->delayed_iput);
2827 spin_unlock(&fs_info->delayed_iput_lock);
2828 iput(&inode->vfs_inode);
2829 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2830 wake_up(&fs_info->delayed_iputs_wait);
2831 spin_lock(&fs_info->delayed_iput_lock);
2832}
2833
2834static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2835 struct btrfs_inode *inode)
2836{
2837 if (!list_empty(&inode->delayed_iput)) {
2838 spin_lock(&fs_info->delayed_iput_lock);
2839 if (!list_empty(&inode->delayed_iput))
2840 run_delayed_iput_locked(fs_info, inode);
2841 spin_unlock(&fs_info->delayed_iput_lock);
2842 }
2843}
2844
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002845void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002846{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002847
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002848 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002849 while (!list_empty(&fs_info->delayed_iputs)) {
2850 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002851
David Sterba8089fe62015-11-19 14:15:51 +01002852 inode = list_first_entry(&fs_info->delayed_iputs,
2853 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002854 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002855 }
David Sterba8089fe62015-11-19 14:15:51 +01002856 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002857}
2858
Josef Bacik034f7842018-12-03 11:06:52 -05002859/**
2860 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2861 * @fs_info - the fs_info for this fs
2862 * @return - EINTR if we were killed, 0 if nothing's pending
2863 *
2864 * This will wait on any delayed iputs that are currently running with KILLABLE
2865 * set. Once they are all done running we will return, unless we are killed in
2866 * which case we return EINTR. This helps in user operations like fallocate etc
2867 * that might get blocked on the iputs.
2868 */
2869int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2870{
2871 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2872 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2873 if (ret)
2874 return -EINTR;
2875 return 0;
2876}
2877
Yan, Zhengd68fc572010-05-16 10:49:58 -04002878/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002879 * This creates an orphan entry for the given inode in case something goes wrong
2880 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002881 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002882int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002883 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002884{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002885 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002886
Omar Sandoval27919062018-05-11 13:13:37 -07002887 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2888 if (ret && ret != -EEXIST) {
2889 btrfs_abort_transaction(trans, ret);
2890 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002891 }
2892
Yan, Zhengd68fc572010-05-16 10:49:58 -04002893 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002894}
2895
2896/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002897 * We have done the delete so we can go ahead and remove the orphan item for
2898 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002899 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002900static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002901 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002902{
Omar Sandoval27919062018-05-11 13:13:37 -07002903 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002904}
2905
2906/*
2907 * this cleans up any orphans that may be left on the list from the last use
2908 * of this root.
2909 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002910int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002911{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002912 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002913 struct btrfs_path *path;
2914 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002915 struct btrfs_key key, found_key;
2916 struct btrfs_trans_handle *trans;
2917 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002918 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002919 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002920
Yan, Zhengd68fc572010-05-16 10:49:58 -04002921 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002922 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002923
2924 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002925 if (!path) {
2926 ret = -ENOMEM;
2927 goto out;
2928 }
David Sterbae4058b52015-11-27 16:31:35 +01002929 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002930
2931 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002932 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04002933 key.offset = (u64)-1;
2934
Josef Bacik7b128762008-07-24 12:17:14 -04002935 while (1) {
2936 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002937 if (ret < 0)
2938 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002939
2940 /*
2941 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002942 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04002943 * find the key and see if we have stuff that matches
2944 */
2945 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002946 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002947 if (path->slots[0] == 0)
2948 break;
2949 path->slots[0]--;
2950 }
2951
2952 /* pull out the item */
2953 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04002954 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2955
2956 /* make sure the item matches what we want */
2957 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2958 break;
David Sterba962a2982014-06-04 18:41:45 +02002959 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04002960 break;
2961
2962 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02002963 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04002964
2965 /*
2966 * this is where we are basically btrfs_lookup, without the
2967 * crossing root thing. we store the inode number in the
2968 * offset of the orphan item.
2969 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002970
2971 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002972 btrfs_err(fs_info,
2973 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002974 ret = -EINVAL;
2975 goto out;
2976 }
2977
2978 last_objectid = found_key.offset;
2979
Yan Zheng5d4f98a2009-06-10 10:45:14 -04002980 found_key.objectid = found_key.offset;
2981 found_key.type = BTRFS_INODE_ITEM_KEY;
2982 found_key.offset = 0;
David Sterba4c66e0d2019-10-03 19:09:35 +02002983 inode = btrfs_iget(fs_info->sb, &found_key, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09302984 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01002985 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002986 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04002987
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002988 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05002989 struct btrfs_root *dead_root;
2990 struct btrfs_fs_info *fs_info = root->fs_info;
2991 int is_dead_root = 0;
2992
2993 /*
2994 * this is an orphan in the tree root. Currently these
2995 * could come from 2 sources:
2996 * a) a snapshot deletion in progress
2997 * b) a free space cache inode
2998 * We need to distinguish those two, as the snapshot
2999 * orphan must not get deleted.
3000 * find_dead_roots already ran before us, so if this
3001 * is a snapshot deletion, we should find the root
3002 * in the dead_roots list
3003 */
3004 spin_lock(&fs_info->trans_lock);
3005 list_for_each_entry(dead_root, &fs_info->dead_roots,
3006 root_list) {
3007 if (dead_root->root_key.objectid ==
3008 found_key.objectid) {
3009 is_dead_root = 1;
3010 break;
3011 }
3012 }
3013 spin_unlock(&fs_info->trans_lock);
3014 if (is_dead_root) {
3015 /* prevent this orphan from being found again */
3016 key.offset = found_key.objectid - 1;
3017 continue;
3018 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003019
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003020 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003021
Josef Bacika8c9e572011-09-21 16:55:59 -04003022 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003023 * If we have an inode with links, there are a couple of
3024 * possibilities. Old kernels (before v3.12) used to create an
3025 * orphan item for truncate indicating that there were possibly
3026 * extent items past i_size that needed to be deleted. In v3.12,
3027 * truncate was changed to update i_size in sync with the extent
3028 * items, but the (useless) orphan item was still created. Since
3029 * v4.18, we don't create the orphan item for truncate at all.
3030 *
3031 * So, this item could mean that we need to do a truncate, but
3032 * only if this filesystem was last used on a pre-v3.12 kernel
3033 * and was not cleanly unmounted. The odds of that are quite
3034 * slim, and it's a pain to do the truncate now, so just delete
3035 * the orphan item.
3036 *
3037 * It's also possible that this orphan item was supposed to be
3038 * deleted but wasn't. The inode number may have been reused,
3039 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003040 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003041 if (ret == -ENOENT || inode->i_nlink) {
3042 if (!ret)
3043 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003044 trans = btrfs_start_transaction(root, 1);
3045 if (IS_ERR(trans)) {
3046 ret = PTR_ERR(trans);
3047 goto out;
3048 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003049 btrfs_debug(fs_info, "auto deleting %Lu",
3050 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003051 ret = btrfs_del_orphan_item(trans, root,
3052 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003053 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003054 if (ret)
3055 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003056 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003057 }
Josef Bacik7b128762008-07-24 12:17:14 -04003058
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003059 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003060
3061 /* this will do delete_inode and everything for us */
3062 iput(inode);
3063 }
Miao Xie3254c872011-11-10 20:45:05 -05003064 /* release the path since we're done with it */
3065 btrfs_release_path(path);
3066
Yan, Zhengd68fc572010-05-16 10:49:58 -04003067 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3068
Omar Sandovala575cee2018-05-11 13:13:38 -07003069 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003070 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003071 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003072 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003073 }
Josef Bacik7b128762008-07-24 12:17:14 -04003074
3075 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003076 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003077
3078out:
3079 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003080 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003081 btrfs_free_path(path);
3082 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003083}
3084
Chris Masond352ac62008-09-29 15:18:18 -04003085/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003086 * very simple check to peek ahead in the leaf looking for xattrs. If we
3087 * don't find any xattrs, we know there can't be any acls.
3088 *
3089 * slot is the slot the inode is in, objectid is the objectid of the inode
3090 */
3091static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003092 int slot, u64 objectid,
3093 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003094{
3095 u32 nritems = btrfs_header_nritems(leaf);
3096 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003097 static u64 xattr_access = 0;
3098 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003099 int scanned = 0;
3100
Josef Bacikf23b5a52013-06-19 10:16:26 -04003101 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003102 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3103 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3104 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3105 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003106 }
3107
Chris Mason46a53cc2009-04-27 11:47:50 -04003108 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003109 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003110 while (slot < nritems) {
3111 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3112
3113 /* we found a different objectid, there must not be acls */
3114 if (found_key.objectid != objectid)
3115 return 0;
3116
3117 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003118 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003119 if (*first_xattr_slot == -1)
3120 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003121 if (found_key.offset == xattr_access ||
3122 found_key.offset == xattr_default)
3123 return 1;
3124 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003125
3126 /*
3127 * we found a key greater than an xattr key, there can't
3128 * be any acls later on
3129 */
3130 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3131 return 0;
3132
3133 slot++;
3134 scanned++;
3135
3136 /*
3137 * it goes inode, inode backrefs, xattrs, extents,
3138 * so if there are a ton of hard links to an inode there can
3139 * be a lot of backrefs. Don't waste time searching too hard,
3140 * this is just an optimization
3141 */
3142 if (scanned >= 8)
3143 break;
3144 }
3145 /* we hit the end of the leaf before we found an xattr or
3146 * something larger than an xattr. We have to assume the inode
3147 * has acls
3148 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003149 if (*first_xattr_slot == -1)
3150 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003151 return 1;
3152}
3153
3154/*
Chris Masond352ac62008-09-29 15:18:18 -04003155 * read an inode from the btree into the in-memory inode
3156 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003157static int btrfs_read_locked_inode(struct inode *inode,
3158 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003160 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003161 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003162 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003163 struct btrfs_inode_item *inode_item;
3164 struct btrfs_root *root = BTRFS_I(inode)->root;
3165 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003166 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003167 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003168 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003169 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003170 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003171 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003172
3173 ret = btrfs_fill_inode(inode, &rdev);
3174 if (!ret)
3175 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003176
Filipe Manana4222ea72018-10-24 10:13:03 +01003177 if (!path) {
3178 path = btrfs_alloc_path();
3179 if (!path)
3180 return -ENOMEM;
3181 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003182
Chris Mason39279cc2007-06-12 06:35:45 -04003183 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003184
Chris Mason39279cc2007-06-12 06:35:45 -04003185 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003186 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003187 if (path != in_path)
3188 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003189 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003190 }
Chris Mason39279cc2007-06-12 06:35:45 -04003191
Chris Mason5f39d392007-10-15 16:14:19 -04003192 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003193
3194 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003195 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003196
Chris Mason5f39d392007-10-15 16:14:19 -04003197 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3198 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003199 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003200 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003201 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3202 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003203 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003204 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3205 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003206
David Sterbaa937b972014-12-12 17:39:12 +01003207 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3208 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003209
David Sterbaa937b972014-12-12 17:39:12 +01003210 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3211 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003212
David Sterbaa937b972014-12-12 17:39:12 +01003213 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3214 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003215
chandan r9cc97d62012-07-04 12:48:07 +05303216 BTRFS_I(inode)->i_otime.tv_sec =
3217 btrfs_timespec_sec(leaf, &inode_item->otime);
3218 BTRFS_I(inode)->i_otime.tv_nsec =
3219 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003220
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003221 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003222 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003223 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3224
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003225 inode_set_iversion_queried(inode,
3226 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003227 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003228 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003229 rdev = btrfs_inode_rdev(leaf, inode_item);
3230
Josef Bacikaec74772008-07-24 12:12:38 -04003231 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003232 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003233
3234cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003235 /*
3236 * If we were modified in the current generation and evicted from memory
3237 * and then re-read we need to do a full sync since we don't have any
3238 * idea about which extents were modified before we were evicted from
3239 * cache.
3240 *
3241 * This is required for both inode re-read from disk and delayed inode
3242 * in delayed_nodes_tree.
3243 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003244 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003245 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3246 &BTRFS_I(inode)->runtime_flags);
3247
Filipe Mananabde6c242015-07-24 00:00:19 +01003248 /*
3249 * We don't persist the id of the transaction where an unlink operation
3250 * against the inode was last made. So here we assume the inode might
3251 * have been evicted, and therefore the exact value of last_unlink_trans
3252 * lost, and set it to last_trans to avoid metadata inconsistencies
3253 * between the inode and its parent if the inode is fsync'ed and the log
3254 * replayed. For example, in the scenario:
3255 *
3256 * touch mydir/foo
3257 * ln mydir/foo mydir/bar
3258 * sync
3259 * unlink mydir/bar
3260 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3261 * xfs_io -c fsync mydir/foo
3262 * <power failure>
3263 * mount fs, triggers fsync log replay
3264 *
3265 * We must make sure that when we fsync our inode foo we also log its
3266 * parent inode, otherwise after log replay the parent still has the
3267 * dentry with the "bar" name but our inode foo has a link count of 1
3268 * and doesn't have an inode ref with the name "bar" anymore.
3269 *
3270 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003271 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003272 * transaction commits on fsync if our inode is a directory, or if our
3273 * inode is not a directory, logging its parent unnecessarily.
3274 */
3275 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3276
Miao Xie67de1172013-12-26 13:07:06 +08003277 path->slots[0]++;
3278 if (inode->i_nlink != 1 ||
3279 path->slots[0] >= btrfs_header_nritems(leaf))
3280 goto cache_acl;
3281
3282 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003283 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003284 goto cache_acl;
3285
3286 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3287 if (location.type == BTRFS_INODE_REF_KEY) {
3288 struct btrfs_inode_ref *ref;
3289
3290 ref = (struct btrfs_inode_ref *)ptr;
3291 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3292 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3293 struct btrfs_inode_extref *extref;
3294
3295 extref = (struct btrfs_inode_extref *)ptr;
3296 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3297 extref);
3298 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003299cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003300 /*
3301 * try to precache a NULL acl entry for files that don't have
3302 * any xattrs or acls
3303 */
Li Zefan33345d012011-04-20 10:31:50 +08003304 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003305 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003306 if (first_xattr_slot != -1) {
3307 path->slots[0] = first_xattr_slot;
3308 ret = btrfs_load_inode_props(inode, path);
3309 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003310 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003311 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003312 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003313 root->root_key.objectid, ret);
3314 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003315 if (path != in_path)
3316 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003317
Al Viro72c04902009-06-24 16:58:48 -04003318 if (!maybe_acls)
3319 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003320
Chris Mason39279cc2007-06-12 06:35:45 -04003321 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003322 case S_IFREG:
3323 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003324 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003325 inode->i_fop = &btrfs_file_operations;
3326 inode->i_op = &btrfs_file_inode_operations;
3327 break;
3328 case S_IFDIR:
3329 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003330 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003331 break;
3332 case S_IFLNK:
3333 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003334 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003335 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003336 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003337 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003338 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003339 init_special_inode(inode, inode->i_mode, rdev);
3340 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003341 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003342
David Sterba7b6a2212018-03-26 18:40:21 +02003343 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003344 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003345}
3346
Chris Masond352ac62008-09-29 15:18:18 -04003347/*
3348 * given a leaf and an inode, copy the inode fields into the leaf
3349 */
Chris Masone02119d2008-09-05 16:13:11 -04003350static void fill_inode_item(struct btrfs_trans_handle *trans,
3351 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003352 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003353 struct inode *inode)
3354{
Liu Bo51fab692012-12-27 09:01:21 +00003355 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003356
David Sterbac82f8232019-08-09 17:48:21 +02003357 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003358
Liu Bo51fab692012-12-27 09:01:21 +00003359 btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3360 btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3361 btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3362 &token);
3363 btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3364 btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003365
David Sterbaa937b972014-12-12 17:39:12 +01003366 btrfs_set_token_timespec_sec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003367 inode->i_atime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003368 btrfs_set_token_timespec_nsec(leaf, &item->atime,
Liu Bo51fab692012-12-27 09:01:21 +00003369 inode->i_atime.tv_nsec, &token);
Chris Mason5f39d392007-10-15 16:14:19 -04003370
David Sterbaa937b972014-12-12 17:39:12 +01003371 btrfs_set_token_timespec_sec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003372 inode->i_mtime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003373 btrfs_set_token_timespec_nsec(leaf, &item->mtime,
Liu Bo51fab692012-12-27 09:01:21 +00003374 inode->i_mtime.tv_nsec, &token);
3375
David Sterbaa937b972014-12-12 17:39:12 +01003376 btrfs_set_token_timespec_sec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003377 inode->i_ctime.tv_sec, &token);
David Sterbaa937b972014-12-12 17:39:12 +01003378 btrfs_set_token_timespec_nsec(leaf, &item->ctime,
Liu Bo51fab692012-12-27 09:01:21 +00003379 inode->i_ctime.tv_nsec, &token);
3380
chandan r9cc97d62012-07-04 12:48:07 +05303381 btrfs_set_token_timespec_sec(leaf, &item->otime,
3382 BTRFS_I(inode)->i_otime.tv_sec, &token);
3383 btrfs_set_token_timespec_nsec(leaf, &item->otime,
3384 BTRFS_I(inode)->i_otime.tv_nsec, &token);
3385
Liu Bo51fab692012-12-27 09:01:21 +00003386 btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3387 &token);
3388 btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3389 &token);
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003390 btrfs_set_token_inode_sequence(leaf, item, inode_peek_iversion(inode),
3391 &token);
Liu Bo51fab692012-12-27 09:01:21 +00003392 btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3393 btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3394 btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3395 btrfs_set_token_inode_block_group(leaf, item, 0, &token);
Chris Mason39279cc2007-06-12 06:35:45 -04003396}
3397
Chris Masond352ac62008-09-29 15:18:18 -04003398/*
3399 * copy everything in the in-memory inode into the btree.
3400 */
Chris Mason21151332011-11-10 20:39:08 -05003401static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003402 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003403{
3404 struct btrfs_inode_item *inode_item;
3405 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003406 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003407 int ret;
3408
3409 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003410 if (!path)
3411 return -ENOMEM;
3412
Chris Masonb9473432009-03-13 11:00:37 -04003413 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003414 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3415 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003416 if (ret) {
3417 if (ret > 0)
3418 ret = -ENOENT;
3419 goto failed;
3420 }
3421
Chris Mason5f39d392007-10-15 16:14:19 -04003422 leaf = path->nodes[0];
3423 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003424 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003425
Chris Masone02119d2008-09-05 16:13:11 -04003426 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003427 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003428 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003429 ret = 0;
3430failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003431 btrfs_free_path(path);
3432 return ret;
3433}
3434
Chris Masond352ac62008-09-29 15:18:18 -04003435/*
Chris Mason21151332011-11-10 20:39:08 -05003436 * copy everything in the in-memory inode into the btree.
3437 */
3438noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3439 struct btrfs_root *root, struct inode *inode)
3440{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003441 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003442 int ret;
3443
3444 /*
3445 * If the inode is a free space inode, we can deadlock during commit
3446 * if we put it into the delayed code.
3447 *
3448 * The data relocation inode should also be directly updated
3449 * without delay
3450 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003451 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003452 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003453 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003454 btrfs_update_root_times(trans, root);
3455
Chris Mason21151332011-11-10 20:39:08 -05003456 ret = btrfs_delayed_update_inode(trans, root, inode);
3457 if (!ret)
3458 btrfs_set_inode_last_trans(trans, inode);
3459 return ret;
3460 }
3461
3462 return btrfs_update_inode_item(trans, root, inode);
3463}
3464
Josef Bacikbe6aef62012-10-22 15:43:12 -04003465noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3466 struct btrfs_root *root,
3467 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003468{
3469 int ret;
3470
3471 ret = btrfs_update_inode(trans, root, inode);
3472 if (ret == -ENOSPC)
3473 return btrfs_update_inode_item(trans, root, inode);
3474 return ret;
3475}
3476
3477/*
Chris Masond352ac62008-09-29 15:18:18 -04003478 * unlink helper that gets used here in inode.c and in the tree logging
3479 * recovery code. It remove a link in a directory with a given name, and
3480 * also drops the back refs in the inode to the directory
3481 */
Al Viro92986792011-03-04 17:14:37 +00003482static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3483 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003484 struct btrfs_inode *dir,
3485 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003486 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003487{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003488 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003489 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003490 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003491 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003492 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003493 u64 ino = btrfs_ino(inode);
3494 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003495
3496 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003497 if (!path) {
3498 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003499 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003500 }
3501
Chris Masonb9473432009-03-13 11:00:37 -04003502 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003503 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003504 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003505 if (IS_ERR_OR_NULL(di)) {
3506 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003507 goto err;
3508 }
Chris Mason39279cc2007-06-12 06:35:45 -04003509 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003510 if (ret)
3511 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003512 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003513
Miao Xie67de1172013-12-26 13:07:06 +08003514 /*
3515 * If we don't have dir index, we have to get it by looking up
3516 * the inode ref, since we get the inode ref, remove it directly,
3517 * it is unnecessary to do delayed deletion.
3518 *
3519 * But if we have dir index, needn't search inode ref to get it.
3520 * Since the inode ref is close to the inode item, it is better
3521 * that we delay to delete it, and just do this deletion when
3522 * we update the inode item.
3523 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003524 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003525 ret = btrfs_delayed_delete_inode_ref(inode);
3526 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003527 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003528 goto skip_backref;
3529 }
3530 }
3531
Li Zefan33345d012011-04-20 10:31:50 +08003532 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3533 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003534 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003535 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003536 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003537 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003538 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003539 goto err;
3540 }
Miao Xie67de1172013-12-26 13:07:06 +08003541skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003542 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003543 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003544 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003545 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003546 }
Chris Mason39279cc2007-06-12 06:35:45 -04003547
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003548 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3549 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003550 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003551 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003552 goto err;
3553 }
Chris Masone02119d2008-09-05 16:13:11 -04003554
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003555 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3556 index);
Chris Mason6418c962010-10-30 07:34:24 -04003557 if (ret == -ENOENT)
3558 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003559 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003560 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003561
3562 /*
3563 * If we have a pending delayed iput we could end up with the final iput
3564 * being run in btrfs-cleaner context. If we have enough of these built
3565 * up we can end up burning a lot of time in btrfs-cleaner without any
3566 * way to throttle the unlinks. Since we're currently holding a ref on
3567 * the inode we can run the delayed iput here without any issues as the
3568 * final iput won't be done until after we drop the ref we're currently
3569 * holding.
3570 */
3571 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003572err:
3573 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003574 if (ret)
3575 goto out;
3576
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003577 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003578 inode_inc_iversion(&inode->vfs_inode);
3579 inode_inc_iversion(&dir->vfs_inode);
3580 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3581 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3582 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003583out:
Chris Mason39279cc2007-06-12 06:35:45 -04003584 return ret;
3585}
3586
Al Viro92986792011-03-04 17:14:37 +00003587int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3588 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003589 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003590 const char *name, int name_len)
3591{
3592 int ret;
3593 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3594 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003595 drop_nlink(&inode->vfs_inode);
3596 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003597 }
3598 return ret;
3599}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003600
3601/*
3602 * helper to start transaction for unlink and rmdir.
3603 *
Josef Bacikd52be812013-05-29 14:54:47 -04003604 * unlink and rmdir are special in btrfs, they do not always free space, so
3605 * if we cannot make our reservations the normal way try and see if there is
3606 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3607 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003608 */
Josef Bacikd52be812013-05-29 14:54:47 -04003609static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003610{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003611 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003612
Josef Bacike70bea52011-10-11 14:18:24 -04003613 /*
3614 * 1 for the possible orphan item
3615 * 1 for the dir item
3616 * 1 for the dir index
3617 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003618 * 1 for the inode
3619 */
Filipe Manana8eab77f2015-11-13 23:57:16 +00003620 return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003621}
3622
Chris Mason39279cc2007-06-12 06:35:45 -04003623static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3624{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003625 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003626 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003627 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003628 int ret;
3629
Josef Bacikd52be812013-05-29 14:54:47 -04003630 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003631 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003632 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003633
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003634 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3635 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003636
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003637 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3638 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3639 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003640 if (ret)
3641 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003642
Yan, Zhenga22285a2010-05-16 10:48:46 -04003643 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003644 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003645 if (ret)
3646 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003647 }
Josef Bacik7b128762008-07-24 12:17:14 -04003648
Tsutomu Itohb5324022011-07-19 07:27:20 +00003649out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003650 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003651 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003652 return ret;
3653}
3654
Misono Tomohirof60a2362018-04-18 11:34:52 +09003655static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003656 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003657{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003658 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003659 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003660 struct btrfs_path *path;
3661 struct extent_buffer *leaf;
3662 struct btrfs_dir_item *di;
3663 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003664 const char *name = dentry->d_name.name;
3665 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003666 u64 index;
3667 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003668 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003669 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003670
Josef Bacik045d3962019-12-18 17:20:27 -05003671 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3672 objectid = inode->root->root_key.objectid;
3673 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3674 objectid = inode->location.objectid;
3675 } else {
3676 WARN_ON(1);
3677 return -EINVAL;
3678 }
3679
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003680 path = btrfs_alloc_path();
3681 if (!path)
3682 return -ENOMEM;
3683
Li Zefan33345d012011-04-20 10:31:50 +08003684 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003685 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003686 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003687 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003688 goto out;
3689 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003690
3691 leaf = path->nodes[0];
3692 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3693 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3694 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003695 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003696 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003697 goto out;
3698 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003699 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003700
Josef Bacikd49d3282019-12-18 17:20:28 -05003701 /*
3702 * This is a placeholder inode for a subvolume we didn't have a
3703 * reference to at the time of the snapshot creation. In the meantime
3704 * we could have renamed the real subvol link into our snapshot, so
3705 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3706 * Instead simply lookup the dir_index_item for this entry so we can
3707 * remove it. Otherwise we know we have a ref to the root and we can
3708 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3709 */
3710 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003711 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003712 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003713 if (IS_ERR_OR_NULL(di)) {
3714 if (!di)
3715 ret = -ENOENT;
3716 else
3717 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003718 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003719 goto out;
3720 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003721
3722 leaf = path->nodes[0];
3723 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003724 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003725 btrfs_release_path(path);
3726 } else {
3727 ret = btrfs_del_root_ref(trans, objectid,
3728 root->root_key.objectid, dir_ino,
3729 &index, name, name_len);
3730 if (ret) {
3731 btrfs_abort_transaction(trans, ret);
3732 goto out;
3733 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003734 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003735
Lu Fengqi9add2942018-08-01 11:32:26 +08003736 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003737 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003738 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003739 goto out;
3740 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003741
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003742 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003743 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003744 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003745 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003746 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003747 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003748out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003749 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003750 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003751}
3752
Misono Tomohiroec42f162018-04-18 11:34:13 +09003753/*
3754 * Helper to check if the subvolume references other subvolumes or if it's
3755 * default.
3756 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003757static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003758{
3759 struct btrfs_fs_info *fs_info = root->fs_info;
3760 struct btrfs_path *path;
3761 struct btrfs_dir_item *di;
3762 struct btrfs_key key;
3763 u64 dir_id;
3764 int ret;
3765
3766 path = btrfs_alloc_path();
3767 if (!path)
3768 return -ENOMEM;
3769
3770 /* Make sure this root isn't set as the default subvol */
3771 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3772 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3773 dir_id, "default", 7, 0);
3774 if (di && !IS_ERR(di)) {
3775 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3776 if (key.objectid == root->root_key.objectid) {
3777 ret = -EPERM;
3778 btrfs_err(fs_info,
3779 "deleting default subvolume %llu is not allowed",
3780 key.objectid);
3781 goto out;
3782 }
3783 btrfs_release_path(path);
3784 }
3785
3786 key.objectid = root->root_key.objectid;
3787 key.type = BTRFS_ROOT_REF_KEY;
3788 key.offset = (u64)-1;
3789
3790 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3791 if (ret < 0)
3792 goto out;
3793 BUG_ON(ret == 0);
3794
3795 ret = 0;
3796 if (path->slots[0] > 0) {
3797 path->slots[0]--;
3798 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3799 if (key.objectid == root->root_key.objectid &&
3800 key.type == BTRFS_ROOT_REF_KEY)
3801 ret = -ENOTEMPTY;
3802 }
3803out:
3804 btrfs_free_path(path);
3805 return ret;
3806}
3807
Nikolay Borisov20a68002018-04-27 14:36:24 +03003808/* Delete all dentries for inodes belonging to the root */
3809static void btrfs_prune_dentries(struct btrfs_root *root)
3810{
3811 struct btrfs_fs_info *fs_info = root->fs_info;
3812 struct rb_node *node;
3813 struct rb_node *prev;
3814 struct btrfs_inode *entry;
3815 struct inode *inode;
3816 u64 objectid = 0;
3817
3818 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3819 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3820
3821 spin_lock(&root->inode_lock);
3822again:
3823 node = root->inode_tree.rb_node;
3824 prev = NULL;
3825 while (node) {
3826 prev = node;
3827 entry = rb_entry(node, struct btrfs_inode, rb_node);
3828
David Sterba37508512018-06-29 10:56:40 +02003829 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003830 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003831 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003832 node = node->rb_right;
3833 else
3834 break;
3835 }
3836 if (!node) {
3837 while (prev) {
3838 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003839 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003840 node = prev;
3841 break;
3842 }
3843 prev = rb_next(prev);
3844 }
3845 }
3846 while (node) {
3847 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003848 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003849 inode = igrab(&entry->vfs_inode);
3850 if (inode) {
3851 spin_unlock(&root->inode_lock);
3852 if (atomic_read(&inode->i_count) > 1)
3853 d_prune_aliases(inode);
3854 /*
3855 * btrfs_drop_inode will have it removed from the inode
3856 * cache when its usage count hits zero.
3857 */
3858 iput(inode);
3859 cond_resched();
3860 spin_lock(&root->inode_lock);
3861 goto again;
3862 }
3863
3864 if (cond_resched_lock(&root->inode_lock))
3865 goto again;
3866
3867 node = rb_next(node);
3868 }
3869 spin_unlock(&root->inode_lock);
3870}
3871
Misono Tomohirof60a2362018-04-18 11:34:52 +09003872int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3873{
3874 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3875 struct btrfs_root *root = BTRFS_I(dir)->root;
3876 struct inode *inode = d_inode(dentry);
3877 struct btrfs_root *dest = BTRFS_I(inode)->root;
3878 struct btrfs_trans_handle *trans;
3879 struct btrfs_block_rsv block_rsv;
3880 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003881 int ret;
3882 int err;
3883
3884 /*
3885 * Don't allow to delete a subvolume with send in progress. This is
3886 * inside the inode lock so the error handling that has to drop the bit
3887 * again is not run concurrently.
3888 */
3889 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003890 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003891 spin_unlock(&dest->root_item_lock);
3892 btrfs_warn(fs_info,
3893 "attempt to delete subvolume %llu during send",
3894 dest->root_key.objectid);
3895 return -EPERM;
3896 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003897 root_flags = btrfs_root_flags(&dest->root_item);
3898 btrfs_set_root_flags(&dest->root_item,
3899 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3900 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003901
3902 down_write(&fs_info->subvol_sem);
3903
3904 err = may_destroy_subvol(dest);
3905 if (err)
3906 goto out_up_write;
3907
3908 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3909 /*
3910 * One for dir inode,
3911 * two for dir entries,
3912 * two for root ref/backref.
3913 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003914 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003915 if (err)
3916 goto out_up_write;
3917
3918 trans = btrfs_start_transaction(root, 0);
3919 if (IS_ERR(trans)) {
3920 err = PTR_ERR(trans);
3921 goto out_release;
3922 }
3923 trans->block_rsv = &block_rsv;
3924 trans->bytes_reserved = block_rsv.size;
3925
3926 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3927
Josef Bacik045d3962019-12-18 17:20:27 -05003928 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003929 if (ret) {
3930 err = ret;
3931 btrfs_abort_transaction(trans, ret);
3932 goto out_end_trans;
3933 }
3934
3935 btrfs_record_root_in_trans(trans, dest);
3936
3937 memset(&dest->root_item.drop_progress, 0,
3938 sizeof(dest->root_item.drop_progress));
3939 dest->root_item.drop_level = 0;
3940 btrfs_set_root_refs(&dest->root_item, 0);
3941
3942 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
3943 ret = btrfs_insert_orphan_item(trans,
3944 fs_info->tree_root,
3945 dest->root_key.objectid);
3946 if (ret) {
3947 btrfs_abort_transaction(trans, ret);
3948 err = ret;
3949 goto out_end_trans;
3950 }
3951 }
3952
Lu Fengqid1957792018-05-29 15:01:54 +08003953 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09003954 BTRFS_UUID_KEY_SUBVOL,
3955 dest->root_key.objectid);
3956 if (ret && ret != -ENOENT) {
3957 btrfs_abort_transaction(trans, ret);
3958 err = ret;
3959 goto out_end_trans;
3960 }
3961 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08003962 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09003963 dest->root_item.received_uuid,
3964 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
3965 dest->root_key.objectid);
3966 if (ret && ret != -ENOENT) {
3967 btrfs_abort_transaction(trans, ret);
3968 err = ret;
3969 goto out_end_trans;
3970 }
3971 }
3972
3973out_end_trans:
3974 trans->block_rsv = NULL;
3975 trans->bytes_reserved = 0;
3976 ret = btrfs_end_transaction(trans);
3977 if (ret && !err)
3978 err = ret;
3979 inode->i_flags |= S_DEAD;
3980out_release:
3981 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
3982out_up_write:
3983 up_write(&fs_info->subvol_sem);
3984 if (err) {
3985 spin_lock(&dest->root_item_lock);
3986 root_flags = btrfs_root_flags(&dest->root_item);
3987 btrfs_set_root_flags(&dest->root_item,
3988 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
3989 spin_unlock(&dest->root_item_lock);
3990 } else {
3991 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03003992 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003993 ASSERT(dest->send_in_progress == 0);
3994
3995 /* the last ref */
3996 if (dest->ino_cache_inode) {
3997 iput(dest->ino_cache_inode);
3998 dest->ino_cache_inode = NULL;
3999 }
4000 }
4001
4002 return err;
4003}
4004
Chris Mason39279cc2007-06-12 06:35:45 -04004005static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4006{
David Howells2b0143b2015-03-17 22:25:59 +00004007 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004008 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004009 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004010 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004011 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004012
David Sterbab3ae2442012-09-13 16:04:34 -06004013 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004014 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004015 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004016 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004017
Josef Bacikd52be812013-05-29 14:54:47 -04004018 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004019 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004020 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004021
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004022 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004023 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004024 goto out;
4025 }
4026
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004027 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004028 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004029 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004030
Filipe Manana44f714d2016-06-06 16:11:13 +01004031 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4032
Chris Mason39279cc2007-06-12 06:35:45 -04004033 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004034 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4035 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4036 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004037 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004038 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004039 /*
4040 * Propagate the last_unlink_trans value of the deleted dir to
4041 * its parent directory. This is to prevent an unrecoverable
4042 * log tree in the case we do something like this:
4043 * 1) create dir foo
4044 * 2) create snapshot under dir foo
4045 * 3) delete the snapshot
4046 * 4) rmdir foo
4047 * 5) mkdir foo
4048 * 6) fsync foo or some file inside foo
4049 */
4050 if (last_unlink_trans >= trans->transid)
4051 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4052 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004053out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004054 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004055 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004056
Chris Mason39279cc2007-06-12 06:35:45 -04004057 return err;
4058}
4059
Josef Bacikddfae632017-10-19 14:16:02 -04004060/*
4061 * Return this if we need to call truncate_block for the last bit of the
4062 * truncate.
4063 */
4064#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004065
Chris Mason323ac952008-10-01 19:05:46 -04004066/*
Chris Mason39279cc2007-06-12 06:35:45 -04004067 * this can truncate away extent items, csum items and directory items.
4068 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004069 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004070 *
4071 * csum items that cross the new i_size are truncated to the new size
4072 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004073 *
4074 * min_type is the minimum key type to truncate down to. If set to 0, this
4075 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004076 */
Yan, Zheng80825102009-11-12 09:35:36 +00004077int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4078 struct btrfs_root *root,
4079 struct inode *inode,
4080 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004082 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004083 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004084 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004085 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004086 struct btrfs_key key;
4087 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004088 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004089 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004090 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004091 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004092 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004093 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004094 int found_extent;
4095 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004096 int pending_del_nr = 0;
4097 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004098 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004099 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004100 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004101 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004102 bool be_nice = false;
4103 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004104 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4105 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004106
4107 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004108
Chris Mason28ed1342014-12-17 09:41:04 -08004109 /*
4110 * for non-free space inodes and ref cows, we want to back off from
4111 * time to time
4112 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004113 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Chris Mason28ed1342014-12-17 09:41:04 -08004114 test_bit(BTRFS_ROOT_REF_COWS, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004115 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004116
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004117 path = btrfs_alloc_path();
4118 if (!path)
4119 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004120 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004121
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004122 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4123 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4124 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004125
Josef Bacik5dc562c2012-08-17 13:14:17 -04004126 /*
4127 * We want to drop from the next block forward in case this new size is
4128 * not block aligned since we will be keeping the last block of the
4129 * extent just the way it is.
4130 */
Miao Xie27cdeb72014-04-02 19:51:05 +08004131 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004132 root == fs_info->tree_root)
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004133 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004134 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004135 (u64)-1, 0);
Yan, Zheng80825102009-11-12 09:35:36 +00004136
Miao Xie16cdcec2011-04-22 18:12:22 +08004137 /*
4138 * This function is also used to drop the items in the log tree before
4139 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004140 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004141 * items.
4142 */
4143 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004144 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004145
Li Zefan33345d012011-04-20 10:31:50 +08004146 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004147 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004148 key.type = (u8)-1;
4149
Chris Mason85e21ba2008-01-29 15:11:36 -05004150search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004151 /*
4152 * with a 16K leaf size and 128MB extents, you can actually queue
4153 * up a huge file in a single leaf. Most of the time that
4154 * bytes_deleted is > 0, it will be huge by the time we get here
4155 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004156 if (be_nice && bytes_deleted > SZ_32M &&
4157 btrfs_should_end_transaction(trans)) {
4158 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004159 goto out;
4160 }
Chris Masond3977122009-01-05 21:25:51 -05004161
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004162 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4163 if (ret < 0)
4164 goto out;
4165
Chris Mason85e21ba2008-01-29 15:11:36 -05004166 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004167 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004168 /* there are no items in the tree for us to truncate, we're
4169 * done
4170 */
Yan, Zheng80825102009-11-12 09:35:36 +00004171 if (path->slots[0] == 0)
4172 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004173 path->slots[0]--;
4174 }
4175
Chris Masond3977122009-01-05 21:25:51 -05004176 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004177 u64 clear_start = 0, clear_len = 0;
4178
Chris Mason39279cc2007-06-12 06:35:45 -04004179 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004180 leaf = path->nodes[0];
4181 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004182 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004183
Li Zefan33345d012011-04-20 10:31:50 +08004184 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004185 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004186
Chris Mason85e21ba2008-01-29 15:11:36 -05004187 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004188 break;
4189
Chris Mason5f39d392007-10-15 16:14:19 -04004190 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004191 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004192 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004193 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004194 extent_type = btrfs_file_extent_type(leaf, fi);
4195 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004196 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004197 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004198
4199 trace_btrfs_truncate_show_fi_regular(
4200 BTRFS_I(inode), leaf, fi,
4201 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004202 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004203 item_end += btrfs_file_extent_ram_bytes(leaf,
4204 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004205
4206 trace_btrfs_truncate_show_fi_inline(
4207 BTRFS_I(inode), leaf, fi, path->slots[0],
4208 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004209 }
Yan008630c2007-11-07 13:31:09 -05004210 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004211 }
Yan, Zheng80825102009-11-12 09:35:36 +00004212 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004213 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004214 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004215 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004216 break;
4217 if (found_key.offset >= new_size)
4218 del_item = 1;
4219 else
4220 del_item = 0;
4221 }
Chris Mason39279cc2007-06-12 06:35:45 -04004222 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004223 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004224 if (found_type != BTRFS_EXTENT_DATA_KEY)
4225 goto delete;
4226
4227 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004228 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004229
4230 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004231 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004232 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004233 u64 orig_num_bytes =
4234 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004235 extent_num_bytes = ALIGN(new_size -
4236 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004237 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004238 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004239 btrfs_set_file_extent_num_bytes(leaf, fi,
4240 extent_num_bytes);
4241 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004242 extent_num_bytes);
Miao Xie27cdeb72014-04-02 19:51:05 +08004243 if (test_bit(BTRFS_ROOT_REF_COWS,
4244 &root->state) &&
4245 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004246 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004247 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004248 } else {
Chris Masondb945352007-10-15 16:15:53 -04004249 extent_num_bytes =
4250 btrfs_file_extent_disk_num_bytes(leaf,
4251 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004252 extent_offset = found_key.offset -
4253 btrfs_file_extent_offset(leaf, fi);
4254
Chris Mason39279cc2007-06-12 06:35:45 -04004255 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004256 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004257 if (extent_start != 0) {
4258 found_extent = 1;
Miao Xie27cdeb72014-04-02 19:51:05 +08004259 if (test_bit(BTRFS_ROOT_REF_COWS,
4260 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004261 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004262 }
4263 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004264 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004265 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004266 /*
4267 * we can't truncate inline items that have had
4268 * special encodings
4269 */
4270 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004271 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004272 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4273 btrfs_file_extent_compression(leaf, fi) == 0) {
4274 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004275
Josef Bacikddfae632017-10-19 14:16:02 -04004276 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4277 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004278 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004279 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004280 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004281 * We have to bail so the last_size is set to
4282 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004283 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004284 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004285 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004286 } else {
4287 /*
4288 * Inline extents are special, we just treat
4289 * them as a full sector worth in the file
4290 * extent tree just for simplicity sake.
4291 */
4292 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004293 }
Josef Bacikddfae632017-10-19 14:16:02 -04004294
4295 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4296 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004297 }
Chris Mason179e29e2007-11-01 11:28:41 -04004298delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004299 /*
4300 * We use btrfs_truncate_inode_items() to clean up log trees for
4301 * multiple fsyncs, and in this case we don't want to clear the
4302 * file extent range because it's just the log.
4303 */
4304 if (root == BTRFS_I(inode)->root) {
4305 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4306 clear_start, clear_len);
4307 if (ret) {
4308 btrfs_abort_transaction(trans, ret);
4309 break;
4310 }
4311 }
4312
Josef Bacikddfae632017-10-19 14:16:02 -04004313 if (del_item)
4314 last_size = found_key.offset;
4315 else
4316 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004317 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004318 if (!pending_del_nr) {
4319 /* no pending yet, add ourselves */
4320 pending_del_slot = path->slots[0];
4321 pending_del_nr = 1;
4322 } else if (pending_del_nr &&
4323 path->slots[0] + 1 == pending_del_slot) {
4324 /* hop on the pending chunk */
4325 pending_del_nr++;
4326 pending_del_slot = path->slots[0];
4327 } else {
Chris Masond3977122009-01-05 21:25:51 -05004328 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004329 }
Chris Mason39279cc2007-06-12 06:35:45 -04004330 } else {
4331 break;
4332 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004333 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004334
Miao Xie27cdeb72014-04-02 19:51:05 +08004335 if (found_extent &&
4336 (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004337 root == fs_info->tree_root)) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004338 struct btrfs_ref ref = { 0 };
4339
Chris Mason28ed1342014-12-17 09:41:04 -08004340 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004341
4342 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4343 extent_start, extent_num_bytes, 0);
4344 ref.real_root = root->root_key.objectid;
4345 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4346 ino, extent_offset);
4347 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004348 if (ret) {
4349 btrfs_abort_transaction(trans, ret);
4350 break;
4351 }
Chris Mason28f75a02015-02-04 06:59:29 -08004352 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004353 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004354 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004355 }
Chris Mason39279cc2007-06-12 06:35:45 -04004356 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004357
Yan, Zheng80825102009-11-12 09:35:36 +00004358 if (found_type == BTRFS_INODE_ITEM_KEY)
4359 break;
4360
4361 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004362 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004363 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004364 if (pending_del_nr) {
4365 ret = btrfs_del_items(trans, root, path,
4366 pending_del_slot,
4367 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004368 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004369 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004370 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004371 }
Yan, Zheng80825102009-11-12 09:35:36 +00004372 pending_del_nr = 0;
4373 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004374 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004375
Chris Mason28f75a02015-02-04 06:59:29 -08004376 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004377 * We can generate a lot of delayed refs, so we need to
4378 * throttle every once and a while and make sure we're
4379 * adding enough space to keep up with the work we are
4380 * generating. Since we hold a transaction here we
4381 * can't flush, and we don't want to FLUSH_LIMIT because
4382 * we could have generated too many delayed refs to
4383 * actually allocate, so just bail if we're short and
4384 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004385 */
Josef Bacik28bad212018-12-03 10:20:38 -05004386 if (should_throttle) {
4387 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4388 BTRFS_RESERVE_NO_FLUSH);
4389 if (ret) {
4390 ret = -EAGAIN;
4391 break;
4392 }
Chris Mason28f75a02015-02-04 06:59:29 -08004393 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004394 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004395 } else {
4396 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004397 }
Chris Mason39279cc2007-06-12 06:35:45 -04004398 }
Yan, Zheng80825102009-11-12 09:35:36 +00004399out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004400 if (ret >= 0 && pending_del_nr) {
4401 int err;
4402
4403 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004404 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004405 if (err) {
4406 btrfs_abort_transaction(trans, err);
4407 ret = err;
4408 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004409 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004410 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4411 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004412 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004413 last_size = new_size;
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004414 btrfs_ordered_update_i_size(inode, last_size, NULL);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004415 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4416 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004417 }
Chris Mason28ed1342014-12-17 09:41:04 -08004418
Chris Mason39279cc2007-06-12 06:35:45 -04004419 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004420 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004421}
4422
Chris Masona52d9a82007-08-27 16:49:44 -04004423/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304424 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004425 * @inode - inode that we're zeroing
4426 * @from - the offset to start zeroing
4427 * @len - the length to zero, 0 to zero the entire range respective to the
4428 * offset
4429 * @front - zero up to the offset instead of from the offset on
4430 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304431 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004432 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004433 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304434int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004435 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004436{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004437 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004438 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004439 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4440 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004441 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004442 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004443 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004444 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004445 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304446 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004447 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004448 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004449 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304450 u64 block_start;
4451 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004452
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004453 if (IS_ALIGNED(offset, blocksize) &&
4454 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004455 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304456
Josef Bacik8b62f872017-10-19 14:15:55 -04004457 block_start = round_down(from, blocksize);
4458 block_end = block_start + blocksize - 1;
4459
Qu Wenruo364ecf32017-02-27 15:10:38 +08004460 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004461 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004462 if (ret)
4463 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004464
Chris Mason211c17f2008-05-15 09:13:45 -04004465again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004466 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004467 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004468 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004469 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004470 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004471 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004472 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004473 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004474
Chris Masona52d9a82007-08-27 16:49:44 -04004475 if (!PageUptodate(page)) {
4476 ret = btrfs_readpage(NULL, page);
4477 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004478 if (page->mapping != mapping) {
4479 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004480 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004481 goto again;
4482 }
Chris Masona52d9a82007-08-27 16:49:44 -04004483 if (!PageUptodate(page)) {
4484 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004485 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004486 }
4487 }
Chris Mason211c17f2008-05-15 09:13:45 -04004488 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004489
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304490 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004491 set_page_extent_mapped(page);
4492
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304493 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004494 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304495 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004496 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004497 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004498 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004499 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004500 btrfs_put_ordered_extent(ordered);
4501 goto again;
4502 }
4503
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304504 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004505 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4506 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004507
Filipe Mananae3b8a482017-11-04 00:16:59 +00004508 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004509 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004510 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304511 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004512 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004513 goto out_unlock;
4514 }
4515
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304516 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004517 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304518 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004519 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004520 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304521 memset(kaddr + (block_start - page_offset(page)),
4522 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004523 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304524 memset(kaddr + (block_start - page_offset(page)) + offset,
4525 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004526 flush_dcache_page(page);
4527 kunmap(page);
4528 }
Chris Mason247e7432008-07-17 12:53:51 -04004529 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004530 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004531 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004532
Chris Mason89642222008-07-24 09:41:53 -04004533out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004534 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004535 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004536 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004537 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004538 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004539 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004540out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004541 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004542 return ret;
4543}
4544
Josef Bacik16e75492013-10-22 12:18:51 -04004545static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4546 u64 offset, u64 len)
4547{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004548 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004549 struct btrfs_trans_handle *trans;
4550 int ret;
4551
4552 /*
4553 * Still need to make sure the inode looks like it's been updated so
4554 * that any holes get logged if we fsync.
4555 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004556 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4557 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004558 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4559 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4560 return 0;
4561 }
4562
4563 /*
4564 * 1 - for the one we're dropping
4565 * 1 - for the one we're adding
4566 * 1 - for updating the inode.
4567 */
4568 trans = btrfs_start_transaction(root, 3);
4569 if (IS_ERR(trans))
4570 return PTR_ERR(trans);
4571
4572 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4573 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004574 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004575 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004576 return ret;
4577 }
4578
David Sterbaf85b7372017-01-20 14:54:07 +01004579 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4580 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004581 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004582 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004583 else
4584 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004585 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004586 return ret;
4587}
4588
Josef Bacik695a0d02011-03-04 15:46:53 -05004589/*
4590 * This function puts in dummy file extents for the area we're creating a hole
4591 * for. So if we are truncating this file to a larger size we need to insert
4592 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4593 * the range between oldsize and size
4594 */
Josef Bacika41ad392011-01-31 15:30:16 -05004595int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004596{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004597 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004598 struct btrfs_root *root = BTRFS_I(inode)->root;
4599 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004600 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004601 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004602 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004603 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4604 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004605 u64 last_byte;
4606 u64 cur_offset;
4607 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004608 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004609
Josef Bacika71754f2013-06-17 17:14:39 -04004610 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304611 * If our size started in the middle of a block we need to zero out the
4612 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004613 * expose stale data.
4614 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304615 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004616 if (err)
4617 return err;
4618
Yan Zheng9036c102008-10-30 14:19:41 -04004619 if (size <= hole_start)
4620 return 0;
4621
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004622 btrfs_lock_and_flush_ordered_range(io_tree, BTRFS_I(inode), hole_start,
4623 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004624 cur_offset = hole_start;
4625 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004626 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004627 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004628 if (IS_ERR(em)) {
4629 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004630 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004631 break;
4632 }
Yan Zheng9036c102008-10-30 14:19:41 -04004633 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004634 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004635 hole_size = last_byte - cur_offset;
4636
Yan, Zheng80825102009-11-12 09:35:36 +00004637 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004638 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004639
Josef Bacik16e75492013-10-22 12:18:51 -04004640 err = maybe_insert_hole(root, inode, cur_offset,
4641 hole_size);
4642 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004643 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004644
4645 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4646 cur_offset, hole_size);
4647 if (err)
4648 break;
4649
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004650 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004651 cur_offset + hole_size - 1, 0);
4652 hole_em = alloc_extent_map();
4653 if (!hole_em) {
4654 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4655 &BTRFS_I(inode)->runtime_flags);
4656 goto next;
4657 }
4658 hole_em->start = cur_offset;
4659 hole_em->len = hole_size;
4660 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004661
Josef Bacik5dc562c2012-08-17 13:14:17 -04004662 hole_em->block_start = EXTENT_MAP_HOLE;
4663 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004664 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004665 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004666 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004667 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004668
4669 while (1) {
4670 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004671 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004672 write_unlock(&em_tree->lock);
4673 if (err != -EEXIST)
4674 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004675 btrfs_drop_extent_cache(BTRFS_I(inode),
4676 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004677 cur_offset +
4678 hole_size - 1, 0);
4679 }
4680 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004681 } else {
4682 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4683 cur_offset, hole_size);
4684 if (err)
4685 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004686 }
Josef Bacik16e75492013-10-22 12:18:51 -04004687next:
Yan Zheng9036c102008-10-30 14:19:41 -04004688 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004689 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004690 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004691 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004692 break;
4693 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004694 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004695 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004696 return err;
4697}
4698
Eric Sandeen3972f262013-01-12 02:57:22 +00004699static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004700{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004701 struct btrfs_root *root = BTRFS_I(inode)->root;
4702 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004703 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004704 loff_t newsize = attr->ia_size;
4705 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004706 int ret;
4707
Eric Sandeen3972f262013-01-12 02:57:22 +00004708 /*
4709 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4710 * special case where we need to update the times despite not having
4711 * these flags set. For all other operations the VFS set these flags
4712 * explicitly if it wants a timestamp update.
4713 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004714 if (newsize != oldsize) {
4715 inode_inc_iversion(inode);
4716 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4717 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004718 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004719 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004720
Josef Bacika41ad392011-01-31 15:30:16 -05004721 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004722 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004723 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004724 * This is to ensure the snapshot captures a fully consistent
4725 * state of this file - if the snapshot captures this expanding
4726 * truncation, it must capture all writes that happened before
4727 * this truncation.
4728 */
Zhao Lei0bc19f902016-01-06 18:56:36 +08004729 btrfs_wait_for_snapshot_creation(root);
Josef Bacika41ad392011-01-31 15:30:16 -05004730 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004731 if (ret) {
David Sterbaea14b57f2017-06-22 02:19:11 +02004732 btrfs_end_write_no_snapshotting(root);
Yan, Zheng80825102009-11-12 09:35:36 +00004733 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004734 }
Yan, Zheng80825102009-11-12 09:35:36 +00004735
Miao Xief4a2f4c2011-12-14 20:12:01 -05004736 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004737 if (IS_ERR(trans)) {
David Sterbaea14b57f2017-06-22 02:19:11 +02004738 btrfs_end_write_no_snapshotting(root);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004739 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004740 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004741
4742 i_size_write(inode, newsize);
4743 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304744 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004745 ret = btrfs_update_inode(trans, root, inode);
David Sterbaea14b57f2017-06-22 02:19:11 +02004746 btrfs_end_write_no_snapshotting(root);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004747 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004748 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004749
Josef Bacika41ad392011-01-31 15:30:16 -05004750 /*
4751 * We're truncating a file that used to have good data down to
4752 * zero. Make sure it gets into the ordered flush list so that
4753 * any new writes get down to disk quickly.
4754 */
4755 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004756 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4757 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004758
Josef Bacika41ad392011-01-31 15:30:16 -05004759 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004760
Andrea Gelmini52042d82018-11-28 12:05:13 +01004761 /* Disable nonlocked read DIO to avoid the endless truncate */
Nikolay Borisovabcefb12017-02-20 13:51:10 +02004762 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004763 inode_dio_wait(inode);
Nikolay Borisov0b581702017-02-20 13:51:11 +02004764 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004765
Filipe Manana213e8c52018-02-06 20:40:31 +00004766 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004767 if (ret && inode->i_nlink) {
4768 int err;
4769
4770 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004771 * Truncate failed, so fix up the in-memory size. We
4772 * adjusted disk_i_size down as we removed extents, so
4773 * wait for disk_i_size to be stable and then update the
4774 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004775 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004776 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004777 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004778 return err;
4779 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004780 }
Yan, Zheng80825102009-11-12 09:35:36 +00004781 }
4782
Josef Bacika41ad392011-01-31 15:30:16 -05004783 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004784}
4785
Chris Mason39279cc2007-06-12 06:35:45 -04004786static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4787{
David Howells2b0143b2015-03-17 22:25:59 +00004788 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004789 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004790 int err;
4791
Li Zefanb83cc962010-12-20 16:04:08 +08004792 if (btrfs_root_readonly(root))
4793 return -EROFS;
4794
Jan Kara31051c82016-05-26 16:55:18 +02004795 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004796 if (err)
4797 return err;
4798
Chris Mason5a3f23d2009-03-31 13:27:11 -04004799 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004800 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004801 if (err)
4802 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004803 }
Yan Zheng9036c102008-10-30 14:19:41 -04004804
Christoph Hellwig10257742010-06-04 11:30:02 +02004805 if (attr->ia_valid) {
4806 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004807 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004808 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004809
Josef Bacik22c44fe2011-11-30 10:45:38 -05004810 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004811 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004812 }
4813
Chris Mason39279cc2007-06-12 06:35:45 -04004814 return err;
4815}
Chris Mason61295eb2008-01-14 16:24:38 -05004816
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004817/*
4818 * While truncating the inode pages during eviction, we get the VFS calling
4819 * btrfs_invalidatepage() against each page of the inode. This is slow because
4820 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4821 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4822 * extent_state structures over and over, wasting lots of time.
4823 *
4824 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4825 * those expensive operations on a per page basis and do only the ordered io
4826 * finishing, while we release here the extent_map and extent_state structures,
4827 * without the excessive merging and splitting.
4828 */
4829static void evict_inode_truncate_pages(struct inode *inode)
4830{
4831 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4832 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4833 struct rb_node *node;
4834
4835 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004836 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004837
4838 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004839 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004840 struct extent_map *em;
4841
Liu Bo07e1ce02018-08-23 03:51:52 +08004842 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004843 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004844 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4845 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004846 remove_extent_mapping(map_tree, em);
4847 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004848 if (need_resched()) {
4849 write_unlock(&map_tree->lock);
4850 cond_resched();
4851 write_lock(&map_tree->lock);
4852 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004853 }
4854 write_unlock(&map_tree->lock);
4855
Filipe Manana6ca07092015-05-26 00:55:42 +01004856 /*
4857 * Keep looping until we have no more ranges in the io tree.
4858 * We can have ongoing bios started by readpages (called from readahead)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004859 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
4860 * still in progress (unlocked the pages in the bio but did not yet
4861 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004862 * ranges can still be locked and eviction started because before
4863 * submitting those bios, which are executed by a separate task (work
4864 * queue kthread), inode references (inode->i_count) were not taken
4865 * (which would be dropped in the end io callback of each bio).
4866 * Therefore here we effectively end up waiting for those bios and
4867 * anyone else holding locked ranges without having bumped the inode's
4868 * reference count - if we don't do it, when they access the inode's
4869 * io_tree to unlock a range it may be too late, leading to an
4870 * use-after-free issue.
4871 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004872 spin_lock(&io_tree->lock);
4873 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4874 struct extent_state *state;
4875 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004876 u64 start;
4877 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004878 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004879
4880 node = rb_first(&io_tree->state);
4881 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004882 start = state->start;
4883 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004884 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004885 spin_unlock(&io_tree->lock);
4886
David Sterbaff13db42015-12-03 14:30:40 +01004887 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004888
4889 /*
4890 * If still has DELALLOC flag, the extent didn't reach disk,
4891 * and its reserved space won't be freed by delayed_ref.
4892 * So we need to free its reserved space here.
4893 * (Refer to comment in btrfs_invalidatepage, case 2)
4894 *
4895 * Note, end is the bytenr of last byte, so we need + 1 here.
4896 */
Filipe Manana421f0922018-10-12 13:02:48 +01004897 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004898 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004899
Filipe Manana6ca07092015-05-26 00:55:42 +01004900 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004901 EXTENT_LOCKED | EXTENT_DELALLOC |
4902 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4903 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004904
Filipe Manana7064dd52014-08-08 02:47:05 +01004905 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004906 spin_lock(&io_tree->lock);
4907 }
4908 spin_unlock(&io_tree->lock);
4909}
4910
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004911static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04004912 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004913{
4914 struct btrfs_fs_info *fs_info = root->fs_info;
4915 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04004916 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04004917 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04004918 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004919
Josef Bacikd3984c92019-08-01 18:19:37 -04004920 /*
4921 * Eviction should be taking place at some place safe because of our
4922 * delayed iputs. However the normal flushing code will run delayed
4923 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
4924 *
4925 * We reserve the delayed_refs_extra here again because we can't use
4926 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
4927 * above. We reserve our extra bit here because we generate a ton of
4928 * delayed refs activity by truncating.
4929 *
4930 * If we cannot make our reservation we'll attempt to steal from the
4931 * global reserve, because we really want to be able to free up space.
4932 */
4933 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
4934 BTRFS_RESERVE_FLUSH_EVICT);
4935 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004936 /*
4937 * Try to steal from the global reserve if there is space for
4938 * it.
4939 */
Josef Bacikd3984c92019-08-01 18:19:37 -04004940 if (btrfs_check_space_for_delayed_refs(fs_info) ||
4941 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
4942 btrfs_warn(fs_info,
4943 "could not allocate space for delete; will truncate on mount");
4944 return ERR_PTR(-ENOSPC);
4945 }
4946 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004947 }
Josef Bacikd3984c92019-08-01 18:19:37 -04004948
4949 trans = btrfs_join_transaction(root);
4950 if (IS_ERR(trans))
4951 return trans;
4952
4953 if (delayed_refs_extra) {
4954 trans->block_rsv = &fs_info->trans_block_rsv;
4955 trans->bytes_reserved = delayed_refs_extra;
4956 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
4957 delayed_refs_extra, 1);
4958 }
4959 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004960}
4961
Al Virobd555972010-06-07 11:35:40 -04004962void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04004963{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004964 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04004965 struct btrfs_trans_handle *trans;
4966 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004967 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04004968 int ret;
4969
liubo1abe9b82011-03-24 11:18:59 +00004970 trace_btrfs_inode_evict(inode);
4971
Nikolay Borisov3d48d982016-06-29 09:46:41 +03004972 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07004973 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03004974 return;
4975 }
4976
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004977 evict_inode_truncate_pages(inode);
4978
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004979 if (inode->i_nlink &&
4980 ((btrfs_root_refs(&root->root_item) != 0 &&
4981 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004982 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04004983 goto no_delete;
4984
Omar Sandoval27919062018-05-11 13:13:37 -07004985 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04004986 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04004987
Nikolay Borisov7ab79562017-02-20 13:50:57 +02004988 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08004989
Omar Sandoval7b40b692018-05-11 13:13:33 -07004990 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00004991 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00004992
Yan, Zheng76dda932009-09-21 16:00:26 -04004993 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02004994 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
4995 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04004996 goto no_delete;
4997 }
4998
Nikolay Borisovaa790212017-01-10 20:35:40 +02004999 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005000 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005001 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005002
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005003 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005004 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005005 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005006 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005007 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005008
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005009 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005010
Yan, Zheng80825102009-11-12 09:35:36 +00005011 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005012 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005013 if (IS_ERR(trans))
5014 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005015
5016 trans->block_rsv = rsv;
5017
Yan, Zhengd68fc572010-05-16 10:49:58 -04005018 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005019 trans->block_rsv = &fs_info->trans_block_rsv;
5020 btrfs_end_transaction(trans);
5021 btrfs_btree_balance_dirty(fs_info);
5022 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5023 goto free_rsv;
5024 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005025 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005026 }
5027
Josef Bacik4ef31a42013-08-13 14:10:08 -04005028 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005029 * Errors here aren't a big deal, it just means we leave orphan items in
5030 * the tree. They will be cleaned up on the next mount. If the inode
5031 * number gets reused, cleanup deletes the orphan item without doing
5032 * anything, and unlink reuses the existing orphan item.
5033 *
5034 * If it turns out that we are dropping too many of these, we might want
5035 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005036 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005037 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005038 if (!IS_ERR(trans)) {
5039 trans->block_rsv = rsv;
5040 btrfs_orphan_del(trans, BTRFS_I(inode));
5041 trans->block_rsv = &fs_info->trans_block_rsv;
5042 btrfs_end_transaction(trans);
5043 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005044
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005045 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005046 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005047 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005048
Omar Sandoval27919062018-05-11 13:13:37 -07005049free_rsv:
5050 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005051no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005052 /*
5053 * If we didn't successfully delete, the orphan item will still be in
5054 * the tree and we'll retry on the next mount. Again, we might also want
5055 * to retry these periodically in the future.
5056 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005057 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005058 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005059}
5060
5061/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005062 * Return the key found in the dir entry in the location pointer, fill @type
5063 * with BTRFS_FT_*, and return 0.
5064 *
Su Yue005d6712018-03-05 17:13:37 +08005065 * If no dir entries were found, returns -ENOENT.
5066 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005067 */
5068static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005069 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005070{
5071 const char *name = dentry->d_name.name;
5072 int namelen = dentry->d_name.len;
5073 struct btrfs_dir_item *di;
5074 struct btrfs_path *path;
5075 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005076 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005077
5078 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005079 if (!path)
5080 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005081
David Sterbaf85b7372017-01-20 14:54:07 +01005082 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5083 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005084 if (IS_ERR_OR_NULL(di)) {
5085 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005086 goto out;
5087 }
Chris Masond3977122009-01-05 21:25:51 -05005088
Chris Mason5f39d392007-10-15 16:14:19 -04005089 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005090 if (location->type != BTRFS_INODE_ITEM_KEY &&
5091 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005092 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005093 btrfs_warn(root->fs_info,
5094"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5095 __func__, name, btrfs_ino(BTRFS_I(dir)),
5096 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005097 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005098 if (!ret)
5099 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005100out:
Chris Mason39279cc2007-06-12 06:35:45 -04005101 btrfs_free_path(path);
5102 return ret;
5103}
5104
5105/*
5106 * when we hit a tree root in a directory, the btrfs part of the inode
5107 * needs to be changed to reflect the root directory of the tree root. This
5108 * is kind of like crossing a mount point.
5109 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005110static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005111 struct inode *dir,
5112 struct dentry *dentry,
5113 struct btrfs_key *location,
5114 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005115{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005116 struct btrfs_path *path;
5117 struct btrfs_root *new_root;
5118 struct btrfs_root_ref *ref;
5119 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005120 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005121 int ret;
5122 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005123
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005124 path = btrfs_alloc_path();
5125 if (!path) {
5126 err = -ENOMEM;
5127 goto out;
5128 }
Chris Mason39279cc2007-06-12 06:35:45 -04005129
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005130 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005131 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5132 key.type = BTRFS_ROOT_REF_KEY;
5133 key.offset = location->objectid;
5134
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005135 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005136 if (ret) {
5137 if (ret < 0)
5138 err = ret;
5139 goto out;
5140 }
Chris Mason39279cc2007-06-12 06:35:45 -04005141
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005142 leaf = path->nodes[0];
5143 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005144 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005145 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5146 goto out;
5147
5148 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5149 (unsigned long)(ref + 1),
5150 dentry->d_name.len);
5151 if (ret)
5152 goto out;
5153
David Sterbab3b4aa72011-04-21 01:20:15 +02005154 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005155
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005156 new_root = btrfs_read_fs_root_no_name(fs_info, location);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005157 if (IS_ERR(new_root)) {
5158 err = PTR_ERR(new_root);
5159 goto out;
5160 }
5161
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005162 *sub_root = new_root;
5163 location->objectid = btrfs_root_dirid(&new_root->root_item);
5164 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005165 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005166 err = 0;
5167out:
5168 btrfs_free_path(path);
5169 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005170}
5171
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005172static void inode_tree_add(struct inode *inode)
5173{
5174 struct btrfs_root *root = BTRFS_I(inode)->root;
5175 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005176 struct rb_node **p;
5177 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005178 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005179 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005180
Al Viro1d3382cb2010-10-23 15:19:20 -04005181 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005182 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005183 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005184 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005185 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005186 while (*p) {
5187 parent = *p;
5188 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5189
David Sterba37508512018-06-29 10:56:40 +02005190 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005191 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005192 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005193 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005194 else {
5195 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005196 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005197 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005198 RB_CLEAR_NODE(parent);
5199 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005200 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005201 }
5202 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005203 rb_link_node(new, parent, p);
5204 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005205 spin_unlock(&root->inode_lock);
5206}
5207
5208static void inode_tree_del(struct inode *inode)
5209{
5210 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005211 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005212
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005213 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005214 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005215 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005216 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005217 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005218 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005219 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005220
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005221 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005222 spin_lock(&root->inode_lock);
5223 empty = RB_EMPTY_ROOT(&root->inode_tree);
5224 spin_unlock(&root->inode_lock);
5225 if (empty)
5226 btrfs_add_dead_root(root);
5227 }
5228}
5229
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005230
Chris Masone02119d2008-09-05 16:13:11 -04005231static int btrfs_init_locked_inode(struct inode *inode, void *p)
5232{
5233 struct btrfs_iget_args *args = p;
Chris Mason90d3e592014-01-09 17:28:00 -08005234 inode->i_ino = args->location->objectid;
5235 memcpy(&BTRFS_I(inode)->location, args->location,
5236 sizeof(*args->location));
Chris Masone02119d2008-09-05 16:13:11 -04005237 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005238 return 0;
5239}
5240
5241static int btrfs_find_actor(struct inode *inode, void *opaque)
5242{
5243 struct btrfs_iget_args *args = opaque;
Chris Mason90d3e592014-01-09 17:28:00 -08005244 return args->location->objectid == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005245 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005246}
5247
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005248static struct inode *btrfs_iget_locked(struct super_block *s,
Chris Mason90d3e592014-01-09 17:28:00 -08005249 struct btrfs_key *location,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005250 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005251{
5252 struct inode *inode;
5253 struct btrfs_iget_args args;
Chris Mason90d3e592014-01-09 17:28:00 -08005254 unsigned long hashval = btrfs_inode_hash(location->objectid, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005255
Chris Mason90d3e592014-01-09 17:28:00 -08005256 args.location = location;
Chris Mason39279cc2007-06-12 06:35:45 -04005257 args.root = root;
5258
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005259 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005260 btrfs_init_locked_inode,
5261 (void *)&args);
5262 return inode;
5263}
5264
David Sterba4c66e0d2019-10-03 19:09:35 +02005265/*
5266 * Get an inode object given its location and corresponding root.
5267 * Path can be preallocated to prevent recursing back to iget through
5268 * allocator. NULL is also valid but may require an additional allocation
5269 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305270 */
Filipe Manana4222ea72018-10-24 10:13:03 +01005271struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005272 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305273{
5274 struct inode *inode;
5275
Chris Mason90d3e592014-01-09 17:28:00 -08005276 inode = btrfs_iget_locked(s, location, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305277 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005278 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305279
5280 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005281 int ret;
5282
Filipe Manana4222ea72018-10-24 10:13:03 +01005283 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005284 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005285 inode_tree_add(inode);
5286 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005287 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005288 iget_failed(inode);
5289 /*
5290 * ret > 0 can come from btrfs_search_slot called by
5291 * btrfs_read_locked_inode, this means the inode item
5292 * was not found.
5293 */
5294 if (ret > 0)
5295 ret = -ENOENT;
5296 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005297 }
5298 }
5299
Balaji Rao1a54ef82008-07-21 02:01:04 +05305300 return inode;
5301}
5302
Filipe Manana4222ea72018-10-24 10:13:03 +01005303struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
David Sterba4c66e0d2019-10-03 19:09:35 +02005304 struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005305{
David Sterba4c66e0d2019-10-03 19:09:35 +02005306 return btrfs_iget_path(s, location, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005307}
5308
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005309static struct inode *new_simple_dir(struct super_block *s,
5310 struct btrfs_key *key,
5311 struct btrfs_root *root)
5312{
5313 struct inode *inode = new_inode(s);
5314
5315 if (!inode)
5316 return ERR_PTR(-ENOMEM);
5317
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005318 BTRFS_I(inode)->root = root;
5319 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005320 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005321
5322 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005323 /*
5324 * We only need lookup, the rest is read-only and there's no inode
5325 * associated with the dentry
5326 */
5327 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005328 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005329 inode->i_fop = &simple_dir_operations;
5330 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005331 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305332 inode->i_atime = inode->i_mtime;
5333 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005334 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005335
5336 return inode;
5337}
5338
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005339static inline u8 btrfs_inode_type(struct inode *inode)
5340{
5341 /*
5342 * Compile-time asserts that generic FT_* types still match
5343 * BTRFS_FT_* types
5344 */
5345 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5346 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5347 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5348 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5349 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5350 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5351 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5352 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5353
5354 return fs_umode_to_ftype(inode->i_mode);
5355}
5356
Chris Mason3de45862008-11-17 21:02:50 -05005357struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005358{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005359 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005360 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005361 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005362 struct btrfs_root *sub_root = root;
5363 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005364 u8 di_type = 0;
Yan, Zheng76dda932009-09-21 16:00:26 -04005365 int index;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005366 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005367
5368 if (dentry->d_name.len > BTRFS_NAME_LEN)
5369 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005370
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005371 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005372 if (ret < 0)
5373 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005374
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005375 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba4c66e0d2019-10-03 19:09:35 +02005376 inode = btrfs_iget(dir->i_sb, &location, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005377 if (IS_ERR(inode))
5378 return inode;
5379
5380 /* Do extra check against inode mode with di_type */
5381 if (btrfs_inode_type(inode) != di_type) {
5382 btrfs_crit(fs_info,
5383"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5384 inode->i_mode, btrfs_inode_type(inode),
5385 di_type);
5386 iput(inode);
5387 return ERR_PTR(-EUCLEAN);
5388 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005389 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005390 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005391
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005392 index = srcu_read_lock(&fs_info->subvol_srcu);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005393 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005394 &location, &sub_root);
5395 if (ret < 0) {
5396 if (ret != -ENOENT)
5397 inode = ERR_PTR(ret);
5398 else
5399 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5400 } else {
David Sterba4c66e0d2019-10-03 19:09:35 +02005401 inode = btrfs_iget(dir->i_sb, &location, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005402 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005403 srcu_read_unlock(&fs_info->subvol_srcu, index);
Yan, Zheng76dda932009-09-21 16:00:26 -04005404
Julia Lawall34d19ba2011-01-24 19:55:19 +00005405 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005406 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005407 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005408 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005409 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005410 if (ret) {
5411 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005412 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005413 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005414 }
5415
Chris Mason3de45862008-11-17 21:02:50 -05005416 return inode;
5417}
5418
Nick Pigginfe15ce42011-01-07 17:49:23 +11005419static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005420{
5421 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005422 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005423
Li Zefan848cce02012-02-21 17:04:28 +08005424 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005425 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005426
Li Zefan848cce02012-02-21 17:04:28 +08005427 if (inode) {
5428 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005429 if (btrfs_root_refs(&root->root_item) == 0)
5430 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005431
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005432 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005433 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005434 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005435 return 0;
5436}
5437
Chris Mason3de45862008-11-17 21:02:50 -05005438static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005439 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005440{
Al Viro3837d202018-10-10 16:38:27 -04005441 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005442
Al Viro3837d202018-10-10 16:38:27 -04005443 if (inode == ERR_PTR(-ENOENT))
5444 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005445 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005446}
5447
Josef Bacik23b5ec72017-07-24 15:14:25 -04005448/*
5449 * All this infrastructure exists because dir_emit can fault, and we are holding
5450 * the tree lock when doing readdir. For now just allocate a buffer and copy
5451 * our information into that, and then dir_emit from the buffer. This is
5452 * similar to what NFS does, only we don't keep the buffer around in pagecache
5453 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5454 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5455 * tree lock.
5456 */
5457static int btrfs_opendir(struct inode *inode, struct file *file)
5458{
5459 struct btrfs_file_private *private;
5460
5461 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5462 if (!private)
5463 return -ENOMEM;
5464 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5465 if (!private->filldir_buf) {
5466 kfree(private);
5467 return -ENOMEM;
5468 }
5469 file->private_data = private;
5470 return 0;
5471}
5472
5473struct dir_entry {
5474 u64 ino;
5475 u64 offset;
5476 unsigned type;
5477 int name_len;
5478};
5479
5480static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5481{
5482 while (entries--) {
5483 struct dir_entry *entry = addr;
5484 char *name = (char *)(entry + 1);
5485
David Sterba92d32172018-04-16 21:10:14 +02005486 ctx->pos = get_unaligned(&entry->offset);
5487 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5488 get_unaligned(&entry->ino),
5489 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005490 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005491 addr += sizeof(struct dir_entry) +
5492 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005493 ctx->pos++;
5494 }
5495 return 0;
5496}
5497
Al Viro9cdda8d2013-05-22 16:48:09 -04005498static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005499{
Al Viro9cdda8d2013-05-22 16:48:09 -04005500 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005501 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005502 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005503 struct btrfs_dir_item *di;
5504 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005505 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005506 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005507 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005508 struct list_head ins_list;
5509 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005510 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005511 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005512 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005513 char *name_ptr;
5514 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005515 int entries = 0;
5516 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005517 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005518 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005519
Al Viro9cdda8d2013-05-22 16:48:09 -04005520 if (!dir_emit_dots(file, ctx))
5521 return 0;
5522
David Woodhouse49593bf2008-08-17 17:08:36 +01005523 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005524 if (!path)
5525 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005526
Josef Bacik23b5ec72017-07-24 15:14:25 -04005527 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005528 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005529
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005530 INIT_LIST_HEAD(&ins_list);
5531 INIT_LIST_HEAD(&del_list);
5532 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005533
Josef Bacik23b5ec72017-07-24 15:14:25 -04005534again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005535 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005536 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005537 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005538
Chris Mason39279cc2007-06-12 06:35:45 -04005539 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5540 if (ret < 0)
5541 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005542
5543 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005544 struct dir_entry *entry;
5545
Chris Mason5f39d392007-10-15 16:14:19 -04005546 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005547 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005548 if (slot >= btrfs_header_nritems(leaf)) {
5549 ret = btrfs_next_leaf(root, path);
5550 if (ret < 0)
5551 goto err;
5552 else if (ret > 0)
5553 break;
5554 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005555 }
Chris Mason3de45862008-11-17 21:02:50 -05005556
Chris Mason5f39d392007-10-15 16:14:19 -04005557 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5558
5559 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005560 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005561 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005562 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005563 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005564 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005565 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005566 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005567 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005568 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005569 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5570 PAGE_SIZE) {
5571 btrfs_release_path(path);
5572 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5573 if (ret)
5574 goto nopos;
5575 addr = private->filldir_buf;
5576 entries = 0;
5577 total_len = 0;
5578 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005579 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005580
5581 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005582 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005583 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005584 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5585 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005586 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005587 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005588 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005589 put_unaligned(location.objectid, &entry->ino);
5590 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005591 entries++;
5592 addr += sizeof(struct dir_entry) + name_len;
5593 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005594next:
5595 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005596 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005597 btrfs_release_path(path);
5598
5599 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5600 if (ret)
5601 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005602
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005603 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005604 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005605 goto nopos;
5606
Zach Browndb62efb2013-07-11 16:19:42 -07005607 /*
5608 * Stop new entries from being returned after we return the last
5609 * entry.
5610 *
5611 * New directory entries are assigned a strictly increasing
5612 * offset. This means that new entries created during readdir
5613 * are *guaranteed* to be seen in the future by that readdir.
5614 * This has broken buggy programs which operate on names as
5615 * they're returned by readdir. Until we re-use freed offsets
5616 * we have this hack to stop new entries from being returned
5617 * under the assumption that they'll never reach this huge
5618 * offset.
5619 *
5620 * This is being careful not to overflow 32bit loff_t unless the
5621 * last entry requires it because doing so has broken 32bit apps
5622 * in the past.
5623 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005624 if (ctx->pos >= INT_MAX)
5625 ctx->pos = LLONG_MAX;
5626 else
5627 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005628nopos:
5629 ret = 0;
5630err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005631 if (put)
5632 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005633 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005634 return ret;
5635}
5636
Chris Mason39279cc2007-06-12 06:35:45 -04005637/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005638 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005639 * inode changes. But, it is most likely to find the inode in cache.
5640 * FIXME, needs more benchmarking...there are no reasons other than performance
5641 * to keep or drop this code.
5642 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005643static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005644{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005645 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005646 struct btrfs_root *root = BTRFS_I(inode)->root;
5647 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005648 int ret;
5649
Josef Bacik72ac3c02012-05-23 14:13:11 -04005650 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005651 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005652
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005653 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005654 if (IS_ERR(trans))
5655 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005656
5657 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005658 if (ret && ret == -ENOSPC) {
5659 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005660 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005661 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005662 if (IS_ERR(trans))
5663 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005664
Chris Mason94b60442010-05-26 11:02:00 -04005665 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005666 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005667 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005668 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005669 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005670
5671 return ret;
5672}
5673
5674/*
5675 * This is a copy of file_update_time. We need this so we can return error on
5676 * ENOSPC for updating the inode in the case of file write and mmap writes.
5677 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005678static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005679 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005680{
Alexander Block2bc5565282012-06-15 09:49:33 +02005681 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005682 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005683
5684 if (btrfs_root_readonly(root))
5685 return -EROFS;
5686
Josef Bacike41f9412012-03-26 09:46:47 -04005687 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005688 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005689 if (flags & S_CTIME)
5690 inode->i_ctime = *now;
5691 if (flags & S_MTIME)
5692 inode->i_mtime = *now;
5693 if (flags & S_ATIME)
5694 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005695 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005696}
5697
Chris Masond352ac62008-09-29 15:18:18 -04005698/*
5699 * find the highest existing sequence number in a directory
5700 * and then set the in-memory index_cnt variable to reflect
5701 * free sequence numbers
5702 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005703static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005704{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005705 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005706 struct btrfs_key key, found_key;
5707 struct btrfs_path *path;
5708 struct extent_buffer *leaf;
5709 int ret;
5710
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005711 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005712 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005713 key.offset = (u64)-1;
5714
5715 path = btrfs_alloc_path();
5716 if (!path)
5717 return -ENOMEM;
5718
5719 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5720 if (ret < 0)
5721 goto out;
5722 /* FIXME: we should be able to handle this */
5723 if (ret == 0)
5724 goto out;
5725 ret = 0;
5726
5727 /*
5728 * MAGIC NUMBER EXPLANATION:
5729 * since we search a directory based on f_pos we have to start at 2
5730 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5731 * else has to start at 2
5732 */
5733 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005734 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005735 goto out;
5736 }
5737
5738 path->slots[0]--;
5739
5740 leaf = path->nodes[0];
5741 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5742
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005743 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005744 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005745 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005746 goto out;
5747 }
5748
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005749 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005750out:
5751 btrfs_free_path(path);
5752 return ret;
5753}
5754
Chris Masond352ac62008-09-29 15:18:18 -04005755/*
5756 * helper to find a free sequence number in a given directory. This current
5757 * code is very simple, later versions will do smarter things in the btree
5758 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005759int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005760{
5761 int ret = 0;
5762
Nikolay Borisov877574e2017-02-20 13:50:33 +02005763 if (dir->index_cnt == (u64)-1) {
5764 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005765 if (ret) {
5766 ret = btrfs_set_inode_index_count(dir);
5767 if (ret)
5768 return ret;
5769 }
Josef Bacikaec74772008-07-24 12:12:38 -04005770 }
5771
Nikolay Borisov877574e2017-02-20 13:50:33 +02005772 *index = dir->index_cnt;
5773 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005774
5775 return ret;
5776}
5777
Chris Masonb0d5d102014-09-08 13:08:51 -07005778static int btrfs_insert_inode_locked(struct inode *inode)
5779{
5780 struct btrfs_iget_args args;
5781 args.location = &BTRFS_I(inode)->location;
5782 args.root = BTRFS_I(inode)->root;
5783
5784 return insert_inode_locked4(inode,
5785 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5786 btrfs_find_actor, &args);
5787}
5788
Anand Jain19aee8d2017-07-18 17:37:05 +08005789/*
5790 * Inherit flags from the parent inode.
5791 *
5792 * Currently only the compression flags and the cow flags are inherited.
5793 */
5794static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5795{
5796 unsigned int flags;
5797
5798 if (!dir)
5799 return;
5800
5801 flags = BTRFS_I(dir)->flags;
5802
5803 if (flags & BTRFS_INODE_NOCOMPRESS) {
5804 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5805 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5806 } else if (flags & BTRFS_INODE_COMPRESS) {
5807 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5808 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5809 }
5810
5811 if (flags & BTRFS_INODE_NODATACOW) {
5812 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5813 if (S_ISREG(inode->i_mode))
5814 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5815 }
5816
David Sterba7b6a2212018-03-26 18:40:21 +02005817 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005818}
5819
Chris Mason39279cc2007-06-12 06:35:45 -04005820static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5821 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005822 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005823 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005824 u64 ref_objectid, u64 objectid,
5825 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005826{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005827 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005828 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005829 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005830 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005831 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005832 struct btrfs_inode_ref *ref;
5833 struct btrfs_key key[2];
5834 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005835 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005836 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005837 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005838 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005839
Chris Mason5f39d392007-10-15 16:14:19 -04005840 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005841 if (!path)
5842 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005843
Josef Bacik11a19a92019-09-09 10:12:04 -04005844 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005845 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005846 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005847 if (!inode) {
5848 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005849 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005850 }
Chris Mason39279cc2007-06-12 06:35:45 -04005851
Li Zefan581bb052011-04-20 10:06:11 +08005852 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005853 * O_TMPFILE, set link count to 0, so that after this point,
5854 * we fill in an inode item with the correct link count.
5855 */
5856 if (!name)
5857 set_nlink(inode, 0);
5858
5859 /*
Li Zefan581bb052011-04-20 10:06:11 +08005860 * we have to initialize this early, so we can reclaim the inode
5861 * number if we fail afterwards in this function.
5862 */
5863 inode->i_ino = objectid;
5864
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005865 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005866 trace_btrfs_inode_request(dir);
5867
Nikolay Borisov877574e2017-02-20 13:50:33 +02005868 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005869 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005870 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005871 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005872 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005873 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005874 } else if (dir) {
5875 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005876 }
5877 /*
5878 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005879 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005880 * number
5881 */
5882 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005883 BTRFS_I(inode)->dir_index = *index;
Chris Mason39279cc2007-06-12 06:35:45 -04005884 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04005885 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005886 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005887
Josef Bacik5dc562c2012-08-17 13:14:17 -04005888 /*
5889 * We could have gotten an inode number from somebody who was fsynced
5890 * and then removed in this same transaction, so let's just set full
5891 * sync since it will be a full sync anyway and this will blow away the
5892 * old info in the log.
5893 */
5894 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5895
Chris Mason9c583092008-01-29 15:15:18 -05005896 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005897 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005898 key[0].offset = 0;
5899
Chris Mason9c583092008-01-29 15:15:18 -05005900 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005901
5902 if (name) {
5903 /*
5904 * Start new inodes with an inode_ref. This is slightly more
5905 * efficient for small numbers of hard links since they will
5906 * be packed into one item. Extended refs will kick in if we
5907 * add more hard links than can fit in the ref item.
5908 */
5909 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005910 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005911 key[1].offset = ref_objectid;
5912
5913 sizes[1] = name_len + sizeof(*ref);
5914 }
Chris Mason9c583092008-01-29 15:15:18 -05005915
Chris Masonb0d5d102014-09-08 13:08:51 -07005916 location = &BTRFS_I(inode)->location;
5917 location->objectid = objectid;
5918 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005919 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005920
5921 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04005922 if (ret < 0) {
5923 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005924 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04005925 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005926
Chris Masonb9473432009-03-13 11:00:37 -04005927 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005928 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005929 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005930 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005931
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005932 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005933 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05305934
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005935 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305936 inode->i_atime = inode->i_mtime;
5937 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005938 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05305939
Chris Mason5f39d392007-10-15 16:14:19 -04005940 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5941 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01005942 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06005943 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04005944 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05005945
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005946 if (name) {
5947 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
5948 struct btrfs_inode_ref);
5949 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
5950 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
5951 ptr = (unsigned long)(ref + 1);
5952 write_extent_buffer(path->nodes[0], name, ptr, name_len);
5953 }
Chris Mason9c583092008-01-29 15:15:18 -05005954
Chris Mason5f39d392007-10-15 16:14:19 -04005955 btrfs_mark_buffer_dirty(path->nodes[0]);
5956 btrfs_free_path(path);
5957
Christoph Hellwig6cbff002009-04-17 10:37:41 +02005958 btrfs_inherit_iflags(inode, dir);
5959
Al Viro569254b2011-07-24 17:08:40 -04005960 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005961 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04005962 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005963 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05005964 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
5965 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04005966 }
5967
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005968 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00005969
5970 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04005971 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00005972
Alexander Block8ea05e32012-07-25 17:35:53 +02005973 btrfs_update_root_times(trans, root);
5974
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005975 ret = btrfs_inode_inherit_props(trans, inode, dir);
5976 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005977 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005978 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01005979 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00005980
Chris Mason39279cc2007-06-12 06:35:45 -04005981 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07005982
5983fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04005984 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04005985fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005986 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04005987 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04005988 btrfs_free_path(path);
5989 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04005990}
5991
Chris Masond352ac62008-09-29 15:18:18 -04005992/*
5993 * utility function to add 'inode' into 'parent_inode' with
5994 * a give name and a given sequence number.
5995 * if 'add_backref' is true, also insert a backref from the
5996 * inode to the parent directory.
5997 */
Chris Masone02119d2008-09-05 16:13:11 -04005998int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02005999 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006000 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006001{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006002 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006003 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006004 struct btrfs_root *root = parent_inode->root;
6005 u64 ino = btrfs_ino(inode);
6006 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006007
Li Zefan33345d012011-04-20 10:31:50 +08006008 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006009 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006010 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006011 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006012 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006013 key.offset = 0;
6014 }
Chris Mason39279cc2007-06-12 06:35:45 -04006015
Li Zefan33345d012011-04-20 10:31:50 +08006016 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006017 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006018 root->root_key.objectid, parent_ino,
6019 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006020 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006021 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6022 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006023 }
6024
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006025 /* Nothing to clean up yet */
6026 if (ret)
6027 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006028
Lu Fengqi684572d2018-08-04 21:10:57 +08006029 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006030 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006031 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006032 goto fail_dir_item;
6033 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006034 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006035 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006036 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006037
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006038 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006039 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006040 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006041 /*
6042 * If we are replaying a log tree, we do not want to update the mtime
6043 * and ctime of the parent directory with the current time, since the
6044 * log replay procedure is responsible for setting them to their correct
6045 * values (the ones it had when the fsync was done).
6046 */
6047 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6048 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6049
6050 parent_inode->vfs_inode.i_mtime = now;
6051 parent_inode->vfs_inode.i_ctime = now;
6052 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006053 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006054 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006055 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006056 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006057
6058fail_dir_item:
6059 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6060 u64 local_index;
6061 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006062 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006063 root->root_key.objectid, parent_ino,
6064 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006065 if (err)
6066 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006067 } else if (add_backref) {
6068 u64 local_index;
6069 int err;
6070
6071 err = btrfs_del_inode_ref(trans, root, name, name_len,
6072 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006073 if (err)
6074 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006075 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006076
6077 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006078 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006079}
6080
6081static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006082 struct btrfs_inode *dir, struct dentry *dentry,
6083 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006084{
Josef Bacika1b075d2010-11-19 20:36:11 +00006085 int err = btrfs_add_link(trans, dir, inode,
6086 dentry->d_name.name, dentry->d_name.len,
6087 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006088 if (err > 0)
6089 err = -EEXIST;
6090 return err;
6091}
6092
Josef Bacik618e21d2007-07-11 10:18:17 -04006093static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006094 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006095{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006096 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006097 struct btrfs_trans_handle *trans;
6098 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006099 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006100 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006101 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006102 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006103
Josef Bacik9ed74f22009-09-11 16:12:44 -04006104 /*
6105 * 2 for inode item and ref
6106 * 2 for dir items
6107 * 1 for xattr if selinux is on
6108 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006109 trans = btrfs_start_transaction(root, 5);
6110 if (IS_ERR(trans))
6111 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006112
Li Zefan581bb052011-04-20 10:06:11 +08006113 err = btrfs_find_free_ino(root, &objectid);
6114 if (err)
6115 goto out_unlock;
6116
Josef Bacikaec74772008-07-24 12:12:38 -04006117 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006118 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6119 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006120 if (IS_ERR(inode)) {
6121 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006122 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006123 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006124 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006125
Casey Schauflerad19db72011-12-15 10:09:07 -05006126 /*
6127 * If the active LSM wants to access the inode during
6128 * d_instantiate it needs these. Smack checks to see
6129 * if the filesystem supports xattrs by looking at the
6130 * ops vector.
6131 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006132 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006133 init_special_inode(inode, inode->i_mode, rdev);
6134
6135 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006136 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006137 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006138
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006139 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6140 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006141 if (err)
6142 goto out_unlock;
6143
6144 btrfs_update_inode(trans, root, inode);
6145 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006146
Josef Bacik618e21d2007-07-11 10:18:17 -04006147out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006148 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006149 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006150 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006151 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006152 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006153 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006154 return err;
6155}
6156
Chris Mason39279cc2007-06-12 06:35:45 -04006157static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006158 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006159{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006160 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006161 struct btrfs_trans_handle *trans;
6162 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006163 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006164 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006165 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006166 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006167
Josef Bacik9ed74f22009-09-11 16:12:44 -04006168 /*
6169 * 2 for inode item and ref
6170 * 2 for dir items
6171 * 1 for xattr if selinux is on
6172 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006173 trans = btrfs_start_transaction(root, 5);
6174 if (IS_ERR(trans))
6175 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006176
Li Zefan581bb052011-04-20 10:06:11 +08006177 err = btrfs_find_free_ino(root, &objectid);
6178 if (err)
6179 goto out_unlock;
6180
Josef Bacikaec74772008-07-24 12:12:38 -04006181 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006182 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6183 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006184 if (IS_ERR(inode)) {
6185 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006186 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006187 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006188 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006189 /*
6190 * If the active LSM wants to access the inode during
6191 * d_instantiate it needs these. Smack checks to see
6192 * if the filesystem supports xattrs by looking at the
6193 * ops vector.
6194 */
6195 inode->i_fop = &btrfs_file_operations;
6196 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006197 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006198
6199 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6200 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006201 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006202
6203 err = btrfs_update_inode(trans, root, inode);
6204 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006205 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006206
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006207 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6208 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006209 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006210 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006211
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006212 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006213 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006214
Chris Mason39279cc2007-06-12 06:35:45 -04006215out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006216 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006217 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006218 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006219 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006220 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006221 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006222 return err;
6223}
6224
6225static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6226 struct dentry *dentry)
6227{
Filipe Manana271dba452016-01-05 16:24:05 +00006228 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006229 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006230 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006231 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006232 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006233 int err;
6234 int drop_inode = 0;
6235
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006236 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006237 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006238 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006239
Mark Fashehf1863732012-08-08 11:32:27 -07006240 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006241 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006242
Nikolay Borisov877574e2017-02-20 13:50:33 +02006243 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006244 if (err)
6245 goto fail;
6246
Yan, Zhenga22285a2010-05-16 10:48:46 -04006247 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006248 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006249 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006250 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006251 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006252 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006253 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006254 if (IS_ERR(trans)) {
6255 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006256 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006257 goto fail;
6258 }
Chris Mason5f39d392007-10-15 16:14:19 -04006259
Miao Xie67de1172013-12-26 13:07:06 +08006260 /* There are several dir indexes for this inode, clear the cache. */
6261 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006262 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006263 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006264 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006265 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006266 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006267
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006268 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6269 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006270
Yan, Zhenga5719522009-09-24 09:17:31 -04006271 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006272 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006273 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006274 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006275 int ret;
6276
Yan, Zhenga5719522009-09-24 09:17:31 -04006277 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006278 if (err)
6279 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006280 if (inode->i_nlink == 1) {
6281 /*
6282 * If new hard link count is 1, it's a file created
6283 * with open(2) O_TMPFILE flag.
6284 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006285 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006286 if (err)
6287 goto fail;
6288 }
Al Viro08c422c2011-12-23 07:58:13 -05006289 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006290 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6291 true, NULL);
6292 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6293 err = btrfs_commit_transaction(trans);
6294 trans = NULL;
6295 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006296 }
Chris Mason39279cc2007-06-12 06:35:45 -04006297
Chris Mason1832a6d2007-12-21 16:27:21 -05006298fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006299 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006300 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006301 if (drop_inode) {
6302 inode_dec_link_count(inode);
6303 iput(inode);
6304 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006305 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006306 return err;
6307}
6308
Al Viro18bb1db2011-07-26 01:41:39 -04006309static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006310{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006311 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006312 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006313 struct btrfs_trans_handle *trans;
6314 struct btrfs_root *root = BTRFS_I(dir)->root;
6315 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006316 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006317 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006318
Josef Bacik9ed74f22009-09-11 16:12:44 -04006319 /*
6320 * 2 items for inode and ref
6321 * 2 items for dir items
6322 * 1 for xattr if selinux is on
6323 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006324 trans = btrfs_start_transaction(root, 5);
6325 if (IS_ERR(trans))
6326 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006327
Li Zefan581bb052011-04-20 10:06:11 +08006328 err = btrfs_find_free_ino(root, &objectid);
6329 if (err)
6330 goto out_fail;
6331
Josef Bacikaec74772008-07-24 12:12:38 -04006332 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006333 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6334 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006335 if (IS_ERR(inode)) {
6336 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006337 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006338 goto out_fail;
6339 }
Chris Mason5f39d392007-10-15 16:14:19 -04006340
Chris Masonb0d5d102014-09-08 13:08:51 -07006341 /* these must be set before we unlock the inode */
6342 inode->i_op = &btrfs_dir_inode_operations;
6343 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006344
Eric Paris2a7dba32011-02-01 11:05:39 -05006345 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006346 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006347 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006348
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006349 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006350 err = btrfs_update_inode(trans, root, inode);
6351 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006352 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006353
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006354 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6355 dentry->d_name.name,
6356 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006357 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006358 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006359
Al Viro1e2e5472018-05-04 08:23:01 -04006360 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006361
6362out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006363 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006364 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006365 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006366 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006367 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006368 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006369 return err;
6370}
6371
Chris Masonc8b97812008-10-29 14:49:59 -04006372static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006373 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006374 size_t pg_offset, u64 extent_offset,
6375 struct btrfs_file_extent_item *item)
6376{
6377 int ret;
6378 struct extent_buffer *leaf = path->nodes[0];
6379 char *tmp;
6380 size_t max_size;
6381 unsigned long inline_size;
6382 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006383 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006384
6385 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006386 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006387 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6388 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006389 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006390 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006391 if (!tmp)
6392 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006393 ptr = btrfs_file_extent_inline_start(item);
6394
6395 read_extent_buffer(leaf, tmp, ptr, inline_size);
6396
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006397 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006398 ret = btrfs_decompress(compress_type, tmp, page,
6399 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006400
6401 /*
6402 * decompression code contains a memset to fill in any space between the end
6403 * of the uncompressed data and the end of max_size in case the decompressed
6404 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6405 * the end of an inline extent and the beginning of the next block, so we
6406 * cover that region here.
6407 */
6408
6409 if (max_size + pg_offset < PAGE_SIZE) {
6410 char *map = kmap(page);
6411 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6412 kunmap(page);
6413 }
Chris Masonc8b97812008-10-29 14:49:59 -04006414 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006415 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006416}
6417
Omar Sandoval39b07b52019-12-02 17:34:23 -08006418/**
6419 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6420 * @inode: file to search in
6421 * @page: page to read extent data into if the extent is inline
6422 * @pg_offset: offset into @page to copy to
6423 * @start: file offset
6424 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006425 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006426 * This returns the first &struct extent_map which overlaps with the given
6427 * range, reading it from the B-tree and caching it if necessary. Note that
6428 * there may be more extents which overlap the given range after the returned
6429 * extent_map.
6430 *
6431 * If @page is not NULL and the extent is inline, this also reads the extent
6432 * data directly into the page and marks the extent up to date in the io_tree.
6433 *
6434 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006435 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006436struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006437 struct page *page, size_t pg_offset,
6438 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006439{
David Sterba3ffbd682018-06-29 10:56:42 +02006440 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006441 int ret;
6442 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006443 u64 extent_start = 0;
6444 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006445 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006446 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006447 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006448 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006449 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006450 struct extent_buffer *leaf;
6451 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006452 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006453 struct extent_map_tree *em_tree = &inode->extent_tree;
6454 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006455
Chris Mason890871b2009-09-02 16:24:52 -04006456 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006457 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006458 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006459
Chris Masona52d9a82007-08-27 16:49:44 -04006460 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006461 if (em->start > start || em->start + em->len <= start)
6462 free_extent_map(em);
6463 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006464 free_extent_map(em);
6465 else
6466 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006467 }
David Sterba172ddd62011-04-21 00:48:27 +02006468 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006469 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006470 err = -ENOMEM;
6471 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006472 }
Chris Masond1310b22008-01-24 16:13:08 -05006473 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006474 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006475 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006476 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006477
Liu Bobee6ec82018-08-17 05:05:28 +08006478 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006479 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006480 err = -ENOMEM;
6481 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006482 }
6483
Liu Bobee6ec82018-08-17 05:05:28 +08006484 /* Chances are we'll be called again, so go ahead and do readahead */
6485 path->reada = READA_FORWARD;
6486
Liu Boe49aabd2018-08-25 13:47:09 +08006487 /*
6488 * Unless we're going to uncompress the inline extent, no sleep would
6489 * happen.
6490 */
6491 path->leave_spinning = 1;
6492
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006493 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006494 if (ret < 0) {
6495 err = ret;
6496 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006497 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006498 if (path->slots[0] == 0)
6499 goto not_found;
6500 path->slots[0]--;
6501 }
6502
Chris Mason5f39d392007-10-15 16:14:19 -04006503 leaf = path->nodes[0];
6504 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006505 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006506 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006507 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006508 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006509 /*
6510 * If we backup past the first extent we want to move forward
6511 * and see if there is an extent in front of us, otherwise we'll
6512 * say there is a hole for our whole search range which can
6513 * cause problems.
6514 */
6515 extent_end = start;
6516 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006517 }
6518
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006519 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006520 extent_start = found_key.offset;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006521 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6522 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006523 /* Only regular file could have regular/prealloc extent */
6524 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6525 ret = -EUCLEAN;
6526 btrfs_crit(fs_info,
6527 "regular/prealloc extent found for non-regular inode %llu",
6528 btrfs_ino(inode));
6529 goto out;
6530 }
Chris Masona52d9a82007-08-27 16:49:44 -04006531 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04006532 btrfs_file_extent_num_bytes(leaf, item);
Liu Bo09ed2f12017-03-10 11:09:48 -08006533
6534 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6535 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006536 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan Zheng9036c102008-10-30 14:19:41 -04006537 size_t size;
Qu Wenruoe41ca582018-06-06 15:41:49 +08006538
6539 size = btrfs_file_extent_ram_bytes(leaf, item);
Jeff Mahoneyda170662016-06-15 09:22:56 -04006540 extent_end = ALIGN(extent_start + size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006541 fs_info->sectorsize);
Liu Bo09ed2f12017-03-10 11:09:48 -08006542
6543 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6544 path->slots[0],
6545 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006546 }
Josef Bacik25a50342013-10-14 12:08:38 -04006547next:
Yan Zheng9036c102008-10-30 14:19:41 -04006548 if (start >= extent_end) {
6549 path->slots[0]++;
6550 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6551 ret = btrfs_next_leaf(root, path);
6552 if (ret < 0) {
6553 err = ret;
6554 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006555 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006556 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006557 }
Yan Zheng9036c102008-10-30 14:19:41 -04006558 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006559 }
Yan Zheng9036c102008-10-30 14:19:41 -04006560 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6561 if (found_key.objectid != objectid ||
6562 found_key.type != BTRFS_EXTENT_DATA_KEY)
6563 goto not_found;
6564 if (start + len <= found_key.offset)
6565 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006566 if (start > found_key.offset)
6567 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006568
6569 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006570 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006571 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006572 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006573 em->block_start = EXTENT_MAP_HOLE;
6574 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006575 }
6576
Omar Sandoval39b07b52019-12-02 17:34:23 -08006577 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006578
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006579 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6580 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006581 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006582 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006583 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006584 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006585 size_t size;
6586 size_t extent_offset;
6587 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006588
Omar Sandoval39b07b52019-12-02 17:34:23 -08006589 if (!page)
Yan689f9342007-10-29 11:41:07 -04006590 goto out;
Yan689f9342007-10-29 11:41:07 -04006591
Qu Wenruoe41ca582018-06-06 15:41:49 +08006592 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006593 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006594 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6595 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006596 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006597 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006598 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006599 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006600 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006601
6602 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006603 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006604 if (btrfs_file_extent_compression(leaf, item) !=
6605 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006606 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006607 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006608 if (ret) {
6609 err = ret;
6610 goto out;
6611 }
Chris Masonc8b97812008-10-29 14:49:59 -04006612 } else {
6613 map = kmap(page);
6614 read_extent_buffer(leaf, map + pg_offset, ptr,
6615 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006616 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006617 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006618 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006619 copy_size);
6620 }
Chris Masonc8b97812008-10-29 14:49:59 -04006621 kunmap(page);
6622 }
Chris Mason179e29e2007-11-01 11:28:41 -04006623 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006624 }
Chris Masond1310b22008-01-24 16:13:08 -05006625 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006626 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006627 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006628 }
6629not_found:
6630 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006631 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006632 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006633 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006634insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006635 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006636 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006637 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006638 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6639 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006640 err = -EIO;
6641 goto out;
6642 }
Chris Masond1310b22008-01-24 16:13:08 -05006643
6644 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006645 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006646 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006647 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006648out:
Liu Boc6414282018-08-23 07:36:17 +08006649 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006650
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006651 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006652
Chris Masona52d9a82007-08-27 16:49:44 -04006653 if (err) {
6654 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006655 return ERR_PTR(err);
6656 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006657 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006658 return em;
6659}
6660
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006661struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006662 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006663{
6664 struct extent_map *em;
6665 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006666 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006667 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006668 u64 delalloc_len;
6669 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006670 int err = 0;
6671
Omar Sandoval39b07b52019-12-02 17:34:23 -08006672 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006673 if (IS_ERR(em))
6674 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006675 /*
6676 * If our em maps to:
6677 * - a hole or
6678 * - a pre-alloc extent,
6679 * there might actually be delalloc bytes behind it.
6680 */
6681 if (em->block_start != EXTENT_MAP_HOLE &&
6682 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6683 return em;
6684 else
6685 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006686
6687 /* check to see if we've wrapped (len == -1 or similar) */
6688 end = start + len;
6689 if (end < start)
6690 end = (u64)-1;
6691 else
6692 end -= 1;
6693
6694 em = NULL;
6695
6696 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006697 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006698 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006699 delalloc_end = delalloc_start + delalloc_len;
6700 if (delalloc_end < delalloc_start)
6701 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006702
6703 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006704 * We didn't find anything useful, return the original results from
6705 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006706 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006707 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006708 em = hole_em;
6709 hole_em = NULL;
6710 goto out;
6711 }
6712
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006713 /*
6714 * Adjust the delalloc_start to make sure it doesn't go backwards from
6715 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006716 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006717 delalloc_start = max(start, delalloc_start);
6718 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006719
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006720 if (delalloc_len > 0) {
6721 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006722 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006723 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006724
David Sterba172ddd62011-04-21 00:48:27 +02006725 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006726 if (!em) {
6727 err = -ENOMEM;
6728 goto out;
6729 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006730
6731 ASSERT(hole_em);
6732 /*
6733 * When btrfs_get_extent can't find anything it returns one
6734 * huge hole
6735 *
6736 * Make sure what it found really fits our range, and adjust to
6737 * make sure it is based on the start from the caller
6738 */
6739 if (hole_end <= start || hole_em->start > end) {
6740 free_extent_map(hole_em);
6741 hole_em = NULL;
6742 } else {
6743 hole_start = max(hole_em->start, start);
6744 hole_len = hole_end - hole_start;
6745 }
6746
6747 if (hole_em && delalloc_start > hole_start) {
6748 /*
6749 * Our hole starts before our delalloc, so we have to
6750 * return just the parts of the hole that go until the
6751 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006752 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006753 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006754 em->start = hole_start;
6755 em->orig_start = hole_start;
6756 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006757 * Don't adjust block start at all, it is fixed at
6758 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006759 */
6760 em->block_start = hole_em->block_start;
6761 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006762 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6763 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006764 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006765 /*
6766 * Hole is out of passed range or it starts after
6767 * delalloc range
6768 */
6769 em->start = delalloc_start;
6770 em->len = delalloc_len;
6771 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006772 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006773 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006774 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006775 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006776 return hole_em;
6777 }
6778out:
6779
6780 free_extent_map(hole_em);
6781 if (err) {
6782 free_extent_map(em);
6783 return ERR_PTR(err);
6784 }
6785 return em;
6786}
6787
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006788static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
6789 const u64 start,
6790 const u64 len,
6791 const u64 orig_start,
6792 const u64 block_start,
6793 const u64 block_len,
6794 const u64 orig_block_len,
6795 const u64 ram_bytes,
6796 const int type)
6797{
6798 struct extent_map *em = NULL;
6799 int ret;
6800
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006801 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08006802 em = create_io_em(inode, start, len, orig_start,
6803 block_start, block_len, orig_block_len,
6804 ram_bytes,
6805 BTRFS_COMPRESS_NONE, /* compress_type */
6806 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006807 if (IS_ERR(em))
6808 goto out;
6809 }
6810 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
6811 len, block_len, type);
6812 if (ret) {
6813 if (em) {
6814 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02006815 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006816 start + len - 1, 0);
6817 }
6818 em = ERR_PTR(ret);
6819 }
6820 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006821
6822 return em;
6823}
6824
Josef Bacik4b46fce2010-05-23 11:00:55 -04006825static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6826 u64 start, u64 len)
6827{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006828 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006829 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006830 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006831 struct btrfs_key ins;
6832 u64 alloc_hint;
6833 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006834
Josef Bacik4b46fce2010-05-23 11:00:55 -04006835 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006836 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006837 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006838 if (ret)
6839 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006840
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006841 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
6842 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006843 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006844 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006845 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006846 btrfs_free_reserved_extent(fs_info, ins.objectid,
6847 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006848
Josef Bacik4b46fce2010-05-23 11:00:55 -04006849 return em;
6850}
6851
Chris Mason46bfbb52010-05-26 11:04:10 -04006852/*
6853 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6854 * block must be cow'd
6855 */
Josef Bacik00361582013-08-14 14:02:47 -04006856noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006857 u64 *orig_start, u64 *orig_block_len,
6858 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006859{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006860 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006861 struct btrfs_path *path;
6862 int ret;
6863 struct extent_buffer *leaf;
6864 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006865 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006866 struct btrfs_file_extent_item *fi;
6867 struct btrfs_key key;
6868 u64 disk_bytenr;
6869 u64 backref_offset;
6870 u64 extent_end;
6871 u64 num_bytes;
6872 int slot;
6873 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006874 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006875
Chris Mason46bfbb52010-05-26 11:04:10 -04006876 path = btrfs_alloc_path();
6877 if (!path)
6878 return -ENOMEM;
6879
David Sterbaf85b7372017-01-20 14:54:07 +01006880 ret = btrfs_lookup_file_extent(NULL, root, path,
6881 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006882 if (ret < 0)
6883 goto out;
6884
6885 slot = path->slots[0];
6886 if (ret == 1) {
6887 if (slot == 0) {
6888 /* can't find the item, must cow */
6889 ret = 0;
6890 goto out;
6891 }
6892 slot--;
6893 }
6894 ret = 0;
6895 leaf = path->nodes[0];
6896 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006897 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006898 key.type != BTRFS_EXTENT_DATA_KEY) {
6899 /* not our file or wrong item type, must cow */
6900 goto out;
6901 }
6902
6903 if (key.offset > offset) {
6904 /* Wrong offset, must cow */
6905 goto out;
6906 }
6907
6908 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6909 found_type = btrfs_file_extent_type(leaf, fi);
6910 if (found_type != BTRFS_FILE_EXTENT_REG &&
6911 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6912 /* not a regular extent, must cow */
6913 goto out;
6914 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006915
6916 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6917 goto out;
6918
Miao Xiee77751a2013-12-27 21:11:50 +08006919 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6920 if (extent_end <= offset)
6921 goto out;
6922
Chris Mason46bfbb52010-05-26 11:04:10 -04006923 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006924 if (disk_bytenr == 0)
6925 goto out;
6926
6927 if (btrfs_file_extent_compression(leaf, fi) ||
6928 btrfs_file_extent_encryption(leaf, fi) ||
6929 btrfs_file_extent_other_encoding(leaf, fi))
6930 goto out;
6931
Ethan Lien78d42952018-05-17 14:58:29 +08006932 /*
6933 * Do the same check as in btrfs_cross_ref_exist but without the
6934 * unnecessary search.
6935 */
6936 if (btrfs_file_extent_generation(leaf, fi) <=
6937 btrfs_root_last_snapshot(&root->root_item))
6938 goto out;
6939
Chris Mason46bfbb52010-05-26 11:04:10 -04006940 backref_offset = btrfs_file_extent_offset(leaf, fi);
6941
Josef Bacik7ee9e442013-06-21 16:37:03 -04006942 if (orig_start) {
6943 *orig_start = key.offset - backref_offset;
6944 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6945 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6946 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006947
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006948 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04006949 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006950
6951 num_bytes = min(offset + *len, extent_end) - offset;
6952 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6953 u64 range_end;
6954
Jeff Mahoneyda170662016-06-15 09:22:56 -04006955 range_end = round_up(offset + num_bytes,
6956 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08006957 ret = test_range_bit(io_tree, offset, range_end,
6958 EXTENT_DELALLOC, 0, NULL);
6959 if (ret) {
6960 ret = -EAGAIN;
6961 goto out;
6962 }
6963 }
6964
Josef Bacik1bda19e2013-10-18 12:10:36 -04006965 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04006966
6967 /*
6968 * look for other files referencing this extent, if we
6969 * find any we must cow
6970 */
Josef Bacik00361582013-08-14 14:02:47 -04006971
Liu Boe4c3b2d2017-01-30 12:25:28 -08006972 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04006973 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04006974 if (ret) {
6975 ret = 0;
6976 goto out;
6977 }
Chris Mason46bfbb52010-05-26 11:04:10 -04006978
6979 /*
6980 * adjust disk_bytenr and num_bytes to cover just the bytes
6981 * in this extent we are about to write. If there
6982 * are any csums in that range we have to cow in order
6983 * to keep the csums correct
6984 */
6985 disk_bytenr += backref_offset;
6986 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006987 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
6988 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04006989 /*
6990 * all of the above have passed, it is safe to overwrite this extent
6991 * without cow
6992 */
Josef Bacikeb384b52013-04-24 16:32:55 -04006993 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04006994 ret = 1;
6995out:
6996 btrfs_free_path(path);
6997 return ret;
6998}
6999
Josef Bacikeb838e72012-07-31 16:28:48 -04007000static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7001 struct extent_state **cached_state, int writing)
7002{
7003 struct btrfs_ordered_extent *ordered;
7004 int ret = 0;
7005
7006 while (1) {
7007 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007008 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007009 /*
7010 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007011 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007012 * extents in this range.
7013 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007014 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007015 lockend - lockstart + 1);
7016
7017 /*
7018 * We need to make sure there are no buffered pages in this
7019 * range either, we could have raced between the invalidate in
7020 * generic_file_direct_write and locking the extent. The
7021 * invalidate needs to happen so that reads after a write do not
7022 * get stale data.
7023 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007024 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007025 (!writing || !filemap_range_has_page(inode->i_mapping,
7026 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007027 break;
7028
7029 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007030 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007031
7032 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007033 /*
7034 * If we are doing a DIO read and the ordered extent we
7035 * found is for a buffered write, we can not wait for it
7036 * to complete and retry, because if we do so we can
7037 * deadlock with concurrent buffered writes on page
7038 * locks. This happens only if our DIO read covers more
7039 * than one extent map, if at this point has already
7040 * created an ordered extent for a previous extent map
7041 * and locked its range in the inode's io tree, and a
7042 * concurrent write against that previous extent map's
7043 * range and this range started (we unlock the ranges
7044 * in the io tree only when the bios complete and
7045 * buffered writes always lock pages before attempting
7046 * to lock range in the io tree).
7047 */
7048 if (writing ||
7049 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7050 btrfs_start_ordered_extent(inode, ordered, 1);
7051 else
7052 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007053 btrfs_put_ordered_extent(ordered);
7054 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007055 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007056 * We could trigger writeback for this range (and wait
7057 * for it to complete) and then invalidate the pages for
7058 * this range (through invalidate_inode_pages2_range()),
7059 * but that can lead us to a deadlock with a concurrent
7060 * call to readpages() (a buffered read or a defrag call
7061 * triggered a readahead) on a page lock due to an
7062 * ordered dio extent we created before but did not have
7063 * yet a corresponding bio submitted (whence it can not
7064 * complete), which makes readpages() wait for that
7065 * ordered extent to complete while holding a lock on
7066 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007067 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007068 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007069 }
7070
Filipe Mananaade77022016-02-18 14:28:55 +00007071 if (ret)
7072 break;
7073
Josef Bacikeb838e72012-07-31 16:28:48 -04007074 cond_resched();
7075 }
7076
7077 return ret;
7078}
7079
Liu Bo6f9994d2017-01-31 07:50:22 -08007080/* The callers of this must take lock_extent() */
7081static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7082 u64 orig_start, u64 block_start,
7083 u64 block_len, u64 orig_block_len,
7084 u64 ram_bytes, int compress_type,
7085 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007086{
7087 struct extent_map_tree *em_tree;
7088 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007089 int ret;
7090
Liu Bo6f9994d2017-01-31 07:50:22 -08007091 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7092 type == BTRFS_ORDERED_COMPRESSED ||
7093 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007094 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007095
Josef Bacik69ffb542012-09-11 15:40:07 -04007096 em_tree = &BTRFS_I(inode)->extent_tree;
7097 em = alloc_extent_map();
7098 if (!em)
7099 return ERR_PTR(-ENOMEM);
7100
7101 em->start = start;
7102 em->orig_start = orig_start;
7103 em->len = len;
7104 em->block_len = block_len;
7105 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007106 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007107 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007108 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007109 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007110 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007111 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007112 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007113 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7114 em->compress_type = compress_type;
7115 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007116
7117 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007118 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007119 em->start + em->len - 1, 0);
7120 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007121 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007122 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007123 /*
7124 * The caller has taken lock_extent(), who could race with us
7125 * to add em?
7126 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007127 } while (ret == -EEXIST);
7128
7129 if (ret) {
7130 free_extent_map(em);
7131 return ERR_PTR(ret);
7132 }
7133
Liu Bo6f9994d2017-01-31 07:50:22 -08007134 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007135 return em;
7136}
7137
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007138
7139static int btrfs_get_blocks_direct_read(struct extent_map *em,
7140 struct buffer_head *bh_result,
7141 struct inode *inode,
7142 u64 start, u64 len)
7143{
David Sterba8530c372019-08-30 15:42:07 +02007144 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7145
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007146 if (em->block_start == EXTENT_MAP_HOLE ||
7147 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7148 return -ENOENT;
7149
7150 len = min(len, em->len - (start - em->start));
7151
7152 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7153 inode->i_blkbits;
7154 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007155 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007156 set_buffer_mapped(bh_result);
7157
7158 return 0;
7159}
7160
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007161static int btrfs_get_blocks_direct_write(struct extent_map **map,
7162 struct buffer_head *bh_result,
7163 struct inode *inode,
7164 struct btrfs_dio_data *dio_data,
7165 u64 start, u64 len)
7166{
7167 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7168 struct extent_map *em = *map;
7169 int ret = 0;
7170
7171 /*
7172 * We don't allocate a new extent in the following cases
7173 *
7174 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7175 * existing extent.
7176 * 2) The extent is marked as PREALLOC. We're good to go here and can
7177 * just use the extent.
7178 *
7179 */
7180 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7181 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7182 em->block_start != EXTENT_MAP_HOLE)) {
7183 int type;
7184 u64 block_start, orig_start, orig_block_len, ram_bytes;
7185
7186 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7187 type = BTRFS_ORDERED_PREALLOC;
7188 else
7189 type = BTRFS_ORDERED_NOCOW;
7190 len = min(len, em->len - (start - em->start));
7191 block_start = em->block_start + (start - em->start);
7192
7193 if (can_nocow_extent(inode, start, &len, &orig_start,
7194 &orig_block_len, &ram_bytes) == 1 &&
7195 btrfs_inc_nocow_writers(fs_info, block_start)) {
7196 struct extent_map *em2;
7197
7198 em2 = btrfs_create_dio_extent(inode, start, len,
7199 orig_start, block_start,
7200 len, orig_block_len,
7201 ram_bytes, type);
7202 btrfs_dec_nocow_writers(fs_info, block_start);
7203 if (type == BTRFS_ORDERED_PREALLOC) {
7204 free_extent_map(em);
7205 *map = em = em2;
7206 }
7207
7208 if (em2 && IS_ERR(em2)) {
7209 ret = PTR_ERR(em2);
7210 goto out;
7211 }
7212 /*
7213 * For inode marked NODATACOW or extent marked PREALLOC,
7214 * use the existing or preallocated extent, so does not
7215 * need to adjust btrfs_space_info's bytes_may_use.
7216 */
7217 btrfs_free_reserved_data_space_noquota(inode, start,
7218 len);
7219 goto skip_cow;
7220 }
7221 }
7222
7223 /* this will cow the extent */
7224 len = bh_result->b_size;
7225 free_extent_map(em);
7226 *map = em = btrfs_new_extent_direct(inode, start, len);
7227 if (IS_ERR(em)) {
7228 ret = PTR_ERR(em);
7229 goto out;
7230 }
7231
7232 len = min(len, em->len - (start - em->start));
7233
7234skip_cow:
7235 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7236 inode->i_blkbits;
7237 bh_result->b_size = len;
David Sterba8530c372019-08-30 15:42:07 +02007238 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007239 set_buffer_mapped(bh_result);
7240
7241 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7242 set_buffer_new(bh_result);
7243
7244 /*
7245 * Need to update the i_size under the extent lock so buffered
7246 * readers will get the updated i_size when we unlock.
7247 */
7248 if (!dio_data->overwrite && start + len > i_size_read(inode))
7249 i_size_write(inode, start + len);
7250
7251 WARN_ON(dio_data->reserve < len);
7252 dio_data->reserve -= len;
7253 dio_data->unsubmitted_oe_range_end = start + len;
7254 current->journal_info = dio_data;
7255out:
7256 return ret;
7257}
7258
Josef Bacik4b46fce2010-05-23 11:00:55 -04007259static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7260 struct buffer_head *bh_result, int create)
7261{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007262 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007263 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007264 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307265 struct btrfs_dio_data *dio_data = NULL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007266 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007267 u64 lockstart, lockend;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007268 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007269 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007270
Omar Sandovale1821632019-08-15 14:04:04 -07007271 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007272 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007273
Josef Bacikc3298612012-08-03 16:49:19 -04007274 lockstart = start;
7275 lockend = start + len - 1;
7276
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007277 if (current->journal_info) {
7278 /*
7279 * Need to pull our outstanding extents and set journal_info to NULL so
Nicholas D Steeves01327612016-05-19 21:18:45 -04007280 * that anything that needs to check if there's a transaction doesn't get
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007281 * confused.
7282 */
chandan50745b02015-08-28 21:10:13 +05307283 dio_data = current->journal_info;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007284 current->journal_info = NULL;
7285 }
7286
Josef Bacikeb838e72012-07-31 16:28:48 -04007287 /*
7288 * If this errors out it's because we couldn't invalidate pagecache for
7289 * this range and we need to fallback to buffered.
7290 */
Filipe Manana9c9464c2015-11-04 09:52:04 +00007291 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7292 create)) {
7293 ret = -ENOTBLK;
7294 goto err;
7295 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007296
Omar Sandoval39b07b52019-12-02 17:34:23 -08007297 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007298 if (IS_ERR(em)) {
7299 ret = PTR_ERR(em);
7300 goto unlock_err;
7301 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007302
7303 /*
7304 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7305 * io. INLINE is special, and we could probably kludge it in here, but
7306 * it's still buffered so for safety lets just fall back to the generic
7307 * buffered path.
7308 *
7309 * For COMPRESSED we _have_ to read the entire extent in so we can
7310 * decompress it, so there will be buffering required no matter what we
7311 * do, so go ahead and fallback to buffered.
7312 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007313 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007314 * to buffered IO. Don't blame me, this is the price we pay for using
7315 * the generic code.
7316 */
7317 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7318 em->block_start == EXTENT_MAP_INLINE) {
7319 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007320 ret = -ENOTBLK;
7321 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007322 }
7323
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007324 if (create) {
7325 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7326 dio_data, start, len);
7327 if (ret < 0)
7328 goto unlock_err;
7329
Omar Sandovale1821632019-08-15 14:04:04 -07007330 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7331 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007332 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007333 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7334 start, len);
7335 /* Can be negative only if we read from a hole */
7336 if (ret < 0) {
7337 ret = 0;
7338 free_extent_map(em);
7339 goto unlock_err;
7340 }
7341 /*
7342 * We need to unlock only the end area that we aren't using.
7343 * The rest is going to be unlocked by the endio routine.
7344 */
7345 lockstart = start + bh_result->b_size;
7346 if (lockstart < lockend) {
Omar Sandovale1821632019-08-15 14:04:04 -07007347 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7348 lockstart, lockend, &cached_state);
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007349 } else {
7350 free_extent_state(cached_state);
7351 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007352 }
7353
Josef Bacik4b46fce2010-05-23 11:00:55 -04007354 free_extent_map(em);
7355
7356 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007357
7358unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007359 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7360 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007361err:
chandan50745b02015-08-28 21:10:13 +05307362 if (dio_data)
7363 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007364 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007365}
7366
Omar Sandoval58efbc92017-08-22 23:45:59 -07007367static inline blk_status_t submit_dio_repair_bio(struct inode *inode,
7368 struct bio *bio,
7369 int mirror_num)
Miao Xie8b110e32014-09-12 18:44:03 +08007370{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007371 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007372 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007373
Mike Christie37226b22016-06-05 14:31:52 -05007374 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007375
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007376 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
Miao Xie8b110e32014-09-12 18:44:03 +08007377 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007378 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007379
Chris Mason08635ba2019-07-10 12:28:14 -07007380 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Nikolay Borisovea057f62017-12-13 10:25:38 +02007381
Miao Xie8b110e32014-09-12 18:44:03 +08007382 return ret;
7383}
7384
7385static int btrfs_check_dio_repairable(struct inode *inode,
7386 struct bio *failed_bio,
7387 struct io_failure_record *failrec,
7388 int failed_mirror)
7389{
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007390 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xie8b110e32014-09-12 18:44:03 +08007391 int num_copies;
7392
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007393 num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
Miao Xie8b110e32014-09-12 18:44:03 +08007394 if (num_copies == 1) {
7395 /*
7396 * we only have a single copy of the data, so don't bother with
7397 * all the retry and error correction code that follows. no
7398 * matter what the error is, it is very likely to persist.
7399 */
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007400 btrfs_debug(fs_info,
7401 "Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7402 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007403 return 0;
7404 }
7405
7406 failrec->failed_mirror = failed_mirror;
7407 failrec->this_mirror++;
7408 if (failrec->this_mirror == failed_mirror)
7409 failrec->this_mirror++;
7410
7411 if (failrec->this_mirror > num_copies) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -04007412 btrfs_debug(fs_info,
7413 "Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7414 num_copies, failrec->this_mirror, failed_mirror);
Miao Xie8b110e32014-09-12 18:44:03 +08007415 return 0;
7416 }
7417
7418 return 1;
7419}
7420
Omar Sandoval58efbc92017-08-22 23:45:59 -07007421static blk_status_t dio_read_error(struct inode *inode, struct bio *failed_bio,
7422 struct page *page, unsigned int pgoff,
7423 u64 start, u64 end, int failed_mirror,
7424 bio_end_io_t *repair_endio, void *repair_arg)
Miao Xie8b110e32014-09-12 18:44:03 +08007425{
7426 struct io_failure_record *failrec;
Josef Bacik7870d082017-05-05 11:57:15 -04007427 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7428 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Miao Xie8b110e32014-09-12 18:44:03 +08007429 struct bio *bio;
7430 int isector;
David Sterbaf1c77c52017-06-06 19:03:49 +02007431 unsigned int read_mode = 0;
Liu Bo17347ce2017-05-15 15:33:27 -07007432 int segs;
Miao Xie8b110e32014-09-12 18:44:03 +08007433 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007434 blk_status_t status;
Ming Leic16a8ac2017-12-18 20:22:12 +08007435 struct bio_vec bvec;
Miao Xie8b110e32014-09-12 18:44:03 +08007436
Mike Christie37226b22016-06-05 14:31:52 -05007437 BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007438
7439 ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7440 if (ret)
Omar Sandoval58efbc92017-08-22 23:45:59 -07007441 return errno_to_blk_status(ret);
Miao Xie8b110e32014-09-12 18:44:03 +08007442
7443 ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7444 failed_mirror);
7445 if (!ret) {
Josef Bacik7870d082017-05-05 11:57:15 -04007446 free_io_failure(failure_tree, io_tree, failrec);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007447 return BLK_STS_IOERR;
Miao Xie8b110e32014-09-12 18:44:03 +08007448 }
7449
Liu Bo17347ce2017-05-15 15:33:27 -07007450 segs = bio_segments(failed_bio);
Ming Leic16a8ac2017-12-18 20:22:12 +08007451 bio_get_first_bvec(failed_bio, &bvec);
Liu Bo17347ce2017-05-15 15:33:27 -07007452 if (segs > 1 ||
Ming Leic16a8ac2017-12-18 20:22:12 +08007453 (bvec.bv_len > btrfs_inode_sectorsize(inode)))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06007454 read_mode |= REQ_FAILFAST_DEV;
Miao Xie8b110e32014-09-12 18:44:03 +08007455
7456 isector = start - btrfs_io_bio(failed_bio)->logical;
7457 isector >>= inode->i_sb->s_blocksize_bits;
7458 bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307459 pgoff, isector, repair_endio, repair_arg);
David Sterbaebcc3262018-06-29 10:56:53 +02007460 bio->bi_opf = REQ_OP_READ | read_mode;
Miao Xie8b110e32014-09-12 18:44:03 +08007461
7462 btrfs_debug(BTRFS_I(inode)->root->fs_info,
David Sterba913e1532017-07-13 15:32:18 +02007463 "repair DIO read error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d",
Miao Xie8b110e32014-09-12 18:44:03 +08007464 read_mode, failrec->this_mirror, failrec->in_validation);
7465
Omar Sandoval58efbc92017-08-22 23:45:59 -07007466 status = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7467 if (status) {
Josef Bacik7870d082017-05-05 11:57:15 -04007468 free_io_failure(failure_tree, io_tree, failrec);
Miao Xie8b110e32014-09-12 18:44:03 +08007469 bio_put(bio);
7470 }
7471
Omar Sandoval58efbc92017-08-22 23:45:59 -07007472 return status;
Miao Xie8b110e32014-09-12 18:44:03 +08007473}
7474
7475struct btrfs_retry_complete {
7476 struct completion done;
7477 struct inode *inode;
7478 u64 start;
7479 int uptodate;
7480};
7481
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007482static void btrfs_retry_endio_nocsum(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007483{
7484 struct btrfs_retry_complete *done = bio->bi_private;
Josef Bacik7870d082017-05-05 11:57:15 -04007485 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007486 struct bio_vec *bvec;
Josef Bacik7870d082017-05-05 11:57:15 -04007487 struct extent_io_tree *io_tree, *failure_tree;
Ming Lei6dc4f102019-02-15 19:13:19 +08007488 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007489
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007490 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007491 goto end;
7492
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307493 ASSERT(bio->bi_vcnt == 1);
Josef Bacik7870d082017-05-05 11:57:15 -04007494 io_tree = &BTRFS_I(inode)->io_tree;
7495 failure_tree = &BTRFS_I(inode)->io_failure_tree;
Ming Lei263663c2017-12-18 20:22:04 +08007496 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307497
Miao Xie8b110e32014-09-12 18:44:03 +08007498 done->uptodate = 1;
David Sterbac09abff2017-07-13 18:10:07 +02007499 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007500 bio_for_each_segment_all(bvec, bio, iter_all)
Josef Bacik7870d082017-05-05 11:57:15 -04007501 clean_io_failure(BTRFS_I(inode)->root->fs_info, failure_tree,
7502 io_tree, done->start, bvec->bv_page,
7503 btrfs_ino(BTRFS_I(inode)), 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007504end:
7505 complete(&done->done);
7506 bio_put(bio);
7507}
7508
Omar Sandoval58efbc92017-08-22 23:45:59 -07007509static blk_status_t __btrfs_correct_data_nocsum(struct inode *inode,
7510 struct btrfs_io_bio *io_bio)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007511{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307512 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007513 struct bio_vec bvec;
7514 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007515 struct btrfs_retry_complete done;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007516 u64 start;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307517 unsigned int pgoff;
7518 u32 sectorsize;
7519 int nr_sectors;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007520 blk_status_t ret;
7521 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007522
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307523 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007524 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307525
Miao Xiec1dc0892014-09-12 18:43:56 +08007526 start = io_bio->logical;
Miao Xie8b110e32014-09-12 18:44:03 +08007527 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007528 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007529
Liu Bo17347ce2017-05-15 15:33:27 -07007530 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7531 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7532 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307533
7534next_block_or_try_again:
Miao Xie8b110e32014-09-12 18:44:03 +08007535 done.uptodate = 0;
7536 done.start = start;
7537 init_completion(&done.done);
7538
Liu Bo17347ce2017-05-15 15:33:27 -07007539 ret = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307540 pgoff, start, start + sectorsize - 1,
7541 io_bio->mirror_num,
7542 btrfs_retry_endio_nocsum, &done);
Liu Bo629ebf42017-05-15 17:20:07 -07007543 if (ret) {
7544 err = ret;
7545 goto next;
7546 }
Miao Xie8b110e32014-09-12 18:44:03 +08007547
David Sterba9c17f6c2017-07-19 19:26:45 +02007548 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007549
7550 if (!done.uptodate) {
7551 /* We might have another mirror, so try again */
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307552 goto next_block_or_try_again;
Miao Xie8b110e32014-09-12 18:44:03 +08007553 }
7554
Liu Bo629ebf42017-05-15 17:20:07 -07007555next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307556 start += sectorsize;
7557
Liu Bo97bf5a52017-04-07 13:11:10 -07007558 nr_sectors--;
7559 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307560 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007561 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307562 goto next_block_or_try_again;
7563 }
Miao Xie8b110e32014-09-12 18:44:03 +08007564 }
7565
Liu Bo629ebf42017-05-15 17:20:07 -07007566 return err;
Miao Xie8b110e32014-09-12 18:44:03 +08007567}
7568
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007569static void btrfs_retry_endio(struct bio *bio)
Miao Xie8b110e32014-09-12 18:44:03 +08007570{
7571 struct btrfs_retry_complete *done = bio->bi_private;
7572 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Josef Bacik7870d082017-05-05 11:57:15 -04007573 struct extent_io_tree *io_tree, *failure_tree;
7574 struct inode *inode = done->inode;
Miao Xie8b110e32014-09-12 18:44:03 +08007575 struct bio_vec *bvec;
7576 int uptodate;
7577 int ret;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007578 int i = 0;
Ming Lei6dc4f102019-02-15 19:13:19 +08007579 struct bvec_iter_all iter_all;
Miao Xie8b110e32014-09-12 18:44:03 +08007580
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007581 if (bio->bi_status)
Miao Xie8b110e32014-09-12 18:44:03 +08007582 goto end;
7583
7584 uptodate = 1;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307585
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307586 ASSERT(bio->bi_vcnt == 1);
Ming Lei263663c2017-12-18 20:22:04 +08007587 ASSERT(bio_first_bvec_all(bio)->bv_len == btrfs_inode_sectorsize(done->inode));
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307588
Josef Bacik7870d082017-05-05 11:57:15 -04007589 io_tree = &BTRFS_I(inode)->io_tree;
7590 failure_tree = &BTRFS_I(inode)->io_failure_tree;
7591
David Sterbac09abff2017-07-13 18:10:07 +02007592 ASSERT(!bio_flagged(bio, BIO_CLONED));
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007593 bio_for_each_segment_all(bvec, bio, iter_all) {
Josef Bacik7870d082017-05-05 11:57:15 -04007594 ret = __readpage_endio_check(inode, io_bio, i, bvec->bv_page,
7595 bvec->bv_offset, done->start,
7596 bvec->bv_len);
Miao Xie8b110e32014-09-12 18:44:03 +08007597 if (!ret)
Josef Bacik7870d082017-05-05 11:57:15 -04007598 clean_io_failure(BTRFS_I(inode)->root->fs_info,
7599 failure_tree, io_tree, done->start,
7600 bvec->bv_page,
7601 btrfs_ino(BTRFS_I(inode)),
7602 bvec->bv_offset);
Miao Xie8b110e32014-09-12 18:44:03 +08007603 else
7604 uptodate = 0;
Christoph Hellwig2b070cf2019-04-25 09:03:00 +02007605 i++;
Miao Xie8b110e32014-09-12 18:44:03 +08007606 }
7607
7608 done->uptodate = uptodate;
7609end:
7610 complete(&done->done);
7611 bio_put(bio);
7612}
7613
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007614static blk_status_t __btrfs_subio_endio_read(struct inode *inode,
7615 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007616{
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307617 struct btrfs_fs_info *fs_info;
Liu Bo17347ce2017-05-15 15:33:27 -07007618 struct bio_vec bvec;
7619 struct bvec_iter iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007620 struct btrfs_retry_complete done;
7621 u64 start;
7622 u64 offset = 0;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307623 u32 sectorsize;
7624 int nr_sectors;
7625 unsigned int pgoff;
7626 int csum_pos;
Liu Boef7cdac12017-04-13 18:11:48 -07007627 bool uptodate = (err == 0);
Miao Xie8b110e32014-09-12 18:44:03 +08007628 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007629 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007630
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307631 fs_info = BTRFS_I(inode)->root->fs_info;
Jeff Mahoneyda170662016-06-15 09:22:56 -04007632 sectorsize = fs_info->sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307633
Omar Sandoval58efbc92017-08-22 23:45:59 -07007634 err = BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007635 start = io_bio->logical;
7636 done.inode = inode;
Liu Bo17347ce2017-05-15 15:33:27 -07007637 io_bio->bio.bi_iter = io_bio->iter;
Miao Xie8b110e32014-09-12 18:44:03 +08007638
Liu Bo17347ce2017-05-15 15:33:27 -07007639 bio_for_each_segment(bvec, &io_bio->bio, iter) {
7640 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307641
Liu Bo17347ce2017-05-15 15:33:27 -07007642 pgoff = bvec.bv_offset;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307643next_block:
Liu Boef7cdac12017-04-13 18:11:48 -07007644 if (uptodate) {
7645 csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
7646 ret = __readpage_endio_check(inode, io_bio, csum_pos,
7647 bvec.bv_page, pgoff, start, sectorsize);
7648 if (likely(!ret))
7649 goto next;
7650 }
Miao Xie8b110e32014-09-12 18:44:03 +08007651try_again:
7652 done.uptodate = 0;
7653 done.start = start;
7654 init_completion(&done.done);
7655
Omar Sandoval58efbc92017-08-22 23:45:59 -07007656 status = dio_read_error(inode, &io_bio->bio, bvec.bv_page,
7657 pgoff, start, start + sectorsize - 1,
7658 io_bio->mirror_num, btrfs_retry_endio,
7659 &done);
7660 if (status) {
7661 err = status;
Miao Xie8b110e32014-09-12 18:44:03 +08007662 goto next;
7663 }
7664
David Sterba9c17f6c2017-07-19 19:26:45 +02007665 wait_for_completion_io(&done.done);
Miao Xie8b110e32014-09-12 18:44:03 +08007666
7667 if (!done.uptodate) {
7668 /* We might have another mirror, so try again */
7669 goto try_again;
7670 }
7671next:
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307672 offset += sectorsize;
7673 start += sectorsize;
7674
7675 ASSERT(nr_sectors);
7676
Liu Bo97bf5a52017-04-07 13:11:10 -07007677 nr_sectors--;
7678 if (nr_sectors) {
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307679 pgoff += sectorsize;
Liu Bo97bf5a52017-04-07 13:11:10 -07007680 ASSERT(pgoff < PAGE_SIZE);
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307681 goto next_block;
7682 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007683 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007684
7685 return err;
7686}
7687
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007688static blk_status_t btrfs_subio_endio_read(struct inode *inode,
7689 struct btrfs_io_bio *io_bio, blk_status_t err)
Miao Xie8b110e32014-09-12 18:44:03 +08007690{
7691 bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
7692
7693 if (skip_csum) {
7694 if (unlikely(err))
7695 return __btrfs_correct_data_nocsum(inode, io_bio);
7696 else
Omar Sandoval58efbc92017-08-22 23:45:59 -07007697 return BLK_STS_OK;
Miao Xie8b110e32014-09-12 18:44:03 +08007698 } else {
7699 return __btrfs_subio_endio_read(inode, io_bio, err);
7700 }
7701}
7702
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007703static void btrfs_endio_direct_read(struct bio *bio)
Miao Xiec1dc0892014-09-12 18:43:56 +08007704{
7705 struct btrfs_dio_private *dip = bio->bi_private;
7706 struct inode *inode = dip->inode;
7707 struct bio *dio_bio;
7708 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007709 blk_status_t err = bio->bi_status;
Miao Xiec1dc0892014-09-12 18:43:56 +08007710
Liu Bo99c4e3b92017-09-15 15:06:51 -06007711 if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
Miao Xie8b110e32014-09-12 18:44:03 +08007712 err = btrfs_subio_endio_read(inode, io_bio, err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007713
Josef Bacik4b46fce2010-05-23 11:00:55 -04007714 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01007715 dip->logical_offset + dip->bytes - 1);
Chris Mason9be33952013-05-17 18:30:14 -04007716 dio_bio = dip->dio_bio;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007717
Josef Bacik4b46fce2010-05-23 11:00:55 -04007718 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007719
Liu Bo99c4e3b92017-09-15 15:06:51 -06007720 dio_bio->bi_status = err;
Christoph Hellwig40553512017-06-03 09:37:58 +02007721 dio_end_io(dio_bio);
David Sterbab3a0dd52018-11-22 17:16:49 +01007722 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007723 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007724}
7725
Qu Wenruo524272602017-03-08 10:25:52 +08007726static void __endio_write_update_ordered(struct inode *inode,
7727 const u64 offset, const u64 bytes,
7728 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007729{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007730 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007731 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007732 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007733 u64 ordered_offset = offset;
7734 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007735 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007736
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007737 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08007738 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007739 else
Qu Wenruo524272602017-03-08 10:25:52 +08007740 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007741
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007742 while (ordered_offset < offset + bytes) {
7743 last_offset = ordered_offset;
7744 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
7745 &ordered_offset,
7746 ordered_bytes,
7747 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007748 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7749 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007750 btrfs_queue_work(wq, &ordered->work);
7751 }
7752 /*
7753 * If btrfs_dec_test_ordered_pending does not find any ordered
7754 * extent in the range, we can exit.
7755 */
7756 if (ordered_offset == last_offset)
7757 return;
7758 /*
7759 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007760 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007761 */
7762 if (ordered_offset < offset + bytes) {
7763 ordered_bytes = offset + bytes - ordered_offset;
7764 ordered = NULL;
7765 }
Chris Mason163cf092010-11-28 19:56:33 -05007766 }
Filipe Manana14543772015-11-24 16:23:54 +00007767}
7768
7769static void btrfs_endio_direct_write(struct bio *bio)
7770{
7771 struct btrfs_dio_private *dip = bio->bi_private;
7772 struct bio *dio_bio = dip->dio_bio;
7773
Qu Wenruo524272602017-03-08 10:25:52 +08007774 __endio_write_update_ordered(dip->inode, dip->logical_offset,
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007775 dip->bytes, !bio->bi_status);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007776
Josef Bacik4b46fce2010-05-23 11:00:55 -04007777 kfree(dip);
Josef Bacikc0da7aa2011-03-22 11:05:07 -04007778
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007779 dio_bio->bi_status = bio->bi_status;
Christoph Hellwig40553512017-06-03 09:37:58 +02007780 dio_end_io(dio_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007781 bio_put(bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007782}
7783
David Sterbad0ee3932018-03-08 14:35:48 +01007784static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007785 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007786{
Josef Bacikc6100a42017-05-05 11:57:13 -04007787 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007788 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007789 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007790 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007791 return 0;
7792}
7793
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007794static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007795{
7796 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007797 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007798
Miao Xie8b110e32014-09-12 18:44:03 +08007799 if (err)
7800 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007801 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007802 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7803 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007804 (unsigned long long)bio->bi_iter.bi_sector,
7805 bio->bi_iter.bi_size, err);
7806
7807 if (dip->subio_endio)
7808 err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
Miao Xiec1dc0892014-09-12 18:43:56 +08007809
7810 if (err) {
Miao Xiee65e1532010-11-22 03:04:43 +00007811 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02007812 * We want to perceive the errors flag being set before
7813 * decrementing the reference count. We don't need a barrier
7814 * since atomic operations with a return value are fully
7815 * ordered as per atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00007816 */
Nikolay Borisovde224b72018-02-14 10:53:36 +02007817 dip->errors = 1;
Miao Xiee65e1532010-11-22 03:04:43 +00007818 }
7819
7820 /* if there are more bios still pending for this dio, just exit */
7821 if (!atomic_dec_and_test(&dip->pending_bios))
7822 goto out;
7823
Chris Mason9be33952013-05-17 18:30:14 -04007824 if (dip->errors) {
Miao Xiee65e1532010-11-22 03:04:43 +00007825 bio_io_error(dip->orig_bio);
Chris Mason9be33952013-05-17 18:30:14 -04007826 } else {
Anand Jain2dbe0c72017-10-14 08:35:56 +08007827 dip->dio_bio->bi_status = BLK_STS_OK;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007828 bio_endio(dip->orig_bio);
Miao Xiee65e1532010-11-22 03:04:43 +00007829 }
7830out:
7831 bio_put(bio);
7832}
7833
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007834static inline blk_status_t btrfs_lookup_and_bind_dio_csum(struct inode *inode,
Miao Xiec1dc0892014-09-12 18:43:56 +08007835 struct btrfs_dio_private *dip,
7836 struct bio *bio,
7837 u64 file_offset)
7838{
7839 struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7840 struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
Omar Sandovale7a04892020-03-02 14:02:49 -08007841 u16 csum_size;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007842 blk_status_t ret;
Miao Xiec1dc0892014-09-12 18:43:56 +08007843
7844 /*
7845 * We load all the csum data we need when we submit
7846 * the first bio to reduce the csum tree search and
7847 * contention.
7848 */
7849 if (dip->logical_offset == file_offset) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08007850 ret = btrfs_lookup_bio_sums(inode, dip->orig_bio, file_offset,
7851 NULL);
Miao Xiec1dc0892014-09-12 18:43:56 +08007852 if (ret)
7853 return ret;
7854 }
7855
7856 if (bio == dip->orig_bio)
7857 return 0;
7858
7859 file_offset -= dip->logical_offset;
7860 file_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandovale7a04892020-03-02 14:02:49 -08007861 csum_size = btrfs_super_csum_size(btrfs_sb(inode->i_sb)->super_copy);
7862 io_bio->csum = orig_io_bio->csum + csum_size * file_offset;
Miao Xiec1dc0892014-09-12 18:43:56 +08007863
7864 return 0;
7865}
7866
David Sterbad0ee3932018-03-08 14:35:48 +01007867static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7868 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007869{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007870 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007871 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007872 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007873 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007874
Liu Bo4c274bc2017-11-01 17:19:27 -06007875 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007876 if (async_submit)
7877 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7878
Josef Bacik5fd02042012-05-02 14:00:54 -04007879 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007880 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007881 if (ret)
7882 goto err;
7883 }
Miao Xiee65e1532010-11-22 03:04:43 +00007884
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007885 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007886 goto map;
7887
7888 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007889 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7890 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007891 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007892 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007893 } else if (write) {
7894 /*
7895 * If we aren't doing async submit, calculate the csum of the
7896 * bio now.
7897 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007898 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007899 if (ret)
7900 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007901 } else {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007902 ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
Miao Xiec1dc0892014-09-12 18:43:56 +08007903 file_offset);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007904 if (ret)
7905 goto err;
7906 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007907map:
Chris Mason08635ba2019-07-10 12:28:14 -07007908 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007909err:
Miao Xiee65e1532010-11-22 03:04:43 +00007910 return ret;
7911}
7912
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007913static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip)
Miao Xiee65e1532010-11-22 03:04:43 +00007914{
7915 struct inode *inode = dip->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007916 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiee65e1532010-11-22 03:04:43 +00007917 struct bio *bio;
7918 struct bio *orig_bio = dip->orig_bio;
Kent Overstreet4f024f32013-10-11 15:44:27 -07007919 u64 start_sector = orig_bio->bi_iter.bi_sector;
Miao Xiee65e1532010-11-22 03:04:43 +00007920 u64 file_offset = dip->logical_offset;
Josef Bacik1ae39932011-04-06 14:41:34 -04007921 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007922 u64 submit_len;
7923 int clone_offset = 0;
7924 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307925 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007926 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007927 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007928
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007929 submit_len = orig_bio->bi_iter.bi_size;
7930 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
7931 start_sector << 9, submit_len, &geom);
Miao Xie7a5c3c92014-06-17 18:58:59 +08007932 if (ret)
Miao Xiee65e1532010-11-22 03:04:43 +00007933 return -EIO;
Miao Xiefacc8a222013-07-25 19:22:34 +08007934
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007935 if (geom.len >= submit_len) {
Josef Bacik02f57c72011-04-06 14:25:44 -04007936 bio = orig_bio;
Miao Xiec1dc0892014-09-12 18:43:56 +08007937 dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
Josef Bacik02f57c72011-04-06 14:25:44 -04007938 goto submit;
7939 }
7940
David Woodhouse53b381b2013-01-29 18:40:14 -05007941 /* async crcs make it difficult to collect full stripe writes. */
Jeff Mahoney1b868262017-05-17 11:38:35 -04007942 if (btrfs_data_alloc_profile(fs_info) & BTRFS_BLOCK_GROUP_RAID56_MASK)
David Woodhouse53b381b2013-01-29 18:40:14 -05007943 async_submit = 0;
7944 else
7945 async_submit = 1;
7946
Liu Bo725130b2017-05-16 09:51:39 -07007947 /* bio split */
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007948 ASSERT(geom.len <= INT_MAX);
Josef Bacik02f57c72011-04-06 14:25:44 -04007949 atomic_inc(&dip->pending_bios);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007950 do {
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007951 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007952
Liu Bo725130b2017-05-16 09:51:39 -07007953 /*
7954 * This will never fail as it's passing GPF_NOFS and
7955 * the allocation is backed by btrfs_bioset.
7956 */
Liu Boe4770942017-05-16 10:57:14 -07007957 bio = btrfs_bio_clone_partial(orig_bio, clone_offset,
Liu Bo725130b2017-05-16 09:51:39 -07007958 clone_len);
7959 bio->bi_private = dip;
7960 bio->bi_end_io = btrfs_end_dio_bio;
7961 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007962
Liu Bo725130b2017-05-16 09:51:39 -07007963 ASSERT(submit_len >= clone_len);
7964 submit_len -= clone_len;
7965 if (submit_len == 0)
7966 break;
Miao Xiee65e1532010-11-22 03:04:43 +00007967
Liu Bo725130b2017-05-16 09:51:39 -07007968 /*
7969 * Increase the count before we submit the bio so we know
7970 * the end IO handler won't happen before we increase the
7971 * count. Otherwise, the dip might get freed before we're
7972 * done setting it up.
7973 */
7974 atomic_inc(&dip->pending_bios);
Miao Xiee65e1532010-11-22 03:04:43 +00007975
David Sterbad0ee3932018-03-08 14:35:48 +01007976 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007977 async_submit);
7978 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007979 bio_put(bio);
7980 atomic_dec(&dip->pending_bios);
7981 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007982 }
Liu Bo725130b2017-05-16 09:51:39 -07007983
7984 clone_offset += clone_len;
7985 start_sector += clone_len >> 9;
7986 file_offset += clone_len;
7987
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007988 ret = btrfs_get_io_geometry(fs_info, btrfs_op(orig_bio),
7989 start_sector << 9, submit_len, &geom);
Liu Bo725130b2017-05-16 09:51:39 -07007990 if (ret)
7991 goto out_err;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007992 } while (submit_len > 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007993
Josef Bacik02f57c72011-04-06 14:25:44 -04007994submit:
David Sterbad0ee3932018-03-08 14:35:48 +01007995 status = btrfs_submit_dio_bio(bio, inode, file_offset, async_submit);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007996 if (!status)
Miao Xiee65e1532010-11-22 03:04:43 +00007997 return 0;
7998
7999 bio_put(bio);
8000out_err:
8001 dip->errors = 1;
8002 /*
Nikolay Borisovde224b72018-02-14 10:53:36 +02008003 * Before atomic variable goto zero, we must make sure dip->errors is
8004 * perceived to be set. This ordering is ensured by the fact that an
8005 * atomic operations with a return value are fully ordered as per
8006 * atomic_t.txt
Miao Xiee65e1532010-11-22 03:04:43 +00008007 */
Miao Xiee65e1532010-11-22 03:04:43 +00008008 if (atomic_dec_and_test(&dip->pending_bios))
8009 bio_io_error(dip->orig_bio);
8010
8011 /* bio_end_io() will handle error, so we needn't return it */
8012 return 0;
8013}
8014
Mike Christie8a4c1e42016-06-05 14:31:50 -05008015static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8016 loff_t file_offset)
Josef Bacik4b46fce2010-05-23 11:00:55 -04008017{
Filipe Manana61de7182015-07-01 12:13:10 +01008018 struct btrfs_dio_private *dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008019 struct bio *bio = NULL;
8020 struct btrfs_io_bio *io_bio;
Mike Christie8a4c1e42016-06-05 14:31:50 -05008021 bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008022 int ret = 0;
8023
David Sterba8b6c1d52017-06-02 17:48:13 +02008024 bio = btrfs_bio_clone(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008025
Miao Xiec1dc0892014-09-12 18:43:56 +08008026 dip = kzalloc(sizeof(*dip), GFP_NOFS);
Chris Mason9be33952013-05-17 18:30:14 -04008027 if (!dip) {
8028 ret = -ENOMEM;
Filipe Manana61de7182015-07-01 12:13:10 +01008029 goto free_ordered;
Chris Mason9be33952013-05-17 18:30:14 -04008030 }
8031
8032 dip->private = dio_bio->bi_private;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008033 dip->inode = inode;
8034 dip->logical_offset = file_offset;
Kent Overstreet4f024f32013-10-11 15:44:27 -07008035 dip->bytes = dio_bio->bi_iter.bi_size;
8036 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Liu Bo3892ac92017-04-17 15:00:28 -07008037 bio->bi_private = dip;
8038 dip->orig_bio = bio;
Chris Mason9be33952013-05-17 18:30:14 -04008039 dip->dio_bio = dio_bio;
Miao Xiee65e1532010-11-22 03:04:43 +00008040 atomic_set(&dip->pending_bios, 0);
Liu Bo3892ac92017-04-17 15:00:28 -07008041 io_bio = btrfs_io_bio(bio);
8042 io_bio->logical = file_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008043
Miao Xiec1dc0892014-09-12 18:43:56 +08008044 if (write) {
Liu Bo3892ac92017-04-17 15:00:28 -07008045 bio->bi_end_io = btrfs_endio_direct_write;
Miao Xiec1dc0892014-09-12 18:43:56 +08008046 } else {
Liu Bo3892ac92017-04-17 15:00:28 -07008047 bio->bi_end_io = btrfs_endio_direct_read;
Miao Xiec1dc0892014-09-12 18:43:56 +08008048 dip->subio_endio = btrfs_subio_endio_read;
8049 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04008050
Filipe Mananaf28a4922015-12-08 19:23:20 +00008051 /*
8052 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8053 * even if we fail to submit a bio, because in such case we do the
8054 * corresponding error handling below and it must not be done a second
8055 * time by btrfs_direct_IO().
8056 */
8057 if (write) {
8058 struct btrfs_dio_data *dio_data = current->journal_info;
8059
8060 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8061 dip->bytes;
8062 dio_data->unsubmitted_oe_range_start =
8063 dio_data->unsubmitted_oe_range_end;
8064 }
8065
Nikolay Borisove6961ca2017-08-03 15:44:58 +03008066 ret = btrfs_submit_direct_hook(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00008067 if (!ret)
Chris Masoneaf25d92010-05-25 09:48:28 -04008068 return;
Chris Mason9be33952013-05-17 18:30:14 -04008069
David Sterbab3a0dd52018-11-22 17:16:49 +01008070 btrfs_io_bio_free_csum(io_bio);
Chris Mason9be33952013-05-17 18:30:14 -04008071
Josef Bacik4b46fce2010-05-23 11:00:55 -04008072free_ordered:
8073 /*
Filipe Manana61de7182015-07-01 12:13:10 +01008074 * If we arrived here it means either we failed to submit the dip
8075 * or we either failed to clone the dio_bio or failed to allocate the
8076 * dip. If we cloned the dio_bio and allocated the dip, we can just
8077 * call bio_endio against our io_bio so that we get proper resource
8078 * cleanup if we fail to submit the dip, otherwise, we must do the
8079 * same as btrfs_endio_direct_[write|read] because we can't call these
8080 * callbacks - they require an allocated dip and a clone of dio_bio.
Josef Bacik4b46fce2010-05-23 11:00:55 -04008081 */
Liu Bo3892ac92017-04-17 15:00:28 -07008082 if (bio && dip) {
Guoqing Jiang054ec2f2017-06-02 16:08:50 +08008083 bio_io_error(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008084 /*
Liu Bo3892ac92017-04-17 15:00:28 -07008085 * The end io callbacks free our dip, do the final put on bio
Filipe Manana61de7182015-07-01 12:13:10 +01008086 * and all the cleanup and final put for dio_bio (through
8087 * dio_end_io()).
8088 */
8089 dip = NULL;
Liu Bo3892ac92017-04-17 15:00:28 -07008090 bio = NULL;
Filipe Manana61de7182015-07-01 12:13:10 +01008091 } else {
Filipe Manana14543772015-11-24 16:23:54 +00008092 if (write)
Qu Wenruo524272602017-03-08 10:25:52 +08008093 __endio_write_update_ordered(inode,
Filipe Manana14543772015-11-24 16:23:54 +00008094 file_offset,
8095 dio_bio->bi_iter.bi_size,
Qu Wenruo524272602017-03-08 10:25:52 +08008096 false);
Filipe Manana14543772015-11-24 16:23:54 +00008097 else
Filipe Manana61de7182015-07-01 12:13:10 +01008098 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8099 file_offset + dio_bio->bi_iter.bi_size - 1);
Filipe Manana14543772015-11-24 16:23:54 +00008100
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02008101 dio_bio->bi_status = BLK_STS_IOERR;
Filipe Manana61de7182015-07-01 12:13:10 +01008102 /*
8103 * Releases and cleans up our dio_bio, no need to bio_put()
8104 * nor bio_endio()/bio_io_error() against dio_bio.
8105 */
Christoph Hellwig40553512017-06-03 09:37:58 +02008106 dio_end_io(dio_bio);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008107 }
Liu Bo3892ac92017-04-17 15:00:28 -07008108 if (bio)
8109 bio_put(bio);
Filipe Manana61de7182015-07-01 12:13:10 +01008110 kfree(dip);
Josef Bacik4b46fce2010-05-23 11:00:55 -04008111}
8112
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008113static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008114 const struct iov_iter *iter, loff_t offset)
Chris Mason5a5f79b2010-05-26 21:33:37 -04008115{
8116 int seg;
Josef Bacika1b75f72011-04-08 15:51:18 +00008117 int i;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008118 unsigned int blocksize_mask = fs_info->sectorsize - 1;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008119 ssize_t retval = -EINVAL;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008120
8121 if (offset & blocksize_mask)
8122 goto out;
8123
Al Viro28060d52014-03-22 05:15:17 -04008124 if (iov_iter_alignment(iter) & blocksize_mask)
8125 goto out;
Josef Bacika1b75f72011-04-08 15:51:18 +00008126
Al Viro28060d52014-03-22 05:15:17 -04008127 /* If this is a write we don't need to check anymore */
Al Virocd27e452016-10-10 13:39:05 -04008128 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
Al Viro28060d52014-03-22 05:15:17 -04008129 return 0;
8130 /*
8131 * Check to make sure we don't have duplicate iov_base's in this
8132 * iovec, if so return EINVAL, otherwise we'll get csum errors
8133 * when reading back.
8134 */
8135 for (seg = 0; seg < iter->nr_segs; seg++) {
8136 for (i = seg + 1; i < iter->nr_segs; i++) {
8137 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
Josef Bacika1b75f72011-04-08 15:51:18 +00008138 goto out;
8139 }
Chris Mason5a5f79b2010-05-26 21:33:37 -04008140 }
8141 retval = 0;
8142out:
8143 return retval;
8144}
Josef Bacikeb838e72012-07-31 16:28:48 -04008145
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008146static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
Chris Mason16432982008-04-10 10:23:21 -04008147{
Josef Bacik4b46fce2010-05-23 11:00:55 -04008148 struct file *file = iocb->ki_filp;
8149 struct inode *inode = file->f_mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008150 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
chandan50745b02015-08-28 21:10:13 +05308151 struct btrfs_dio_data dio_data = { 0 };
Qu Wenruo364ecf32017-02-27 15:10:38 +08008152 struct extent_changeset *data_reserved = NULL;
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008153 loff_t offset = iocb->ki_pos;
Miao Xie09348562013-02-07 10:12:07 +00008154 size_t count = 0;
Miao Xie2e60a512013-02-08 07:01:08 +00008155 int flags = 0;
Miao Xie38851cc2013-02-08 07:04:11 +00008156 bool wakeup = true;
8157 bool relock = false;
Miao Xie09348562013-02-07 10:12:07 +00008158 ssize_t ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04008159
Nikolay Borisov8c70c9f2017-08-21 12:43:46 +03008160 if (check_direct_IO(fs_info, iter, offset))
Chris Mason5a5f79b2010-05-26 21:33:37 -04008161 return 0;
Chris Mason5a5f79b2010-05-26 21:33:37 -04008162
Jens Axboefe0f07d2015-04-15 17:05:48 -06008163 inode_dio_begin(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008164
Josef Bacik0e267c42013-07-02 10:38:02 -04008165 /*
Miao Xie41bd9ca2014-03-06 13:54:57 +08008166 * The generic stuff only does filemap_write_and_wait_range, which
8167 * isn't enough if we've written compressed pages to this area, so
8168 * we need to flush the dirty pages again to make absolutely sure
8169 * that any outstanding dirty pages are on disk.
Josef Bacik0e267c42013-07-02 10:38:02 -04008170 */
Al Viroa6cbcd42014-03-04 22:38:00 -05008171 count = iov_iter_count(iter);
Miao Xie41bd9ca2014-03-06 13:54:57 +08008172 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8173 &BTRFS_I(inode)->runtime_flags))
Wang Shilong9a025a02014-07-17 11:44:13 +08008174 filemap_fdatawrite_range(inode->i_mapping, offset,
8175 offset + count - 1);
Josef Bacik0e267c42013-07-02 10:38:02 -04008176
Omar Sandoval6f673762015-03-16 04:33:52 -07008177 if (iov_iter_rw(iter) == WRITE) {
Miao Xie38851cc2013-02-08 07:04:11 +00008178 /*
8179 * If the write DIO is beyond the EOF, we need update
8180 * the isize, but it is protected by i_mutex. So we can
8181 * not unlock the i_mutex at this case.
8182 */
8183 if (offset + count <= inode->i_size) {
Liu Bo4aaedfb2016-12-14 22:36:05 -08008184 dio_data.overwrite = 1;
Al Viro59551022016-01-22 15:40:57 -05008185 inode_unlock(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008186 relock = true;
Goldwyn Rodriguesedf064e2017-06-20 07:05:49 -05008187 } else if (iocb->ki_flags & IOCB_NOWAIT) {
8188 ret = -EAGAIN;
8189 goto out;
Miao Xie38851cc2013-02-08 07:04:11 +00008190 }
Qu Wenruo364ecf32017-02-27 15:10:38 +08008191 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
8192 offset, count);
Miao Xie09348562013-02-07 10:12:07 +00008193 if (ret)
Miao Xie38851cc2013-02-08 07:04:11 +00008194 goto out;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008195
8196 /*
8197 * We need to know how many extents we reserved so that we can
8198 * do the accounting properly if we go over the number we
8199 * originally calculated. Abuse current->journal_info for this.
8200 */
Jeff Mahoneyda170662016-06-15 09:22:56 -04008201 dio_data.reserve = round_up(count,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008202 fs_info->sectorsize);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008203 dio_data.unsubmitted_oe_range_start = (u64)offset;
8204 dio_data.unsubmitted_oe_range_end = (u64)offset;
chandan50745b02015-08-28 21:10:13 +05308205 current->journal_info = &dio_data;
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308206 down_read(&BTRFS_I(inode)->dio_sem);
David Sterbaee39b432014-09-30 01:33:33 +02008207 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8208 &BTRFS_I(inode)->runtime_flags)) {
Jens Axboefe0f07d2015-04-15 17:05:48 -06008209 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008210 flags = DIO_LOCKING | DIO_SKIP_HOLES;
8211 wakeup = false;
Miao Xie09348562013-02-07 10:12:07 +00008212 }
8213
Omar Sandoval17f8c842015-03-16 04:33:50 -07008214 ret = __blockdev_direct_IO(iocb, inode,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008215 fs_info->fs_devices->latest_bdev,
Christoph Hellwigc8b8e322016-04-07 08:51:58 -07008216 iter, btrfs_get_blocks_direct, NULL,
Omar Sandoval17f8c842015-03-16 04:33:50 -07008217 btrfs_submit_direct, flags);
Omar Sandoval6f673762015-03-16 04:33:52 -07008218 if (iov_iter_rw(iter) == WRITE) {
Chandan Rajendra97dcdea2016-12-23 15:00:18 +05308219 up_read(&BTRFS_I(inode)->dio_sem);
Josef Bacike1cbbfa2015-03-17 10:52:28 -04008220 current->journal_info = NULL;
Liu Boddba1bf2015-06-17 16:59:58 +08008221 if (ret < 0 && ret != -EIOCBQUEUED) {
chandan50745b02015-08-28 21:10:13 +05308222 if (dio_data.reserve)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008223 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008224 offset, dio_data.reserve, true);
Filipe Mananaf28a4922015-12-08 19:23:20 +00008225 /*
8226 * On error we might have left some ordered extents
8227 * without submitting corresponding bios for them, so
8228 * cleanup them up to avoid other tasks getting them
8229 * and waiting for them to complete forever.
8230 */
8231 if (dio_data.unsubmitted_oe_range_start <
8232 dio_data.unsubmitted_oe_range_end)
Qu Wenruo524272602017-03-08 10:25:52 +08008233 __endio_write_update_ordered(inode,
Filipe Mananaf28a4922015-12-08 19:23:20 +00008234 dio_data.unsubmitted_oe_range_start,
8235 dio_data.unsubmitted_oe_range_end -
8236 dio_data.unsubmitted_oe_range_start,
Qu Wenruo524272602017-03-08 10:25:52 +08008237 false);
Liu Boddba1bf2015-06-17 16:59:58 +08008238 } else if (ret >= 0 && (size_t)ret < count)
Qu Wenruobc42bda2017-02-27 15:10:39 +08008239 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008240 offset, count - (size_t)ret, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08008241 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
Miao Xie09348562013-02-07 10:12:07 +00008242 }
Miao Xie38851cc2013-02-08 07:04:11 +00008243out:
Miao Xie2e60a512013-02-08 07:01:08 +00008244 if (wakeup)
Jens Axboefe0f07d2015-04-15 17:05:48 -06008245 inode_dio_end(inode);
Miao Xie38851cc2013-02-08 07:04:11 +00008246 if (relock)
Al Viro59551022016-01-22 15:40:57 -05008247 inode_lock(inode);
Miao Xie09348562013-02-07 10:12:07 +00008248
Qu Wenruo364ecf32017-02-27 15:10:38 +08008249 extent_changeset_free(data_reserved);
Miao Xie09348562013-02-07 10:12:07 +00008250 return ret;
Chris Mason16432982008-04-10 10:23:21 -04008251}
8252
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008253#define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
8254
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008255static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8256 __u64 start, __u64 len)
8257{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008258 int ret;
8259
8260 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8261 if (ret)
8262 return ret;
8263
David Sterba2135fb92017-06-23 04:09:57 +02008264 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008265}
8266
Chris Mason9ebefb182007-06-15 13:50:00 -04008267int btrfs_readpage(struct file *file, struct page *page)
8268{
Chris Masond1310b22008-01-24 16:13:08 -05008269 struct extent_io_tree *tree;
8270 tree = &BTRFS_I(page->mapping->host)->io_tree;
Jan Schmidt8ddc7d92011-06-13 20:02:58 +02008271 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008272}
Chris Mason1832a6d2007-12-21 16:27:21 -05008273
Chris Mason39279cc2007-06-12 06:35:45 -04008274static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8275{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008276 struct inode *inode = page->mapping->host;
8277 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008278
8279 if (current->flags & PF_MEMALLOC) {
8280 redirty_page_for_writepage(wbc, page);
8281 unlock_page(page);
8282 return 0;
8283 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008284
8285 /*
8286 * If we are under memory pressure we will call this directly from the
8287 * VM, we need to make sure we have the inode referenced for the ordered
8288 * extent. If not just return like we didn't do anything.
8289 */
8290 if (!igrab(inode)) {
8291 redirty_page_for_writepage(wbc, page);
8292 return AOP_WRITEPAGE_ACTIVATE;
8293 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008294 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008295 btrfs_add_delayed_iput(inode);
8296 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008297}
Chris Mason39279cc2007-06-12 06:35:45 -04008298
Eric Sandeen48a3b632013-04-25 20:41:01 +00008299static int btrfs_writepages(struct address_space *mapping,
8300 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008301{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008302 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008303}
8304
Chris Mason3ab2fb52007-11-08 10:59:22 -05008305static int
8306btrfs_readpages(struct file *file, struct address_space *mapping,
8307 struct list_head *pages, unsigned nr_pages)
8308{
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008309 return extent_readpages(mapping, pages, nr_pages);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008310}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008311
Chris Masone6dcd2d2008-07-17 12:53:50 -04008312static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008313{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008314 int ret = try_release_extent_mapping(page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04008315 if (ret == 1) {
8316 ClearPagePrivate(page);
8317 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008318 put_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008319 }
8320 return ret;
8321}
Chris Mason39279cc2007-06-12 06:35:45 -04008322
Chris Masone6dcd2d2008-07-17 12:53:50 -04008323static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8324{
Chris Mason98509cf2008-09-11 15:51:43 -04008325 if (PageWriteback(page) || PageDirty(page))
8326 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008327 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008328}
8329
Lukas Czernerd47992f2013-05-21 23:17:23 -04008330static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8331 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008332{
Josef Bacik5fd02042012-05-02 14:00:54 -04008333 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008334 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008335 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008336 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008337 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008338 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308339 u64 start;
8340 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008341 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008342
Chris Mason8b62b722009-09-02 16:53:46 -04008343 /*
8344 * we have the page locked, so new writeback can't start,
8345 * and the dirty bit won't be cleared while we are here.
8346 *
8347 * Wait for IO on this page so that we can safely clear
8348 * the PagePrivate2 bit and do ordered accounting
8349 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008350 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008351
Josef Bacik5fd02042012-05-02 14:00:54 -04008352 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008353 if (offset) {
8354 btrfs_releasepage(page, GFP_NOFS);
8355 return;
8356 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008357
8358 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008359 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308360again:
8361 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008362 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308363 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008364 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008365 end = min(page_end,
8366 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008367 /*
8368 * IO on this page will never be started, so we need
8369 * to account for any ordered extents now
8370 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008371 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308372 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008373 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008374 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008375 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008376 /*
8377 * whoever cleared the private bit is responsible
8378 * for the finish_ordered_io
8379 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008380 if (TestClearPagePrivate2(page)) {
8381 struct btrfs_ordered_inode_tree *tree;
8382 u64 new_len;
8383
8384 tree = &BTRFS_I(inode)->ordered_tree;
8385
8386 spin_lock_irq(&tree->lock);
8387 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308388 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008389 if (new_len < ordered->truncated_len)
8390 ordered->truncated_len = new_len;
8391 spin_unlock_irq(&tree->lock);
8392
8393 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308394 start,
8395 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008396 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008397 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008398 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008399 if (!inode_evicting) {
8400 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308401 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008402 &cached_state);
8403 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308404
8405 start = end + 1;
8406 if (start < page_end)
8407 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008408 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008409
Qu Wenruob9d0b382015-09-29 10:35:16 +08008410 /*
8411 * Qgroup reserved space handler
8412 * Page here will be either
8413 * 1) Already written to disk
8414 * In this case, its reserved space is released from data rsv map
8415 * and will be freed by delayed_ref handler finally.
8416 * So even we call qgroup_free_data(), it won't decrease reserved
8417 * space.
8418 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008419 * This means the reserved space should be freed here. However,
8420 * if a truncate invalidates the page (by clearing PageDirty)
8421 * and the page is accounted for while allocating extent
8422 * in btrfs_check_data_free_space() we let delayed_ref to
8423 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008424 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008425 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008426 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008427 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008428 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008429 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8430 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008431 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008432
8433 __btrfs_releasepage(page, GFP_NOFS);
8434 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008435
Chris Mason4a096752008-07-21 10:29:44 -04008436 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008437 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008438 ClearPagePrivate(page);
8439 set_page_private(page, 0);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008440 put_page(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04008441 }
Chris Mason39279cc2007-06-12 06:35:45 -04008442}
8443
Chris Mason9ebefb182007-06-15 13:50:00 -04008444/*
8445 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8446 * called from a page fault handler when a page is first dirtied. Hence we must
8447 * be careful to check for EOF conditions here. We set the page up correctly
8448 * for a written page which means we get ENOSPC checking when writing into
8449 * holes and correct delalloc and unwritten extent mapping on filesystems that
8450 * support these features.
8451 *
8452 * We are not allowed to take the i_mutex here so we have to play games to
8453 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008454 * truncate_setsize() writes the inode size before removing pages, once we have
8455 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008456 * beyond EOF, then the page is guaranteed safe against truncation until we
8457 * unlock the page.
8458 */
Souptick Joardera528a242018-06-06 19:54:44 +05308459vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008460{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008461 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008462 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008463 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008464 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8465 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008466 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008467 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008468 char *kaddr;
8469 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008470 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308471 vm_fault_t ret;
8472 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008473 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308474 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008475 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008476 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308477 u64 end;
8478
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008479 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008480
Jan Karab2b5ef52012-06-12 16:20:45 +02008481 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008482 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008483 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308484 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008485
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308486 /*
8487 * Reserving delalloc space after obtaining the page lock can lead to
8488 * deadlock. For example, if a dirty page is locked by this function
8489 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8490 * dirty page write out, then the btrfs_writepage() function could
8491 * end up waiting indefinitely to get a lock on the page currently
8492 * being processed by btrfs_page_mkwrite() function.
8493 */
Souptick Joardera528a242018-06-06 19:54:44 +05308494 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308495 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308496 if (!ret2) {
8497 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008498 reserved = 1;
8499 }
Souptick Joardera528a242018-06-06 19:54:44 +05308500 if (ret2) {
8501 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008502 if (reserved)
8503 goto out;
8504 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008505 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008506
Nick Piggin56a76f82009-03-31 15:23:23 -07008507 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008508again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008509 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008510 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008511
Chris Mason9ebefb182007-06-15 13:50:00 -04008512 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008513 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008514 /* page got truncated out from underneath us */
8515 goto out_unlock;
8516 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008517 wait_on_page_writeback(page);
8518
David Sterbaff13db42015-12-03 14:30:40 +01008519 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008520 set_page_extent_mapped(page);
8521
Chris Masoneb84ae02008-07-17 13:53:27 -04008522 /*
8523 * we can't set the delalloc bits if there are pending ordered
8524 * extents. Drop our locks and wait for them to finish
8525 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008526 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8527 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008528 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008529 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008530 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008531 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008532 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008533 btrfs_put_ordered_extent(ordered);
8534 goto again;
8535 }
8536
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008537 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008538 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008539 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008540 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308541 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008542 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008543 page_start, PAGE_SIZE - reserved_space,
8544 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308545 }
8546 }
8547
Josef Bacikfbf19082009-10-01 17:10:23 -04008548 /*
Liu Bo54160342016-12-13 12:15:19 -08008549 * page_mkwrite gets called when the page is firstly dirtied after it's
8550 * faulted in, but write(2) could also dirty a page and set delalloc
8551 * bits, thus in this case for space account reason, we still need to
8552 * clear any delalloc bits within this page range since we have to
8553 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008554 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308555 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008556 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8557 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008558
Souptick Joardera528a242018-06-06 19:54:44 +05308559 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008560 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308561 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008562 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008563 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008564 ret = VM_FAULT_SIGBUS;
8565 goto out_unlock;
8566 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008567
8568 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008569 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008570 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008571 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008572 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008573
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008574 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008575 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008576 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008577 flush_dcache_page(page);
8578 kunmap(page);
8579 }
Chris Mason247e7432008-07-17 12:53:51 -04008580 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008581 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008582 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008583
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008584 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008585 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008586 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008587
David Sterbae43bbe52017-12-12 21:43:52 +01008588 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008589
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008590 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8591 sb_end_pagefault(inode->i_sb);
8592 extent_changeset_free(data_reserved);
8593 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008594
8595out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008596 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008597out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008598 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008599 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008600 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008601out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008602 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008603 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008604 return ret;
8605}
8606
Filipe Manana213e8c52018-02-06 20:40:31 +00008607static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008608{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008609 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008610 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008611 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008612 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008613 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008614 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008615 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008616
Filipe Manana213e8c52018-02-06 20:40:31 +00008617 if (!skip_writeback) {
8618 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8619 (u64)-1);
8620 if (ret)
8621 return ret;
8622 }
Chris Mason39279cc2007-06-12 06:35:45 -04008623
Josef Bacikfcb80c22011-05-03 10:40:22 -04008624 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008625 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8626 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008627 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008628 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008629 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008630 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008631 * be free'd up by the truncate operation, but also have some slack
8632 * space reserved in case it uses space during the truncate (thank you
8633 * very much snapshotting).
8634 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008635 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008636 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008637 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008638 * doesn't end up using space reserved for updating the inode. We also
8639 * need to be able to stop the transaction and start a new one, which
8640 * means we need to be able to update the inode several times, and we
8641 * have no idea of knowing how many times that will be, so we can't just
8642 * reserve 1 item for the entirety of the operation, so that has to be
8643 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008644 *
8645 * So that leaves us with
8646 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008647 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008648 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008649 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008650 * updating the inode.
8651 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008652 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008653 if (!rsv)
8654 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008655 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008656 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008657
Josef Bacik907cbce2011-08-08 13:46:15 -04008658 /*
Josef Bacik07127182011-08-19 10:29:59 -04008659 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008660 * 1 for updating the inode.
8661 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008662 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008663 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008664 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008665 goto out;
8666 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008667
Josef Bacik907cbce2011-08-08 13:46:15 -04008668 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008669 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008670 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008671 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008672
Chris Mason5a3f23d2009-03-31 13:27:11 -04008673 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008674 * So if we truncate and then write and fsync we normally would just
8675 * write the extents that changed, which is a problem if we need to
8676 * first truncate that entire inode. So set this flag so we write out
8677 * all of the extents in the inode to the sync log so we're completely
8678 * safe.
8679 */
8680 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008681 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008682
Yan, Zheng80825102009-11-12 09:35:36 +00008683 while (1) {
8684 ret = btrfs_truncate_inode_items(trans, root, inode,
8685 inode->i_size,
8686 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008687 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008688 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008689 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008690
Yan, Zheng80825102009-11-12 09:35:36 +00008691 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008692 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008693 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008694
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008695 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008696 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008697
8698 trans = btrfs_start_transaction(root, 2);
8699 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008700 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008701 trans = NULL;
8702 break;
8703 }
8704
Wang Xiaoguang47b5d642016-09-07 20:17:38 +08008705 btrfs_block_rsv_release(fs_info, rsv, -1);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008706 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008707 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008708 BUG_ON(ret); /* shouldn't happen */
8709 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008710 }
8711
Josef Bacikddfae632017-10-19 14:16:02 -04008712 /*
8713 * We can't call btrfs_truncate_block inside a trans handle as we could
8714 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8715 * we've truncated everything except the last little bit, and can do
8716 * btrfs_truncate_block and then update the disk_i_size.
8717 */
8718 if (ret == NEED_TRUNCATE_BLOCK) {
8719 btrfs_end_transaction(trans);
8720 btrfs_btree_balance_dirty(fs_info);
8721
8722 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8723 if (ret)
8724 goto out;
8725 trans = btrfs_start_transaction(root, 1);
8726 if (IS_ERR(trans)) {
8727 ret = PTR_ERR(trans);
8728 goto out;
8729 }
8730 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
8731 }
8732
Chris Mason917c16b2011-11-08 14:49:59 -05008733 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008734 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008735
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008736 trans->block_rsv = &fs_info->trans_block_rsv;
8737 ret2 = btrfs_update_inode(trans, root, inode);
8738 if (ret2 && !ret)
8739 ret = ret2;
8740
8741 ret2 = btrfs_end_transaction(trans);
8742 if (ret2 && !ret)
8743 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008744 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008745 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008746out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008747 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008748
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008749 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008750}
8751
Sven Wegener3b963622008-06-09 21:57:42 -04008752/*
Chris Masond352ac62008-09-29 15:18:18 -04008753 * create a new subvolume directory/inode (helper for the ioctl).
8754 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008755int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008756 struct btrfs_root *new_root,
8757 struct btrfs_root *parent_root,
8758 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008759{
Chris Mason39279cc2007-06-12 06:35:45 -04008760 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008761 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008762 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008763
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008764 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8765 new_dirid, new_dirid,
8766 S_IFDIR | (~current_umask() & S_IRWXUGO),
8767 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008768 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008769 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008770 inode->i_op = &btrfs_dir_inode_operations;
8771 inode->i_fop = &btrfs_dir_file_operations;
8772
Miklos Szeredibfe86842011-10-28 14:13:29 +02008773 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008774 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008775 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008776
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008777 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8778 if (err)
8779 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008780 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008781 new_root->root_key.objectid, err);
8782
Yan, Zheng76dda932009-09-21 16:00:26 -04008783 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008784
Yan, Zheng76dda932009-09-21 16:00:26 -04008785 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008786 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008787}
8788
Chris Mason39279cc2007-06-12 06:35:45 -04008789struct inode *btrfs_alloc_inode(struct super_block *sb)
8790{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008791 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008792 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008793 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008794
David Sterba712e36c2017-10-31 17:08:27 +01008795 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008796 if (!ei)
8797 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008798
8799 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008800 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008801 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008802 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008803 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008804 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008805 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008806 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008807 ei->disk_i_size = 0;
8808 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008809 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008810 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008811 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008812 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008813 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008814
Josef Bacik9e0baf62011-07-15 15:16:44 +00008815 spin_lock_init(&ei->lock);
8816 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008817 if (sb->s_magic != BTRFS_TEST_MAGIC)
8818 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8819 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008820 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008821 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008822 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008823
Miao Xie16cdcec2011-04-22 18:12:22 +08008824 ei->delayed_node = NULL;
8825
chandan r9cc97d62012-07-04 12:48:07 +05308826 ei->i_otime.tv_sec = 0;
8827 ei->i_otime.tv_nsec = 0;
8828
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008829 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008830 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008831 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8832 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8833 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008834 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8835 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008836 ei->io_tree.track_uptodate = true;
8837 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008838 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008839 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008840 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008841 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008842 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008843 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008844 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008845
8846 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008847}
8848
Josef Bacikaaedb552013-10-11 14:44:09 -04008849#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8850void btrfs_test_destroy_inode(struct inode *inode)
8851{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008852 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008853 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8854}
8855#endif
8856
Al Viro26602ca2019-04-10 15:14:41 -04008857void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008858{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008859 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8860}
8861
Chris Mason39279cc2007-06-12 06:35:45 -04008862void btrfs_destroy_inode(struct inode *inode)
8863{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008864 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008865 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008866 struct btrfs_root *root = BTRFS_I(inode)->root;
8867
Al Virob3d9b7a2012-06-09 13:51:19 -04008868 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008869 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008870 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8871 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008872 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008873 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008874 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008875 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008876 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008877
Chris Mason5a3f23d2009-03-31 13:27:11 -04008878 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008879 * This can happen where we create an inode, but somebody else also
8880 * created the same inode and we need to destroy the one we already
8881 * created.
8882 */
8883 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008884 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008885
Chris Masond3977122009-01-05 21:25:51 -05008886 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008887 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8888 if (!ordered)
8889 break;
8890 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008891 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008892 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008893 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008894 btrfs_remove_ordered_extent(inode, ordered);
8895 btrfs_put_ordered_extent(ordered);
8896 btrfs_put_ordered_extent(ordered);
8897 }
8898 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008899 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008900 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008901 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008902 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04008903}
8904
Al Viro45321ac2010-06-07 13:43:19 -04008905int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008906{
8907 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008908
Naohiro Aota6379ef92013-06-06 09:56:34 +00008909 if (root == NULL)
8910 return 1;
8911
Liu Bofa6ac872013-02-20 14:10:23 +00008912 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008913 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008914 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008915 else
Al Viro45321ac2010-06-07 13:43:19 -04008916 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008917}
8918
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008919static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008920{
8921 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8922
8923 inode_init_once(&ei->vfs_inode);
8924}
8925
David Sterbae67c7182018-02-19 17:24:18 +01008926void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008927{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008928 /*
8929 * Make sure all delayed rcu free inodes are flushed before we
8930 * destroy cache.
8931 */
8932 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008933 kmem_cache_destroy(btrfs_inode_cachep);
8934 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008935 kmem_cache_destroy(btrfs_path_cachep);
8936 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008937 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008938}
8939
Liu Bof5c29bd2017-11-02 17:21:50 -06008940int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008941{
David Sterba837e1972012-09-07 03:00:48 -06008942 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008943 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008944 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8945 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008946 if (!btrfs_inode_cachep)
8947 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008948
David Sterba837e1972012-09-07 03:00:48 -06008949 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008950 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008951 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008952 if (!btrfs_trans_handle_cachep)
8953 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008954
David Sterba837e1972012-09-07 03:00:48 -06008955 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008956 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008957 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008958 if (!btrfs_path_cachep)
8959 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008960
David Sterba837e1972012-09-07 03:00:48 -06008961 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008962 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008963 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008964 if (!btrfs_free_space_cachep)
8965 goto fail;
8966
Christophe Leroy3acd4852019-08-21 15:05:55 +00008967 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8968 PAGE_SIZE, PAGE_SIZE,
8969 SLAB_RED_ZONE, NULL);
8970 if (!btrfs_free_space_bitmap_cachep)
8971 goto fail;
8972
Chris Mason39279cc2007-06-12 06:35:45 -04008973 return 0;
8974fail:
8975 btrfs_destroy_cachep();
8976 return -ENOMEM;
8977}
8978
David Howellsa528d352017-01-31 16:46:22 +00008979static int btrfs_getattr(const struct path *path, struct kstat *stat,
8980 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008981{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008982 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008983 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008984 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008985 u32 bi_flags = BTRFS_I(inode)->flags;
8986
8987 stat->result_mask |= STATX_BTIME;
8988 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8989 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8990 if (bi_flags & BTRFS_INODE_APPEND)
8991 stat->attributes |= STATX_ATTR_APPEND;
8992 if (bi_flags & BTRFS_INODE_COMPRESS)
8993 stat->attributes |= STATX_ATTR_COMPRESSED;
8994 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8995 stat->attributes |= STATX_ATTR_IMMUTABLE;
8996 if (bi_flags & BTRFS_INODE_NODUMP)
8997 stat->attributes |= STATX_ATTR_NODUMP;
8998
8999 stat->attributes_mask |= (STATX_ATTR_APPEND |
9000 STATX_ATTR_COMPRESSED |
9001 STATX_ATTR_IMMUTABLE |
9002 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05009003
Chris Mason39279cc2007-06-12 06:35:45 -04009004 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04009005 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009006
9007 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01009008 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009009 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05009010 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00009011 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04009012 return 0;
9013}
9014
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009015static int btrfs_rename_exchange(struct inode *old_dir,
9016 struct dentry *old_dentry,
9017 struct inode *new_dir,
9018 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04009019{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009020 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009021 struct btrfs_trans_handle *trans;
9022 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009023 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009024 struct inode *new_inode = new_dentry->d_inode;
9025 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07009026 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009027 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009028 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9029 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009030 u64 old_idx = 0;
9031 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009032 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01009033 bool root_log_pinned = false;
9034 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009035 struct btrfs_log_ctx ctx_root;
9036 struct btrfs_log_ctx ctx_dest;
9037 bool sync_log_root = false;
9038 bool sync_log_dest = false;
9039 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009040
9041 /* we only allow rename subvolume link between subvolumes */
9042 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9043 return -EXDEV;
9044
Filipe Mananad4682ba2018-06-11 19:24:28 +01009045 btrfs_init_log_ctx(&ctx_root, old_inode);
9046 btrfs_init_log_ctx(&ctx_dest, new_inode);
9047
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009048 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05009049 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
9050 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009051 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009052
9053 /*
9054 * We want to reserve the absolute worst case amount of items. So if
9055 * both inodes are subvols and we need to unlink them then that would
9056 * require 4 item modifications, but if they are both normal inodes it
9057 * would require 5 item modifications, so we'll assume their normal
9058 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9059 * should cover the worst case number of items we'll modify.
9060 */
9061 trans = btrfs_start_transaction(root, 12);
9062 if (IS_ERR(trans)) {
9063 ret = PTR_ERR(trans);
9064 goto out_notrans;
9065 }
9066
Josef Bacik3e174092019-11-15 15:43:06 -05009067 if (dest != root)
9068 btrfs_record_root_in_trans(trans, dest);
9069
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009070 /*
9071 * We need to find a free sequence number both in the source and
9072 * in the destination directory for the exchange.
9073 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02009074 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009075 if (ret)
9076 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02009077 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009078 if (ret)
9079 goto out_fail;
9080
9081 BTRFS_I(old_inode)->dir_index = 0ULL;
9082 BTRFS_I(new_inode)->dir_index = 0ULL;
9083
9084 /* Reference for the source. */
9085 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9086 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009087 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009088 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009089 btrfs_pin_log_trans(root);
9090 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009091 ret = btrfs_insert_inode_ref(trans, dest,
9092 new_dentry->d_name.name,
9093 new_dentry->d_name.len,
9094 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009095 btrfs_ino(BTRFS_I(new_dir)),
9096 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009097 if (ret)
9098 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009099 }
9100
9101 /* And now for the dest. */
9102 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9103 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009104 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009105 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01009106 btrfs_pin_log_trans(dest);
9107 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009108 ret = btrfs_insert_inode_ref(trans, root,
9109 old_dentry->d_name.name,
9110 old_dentry->d_name.len,
9111 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01009112 btrfs_ino(BTRFS_I(old_dir)),
9113 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009114 if (ret)
9115 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009116 }
9117
9118 /* Update inode version and ctime/mtime. */
9119 inode_inc_iversion(old_dir);
9120 inode_inc_iversion(new_dir);
9121 inode_inc_iversion(old_inode);
9122 inode_inc_iversion(new_inode);
9123 old_dir->i_ctime = old_dir->i_mtime = ctime;
9124 new_dir->i_ctime = new_dir->i_mtime = ctime;
9125 old_inode->i_ctime = ctime;
9126 new_inode->i_ctime = ctime;
9127
9128 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009129 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9130 BTRFS_I(old_inode), 1);
9131 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9132 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009133 }
9134
9135 /* src is a subvolume */
9136 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009137 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009138 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009139 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9140 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009141 old_dentry->d_name.name,
9142 old_dentry->d_name.len);
9143 if (!ret)
9144 ret = btrfs_update_inode(trans, root, old_inode);
9145 }
9146 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009147 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009148 goto out_fail;
9149 }
9150
9151 /* dest is a subvolume */
9152 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009153 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009154 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009155 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9156 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009157 new_dentry->d_name.name,
9158 new_dentry->d_name.len);
9159 if (!ret)
9160 ret = btrfs_update_inode(trans, dest, new_inode);
9161 }
9162 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009163 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009164 goto out_fail;
9165 }
9166
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009167 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009168 new_dentry->d_name.name,
9169 new_dentry->d_name.len, 0, old_idx);
9170 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009171 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009172 goto out_fail;
9173 }
9174
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009175 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009176 old_dentry->d_name.name,
9177 old_dentry->d_name.len, 0, new_idx);
9178 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009179 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009180 goto out_fail;
9181 }
9182
9183 if (old_inode->i_nlink == 1)
9184 BTRFS_I(old_inode)->dir_index = old_idx;
9185 if (new_inode->i_nlink == 1)
9186 BTRFS_I(new_inode)->dir_index = new_idx;
9187
Filipe Manana86e8aa02016-05-05 02:02:27 +01009188 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009189 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009190 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9191 BTRFS_I(old_dir), parent,
9192 false, &ctx_root);
9193 if (ret == BTRFS_NEED_LOG_SYNC)
9194 sync_log_root = true;
9195 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9196 commit_transaction = true;
9197 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009198 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009199 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009200 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009201 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01009202 if (!commit_transaction) {
9203 parent = old_dentry->d_parent;
9204 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
9205 BTRFS_I(new_dir), parent,
9206 false, &ctx_dest);
9207 if (ret == BTRFS_NEED_LOG_SYNC)
9208 sync_log_dest = true;
9209 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9210 commit_transaction = true;
9211 ret = 0;
9212 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009213 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009214 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009215 }
9216out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009217 /*
9218 * If we have pinned a log and an error happened, we unpin tasks
9219 * trying to sync the log and force them to fallback to a transaction
9220 * commit if the log currently contains any of the inodes involved in
9221 * this rename operation (to ensure we do not persist a log with an
9222 * inconsistent state for any of these inodes or leading to any
9223 * inconsistencies when replayed). If the transaction was aborted, the
9224 * abortion reason is propagated to userspace when attempting to commit
9225 * the transaction. If the log does not contain any of these inodes, we
9226 * allow the tasks to sync it.
9227 */
9228 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009229 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9230 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9231 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009232 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009233 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009234 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009235
9236 if (root_log_pinned) {
9237 btrfs_end_log_trans(root);
9238 root_log_pinned = false;
9239 }
9240 if (dest_log_pinned) {
9241 btrfs_end_log_trans(dest);
9242 dest_log_pinned = false;
9243 }
9244 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009245 if (!ret && sync_log_root && !commit_transaction) {
9246 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
9247 &ctx_root);
9248 if (ret)
9249 commit_transaction = true;
9250 }
9251 if (!ret && sync_log_dest && !commit_transaction) {
9252 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9253 &ctx_dest);
9254 if (ret)
9255 commit_transaction = true;
9256 }
9257 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009258 /*
9259 * We may have set commit_transaction when logging the new name
9260 * in the destination root, in which case we left the source
9261 * root context in the list of log contextes. So make sure we
9262 * remove it to avoid invalid memory accesses, since the context
9263 * was allocated in our stack frame.
9264 */
9265 if (sync_log_root) {
9266 mutex_lock(&root->log_mutex);
9267 list_del_init(&ctx_root.list);
9268 mutex_unlock(&root->log_mutex);
9269 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009270 ret = btrfs_commit_transaction(trans);
9271 } else {
9272 int ret2;
9273
9274 ret2 = btrfs_end_transaction(trans);
9275 ret = ret ? ret : ret2;
9276 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009277out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009278 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9279 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009280 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009281
Filipe Mananae6c61712019-11-08 16:11:56 +00009282 ASSERT(list_empty(&ctx_root.list));
9283 ASSERT(list_empty(&ctx_dest.list));
9284
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009285 return ret;
9286}
9287
9288static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9289 struct btrfs_root *root,
9290 struct inode *dir,
9291 struct dentry *dentry)
9292{
9293 int ret;
9294 struct inode *inode;
9295 u64 objectid;
9296 u64 index;
9297
9298 ret = btrfs_find_free_ino(root, &objectid);
9299 if (ret)
9300 return ret;
9301
9302 inode = btrfs_new_inode(trans, root, dir,
9303 dentry->d_name.name,
9304 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009305 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009306 objectid,
9307 S_IFCHR | WHITEOUT_MODE,
9308 &index);
9309
9310 if (IS_ERR(inode)) {
9311 ret = PTR_ERR(inode);
9312 return ret;
9313 }
9314
9315 inode->i_op = &btrfs_special_inode_operations;
9316 init_special_inode(inode, inode->i_mode,
9317 WHITEOUT_DEV);
9318
9319 ret = btrfs_init_inode_security(trans, inode, dir,
9320 &dentry->d_name);
9321 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009322 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009323
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009324 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9325 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009326 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009327 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009328
9329 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009330out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009331 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009332 if (ret)
9333 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009334 iput(inode);
9335
Filipe Mananac9901612016-05-05 01:41:57 +01009336 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009337}
9338
Chris Mason39279cc2007-06-12 06:35:45 -04009339static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009340 struct inode *new_dir, struct dentry *new_dentry,
9341 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009342{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009343 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009344 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009345 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009346 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9347 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009348 struct inode *new_inode = d_inode(new_dentry);
9349 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009350 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009351 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009352 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009353 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009354 struct btrfs_log_ctx ctx;
9355 bool sync_log = false;
9356 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009357
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009358 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009359 return -EPERM;
9360
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009361 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009362 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009363 return -EXDEV;
9364
Li Zefan33345d012011-04-20 10:31:50 +08009365 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009366 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009367 return -ENOTEMPTY;
9368
Chris Mason39279cc2007-06-12 06:35:45 -04009369 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009370 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009371 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009372
9373
9374 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009375 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009376 new_dentry->d_name.name,
9377 new_dentry->d_name.len);
9378
9379 if (ret) {
9380 if (ret == -EEXIST) {
9381 /* we shouldn't get
9382 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309383 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009384 return ret;
9385 }
9386 } else {
9387 /* maybe -EOVERFLOW */
9388 return ret;
9389 }
9390 }
9391 ret = 0;
9392
Chris Mason5a3f23d2009-03-31 13:27:11 -04009393 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009394 * we're using rename to replace one file with another. Start IO on it
9395 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009396 */
Chris Mason8d875f92014-08-12 10:47:42 -07009397 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009398 filemap_flush(old_inode->i_mapping);
9399
Yan, Zheng76dda932009-09-21 16:00:26 -04009400 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009401 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009402 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009403 /*
9404 * We want to reserve the absolute worst case amount of items. So if
9405 * both inodes are subvols and we need to unlink them then that would
9406 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009407 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009408 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9409 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009410 * If our rename has the whiteout flag, we need more 5 units for the
9411 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9412 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009413 */
Filipe Manana5062af32016-05-05 10:26:26 +01009414 trans_num_items = 11;
9415 if (flags & RENAME_WHITEOUT)
9416 trans_num_items += 5;
9417 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009418 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009419 ret = PTR_ERR(trans);
9420 goto out_notrans;
9421 }
Chris Mason5f39d392007-10-15 16:14:19 -04009422
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009423 if (dest != root)
9424 btrfs_record_root_in_trans(trans, dest);
9425
Nikolay Borisov877574e2017-02-20 13:50:33 +02009426 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009427 if (ret)
9428 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009429
Miao Xie67de1172013-12-26 13:07:06 +08009430 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009431 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009432 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009433 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009434 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009435 btrfs_pin_log_trans(root);
9436 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009437 ret = btrfs_insert_inode_ref(trans, dest,
9438 new_dentry->d_name.name,
9439 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009440 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009441 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009442 if (ret)
9443 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009444 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009445
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009446 inode_inc_iversion(old_dir);
9447 inode_inc_iversion(new_dir);
9448 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009449 old_dir->i_ctime = old_dir->i_mtime =
9450 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009451 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009452
Chris Mason12fcfd22009-03-24 10:24:20 -04009453 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009454 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9455 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009456
Li Zefan33345d012011-04-20 10:31:50 +08009457 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009458 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009459 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009460 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9461 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009462 old_dentry->d_name.name,
9463 old_dentry->d_name.len);
9464 if (!ret)
9465 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009466 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009467 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009468 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009469 goto out_fail;
9470 }
Chris Mason39279cc2007-06-12 06:35:45 -04009471
9472 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009473 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009474 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009475 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009476 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009477 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009478 BUG_ON(new_inode->i_nlink == 0);
9479 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009480 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9481 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009482 new_dentry->d_name.name,
9483 new_dentry->d_name.len);
9484 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009485 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009486 ret = btrfs_orphan_add(trans,
9487 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009488 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009489 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009490 goto out_fail;
9491 }
Chris Mason39279cc2007-06-12 06:35:45 -04009492 }
Josef Bacikaec74772008-07-24 12:12:38 -04009493
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009494 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009495 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009496 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009497 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009498 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009499 goto out_fail;
9500 }
Chris Mason39279cc2007-06-12 06:35:45 -04009501
Miao Xie67de1172013-12-26 13:07:06 +08009502 if (old_inode->i_nlink == 1)
9503 BTRFS_I(old_inode)->dir_index = index;
9504
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009505 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009506 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009507
Filipe Mananad4682ba2018-06-11 19:24:28 +01009508 btrfs_init_log_ctx(&ctx, old_inode);
9509 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9510 BTRFS_I(old_dir), parent,
9511 false, &ctx);
9512 if (ret == BTRFS_NEED_LOG_SYNC)
9513 sync_log = true;
9514 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9515 commit_transaction = true;
9516 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009517 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009518 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009519 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009520
9521 if (flags & RENAME_WHITEOUT) {
9522 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9523 old_dentry);
9524
9525 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009526 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009527 goto out_fail;
9528 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009529 }
Chris Mason39279cc2007-06-12 06:35:45 -04009530out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009531 /*
9532 * If we have pinned the log and an error happened, we unpin tasks
9533 * trying to sync the log and force them to fallback to a transaction
9534 * commit if the log currently contains any of the inodes involved in
9535 * this rename operation (to ensure we do not persist a log with an
9536 * inconsistent state for any of these inodes or leading to any
9537 * inconsistencies when replayed). If the transaction was aborted, the
9538 * abortion reason is propagated to userspace when attempting to commit
9539 * the transaction. If the log does not contain any of these inodes, we
9540 * allow the tasks to sync it.
9541 */
9542 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009543 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9544 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9545 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009546 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009547 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009548 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009549
9550 btrfs_end_log_trans(root);
9551 log_pinned = false;
9552 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009553 if (!ret && sync_log) {
9554 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9555 if (ret)
9556 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009557 } else if (sync_log) {
9558 mutex_lock(&root->log_mutex);
9559 list_del(&ctx.list);
9560 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009561 }
9562 if (commit_transaction) {
9563 ret = btrfs_commit_transaction(trans);
9564 } else {
9565 int ret2;
9566
9567 ret2 = btrfs_end_transaction(trans);
9568 ret = ret ? ret : ret2;
9569 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009570out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009571 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009572 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009573
Chris Mason39279cc2007-06-12 06:35:45 -04009574 return ret;
9575}
9576
Miklos Szeredi80ace852014-07-23 15:15:32 +02009577static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9578 struct inode *new_dir, struct dentry *new_dentry,
9579 unsigned int flags)
9580{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009581 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009582 return -EINVAL;
9583
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009584 if (flags & RENAME_EXCHANGE)
9585 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9586 new_dentry);
9587
9588 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009589}
9590
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009591struct btrfs_delalloc_work {
9592 struct inode *inode;
9593 struct completion completion;
9594 struct list_head list;
9595 struct btrfs_work work;
9596};
9597
Miao Xie8ccf6f192012-10-25 09:28:04 +00009598static void btrfs_run_delalloc_work(struct btrfs_work *work)
9599{
9600 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009601 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009602
9603 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9604 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009605 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009606 filemap_flush(inode->i_mapping);
9607 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9608 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009609 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009610
Nikolay Borisov076da912018-04-23 10:54:16 +03009611 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009612 complete(&delalloc_work->completion);
9613}
9614
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009615static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009616{
9617 struct btrfs_delalloc_work *work;
9618
David Sterba100d5702015-12-08 14:39:32 +01009619 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009620 if (!work)
9621 return NULL;
9622
9623 init_completion(&work->completion);
9624 INIT_LIST_HEAD(&work->list);
9625 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009626 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009627
9628 return work;
9629}
9630
Chris Masond352ac62008-09-29 15:18:18 -04009631/*
9632 * some fairly slow code that needs optimization. This walks the list
9633 * of all the inodes with pending delalloc and forces them to disk.
9634 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009635static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009636{
Chris Masonea8c2812008-08-04 23:17:27 -04009637 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009638 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009639 struct btrfs_delalloc_work *work, *next;
9640 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009641 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009642 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009643
Miao Xie8ccf6f192012-10-25 09:28:04 +00009644 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009645 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009646
Miao Xie573bfb72014-03-06 13:55:03 +08009647 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009648 spin_lock(&root->delalloc_lock);
9649 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009650 while (!list_empty(&splice)) {
9651 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009652 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009653
Miao Xieeb73c1b2013-05-15 07:48:22 +00009654 list_move_tail(&binode->delalloc_inodes,
9655 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009656 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009657 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009658 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009659 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009660 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009661 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009662
Ethan Lien3cd24c62018-11-01 14:49:03 +08009663 if (snapshot)
9664 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9665 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009666 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009667 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009668 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009669 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009670 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009671 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009672 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009673 btrfs_queue_work(root->fs_info->flush_workers,
9674 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009675 ret++;
9676 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009677 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009678 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009679 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009680 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009681 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009682
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009683out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009684 list_for_each_entry_safe(work, next, &works, list) {
9685 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009686 wait_for_completion(&work->completion);
9687 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009688 }
9689
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009690 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009691 spin_lock(&root->delalloc_lock);
9692 list_splice_tail(&splice, &root->delalloc_inodes);
9693 spin_unlock(&root->delalloc_lock);
9694 }
Miao Xie573bfb72014-03-06 13:55:03 +08009695 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009696 return ret;
9697}
9698
Ethan Lien3cd24c62018-11-01 14:49:03 +08009699int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009700{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009701 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009702 int ret;
9703
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009704 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009705 return -EROFS;
9706
Ethan Lien3cd24c62018-11-01 14:49:03 +08009707 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009708 if (ret > 0)
9709 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009710 return ret;
9711}
9712
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009713int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009714{
9715 struct btrfs_root *root;
9716 struct list_head splice;
9717 int ret;
9718
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009719 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009720 return -EROFS;
9721
9722 INIT_LIST_HEAD(&splice);
9723
Miao Xie573bfb72014-03-06 13:55:03 +08009724 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009725 spin_lock(&fs_info->delalloc_root_lock);
9726 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009727 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009728 root = list_first_entry(&splice, struct btrfs_root,
9729 delalloc_root);
9730 root = btrfs_grab_fs_root(root);
9731 BUG_ON(!root);
9732 list_move_tail(&root->delalloc_root,
9733 &fs_info->delalloc_roots);
9734 spin_unlock(&fs_info->delalloc_root_lock);
9735
Ethan Lien3cd24c62018-11-01 14:49:03 +08009736 ret = start_delalloc_inodes(root, nr, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009737 btrfs_put_fs_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009738 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009739 goto out;
9740
Miao Xie6c255e62014-03-06 13:55:01 +08009741 if (nr != -1) {
9742 nr -= ret;
9743 WARN_ON(nr < 0);
9744 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009745 spin_lock(&fs_info->delalloc_root_lock);
9746 }
9747 spin_unlock(&fs_info->delalloc_root_lock);
9748
Miao Xie6c255e62014-03-06 13:55:01 +08009749 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009750out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009751 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009752 spin_lock(&fs_info->delalloc_root_lock);
9753 list_splice_tail(&splice, &fs_info->delalloc_roots);
9754 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009755 }
Miao Xie573bfb72014-03-06 13:55:03 +08009756 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009757 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009758}
9759
Chris Mason39279cc2007-06-12 06:35:45 -04009760static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9761 const char *symname)
9762{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009763 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009764 struct btrfs_trans_handle *trans;
9765 struct btrfs_root *root = BTRFS_I(dir)->root;
9766 struct btrfs_path *path;
9767 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009768 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009769 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009770 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309771 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009772 int name_len;
9773 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009774 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009775 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009776 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009777
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009778 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009779 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009780 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009781
Josef Bacik9ed74f22009-09-11 16:12:44 -04009782 /*
9783 * 2 items for inode item and ref
9784 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009785 * 1 item for updating parent inode item
9786 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009787 * 1 item for xattr if selinux is on
9788 */
Filipe Manana9269d122015-12-31 18:16:29 +00009789 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009790 if (IS_ERR(trans))
9791 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009792
Li Zefan581bb052011-04-20 10:06:11 +08009793 err = btrfs_find_free_ino(root, &objectid);
9794 if (err)
9795 goto out_unlock;
9796
Josef Bacikaec74772008-07-24 12:12:38 -04009797 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009798 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9799 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009800 if (IS_ERR(inode)) {
9801 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009802 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009803 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009804 }
Chris Mason39279cc2007-06-12 06:35:45 -04009805
Casey Schauflerad19db72011-12-15 10:09:07 -05009806 /*
9807 * If the active LSM wants to access the inode during
9808 * d_instantiate it needs these. Smack checks to see
9809 * if the filesystem supports xattrs by looking at the
9810 * ops vector.
9811 */
9812 inode->i_fop = &btrfs_file_operations;
9813 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009814 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009815 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9816
9817 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9818 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009819 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009820
Chris Mason39279cc2007-06-12 06:35:45 -04009821 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009822 if (!path) {
9823 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009824 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009825 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009826 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009827 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009828 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009829 datasize = btrfs_file_extent_calc_inline_size(name_len);
9830 err = btrfs_insert_empty_item(trans, root, path, &key,
9831 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009832 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009833 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009834 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009835 }
Chris Mason5f39d392007-10-15 16:14:19 -04009836 leaf = path->nodes[0];
9837 ei = btrfs_item_ptr(leaf, path->slots[0],
9838 struct btrfs_file_extent_item);
9839 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9840 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009841 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009842 btrfs_set_file_extent_encryption(leaf, ei, 0);
9843 btrfs_set_file_extent_compression(leaf, ei, 0);
9844 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9845 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9846
Chris Mason39279cc2007-06-12 06:35:45 -04009847 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009848 write_extent_buffer(leaf, symname, ptr, name_len);
9849 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009850 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009851
Chris Mason39279cc2007-06-12 06:35:45 -04009852 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009853 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009854 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009855 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009856 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009857 /*
9858 * Last step, add directory indexes for our symlink inode. This is the
9859 * last step to avoid extra cleanup of these indexes if an error happens
9860 * elsewhere above.
9861 */
9862 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009863 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9864 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009865 if (err)
9866 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009867
Al Viro1e2e5472018-05-04 08:23:01 -04009868 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009869
9870out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009871 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009872 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009873 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009874 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009875 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009876 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009877 return err;
9878}
Chris Mason16432982008-04-10 10:23:21 -04009879
Josef Bacik0af3d002010-06-21 14:48:16 -04009880static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9881 u64 start, u64 num_bytes, u64 min_size,
9882 loff_t actual_len, u64 *alloc_hint,
9883 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009884{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009885 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009886 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9887 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009888 struct btrfs_root *root = BTRFS_I(inode)->root;
9889 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009890 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009891 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009892 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009893 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009894 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009895 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009896 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009897 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009898
Josef Bacik0af3d002010-06-21 14:48:16 -04009899 if (trans)
9900 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009901 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009902 if (own_trans) {
9903 trans = btrfs_start_transaction(root, 3);
9904 if (IS_ERR(trans)) {
9905 ret = PTR_ERR(trans);
9906 break;
9907 }
Yan Zhengd899e052008-10-30 14:25:28 -04009908 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009909
Byongho Leeee221842015-12-15 01:42:10 +09009910 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009911 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009912 /*
9913 * If we are severely fragmented we could end up with really
9914 * small allocations, so if the allocator is returning small
9915 * chunks lets make its job easier by only searching for those
9916 * sized chunks.
9917 */
9918 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009919 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9920 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009921 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009922 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009923 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009924 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009925 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009926
9927 /*
9928 * We've reserved this space, and thus converted it from
9929 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9930 * from here on out we will only need to clear our reservation
9931 * for the remaining unreserved area, so advance our
9932 * clear_offset by our extent size.
9933 */
9934 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009935 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009936
Josef Bacik0b670dc2015-09-23 17:11:16 -04009937 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04009938 ret = insert_reserved_file_extent(trans, inode,
9939 cur_offset, ins.objectid,
9940 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009941 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009942 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009943 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009944 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009945 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009946 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009947 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009948 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009949 break;
9950 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009951
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009952 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009953 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009954
Josef Bacik5dc562c2012-08-17 13:14:17 -04009955 em = alloc_extent_map();
9956 if (!em) {
9957 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9958 &BTRFS_I(inode)->runtime_flags);
9959 goto next;
9960 }
9961
9962 em->start = cur_offset;
9963 em->orig_start = cur_offset;
9964 em->len = ins.offset;
9965 em->block_start = ins.objectid;
9966 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009967 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009968 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009969 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9970 em->generation = trans->transid;
9971
9972 while (1) {
9973 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009974 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009975 write_unlock(&em_tree->lock);
9976 if (ret != -EEXIST)
9977 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009978 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009979 cur_offset + ins.offset - 1,
9980 0);
9981 }
9982 free_extent_map(em);
9983next:
Yan Zhengd899e052008-10-30 14:25:28 -04009984 num_bytes -= ins.offset;
9985 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009986 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009987
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009988 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009989 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009990 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009991 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009992 (actual_len > inode->i_size) &&
9993 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009994 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009995 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009996 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009997 i_size = cur_offset;
9998 i_size_write(inode, i_size);
9999 btrfs_ordered_update_i_size(inode, i_size, NULL);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010000 }
10001
Yan Zhengd899e052008-10-30 14:25:28 -040010002 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010003
10004 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -040010005 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010006 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010007 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +010010008 break;
10009 }
Yan Zhengd899e052008-10-30 14:25:28 -040010010
Josef Bacik0af3d002010-06-21 14:48:16 -040010011 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010012 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +000010013 }
Josef Bacikb778cf92020-02-13 10:47:31 -050010014 if (clear_offset < end)
10015 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
10016 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -040010017 return ret;
10018}
10019
Josef Bacik0af3d002010-06-21 14:48:16 -040010020int btrfs_prealloc_file_range(struct inode *inode, int mode,
10021 u64 start, u64 num_bytes, u64 min_size,
10022 loff_t actual_len, u64 *alloc_hint)
10023{
10024 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10025 min_size, actual_len, alloc_hint,
10026 NULL);
10027}
10028
10029int btrfs_prealloc_file_range_trans(struct inode *inode,
10030 struct btrfs_trans_handle *trans, int mode,
10031 u64 start, u64 num_bytes, u64 min_size,
10032 loff_t actual_len, u64 *alloc_hint)
10033{
10034 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10035 min_size, actual_len, alloc_hint, trans);
10036}
10037
Chris Masone6dcd2d2008-07-17 12:53:50 -040010038static int btrfs_set_page_dirty(struct page *page)
10039{
Chris Masone6dcd2d2008-07-17 12:53:50 -040010040 return __set_page_dirty_nobuffers(page);
10041}
10042
Al Viro10556cb22011-06-20 19:28:19 -040010043static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -050010044{
Li Zefanb83cc962010-12-20 16:04:08 +080010045 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010046 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +080010047
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +000010048 if (mask & MAY_WRITE &&
10049 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10050 if (btrfs_root_readonly(root))
10051 return -EROFS;
10052 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10053 return -EACCES;
10054 }
Al Viro2830ba72011-06-20 19:16:29 -040010055 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -050010056}
Chris Mason39279cc2007-06-12 06:35:45 -040010057
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010058static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10059{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010060 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010061 struct btrfs_trans_handle *trans;
10062 struct btrfs_root *root = BTRFS_I(dir)->root;
10063 struct inode *inode = NULL;
10064 u64 objectid;
10065 u64 index;
10066 int ret = 0;
10067
10068 /*
10069 * 5 units required for adding orphan entry
10070 */
10071 trans = btrfs_start_transaction(root, 5);
10072 if (IS_ERR(trans))
10073 return PTR_ERR(trans);
10074
10075 ret = btrfs_find_free_ino(root, &objectid);
10076 if (ret)
10077 goto out;
10078
10079 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +010010080 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010081 if (IS_ERR(inode)) {
10082 ret = PTR_ERR(inode);
10083 inode = NULL;
10084 goto out;
10085 }
10086
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010087 inode->i_fop = &btrfs_file_operations;
10088 inode->i_op = &btrfs_file_inode_operations;
10089
10090 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010091 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10092
Chris Masonb0d5d102014-09-08 13:08:51 -070010093 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10094 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010095 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -070010096
10097 ret = btrfs_update_inode(trans, root, inode);
10098 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010099 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +020010100 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010101 if (ret)
Al Viro32955c52018-05-16 12:20:05 -040010102 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010103
Filipe Manana5762b5c2014-08-01 00:10:32 +010010104 /*
10105 * We set number of links to 0 in btrfs_new_inode(), and here we set
10106 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10107 * through:
10108 *
10109 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10110 */
10111 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010112 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -040010113 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010114 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010115out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -040010116 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -040010117 if (ret && inode)
10118 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -040010119 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010120 return ret;
10121}
10122
David Sterba5cdc84b2018-07-18 20:32:52 +020010123void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010124{
David Sterba5cdc84b2018-07-18 20:32:52 +020010125 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010126 unsigned long index = start >> PAGE_SHIFT;
10127 unsigned long end_index = end >> PAGE_SHIFT;
10128 struct page *page;
10129
10130 while (index <= end_index) {
10131 page = find_get_page(inode->i_mapping, index);
10132 ASSERT(page); /* Pages should be in the extent_io_tree */
10133 set_page_writeback(page);
10134 put_page(page);
10135 index++;
10136 }
10137}
10138
Omar Sandovaled46ff32016-11-03 10:28:14 -070010139#ifdef CONFIG_SWAP
10140/*
10141 * Add an entry indicating a block group or device which is pinned by a
10142 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10143 * negative errno on failure.
10144 */
10145static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10146 bool is_block_group)
10147{
10148 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10149 struct btrfs_swapfile_pin *sp, *entry;
10150 struct rb_node **p;
10151 struct rb_node *parent = NULL;
10152
10153 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10154 if (!sp)
10155 return -ENOMEM;
10156 sp->ptr = ptr;
10157 sp->inode = inode;
10158 sp->is_block_group = is_block_group;
10159
10160 spin_lock(&fs_info->swapfile_pins_lock);
10161 p = &fs_info->swapfile_pins.rb_node;
10162 while (*p) {
10163 parent = *p;
10164 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10165 if (sp->ptr < entry->ptr ||
10166 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10167 p = &(*p)->rb_left;
10168 } else if (sp->ptr > entry->ptr ||
10169 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10170 p = &(*p)->rb_right;
10171 } else {
10172 spin_unlock(&fs_info->swapfile_pins_lock);
10173 kfree(sp);
10174 return 1;
10175 }
10176 }
10177 rb_link_node(&sp->node, parent, p);
10178 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10179 spin_unlock(&fs_info->swapfile_pins_lock);
10180 return 0;
10181}
10182
10183/* Free all of the entries pinned by this swapfile. */
10184static void btrfs_free_swapfile_pins(struct inode *inode)
10185{
10186 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10187 struct btrfs_swapfile_pin *sp;
10188 struct rb_node *node, *next;
10189
10190 spin_lock(&fs_info->swapfile_pins_lock);
10191 node = rb_first(&fs_info->swapfile_pins);
10192 while (node) {
10193 next = rb_next(node);
10194 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10195 if (sp->inode == inode) {
10196 rb_erase(&sp->node, &fs_info->swapfile_pins);
10197 if (sp->is_block_group)
10198 btrfs_put_block_group(sp->ptr);
10199 kfree(sp);
10200 }
10201 node = next;
10202 }
10203 spin_unlock(&fs_info->swapfile_pins_lock);
10204}
10205
10206struct btrfs_swap_info {
10207 u64 start;
10208 u64 block_start;
10209 u64 block_len;
10210 u64 lowest_ppage;
10211 u64 highest_ppage;
10212 unsigned long nr_pages;
10213 int nr_extents;
10214};
10215
10216static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10217 struct btrfs_swap_info *bsi)
10218{
10219 unsigned long nr_pages;
10220 u64 first_ppage, first_ppage_reported, next_ppage;
10221 int ret;
10222
10223 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10224 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10225 PAGE_SIZE) >> PAGE_SHIFT;
10226
10227 if (first_ppage >= next_ppage)
10228 return 0;
10229 nr_pages = next_ppage - first_ppage;
10230
10231 first_ppage_reported = first_ppage;
10232 if (bsi->start == 0)
10233 first_ppage_reported++;
10234 if (bsi->lowest_ppage > first_ppage_reported)
10235 bsi->lowest_ppage = first_ppage_reported;
10236 if (bsi->highest_ppage < (next_ppage - 1))
10237 bsi->highest_ppage = next_ppage - 1;
10238
10239 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10240 if (ret < 0)
10241 return ret;
10242 bsi->nr_extents += ret;
10243 bsi->nr_pages += nr_pages;
10244 return 0;
10245}
10246
10247static void btrfs_swap_deactivate(struct file *file)
10248{
10249 struct inode *inode = file_inode(file);
10250
10251 btrfs_free_swapfile_pins(inode);
10252 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10253}
10254
10255static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10256 sector_t *span)
10257{
10258 struct inode *inode = file_inode(file);
10259 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10260 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10261 struct extent_state *cached_state = NULL;
10262 struct extent_map *em = NULL;
10263 struct btrfs_device *device = NULL;
10264 struct btrfs_swap_info bsi = {
10265 .lowest_ppage = (sector_t)-1ULL,
10266 };
10267 int ret = 0;
10268 u64 isize;
10269 u64 start;
10270
10271 /*
10272 * If the swap file was just created, make sure delalloc is done. If the
10273 * file changes again after this, the user is doing something stupid and
10274 * we don't really care.
10275 */
10276 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10277 if (ret)
10278 return ret;
10279
10280 /*
10281 * The inode is locked, so these flags won't change after we check them.
10282 */
10283 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10284 btrfs_warn(fs_info, "swapfile must not be compressed");
10285 return -EINVAL;
10286 }
10287 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10288 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10289 return -EINVAL;
10290 }
10291 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10292 btrfs_warn(fs_info, "swapfile must not be checksummed");
10293 return -EINVAL;
10294 }
10295
10296 /*
10297 * Balance or device remove/replace/resize can move stuff around from
10298 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10299 * concurrently while we are mapping the swap extents, and
10300 * fs_info->swapfile_pins prevents them from running while the swap file
10301 * is active and moving the extents. Note that this also prevents a
10302 * concurrent device add which isn't actually necessary, but it's not
10303 * really worth the trouble to allow it.
10304 */
10305 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10306 btrfs_warn(fs_info,
10307 "cannot activate swapfile while exclusive operation is running");
10308 return -EBUSY;
10309 }
10310 /*
10311 * Snapshots can create extents which require COW even if NODATACOW is
10312 * set. We use this counter to prevent snapshots. We must increment it
10313 * before walking the extents because we don't want a concurrent
10314 * snapshot to run after we've already checked the extents.
10315 */
10316 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10317
10318 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10319
10320 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10321 start = 0;
10322 while (start < isize) {
10323 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010324 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010325 u64 len = isize - start;
10326
Omar Sandoval39b07b52019-12-02 17:34:23 -080010327 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010328 if (IS_ERR(em)) {
10329 ret = PTR_ERR(em);
10330 goto out;
10331 }
10332
10333 if (em->block_start == EXTENT_MAP_HOLE) {
10334 btrfs_warn(fs_info, "swapfile must not have holes");
10335 ret = -EINVAL;
10336 goto out;
10337 }
10338 if (em->block_start == EXTENT_MAP_INLINE) {
10339 /*
10340 * It's unlikely we'll ever actually find ourselves
10341 * here, as a file small enough to fit inline won't be
10342 * big enough to store more than the swap header, but in
10343 * case something changes in the future, let's catch it
10344 * here rather than later.
10345 */
10346 btrfs_warn(fs_info, "swapfile must not be inline");
10347 ret = -EINVAL;
10348 goto out;
10349 }
10350 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10351 btrfs_warn(fs_info, "swapfile must not be compressed");
10352 ret = -EINVAL;
10353 goto out;
10354 }
10355
10356 logical_block_start = em->block_start + (start - em->start);
10357 len = min(len, em->len - (start - em->start));
10358 free_extent_map(em);
10359 em = NULL;
10360
10361 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10362 if (ret < 0) {
10363 goto out;
10364 } else if (ret) {
10365 ret = 0;
10366 } else {
10367 btrfs_warn(fs_info,
10368 "swapfile must not be copy-on-write");
10369 ret = -EINVAL;
10370 goto out;
10371 }
10372
10373 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10374 if (IS_ERR(em)) {
10375 ret = PTR_ERR(em);
10376 goto out;
10377 }
10378
10379 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10380 btrfs_warn(fs_info,
10381 "swapfile must have single data profile");
10382 ret = -EINVAL;
10383 goto out;
10384 }
10385
10386 if (device == NULL) {
10387 device = em->map_lookup->stripes[0].dev;
10388 ret = btrfs_add_swapfile_pin(inode, device, false);
10389 if (ret == 1)
10390 ret = 0;
10391 else if (ret)
10392 goto out;
10393 } else if (device != em->map_lookup->stripes[0].dev) {
10394 btrfs_warn(fs_info, "swapfile must be on one device");
10395 ret = -EINVAL;
10396 goto out;
10397 }
10398
10399 physical_block_start = (em->map_lookup->stripes[0].physical +
10400 (logical_block_start - em->start));
10401 len = min(len, em->len - (logical_block_start - em->start));
10402 free_extent_map(em);
10403 em = NULL;
10404
10405 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10406 if (!bg) {
10407 btrfs_warn(fs_info,
10408 "could not find block group containing swapfile");
10409 ret = -EINVAL;
10410 goto out;
10411 }
10412
10413 ret = btrfs_add_swapfile_pin(inode, bg, true);
10414 if (ret) {
10415 btrfs_put_block_group(bg);
10416 if (ret == 1)
10417 ret = 0;
10418 else
10419 goto out;
10420 }
10421
10422 if (bsi.block_len &&
10423 bsi.block_start + bsi.block_len == physical_block_start) {
10424 bsi.block_len += len;
10425 } else {
10426 if (bsi.block_len) {
10427 ret = btrfs_add_swap_extent(sis, &bsi);
10428 if (ret)
10429 goto out;
10430 }
10431 bsi.start = start;
10432 bsi.block_start = physical_block_start;
10433 bsi.block_len = len;
10434 }
10435
10436 start += len;
10437 }
10438
10439 if (bsi.block_len)
10440 ret = btrfs_add_swap_extent(sis, &bsi);
10441
10442out:
10443 if (!IS_ERR_OR_NULL(em))
10444 free_extent_map(em);
10445
10446 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10447
10448 if (ret)
10449 btrfs_swap_deactivate(file);
10450
10451 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10452
10453 if (ret)
10454 return ret;
10455
10456 if (device)
10457 sis->bdev = device->bdev;
10458 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10459 sis->max = bsi.nr_pages;
10460 sis->pages = bsi.nr_pages - 1;
10461 sis->highest_bit = bsi.nr_pages - 1;
10462 return bsi.nr_extents;
10463}
10464#else
10465static void btrfs_swap_deactivate(struct file *file)
10466{
10467}
10468
10469static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10470 sector_t *span)
10471{
10472 return -EOPNOTSUPP;
10473}
10474#endif
10475
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010476static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010477 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010478 .lookup = btrfs_lookup,
10479 .create = btrfs_create,
10480 .unlink = btrfs_unlink,
10481 .link = btrfs_link,
10482 .mkdir = btrfs_mkdir,
10483 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010484 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010485 .symlink = btrfs_symlink,
10486 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010487 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010488 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010489 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010490 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010491 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010492 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010493 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010494};
Yan, Zheng76dda932009-09-21 16:00:26 -040010495
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010496static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010497 .llseek = generic_file_llseek,
10498 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010499 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010500 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010501 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010502#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010503 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010504#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010505 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010506 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010507};
10508
David Sterba20e55062015-11-19 11:42:28 +010010509static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010510 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010511 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010512 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10513};
10514
Chris Mason35054392009-01-21 13:11:13 -050010515/*
10516 * btrfs doesn't support the bmap operation because swapfiles
10517 * use bmap to make a mapping of extents in the file. They assume
10518 * these extents won't change over the life of the file and they
10519 * use the bmap result to do IO directly to the drive.
10520 *
10521 * the btrfs bmap call would return logical addresses that aren't
10522 * suitable for IO and they also will change frequently as COW
10523 * operations happen. So, swapfile + btrfs == corruption.
10524 *
10525 * For now we're avoiding this by dropping bmap.
10526 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010527static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010528 .readpage = btrfs_readpage,
10529 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010530 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -050010531 .readpages = btrfs_readpages,
Chris Mason16432982008-04-10 10:23:21 -040010532 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010533 .invalidatepage = btrfs_invalidatepage,
10534 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -040010535 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010536 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010537 .swap_activate = btrfs_swap_activate,
10538 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010539};
10540
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010541static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010542 .getattr = btrfs_getattr,
10543 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010544 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010545 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010546 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010547 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010548 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010549 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010550};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010551static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010552 .getattr = btrfs_getattr,
10553 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010554 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010555 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010556 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010557 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010558 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010559};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010560static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010561 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010562 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010563 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010564 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010565 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010566 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010567};
Yan, Zheng76dda932009-09-21 16:00:26 -040010568
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010569const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010570 .d_delete = btrfs_dentry_delete,
10571};