blob: d901d53e4f03baeb6ab610bbc6dfe75f16c19dec [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Herbert Xu79990962020-06-12 16:57:37 +10006#include <crypto/hash.h>
Chris Mason8f18cf12008-04-25 16:53:30 -04007#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05008#include <linux/bio.h>
David Sterba55e20bd2020-06-09 19:56:06 +02009#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040010#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040011#include <linux/fs.h>
12#include <linux/pagemap.h>
13#include <linux/highmem.h>
14#include <linux/time.h>
15#include <linux/init.h>
16#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040019#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050020#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040021#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040022#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020024#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000025#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050026#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040027#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080028#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040029#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050030#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070031#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080032#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030033#include <linux/sched/mm.h>
David Sterba92d32172018-04-16 21:10:14 +020034#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020035#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040041#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040042#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040043#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020044#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040045#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050046#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050047#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080048#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000049#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080050#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040051#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040052#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010053#include "space-info.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020056 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040057 struct btrfs_root *root;
58};
59
Filipe Mananaf28a4922015-12-08 19:23:20 +000060struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000061 u64 reserve;
David Sterba55e20bd2020-06-09 19:56:06 +020062 u64 unsubmitted_oe_range_start;
63 u64 unsubmitted_oe_range_end;
64 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000065};
66
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070067static const struct inode_operations btrfs_dir_inode_operations;
68static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_special_inode_operations;
70static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070071static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010073static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
76struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000079struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080
Eric Sandeen3972f262013-01-12 02:57:22 +000081static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000082static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040083static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Chris Mason771ed682008-11-06 22:02:51 -050084static noinline int cow_file_range(struct inode *inode,
85 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030086 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030087 unsigned long *nr_written, int unlock);
Liu Bo6f9994d2017-01-31 07:50:22 -080088static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
89 u64 orig_start, u64 block_start,
90 u64 block_len, u64 orig_block_len,
91 u64 ram_bytes, int compress_type,
92 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040093
Qu Wenruo524272602017-03-08 10:25:52 +080094static void __endio_write_update_ordered(struct inode *inode,
95 const u64 offset, const u64 bytes,
96 const bool uptodate);
97
98/*
99 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100100 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800101 *
102 * NOTE: caller must ensure that when an error happens, it can not call
103 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
104 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
105 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200106 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800107 */
108static inline void btrfs_cleanup_ordered_extents(struct inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200109 struct page *locked_page,
110 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800111{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900112 unsigned long index = offset >> PAGE_SHIFT;
113 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200114 u64 page_start = page_offset(locked_page);
115 u64 page_end = page_start + PAGE_SIZE - 1;
116
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900117 struct page *page;
118
119 while (index <= end_index) {
120 page = find_get_page(inode->i_mapping, index);
121 index++;
122 if (!page)
123 continue;
124 ClearPagePrivate2(page);
125 put_page(page);
126 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200127
128 /*
129 * In case this page belongs to the delalloc range being instantiated
130 * then skip it, since the first page of a range is going to be
131 * properly cleaned up by the caller of run_delalloc_range
132 */
133 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
134 offset += PAGE_SIZE;
135 bytes -= PAGE_SIZE;
136 }
137
138 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800139}
140
Eric Sandeen48a3b632013-04-25 20:41:01 +0000141static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400142
Josef Bacik6a3891c2015-03-16 17:38:52 -0400143#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
144void btrfs_test_inode_set_ops(struct inode *inode)
145{
146 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
147}
148#endif
149
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000150static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500151 struct inode *inode, struct inode *dir,
152 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500153{
154 int err;
155
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000156 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500157 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500158 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500159 return err;
160}
161
Chris Masond352ac62008-09-29 15:18:18 -0400162/*
Chris Masonc8b97812008-10-29 14:49:59 -0400163 * this does all the hard work for inserting an inline extent into
164 * the btree. The caller should have done a btrfs_drop_extents so that
165 * no overlapping inline items exist in the btree
166 */
Chris Mason40f76582014-05-21 13:35:51 -0700167static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000168 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400169 struct btrfs_root *root, struct inode *inode,
170 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000171 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400172 struct page **compressed_pages)
173{
Chris Masonc8b97812008-10-29 14:49:59 -0400174 struct extent_buffer *leaf;
175 struct page *page = NULL;
176 char *kaddr;
177 unsigned long ptr;
178 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400179 int ret;
180 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400181 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400182
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800183 ASSERT((compressed_size > 0 && compressed_pages) ||
184 (compressed_size == 0 && !compressed_pages));
185
Li Zefanfe3f5662011-03-28 08:30:38 +0000186 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400187 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400188
Chris Masonc8b97812008-10-29 14:49:59 -0400189 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000190
191 if (!extent_inserted) {
192 struct btrfs_key key;
193 size_t datasize;
194
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200195 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000196 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200197 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000198
199 datasize = btrfs_file_extent_calc_inline_size(cur_size);
200 path->leave_spinning = 1;
201 ret = btrfs_insert_empty_item(trans, root, path, &key,
202 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200203 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000204 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400205 }
206 leaf = path->nodes[0];
207 ei = btrfs_item_ptr(leaf, path->slots[0],
208 struct btrfs_file_extent_item);
209 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
210 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
211 btrfs_set_file_extent_encryption(leaf, ei, 0);
212 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
213 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
214 ptr = btrfs_file_extent_inline_start(ei);
215
Li Zefan261507a02010-12-17 14:21:50 +0800216 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400217 struct page *cpage;
218 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500219 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400220 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500221 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300222 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400223
Cong Wang7ac687d2011-11-25 23:14:28 +0800224 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400225 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800226 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400227
228 i++;
229 ptr += cur_size;
230 compressed_size -= cur_size;
231 }
232 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800233 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400234 } else {
235 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300236 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400237 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800238 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100239 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400240 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800241 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300242 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400243 }
244 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000245 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400246
Yan, Zhengc2167752009-11-12 09:34:21 +0000247 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500248 * We align size to sectorsize for inline extents just for simplicity
249 * sake.
250 */
251 size = ALIGN(size, root->fs_info->sectorsize);
252 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
253 if (ret)
254 goto fail;
255
256 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000257 * we're an inline extent, so nobody can
258 * extend the file past i_size without locking
259 * a page we already have locked.
260 *
261 * We must do any isize and inode updates
262 * before we unlock the pages. Otherwise we
263 * could end up racing with unlink.
264 */
Chris Masonc8b97812008-10-29 14:49:59 -0400265 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100266 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000267
Chris Masonc8b97812008-10-29 14:49:59 -0400268fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200269 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400270}
271
272
273/*
274 * conditionally insert an inline extent into the file. This
275 * does the checks required to make sure the data is small enough
276 * to fit as an inline extent.
277 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200278static noinline int cow_file_range_inline(struct inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400279 u64 end, size_t compressed_size,
280 int compress_type,
281 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400282{
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200283 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400284 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400285 struct btrfs_trans_handle *trans;
Chris Masonc8b97812008-10-29 14:49:59 -0400286 u64 isize = i_size_read(inode);
287 u64 actual_end = min(end + 1, isize);
288 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400289 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400290 u64 data_len = inline_len;
291 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000292 struct btrfs_path *path;
293 int extent_inserted = 0;
294 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400295
296 if (compressed_size)
297 data_len = compressed_size;
298
299 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400300 actual_end > fs_info->sectorsize ||
301 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400302 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400303 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400304 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400305 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400306 return 1;
307 }
308
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000309 path = btrfs_alloc_path();
310 if (!path)
311 return -ENOMEM;
312
Josef Bacik00361582013-08-14 14:02:47 -0400313 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000314 if (IS_ERR(trans)) {
315 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400316 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000317 }
Josef Bacik69fe2d72017-10-19 14:15:57 -0400318 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400319
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000320 if (compressed_size && compressed_pages)
321 extent_item_size = btrfs_file_extent_calc_inline_size(
322 compressed_size);
323 else
324 extent_item_size = btrfs_file_extent_calc_inline_size(
325 inline_len);
326
327 ret = __btrfs_drop_extents(trans, root, inode, path,
328 start, aligned_end, NULL,
329 1, 1, extent_item_size, &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400330 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400331 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400332 goto out;
333 }
Chris Masonc8b97812008-10-29 14:49:59 -0400334
335 if (isize > actual_end)
336 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000337 ret = insert_inline_extent(trans, path, extent_inserted,
338 root, inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400339 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000340 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400341 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400342 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400343 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400344 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400345 ret = 1;
346 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100347 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400348
Josef Bacikbdc20e62013-02-28 13:23:38 -0500349 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200350 btrfs_drop_extent_cache(BTRFS_I(inode), start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400351out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800352 /*
353 * Don't forget to free the reserved space, as for inlined extent
354 * it won't count as data extent, free them directly here.
355 * And at reserve time, it's always aligned to page size, so
356 * just free one page here.
357 */
Qu Wenruobc42bda2017-02-27 15:10:39 +0800358 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000359 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400360 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400361 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400362}
363
Chris Mason771ed682008-11-06 22:02:51 -0500364struct async_extent {
365 u64 start;
366 u64 ram_size;
367 u64 compressed_size;
368 struct page **pages;
369 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800370 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500371 struct list_head list;
372};
373
Nikolay Borisov97db1202019-03-12 17:20:24 +0200374struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500375 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500376 struct page *locked_page;
377 u64 start;
378 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600379 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500380 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700381 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500382 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200383 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500384};
385
Nikolay Borisov97db1202019-03-12 17:20:24 +0200386struct async_cow {
387 /* Number of chunks in flight; must be first in the structure */
388 atomic_t num_chunks;
389 struct async_chunk chunks[];
390};
391
392static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500393 u64 start, u64 ram_size,
394 u64 compressed_size,
395 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800396 unsigned long nr_pages,
397 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500398{
399 struct async_extent *async_extent;
400
401 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100402 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500403 async_extent->start = start;
404 async_extent->ram_size = ram_size;
405 async_extent->compressed_size = compressed_size;
406 async_extent->pages = pages;
407 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800408 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500409 list_add_tail(&async_extent->list, &cow->extents);
410 return 0;
411}
412
Qu Wenruo42c16da2019-07-01 05:12:46 +0000413/*
414 * Check if the inode has flags compatible with compression
415 */
416static inline bool inode_can_compress(struct inode *inode)
417{
418 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
419 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
420 return false;
421 return true;
422}
423
424/*
425 * Check if the inode needs to be submitted to compression, based on mount
426 * options, defragmentation, properties or heuristics.
427 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300428static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800429{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400430 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800431
Qu Wenruo42c16da2019-07-01 05:12:46 +0000432 if (!inode_can_compress(inode)) {
433 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
434 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
435 btrfs_ino(BTRFS_I(inode)));
436 return 0;
437 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800438 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400439 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800440 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200441 /* defrag ioctl */
442 if (BTRFS_I(inode)->defrag_compress)
443 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800444 /* bad compression ratios */
445 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
446 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400447 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800448 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200449 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300450 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800451 return 0;
452}
453
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200454static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800455 u64 start, u64 end, u64 num_bytes, u64 small_write)
456{
457 /* If this is a small write inside eof, kick off a defrag */
458 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200459 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800460 btrfs_add_inode_defrag(NULL, inode);
461}
462
Chris Masonc8b97812008-10-29 14:49:59 -0400463/*
Chris Mason771ed682008-11-06 22:02:51 -0500464 * we create compressed extents in two phases. The first
465 * phase compresses a range of pages that have already been
466 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400467 *
Chris Mason771ed682008-11-06 22:02:51 -0500468 * This is done inside an ordered work queue, and the compression
469 * is spread across many cpus. The actual IO submission is step
470 * two, and the ordered work queue takes care of making sure that
471 * happens in the same order things were put onto the queue by
472 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400473 *
Chris Mason771ed682008-11-06 22:02:51 -0500474 * If this code finds it can't get good compression, it puts an
475 * entry onto the work queue to write the uncompressed bytes. This
476 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300477 * are written in the same order that the flusher thread sent them
478 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400479 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300480static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400481{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200482 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400483 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400484 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200485 u64 start = async_chunk->start;
486 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400487 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400488 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400489 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400490 struct page **pages = NULL;
491 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400492 unsigned long total_compressed = 0;
493 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400494 int i;
495 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400496 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300497 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400498 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400499
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200500 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
501 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400502
Josef Bacikd98da492019-10-11 09:03:54 -0400503 /*
504 * We need to save i_size before now because it could change in between
505 * us evaluating the size and assigning it. This is because we lock and
506 * unlock the page in truncate and fallocate, and then modify the i_size
507 * later on.
508 *
509 * The barriers are to emulate READ_ONCE, remove that once i_size_read
510 * does that for us.
511 */
512 barrier();
513 i_size = i_size_read(inode);
514 barrier();
515 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400516again:
517 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300518 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100519 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
520 nr_pages = min_t(unsigned long, nr_pages,
521 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400522
Chris Masonf03d9301f2009-02-04 09:31:06 -0500523 /*
524 * we don't want to send crud past the end of i_size through
525 * compression, that's just a waste of CPU time. So, if the
526 * end of the file is before the start of our current
527 * requested range of bytes, we bail out to the uncompressed
528 * cleanup code that can deal with all of this.
529 *
530 * It isn't really the fastest way to fix things, but this is a
531 * very uncommon corner.
532 */
533 if (actual_end <= start)
534 goto cleanup_and_bail_uncompressed;
535
Chris Masonc8b97812008-10-29 14:49:59 -0400536 total_compressed = actual_end - start;
537
Shilong Wang4bcbb332014-10-07 18:44:35 -0400538 /*
539 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400540 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400541 */
542 if (total_compressed <= blocksize &&
543 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
544 goto cleanup_and_bail_uncompressed;
545
David Sterba069eac72017-02-14 19:36:54 +0100546 total_compressed = min_t(unsigned long, total_compressed,
547 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400548 total_in = 0;
549 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400550
Chris Mason771ed682008-11-06 22:02:51 -0500551 /*
552 * we do compression for mount -o compress and when the
553 * inode has not been flagged as nocompress. This flag can
554 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400555 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300556 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400557 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100558 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800559 if (!pages) {
560 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100561 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800562 goto cont;
563 }
Chris Mason179e29e2007-11-01 11:28:41 -0400564
David Sterbaeec63c62017-07-17 19:41:31 +0200565 if (BTRFS_I(inode)->defrag_compress)
566 compress_type = BTRFS_I(inode)->defrag_compress;
567 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200568 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800569
Chris Mason4adaa612013-03-26 13:07:00 -0400570 /*
571 * we need to call clear_page_dirty_for_io on each
572 * page in the range. Otherwise applications with the file
573 * mmap'd can wander in and change the page contents while
574 * we are compressing them.
575 *
576 * If the compression fails for any reason, we set the pages
577 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300578 *
579 * Note that the remaining part is redirtied, the start pointer
580 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400581 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300582 if (!redirty) {
583 extent_range_clear_dirty_for_io(inode, start, end);
584 redirty = 1;
585 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200586
587 /* Compression level is applied here and only here */
588 ret = btrfs_compress_pages(
589 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800590 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100591 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100592 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800593 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100594 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400595
596 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100597 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100598 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400599 char *kaddr;
600
601 /* zero the tail end of the last page, we might be
602 * sending it down to disk
603 */
604 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800605 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400606 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300607 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800608 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400609 }
610 will_compress = 1;
611 }
612 }
Li Zefan560f7d72011-09-08 10:22:01 +0800613cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400614 if (start == 0) {
615 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300616 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400617 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500618 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400619 */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200620 ret = cow_file_range_inline(inode, start, end, 0,
621 BTRFS_COMPRESS_NONE, NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400622 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500623 /* try making a compressed inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +0200624 ret = cow_file_range_inline(inode, start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000625 total_compressed,
626 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400627 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100628 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400629 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400630 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
631 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100632 unsigned long page_error_op;
633
Filipe Mananae6eb4312014-10-10 10:45:12 +0100634 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400635
Chris Mason771ed682008-11-06 22:02:51 -0500636 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100637 * inline extent creation worked or returned error,
638 * we don't need to create any more async work items.
639 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400640 *
641 * We use DO_ACCOUNTING here because we need the
642 * delalloc_release_metadata to be done _after_ we drop
643 * our outstanding extent for clearing delalloc for this
644 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500645 */
Nikolay Borisov74e91942019-07-17 16:18:16 +0300646 extent_clear_unlock_delalloc(inode, start, end, NULL,
647 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800648 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400649 PAGE_CLEAR_DIRTY |
650 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100651 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400652 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300653
654 for (i = 0; i < nr_pages; i++) {
655 WARN_ON(pages[i]->mapping);
656 put_page(pages[i]);
657 }
658 kfree(pages);
659
660 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400661 }
662 }
663
664 if (will_compress) {
665 /*
666 * we aren't doing an inline extent round the compressed size
667 * up to a block size boundary so the allocator does sane
668 * things
669 */
Qu Wenruofda28322013-02-26 08:10:22 +0000670 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400671
672 /*
673 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300674 * win, compare the page count read with the blocks on disk,
675 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400676 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300677 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300678 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300679 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700680
681 /*
682 * The async work queues will take care of doing actual
683 * allocation on disk for these compressed pages, and
684 * will submit them to the elevator.
685 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200686 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100687 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700688 compress_type);
689
Timofey Titovets11708622017-10-03 18:06:01 +0300690 if (start + total_in < end) {
691 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700692 pages = NULL;
693 cond_resched();
694 goto again;
695 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300696 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400697 }
698 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700699 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400700 /*
701 * the compression code ran but failed to make things smaller,
702 * free any pages it allocated and our page pointer array
703 */
David Sterba4d3a8002017-02-14 19:04:07 +0100704 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400705 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300706 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400707 }
708 kfree(pages);
709 pages = NULL;
710 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100711 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400712
713 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400714 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200715 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500716 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500717 }
Chris Masonc8b97812008-10-29 14:49:59 -0400718 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500719cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700720 /*
721 * No compression, but we still need to write the pages in the file
722 * we've been given so far. redirty the locked page if it corresponds
723 * to our extent and set things up for the async work queue to run
724 * cow_file_range to do the normal delalloc dance.
725 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700726 if (async_chunk->locked_page &&
727 (page_offset(async_chunk->locked_page) >= start &&
728 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200729 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700730 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700731 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700732
733 if (redirty)
734 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200735 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700736 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300737 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500738
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300739 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500740}
Chris Mason771ed682008-11-06 22:02:51 -0500741
Filipe Manana40ae8372014-10-06 22:14:24 +0100742static void free_async_extent_pages(struct async_extent *async_extent)
743{
744 int i;
745
746 if (!async_extent->pages)
747 return;
748
749 for (i = 0; i < async_extent->nr_pages; i++) {
750 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300751 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100752 }
753 kfree(async_extent->pages);
754 async_extent->nr_pages = 0;
755 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500756}
757
758/*
759 * phase two of compressed writeback. This is the ordered portion
760 * of the code, which only gets called in the order the work was
761 * queued. We walk all the async extents created by compress_file_range
762 * and send them down to the disk.
763 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200764static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500765{
Nikolay Borisovb5326272019-03-12 17:20:25 +0200766 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400767 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason771ed682008-11-06 22:02:51 -0500768 struct async_extent *async_extent;
769 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500770 struct btrfs_key ins;
771 struct extent_map *em;
772 struct btrfs_root *root = BTRFS_I(inode)->root;
Nikolay Borisov43366502019-03-12 17:20:29 +0200773 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500774 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500775
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500776again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200777 while (!list_empty(&async_chunk->extents)) {
778 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500779 struct async_extent, list);
780 list_del(&async_extent->list);
781
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500782retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200783 lock_extent(io_tree, async_extent->start,
784 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500785 /* did the compression code fall back to uncompressed IO? */
786 if (!async_extent->pages) {
787 int page_started = 0;
788 unsigned long nr_written = 0;
789
Chris Mason771ed682008-11-06 22:02:51 -0500790 /* allocate blocks */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200791 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500792 async_extent->start,
793 async_extent->start +
794 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300795 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500796
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100797 /* JDM XXX */
798
Chris Mason771ed682008-11-06 22:02:51 -0500799 /*
800 * if page_started, cow_file_range inserted an
801 * inline extent and took care of all the unlocking
802 * and IO for us. Otherwise, we need to submit
803 * all those pages down to the drive.
804 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500805 if (!page_started && !ret)
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200806 extent_write_locked_range(inode,
807 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500808 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500809 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500810 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700811 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200812 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500813 kfree(async_extent);
814 cond_resched();
815 continue;
816 }
817
Wang Xiaoguang18513092016-07-25 15:51:40 +0800818 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500819 async_extent->compressed_size,
820 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800821 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500822 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100823 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500824
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400825 if (ret == -ENOSPC) {
826 unlock_extent(io_tree, async_extent->start,
827 async_extent->start +
828 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800829
830 /*
831 * we need to redirty the pages if we decide to
832 * fallback to uncompressed IO, otherwise we
833 * will not submit these pages down to lower
834 * layers.
835 */
836 extent_range_redirty_for_io(inode,
837 async_extent->start,
838 async_extent->start +
839 async_extent->ram_size - 1);
840
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100841 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400842 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500843 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500844 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000845 /*
846 * here we're doing allocation and writeback of the
847 * compressed pages
848 */
Liu Bo6f9994d2017-01-31 07:50:22 -0800849 em = create_io_em(inode, async_extent->start,
850 async_extent->ram_size, /* len */
851 async_extent->start, /* orig_start */
852 ins.objectid, /* block_start */
853 ins.offset, /* block_len */
854 ins.offset, /* orig_block_len */
855 async_extent->ram_size, /* ram_bytes */
856 async_extent->compress_type,
857 BTRFS_ORDERED_COMPRESSED);
858 if (IS_ERR(em))
859 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500860 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800861 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500862
Li Zefan261507a02010-12-17 14:21:50 +0800863 ret = btrfs_add_ordered_extent_compress(inode,
864 async_extent->start,
865 ins.objectid,
866 async_extent->ram_size,
867 ins.offset,
868 BTRFS_ORDERED_COMPRESSED,
869 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100870 if (ret) {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +0200871 btrfs_drop_extent_cache(BTRFS_I(inode),
872 async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100873 async_extent->start +
874 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500875 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100876 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400877 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500878
Chris Mason771ed682008-11-06 22:02:51 -0500879 /*
880 * clear dirty, set writeback and unlock the pages.
881 */
Josef Bacikc2790a22013-07-29 11:20:47 -0400882 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400883 async_extent->start +
884 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400885 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
886 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400887 PAGE_SET_WRITEBACK);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200888 if (btrfs_submit_compressed_write(inode,
Chris Masond3977122009-01-05 21:25:51 -0500889 async_extent->start,
890 async_extent->ram_size,
891 ins.objectid,
892 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600893 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700894 async_chunk->write_flags,
895 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100896 struct page *p = async_extent->pages[0];
897 const u64 start = async_extent->start;
898 const u64 end = start + async_extent->ram_size - 1;
899
900 p->mapping = inode->i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200901 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200902
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100903 p->mapping = NULL;
Nikolay Borisov74e91942019-07-17 16:18:16 +0300904 extent_clear_unlock_delalloc(inode, start, end,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800905 NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100906 PAGE_END_WRITEBACK |
907 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100908 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100909 }
Chris Mason771ed682008-11-06 22:02:51 -0500910 alloc_hint = ins.objectid + ins.offset;
911 kfree(async_extent);
912 cond_resched();
913 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100914 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500915out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400916 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400917 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100918out_free:
Josef Bacikc2790a22013-07-29 11:20:47 -0400919 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500920 async_extent->start +
921 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400922 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100923 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400924 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
925 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100926 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
927 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100928 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100929 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500930 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500931}
932
Josef Bacik4b46fce2010-05-23 11:00:55 -0400933static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
934 u64 num_bytes)
935{
936 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
937 struct extent_map *em;
938 u64 alloc_hint = 0;
939
940 read_lock(&em_tree->lock);
941 em = search_extent_mapping(em_tree, start, num_bytes);
942 if (em) {
943 /*
944 * if block start isn't an actual block number then find the
945 * first block in this inode and use that as a hint. If that
946 * block is also bogus then just don't worry about it.
947 */
948 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
949 free_extent_map(em);
950 em = search_extent_mapping(em_tree, 0, 0);
951 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
952 alloc_hint = em->block_start;
953 if (em)
954 free_extent_map(em);
955 } else {
956 alloc_hint = em->block_start;
957 free_extent_map(em);
958 }
959 }
960 read_unlock(&em_tree->lock);
961
962 return alloc_hint;
963}
964
Chris Mason771ed682008-11-06 22:02:51 -0500965/*
966 * when extent_io.c finds a delayed allocation range in the file,
967 * the call backs end up in this code. The basic idea is to
968 * allocate extents on disk for the range, and create ordered data structs
969 * in ram to track those extents.
970 *
971 * locked_page is the page that writepage had locked already. We use
972 * it to make sure we don't do extra locks or unlocks.
973 *
974 * *page_started is set to one if we unlock locked_page and do everything
975 * required to start IO on it. It may be clean and already done with
976 * IO when we return.
977 */
Josef Bacik00361582013-08-14 14:02:47 -0400978static noinline int cow_file_range(struct inode *inode,
979 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300980 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300981 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500982{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400983 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik00361582013-08-14 14:02:47 -0400984 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason771ed682008-11-06 22:02:51 -0500985 u64 alloc_hint = 0;
986 u64 num_bytes;
987 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000988 u64 cur_alloc_size = 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400989 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500990 struct btrfs_key ins;
991 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000992 unsigned clear_bits;
993 unsigned long page_ops;
994 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500995 int ret = 0;
996
Nikolay Borisov70ddc552017-02-20 13:50:35 +0200997 if (btrfs_is_free_space_inode(BTRFS_I(inode))) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400998 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500999 ret = -EINVAL;
1000 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001001 }
Chris Mason771ed682008-11-06 22:02:51 -05001002
Qu Wenruofda28322013-02-26 08:10:22 +00001003 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001004 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001005 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001006
Nikolay Borisov6158e1c2017-02-20 13:50:43 +02001007 inode_should_defrag(BTRFS_I(inode), start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001008
Chris Mason771ed682008-11-06 22:02:51 -05001009 if (start == 0) {
1010 /* lets try to make an inline extent */
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001011 ret = cow_file_range_inline(inode, start, end, 0,
1012 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001013 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001014 /*
1015 * We use DO_ACCOUNTING here because we need the
1016 * delalloc_release_metadata to be run _after_ we drop
1017 * our outstanding extent for clearing delalloc for this
1018 * range.
1019 */
Nikolay Borisov74e91942019-07-17 16:18:16 +03001020 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001021 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001022 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1023 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001024 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1025 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001026 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001027 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001028 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001029 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001030 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001032 }
1033 }
Chris Masonc8b97812008-10-29 14:49:59 -04001034
Josef Bacik4b46fce2010-05-23 11:00:55 -04001035 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001036 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1037 start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001038
Anand Jain3752d222018-02-15 12:29:38 +08001039 while (num_bytes > 0) {
1040 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001041 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001042 fs_info->sectorsize, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001043 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001044 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001045 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001046 cur_alloc_size = ins.offset;
1047 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001048
Chris Mason771ed682008-11-06 22:02:51 -05001049 ram_size = ins.offset;
Liu Bo6f9994d2017-01-31 07:50:22 -08001050 em = create_io_em(inode, start, ins.offset, /* len */
1051 start, /* orig_start */
1052 ins.objectid, /* block_start */
1053 ins.offset, /* block_len */
1054 ins.offset, /* orig_block_len */
1055 ram_size, /* ram_bytes */
1056 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001057 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001058 if (IS_ERR(em)) {
1059 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001060 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001061 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001062 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001063
Chris Masone6dcd2d2008-07-17 12:53:50 -04001064 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Chris Mason771ed682008-11-06 22:02:51 -05001065 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001066 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001067 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001068
Yan Zheng17d217f2008-12-12 10:03:38 -05001069 if (root->root_key.objectid ==
1070 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1071 ret = btrfs_reloc_clone_csums(inode, start,
1072 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001073 /*
1074 * Only drop cache here, and process as normal.
1075 *
1076 * We must not allow extent_clear_unlock_delalloc()
1077 * at out_unlock label to free meta of this ordered
1078 * extent, as its meta should be freed by
1079 * btrfs_finish_ordered_io().
1080 *
1081 * So we must continue until @start is increased to
1082 * skip current ordered extent.
1083 */
Josef Bacik00361582013-08-14 14:02:47 -04001084 if (ret)
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001085 btrfs_drop_extent_cache(BTRFS_I(inode), start,
1086 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001087 }
1088
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001089 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001090
Chris Masonc8b97812008-10-29 14:49:59 -04001091 /* we're not doing compressed IO, don't unlock the first
1092 * page (which the caller expects to stay locked), don't
1093 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001094 *
1095 * Do set the Private2 bit so we know this page was properly
1096 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001097 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001098 page_ops = unlock ? PAGE_UNLOCK : 0;
1099 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001100
Josef Bacikc2790a22013-07-29 11:20:47 -04001101 extent_clear_unlock_delalloc(inode, start,
Qu Wenruoba8b04c2016-07-19 16:50:36 +08001102 start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001103 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001104 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001105 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001106 if (num_bytes < cur_alloc_size)
1107 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001108 else
Anand Jain3752d222018-02-15 12:29:38 +08001109 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001110 alloc_hint = ins.objectid + ins.offset;
1111 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001112 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001113
1114 /*
1115 * btrfs_reloc_clone_csums() error, since start is increased
1116 * extent_clear_unlock_delalloc() at out_unlock label won't
1117 * free metadata of current ordered extent, we're OK to exit.
1118 */
1119 if (ret)
1120 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001121 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001122out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001123 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001124
Filipe Mananad9f85962014-08-25 10:43:00 +01001125out_drop_extent_cache:
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02001126 btrfs_drop_extent_cache(BTRFS_I(inode), start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001127out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001128 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001129 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001130out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001131 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1132 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001133 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1134 PAGE_END_WRITEBACK;
1135 /*
1136 * If we reserved an extent for our delalloc range (or a subrange) and
1137 * failed to create the respective ordered extent, then it means that
1138 * when we reserved the extent we decremented the extent's size from
1139 * the data space_info's bytes_may_use counter and incremented the
1140 * space_info's bytes_reserved counter by the same amount. We must make
1141 * sure extent_clear_unlock_delalloc() does not try to decrement again
1142 * the data space_info's bytes_may_use counter, therefore we do not pass
1143 * it the flag EXTENT_CLEAR_DATA_RESV.
1144 */
1145 if (extent_reserved) {
1146 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001147 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001148 locked_page,
1149 clear_bits,
1150 page_ops);
1151 start += cur_alloc_size;
1152 if (start >= end)
1153 goto out;
1154 }
Nikolay Borisov74e91942019-07-17 16:18:16 +03001155 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001156 clear_bits | EXTENT_CLEAR_DATA_RESV,
1157 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001158 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001159}
Chris Masonc8b97812008-10-29 14:49:59 -04001160
Chris Mason771ed682008-11-06 22:02:51 -05001161/*
1162 * work queue call back to started compression on a file and pages
1163 */
1164static noinline void async_cow_start(struct btrfs_work *work)
1165{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001166 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001167 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001168
Nikolay Borisovb5326272019-03-12 17:20:25 +02001169 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001170
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001171 compressed_extents = compress_file_range(async_chunk);
1172 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001173 btrfs_add_delayed_iput(async_chunk->inode);
1174 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001175 }
Chris Mason771ed682008-11-06 22:02:51 -05001176}
1177
1178/*
1179 * work queue call back to submit previously compressed pages
1180 */
1181static noinline void async_cow_submit(struct btrfs_work *work)
1182{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001183 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1184 work);
1185 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001186 unsigned long nr_pages;
1187
Nikolay Borisovb5326272019-03-12 17:20:25 +02001188 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001189 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001190
David Sterba093258e2018-02-26 16:15:17 +01001191 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001192 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001193 5 * SZ_1M)
1194 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001195
Nikolay Borisov4546d172019-01-03 10:50:03 +02001196 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001197 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001198 * in which case we don't have anything to submit, yet we need to
1199 * always adjust ->async_delalloc_pages as its paired with the init
1200 * happening in cow_file_range_async
1201 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001202 if (async_chunk->inode)
1203 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001204}
Chris Masonc8b97812008-10-29 14:49:59 -04001205
Chris Mason771ed682008-11-06 22:02:51 -05001206static noinline void async_cow_free(struct btrfs_work *work)
1207{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001208 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001209
Nikolay Borisovb5326272019-03-12 17:20:25 +02001210 async_chunk = container_of(work, struct async_chunk, work);
1211 if (async_chunk->inode)
1212 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001213 if (async_chunk->blkcg_css)
1214 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001215 /*
1216 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001217 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001218 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001219 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001220 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001221}
1222
Chris Masonec39f762019-07-10 12:28:17 -07001223static int cow_file_range_async(struct inode *inode,
1224 struct writeback_control *wbc,
1225 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001226 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001227 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001228{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001229 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonec39f762019-07-10 12:28:17 -07001230 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001231 struct async_cow *ctx;
1232 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001233 unsigned long nr_pages;
1234 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001235 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1236 int i;
1237 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001238 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001239 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001240
Nikolay Borisov69684c52019-03-12 17:20:28 +02001241 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001242
1243 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1244 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1245 num_chunks = 1;
1246 should_compress = false;
1247 } else {
1248 should_compress = true;
1249 }
1250
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001251 nofs_flag = memalloc_nofs_save();
1252 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1253 memalloc_nofs_restore(nofs_flag);
1254
Nikolay Borisov97db1202019-03-12 17:20:24 +02001255 if (!ctx) {
1256 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1257 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1258 EXTENT_DO_ACCOUNTING;
1259 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1260 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1261 PAGE_SET_ERROR;
1262
Nikolay Borisov74e91942019-07-17 16:18:16 +03001263 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Nikolay Borisov97db1202019-03-12 17:20:24 +02001264 clear_bits, page_ops);
1265 return -ENOMEM;
1266 }
1267
1268 async_chunk = ctx->chunks;
1269 atomic_set(&ctx->num_chunks, num_chunks);
1270
1271 for (i = 0; i < num_chunks; i++) {
1272 if (should_compress)
1273 cur_end = min(end, start + SZ_512K - 1);
1274 else
1275 cur_end = end;
1276
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001277 /*
1278 * igrab is called higher up in the call chain, take only the
1279 * lightweight reference for the callback lifetime
1280 */
1281 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001282 async_chunk[i].pending = &ctx->num_chunks;
1283 async_chunk[i].inode = inode;
1284 async_chunk[i].start = start;
1285 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001286 async_chunk[i].write_flags = write_flags;
1287 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001288
Chris Mason1d53c9e2019-07-10 12:28:16 -07001289 /*
1290 * The locked_page comes all the way from writepage and its
1291 * the original page we were actually given. As we spread
1292 * this large delalloc region across multiple async_chunk
1293 * structs, only the first struct needs a pointer to locked_page
1294 *
1295 * This way we don't need racey decisions about who is supposed
1296 * to unlock it.
1297 */
1298 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001299 /*
1300 * Depending on the compressibility, the pages might or
1301 * might not go through async. We want all of them to
1302 * be accounted against wbc once. Let's do it here
1303 * before the paths diverge. wbc accounting is used
1304 * only for foreign writeback detection and doesn't
1305 * need full accuracy. Just account the whole thing
1306 * against the first page.
1307 */
1308 wbc_account_cgroup_owner(wbc, locked_page,
1309 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001310 async_chunk[i].locked_page = locked_page;
1311 locked_page = NULL;
1312 } else {
1313 async_chunk[i].locked_page = NULL;
1314 }
1315
Chris Masonec39f762019-07-10 12:28:17 -07001316 if (blkcg_css != blkcg_root_css) {
1317 css_get(blkcg_css);
1318 async_chunk[i].blkcg_css = blkcg_css;
1319 } else {
1320 async_chunk[i].blkcg_css = NULL;
1321 }
1322
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001323 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1324 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001325
Nikolay Borisov97db1202019-03-12 17:20:24 +02001326 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001327 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001328
Nikolay Borisov97db1202019-03-12 17:20:24 +02001329 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001330
Chris Mason771ed682008-11-06 22:02:51 -05001331 *nr_written += nr_pages;
1332 start = cur_end + 1;
1333 }
1334 *page_started = 1;
1335 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001336}
1337
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001338static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001339 u64 bytenr, u64 num_bytes)
1340{
1341 int ret;
1342 struct btrfs_ordered_sum *sums;
1343 LIST_HEAD(list);
1344
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001345 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001346 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001347 if (ret == 0 && list_empty(&list))
1348 return 0;
1349
1350 while (!list_empty(&list)) {
1351 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1352 list_del(&sums->list);
1353 kfree(sums);
1354 }
Liu Bo58113752018-01-31 17:09:13 -07001355 if (ret < 0)
1356 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001357 return 1;
1358}
1359
Filipe Manana467dc472020-05-27 11:16:07 +01001360static int fallback_to_cow(struct inode *inode, struct page *locked_page,
1361 const u64 start, const u64 end,
1362 int *page_started, unsigned long *nr_written)
1363{
Filipe Manana2166e5e2020-05-27 11:16:19 +01001364 const bool is_space_ino = btrfs_is_free_space_inode(BTRFS_I(inode));
1365 const u64 range_bytes = end + 1 - start;
Filipe Manana467dc472020-05-27 11:16:07 +01001366 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1367 u64 range_start = start;
1368 u64 count;
1369
1370 /*
1371 * If EXTENT_NORESERVE is set it means that when the buffered write was
1372 * made we had not enough available data space and therefore we did not
1373 * reserve data space for it, since we though we could do NOCOW for the
1374 * respective file range (either there is prealloc extent or the inode
1375 * has the NOCOW bit set).
1376 *
1377 * However when we need to fallback to COW mode (because for example the
1378 * block group for the corresponding extent was turned to RO mode by a
1379 * scrub or relocation) we need to do the following:
1380 *
1381 * 1) We increment the bytes_may_use counter of the data space info.
1382 * If COW succeeds, it allocates a new data extent and after doing
1383 * that it decrements the space info's bytes_may_use counter and
1384 * increments its bytes_reserved counter by the same amount (we do
1385 * this at btrfs_add_reserved_bytes()). So we need to increment the
1386 * bytes_may_use counter to compensate (when space is reserved at
1387 * buffered write time, the bytes_may_use counter is incremented);
1388 *
1389 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1390 * that if the COW path fails for any reason, it decrements (through
1391 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1392 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001393 *
1394 * If we need to fallback to cow and the inode corresponds to a free
1395 * space cache inode, we must also increment bytes_may_use of the data
1396 * space_info for the same reason. Space caches always get a prealloc
1397 * extent for them, however scrub or balance may have set the block
1398 * group that contains that extent to RO mode.
Filipe Manana467dc472020-05-27 11:16:07 +01001399 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001400 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001401 EXTENT_NORESERVE, 0);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001402 if (count > 0 || is_space_ino) {
1403 const u64 bytes = is_space_ino ? range_bytes : count;
Filipe Manana467dc472020-05-27 11:16:07 +01001404 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1405 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1406
1407 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001408 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001409 spin_unlock(&sinfo->lock);
1410
Filipe Manana2166e5e2020-05-27 11:16:19 +01001411 if (count > 0)
1412 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1413 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001414 }
1415
1416 return cow_file_range(inode, locked_page, start, end, page_started,
1417 nr_written, 1);
1418}
1419
Chris Masond352ac62008-09-29 15:18:18 -04001420/*
1421 * when nowcow writeback call back. This checks for snapshots or COW copies
1422 * of the extents that exist in the file, and COWs the file as required.
1423 *
1424 * If no cow copies or snapshots exist, we write directly to the existing
1425 * blocks on disk
1426 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001427static noinline int run_delalloc_nocow(struct inode *inode,
1428 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001429 const u64 start, const u64 end,
1430 int *page_started, int force,
1431 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001432{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001433 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001434 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001435 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001436 u64 cow_start = (u64)-1;
1437 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001438 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001439 bool check_prev = true;
1440 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001441 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001442 bool nocow = false;
1443 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001444
1445 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001446 if (!path) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001447 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001448 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001449 EXTENT_DO_ACCOUNTING |
1450 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001451 PAGE_CLEAR_DIRTY |
1452 PAGE_SET_WRITEBACK |
1453 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001454 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001455 }
Li Zefan82d59022011-04-20 10:33:24 +08001456
Yan Zheng80ff3852008-10-30 14:20:02 -04001457 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001458 struct btrfs_key found_key;
1459 struct btrfs_file_extent_item *fi;
1460 struct extent_buffer *leaf;
1461 u64 extent_end;
1462 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001463 u64 num_bytes = 0;
1464 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001465 u64 ram_bytes;
1466 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001467
1468 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001469
Liu Boe4c3b2d2017-01-30 12:25:28 -08001470 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001471 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001472 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001473 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001474
1475 /*
1476 * If there is no extent for our range when doing the initial
1477 * search, then go back to the previous slot as it will be the
1478 * one containing the search offset
1479 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001480 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1481 leaf = path->nodes[0];
1482 btrfs_item_key_to_cpu(leaf, &found_key,
1483 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001484 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001485 found_key.type == BTRFS_EXTENT_DATA_KEY)
1486 path->slots[0]--;
1487 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001488 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001489next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001490 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001491 leaf = path->nodes[0];
1492 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1493 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001494 if (ret < 0) {
1495 if (cow_start != (u64)-1)
1496 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001497 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001498 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001499 if (ret > 0)
1500 break;
1501 leaf = path->nodes[0];
1502 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001503
Yan Zheng80ff3852008-10-30 14:20:02 -04001504 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001505
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001506 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001507 if (found_key.objectid > ino)
1508 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001509 /*
1510 * Keep searching until we find an EXTENT_ITEM or there are no
1511 * more extents for this inode
1512 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001513 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1514 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1515 path->slots[0]++;
1516 goto next_slot;
1517 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001518
1519 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001520 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001521 found_key.offset > end)
1522 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001523
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001524 /*
1525 * If the found extent starts after requested offset, then
1526 * adjust extent_end to be right before this extent begins
1527 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001528 if (found_key.offset > cur_offset) {
1529 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001530 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001531 goto out_check;
1532 }
Chris Masonc31f8832008-01-08 15:46:31 -05001533
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001534 /*
1535 * Found extent which begins before our range and potentially
1536 * intersect it
1537 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001538 fi = btrfs_item_ptr(leaf, path->slots[0],
1539 struct btrfs_file_extent_item);
1540 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001541
Josef Bacikcc95bef2013-04-04 14:31:27 -04001542 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001543 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1544 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001545 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001546 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001547 extent_end = found_key.offset +
1548 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001549 disk_num_bytes =
1550 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001551 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001552 * If the extent we got ends before our current offset,
1553 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001554 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001555 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001556 path->slots[0]++;
1557 goto next_slot;
1558 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001559 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001560 if (disk_bytenr == 0)
1561 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001562 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001563 if (btrfs_file_extent_compression(leaf, fi) ||
1564 btrfs_file_extent_encryption(leaf, fi) ||
1565 btrfs_file_extent_other_encoding(leaf, fi))
1566 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001567 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001568 * If extent is created before the last volume's snapshot
1569 * this implies the extent is shared, hence we can't do
1570 * nocow. This is the same check as in
1571 * btrfs_cross_ref_exist but without calling
1572 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001573 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001574 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001575 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001576 btrfs_root_last_snapshot(&root->root_item))
1577 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001578 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1579 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001580 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001581 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001582 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001583 ret = btrfs_cross_ref_exist(root, ino,
1584 found_key.offset -
1585 extent_offset, disk_bytenr);
1586 if (ret) {
1587 /*
1588 * ret could be -EIO if the above fails to read
1589 * metadata.
1590 */
1591 if (ret < 0) {
1592 if (cow_start != (u64)-1)
1593 cur_offset = cow_start;
1594 goto error;
1595 }
1596
Nikolay Borisov3e024842019-08-21 10:42:03 +03001597 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001598 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001599 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001600 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001601 disk_bytenr += cur_offset - found_key.offset;
1602 num_bytes = min(end + 1, extent_end) - cur_offset;
1603 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001604 * If there are pending snapshots for this root, we
1605 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001606 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001607 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001608 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001609 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001610 * force cow if csum exists in the range.
1611 * this ensure that csum for a given extent are
1612 * either valid or do not exist.
1613 */
Liu Bo58113752018-01-31 17:09:13 -07001614 ret = csum_exist_in_range(fs_info, disk_bytenr,
1615 num_bytes);
1616 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001617 /*
1618 * ret could be -EIO if the above fails to read
1619 * metadata.
1620 */
1621 if (ret < 0) {
1622 if (cow_start != (u64)-1)
1623 cur_offset = cow_start;
1624 goto error;
1625 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001626 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001627 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001628 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001629 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001630 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001631 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001632 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001633 extent_end = found_key.offset + ram_bytes;
1634 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001635 /* Skip extents outside of our requested range */
1636 if (extent_end <= start) {
1637 path->slots[0]++;
1638 goto next_slot;
1639 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001640 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001641 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001642 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001643 }
1644out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001645 /*
1646 * If nocow is false then record the beginning of the range
1647 * that needs to be COWed
1648 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001649 if (!nocow) {
1650 if (cow_start == (u64)-1)
1651 cow_start = cur_offset;
1652 cur_offset = extent_end;
1653 if (cur_offset > end)
1654 break;
1655 path->slots[0]++;
1656 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001657 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001658
David Sterbab3b4aa72011-04-21 01:20:15 +02001659 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001660
1661 /*
1662 * COW range from cow_start to found_key.offset - 1. As the key
1663 * will contain the beginning of the first extent that can be
1664 * NOCOW, following one which needs to be COW'ed
1665 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001666 if (cow_start != (u64)-1) {
Filipe Manana467dc472020-05-27 11:16:07 +01001667 ret = fallback_to_cow(inode, locked_page, cow_start,
1668 found_key.offset - 1,
1669 page_started, nr_written);
Wang Shilonge9894fd2014-03-27 11:12:25 +08001670 if (ret) {
Filipe Mananaf78c4362016-05-09 13:15:41 +01001671 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001672 btrfs_dec_nocow_writers(fs_info,
Filipe Mananaf78c4362016-05-09 13:15:41 +01001673 disk_bytenr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001674 goto error;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001675 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001676 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001677 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001678
Yan Zhengd899e052008-10-30 14:25:28 -04001679 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001680 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001681 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001682
1683 em = create_io_em(inode, cur_offset, num_bytes,
1684 orig_start,
1685 disk_bytenr, /* block_start */
1686 num_bytes, /* block_len */
1687 disk_num_bytes, /* orig_block_len */
1688 ram_bytes, BTRFS_COMPRESS_NONE,
1689 BTRFS_ORDERED_PREALLOC);
1690 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001691 if (nocow)
1692 btrfs_dec_nocow_writers(fs_info,
1693 disk_bytenr);
1694 ret = PTR_ERR(em);
1695 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001696 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001697 free_extent_map(em);
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001698 ret = btrfs_add_ordered_extent(inode, cur_offset,
1699 disk_bytenr, num_bytes,
1700 num_bytes,
1701 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001702 if (ret) {
1703 btrfs_drop_extent_cache(BTRFS_I(inode),
1704 cur_offset,
1705 cur_offset + num_bytes - 1,
1706 0);
1707 goto error;
1708 }
Yan Zhengd899e052008-10-30 14:25:28 -04001709 } else {
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001710 ret = btrfs_add_ordered_extent(inode, cur_offset,
1711 disk_bytenr, num_bytes,
1712 num_bytes,
1713 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001714 if (ret)
1715 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001716 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001717
Filipe Mananaf78c4362016-05-09 13:15:41 +01001718 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001719 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001720 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001721
Yan, Zhengefa56462010-05-16 10:49:59 -04001722 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001723 BTRFS_DATA_RELOC_TREE_OBJECTID)
1724 /*
1725 * Error handled later, as we must prevent
1726 * extent_clear_unlock_delalloc() in error handler
1727 * from freeing metadata of created ordered extent.
1728 */
Yan, Zhengefa56462010-05-16 10:49:59 -04001729 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1730 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001731
Josef Bacikc2790a22013-07-29 11:20:47 -04001732 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001733 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001734 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001735 EXTENT_DELALLOC |
1736 EXTENT_CLEAR_DATA_RESV,
1737 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1738
Yan Zheng80ff3852008-10-30 14:20:02 -04001739 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001740
1741 /*
1742 * btrfs_reloc_clone_csums() error, now we're OK to call error
1743 * handler, as metadata for created ordered extent will only
1744 * be freed by btrfs_finish_ordered_io().
1745 */
1746 if (ret)
1747 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001748 if (cur_offset > end)
1749 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001750 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001751 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001752
Robbie Ko506481b2018-10-30 18:04:04 +08001753 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001754 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001755
Yan Zheng80ff3852008-10-30 14:20:02 -04001756 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001757 cur_offset = end;
Filipe Manana467dc472020-05-27 11:16:07 +01001758 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1759 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001760 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001761 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001762 }
1763
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001764error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001765 if (nocow)
1766 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1767
Josef Bacik17ca04a2012-05-31 15:58:55 -04001768 if (ret && cur_offset < end)
Nikolay Borisov74e91942019-07-17 16:18:16 +03001769 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001770 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001771 EXTENT_DELALLOC | EXTENT_DEFRAG |
1772 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1773 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001774 PAGE_SET_WRITEBACK |
1775 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001776 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001777 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001778}
1779
Wang Shilong47059d92014-07-03 18:22:07 +08001780static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1781{
1782
1783 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1784 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1785 return 0;
1786
1787 /*
1788 * @defrag_bytes is a hint value, no spinlock held here,
1789 * if is not zero, it means the file is defragging.
1790 * Force cow if given extent needs to be defragged.
1791 */
1792 if (BTRFS_I(inode)->defrag_bytes &&
1793 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1794 EXTENT_DEFRAG, 0, NULL))
1795 return 1;
1796
1797 return 0;
1798}
1799
Chris Masond352ac62008-09-29 15:18:18 -04001800/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001801 * Function to process delayed allocation (create CoW) for ranges which are
1802 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001803 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001804int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001805 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1806 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001807{
Chris Masonbe20aa92007-12-17 20:14:01 -05001808 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001809 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001810
Wang Shilong47059d92014-07-03 18:22:07 +08001811 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001812 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001813 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001814 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001815 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001816 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001817 } else if (!inode_can_compress(inode) ||
1818 !inode_need_compress(inode, start, end)) {
Nikolay Borisov74e91942019-07-17 16:18:16 +03001819 ret = cow_file_range(inode, locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001820 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001821 } else {
1822 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1823 &BTRFS_I(inode)->runtime_flags);
Chris Masonec39f762019-07-10 12:28:17 -07001824 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001825 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001826 }
Qu Wenruo524272602017-03-08 10:25:52 +08001827 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001828 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1829 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001830 return ret;
1831}
1832
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001833void btrfs_split_delalloc_extent(struct inode *inode,
1834 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001835{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001836 u64 size;
1837
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001838 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001839 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001840 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001841
Josef Bacikdcab6a32015-02-11 15:08:59 -05001842 size = orig->end - orig->start + 1;
1843 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001844 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001845 u64 new_size;
1846
1847 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001848 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001849 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001850 */
1851 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001852 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001853 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001854 num_extents += count_max_extents(new_size);
1855 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001856 return;
1857 }
1858
Josef Bacik9e0baf62011-07-15 15:16:44 +00001859 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001860 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001861 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001862}
1863
1864/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001865 * Handle merged delayed allocation extents so we can keep track of new extents
1866 * that are just merged onto old extents, such as when we are doing sequential
1867 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001868 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001869void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1870 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001871{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001872 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001873 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001874
Josef Bacik9ed74f22009-09-11 16:12:44 -04001875 /* not delalloc, ignore it */
1876 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001877 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001878
Josef Bacik8461a3d2015-03-13 15:12:08 -04001879 if (new->start > other->start)
1880 new_size = new->end - other->start + 1;
1881 else
1882 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001883
1884 /* we're not bigger than the max, unreserve the space and go */
1885 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1886 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001887 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001888 spin_unlock(&BTRFS_I(inode)->lock);
1889 return;
1890 }
1891
1892 /*
Josef Bacikba117212015-03-13 15:01:24 -04001893 * We have to add up either side to figure out how many extents were
1894 * accounted for before we merged into one big extent. If the number of
1895 * extents we accounted for is <= the amount we need for the new range
1896 * then we can return, otherwise drop. Think of it like this
1897 *
1898 * [ 4k][MAX_SIZE]
1899 *
1900 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1901 * need 2 outstanding extents, on one side we have 1 and the other side
1902 * we have 1 so they are == and we can return. But in this case
1903 *
1904 * [MAX_SIZE+4k][MAX_SIZE+4k]
1905 *
1906 * Each range on their own accounts for 2 extents, but merged together
1907 * they are only 3 extents worth of accounting, so we need to drop in
1908 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001909 */
Josef Bacikba117212015-03-13 15:01:24 -04001910 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001911 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001912 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001913 num_extents += count_max_extents(old_size);
1914 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001915 return;
1916
Josef Bacik9e0baf62011-07-15 15:16:44 +00001917 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001918 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001919 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001920}
1921
Miao Xieeb73c1b2013-05-15 07:48:22 +00001922static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1923 struct inode *inode)
1924{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001925 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1926
Miao Xieeb73c1b2013-05-15 07:48:22 +00001927 spin_lock(&root->delalloc_lock);
1928 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1929 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1930 &root->delalloc_inodes);
1931 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1932 &BTRFS_I(inode)->runtime_flags);
1933 root->nr_delalloc_inodes++;
1934 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001935 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001936 BUG_ON(!list_empty(&root->delalloc_root));
1937 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001938 &fs_info->delalloc_roots);
1939 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001940 }
1941 }
1942 spin_unlock(&root->delalloc_lock);
1943}
1944
Nikolay Borisov2b877332018-04-27 12:21:51 +03001945
1946void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1947 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001948{
David Sterba3ffbd682018-06-29 10:56:42 +02001949 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001950
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001951 if (!list_empty(&inode->delalloc_inodes)) {
1952 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001953 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001954 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001955 root->nr_delalloc_inodes--;
1956 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001957 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001958 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001959 BUG_ON(list_empty(&root->delalloc_root));
1960 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001961 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001962 }
1963 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001964}
1965
1966static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1967 struct btrfs_inode *inode)
1968{
1969 spin_lock(&root->delalloc_lock);
1970 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001971 spin_unlock(&root->delalloc_lock);
1972}
1973
Chris Masond352ac62008-09-29 15:18:18 -04001974/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001975 * Properly track delayed allocation bytes in the inode and to maintain the
1976 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001977 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001978void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1979 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001980{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001981 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1982
Wang Shilong47059d92014-07-03 18:22:07 +08001983 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1984 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001985 /*
1986 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001987 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001988 * bit, which is only set or cleared with irqs on
1989 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001990 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05001991 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001992 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04001993 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02001994 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04001995
Josef Bacik8b62f872017-10-19 14:15:55 -04001996 spin_lock(&BTRFS_I(inode)->lock);
1997 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
1998 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00001999
Josef Bacik6a3891c2015-03-16 17:38:52 -04002000 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002001 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002002 return;
2003
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002004 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2005 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002006 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002007 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002008 if (*bits & EXTENT_DEFRAG)
2009 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002010 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002011 &BTRFS_I(inode)->runtime_flags))
2012 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002013 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002014 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002015
2016 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2017 (*bits & EXTENT_DELALLOC_NEW)) {
2018 spin_lock(&BTRFS_I(inode)->lock);
2019 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2020 state->start;
2021 spin_unlock(&BTRFS_I(inode)->lock);
2022 }
Chris Mason291d6732008-01-29 15:55:23 -05002023}
2024
Chris Masond352ac62008-09-29 15:18:18 -04002025/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002026 * Once a range is no longer delalloc this function ensures that proper
2027 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002028 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002029void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2030 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002031{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002032 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2033 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002034 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002035 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002036
Filipe Manana4a4b9642017-07-27 19:52:55 +01002037 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2038 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002039 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002040 spin_unlock(&inode->lock);
2041 }
Wang Shilong47059d92014-07-03 18:22:07 +08002042
Chris Mason75eff682008-12-15 15:54:40 -05002043 /*
2044 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002045 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002046 * bit, which is only set or cleared with irqs on
2047 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002048 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002049 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002050 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002051
Josef Bacik8b62f872017-10-19 14:15:55 -04002052 spin_lock(&inode->lock);
2053 btrfs_mod_outstanding_extents(inode, -num_extents);
2054 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002055
Josef Bacikb6d08f02013-09-27 14:57:43 -04002056 /*
2057 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002058 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002059 * error.
2060 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002061 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002062 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002063 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002064
Josef Bacik6a3891c2015-03-16 17:38:52 -04002065 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002066 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002067 return;
2068
Filipe Mananaa315e682017-03-06 23:04:20 +00002069 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2070 do_list && !(state->state & EXTENT_NORESERVE) &&
2071 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002072 btrfs_free_reserved_data_space_noquota(
2073 &inode->vfs_inode,
Qu Wenruo51773be2015-10-08 18:19:37 +08002074 state->start, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002075
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002076 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2077 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002078 spin_lock(&inode->lock);
2079 inode->delalloc_bytes -= len;
2080 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002081 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002082 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002083 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002084 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002085 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002086
2087 if ((state->state & EXTENT_DELALLOC_NEW) &&
2088 (*bits & EXTENT_DELALLOC_NEW)) {
2089 spin_lock(&inode->lock);
2090 ASSERT(inode->new_delalloc_bytes >= len);
2091 inode->new_delalloc_bytes -= len;
2092 spin_unlock(&inode->lock);
2093 }
Chris Mason291d6732008-01-29 15:55:23 -05002094}
2095
Chris Masond352ac62008-09-29 15:18:18 -04002096/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002097 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2098 * in a chunk's stripe. This function ensures that bios do not span a
2099 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002100 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002101 * @page - The page we are about to add to the bio
2102 * @size - size we want to add to the bio
2103 * @bio - bio we want to ensure is smaller than a stripe
2104 * @bio_flags - flags of the bio
2105 *
2106 * return 1 if page cannot be added to the bio
2107 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002108 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002109 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002110int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2111 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002112{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002113 struct inode *inode = page->mapping->host;
2114 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002115 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002116 u64 length = 0;
2117 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002118 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002119 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002120
Chris Mason771ed682008-11-06 22:02:51 -05002121 if (bio_flags & EXTENT_BIO_COMPRESSED)
2122 return 0;
2123
Kent Overstreet4f024f32013-10-11 15:44:27 -07002124 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002125 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002126 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2127 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002128 if (ret < 0)
2129 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002130
2131 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002132 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002133 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002134}
2135
Chris Masond352ac62008-09-29 15:18:18 -04002136/*
2137 * in order to insert checksums into the metadata in large chunks,
2138 * we wait until bio submission time. All the pages in the bio are
2139 * checksummed and sums are attached onto the ordered extent record.
2140 *
2141 * At IO completion time the cums attached on the ordered extent record
2142 * are inserted into the btree
2143 */
David Sterbad0ee3932018-03-08 14:35:48 +01002144static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002145 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002146{
Josef Bacikc6100a42017-05-05 11:57:13 -04002147 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002148 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002149
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002150 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002151 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002152 return 0;
2153}
Chris Masone0156402008-04-16 11:15:20 -04002154
Chris Mason4a69a412008-11-06 22:03:00 -05002155/*
Chris Masoncad321a2008-12-17 14:51:42 -05002156 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002157 * on write, or reading the csums from the tree before a read.
2158 *
2159 * Rules about async/sync submit,
2160 * a) read: sync submit
2161 *
2162 * b) write without checksum: sync submit
2163 *
2164 * c) write with checksum:
2165 * c-1) if bio is issued by fsync: sync submit
2166 * (sync_writers != 0)
2167 *
2168 * c-2) if root is reloc root: sync submit
2169 * (only in case of buffered IO)
2170 *
2171 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002172 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002173static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002174 int mirror_num,
2175 unsigned long bio_flags)
2176
Chris Mason44b8bd72008-04-16 11:14:51 -04002177{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002178 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002179 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302180 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002181 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002182 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002183 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002184
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002185 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002186
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002187 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302188 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002189
Mike Christie37226b22016-06-05 14:31:52 -05002190 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002191 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002192 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002193 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002194
Chris Masond20f7042008-12-08 16:58:54 -05002195 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002196 ret = btrfs_submit_compressed_read(inode, bio,
2197 mirror_num,
2198 bio_flags);
2199 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002200 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002201 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002202 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002203 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002204 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002205 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002206 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002207 /* csum items have already been cloned */
2208 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2209 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002210 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002211 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002212 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002213 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002214 } else if (!skip_sum) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002215 ret = btrfs_csum_one_bio(inode, bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002216 if (ret)
2217 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002218 }
2219
Chris Mason0b86a832008-03-24 15:01:56 -04002220mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002221 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002222
2223out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002224 if (ret) {
2225 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002226 bio_endio(bio);
2227 }
Stefan Behrens61891922012-11-05 18:51:52 +01002228 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002229}
Chris Mason6885f302008-02-20 16:11:05 -05002230
Chris Masond352ac62008-09-29 15:18:18 -04002231/*
2232 * given a list of ordered sums record them in the inode. This happens
2233 * at IO completion time based on sums calculated at bio submission time.
2234 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002235static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002236 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002237{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002238 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002239 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002240
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002241 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002242 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002243 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002244 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002245 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002246 if (ret)
2247 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002248 }
2249 return 0;
2250}
2251
Josef Bacik2ac55d42010-02-03 19:33:23 +00002252int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002253 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002254 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002255{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002256 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002257 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002258 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002259}
2260
Chris Masond352ac62008-09-29 15:18:18 -04002261/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002262struct btrfs_writepage_fixup {
2263 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002264 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002265 struct btrfs_work work;
2266};
2267
Christoph Hellwigb2950862008-12-02 09:54:17 -05002268static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002269{
2270 struct btrfs_writepage_fixup *fixup;
2271 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002272 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002273 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002274 struct page *page;
2275 struct inode *inode;
2276 u64 page_start;
2277 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002278 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002279 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002280
2281 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2282 page = fixup->page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002283 inode = fixup->inode;
2284 page_start = page_offset(page);
2285 page_end = page_offset(page) + PAGE_SIZE - 1;
2286
2287 /*
2288 * This is similar to page_mkwrite, we need to reserve the space before
2289 * we take the page lock.
2290 */
2291 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2292 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002293again:
Chris Mason247e7432008-07-17 12:53:51 -04002294 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002295
Chris Mason25f3c502020-01-21 11:51:42 -05002296 /*
2297 * Before we queued this fixup, we took a reference on the page.
2298 * page->mapping may go NULL, but it shouldn't be moved to a different
2299 * address space.
2300 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002301 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2302 /*
2303 * Unfortunately this is a little tricky, either
2304 *
2305 * 1) We got here and our page had already been dealt with and
2306 * we reserved our space, thus ret == 0, so we need to just
2307 * drop our space reservation and bail. This can happen the
2308 * first time we come into the fixup worker, or could happen
2309 * while waiting for the ordered extent.
2310 * 2) Our page was already dealt with, but we happened to get an
2311 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2312 * this case we obviously don't have anything to release, but
2313 * because the page was already dealt with we don't want to
2314 * mark the page with an error, so make sure we're resetting
2315 * ret to 0. This is why we have this check _before_ the ret
2316 * check, because we do not want to have a surprise ENOSPC
2317 * when the page was already properly dealt with.
2318 */
2319 if (!ret) {
2320 btrfs_delalloc_release_extents(BTRFS_I(inode),
2321 PAGE_SIZE);
2322 btrfs_delalloc_release_space(inode, data_reserved,
2323 page_start, PAGE_SIZE,
2324 true);
2325 }
2326 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002327 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002328 }
Chris Mason25f3c502020-01-21 11:51:42 -05002329
2330 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002331 * We can't mess with the page state unless it is locked, so now that
2332 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002333 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002334 if (ret)
2335 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002336
David Sterbaff13db42015-12-03 14:30:40 +01002337 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002338 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002339
2340 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002341 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002342 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002343
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002344 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002345 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002346 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002347 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002348 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002349 unlock_page(page);
2350 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002351 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002352 goto again;
2353 }
Chris Mason247e7432008-07-17 12:53:51 -04002354
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002355 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002356 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002357 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002358 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002359
Chris Mason25f3c502020-01-21 11:51:42 -05002360 /*
2361 * Everything went as planned, we're now the owner of a dirty page with
2362 * delayed allocation bits set and space reserved for our COW
2363 * destination.
2364 *
2365 * The page was dirty when we started, nothing should have cleaned it.
2366 */
2367 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002368 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002369out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002370 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002371 if (free_delalloc_space)
Filipe Manana53687002019-10-09 17:43:59 +01002372 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2373 PAGE_SIZE, true);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002374 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002375 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002376out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002377 if (ret) {
2378 /*
2379 * We hit ENOSPC or other errors. Update the mapping and page
2380 * to reflect the errors and clean the page.
2381 */
2382 mapping_set_error(page->mapping, ret);
2383 end_extent_writepage(page, ret, page_start, page_end);
2384 clear_page_dirty_for_io(page);
2385 SetPageError(page);
2386 }
2387 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002388 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002389 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002390 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002391 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002392 /*
2393 * As a precaution, do a delayed iput in case it would be the last iput
2394 * that could need flushing space. Recursing back to fixup worker would
2395 * deadlock.
2396 */
2397 btrfs_add_delayed_iput(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002398}
2399
2400/*
2401 * There are a few paths in the higher layers of the kernel that directly
2402 * set the page dirty bit without asking the filesystem if it is a
2403 * good idea. This causes problems because we want to make sure COW
2404 * properly happens and the data=ordered rules are followed.
2405 *
Chris Masonc8b97812008-10-29 14:49:59 -04002406 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002407 * hasn't been properly setup for IO. We kick off an async process
2408 * to fix it up. The async helper will wait for ordered extents, set
2409 * the delalloc bit and make it safe to write the page.
2410 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002411int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002412{
2413 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002414 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002415 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002416
Chris Mason8b62b722009-09-02 16:53:46 -04002417 /* this page is properly in the ordered list */
2418 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002419 return 0;
2420
Chris Mason25f3c502020-01-21 11:51:42 -05002421 /*
2422 * PageChecked is set below when we create a fixup worker for this page,
2423 * don't try to create another one if we're already PageChecked()
2424 *
2425 * The extent_io writepage code will redirty the page if we send back
2426 * EAGAIN.
2427 */
Chris Mason247e7432008-07-17 12:53:51 -04002428 if (PageChecked(page))
2429 return -EAGAIN;
2430
2431 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2432 if (!fixup)
2433 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002434
Josef Bacikf4b13632020-01-21 14:34:52 -05002435 /*
2436 * We are already holding a reference to this inode from
2437 * write_cache_pages. We need to hold it because the space reservation
2438 * takes place outside of the page lock, and we can't trust
2439 * page->mapping outside of the page lock.
2440 */
2441 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002442 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002443 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002444 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002445 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002446 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002447 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002448
2449 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002450}
2451
Yan Zhengd899e052008-10-30 14:25:28 -04002452static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2453 struct inode *inode, u64 file_pos,
2454 u64 disk_bytenr, u64 disk_num_bytes,
2455 u64 num_bytes, u64 ram_bytes,
2456 u8 compression, u8 encryption,
2457 u16 other_encoding, int extent_type)
2458{
2459 struct btrfs_root *root = BTRFS_I(inode)->root;
2460 struct btrfs_file_extent_item *fi;
2461 struct btrfs_path *path;
2462 struct extent_buffer *leaf;
2463 struct btrfs_key ins;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002464 u64 qg_released;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002465 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002466 int ret;
2467
2468 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002469 if (!path)
2470 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002471
Chris Masona1ed8352009-09-11 12:27:37 -04002472 /*
2473 * we may be replacing one extent in the tree with another.
2474 * The new extent is pinned in the extent map, and we don't want
2475 * to drop it from the cache until it is completely in the btree.
2476 *
2477 * So, tell btrfs_drop_extents to leave this extent in the cache.
2478 * the caller is expected to unpin it and allow it to be merged
2479 * with the others.
2480 */
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002481 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2482 file_pos + num_bytes, NULL, 0,
2483 1, sizeof(*fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002484 if (ret)
2485 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002486
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002487 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002488 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002489 ins.offset = file_pos;
2490 ins.type = BTRFS_EXTENT_DATA_KEY;
2491
2492 path->leave_spinning = 1;
2493 ret = btrfs_insert_empty_item(trans, root, path, &ins,
2494 sizeof(*fi));
2495 if (ret)
2496 goto out;
2497 }
Yan Zhengd899e052008-10-30 14:25:28 -04002498 leaf = path->nodes[0];
2499 fi = btrfs_item_ptr(leaf, path->slots[0],
2500 struct btrfs_file_extent_item);
2501 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2502 btrfs_set_file_extent_type(leaf, fi, extent_type);
2503 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2504 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2505 btrfs_set_file_extent_offset(leaf, fi, 0);
2506 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2507 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2508 btrfs_set_file_extent_compression(leaf, fi, compression);
2509 btrfs_set_file_extent_encryption(leaf, fi, encryption);
2510 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
Chris Masonb9473432009-03-13 11:00:37 -04002511
Yan Zhengd899e052008-10-30 14:25:28 -04002512 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002513 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002514
2515 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002516
2517 ins.objectid = disk_bytenr;
2518 ins.offset = disk_num_bytes;
2519 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002520
Josef Bacik9ddc9592020-01-17 09:02:22 -05002521 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), file_pos,
2522 ram_bytes);
2523 if (ret)
2524 goto out;
2525
Qu Wenruo297d7502015-09-08 17:08:37 +08002526 /*
Qu Wenruo5846a3c2015-10-26 14:11:18 +08002527 * Release the reserved range from inode dirty range map, as it is
2528 * already moved into delayed_ref_head
Qu Wenruo297d7502015-09-08 17:08:37 +08002529 */
Qu Wenruoa12b8772017-02-27 15:10:37 +08002530 ret = btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2531 if (ret < 0)
2532 goto out;
2533 qg_released = ret;
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002534 ret = btrfs_alloc_reserved_file_extent(trans, root,
2535 btrfs_ino(BTRFS_I(inode)),
2536 file_pos, qg_released, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002537out:
Yan Zhengd899e052008-10-30 14:25:28 -04002538 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002539
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002540 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002541}
2542
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002543static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002544 u64 start, u64 len)
2545{
David Sterba32da53862019-10-29 19:20:18 +01002546 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002547
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002548 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002549 ASSERT(cache);
2550
2551 spin_lock(&cache->lock);
2552 cache->delalloc_bytes -= len;
2553 spin_unlock(&cache->lock);
2554
2555 btrfs_put_block_group(cache);
2556}
2557
Chris Masond352ac62008-09-29 15:18:18 -04002558/* as ordered data IO finishes, this gets called so we can finish
2559 * an ordered extent if the range of bytes in the file it covers are
2560 * fully written.
2561 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002562static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002563{
Josef Bacik5fd02042012-05-02 14:00:54 -04002564 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002565 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002566 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002567 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002568 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002569 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002570 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002571 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002572 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002573 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002574 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002575 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002576 bool range_locked = false;
2577 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002578 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002579 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002580
Omar Sandovalbffe6332019-12-02 17:34:19 -08002581 start = ordered_extent->file_offset;
2582 end = start + ordered_extent->num_bytes - 1;
2583
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002584 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2585 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2586 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2587 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002588
Nikolay Borisov8d510122019-10-08 20:43:06 +03002589 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002590
Josef Bacik5fd02042012-05-02 14:00:54 -04002591 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2592 ret = -EIO;
2593 goto out;
2594 }
2595
Omar Sandovalbffe6332019-12-02 17:34:19 -08002596 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002597
Josef Bacik77cef2e2013-08-29 13:57:21 -04002598 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2599 truncated = true;
2600 logical_len = ordered_extent->truncated_len;
2601 /* Truncated the entire extent, don't bother adding */
2602 if (!logical_len)
2603 goto out;
2604 }
2605
Yan, Zhengc2167752009-11-12 09:34:21 +00002606 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002607 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002608
2609 /*
2610 * For mwrite(mmap + memset to write) case, we still reserve
2611 * space for NOCOW range.
2612 * As NOCOW won't cause a new delayed ref, just free the space
2613 */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002614 btrfs_qgroup_free_data(inode, NULL, start,
2615 ordered_extent->num_bytes);
Josef Bacikd923afe2020-01-17 09:02:23 -05002616 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002617 if (freespace_inode)
2618 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002619 else
2620 trans = btrfs_join_transaction(root);
2621 if (IS_ERR(trans)) {
2622 ret = PTR_ERR(trans);
2623 trans = NULL;
2624 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002625 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002626 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002627 ret = btrfs_update_inode_fallback(trans, root, inode);
2628 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002629 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002630 goto out;
2631 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002632
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002633 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002634 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002635
Nikolay Borisov8d510122019-10-08 20:43:06 +03002636 if (freespace_inode)
2637 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002638 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002639 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002640 if (IS_ERR(trans)) {
2641 ret = PTR_ERR(trans);
2642 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002643 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002644 }
Chris Masona79b7d42014-05-22 16:18:52 -07002645
Josef Bacik69fe2d72017-10-19 14:15:57 -04002646 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002647
Chris Masonc8b97812008-10-29 14:49:59 -04002648 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002649 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002650 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002651 BUG_ON(compress_type);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002652 btrfs_qgroup_free_data(inode, NULL, start,
2653 ordered_extent->num_bytes);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002654 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002655 ordered_extent->file_offset,
2656 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002657 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002658 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002659 BUG_ON(root == fs_info->tree_root);
Omar Sandovalbffe6332019-12-02 17:34:19 -08002660 ret = insert_reserved_file_extent(trans, inode, start,
2661 ordered_extent->disk_bytenr,
2662 ordered_extent->disk_num_bytes,
Josef Bacik77cef2e2013-08-29 13:57:21 -04002663 logical_len, logical_len,
Li Zefan261507a02010-12-17 14:21:50 +08002664 compress_type, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04002665 BTRFS_FILE_EXTENT_REG);
Josef Bacik49940bd2018-10-11 15:54:21 -04002666 if (!ret) {
2667 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002668 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002669 ordered_extent->disk_bytenr,
2670 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002671 }
Yan Zhengd899e052008-10-30 14:25:28 -04002672 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002673 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002674 ordered_extent->file_offset,
2675 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002676 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002677 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002678 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002679 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002680
Nikolay Borisovac01f262018-01-08 10:59:43 +02002681 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2682 if (ret) {
2683 btrfs_abort_transaction(trans, ret);
2684 goto out;
2685 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002686
Josef Bacikd923afe2020-01-17 09:02:23 -05002687 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002688 ret = btrfs_update_inode_fallback(trans, root, inode);
2689 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002690 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002691 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002692 }
2693 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002694out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002695 clear_bits = EXTENT_DEFRAG;
2696 if (range_locked)
2697 clear_bits |= EXTENT_LOCKED;
2698 if (clear_new_delalloc_bytes)
2699 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002700 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2701 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002702 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002703
Miao Xiea698d0752012-09-20 01:51:59 -06002704 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002705 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002706
Josef Bacik77cef2e2013-08-29 13:57:21 -04002707 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002708 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002709
2710 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002711 unwritten_start += logical_len;
2712 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002713
2714 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002715 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002716
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002717 /*
2718 * If the ordered extent had an IOERR or something else went
2719 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002720 * back to the allocator. We only free the extent in the
2721 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002722 *
2723 * If we made it past insert_reserved_file_extent before we
2724 * errored out then we don't need to do this as the accounting
2725 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002726 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002727 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002728 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002729 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002730 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2731 /*
2732 * Discard the range before returning it back to the
2733 * free space pool
2734 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002735 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002736 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002737 ordered_extent->disk_bytenr,
2738 ordered_extent->disk_num_bytes,
2739 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002740 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002741 ordered_extent->disk_bytenr,
2742 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002743 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002744 }
2745
Josef Bacik5fd02042012-05-02 14:00:54 -04002746 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002747 * This needs to be done to make sure anybody waiting knows we are done
2748 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002749 */
2750 btrfs_remove_ordered_extent(inode, ordered_extent);
2751
Chris Masone6dcd2d2008-07-17 12:53:50 -04002752 /* once for us */
2753 btrfs_put_ordered_extent(ordered_extent);
2754 /* once for the tree */
2755 btrfs_put_ordered_extent(ordered_extent);
2756
Josef Bacik5fd02042012-05-02 14:00:54 -04002757 return ret;
2758}
2759
2760static void finish_ordered_fn(struct btrfs_work *work)
2761{
2762 struct btrfs_ordered_extent *ordered_extent;
2763 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2764 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002765}
2766
Nikolay Borisovc6297322018-11-08 10:18:08 +02002767void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2768 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002769{
Josef Bacik5fd02042012-05-02 14:00:54 -04002770 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002771 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002772 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002773 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002774
liubo1abe9b82011-03-24 11:18:59 +00002775 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2776
Chris Mason8b62b722009-09-02 16:53:46 -04002777 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002778 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2779 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002780 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002781
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002782 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002783 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002784 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002785 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002786
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002787 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002788 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002789}
2790
Omar Sandoval47df7762020-04-16 14:46:17 -07002791static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2792 int icsum, struct page *page, int pgoff, u64 start,
2793 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002794{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002795 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2796 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002797 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002798 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2799 u8 *csum_expected;
2800 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002801
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002802 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002803
2804 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002805 shash->tfm = fs_info->csum_shash;
2806
Eric Biggersfd080012020-04-30 23:51:59 -07002807 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002808
2809 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002810 goto zeroit;
2811
2812 kunmap_atomic(kaddr);
2813 return 0;
2814zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002815 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2816 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002817 memset(kaddr + pgoff, 1, len);
2818 flush_dcache_page(page);
2819 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002820 return -EIO;
2821}
2822
Chris Masond352ac62008-09-29 15:18:18 -04002823/*
Chris Masond352ac62008-09-29 15:18:18 -04002824 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002825 * if there's a match, we allow the bio to finish. If not, the code in
2826 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002827 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002828static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2829 u64 phy_offset, struct page *page,
2830 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002831{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002832 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002833 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002834 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002835 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002836
Chris Masond20f7042008-12-08 16:58:54 -05002837 if (PageChecked(page)) {
2838 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002839 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002840 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002841
2842 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002843 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002844
Yan Zheng17d217f2008-12-12 10:03:38 -05002845 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002846 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002847 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002848 return 0;
2849 }
2850
Miao Xiefacc8a222013-07-25 19:22:34 +08002851 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002852 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2853 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002854}
Chris Masonb888db22007-08-27 16:49:44 -04002855
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002856/*
2857 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2858 *
2859 * @inode: The inode we want to perform iput on
2860 *
2861 * This function uses the generic vfs_inode::i_count to track whether we should
2862 * just decrement it (in case it's > 1) or if this is the last iput then link
2863 * the inode to the delayed iput machinery. Delayed iputs are processed at
2864 * transaction commit time/superblock commit/cleaner kthread.
2865 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002866void btrfs_add_delayed_iput(struct inode *inode)
2867{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002868 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002869 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002870
2871 if (atomic_add_unless(&inode->i_count, -1, 1))
2872 return;
2873
Josef Bacik034f7842018-12-03 11:06:52 -05002874 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002875 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002876 ASSERT(list_empty(&binode->delayed_iput));
2877 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002878 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002879 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2880 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002881}
2882
Josef Bacik63611e72019-06-18 10:59:18 -04002883static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2884 struct btrfs_inode *inode)
2885{
2886 list_del_init(&inode->delayed_iput);
2887 spin_unlock(&fs_info->delayed_iput_lock);
2888 iput(&inode->vfs_inode);
2889 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2890 wake_up(&fs_info->delayed_iputs_wait);
2891 spin_lock(&fs_info->delayed_iput_lock);
2892}
2893
2894static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2895 struct btrfs_inode *inode)
2896{
2897 if (!list_empty(&inode->delayed_iput)) {
2898 spin_lock(&fs_info->delayed_iput_lock);
2899 if (!list_empty(&inode->delayed_iput))
2900 run_delayed_iput_locked(fs_info, inode);
2901 spin_unlock(&fs_info->delayed_iput_lock);
2902 }
2903}
2904
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002905void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002906{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002907
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002908 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002909 while (!list_empty(&fs_info->delayed_iputs)) {
2910 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002911
David Sterba8089fe62015-11-19 14:15:51 +01002912 inode = list_first_entry(&fs_info->delayed_iputs,
2913 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002914 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002915 }
David Sterba8089fe62015-11-19 14:15:51 +01002916 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002917}
2918
Josef Bacik034f7842018-12-03 11:06:52 -05002919/**
2920 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2921 * @fs_info - the fs_info for this fs
2922 * @return - EINTR if we were killed, 0 if nothing's pending
2923 *
2924 * This will wait on any delayed iputs that are currently running with KILLABLE
2925 * set. Once they are all done running we will return, unless we are killed in
2926 * which case we return EINTR. This helps in user operations like fallocate etc
2927 * that might get blocked on the iputs.
2928 */
2929int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2930{
2931 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2932 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2933 if (ret)
2934 return -EINTR;
2935 return 0;
2936}
2937
Yan, Zhengd68fc572010-05-16 10:49:58 -04002938/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002939 * This creates an orphan entry for the given inode in case something goes wrong
2940 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002941 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002942int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002943 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002944{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002945 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002946
Omar Sandoval27919062018-05-11 13:13:37 -07002947 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2948 if (ret && ret != -EEXIST) {
2949 btrfs_abort_transaction(trans, ret);
2950 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002951 }
2952
Yan, Zhengd68fc572010-05-16 10:49:58 -04002953 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002954}
2955
2956/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002957 * We have done the delete so we can go ahead and remove the orphan item for
2958 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002959 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002960static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002961 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002962{
Omar Sandoval27919062018-05-11 13:13:37 -07002963 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002964}
2965
2966/*
2967 * this cleans up any orphans that may be left on the list from the last use
2968 * of this root.
2969 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002970int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002971{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002972 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002973 struct btrfs_path *path;
2974 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002975 struct btrfs_key key, found_key;
2976 struct btrfs_trans_handle *trans;
2977 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002978 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002979 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002980
Yan, Zhengd68fc572010-05-16 10:49:58 -04002981 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002982 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002983
2984 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002985 if (!path) {
2986 ret = -ENOMEM;
2987 goto out;
2988 }
David Sterbae4058b52015-11-27 16:31:35 +01002989 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002990
2991 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002992 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04002993 key.offset = (u64)-1;
2994
Josef Bacik7b128762008-07-24 12:17:14 -04002995 while (1) {
2996 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002997 if (ret < 0)
2998 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04002999
3000 /*
3001 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003002 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003003 * find the key and see if we have stuff that matches
3004 */
3005 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003006 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003007 if (path->slots[0] == 0)
3008 break;
3009 path->slots[0]--;
3010 }
3011
3012 /* pull out the item */
3013 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003014 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3015
3016 /* make sure the item matches what we want */
3017 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3018 break;
David Sterba962a2982014-06-04 18:41:45 +02003019 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003020 break;
3021
3022 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003023 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003024
3025 /*
3026 * this is where we are basically btrfs_lookup, without the
3027 * crossing root thing. we store the inode number in the
3028 * offset of the orphan item.
3029 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003030
3031 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003032 btrfs_err(fs_info,
3033 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003034 ret = -EINVAL;
3035 goto out;
3036 }
3037
3038 last_objectid = found_key.offset;
3039
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003040 found_key.objectid = found_key.offset;
3041 found_key.type = BTRFS_INODE_ITEM_KEY;
3042 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003043 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303044 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003045 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003046 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003047
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003048 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003049 struct btrfs_root *dead_root;
3050 struct btrfs_fs_info *fs_info = root->fs_info;
3051 int is_dead_root = 0;
3052
3053 /*
3054 * this is an orphan in the tree root. Currently these
3055 * could come from 2 sources:
3056 * a) a snapshot deletion in progress
3057 * b) a free space cache inode
3058 * We need to distinguish those two, as the snapshot
3059 * orphan must not get deleted.
3060 * find_dead_roots already ran before us, so if this
3061 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003062 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003063 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003064
3065 spin_lock(&fs_info->fs_roots_radix_lock);
3066 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3067 (unsigned long)found_key.objectid);
3068 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3069 is_dead_root = 1;
3070 spin_unlock(&fs_info->fs_roots_radix_lock);
3071
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003072 if (is_dead_root) {
3073 /* prevent this orphan from being found again */
3074 key.offset = found_key.objectid - 1;
3075 continue;
3076 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003077
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003078 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003079
Josef Bacika8c9e572011-09-21 16:55:59 -04003080 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003081 * If we have an inode with links, there are a couple of
3082 * possibilities. Old kernels (before v3.12) used to create an
3083 * orphan item for truncate indicating that there were possibly
3084 * extent items past i_size that needed to be deleted. In v3.12,
3085 * truncate was changed to update i_size in sync with the extent
3086 * items, but the (useless) orphan item was still created. Since
3087 * v4.18, we don't create the orphan item for truncate at all.
3088 *
3089 * So, this item could mean that we need to do a truncate, but
3090 * only if this filesystem was last used on a pre-v3.12 kernel
3091 * and was not cleanly unmounted. The odds of that are quite
3092 * slim, and it's a pain to do the truncate now, so just delete
3093 * the orphan item.
3094 *
3095 * It's also possible that this orphan item was supposed to be
3096 * deleted but wasn't. The inode number may have been reused,
3097 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003098 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003099 if (ret == -ENOENT || inode->i_nlink) {
3100 if (!ret)
3101 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003102 trans = btrfs_start_transaction(root, 1);
3103 if (IS_ERR(trans)) {
3104 ret = PTR_ERR(trans);
3105 goto out;
3106 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003107 btrfs_debug(fs_info, "auto deleting %Lu",
3108 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003109 ret = btrfs_del_orphan_item(trans, root,
3110 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003111 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003112 if (ret)
3113 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003114 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003115 }
Josef Bacik7b128762008-07-24 12:17:14 -04003116
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003117 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003118
3119 /* this will do delete_inode and everything for us */
3120 iput(inode);
3121 }
Miao Xie3254c872011-11-10 20:45:05 -05003122 /* release the path since we're done with it */
3123 btrfs_release_path(path);
3124
Yan, Zhengd68fc572010-05-16 10:49:58 -04003125 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3126
Omar Sandovala575cee2018-05-11 13:13:38 -07003127 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003128 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003129 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003130 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003131 }
Josef Bacik7b128762008-07-24 12:17:14 -04003132
3133 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003134 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003135
3136out:
3137 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003138 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003139 btrfs_free_path(path);
3140 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003141}
3142
Chris Masond352ac62008-09-29 15:18:18 -04003143/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003144 * very simple check to peek ahead in the leaf looking for xattrs. If we
3145 * don't find any xattrs, we know there can't be any acls.
3146 *
3147 * slot is the slot the inode is in, objectid is the objectid of the inode
3148 */
3149static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003150 int slot, u64 objectid,
3151 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003152{
3153 u32 nritems = btrfs_header_nritems(leaf);
3154 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003155 static u64 xattr_access = 0;
3156 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003157 int scanned = 0;
3158
Josef Bacikf23b5a52013-06-19 10:16:26 -04003159 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003160 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3161 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3162 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3163 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003164 }
3165
Chris Mason46a53cc2009-04-27 11:47:50 -04003166 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003167 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003168 while (slot < nritems) {
3169 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3170
3171 /* we found a different objectid, there must not be acls */
3172 if (found_key.objectid != objectid)
3173 return 0;
3174
3175 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003176 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003177 if (*first_xattr_slot == -1)
3178 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003179 if (found_key.offset == xattr_access ||
3180 found_key.offset == xattr_default)
3181 return 1;
3182 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003183
3184 /*
3185 * we found a key greater than an xattr key, there can't
3186 * be any acls later on
3187 */
3188 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3189 return 0;
3190
3191 slot++;
3192 scanned++;
3193
3194 /*
3195 * it goes inode, inode backrefs, xattrs, extents,
3196 * so if there are a ton of hard links to an inode there can
3197 * be a lot of backrefs. Don't waste time searching too hard,
3198 * this is just an optimization
3199 */
3200 if (scanned >= 8)
3201 break;
3202 }
3203 /* we hit the end of the leaf before we found an xattr or
3204 * something larger than an xattr. We have to assume the inode
3205 * has acls
3206 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003207 if (*first_xattr_slot == -1)
3208 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003209 return 1;
3210}
3211
3212/*
Chris Masond352ac62008-09-29 15:18:18 -04003213 * read an inode from the btree into the in-memory inode
3214 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003215static int btrfs_read_locked_inode(struct inode *inode,
3216 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003217{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003218 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003219 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003220 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003221 struct btrfs_inode_item *inode_item;
3222 struct btrfs_root *root = BTRFS_I(inode)->root;
3223 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003224 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003225 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003226 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003227 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003228 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003229 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003230
3231 ret = btrfs_fill_inode(inode, &rdev);
3232 if (!ret)
3233 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003234
Filipe Manana4222ea72018-10-24 10:13:03 +01003235 if (!path) {
3236 path = btrfs_alloc_path();
3237 if (!path)
3238 return -ENOMEM;
3239 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003240
Chris Mason39279cc2007-06-12 06:35:45 -04003241 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003242
Chris Mason39279cc2007-06-12 06:35:45 -04003243 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003244 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003245 if (path != in_path)
3246 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003247 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003248 }
Chris Mason39279cc2007-06-12 06:35:45 -04003249
Chris Mason5f39d392007-10-15 16:14:19 -04003250 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003251
3252 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003253 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003254
Chris Mason5f39d392007-10-15 16:14:19 -04003255 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3256 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003257 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003258 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003259 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3260 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003261 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003262 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3263 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003264
David Sterbaa937b972014-12-12 17:39:12 +01003265 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3266 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003267
David Sterbaa937b972014-12-12 17:39:12 +01003268 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3269 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003270
David Sterbaa937b972014-12-12 17:39:12 +01003271 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3272 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003273
chandan r9cc97d62012-07-04 12:48:07 +05303274 BTRFS_I(inode)->i_otime.tv_sec =
3275 btrfs_timespec_sec(leaf, &inode_item->otime);
3276 BTRFS_I(inode)->i_otime.tv_nsec =
3277 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003278
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003279 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003280 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003281 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3282
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003283 inode_set_iversion_queried(inode,
3284 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003285 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003286 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003287 rdev = btrfs_inode_rdev(leaf, inode_item);
3288
Josef Bacikaec74772008-07-24 12:12:38 -04003289 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003290 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003291
3292cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003293 /*
3294 * If we were modified in the current generation and evicted from memory
3295 * and then re-read we need to do a full sync since we don't have any
3296 * idea about which extents were modified before we were evicted from
3297 * cache.
3298 *
3299 * This is required for both inode re-read from disk and delayed inode
3300 * in delayed_nodes_tree.
3301 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003302 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003303 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3304 &BTRFS_I(inode)->runtime_flags);
3305
Filipe Mananabde6c242015-07-24 00:00:19 +01003306 /*
3307 * We don't persist the id of the transaction where an unlink operation
3308 * against the inode was last made. So here we assume the inode might
3309 * have been evicted, and therefore the exact value of last_unlink_trans
3310 * lost, and set it to last_trans to avoid metadata inconsistencies
3311 * between the inode and its parent if the inode is fsync'ed and the log
3312 * replayed. For example, in the scenario:
3313 *
3314 * touch mydir/foo
3315 * ln mydir/foo mydir/bar
3316 * sync
3317 * unlink mydir/bar
3318 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3319 * xfs_io -c fsync mydir/foo
3320 * <power failure>
3321 * mount fs, triggers fsync log replay
3322 *
3323 * We must make sure that when we fsync our inode foo we also log its
3324 * parent inode, otherwise after log replay the parent still has the
3325 * dentry with the "bar" name but our inode foo has a link count of 1
3326 * and doesn't have an inode ref with the name "bar" anymore.
3327 *
3328 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003329 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003330 * transaction commits on fsync if our inode is a directory, or if our
3331 * inode is not a directory, logging its parent unnecessarily.
3332 */
3333 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3334
Miao Xie67de1172013-12-26 13:07:06 +08003335 path->slots[0]++;
3336 if (inode->i_nlink != 1 ||
3337 path->slots[0] >= btrfs_header_nritems(leaf))
3338 goto cache_acl;
3339
3340 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003341 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003342 goto cache_acl;
3343
3344 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3345 if (location.type == BTRFS_INODE_REF_KEY) {
3346 struct btrfs_inode_ref *ref;
3347
3348 ref = (struct btrfs_inode_ref *)ptr;
3349 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3350 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3351 struct btrfs_inode_extref *extref;
3352
3353 extref = (struct btrfs_inode_extref *)ptr;
3354 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3355 extref);
3356 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003357cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003358 /*
3359 * try to precache a NULL acl entry for files that don't have
3360 * any xattrs or acls
3361 */
Li Zefan33345d012011-04-20 10:31:50 +08003362 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003363 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003364 if (first_xattr_slot != -1) {
3365 path->slots[0] = first_xattr_slot;
3366 ret = btrfs_load_inode_props(inode, path);
3367 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003368 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003369 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003370 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003371 root->root_key.objectid, ret);
3372 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003373 if (path != in_path)
3374 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003375
Al Viro72c04902009-06-24 16:58:48 -04003376 if (!maybe_acls)
3377 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003378
Chris Mason39279cc2007-06-12 06:35:45 -04003379 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003380 case S_IFREG:
3381 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003382 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003383 inode->i_fop = &btrfs_file_operations;
3384 inode->i_op = &btrfs_file_inode_operations;
3385 break;
3386 case S_IFDIR:
3387 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003388 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003389 break;
3390 case S_IFLNK:
3391 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003392 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003393 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003394 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003395 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003396 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003397 init_special_inode(inode, inode->i_mode, rdev);
3398 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003399 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003400
David Sterba7b6a2212018-03-26 18:40:21 +02003401 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003402 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003403}
3404
Chris Masond352ac62008-09-29 15:18:18 -04003405/*
3406 * given a leaf and an inode, copy the inode fields into the leaf
3407 */
Chris Masone02119d2008-09-05 16:13:11 -04003408static void fill_inode_item(struct btrfs_trans_handle *trans,
3409 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003410 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003411 struct inode *inode)
3412{
Liu Bo51fab692012-12-27 09:01:21 +00003413 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003414
David Sterbac82f8232019-08-09 17:48:21 +02003415 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003416
David Sterbacc4c13d2020-04-29 02:15:56 +02003417 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3418 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3419 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3420 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3421 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003422
David Sterbacc4c13d2020-04-29 02:15:56 +02003423 btrfs_set_token_timespec_sec(&token, &item->atime,
3424 inode->i_atime.tv_sec);
3425 btrfs_set_token_timespec_nsec(&token, &item->atime,
3426 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003427
David Sterbacc4c13d2020-04-29 02:15:56 +02003428 btrfs_set_token_timespec_sec(&token, &item->mtime,
3429 inode->i_mtime.tv_sec);
3430 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3431 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003432
David Sterbacc4c13d2020-04-29 02:15:56 +02003433 btrfs_set_token_timespec_sec(&token, &item->ctime,
3434 inode->i_ctime.tv_sec);
3435 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3436 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003437
David Sterbacc4c13d2020-04-29 02:15:56 +02003438 btrfs_set_token_timespec_sec(&token, &item->otime,
3439 BTRFS_I(inode)->i_otime.tv_sec);
3440 btrfs_set_token_timespec_nsec(&token, &item->otime,
3441 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303442
David Sterbacc4c13d2020-04-29 02:15:56 +02003443 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3444 btrfs_set_token_inode_generation(&token, item,
3445 BTRFS_I(inode)->generation);
3446 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3447 btrfs_set_token_inode_transid(&token, item, trans->transid);
3448 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3449 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3450 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003451}
3452
Chris Masond352ac62008-09-29 15:18:18 -04003453/*
3454 * copy everything in the in-memory inode into the btree.
3455 */
Chris Mason21151332011-11-10 20:39:08 -05003456static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003457 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003458{
3459 struct btrfs_inode_item *inode_item;
3460 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003461 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003462 int ret;
3463
3464 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003465 if (!path)
3466 return -ENOMEM;
3467
Chris Masonb9473432009-03-13 11:00:37 -04003468 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003469 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3470 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003471 if (ret) {
3472 if (ret > 0)
3473 ret = -ENOENT;
3474 goto failed;
3475 }
3476
Chris Mason5f39d392007-10-15 16:14:19 -04003477 leaf = path->nodes[0];
3478 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003479 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003480
Chris Masone02119d2008-09-05 16:13:11 -04003481 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003482 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003483 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003484 ret = 0;
3485failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003486 btrfs_free_path(path);
3487 return ret;
3488}
3489
Chris Masond352ac62008-09-29 15:18:18 -04003490/*
Chris Mason21151332011-11-10 20:39:08 -05003491 * copy everything in the in-memory inode into the btree.
3492 */
3493noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3494 struct btrfs_root *root, struct inode *inode)
3495{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003496 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003497 int ret;
3498
3499 /*
3500 * If the inode is a free space inode, we can deadlock during commit
3501 * if we put it into the delayed code.
3502 *
3503 * The data relocation inode should also be directly updated
3504 * without delay
3505 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003506 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003507 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003508 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003509 btrfs_update_root_times(trans, root);
3510
Chris Mason21151332011-11-10 20:39:08 -05003511 ret = btrfs_delayed_update_inode(trans, root, inode);
3512 if (!ret)
3513 btrfs_set_inode_last_trans(trans, inode);
3514 return ret;
3515 }
3516
3517 return btrfs_update_inode_item(trans, root, inode);
3518}
3519
Josef Bacikbe6aef62012-10-22 15:43:12 -04003520noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3521 struct btrfs_root *root,
3522 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003523{
3524 int ret;
3525
3526 ret = btrfs_update_inode(trans, root, inode);
3527 if (ret == -ENOSPC)
3528 return btrfs_update_inode_item(trans, root, inode);
3529 return ret;
3530}
3531
3532/*
Chris Masond352ac62008-09-29 15:18:18 -04003533 * unlink helper that gets used here in inode.c and in the tree logging
3534 * recovery code. It remove a link in a directory with a given name, and
3535 * also drops the back refs in the inode to the directory
3536 */
Al Viro92986792011-03-04 17:14:37 +00003537static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3538 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003539 struct btrfs_inode *dir,
3540 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003541 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003542{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003543 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003544 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003545 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003546 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003547 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003548 u64 ino = btrfs_ino(inode);
3549 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003550
3551 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003552 if (!path) {
3553 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003554 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003555 }
3556
Chris Masonb9473432009-03-13 11:00:37 -04003557 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003558 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003559 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003560 if (IS_ERR_OR_NULL(di)) {
3561 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003562 goto err;
3563 }
Chris Mason39279cc2007-06-12 06:35:45 -04003564 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003565 if (ret)
3566 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003567 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003568
Miao Xie67de1172013-12-26 13:07:06 +08003569 /*
3570 * If we don't have dir index, we have to get it by looking up
3571 * the inode ref, since we get the inode ref, remove it directly,
3572 * it is unnecessary to do delayed deletion.
3573 *
3574 * But if we have dir index, needn't search inode ref to get it.
3575 * Since the inode ref is close to the inode item, it is better
3576 * that we delay to delete it, and just do this deletion when
3577 * we update the inode item.
3578 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003579 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003580 ret = btrfs_delayed_delete_inode_ref(inode);
3581 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003582 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003583 goto skip_backref;
3584 }
3585 }
3586
Li Zefan33345d012011-04-20 10:31:50 +08003587 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3588 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003589 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003590 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003591 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003592 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003593 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003594 goto err;
3595 }
Miao Xie67de1172013-12-26 13:07:06 +08003596skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003597 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003598 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003599 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003600 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003601 }
Chris Mason39279cc2007-06-12 06:35:45 -04003602
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003603 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3604 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003605 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003606 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003607 goto err;
3608 }
Chris Masone02119d2008-09-05 16:13:11 -04003609
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003610 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3611 index);
Chris Mason6418c962010-10-30 07:34:24 -04003612 if (ret == -ENOENT)
3613 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003614 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003615 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003616
3617 /*
3618 * If we have a pending delayed iput we could end up with the final iput
3619 * being run in btrfs-cleaner context. If we have enough of these built
3620 * up we can end up burning a lot of time in btrfs-cleaner without any
3621 * way to throttle the unlinks. Since we're currently holding a ref on
3622 * the inode we can run the delayed iput here without any issues as the
3623 * final iput won't be done until after we drop the ref we're currently
3624 * holding.
3625 */
3626 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003627err:
3628 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003629 if (ret)
3630 goto out;
3631
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003632 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003633 inode_inc_iversion(&inode->vfs_inode);
3634 inode_inc_iversion(&dir->vfs_inode);
3635 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3636 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3637 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003638out:
Chris Mason39279cc2007-06-12 06:35:45 -04003639 return ret;
3640}
3641
Al Viro92986792011-03-04 17:14:37 +00003642int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3643 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003644 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003645 const char *name, int name_len)
3646{
3647 int ret;
3648 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3649 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003650 drop_nlink(&inode->vfs_inode);
3651 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003652 }
3653 return ret;
3654}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003655
3656/*
3657 * helper to start transaction for unlink and rmdir.
3658 *
Josef Bacikd52be812013-05-29 14:54:47 -04003659 * unlink and rmdir are special in btrfs, they do not always free space, so
3660 * if we cannot make our reservations the normal way try and see if there is
3661 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3662 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003663 */
Josef Bacikd52be812013-05-29 14:54:47 -04003664static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003665{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003666 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003667
Josef Bacike70bea52011-10-11 14:18:24 -04003668 /*
3669 * 1 for the possible orphan item
3670 * 1 for the dir item
3671 * 1 for the dir index
3672 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003673 * 1 for the inode
3674 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003675 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003676}
3677
Chris Mason39279cc2007-06-12 06:35:45 -04003678static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3679{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003680 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003681 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003682 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003683 int ret;
3684
Josef Bacikd52be812013-05-29 14:54:47 -04003685 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003686 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003687 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003688
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003689 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3690 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003691
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003692 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3693 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3694 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003695 if (ret)
3696 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003697
Yan, Zhenga22285a2010-05-16 10:48:46 -04003698 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003699 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003700 if (ret)
3701 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 }
Josef Bacik7b128762008-07-24 12:17:14 -04003703
Tsutomu Itohb5324022011-07-19 07:27:20 +00003704out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003705 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003706 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003707 return ret;
3708}
3709
Misono Tomohirof60a2362018-04-18 11:34:52 +09003710static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003711 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003712{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003713 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003714 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003715 struct btrfs_path *path;
3716 struct extent_buffer *leaf;
3717 struct btrfs_dir_item *di;
3718 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003719 const char *name = dentry->d_name.name;
3720 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003721 u64 index;
3722 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003723 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003724 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003725
Josef Bacik045d3962019-12-18 17:20:27 -05003726 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3727 objectid = inode->root->root_key.objectid;
3728 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3729 objectid = inode->location.objectid;
3730 } else {
3731 WARN_ON(1);
3732 return -EINVAL;
3733 }
3734
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003735 path = btrfs_alloc_path();
3736 if (!path)
3737 return -ENOMEM;
3738
Li Zefan33345d012011-04-20 10:31:50 +08003739 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003740 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003741 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003742 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003743 goto out;
3744 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003745
3746 leaf = path->nodes[0];
3747 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3748 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3749 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003750 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003751 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003752 goto out;
3753 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003754 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003755
Josef Bacikd49d3282019-12-18 17:20:28 -05003756 /*
3757 * This is a placeholder inode for a subvolume we didn't have a
3758 * reference to at the time of the snapshot creation. In the meantime
3759 * we could have renamed the real subvol link into our snapshot, so
3760 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3761 * Instead simply lookup the dir_index_item for this entry so we can
3762 * remove it. Otherwise we know we have a ref to the root and we can
3763 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3764 */
3765 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003766 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003767 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003768 if (IS_ERR_OR_NULL(di)) {
3769 if (!di)
3770 ret = -ENOENT;
3771 else
3772 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003773 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003774 goto out;
3775 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003776
3777 leaf = path->nodes[0];
3778 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003779 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003780 btrfs_release_path(path);
3781 } else {
3782 ret = btrfs_del_root_ref(trans, objectid,
3783 root->root_key.objectid, dir_ino,
3784 &index, name, name_len);
3785 if (ret) {
3786 btrfs_abort_transaction(trans, ret);
3787 goto out;
3788 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003789 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003790
Lu Fengqi9add2942018-08-01 11:32:26 +08003791 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003792 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003793 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003794 goto out;
3795 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003796
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003797 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003798 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003799 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003800 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003801 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003802 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003803out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003804 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003806}
3807
Misono Tomohiroec42f162018-04-18 11:34:13 +09003808/*
3809 * Helper to check if the subvolume references other subvolumes or if it's
3810 * default.
3811 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003812static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003813{
3814 struct btrfs_fs_info *fs_info = root->fs_info;
3815 struct btrfs_path *path;
3816 struct btrfs_dir_item *di;
3817 struct btrfs_key key;
3818 u64 dir_id;
3819 int ret;
3820
3821 path = btrfs_alloc_path();
3822 if (!path)
3823 return -ENOMEM;
3824
3825 /* Make sure this root isn't set as the default subvol */
3826 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3827 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3828 dir_id, "default", 7, 0);
3829 if (di && !IS_ERR(di)) {
3830 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3831 if (key.objectid == root->root_key.objectid) {
3832 ret = -EPERM;
3833 btrfs_err(fs_info,
3834 "deleting default subvolume %llu is not allowed",
3835 key.objectid);
3836 goto out;
3837 }
3838 btrfs_release_path(path);
3839 }
3840
3841 key.objectid = root->root_key.objectid;
3842 key.type = BTRFS_ROOT_REF_KEY;
3843 key.offset = (u64)-1;
3844
3845 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3846 if (ret < 0)
3847 goto out;
3848 BUG_ON(ret == 0);
3849
3850 ret = 0;
3851 if (path->slots[0] > 0) {
3852 path->slots[0]--;
3853 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3854 if (key.objectid == root->root_key.objectid &&
3855 key.type == BTRFS_ROOT_REF_KEY)
3856 ret = -ENOTEMPTY;
3857 }
3858out:
3859 btrfs_free_path(path);
3860 return ret;
3861}
3862
Nikolay Borisov20a68002018-04-27 14:36:24 +03003863/* Delete all dentries for inodes belonging to the root */
3864static void btrfs_prune_dentries(struct btrfs_root *root)
3865{
3866 struct btrfs_fs_info *fs_info = root->fs_info;
3867 struct rb_node *node;
3868 struct rb_node *prev;
3869 struct btrfs_inode *entry;
3870 struct inode *inode;
3871 u64 objectid = 0;
3872
3873 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3874 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3875
3876 spin_lock(&root->inode_lock);
3877again:
3878 node = root->inode_tree.rb_node;
3879 prev = NULL;
3880 while (node) {
3881 prev = node;
3882 entry = rb_entry(node, struct btrfs_inode, rb_node);
3883
David Sterba37508512018-06-29 10:56:40 +02003884 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003885 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003886 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003887 node = node->rb_right;
3888 else
3889 break;
3890 }
3891 if (!node) {
3892 while (prev) {
3893 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003894 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003895 node = prev;
3896 break;
3897 }
3898 prev = rb_next(prev);
3899 }
3900 }
3901 while (node) {
3902 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003903 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003904 inode = igrab(&entry->vfs_inode);
3905 if (inode) {
3906 spin_unlock(&root->inode_lock);
3907 if (atomic_read(&inode->i_count) > 1)
3908 d_prune_aliases(inode);
3909 /*
3910 * btrfs_drop_inode will have it removed from the inode
3911 * cache when its usage count hits zero.
3912 */
3913 iput(inode);
3914 cond_resched();
3915 spin_lock(&root->inode_lock);
3916 goto again;
3917 }
3918
3919 if (cond_resched_lock(&root->inode_lock))
3920 goto again;
3921
3922 node = rb_next(node);
3923 }
3924 spin_unlock(&root->inode_lock);
3925}
3926
Misono Tomohirof60a2362018-04-18 11:34:52 +09003927int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3928{
3929 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3930 struct btrfs_root *root = BTRFS_I(dir)->root;
3931 struct inode *inode = d_inode(dentry);
3932 struct btrfs_root *dest = BTRFS_I(inode)->root;
3933 struct btrfs_trans_handle *trans;
3934 struct btrfs_block_rsv block_rsv;
3935 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003936 int ret;
3937 int err;
3938
3939 /*
3940 * Don't allow to delete a subvolume with send in progress. This is
3941 * inside the inode lock so the error handling that has to drop the bit
3942 * again is not run concurrently.
3943 */
3944 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003945 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003946 spin_unlock(&dest->root_item_lock);
3947 btrfs_warn(fs_info,
3948 "attempt to delete subvolume %llu during send",
3949 dest->root_key.objectid);
3950 return -EPERM;
3951 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003952 root_flags = btrfs_root_flags(&dest->root_item);
3953 btrfs_set_root_flags(&dest->root_item,
3954 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3955 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003956
3957 down_write(&fs_info->subvol_sem);
3958
3959 err = may_destroy_subvol(dest);
3960 if (err)
3961 goto out_up_write;
3962
3963 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3964 /*
3965 * One for dir inode,
3966 * two for dir entries,
3967 * two for root ref/backref.
3968 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003969 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003970 if (err)
3971 goto out_up_write;
3972
3973 trans = btrfs_start_transaction(root, 0);
3974 if (IS_ERR(trans)) {
3975 err = PTR_ERR(trans);
3976 goto out_release;
3977 }
3978 trans->block_rsv = &block_rsv;
3979 trans->bytes_reserved = block_rsv.size;
3980
3981 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3982
Josef Bacik045d3962019-12-18 17:20:27 -05003983 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003984 if (ret) {
3985 err = ret;
3986 btrfs_abort_transaction(trans, ret);
3987 goto out_end_trans;
3988 }
3989
3990 btrfs_record_root_in_trans(trans, dest);
3991
3992 memset(&dest->root_item.drop_progress, 0,
3993 sizeof(dest->root_item.drop_progress));
3994 dest->root_item.drop_level = 0;
3995 btrfs_set_root_refs(&dest->root_item, 0);
3996
3997 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
3998 ret = btrfs_insert_orphan_item(trans,
3999 fs_info->tree_root,
4000 dest->root_key.objectid);
4001 if (ret) {
4002 btrfs_abort_transaction(trans, ret);
4003 err = ret;
4004 goto out_end_trans;
4005 }
4006 }
4007
Lu Fengqid1957792018-05-29 15:01:54 +08004008 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004009 BTRFS_UUID_KEY_SUBVOL,
4010 dest->root_key.objectid);
4011 if (ret && ret != -ENOENT) {
4012 btrfs_abort_transaction(trans, ret);
4013 err = ret;
4014 goto out_end_trans;
4015 }
4016 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004017 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004018 dest->root_item.received_uuid,
4019 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4020 dest->root_key.objectid);
4021 if (ret && ret != -ENOENT) {
4022 btrfs_abort_transaction(trans, ret);
4023 err = ret;
4024 goto out_end_trans;
4025 }
4026 }
4027
4028out_end_trans:
4029 trans->block_rsv = NULL;
4030 trans->bytes_reserved = 0;
4031 ret = btrfs_end_transaction(trans);
4032 if (ret && !err)
4033 err = ret;
4034 inode->i_flags |= S_DEAD;
4035out_release:
4036 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4037out_up_write:
4038 up_write(&fs_info->subvol_sem);
4039 if (err) {
4040 spin_lock(&dest->root_item_lock);
4041 root_flags = btrfs_root_flags(&dest->root_item);
4042 btrfs_set_root_flags(&dest->root_item,
4043 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4044 spin_unlock(&dest->root_item_lock);
4045 } else {
4046 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004047 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004048 ASSERT(dest->send_in_progress == 0);
4049
4050 /* the last ref */
4051 if (dest->ino_cache_inode) {
4052 iput(dest->ino_cache_inode);
4053 dest->ino_cache_inode = NULL;
4054 }
4055 }
4056
4057 return err;
4058}
4059
Chris Mason39279cc2007-06-12 06:35:45 -04004060static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4061{
David Howells2b0143b2015-03-17 22:25:59 +00004062 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004063 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004064 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004065 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004066 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004067
David Sterbab3ae2442012-09-13 16:04:34 -06004068 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004069 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004070 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004071 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004072
Josef Bacikd52be812013-05-29 14:54:47 -04004073 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004074 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004075 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004076
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004077 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004078 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004079 goto out;
4080 }
4081
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004082 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004083 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004084 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004085
Filipe Manana44f714d2016-06-06 16:11:13 +01004086 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4087
Chris Mason39279cc2007-06-12 06:35:45 -04004088 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004089 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4090 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4091 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004092 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004093 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004094 /*
4095 * Propagate the last_unlink_trans value of the deleted dir to
4096 * its parent directory. This is to prevent an unrecoverable
4097 * log tree in the case we do something like this:
4098 * 1) create dir foo
4099 * 2) create snapshot under dir foo
4100 * 3) delete the snapshot
4101 * 4) rmdir foo
4102 * 5) mkdir foo
4103 * 6) fsync foo or some file inside foo
4104 */
4105 if (last_unlink_trans >= trans->transid)
4106 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4107 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004108out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004109 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004110 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004111
Chris Mason39279cc2007-06-12 06:35:45 -04004112 return err;
4113}
4114
Josef Bacikddfae632017-10-19 14:16:02 -04004115/*
4116 * Return this if we need to call truncate_block for the last bit of the
4117 * truncate.
4118 */
4119#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004120
Chris Mason323ac952008-10-01 19:05:46 -04004121/*
Chris Mason39279cc2007-06-12 06:35:45 -04004122 * this can truncate away extent items, csum items and directory items.
4123 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004124 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004125 *
4126 * csum items that cross the new i_size are truncated to the new size
4127 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004128 *
4129 * min_type is the minimum key type to truncate down to. If set to 0, this
4130 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004131 */
Yan, Zheng80825102009-11-12 09:35:36 +00004132int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4133 struct btrfs_root *root,
4134 struct inode *inode,
4135 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004136{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004137 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004138 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004139 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004140 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004141 struct btrfs_key key;
4142 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004143 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004144 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004145 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004146 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004147 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004148 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004149 int found_extent;
4150 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004151 int pending_del_nr = 0;
4152 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004153 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004154 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004155 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004156 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004157 bool be_nice = false;
4158 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004159 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4160 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004161
4162 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004163
Chris Mason28ed1342014-12-17 09:41:04 -08004164 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004165 * For non-free space inodes and non-shareable roots, we want to back
4166 * off from time to time. This means all inodes in subvolume roots,
4167 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004168 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004169 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004170 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004171 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004172
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004173 path = btrfs_alloc_path();
4174 if (!path)
4175 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004176 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004177
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004178 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004179 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4180 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004181
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004182 /*
4183 * We want to drop from the next block forward in case this
4184 * new size is not block aligned since we will be keeping the
4185 * last block of the extent just the way it is.
4186 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004187 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004188 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004189 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004190 }
Yan, Zheng80825102009-11-12 09:35:36 +00004191
Miao Xie16cdcec2011-04-22 18:12:22 +08004192 /*
4193 * This function is also used to drop the items in the log tree before
4194 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004195 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004196 * items.
4197 */
4198 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004199 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004200
Li Zefan33345d012011-04-20 10:31:50 +08004201 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004202 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004203 key.type = (u8)-1;
4204
Chris Mason85e21ba2008-01-29 15:11:36 -05004205search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004206 /*
4207 * with a 16K leaf size and 128MB extents, you can actually queue
4208 * up a huge file in a single leaf. Most of the time that
4209 * bytes_deleted is > 0, it will be huge by the time we get here
4210 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004211 if (be_nice && bytes_deleted > SZ_32M &&
4212 btrfs_should_end_transaction(trans)) {
4213 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004214 goto out;
4215 }
Chris Masond3977122009-01-05 21:25:51 -05004216
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004217 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4218 if (ret < 0)
4219 goto out;
4220
Chris Mason85e21ba2008-01-29 15:11:36 -05004221 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004222 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004223 /* there are no items in the tree for us to truncate, we're
4224 * done
4225 */
Yan, Zheng80825102009-11-12 09:35:36 +00004226 if (path->slots[0] == 0)
4227 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004228 path->slots[0]--;
4229 }
4230
Chris Masond3977122009-01-05 21:25:51 -05004231 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004232 u64 clear_start = 0, clear_len = 0;
4233
Chris Mason39279cc2007-06-12 06:35:45 -04004234 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004235 leaf = path->nodes[0];
4236 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004237 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004238
Li Zefan33345d012011-04-20 10:31:50 +08004239 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004240 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004241
Chris Mason85e21ba2008-01-29 15:11:36 -05004242 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004243 break;
4244
Chris Mason5f39d392007-10-15 16:14:19 -04004245 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004246 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004247 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004248 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004249 extent_type = btrfs_file_extent_type(leaf, fi);
4250 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004251 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004252 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004253
4254 trace_btrfs_truncate_show_fi_regular(
4255 BTRFS_I(inode), leaf, fi,
4256 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004257 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004258 item_end += btrfs_file_extent_ram_bytes(leaf,
4259 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004260
4261 trace_btrfs_truncate_show_fi_inline(
4262 BTRFS_I(inode), leaf, fi, path->slots[0],
4263 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004264 }
Yan008630c2007-11-07 13:31:09 -05004265 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004266 }
Yan, Zheng80825102009-11-12 09:35:36 +00004267 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004268 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004269 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004270 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004271 break;
4272 if (found_key.offset >= new_size)
4273 del_item = 1;
4274 else
4275 del_item = 0;
4276 }
Chris Mason39279cc2007-06-12 06:35:45 -04004277 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004278 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004279 if (found_type != BTRFS_EXTENT_DATA_KEY)
4280 goto delete;
4281
4282 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004283 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004284
4285 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004286 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004287 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004288 u64 orig_num_bytes =
4289 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004290 extent_num_bytes = ALIGN(new_size -
4291 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004292 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004293 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004294 btrfs_set_file_extent_num_bytes(leaf, fi,
4295 extent_num_bytes);
4296 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004297 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004298 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004299 &root->state) &&
4300 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004301 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004302 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004303 } else {
Chris Masondb945352007-10-15 16:15:53 -04004304 extent_num_bytes =
4305 btrfs_file_extent_disk_num_bytes(leaf,
4306 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004307 extent_offset = found_key.offset -
4308 btrfs_file_extent_offset(leaf, fi);
4309
Chris Mason39279cc2007-06-12 06:35:45 -04004310 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004311 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004312 if (extent_start != 0) {
4313 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004314 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004315 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004316 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004317 }
4318 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004319 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004320 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004321 /*
4322 * we can't truncate inline items that have had
4323 * special encodings
4324 */
4325 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004326 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004327 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4328 btrfs_file_extent_compression(leaf, fi) == 0) {
4329 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004330
Josef Bacikddfae632017-10-19 14:16:02 -04004331 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4332 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004333 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004334 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004335 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004336 * We have to bail so the last_size is set to
4337 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004338 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004339 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004340 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004341 } else {
4342 /*
4343 * Inline extents are special, we just treat
4344 * them as a full sector worth in the file
4345 * extent tree just for simplicity sake.
4346 */
4347 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004348 }
Josef Bacikddfae632017-10-19 14:16:02 -04004349
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004350 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004351 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004352 }
Chris Mason179e29e2007-11-01 11:28:41 -04004353delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004354 /*
4355 * We use btrfs_truncate_inode_items() to clean up log trees for
4356 * multiple fsyncs, and in this case we don't want to clear the
4357 * file extent range because it's just the log.
4358 */
4359 if (root == BTRFS_I(inode)->root) {
4360 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4361 clear_start, clear_len);
4362 if (ret) {
4363 btrfs_abort_transaction(trans, ret);
4364 break;
4365 }
4366 }
4367
Josef Bacikddfae632017-10-19 14:16:02 -04004368 if (del_item)
4369 last_size = found_key.offset;
4370 else
4371 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004372 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004373 if (!pending_del_nr) {
4374 /* no pending yet, add ourselves */
4375 pending_del_slot = path->slots[0];
4376 pending_del_nr = 1;
4377 } else if (pending_del_nr &&
4378 path->slots[0] + 1 == pending_del_slot) {
4379 /* hop on the pending chunk */
4380 pending_del_nr++;
4381 pending_del_slot = path->slots[0];
4382 } else {
Chris Masond3977122009-01-05 21:25:51 -05004383 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004384 }
Chris Mason39279cc2007-06-12 06:35:45 -04004385 } else {
4386 break;
4387 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004388 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004389
Miao Xie27cdeb72014-04-02 19:51:05 +08004390 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004391 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004392 struct btrfs_ref ref = { 0 };
4393
Chris Mason28ed1342014-12-17 09:41:04 -08004394 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004395
4396 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4397 extent_start, extent_num_bytes, 0);
4398 ref.real_root = root->root_key.objectid;
4399 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4400 ino, extent_offset);
4401 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004402 if (ret) {
4403 btrfs_abort_transaction(trans, ret);
4404 break;
4405 }
Chris Mason28f75a02015-02-04 06:59:29 -08004406 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004407 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004408 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004409 }
Chris Mason39279cc2007-06-12 06:35:45 -04004410 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004411
Yan, Zheng80825102009-11-12 09:35:36 +00004412 if (found_type == BTRFS_INODE_ITEM_KEY)
4413 break;
4414
4415 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004416 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004417 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004418 if (pending_del_nr) {
4419 ret = btrfs_del_items(trans, root, path,
4420 pending_del_slot,
4421 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004422 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004423 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004424 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004425 }
Yan, Zheng80825102009-11-12 09:35:36 +00004426 pending_del_nr = 0;
4427 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004428 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004429
Chris Mason28f75a02015-02-04 06:59:29 -08004430 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004431 * We can generate a lot of delayed refs, so we need to
4432 * throttle every once and a while and make sure we're
4433 * adding enough space to keep up with the work we are
4434 * generating. Since we hold a transaction here we
4435 * can't flush, and we don't want to FLUSH_LIMIT because
4436 * we could have generated too many delayed refs to
4437 * actually allocate, so just bail if we're short and
4438 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004439 */
Josef Bacik28bad212018-12-03 10:20:38 -05004440 if (should_throttle) {
4441 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4442 BTRFS_RESERVE_NO_FLUSH);
4443 if (ret) {
4444 ret = -EAGAIN;
4445 break;
4446 }
Chris Mason28f75a02015-02-04 06:59:29 -08004447 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004448 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004449 } else {
4450 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004451 }
Chris Mason39279cc2007-06-12 06:35:45 -04004452 }
Yan, Zheng80825102009-11-12 09:35:36 +00004453out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004454 if (ret >= 0 && pending_del_nr) {
4455 int err;
4456
4457 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004458 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004459 if (err) {
4460 btrfs_abort_transaction(trans, err);
4461 ret = err;
4462 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004463 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004464 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4465 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004466 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004467 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004468 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004469 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4470 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004471 }
Chris Mason28ed1342014-12-17 09:41:04 -08004472
Chris Mason39279cc2007-06-12 06:35:45 -04004473 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004474 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004475}
4476
Chris Masona52d9a82007-08-27 16:49:44 -04004477/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304478 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004479 * @inode - inode that we're zeroing
4480 * @from - the offset to start zeroing
4481 * @len - the length to zero, 0 to zero the entire range respective to the
4482 * offset
4483 * @front - zero up to the offset instead of from the offset on
4484 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304485 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004486 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004487 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304488int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004489 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004490{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004491 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004492 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004493 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4494 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004495 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004496 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004497 char *kaddr;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004498 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004499 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304500 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004501 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004502 gfp_t mask = btrfs_alloc_write_mask(mapping);
Chris Masona52d9a82007-08-27 16:49:44 -04004503 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304504 u64 block_start;
4505 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004506
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004507 if (IS_ALIGNED(offset, blocksize) &&
4508 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004509 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304510
Josef Bacik8b62f872017-10-19 14:15:55 -04004511 block_start = round_down(from, blocksize);
4512 block_end = block_start + blocksize - 1;
4513
Qu Wenruo364ecf32017-02-27 15:10:38 +08004514 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
Josef Bacik8b62f872017-10-19 14:15:55 -04004515 block_start, blocksize);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004516 if (ret)
4517 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04004518
Chris Mason211c17f2008-05-15 09:13:45 -04004519again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004520 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004521 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004522 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004523 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004524 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004525 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004526 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004527 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004528
Chris Masona52d9a82007-08-27 16:49:44 -04004529 if (!PageUptodate(page)) {
4530 ret = btrfs_readpage(NULL, page);
4531 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004532 if (page->mapping != mapping) {
4533 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004534 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004535 goto again;
4536 }
Chris Masona52d9a82007-08-27 16:49:44 -04004537 if (!PageUptodate(page)) {
4538 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004539 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004540 }
4541 }
Chris Mason211c17f2008-05-15 09:13:45 -04004542 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004543
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304544 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004545 set_page_extent_mapped(page);
4546
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304547 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004548 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304549 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004550 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004551 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004552 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004553 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004554 btrfs_put_ordered_extent(ordered);
4555 goto again;
4556 }
4557
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304558 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004559 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4560 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004561
Filipe Mananae3b8a482017-11-04 00:16:59 +00004562 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004563 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004564 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304565 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004566 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004567 goto out_unlock;
4568 }
4569
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304570 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004571 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304572 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004573 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004574 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304575 memset(kaddr + (block_start - page_offset(page)),
4576 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004577 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304578 memset(kaddr + (block_start - page_offset(page)) + offset,
4579 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004580 flush_dcache_page(page);
4581 kunmap(page);
4582 }
Chris Mason247e7432008-07-17 12:53:51 -04004583 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004584 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004585 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004586
Chris Mason89642222008-07-24 09:41:53 -04004587out_unlock:
Josef Bacik5d5e1032009-10-13 16:46:49 -04004588 if (ret)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004589 btrfs_delalloc_release_space(inode, data_reserved, block_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08004590 blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004591 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004592 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004593 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004594out:
Qu Wenruo364ecf32017-02-27 15:10:38 +08004595 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004596 return ret;
4597}
4598
Josef Bacik16e75492013-10-22 12:18:51 -04004599static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4600 u64 offset, u64 len)
4601{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004602 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004603 struct btrfs_trans_handle *trans;
4604 int ret;
4605
4606 /*
4607 * Still need to make sure the inode looks like it's been updated so
4608 * that any holes get logged if we fsync.
4609 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004610 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4611 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004612 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4613 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4614 return 0;
4615 }
4616
4617 /*
4618 * 1 - for the one we're dropping
4619 * 1 - for the one we're adding
4620 * 1 - for updating the inode.
4621 */
4622 trans = btrfs_start_transaction(root, 3);
4623 if (IS_ERR(trans))
4624 return PTR_ERR(trans);
4625
4626 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4627 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004628 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004629 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004630 return ret;
4631 }
4632
David Sterbaf85b7372017-01-20 14:54:07 +01004633 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4634 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004635 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004636 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004637 else
4638 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004639 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004640 return ret;
4641}
4642
Josef Bacik695a0d02011-03-04 15:46:53 -05004643/*
4644 * This function puts in dummy file extents for the area we're creating a hole
4645 * for. So if we are truncating this file to a larger size we need to insert
4646 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4647 * the range between oldsize and size
4648 */
Josef Bacika41ad392011-01-31 15:30:16 -05004649int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004651 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004652 struct btrfs_root *root = BTRFS_I(inode)->root;
4653 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004654 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004655 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004656 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004657 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4658 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004659 u64 last_byte;
4660 u64 cur_offset;
4661 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004662 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004663
Josef Bacika71754f2013-06-17 17:14:39 -04004664 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304665 * If our size started in the middle of a block we need to zero out the
4666 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004667 * expose stale data.
4668 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304669 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004670 if (err)
4671 return err;
4672
Yan Zheng9036c102008-10-30 14:19:41 -04004673 if (size <= hole_start)
4674 return 0;
4675
David Sterbab272ae22020-02-05 19:09:33 +01004676 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004677 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004678 cur_offset = hole_start;
4679 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004680 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004681 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004682 if (IS_ERR(em)) {
4683 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004684 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004685 break;
4686 }
Yan Zheng9036c102008-10-30 14:19:41 -04004687 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004688 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004689 hole_size = last_byte - cur_offset;
4690
Yan, Zheng80825102009-11-12 09:35:36 +00004691 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004692 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004693
Josef Bacik16e75492013-10-22 12:18:51 -04004694 err = maybe_insert_hole(root, inode, cur_offset,
4695 hole_size);
4696 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004697 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004698
4699 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4700 cur_offset, hole_size);
4701 if (err)
4702 break;
4703
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004704 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004705 cur_offset + hole_size - 1, 0);
4706 hole_em = alloc_extent_map();
4707 if (!hole_em) {
4708 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4709 &BTRFS_I(inode)->runtime_flags);
4710 goto next;
4711 }
4712 hole_em->start = cur_offset;
4713 hole_em->len = hole_size;
4714 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004715
Josef Bacik5dc562c2012-08-17 13:14:17 -04004716 hole_em->block_start = EXTENT_MAP_HOLE;
4717 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004718 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004719 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004720 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004721 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004722
4723 while (1) {
4724 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004725 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004726 write_unlock(&em_tree->lock);
4727 if (err != -EEXIST)
4728 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004729 btrfs_drop_extent_cache(BTRFS_I(inode),
4730 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004731 cur_offset +
4732 hole_size - 1, 0);
4733 }
4734 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004735 } else {
4736 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4737 cur_offset, hole_size);
4738 if (err)
4739 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004740 }
Josef Bacik16e75492013-10-22 12:18:51 -04004741next:
Yan Zheng9036c102008-10-30 14:19:41 -04004742 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004743 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004744 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004745 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004746 break;
4747 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004748 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004749 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004750 return err;
4751}
4752
Eric Sandeen3972f262013-01-12 02:57:22 +00004753static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004754{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004755 struct btrfs_root *root = BTRFS_I(inode)->root;
4756 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004757 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004758 loff_t newsize = attr->ia_size;
4759 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004760 int ret;
4761
Eric Sandeen3972f262013-01-12 02:57:22 +00004762 /*
4763 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4764 * special case where we need to update the times despite not having
4765 * these flags set. For all other operations the VFS set these flags
4766 * explicitly if it wants a timestamp update.
4767 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004768 if (newsize != oldsize) {
4769 inode_inc_iversion(inode);
4770 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4771 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004772 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004773 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004774
Josef Bacika41ad392011-01-31 15:30:16 -05004775 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004776 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004777 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004778 * This is to ensure the snapshot captures a fully consistent
4779 * state of this file - if the snapshot captures this expanding
4780 * truncation, it must capture all writes that happened before
4781 * this truncation.
4782 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004783 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004784 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004785 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004786 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004787 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004788 }
Yan, Zheng80825102009-11-12 09:35:36 +00004789
Miao Xief4a2f4c2011-12-14 20:12:01 -05004790 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004791 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004792 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004793 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004794 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004795
4796 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004797 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304798 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004799 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004800 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004801 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004802 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004803
Josef Bacika41ad392011-01-31 15:30:16 -05004804 /*
4805 * We're truncating a file that used to have good data down to
4806 * zero. Make sure it gets into the ordered flush list so that
4807 * any new writes get down to disk quickly.
4808 */
4809 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004810 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4811 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004812
Josef Bacika41ad392011-01-31 15:30:16 -05004813 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004814
David Sterba8e0fa5d2020-06-09 19:21:48 +02004815 /* Disable nonlocked read DIO to avoid the endless truncate */
4816 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004817 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004818 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004819
Filipe Manana213e8c52018-02-06 20:40:31 +00004820 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004821 if (ret && inode->i_nlink) {
4822 int err;
4823
4824 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004825 * Truncate failed, so fix up the in-memory size. We
4826 * adjusted disk_i_size down as we removed extents, so
4827 * wait for disk_i_size to be stable and then update the
4828 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004829 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004830 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004831 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004832 return err;
4833 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004834 }
Yan, Zheng80825102009-11-12 09:35:36 +00004835 }
4836
Josef Bacika41ad392011-01-31 15:30:16 -05004837 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004838}
4839
Chris Mason39279cc2007-06-12 06:35:45 -04004840static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4841{
David Howells2b0143b2015-03-17 22:25:59 +00004842 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004843 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004844 int err;
4845
Li Zefanb83cc962010-12-20 16:04:08 +08004846 if (btrfs_root_readonly(root))
4847 return -EROFS;
4848
Jan Kara31051c82016-05-26 16:55:18 +02004849 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004850 if (err)
4851 return err;
4852
Chris Mason5a3f23d2009-03-31 13:27:11 -04004853 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004854 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004855 if (err)
4856 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004857 }
Yan Zheng9036c102008-10-30 14:19:41 -04004858
Christoph Hellwig10257742010-06-04 11:30:02 +02004859 if (attr->ia_valid) {
4860 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004861 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004862 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004863
Josef Bacik22c44fe2011-11-30 10:45:38 -05004864 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004865 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004866 }
4867
Chris Mason39279cc2007-06-12 06:35:45 -04004868 return err;
4869}
Chris Mason61295eb2008-01-14 16:24:38 -05004870
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004871/*
4872 * While truncating the inode pages during eviction, we get the VFS calling
4873 * btrfs_invalidatepage() against each page of the inode. This is slow because
4874 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4875 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4876 * extent_state structures over and over, wasting lots of time.
4877 *
4878 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4879 * those expensive operations on a per page basis and do only the ordered io
4880 * finishing, while we release here the extent_map and extent_state structures,
4881 * without the excessive merging and splitting.
4882 */
4883static void evict_inode_truncate_pages(struct inode *inode)
4884{
4885 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4886 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4887 struct rb_node *node;
4888
4889 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004890 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004891
4892 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004893 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004894 struct extent_map *em;
4895
Liu Bo07e1ce02018-08-23 03:51:52 +08004896 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004897 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004898 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4899 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004900 remove_extent_mapping(map_tree, em);
4901 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004902 if (need_resched()) {
4903 write_unlock(&map_tree->lock);
4904 cond_resched();
4905 write_lock(&map_tree->lock);
4906 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004907 }
4908 write_unlock(&map_tree->lock);
4909
Filipe Manana6ca07092015-05-26 00:55:42 +01004910 /*
4911 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004912 * We can have ongoing bios started by readahead that have
4913 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004914 * still in progress (unlocked the pages in the bio but did not yet
4915 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004916 * ranges can still be locked and eviction started because before
4917 * submitting those bios, which are executed by a separate task (work
4918 * queue kthread), inode references (inode->i_count) were not taken
4919 * (which would be dropped in the end io callback of each bio).
4920 * Therefore here we effectively end up waiting for those bios and
4921 * anyone else holding locked ranges without having bumped the inode's
4922 * reference count - if we don't do it, when they access the inode's
4923 * io_tree to unlock a range it may be too late, leading to an
4924 * use-after-free issue.
4925 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004926 spin_lock(&io_tree->lock);
4927 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4928 struct extent_state *state;
4929 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004930 u64 start;
4931 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004932 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004933
4934 node = rb_first(&io_tree->state);
4935 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004936 start = state->start;
4937 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004938 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004939 spin_unlock(&io_tree->lock);
4940
David Sterbaff13db42015-12-03 14:30:40 +01004941 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004942
4943 /*
4944 * If still has DELALLOC flag, the extent didn't reach disk,
4945 * and its reserved space won't be freed by delayed_ref.
4946 * So we need to free its reserved space here.
4947 * (Refer to comment in btrfs_invalidatepage, case 2)
4948 *
4949 * Note, end is the bytenr of last byte, so we need + 1 here.
4950 */
Filipe Manana421f0922018-10-12 13:02:48 +01004951 if (state_flags & EXTENT_DELALLOC)
Qu Wenruobc42bda2017-02-27 15:10:39 +08004952 btrfs_qgroup_free_data(inode, NULL, start, end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004953
Filipe Manana6ca07092015-05-26 00:55:42 +01004954 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004955 EXTENT_LOCKED | EXTENT_DELALLOC |
4956 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4957 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004958
Filipe Manana7064dd52014-08-08 02:47:05 +01004959 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004960 spin_lock(&io_tree->lock);
4961 }
4962 spin_unlock(&io_tree->lock);
4963}
4964
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004965static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04004966 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004967{
4968 struct btrfs_fs_info *fs_info = root->fs_info;
4969 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04004970 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04004971 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04004972 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004973
Josef Bacikd3984c92019-08-01 18:19:37 -04004974 /*
4975 * Eviction should be taking place at some place safe because of our
4976 * delayed iputs. However the normal flushing code will run delayed
4977 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
4978 *
4979 * We reserve the delayed_refs_extra here again because we can't use
4980 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
4981 * above. We reserve our extra bit here because we generate a ton of
4982 * delayed refs activity by truncating.
4983 *
4984 * If we cannot make our reservation we'll attempt to steal from the
4985 * global reserve, because we really want to be able to free up space.
4986 */
4987 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
4988 BTRFS_RESERVE_FLUSH_EVICT);
4989 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004990 /*
4991 * Try to steal from the global reserve if there is space for
4992 * it.
4993 */
Josef Bacikd3984c92019-08-01 18:19:37 -04004994 if (btrfs_check_space_for_delayed_refs(fs_info) ||
4995 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
4996 btrfs_warn(fs_info,
4997 "could not allocate space for delete; will truncate on mount");
4998 return ERR_PTR(-ENOSPC);
4999 }
5000 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005001 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005002
5003 trans = btrfs_join_transaction(root);
5004 if (IS_ERR(trans))
5005 return trans;
5006
5007 if (delayed_refs_extra) {
5008 trans->block_rsv = &fs_info->trans_block_rsv;
5009 trans->bytes_reserved = delayed_refs_extra;
5010 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5011 delayed_refs_extra, 1);
5012 }
5013 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005014}
5015
Al Virobd555972010-06-07 11:35:40 -04005016void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005017{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005018 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005019 struct btrfs_trans_handle *trans;
5020 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005021 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005022 int ret;
5023
liubo1abe9b82011-03-24 11:18:59 +00005024 trace_btrfs_inode_evict(inode);
5025
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005026 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005027 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005028 return;
5029 }
5030
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005031 evict_inode_truncate_pages(inode);
5032
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005033 if (inode->i_nlink &&
5034 ((btrfs_root_refs(&root->root_item) != 0 &&
5035 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005036 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005037 goto no_delete;
5038
Omar Sandoval27919062018-05-11 13:13:37 -07005039 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005040 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005041
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005042 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005043
Omar Sandoval7b40b692018-05-11 13:13:33 -07005044 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005045 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005046
Yan, Zheng76dda932009-09-21 16:00:26 -04005047 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005048 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5049 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005050 goto no_delete;
5051 }
5052
Nikolay Borisovaa790212017-01-10 20:35:40 +02005053 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005054 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005055 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005056
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005057 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005058 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005059 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005060 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005061 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005062
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005063 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005064
Yan, Zheng80825102009-11-12 09:35:36 +00005065 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005066 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005067 if (IS_ERR(trans))
5068 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005069
5070 trans->block_rsv = rsv;
5071
Yan, Zhengd68fc572010-05-16 10:49:58 -04005072 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005073 trans->block_rsv = &fs_info->trans_block_rsv;
5074 btrfs_end_transaction(trans);
5075 btrfs_btree_balance_dirty(fs_info);
5076 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5077 goto free_rsv;
5078 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005079 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005080 }
5081
Josef Bacik4ef31a42013-08-13 14:10:08 -04005082 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005083 * Errors here aren't a big deal, it just means we leave orphan items in
5084 * the tree. They will be cleaned up on the next mount. If the inode
5085 * number gets reused, cleanup deletes the orphan item without doing
5086 * anything, and unlink reuses the existing orphan item.
5087 *
5088 * If it turns out that we are dropping too many of these, we might want
5089 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005090 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005091 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005092 if (!IS_ERR(trans)) {
5093 trans->block_rsv = rsv;
5094 btrfs_orphan_del(trans, BTRFS_I(inode));
5095 trans->block_rsv = &fs_info->trans_block_rsv;
5096 btrfs_end_transaction(trans);
5097 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005098
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005099 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005100 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005101 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005102
Omar Sandoval27919062018-05-11 13:13:37 -07005103free_rsv:
5104 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005105no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005106 /*
5107 * If we didn't successfully delete, the orphan item will still be in
5108 * the tree and we'll retry on the next mount. Again, we might also want
5109 * to retry these periodically in the future.
5110 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005111 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005112 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005113}
5114
5115/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005116 * Return the key found in the dir entry in the location pointer, fill @type
5117 * with BTRFS_FT_*, and return 0.
5118 *
Su Yue005d6712018-03-05 17:13:37 +08005119 * If no dir entries were found, returns -ENOENT.
5120 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005121 */
5122static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005123 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005124{
5125 const char *name = dentry->d_name.name;
5126 int namelen = dentry->d_name.len;
5127 struct btrfs_dir_item *di;
5128 struct btrfs_path *path;
5129 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005130 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005131
5132 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005133 if (!path)
5134 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005135
David Sterbaf85b7372017-01-20 14:54:07 +01005136 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5137 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005138 if (IS_ERR_OR_NULL(di)) {
5139 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005140 goto out;
5141 }
Chris Masond3977122009-01-05 21:25:51 -05005142
Chris Mason5f39d392007-10-15 16:14:19 -04005143 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005144 if (location->type != BTRFS_INODE_ITEM_KEY &&
5145 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005146 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005147 btrfs_warn(root->fs_info,
5148"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5149 __func__, name, btrfs_ino(BTRFS_I(dir)),
5150 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005151 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005152 if (!ret)
5153 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005154out:
Chris Mason39279cc2007-06-12 06:35:45 -04005155 btrfs_free_path(path);
5156 return ret;
5157}
5158
5159/*
5160 * when we hit a tree root in a directory, the btrfs part of the inode
5161 * needs to be changed to reflect the root directory of the tree root. This
5162 * is kind of like crossing a mount point.
5163 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005164static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005165 struct inode *dir,
5166 struct dentry *dentry,
5167 struct btrfs_key *location,
5168 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005169{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005170 struct btrfs_path *path;
5171 struct btrfs_root *new_root;
5172 struct btrfs_root_ref *ref;
5173 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005174 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005175 int ret;
5176 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005177
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005178 path = btrfs_alloc_path();
5179 if (!path) {
5180 err = -ENOMEM;
5181 goto out;
5182 }
Chris Mason39279cc2007-06-12 06:35:45 -04005183
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005184 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005185 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5186 key.type = BTRFS_ROOT_REF_KEY;
5187 key.offset = location->objectid;
5188
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005189 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005190 if (ret) {
5191 if (ret < 0)
5192 err = ret;
5193 goto out;
5194 }
Chris Mason39279cc2007-06-12 06:35:45 -04005195
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005196 leaf = path->nodes[0];
5197 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005198 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005199 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5200 goto out;
5201
5202 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5203 (unsigned long)(ref + 1),
5204 dentry->d_name.len);
5205 if (ret)
5206 goto out;
5207
David Sterbab3b4aa72011-04-21 01:20:15 +02005208 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005209
David Sterba56e93572020-05-15 19:35:55 +02005210 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005211 if (IS_ERR(new_root)) {
5212 err = PTR_ERR(new_root);
5213 goto out;
5214 }
5215
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005216 *sub_root = new_root;
5217 location->objectid = btrfs_root_dirid(&new_root->root_item);
5218 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005219 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005220 err = 0;
5221out:
5222 btrfs_free_path(path);
5223 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005224}
5225
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005226static void inode_tree_add(struct inode *inode)
5227{
5228 struct btrfs_root *root = BTRFS_I(inode)->root;
5229 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005230 struct rb_node **p;
5231 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005232 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005233 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005234
Al Viro1d3382cb2010-10-23 15:19:20 -04005235 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005236 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005237 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005238 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005239 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005240 while (*p) {
5241 parent = *p;
5242 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5243
David Sterba37508512018-06-29 10:56:40 +02005244 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005245 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005246 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005247 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005248 else {
5249 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005250 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005251 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005252 RB_CLEAR_NODE(parent);
5253 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005254 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005255 }
5256 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005257 rb_link_node(new, parent, p);
5258 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005259 spin_unlock(&root->inode_lock);
5260}
5261
5262static void inode_tree_del(struct inode *inode)
5263{
5264 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005265 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005266
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005267 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005268 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005269 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005270 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005271 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005272 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005273 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005274
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005275 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005276 spin_lock(&root->inode_lock);
5277 empty = RB_EMPTY_ROOT(&root->inode_tree);
5278 spin_unlock(&root->inode_lock);
5279 if (empty)
5280 btrfs_add_dead_root(root);
5281 }
5282}
5283
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005284
Chris Masone02119d2008-09-05 16:13:11 -04005285static int btrfs_init_locked_inode(struct inode *inode, void *p)
5286{
5287 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005288
5289 inode->i_ino = args->ino;
5290 BTRFS_I(inode)->location.objectid = args->ino;
5291 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5292 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005293 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5294 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005295 return 0;
5296}
5297
5298static int btrfs_find_actor(struct inode *inode, void *opaque)
5299{
5300 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005301
5302 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005303 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005304}
5305
David Sterba0202e832020-05-15 19:35:59 +02005306static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005307 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005308{
5309 struct inode *inode;
5310 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005311 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005312
David Sterba0202e832020-05-15 19:35:59 +02005313 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005314 args.root = root;
5315
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005316 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005317 btrfs_init_locked_inode,
5318 (void *)&args);
5319 return inode;
5320}
5321
David Sterba4c66e0d2019-10-03 19:09:35 +02005322/*
David Sterba0202e832020-05-15 19:35:59 +02005323 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005324 * Path can be preallocated to prevent recursing back to iget through
5325 * allocator. NULL is also valid but may require an additional allocation
5326 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305327 */
David Sterba0202e832020-05-15 19:35:59 +02005328struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005329 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305330{
5331 struct inode *inode;
5332
David Sterba0202e832020-05-15 19:35:59 +02005333 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305334 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005335 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305336
5337 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005338 int ret;
5339
Filipe Manana4222ea72018-10-24 10:13:03 +01005340 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005341 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005342 inode_tree_add(inode);
5343 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005344 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005345 iget_failed(inode);
5346 /*
5347 * ret > 0 can come from btrfs_search_slot called by
5348 * btrfs_read_locked_inode, this means the inode item
5349 * was not found.
5350 */
5351 if (ret > 0)
5352 ret = -ENOENT;
5353 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005354 }
5355 }
5356
Balaji Rao1a54ef82008-07-21 02:01:04 +05305357 return inode;
5358}
5359
David Sterba0202e832020-05-15 19:35:59 +02005360struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005361{
David Sterba0202e832020-05-15 19:35:59 +02005362 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005363}
5364
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005365static struct inode *new_simple_dir(struct super_block *s,
5366 struct btrfs_key *key,
5367 struct btrfs_root *root)
5368{
5369 struct inode *inode = new_inode(s);
5370
5371 if (!inode)
5372 return ERR_PTR(-ENOMEM);
5373
Josef Bacik5c8fd992020-02-14 16:11:43 -05005374 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005375 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005376 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005377
5378 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005379 /*
5380 * We only need lookup, the rest is read-only and there's no inode
5381 * associated with the dentry
5382 */
5383 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005384 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005385 inode->i_fop = &simple_dir_operations;
5386 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005387 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305388 inode->i_atime = inode->i_mtime;
5389 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005390 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005391
5392 return inode;
5393}
5394
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005395static inline u8 btrfs_inode_type(struct inode *inode)
5396{
5397 /*
5398 * Compile-time asserts that generic FT_* types still match
5399 * BTRFS_FT_* types
5400 */
5401 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5402 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5403 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5404 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5405 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5406 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5407 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5408 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5409
5410 return fs_umode_to_ftype(inode->i_mode);
5411}
5412
Chris Mason3de45862008-11-17 21:02:50 -05005413struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005414{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005415 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005416 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005417 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005418 struct btrfs_root *sub_root = root;
5419 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005420 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005421 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005422
5423 if (dentry->d_name.len > BTRFS_NAME_LEN)
5424 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005425
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005426 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005427 if (ret < 0)
5428 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005429
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005430 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005431 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005432 if (IS_ERR(inode))
5433 return inode;
5434
5435 /* Do extra check against inode mode with di_type */
5436 if (btrfs_inode_type(inode) != di_type) {
5437 btrfs_crit(fs_info,
5438"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5439 inode->i_mode, btrfs_inode_type(inode),
5440 di_type);
5441 iput(inode);
5442 return ERR_PTR(-EUCLEAN);
5443 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005444 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005445 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005446
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005447 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005448 &location, &sub_root);
5449 if (ret < 0) {
5450 if (ret != -ENOENT)
5451 inode = ERR_PTR(ret);
5452 else
5453 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5454 } else {
David Sterba0202e832020-05-15 19:35:59 +02005455 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005456 }
Josef Bacik87270022020-01-24 09:32:31 -05005457 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005458 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005459
Julia Lawall34d19ba2011-01-24 19:55:19 +00005460 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005461 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005462 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005463 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005464 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005465 if (ret) {
5466 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005467 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005468 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005469 }
5470
Chris Mason3de45862008-11-17 21:02:50 -05005471 return inode;
5472}
5473
Nick Pigginfe15ce42011-01-07 17:49:23 +11005474static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005475{
5476 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005477 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005478
Li Zefan848cce02012-02-21 17:04:28 +08005479 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005480 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005481
Li Zefan848cce02012-02-21 17:04:28 +08005482 if (inode) {
5483 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005484 if (btrfs_root_refs(&root->root_item) == 0)
5485 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005486
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005487 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005488 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005489 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005490 return 0;
5491}
5492
Chris Mason3de45862008-11-17 21:02:50 -05005493static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005494 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005495{
Al Viro3837d202018-10-10 16:38:27 -04005496 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005497
Al Viro3837d202018-10-10 16:38:27 -04005498 if (inode == ERR_PTR(-ENOENT))
5499 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005500 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005501}
5502
Josef Bacik23b5ec72017-07-24 15:14:25 -04005503/*
5504 * All this infrastructure exists because dir_emit can fault, and we are holding
5505 * the tree lock when doing readdir. For now just allocate a buffer and copy
5506 * our information into that, and then dir_emit from the buffer. This is
5507 * similar to what NFS does, only we don't keep the buffer around in pagecache
5508 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5509 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5510 * tree lock.
5511 */
5512static int btrfs_opendir(struct inode *inode, struct file *file)
5513{
5514 struct btrfs_file_private *private;
5515
5516 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5517 if (!private)
5518 return -ENOMEM;
5519 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5520 if (!private->filldir_buf) {
5521 kfree(private);
5522 return -ENOMEM;
5523 }
5524 file->private_data = private;
5525 return 0;
5526}
5527
5528struct dir_entry {
5529 u64 ino;
5530 u64 offset;
5531 unsigned type;
5532 int name_len;
5533};
5534
5535static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5536{
5537 while (entries--) {
5538 struct dir_entry *entry = addr;
5539 char *name = (char *)(entry + 1);
5540
David Sterba92d32172018-04-16 21:10:14 +02005541 ctx->pos = get_unaligned(&entry->offset);
5542 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5543 get_unaligned(&entry->ino),
5544 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005545 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005546 addr += sizeof(struct dir_entry) +
5547 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005548 ctx->pos++;
5549 }
5550 return 0;
5551}
5552
Al Viro9cdda8d2013-05-22 16:48:09 -04005553static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005554{
Al Viro9cdda8d2013-05-22 16:48:09 -04005555 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005556 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005557 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005558 struct btrfs_dir_item *di;
5559 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005560 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005561 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005562 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005563 struct list_head ins_list;
5564 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005565 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005566 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005567 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005568 char *name_ptr;
5569 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005570 int entries = 0;
5571 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005572 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005573 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005574
Al Viro9cdda8d2013-05-22 16:48:09 -04005575 if (!dir_emit_dots(file, ctx))
5576 return 0;
5577
David Woodhouse49593bf2008-08-17 17:08:36 +01005578 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005579 if (!path)
5580 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005581
Josef Bacik23b5ec72017-07-24 15:14:25 -04005582 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005583 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005584
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005585 INIT_LIST_HEAD(&ins_list);
5586 INIT_LIST_HEAD(&del_list);
5587 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005588
Josef Bacik23b5ec72017-07-24 15:14:25 -04005589again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005590 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005591 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005592 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005593
Chris Mason39279cc2007-06-12 06:35:45 -04005594 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5595 if (ret < 0)
5596 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005597
5598 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005599 struct dir_entry *entry;
5600
Chris Mason5f39d392007-10-15 16:14:19 -04005601 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005602 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005603 if (slot >= btrfs_header_nritems(leaf)) {
5604 ret = btrfs_next_leaf(root, path);
5605 if (ret < 0)
5606 goto err;
5607 else if (ret > 0)
5608 break;
5609 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005610 }
Chris Mason3de45862008-11-17 21:02:50 -05005611
Chris Mason5f39d392007-10-15 16:14:19 -04005612 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5613
5614 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005615 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005616 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005617 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005618 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005619 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005620 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005621 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005622 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005623 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005624 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5625 PAGE_SIZE) {
5626 btrfs_release_path(path);
5627 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5628 if (ret)
5629 goto nopos;
5630 addr = private->filldir_buf;
5631 entries = 0;
5632 total_len = 0;
5633 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005634 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005635
5636 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005637 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005638 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005639 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5640 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005641 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005642 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005643 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005644 put_unaligned(location.objectid, &entry->ino);
5645 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005646 entries++;
5647 addr += sizeof(struct dir_entry) + name_len;
5648 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005649next:
5650 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005651 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005652 btrfs_release_path(path);
5653
5654 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5655 if (ret)
5656 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005657
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005658 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005659 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005660 goto nopos;
5661
Zach Browndb62efb2013-07-11 16:19:42 -07005662 /*
5663 * Stop new entries from being returned after we return the last
5664 * entry.
5665 *
5666 * New directory entries are assigned a strictly increasing
5667 * offset. This means that new entries created during readdir
5668 * are *guaranteed* to be seen in the future by that readdir.
5669 * This has broken buggy programs which operate on names as
5670 * they're returned by readdir. Until we re-use freed offsets
5671 * we have this hack to stop new entries from being returned
5672 * under the assumption that they'll never reach this huge
5673 * offset.
5674 *
5675 * This is being careful not to overflow 32bit loff_t unless the
5676 * last entry requires it because doing so has broken 32bit apps
5677 * in the past.
5678 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005679 if (ctx->pos >= INT_MAX)
5680 ctx->pos = LLONG_MAX;
5681 else
5682 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005683nopos:
5684 ret = 0;
5685err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005686 if (put)
5687 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005688 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005689 return ret;
5690}
5691
Chris Mason39279cc2007-06-12 06:35:45 -04005692/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005693 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005694 * inode changes. But, it is most likely to find the inode in cache.
5695 * FIXME, needs more benchmarking...there are no reasons other than performance
5696 * to keep or drop this code.
5697 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005698static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005699{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005700 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005701 struct btrfs_root *root = BTRFS_I(inode)->root;
5702 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005703 int ret;
5704
Josef Bacik72ac3c02012-05-23 14:13:11 -04005705 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005706 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005707
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005708 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005709 if (IS_ERR(trans))
5710 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005711
5712 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005713 if (ret && ret == -ENOSPC) {
5714 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005715 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005716 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005717 if (IS_ERR(trans))
5718 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005719
Chris Mason94b60442010-05-26 11:02:00 -04005720 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005721 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005722 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005723 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005724 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005725
5726 return ret;
5727}
5728
5729/*
5730 * This is a copy of file_update_time. We need this so we can return error on
5731 * ENOSPC for updating the inode in the case of file write and mmap writes.
5732 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005733static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005734 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005735{
Alexander Block2bc5565282012-06-15 09:49:33 +02005736 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005737 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005738
5739 if (btrfs_root_readonly(root))
5740 return -EROFS;
5741
Josef Bacike41f9412012-03-26 09:46:47 -04005742 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005743 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005744 if (flags & S_CTIME)
5745 inode->i_ctime = *now;
5746 if (flags & S_MTIME)
5747 inode->i_mtime = *now;
5748 if (flags & S_ATIME)
5749 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005750 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005751}
5752
Chris Masond352ac62008-09-29 15:18:18 -04005753/*
5754 * find the highest existing sequence number in a directory
5755 * and then set the in-memory index_cnt variable to reflect
5756 * free sequence numbers
5757 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005758static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005759{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005760 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005761 struct btrfs_key key, found_key;
5762 struct btrfs_path *path;
5763 struct extent_buffer *leaf;
5764 int ret;
5765
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005766 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005767 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005768 key.offset = (u64)-1;
5769
5770 path = btrfs_alloc_path();
5771 if (!path)
5772 return -ENOMEM;
5773
5774 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5775 if (ret < 0)
5776 goto out;
5777 /* FIXME: we should be able to handle this */
5778 if (ret == 0)
5779 goto out;
5780 ret = 0;
5781
5782 /*
5783 * MAGIC NUMBER EXPLANATION:
5784 * since we search a directory based on f_pos we have to start at 2
5785 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5786 * else has to start at 2
5787 */
5788 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005789 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005790 goto out;
5791 }
5792
5793 path->slots[0]--;
5794
5795 leaf = path->nodes[0];
5796 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5797
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005798 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005799 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005800 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005801 goto out;
5802 }
5803
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005804 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005805out:
5806 btrfs_free_path(path);
5807 return ret;
5808}
5809
Chris Masond352ac62008-09-29 15:18:18 -04005810/*
5811 * helper to find a free sequence number in a given directory. This current
5812 * code is very simple, later versions will do smarter things in the btree
5813 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005814int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005815{
5816 int ret = 0;
5817
Nikolay Borisov877574e2017-02-20 13:50:33 +02005818 if (dir->index_cnt == (u64)-1) {
5819 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005820 if (ret) {
5821 ret = btrfs_set_inode_index_count(dir);
5822 if (ret)
5823 return ret;
5824 }
Josef Bacikaec74772008-07-24 12:12:38 -04005825 }
5826
Nikolay Borisov877574e2017-02-20 13:50:33 +02005827 *index = dir->index_cnt;
5828 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005829
5830 return ret;
5831}
5832
Chris Masonb0d5d102014-09-08 13:08:51 -07005833static int btrfs_insert_inode_locked(struct inode *inode)
5834{
5835 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005836
5837 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005838 args.root = BTRFS_I(inode)->root;
5839
5840 return insert_inode_locked4(inode,
5841 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5842 btrfs_find_actor, &args);
5843}
5844
Anand Jain19aee8d2017-07-18 17:37:05 +08005845/*
5846 * Inherit flags from the parent inode.
5847 *
5848 * Currently only the compression flags and the cow flags are inherited.
5849 */
5850static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5851{
5852 unsigned int flags;
5853
5854 if (!dir)
5855 return;
5856
5857 flags = BTRFS_I(dir)->flags;
5858
5859 if (flags & BTRFS_INODE_NOCOMPRESS) {
5860 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5861 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5862 } else if (flags & BTRFS_INODE_COMPRESS) {
5863 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5864 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5865 }
5866
5867 if (flags & BTRFS_INODE_NODATACOW) {
5868 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5869 if (S_ISREG(inode->i_mode))
5870 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5871 }
5872
David Sterba7b6a2212018-03-26 18:40:21 +02005873 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005874}
5875
Chris Mason39279cc2007-06-12 06:35:45 -04005876static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5877 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005878 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005879 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005880 u64 ref_objectid, u64 objectid,
5881 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005882{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005883 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005884 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005885 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005886 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005887 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005888 struct btrfs_inode_ref *ref;
5889 struct btrfs_key key[2];
5890 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005891 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005892 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005893 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005894 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005895
Chris Mason5f39d392007-10-15 16:14:19 -04005896 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005897 if (!path)
5898 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005899
Josef Bacik11a19a92019-09-09 10:12:04 -04005900 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005901 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005902 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005903 if (!inode) {
5904 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005905 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005906 }
Chris Mason39279cc2007-06-12 06:35:45 -04005907
Li Zefan581bb052011-04-20 10:06:11 +08005908 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005909 * O_TMPFILE, set link count to 0, so that after this point,
5910 * we fill in an inode item with the correct link count.
5911 */
5912 if (!name)
5913 set_nlink(inode, 0);
5914
5915 /*
Li Zefan581bb052011-04-20 10:06:11 +08005916 * we have to initialize this early, so we can reclaim the inode
5917 * number if we fail afterwards in this function.
5918 */
5919 inode->i_ino = objectid;
5920
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005921 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005922 trace_btrfs_inode_request(dir);
5923
Nikolay Borisov877574e2017-02-20 13:50:33 +02005924 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005925 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005926 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005927 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005928 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005929 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005930 } else if (dir) {
5931 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005932 }
5933 /*
5934 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005935 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005936 * number
5937 */
5938 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005939 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005940 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005941 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005942 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005943
Josef Bacik5dc562c2012-08-17 13:14:17 -04005944 /*
5945 * We could have gotten an inode number from somebody who was fsynced
5946 * and then removed in this same transaction, so let's just set full
5947 * sync since it will be a full sync anyway and this will blow away the
5948 * old info in the log.
5949 */
5950 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5951
Chris Mason9c583092008-01-29 15:15:18 -05005952 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005953 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005954 key[0].offset = 0;
5955
Chris Mason9c583092008-01-29 15:15:18 -05005956 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005957
5958 if (name) {
5959 /*
5960 * Start new inodes with an inode_ref. This is slightly more
5961 * efficient for small numbers of hard links since they will
5962 * be packed into one item. Extended refs will kick in if we
5963 * add more hard links than can fit in the ref item.
5964 */
5965 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005966 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005967 key[1].offset = ref_objectid;
5968
5969 sizes[1] = name_len + sizeof(*ref);
5970 }
Chris Mason9c583092008-01-29 15:15:18 -05005971
Chris Masonb0d5d102014-09-08 13:08:51 -07005972 location = &BTRFS_I(inode)->location;
5973 location->objectid = objectid;
5974 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02005975 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07005976
5977 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04005978 if (ret < 0) {
5979 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07005980 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04005981 }
Chris Masonb0d5d102014-09-08 13:08:51 -07005982
Chris Masonb9473432009-03-13 11:00:37 -04005983 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005984 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05005985 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07005986 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04005987
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03005988 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005989 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05305990
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005991 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305992 inode->i_atime = inode->i_mtime;
5993 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005994 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05305995
Chris Mason5f39d392007-10-15 16:14:19 -04005996 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
5997 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01005998 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06005999 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006000 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006001
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006002 if (name) {
6003 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6004 struct btrfs_inode_ref);
6005 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6006 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6007 ptr = (unsigned long)(ref + 1);
6008 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6009 }
Chris Mason9c583092008-01-29 15:15:18 -05006010
Chris Mason5f39d392007-10-15 16:14:19 -04006011 btrfs_mark_buffer_dirty(path->nodes[0]);
6012 btrfs_free_path(path);
6013
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006014 btrfs_inherit_iflags(inode, dir);
6015
Al Viro569254b2011-07-24 17:08:40 -04006016 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006017 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006018 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006019 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006020 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6021 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006022 }
6023
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006024 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006025
6026 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006027 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006028
Alexander Block8ea05e32012-07-25 17:35:53 +02006029 btrfs_update_root_times(trans, root);
6030
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006031 ret = btrfs_inode_inherit_props(trans, inode, dir);
6032 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006033 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006034 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006035 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006036
Chris Mason39279cc2007-06-12 06:35:45 -04006037 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006038
6039fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006040 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006041fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006042 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006043 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006044 btrfs_free_path(path);
6045 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006046}
6047
Chris Masond352ac62008-09-29 15:18:18 -04006048/*
6049 * utility function to add 'inode' into 'parent_inode' with
6050 * a give name and a given sequence number.
6051 * if 'add_backref' is true, also insert a backref from the
6052 * inode to the parent directory.
6053 */
Chris Masone02119d2008-09-05 16:13:11 -04006054int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006055 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006056 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006057{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006058 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006059 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006060 struct btrfs_root *root = parent_inode->root;
6061 u64 ino = btrfs_ino(inode);
6062 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006063
Li Zefan33345d012011-04-20 10:31:50 +08006064 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006065 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006066 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006067 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006068 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006069 key.offset = 0;
6070 }
Chris Mason39279cc2007-06-12 06:35:45 -04006071
Li Zefan33345d012011-04-20 10:31:50 +08006072 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006073 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006074 root->root_key.objectid, parent_ino,
6075 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006076 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006077 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6078 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006079 }
6080
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006081 /* Nothing to clean up yet */
6082 if (ret)
6083 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006084
Lu Fengqi684572d2018-08-04 21:10:57 +08006085 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006086 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006087 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006088 goto fail_dir_item;
6089 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006090 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006091 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006092 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006093
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006094 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006095 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006096 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006097 /*
6098 * If we are replaying a log tree, we do not want to update the mtime
6099 * and ctime of the parent directory with the current time, since the
6100 * log replay procedure is responsible for setting them to their correct
6101 * values (the ones it had when the fsync was done).
6102 */
6103 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6104 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6105
6106 parent_inode->vfs_inode.i_mtime = now;
6107 parent_inode->vfs_inode.i_ctime = now;
6108 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006109 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006110 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006111 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006112 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006113
6114fail_dir_item:
6115 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6116 u64 local_index;
6117 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006118 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006119 root->root_key.objectid, parent_ino,
6120 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006121 if (err)
6122 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006123 } else if (add_backref) {
6124 u64 local_index;
6125 int err;
6126
6127 err = btrfs_del_inode_ref(trans, root, name, name_len,
6128 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006129 if (err)
6130 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006131 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006132
6133 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006134 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006135}
6136
6137static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006138 struct btrfs_inode *dir, struct dentry *dentry,
6139 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006140{
Josef Bacika1b075d2010-11-19 20:36:11 +00006141 int err = btrfs_add_link(trans, dir, inode,
6142 dentry->d_name.name, dentry->d_name.len,
6143 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006144 if (err > 0)
6145 err = -EEXIST;
6146 return err;
6147}
6148
Josef Bacik618e21d2007-07-11 10:18:17 -04006149static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006150 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006151{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006152 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006153 struct btrfs_trans_handle *trans;
6154 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006155 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006156 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006157 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006158 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006159
Josef Bacik9ed74f22009-09-11 16:12:44 -04006160 /*
6161 * 2 for inode item and ref
6162 * 2 for dir items
6163 * 1 for xattr if selinux is on
6164 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006165 trans = btrfs_start_transaction(root, 5);
6166 if (IS_ERR(trans))
6167 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006168
Li Zefan581bb052011-04-20 10:06:11 +08006169 err = btrfs_find_free_ino(root, &objectid);
6170 if (err)
6171 goto out_unlock;
6172
Josef Bacikaec74772008-07-24 12:12:38 -04006173 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006174 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6175 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006176 if (IS_ERR(inode)) {
6177 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006178 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006179 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006180 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006181
Casey Schauflerad19db72011-12-15 10:09:07 -05006182 /*
6183 * If the active LSM wants to access the inode during
6184 * d_instantiate it needs these. Smack checks to see
6185 * if the filesystem supports xattrs by looking at the
6186 * ops vector.
6187 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006188 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006189 init_special_inode(inode, inode->i_mode, rdev);
6190
6191 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006192 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006193 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006194
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006195 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6196 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006197 if (err)
6198 goto out_unlock;
6199
6200 btrfs_update_inode(trans, root, inode);
6201 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006202
Josef Bacik618e21d2007-07-11 10:18:17 -04006203out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006204 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006205 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006206 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006207 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006208 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006209 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006210 return err;
6211}
6212
Chris Mason39279cc2007-06-12 06:35:45 -04006213static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006214 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006215{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006216 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006217 struct btrfs_trans_handle *trans;
6218 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006219 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006220 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006221 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006222 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006223
Josef Bacik9ed74f22009-09-11 16:12:44 -04006224 /*
6225 * 2 for inode item and ref
6226 * 2 for dir items
6227 * 1 for xattr if selinux is on
6228 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006229 trans = btrfs_start_transaction(root, 5);
6230 if (IS_ERR(trans))
6231 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006232
Li Zefan581bb052011-04-20 10:06:11 +08006233 err = btrfs_find_free_ino(root, &objectid);
6234 if (err)
6235 goto out_unlock;
6236
Josef Bacikaec74772008-07-24 12:12:38 -04006237 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006238 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6239 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006240 if (IS_ERR(inode)) {
6241 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006242 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006243 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006244 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006245 /*
6246 * If the active LSM wants to access the inode during
6247 * d_instantiate it needs these. Smack checks to see
6248 * if the filesystem supports xattrs by looking at the
6249 * ops vector.
6250 */
6251 inode->i_fop = &btrfs_file_operations;
6252 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006253 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006254
6255 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6256 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006257 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006258
6259 err = btrfs_update_inode(trans, root, inode);
6260 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006261 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006262
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006263 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6264 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006265 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006266 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006267
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006268 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006269 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006270
Chris Mason39279cc2007-06-12 06:35:45 -04006271out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006272 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006273 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006274 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006275 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006276 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006277 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006278 return err;
6279}
6280
6281static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6282 struct dentry *dentry)
6283{
Filipe Manana271dba452016-01-05 16:24:05 +00006284 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006285 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006286 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006287 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006288 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006289 int err;
6290 int drop_inode = 0;
6291
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006292 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006293 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006294 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006295
Mark Fashehf1863732012-08-08 11:32:27 -07006296 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006297 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006298
Nikolay Borisov877574e2017-02-20 13:50:33 +02006299 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006300 if (err)
6301 goto fail;
6302
Yan, Zhenga22285a2010-05-16 10:48:46 -04006303 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006304 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006305 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006306 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006307 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006308 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006309 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006310 if (IS_ERR(trans)) {
6311 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006312 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006313 goto fail;
6314 }
Chris Mason5f39d392007-10-15 16:14:19 -04006315
Miao Xie67de1172013-12-26 13:07:06 +08006316 /* There are several dir indexes for this inode, clear the cache. */
6317 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006318 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006319 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006320 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006321 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006322 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006323
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006324 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6325 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006326
Yan, Zhenga5719522009-09-24 09:17:31 -04006327 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006328 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006329 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006330 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006331 int ret;
6332
Yan, Zhenga5719522009-09-24 09:17:31 -04006333 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006334 if (err)
6335 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006336 if (inode->i_nlink == 1) {
6337 /*
6338 * If new hard link count is 1, it's a file created
6339 * with open(2) O_TMPFILE flag.
6340 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006341 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006342 if (err)
6343 goto fail;
6344 }
Al Viro08c422c2011-12-23 07:58:13 -05006345 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006346 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6347 true, NULL);
6348 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6349 err = btrfs_commit_transaction(trans);
6350 trans = NULL;
6351 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006352 }
Chris Mason39279cc2007-06-12 06:35:45 -04006353
Chris Mason1832a6d2007-12-21 16:27:21 -05006354fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006355 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006356 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006357 if (drop_inode) {
6358 inode_dec_link_count(inode);
6359 iput(inode);
6360 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006361 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006362 return err;
6363}
6364
Al Viro18bb1db2011-07-26 01:41:39 -04006365static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006366{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006367 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006368 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006369 struct btrfs_trans_handle *trans;
6370 struct btrfs_root *root = BTRFS_I(dir)->root;
6371 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006372 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006373 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006374
Josef Bacik9ed74f22009-09-11 16:12:44 -04006375 /*
6376 * 2 items for inode and ref
6377 * 2 items for dir items
6378 * 1 for xattr if selinux is on
6379 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006380 trans = btrfs_start_transaction(root, 5);
6381 if (IS_ERR(trans))
6382 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006383
Li Zefan581bb052011-04-20 10:06:11 +08006384 err = btrfs_find_free_ino(root, &objectid);
6385 if (err)
6386 goto out_fail;
6387
Josef Bacikaec74772008-07-24 12:12:38 -04006388 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006389 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6390 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006391 if (IS_ERR(inode)) {
6392 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006393 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006394 goto out_fail;
6395 }
Chris Mason5f39d392007-10-15 16:14:19 -04006396
Chris Masonb0d5d102014-09-08 13:08:51 -07006397 /* these must be set before we unlock the inode */
6398 inode->i_op = &btrfs_dir_inode_operations;
6399 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006400
Eric Paris2a7dba32011-02-01 11:05:39 -05006401 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006402 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006403 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006404
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006405 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006406 err = btrfs_update_inode(trans, root, inode);
6407 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006408 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006409
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006410 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6411 dentry->d_name.name,
6412 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006413 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006414 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006415
Al Viro1e2e5472018-05-04 08:23:01 -04006416 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006417
6418out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006419 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006420 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006421 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006422 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006423 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006424 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006425 return err;
6426}
6427
Chris Masonc8b97812008-10-29 14:49:59 -04006428static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006429 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006430 size_t pg_offset, u64 extent_offset,
6431 struct btrfs_file_extent_item *item)
6432{
6433 int ret;
6434 struct extent_buffer *leaf = path->nodes[0];
6435 char *tmp;
6436 size_t max_size;
6437 unsigned long inline_size;
6438 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006439 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006440
6441 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006442 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006443 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6444 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006445 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006446 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006447 if (!tmp)
6448 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006449 ptr = btrfs_file_extent_inline_start(item);
6450
6451 read_extent_buffer(leaf, tmp, ptr, inline_size);
6452
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006453 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006454 ret = btrfs_decompress(compress_type, tmp, page,
6455 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006456
6457 /*
6458 * decompression code contains a memset to fill in any space between the end
6459 * of the uncompressed data and the end of max_size in case the decompressed
6460 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6461 * the end of an inline extent and the beginning of the next block, so we
6462 * cover that region here.
6463 */
6464
6465 if (max_size + pg_offset < PAGE_SIZE) {
6466 char *map = kmap(page);
6467 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6468 kunmap(page);
6469 }
Chris Masonc8b97812008-10-29 14:49:59 -04006470 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006471 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006472}
6473
Omar Sandoval39b07b52019-12-02 17:34:23 -08006474/**
6475 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6476 * @inode: file to search in
6477 * @page: page to read extent data into if the extent is inline
6478 * @pg_offset: offset into @page to copy to
6479 * @start: file offset
6480 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006481 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006482 * This returns the first &struct extent_map which overlaps with the given
6483 * range, reading it from the B-tree and caching it if necessary. Note that
6484 * there may be more extents which overlap the given range after the returned
6485 * extent_map.
6486 *
6487 * If @page is not NULL and the extent is inline, this also reads the extent
6488 * data directly into the page and marks the extent up to date in the io_tree.
6489 *
6490 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006491 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006492struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006493 struct page *page, size_t pg_offset,
6494 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006495{
David Sterba3ffbd682018-06-29 10:56:42 +02006496 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006497 int ret;
6498 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006499 u64 extent_start = 0;
6500 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006501 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006502 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006503 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006504 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006505 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006506 struct extent_buffer *leaf;
6507 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006508 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006509 struct extent_map_tree *em_tree = &inode->extent_tree;
6510 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006511
Chris Mason890871b2009-09-02 16:24:52 -04006512 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006513 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006514 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006515
Chris Masona52d9a82007-08-27 16:49:44 -04006516 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006517 if (em->start > start || em->start + em->len <= start)
6518 free_extent_map(em);
6519 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006520 free_extent_map(em);
6521 else
6522 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006523 }
David Sterba172ddd62011-04-21 00:48:27 +02006524 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006525 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006526 err = -ENOMEM;
6527 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006528 }
Chris Masond1310b22008-01-24 16:13:08 -05006529 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006530 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006531 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006532 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006533
Liu Bobee6ec82018-08-17 05:05:28 +08006534 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006535 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006536 err = -ENOMEM;
6537 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006538 }
6539
Liu Bobee6ec82018-08-17 05:05:28 +08006540 /* Chances are we'll be called again, so go ahead and do readahead */
6541 path->reada = READA_FORWARD;
6542
Liu Boe49aabd2018-08-25 13:47:09 +08006543 /*
6544 * Unless we're going to uncompress the inline extent, no sleep would
6545 * happen.
6546 */
6547 path->leave_spinning = 1;
6548
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006549 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006550 if (ret < 0) {
6551 err = ret;
6552 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006553 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006554 if (path->slots[0] == 0)
6555 goto not_found;
6556 path->slots[0]--;
6557 }
6558
Chris Mason5f39d392007-10-15 16:14:19 -04006559 leaf = path->nodes[0];
6560 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006561 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006562 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006563 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006564 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006565 /*
6566 * If we backup past the first extent we want to move forward
6567 * and see if there is an extent in front of us, otherwise we'll
6568 * say there is a hole for our whole search range which can
6569 * cause problems.
6570 */
6571 extent_end = start;
6572 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006573 }
6574
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006575 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006576 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006577 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006578 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6579 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006580 /* Only regular file could have regular/prealloc extent */
6581 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6582 ret = -EUCLEAN;
6583 btrfs_crit(fs_info,
6584 "regular/prealloc extent found for non-regular inode %llu",
6585 btrfs_ino(inode));
6586 goto out;
6587 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006588 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6589 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006590 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006591 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6592 path->slots[0],
6593 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006594 }
Josef Bacik25a50342013-10-14 12:08:38 -04006595next:
Yan Zheng9036c102008-10-30 14:19:41 -04006596 if (start >= extent_end) {
6597 path->slots[0]++;
6598 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6599 ret = btrfs_next_leaf(root, path);
6600 if (ret < 0) {
6601 err = ret;
6602 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006603 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006604 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006605 }
Yan Zheng9036c102008-10-30 14:19:41 -04006606 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006607 }
Yan Zheng9036c102008-10-30 14:19:41 -04006608 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6609 if (found_key.objectid != objectid ||
6610 found_key.type != BTRFS_EXTENT_DATA_KEY)
6611 goto not_found;
6612 if (start + len <= found_key.offset)
6613 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006614 if (start > found_key.offset)
6615 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006616
6617 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006618 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006619 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006620 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006621 em->block_start = EXTENT_MAP_HOLE;
6622 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006623 }
6624
Omar Sandoval39b07b52019-12-02 17:34:23 -08006625 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006626
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006627 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6628 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006629 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006630 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006631 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006632 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006633 size_t size;
6634 size_t extent_offset;
6635 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006636
Omar Sandoval39b07b52019-12-02 17:34:23 -08006637 if (!page)
Yan689f9342007-10-29 11:41:07 -04006638 goto out;
Yan689f9342007-10-29 11:41:07 -04006639
Qu Wenruoe41ca582018-06-06 15:41:49 +08006640 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006641 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006642 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6643 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006644 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006645 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006646 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006647 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006648 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006649
6650 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006651 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006652 if (btrfs_file_extent_compression(leaf, item) !=
6653 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006654 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006655 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006656 if (ret) {
6657 err = ret;
6658 goto out;
6659 }
Chris Masonc8b97812008-10-29 14:49:59 -04006660 } else {
6661 map = kmap(page);
6662 read_extent_buffer(leaf, map + pg_offset, ptr,
6663 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006664 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006665 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006666 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006667 copy_size);
6668 }
Chris Masonc8b97812008-10-29 14:49:59 -04006669 kunmap(page);
6670 }
Chris Mason179e29e2007-11-01 11:28:41 -04006671 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006672 }
Chris Masond1310b22008-01-24 16:13:08 -05006673 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006674 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006675 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006676 }
6677not_found:
6678 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006679 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006680 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006681 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006682insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006683 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006684 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006685 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006686 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6687 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006688 err = -EIO;
6689 goto out;
6690 }
Chris Masond1310b22008-01-24 16:13:08 -05006691
6692 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006693 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006694 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006695 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006696out:
Liu Boc6414282018-08-23 07:36:17 +08006697 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006698
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006699 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006700
Chris Masona52d9a82007-08-27 16:49:44 -04006701 if (err) {
6702 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006703 return ERR_PTR(err);
6704 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006705 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006706 return em;
6707}
6708
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006709struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006710 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006711{
6712 struct extent_map *em;
6713 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006714 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006715 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006716 u64 delalloc_len;
6717 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006718 int err = 0;
6719
Omar Sandoval39b07b52019-12-02 17:34:23 -08006720 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006721 if (IS_ERR(em))
6722 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006723 /*
6724 * If our em maps to:
6725 * - a hole or
6726 * - a pre-alloc extent,
6727 * there might actually be delalloc bytes behind it.
6728 */
6729 if (em->block_start != EXTENT_MAP_HOLE &&
6730 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6731 return em;
6732 else
6733 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006734
6735 /* check to see if we've wrapped (len == -1 or similar) */
6736 end = start + len;
6737 if (end < start)
6738 end = (u64)-1;
6739 else
6740 end -= 1;
6741
6742 em = NULL;
6743
6744 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006745 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006746 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006747 delalloc_end = delalloc_start + delalloc_len;
6748 if (delalloc_end < delalloc_start)
6749 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006750
6751 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006752 * We didn't find anything useful, return the original results from
6753 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006754 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006755 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006756 em = hole_em;
6757 hole_em = NULL;
6758 goto out;
6759 }
6760
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006761 /*
6762 * Adjust the delalloc_start to make sure it doesn't go backwards from
6763 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006764 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006765 delalloc_start = max(start, delalloc_start);
6766 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006767
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006768 if (delalloc_len > 0) {
6769 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006770 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006771 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006772
David Sterba172ddd62011-04-21 00:48:27 +02006773 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006774 if (!em) {
6775 err = -ENOMEM;
6776 goto out;
6777 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006778
6779 ASSERT(hole_em);
6780 /*
6781 * When btrfs_get_extent can't find anything it returns one
6782 * huge hole
6783 *
6784 * Make sure what it found really fits our range, and adjust to
6785 * make sure it is based on the start from the caller
6786 */
6787 if (hole_end <= start || hole_em->start > end) {
6788 free_extent_map(hole_em);
6789 hole_em = NULL;
6790 } else {
6791 hole_start = max(hole_em->start, start);
6792 hole_len = hole_end - hole_start;
6793 }
6794
6795 if (hole_em && delalloc_start > hole_start) {
6796 /*
6797 * Our hole starts before our delalloc, so we have to
6798 * return just the parts of the hole that go until the
6799 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006800 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006801 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006802 em->start = hole_start;
6803 em->orig_start = hole_start;
6804 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006805 * Don't adjust block start at all, it is fixed at
6806 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006807 */
6808 em->block_start = hole_em->block_start;
6809 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006810 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6811 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006812 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006813 /*
6814 * Hole is out of passed range or it starts after
6815 * delalloc range
6816 */
6817 em->start = delalloc_start;
6818 em->len = delalloc_len;
6819 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006820 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006821 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006822 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006823 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006824 return hole_em;
6825 }
6826out:
6827
6828 free_extent_map(hole_em);
6829 if (err) {
6830 free_extent_map(em);
6831 return ERR_PTR(err);
6832 }
6833 return em;
6834}
6835
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006836static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
6837 const u64 start,
6838 const u64 len,
6839 const u64 orig_start,
6840 const u64 block_start,
6841 const u64 block_len,
6842 const u64 orig_block_len,
6843 const u64 ram_bytes,
6844 const int type)
6845{
6846 struct extent_map *em = NULL;
6847 int ret;
6848
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006849 if (type != BTRFS_ORDERED_NOCOW) {
Liu Bo6f9994d2017-01-31 07:50:22 -08006850 em = create_io_em(inode, start, len, orig_start,
6851 block_start, block_len, orig_block_len,
6852 ram_bytes,
6853 BTRFS_COMPRESS_NONE, /* compress_type */
6854 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006855 if (IS_ERR(em))
6856 goto out;
6857 }
6858 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
6859 len, block_len, type);
6860 if (ret) {
6861 if (em) {
6862 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02006863 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006864 start + len - 1, 0);
6865 }
6866 em = ERR_PTR(ret);
6867 }
6868 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006869
6870 return em;
6871}
6872
Josef Bacik4b46fce2010-05-23 11:00:55 -04006873static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6874 u64 start, u64 len)
6875{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006876 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006877 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006878 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006879 struct btrfs_key ins;
6880 u64 alloc_hint;
6881 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006882
Josef Bacik4b46fce2010-05-23 11:00:55 -04006883 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006884 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006885 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006886 if (ret)
6887 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006888
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006889 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
6890 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006891 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006892 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006893 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006894 btrfs_free_reserved_extent(fs_info, ins.objectid,
6895 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006896
Josef Bacik4b46fce2010-05-23 11:00:55 -04006897 return em;
6898}
6899
Chris Mason46bfbb52010-05-26 11:04:10 -04006900/*
6901 * returns 1 when the nocow is safe, < 1 on error, 0 if the
6902 * block must be cow'd
6903 */
Josef Bacik00361582013-08-14 14:02:47 -04006904noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006905 u64 *orig_start, u64 *orig_block_len,
6906 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006907{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006908 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006909 struct btrfs_path *path;
6910 int ret;
6911 struct extent_buffer *leaf;
6912 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006913 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006914 struct btrfs_file_extent_item *fi;
6915 struct btrfs_key key;
6916 u64 disk_bytenr;
6917 u64 backref_offset;
6918 u64 extent_end;
6919 u64 num_bytes;
6920 int slot;
6921 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006922 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006923
Chris Mason46bfbb52010-05-26 11:04:10 -04006924 path = btrfs_alloc_path();
6925 if (!path)
6926 return -ENOMEM;
6927
David Sterbaf85b7372017-01-20 14:54:07 +01006928 ret = btrfs_lookup_file_extent(NULL, root, path,
6929 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006930 if (ret < 0)
6931 goto out;
6932
6933 slot = path->slots[0];
6934 if (ret == 1) {
6935 if (slot == 0) {
6936 /* can't find the item, must cow */
6937 ret = 0;
6938 goto out;
6939 }
6940 slot--;
6941 }
6942 ret = 0;
6943 leaf = path->nodes[0];
6944 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006945 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006946 key.type != BTRFS_EXTENT_DATA_KEY) {
6947 /* not our file or wrong item type, must cow */
6948 goto out;
6949 }
6950
6951 if (key.offset > offset) {
6952 /* Wrong offset, must cow */
6953 goto out;
6954 }
6955
6956 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
6957 found_type = btrfs_file_extent_type(leaf, fi);
6958 if (found_type != BTRFS_FILE_EXTENT_REG &&
6959 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
6960 /* not a regular extent, must cow */
6961 goto out;
6962 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04006963
6964 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
6965 goto out;
6966
Miao Xiee77751a2013-12-27 21:11:50 +08006967 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
6968 if (extent_end <= offset)
6969 goto out;
6970
Chris Mason46bfbb52010-05-26 11:04:10 -04006971 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04006972 if (disk_bytenr == 0)
6973 goto out;
6974
6975 if (btrfs_file_extent_compression(leaf, fi) ||
6976 btrfs_file_extent_encryption(leaf, fi) ||
6977 btrfs_file_extent_other_encoding(leaf, fi))
6978 goto out;
6979
Ethan Lien78d42952018-05-17 14:58:29 +08006980 /*
6981 * Do the same check as in btrfs_cross_ref_exist but without the
6982 * unnecessary search.
6983 */
6984 if (btrfs_file_extent_generation(leaf, fi) <=
6985 btrfs_root_last_snapshot(&root->root_item))
6986 goto out;
6987
Chris Mason46bfbb52010-05-26 11:04:10 -04006988 backref_offset = btrfs_file_extent_offset(leaf, fi);
6989
Josef Bacik7ee9e442013-06-21 16:37:03 -04006990 if (orig_start) {
6991 *orig_start = key.offset - backref_offset;
6992 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
6993 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6994 }
Josef Bacikeb384b52013-04-24 16:32:55 -04006995
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006996 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04006997 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08006998
6999 num_bytes = min(offset + *len, extent_end) - offset;
7000 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7001 u64 range_end;
7002
Jeff Mahoneyda170662016-06-15 09:22:56 -04007003 range_end = round_up(offset + num_bytes,
7004 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007005 ret = test_range_bit(io_tree, offset, range_end,
7006 EXTENT_DELALLOC, 0, NULL);
7007 if (ret) {
7008 ret = -EAGAIN;
7009 goto out;
7010 }
7011 }
7012
Josef Bacik1bda19e2013-10-18 12:10:36 -04007013 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007014
7015 /*
7016 * look for other files referencing this extent, if we
7017 * find any we must cow
7018 */
Josef Bacik00361582013-08-14 14:02:47 -04007019
Liu Boe4c3b2d2017-01-30 12:25:28 -08007020 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007021 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007022 if (ret) {
7023 ret = 0;
7024 goto out;
7025 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007026
7027 /*
7028 * adjust disk_bytenr and num_bytes to cover just the bytes
7029 * in this extent we are about to write. If there
7030 * are any csums in that range we have to cow in order
7031 * to keep the csums correct
7032 */
7033 disk_bytenr += backref_offset;
7034 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007035 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7036 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007037 /*
7038 * all of the above have passed, it is safe to overwrite this extent
7039 * without cow
7040 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007041 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007042 ret = 1;
7043out:
7044 btrfs_free_path(path);
7045 return ret;
7046}
7047
Josef Bacikeb838e72012-07-31 16:28:48 -04007048static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
David Sterba55e20bd2020-06-09 19:56:06 +02007049 struct extent_state **cached_state, int writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007050{
7051 struct btrfs_ordered_extent *ordered;
7052 int ret = 0;
7053
7054 while (1) {
7055 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007056 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007057 /*
7058 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007059 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007060 * extents in this range.
7061 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007062 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007063 lockend - lockstart + 1);
7064
7065 /*
7066 * We need to make sure there are no buffered pages in this
7067 * range either, we could have raced between the invalidate in
7068 * generic_file_direct_write and locking the extent. The
7069 * invalidate needs to happen so that reads after a write do not
7070 * get stale data.
7071 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007072 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007073 (!writing || !filemap_range_has_page(inode->i_mapping,
7074 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007075 break;
7076
7077 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007078 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007079
7080 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007081 /*
7082 * If we are doing a DIO read and the ordered extent we
7083 * found is for a buffered write, we can not wait for it
7084 * to complete and retry, because if we do so we can
7085 * deadlock with concurrent buffered writes on page
7086 * locks. This happens only if our DIO read covers more
7087 * than one extent map, if at this point has already
7088 * created an ordered extent for a previous extent map
7089 * and locked its range in the inode's io tree, and a
7090 * concurrent write against that previous extent map's
7091 * range and this range started (we unlock the ranges
7092 * in the io tree only when the bios complete and
7093 * buffered writes always lock pages before attempting
7094 * to lock range in the io tree).
7095 */
7096 if (writing ||
7097 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7098 btrfs_start_ordered_extent(inode, ordered, 1);
7099 else
7100 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007101 btrfs_put_ordered_extent(ordered);
7102 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007103 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007104 * We could trigger writeback for this range (and wait
7105 * for it to complete) and then invalidate the pages for
7106 * this range (through invalidate_inode_pages2_range()),
7107 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007108 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007109 * triggered a readahead) on a page lock due to an
7110 * ordered dio extent we created before but did not have
7111 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007112 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007113 * ordered extent to complete while holding a lock on
7114 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007115 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007116 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007117 }
7118
Filipe Mananaade77022016-02-18 14:28:55 +00007119 if (ret)
7120 break;
7121
Josef Bacikeb838e72012-07-31 16:28:48 -04007122 cond_resched();
7123 }
7124
7125 return ret;
7126}
7127
Liu Bo6f9994d2017-01-31 07:50:22 -08007128/* The callers of this must take lock_extent() */
7129static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7130 u64 orig_start, u64 block_start,
7131 u64 block_len, u64 orig_block_len,
7132 u64 ram_bytes, int compress_type,
7133 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007134{
7135 struct extent_map_tree *em_tree;
7136 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007137 int ret;
7138
Liu Bo6f9994d2017-01-31 07:50:22 -08007139 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7140 type == BTRFS_ORDERED_COMPRESSED ||
7141 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007142 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007143
Josef Bacik69ffb542012-09-11 15:40:07 -04007144 em_tree = &BTRFS_I(inode)->extent_tree;
7145 em = alloc_extent_map();
7146 if (!em)
7147 return ERR_PTR(-ENOMEM);
7148
7149 em->start = start;
7150 em->orig_start = orig_start;
7151 em->len = len;
7152 em->block_len = block_len;
7153 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007154 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007155 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007156 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007157 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007158 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007159 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007160 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007161 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7162 em->compress_type = compress_type;
7163 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007164
7165 do {
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02007166 btrfs_drop_extent_cache(BTRFS_I(inode), em->start,
Josef Bacik69ffb542012-09-11 15:40:07 -04007167 em->start + em->len - 1, 0);
7168 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007169 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007170 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007171 /*
7172 * The caller has taken lock_extent(), who could race with us
7173 * to add em?
7174 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007175 } while (ret == -EEXIST);
7176
7177 if (ret) {
7178 free_extent_map(em);
7179 return ERR_PTR(ret);
7180 }
7181
Liu Bo6f9994d2017-01-31 07:50:22 -08007182 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007183 return em;
7184}
7185
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007186
David Sterba55e20bd2020-06-09 19:56:06 +02007187static int btrfs_get_blocks_direct_read(struct extent_map *em,
7188 struct buffer_head *bh_result,
7189 struct inode *inode,
7190 u64 start, u64 len)
7191{
7192 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7193
7194 if (em->block_start == EXTENT_MAP_HOLE ||
7195 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7196 return -ENOENT;
7197
7198 len = min(len, em->len - (start - em->start));
7199
7200 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7201 inode->i_blkbits;
7202 bh_result->b_size = len;
7203 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7204 set_buffer_mapped(bh_result);
7205
7206 return 0;
7207}
7208
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007209static int btrfs_get_blocks_direct_write(struct extent_map **map,
David Sterba55e20bd2020-06-09 19:56:06 +02007210 struct buffer_head *bh_result,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007211 struct inode *inode,
7212 struct btrfs_dio_data *dio_data,
7213 u64 start, u64 len)
7214{
7215 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7216 struct extent_map *em = *map;
7217 int ret = 0;
7218
7219 /*
7220 * We don't allocate a new extent in the following cases
7221 *
7222 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7223 * existing extent.
7224 * 2) The extent is marked as PREALLOC. We're good to go here and can
7225 * just use the extent.
7226 *
7227 */
7228 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7229 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7230 em->block_start != EXTENT_MAP_HOLE)) {
7231 int type;
7232 u64 block_start, orig_start, orig_block_len, ram_bytes;
7233
7234 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7235 type = BTRFS_ORDERED_PREALLOC;
7236 else
7237 type = BTRFS_ORDERED_NOCOW;
7238 len = min(len, em->len - (start - em->start));
7239 block_start = em->block_start + (start - em->start);
7240
7241 if (can_nocow_extent(inode, start, &len, &orig_start,
7242 &orig_block_len, &ram_bytes) == 1 &&
7243 btrfs_inc_nocow_writers(fs_info, block_start)) {
7244 struct extent_map *em2;
7245
7246 em2 = btrfs_create_dio_extent(inode, start, len,
7247 orig_start, block_start,
7248 len, orig_block_len,
7249 ram_bytes, type);
7250 btrfs_dec_nocow_writers(fs_info, block_start);
7251 if (type == BTRFS_ORDERED_PREALLOC) {
7252 free_extent_map(em);
7253 *map = em = em2;
7254 }
7255
7256 if (em2 && IS_ERR(em2)) {
7257 ret = PTR_ERR(em2);
7258 goto out;
7259 }
7260 /*
7261 * For inode marked NODATACOW or extent marked PREALLOC,
7262 * use the existing or preallocated extent, so does not
7263 * need to adjust btrfs_space_info's bytes_may_use.
7264 */
7265 btrfs_free_reserved_data_space_noquota(inode, start,
7266 len);
7267 goto skip_cow;
7268 }
7269 }
7270
7271 /* this will cow the extent */
David Sterba55e20bd2020-06-09 19:56:06 +02007272 len = bh_result->b_size;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007273 free_extent_map(em);
7274 *map = em = btrfs_new_extent_direct(inode, start, len);
7275 if (IS_ERR(em)) {
7276 ret = PTR_ERR(em);
7277 goto out;
7278 }
7279
7280 len = min(len, em->len - (start - em->start));
7281
7282skip_cow:
David Sterba55e20bd2020-06-09 19:56:06 +02007283 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7284 inode->i_blkbits;
7285 bh_result->b_size = len;
7286 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7287 set_buffer_mapped(bh_result);
7288
7289 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7290 set_buffer_new(bh_result);
7291
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007292 /*
7293 * Need to update the i_size under the extent lock so buffered
7294 * readers will get the updated i_size when we unlock.
7295 */
David Sterba55e20bd2020-06-09 19:56:06 +02007296 if (!dio_data->overwrite && start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007297 i_size_write(inode, start + len);
7298
David Sterba55e20bd2020-06-09 19:56:06 +02007299 WARN_ON(dio_data->reserve < len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007300 dio_data->reserve -= len;
David Sterba55e20bd2020-06-09 19:56:06 +02007301 dio_data->unsubmitted_oe_range_end = start + len;
7302 current->journal_info = dio_data;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007303out:
7304 return ret;
7305}
7306
David Sterba55e20bd2020-06-09 19:56:06 +02007307static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7308 struct buffer_head *bh_result, int create)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007309{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007310 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007311 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007312 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307313 struct btrfs_dio_data *dio_data = NULL;
David Sterba55e20bd2020-06-09 19:56:06 +02007314 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007315 u64 lockstart, lockend;
David Sterba55e20bd2020-06-09 19:56:06 +02007316 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007317 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007318
David Sterba55e20bd2020-06-09 19:56:06 +02007319 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007320 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007321
Josef Bacikc3298612012-08-03 16:49:19 -04007322 lockstart = start;
7323 lockend = start + len - 1;
7324
David Sterba55e20bd2020-06-09 19:56:06 +02007325 if (current->journal_info) {
7326 /*
7327 * Need to pull our outstanding extents and set journal_info to NULL so
7328 * that anything that needs to check if there's a transaction doesn't get
7329 * confused.
7330 */
7331 dio_data = current->journal_info;
7332 current->journal_info = NULL;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007333 }
7334
Josef Bacikeb838e72012-07-31 16:28:48 -04007335 /*
7336 * If this errors out it's because we couldn't invalidate pagecache for
7337 * this range and we need to fallback to buffered.
7338 */
David Sterba55e20bd2020-06-09 19:56:06 +02007339 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7340 create)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007341 ret = -ENOTBLK;
7342 goto err;
7343 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007344
Omar Sandoval39b07b52019-12-02 17:34:23 -08007345 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007346 if (IS_ERR(em)) {
7347 ret = PTR_ERR(em);
7348 goto unlock_err;
7349 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007350
7351 /*
7352 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7353 * io. INLINE is special, and we could probably kludge it in here, but
7354 * it's still buffered so for safety lets just fall back to the generic
7355 * buffered path.
7356 *
7357 * For COMPRESSED we _have_ to read the entire extent in so we can
7358 * decompress it, so there will be buffering required no matter what we
7359 * do, so go ahead and fallback to buffered.
7360 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007361 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007362 * to buffered IO. Don't blame me, this is the price we pay for using
7363 * the generic code.
7364 */
7365 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7366 em->block_start == EXTENT_MAP_INLINE) {
7367 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007368 ret = -ENOTBLK;
7369 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007370 }
7371
David Sterba55e20bd2020-06-09 19:56:06 +02007372 if (create) {
7373 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7374 dio_data, start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007375 if (ret < 0)
7376 goto unlock_err;
David Sterba55e20bd2020-06-09 19:56:06 +02007377
7378 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7379 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007380 } else {
David Sterba55e20bd2020-06-09 19:56:06 +02007381 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7382 start, len);
7383 /* Can be negative only if we read from a hole */
7384 if (ret < 0) {
7385 ret = 0;
7386 free_extent_map(em);
7387 goto unlock_err;
7388 }
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007389 /*
7390 * We need to unlock only the end area that we aren't using.
7391 * The rest is going to be unlocked by the endio routine.
7392 */
David Sterba55e20bd2020-06-09 19:56:06 +02007393 lockstart = start + bh_result->b_size;
7394 if (lockstart < lockend) {
7395 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7396 lockstart, lockend, &cached_state);
7397 } else {
7398 free_extent_state(cached_state);
7399 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007400 }
7401
Josef Bacik4b46fce2010-05-23 11:00:55 -04007402 free_extent_map(em);
7403
7404 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007405
7406unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007407 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7408 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007409err:
David Sterba55e20bd2020-06-09 19:56:06 +02007410 if (dio_data)
7411 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007412 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007413}
7414
Omar Sandoval769b4f22020-04-16 14:46:22 -07007415static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7416{
7417 /*
7418 * This implies a barrier so that stores to dio_bio->bi_status before
7419 * this and loads of dio_bio->bi_status after this are fully ordered.
7420 */
7421 if (!refcount_dec_and_test(&dip->refs))
7422 return;
7423
7424 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
7425 __endio_write_update_ordered(dip->inode, dip->logical_offset,
7426 dip->bytes,
7427 !dip->dio_bio->bi_status);
7428 } else {
7429 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7430 dip->logical_offset,
7431 dip->logical_offset + dip->bytes - 1);
7432 }
7433
David Sterba55e20bd2020-06-09 19:56:06 +02007434 dio_end_io(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007435 kfree(dip);
7436}
7437
Omar Sandoval77d5d682020-04-16 14:46:25 -07007438static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7439 int mirror_num,
7440 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007441{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007442 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007443 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007444 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007445
Mike Christie37226b22016-06-05 14:31:52 -05007446 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007447
Omar Sandoval5c047a62020-04-16 14:46:24 -07007448 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007449 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007450 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007451
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007452 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007453 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007454 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007455 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007456 return ret;
7457}
7458
Omar Sandoval769b4f22020-04-16 14:46:22 -07007459static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7460 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007461 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007462{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007463 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7464 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007465 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007466 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7467 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007468 struct bio_vec bvec;
7469 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007470 u64 start = io_bio->logical;
7471 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007472 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007473
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007474 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7475 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307476
Liu Bo17347ce2017-05-15 15:33:27 -07007477 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7478 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007479 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007480 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007481 if (uptodate &&
7482 (!csum || !check_data_csum(inode, io_bio, icsum,
7483 bvec.bv_page, pgoff,
7484 start, sectorsize))) {
7485 clean_io_failure(fs_info, failure_tree, io_tree,
7486 start, bvec.bv_page,
7487 btrfs_ino(BTRFS_I(inode)),
7488 pgoff);
7489 } else {
7490 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007491
Omar Sandoval77d5d682020-04-16 14:46:25 -07007492 status = btrfs_submit_read_repair(inode,
7493 &io_bio->bio,
7494 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007495 bvec.bv_page, pgoff,
7496 start,
7497 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007498 io_bio->mirror_num,
7499 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007500 if (status)
7501 err = status;
7502 }
7503 start += sectorsize;
7504 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307505 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307506 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007507 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007508 return err;
7509}
7510
Qu Wenruo524272602017-03-08 10:25:52 +08007511static void __endio_write_update_ordered(struct inode *inode,
7512 const u64 offset, const u64 bytes,
7513 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007514{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007515 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007516 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007517 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007518 u64 ordered_offset = offset;
7519 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007520 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007521
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007522 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08007523 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007524 else
Qu Wenruo524272602017-03-08 10:25:52 +08007525 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007526
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007527 while (ordered_offset < offset + bytes) {
7528 last_offset = ordered_offset;
7529 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
7530 &ordered_offset,
7531 ordered_bytes,
7532 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007533 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7534 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007535 btrfs_queue_work(wq, &ordered->work);
7536 }
7537 /*
7538 * If btrfs_dec_test_ordered_pending does not find any ordered
7539 * extent in the range, we can exit.
7540 */
7541 if (ordered_offset == last_offset)
7542 return;
7543 /*
7544 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007545 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007546 */
7547 if (ordered_offset < offset + bytes) {
7548 ordered_bytes = offset + bytes - ordered_offset;
7549 ordered = NULL;
7550 }
Chris Mason163cf092010-11-28 19:56:33 -05007551 }
Filipe Manana14543772015-11-24 16:23:54 +00007552}
7553
David Sterbad0ee3932018-03-08 14:35:48 +01007554static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007555 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007556{
Josef Bacikc6100a42017-05-05 11:57:13 -04007557 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007558 blk_status_t ret;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007559 ret = btrfs_csum_one_bio(inode, bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007560 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007561 return 0;
7562}
7563
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007564static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007565{
7566 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007567 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007568
Miao Xie8b110e32014-09-12 18:44:03 +08007569 if (err)
7570 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007571 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007572 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7573 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007574 (unsigned long long)bio->bi_iter.bi_sector,
7575 bio->bi_iter.bi_size, err);
7576
Omar Sandoval769b4f22020-04-16 14:46:22 -07007577 if (bio_op(bio) == REQ_OP_READ) {
7578 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007579 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007580 }
7581
Omar Sandoval769b4f22020-04-16 14:46:22 -07007582 if (err)
7583 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007584
Miao Xiee65e1532010-11-22 03:04:43 +00007585 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007586 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007587}
7588
David Sterbad0ee3932018-03-08 14:35:48 +01007589static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7590 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007591{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007592 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007593 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007594 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007595 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007596
Liu Bo4c274bc2017-11-01 17:19:27 -06007597 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007598 if (async_submit)
7599 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7600
Josef Bacik5fd02042012-05-02 14:00:54 -04007601 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007602 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007603 if (ret)
7604 goto err;
7605 }
Miao Xiee65e1532010-11-22 03:04:43 +00007606
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007607 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007608 goto map;
7609
7610 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007611 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7612 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007613 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007614 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007615 } else if (write) {
7616 /*
7617 * If we aren't doing async submit, calculate the csum of the
7618 * bio now.
7619 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007620 ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007621 if (ret)
7622 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007623 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007624 u64 csum_offset;
7625
7626 csum_offset = file_offset - dip->logical_offset;
7627 csum_offset >>= inode->i_sb->s_blocksize_bits;
7628 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7629 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007630 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007631map:
Chris Mason08635ba2019-07-10 12:28:14 -07007632 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007633err:
Miao Xiee65e1532010-11-22 03:04:43 +00007634 return ret;
7635}
7636
Omar Sandovalc36cac22020-04-16 14:46:13 -07007637/*
7638 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7639 * or ordered extents whether or not we submit any bios.
7640 */
7641static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7642 struct inode *inode,
7643 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007644{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007645 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007646 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7647 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007648 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007649
Omar Sandoval85879572020-04-16 14:46:21 -07007650 dip_size = sizeof(*dip);
7651 if (!write && csum) {
7652 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7653 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7654 size_t nblocks;
7655
7656 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7657 dip_size += csum_size * nblocks;
7658 }
7659
7660 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007661 if (!dip)
7662 return NULL;
7663
Omar Sandovalc36cac22020-04-16 14:46:13 -07007664 dip->inode = inode;
7665 dip->logical_offset = file_offset;
7666 dip->bytes = dio_bio->bi_iter.bi_size;
7667 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007668 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007669 refcount_set(&dip->refs, 1);
David Sterba55e20bd2020-06-09 19:56:06 +02007670
7671 if (write) {
7672 struct btrfs_dio_data *dio_data = current->journal_info;
7673
7674 /*
7675 * Setting range start and end to the same value means that
7676 * no cleanup will happen in btrfs_direct_IO
7677 */
7678 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7679 dip->bytes;
7680 dio_data->unsubmitted_oe_range_start =
7681 dio_data->unsubmitted_oe_range_end;
7682 }
Omar Sandovalc36cac22020-04-16 14:46:13 -07007683 return dip;
7684}
7685
David Sterba55e20bd2020-06-09 19:56:06 +02007686static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7687 loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007688{
7689 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007690 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007691 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007692 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7693 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007694 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007695 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007696 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007697 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007698 u64 submit_len;
7699 int clone_offset = 0;
7700 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307701 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007702 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007703 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007704
Omar Sandovalc36cac22020-04-16 14:46:13 -07007705 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7706 if (!dip) {
7707 if (!write) {
7708 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7709 file_offset + dio_bio->bi_iter.bi_size - 1);
7710 }
7711 dio_bio->bi_status = BLK_STS_RESOURCE;
David Sterba55e20bd2020-06-09 19:56:06 +02007712 dio_end_io(dio_bio);
7713 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04007714 }
7715
Omar Sandoval85879572020-04-16 14:46:21 -07007716 if (!write && csum) {
7717 /*
7718 * Load the csums up front to reduce csum tree searches and
7719 * contention when submitting bios.
7720 */
7721 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7722 dip->csums);
7723 if (status != BLK_STS_OK)
7724 goto out_err;
7725 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007726
Omar Sandoval769b4f22020-04-16 14:46:22 -07007727 start_sector = dio_bio->bi_iter.bi_sector;
7728 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007729
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007730 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007731 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7732 start_sector << 9, submit_len,
7733 &geom);
7734 if (ret) {
7735 status = errno_to_blk_status(ret);
7736 goto out_err;
7737 }
7738 ASSERT(geom.len <= INT_MAX);
7739
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007740 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007741
Liu Bo725130b2017-05-16 09:51:39 -07007742 /*
7743 * This will never fail as it's passing GPF_NOFS and
7744 * the allocation is backed by btrfs_bioset.
7745 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007746 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007747 bio->bi_private = dip;
7748 bio->bi_end_io = btrfs_end_dio_bio;
7749 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007750
Liu Bo725130b2017-05-16 09:51:39 -07007751 ASSERT(submit_len >= clone_len);
7752 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007753
Liu Bo725130b2017-05-16 09:51:39 -07007754 /*
7755 * Increase the count before we submit the bio so we know
7756 * the end IO handler won't happen before we increase the
7757 * count. Otherwise, the dip might get freed before we're
7758 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007759 *
7760 * We transfer the initial reference to the last bio, so we
7761 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007762 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007763 if (submit_len > 0) {
7764 refcount_inc(&dip->refs);
7765 /*
7766 * If we are submitting more than one bio, submit them
7767 * all asynchronously. The exception is RAID 5 or 6, as
7768 * asynchronous checksums make it difficult to collect
7769 * full stripe writes.
7770 */
7771 if (!raid56)
7772 async_submit = 1;
7773 }
Miao Xiee65e1532010-11-22 03:04:43 +00007774
David Sterbad0ee3932018-03-08 14:35:48 +01007775 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007776 async_submit);
7777 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007778 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007779 if (submit_len > 0)
7780 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007781 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007782 }
Liu Bo725130b2017-05-16 09:51:39 -07007783
7784 clone_offset += clone_len;
7785 start_sector += clone_len >> 9;
7786 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007787 } while (submit_len > 0);
David Sterba55e20bd2020-06-09 19:56:06 +02007788 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007789
Miao Xiee65e1532010-11-22 03:04:43 +00007790out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007791 dip->dio_bio->bi_status = status;
7792 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007793}
7794
David Sterbaf4c48b42020-06-09 19:19:27 +02007795static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7796 const struct iov_iter *iter, loff_t offset)
7797{
7798 int seg;
7799 int i;
7800 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7801 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007802
David Sterbaf4c48b42020-06-09 19:19:27 +02007803 if (offset & blocksize_mask)
7804 goto out;
7805
7806 if (iov_iter_alignment(iter) & blocksize_mask)
7807 goto out;
7808
7809 /* If this is a write we don't need to check anymore */
7810 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7811 return 0;
7812 /*
7813 * Check to make sure we don't have duplicate iov_base's in this
7814 * iovec, if so return EINVAL, otherwise we'll get csum errors
7815 * when reading back.
7816 */
7817 for (seg = 0; seg < iter->nr_segs; seg++) {
7818 for (i = seg + 1; i < iter->nr_segs; i++) {
7819 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7820 goto out;
7821 }
7822 }
7823 retval = 0;
7824out:
7825 return retval;
7826}
7827
David Sterba55e20bd2020-06-09 19:56:06 +02007828static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007829{
7830 struct file *file = iocb->ki_filp;
7831 struct inode *inode = file->f_mapping->host;
7832 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba55e20bd2020-06-09 19:56:06 +02007833 struct btrfs_dio_data dio_data = { 0 };
David Sterbaf4c48b42020-06-09 19:19:27 +02007834 struct extent_changeset *data_reserved = NULL;
7835 loff_t offset = iocb->ki_pos;
7836 size_t count = 0;
David Sterba55e20bd2020-06-09 19:56:06 +02007837 int flags = 0;
7838 bool wakeup = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007839 bool relock = false;
7840 ssize_t ret;
7841
7842 if (check_direct_IO(fs_info, iter, offset))
7843 return 0;
7844
David Sterba55e20bd2020-06-09 19:56:06 +02007845 inode_dio_begin(inode);
7846
7847 /*
7848 * The generic stuff only does filemap_write_and_wait_range, which
7849 * isn't enough if we've written compressed pages to this area, so
7850 * we need to flush the dirty pages again to make absolutely sure
7851 * that any outstanding dirty pages are on disk.
7852 */
David Sterbaf4c48b42020-06-09 19:19:27 +02007853 count = iov_iter_count(iter);
David Sterba55e20bd2020-06-09 19:56:06 +02007854 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7855 &BTRFS_I(inode)->runtime_flags))
7856 filemap_fdatawrite_range(inode->i_mapping, offset,
7857 offset + count - 1);
7858
David Sterbaf4c48b42020-06-09 19:19:27 +02007859 if (iov_iter_rw(iter) == WRITE) {
7860 /*
7861 * If the write DIO is beyond the EOF, we need update
7862 * the isize, but it is protected by i_mutex. So we can
7863 * not unlock the i_mutex at this case.
7864 */
7865 if (offset + count <= inode->i_size) {
David Sterba55e20bd2020-06-09 19:56:06 +02007866 dio_data.overwrite = 1;
David Sterbaf4c48b42020-06-09 19:19:27 +02007867 inode_unlock(inode);
7868 relock = true;
7869 } else if (iocb->ki_flags & IOCB_NOWAIT) {
7870 ret = -EAGAIN;
7871 goto out;
7872 }
David Sterba55e20bd2020-06-09 19:56:06 +02007873 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
7874 offset, count);
7875 if (ret)
7876 goto out;
7877
7878 /*
7879 * We need to know how many extents we reserved so that we can
7880 * do the accounting properly if we go over the number we
7881 * originally calculated. Abuse current->journal_info for this.
7882 */
7883 dio_data.reserve = round_up(count,
7884 fs_info->sectorsize);
7885 dio_data.unsubmitted_oe_range_start = (u64)offset;
7886 dio_data.unsubmitted_oe_range_end = (u64)offset;
7887 current->journal_info = &dio_data;
David Sterbaf4c48b42020-06-09 19:19:27 +02007888 down_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007889 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7890 &BTRFS_I(inode)->runtime_flags)) {
7891 inode_dio_end(inode);
7892 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7893 wakeup = false;
David Sterbaf4c48b42020-06-09 19:19:27 +02007894 }
7895
David Sterba55e20bd2020-06-09 19:56:06 +02007896 ret = __blockdev_direct_IO(iocb, inode,
7897 fs_info->fs_devices->latest_bdev,
7898 iter, btrfs_get_blocks_direct, NULL,
7899 btrfs_submit_direct, flags);
David Sterbaf4c48b42020-06-09 19:19:27 +02007900 if (iov_iter_rw(iter) == WRITE) {
7901 up_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007902 current->journal_info = NULL;
7903 if (ret < 0 && ret != -EIOCBQUEUED) {
7904 if (dio_data.reserve)
7905 btrfs_delalloc_release_space(inode, data_reserved,
7906 offset, dio_data.reserve, true);
7907 /*
7908 * On error we might have left some ordered extents
7909 * without submitting corresponding bios for them, so
7910 * cleanup them up to avoid other tasks getting them
7911 * and waiting for them to complete forever.
7912 */
7913 if (dio_data.unsubmitted_oe_range_start <
7914 dio_data.unsubmitted_oe_range_end)
7915 __endio_write_update_ordered(inode,
7916 dio_data.unsubmitted_oe_range_start,
7917 dio_data.unsubmitted_oe_range_end -
7918 dio_data.unsubmitted_oe_range_start,
7919 false);
7920 } else if (ret >= 0 && (size_t)ret < count)
7921 btrfs_delalloc_release_space(inode, data_reserved,
7922 offset, count - (size_t)ret, true);
7923 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
David Sterbaf4c48b42020-06-09 19:19:27 +02007924 }
7925out:
David Sterba55e20bd2020-06-09 19:56:06 +02007926 if (wakeup)
7927 inode_dio_end(inode);
David Sterbaf4c48b42020-06-09 19:19:27 +02007928 if (relock)
7929 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02007930
David Sterbaf4c48b42020-06-09 19:19:27 +02007931 extent_changeset_free(data_reserved);
7932 return ret;
7933}
Chris Mason16432982008-04-10 10:23:21 -04007934
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007935static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
7936 __u64 start, __u64 len)
7937{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007938 int ret;
7939
Christoph Hellwig45dd0522020-05-23 09:30:14 +02007940 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007941 if (ret)
7942 return ret;
7943
David Sterba2135fb92017-06-23 04:09:57 +02007944 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007945}
7946
Chris Mason9ebefb182007-06-15 13:50:00 -04007947int btrfs_readpage(struct file *file, struct page *page)
7948{
David Sterba71ad38b2020-02-05 19:09:35 +01007949 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007950}
Chris Mason1832a6d2007-12-21 16:27:21 -05007951
Chris Mason39279cc2007-06-12 06:35:45 -04007952static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7953{
Josef Bacikbe7bd732015-10-22 15:05:09 -04007954 struct inode *inode = page->mapping->host;
7955 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04007956
7957 if (current->flags & PF_MEMALLOC) {
7958 redirty_page_for_writepage(wbc, page);
7959 unlock_page(page);
7960 return 0;
7961 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04007962
7963 /*
7964 * If we are under memory pressure we will call this directly from the
7965 * VM, we need to make sure we have the inode referenced for the ordered
7966 * extent. If not just return like we didn't do anything.
7967 */
7968 if (!igrab(inode)) {
7969 redirty_page_for_writepage(wbc, page);
7970 return AOP_WRITEPAGE_ACTIVATE;
7971 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02007972 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04007973 btrfs_add_delayed_iput(inode);
7974 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04007975}
Chris Mason39279cc2007-06-12 06:35:45 -04007976
Eric Sandeen48a3b632013-04-25 20:41:01 +00007977static int btrfs_writepages(struct address_space *mapping,
7978 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04007979{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03007980 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04007981}
7982
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007983static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05007984{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007985 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05007986}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03007987
Chris Masone6dcd2d2008-07-17 12:53:50 -04007988static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04007989{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03007990 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07007991 if (ret == 1)
7992 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04007993 return ret;
7994}
Chris Mason39279cc2007-06-12 06:35:45 -04007995
Chris Masone6dcd2d2008-07-17 12:53:50 -04007996static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
7997{
Chris Mason98509cf2008-09-11 15:51:43 -04007998 if (PageWriteback(page) || PageDirty(page))
7999 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008000 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008001}
8002
Roman Gushchinf8e66082020-03-04 16:57:35 -08008003#ifdef CONFIG_MIGRATION
8004static int btrfs_migratepage(struct address_space *mapping,
8005 struct page *newpage, struct page *page,
8006 enum migrate_mode mode)
8007{
8008 int ret;
8009
8010 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8011 if (ret != MIGRATEPAGE_SUCCESS)
8012 return ret;
8013
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008014 if (page_has_private(page))
8015 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008016
8017 if (PagePrivate2(page)) {
8018 ClearPagePrivate2(page);
8019 SetPagePrivate2(newpage);
8020 }
8021
8022 if (mode != MIGRATE_SYNC_NO_COPY)
8023 migrate_page_copy(newpage, page);
8024 else
8025 migrate_page_states(newpage, page);
8026 return MIGRATEPAGE_SUCCESS;
8027}
8028#endif
8029
Lukas Czernerd47992f2013-05-21 23:17:23 -04008030static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8031 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008032{
Josef Bacik5fd02042012-05-02 14:00:54 -04008033 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008034 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008035 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008036 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008037 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008038 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308039 u64 start;
8040 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008041 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008042
Chris Mason8b62b722009-09-02 16:53:46 -04008043 /*
8044 * we have the page locked, so new writeback can't start,
8045 * and the dirty bit won't be cleared while we are here.
8046 *
8047 * Wait for IO on this page so that we can safely clear
8048 * the PagePrivate2 bit and do ordered accounting
8049 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008050 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008051
Josef Bacik5fd02042012-05-02 14:00:54 -04008052 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008053 if (offset) {
8054 btrfs_releasepage(page, GFP_NOFS);
8055 return;
8056 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008057
8058 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008059 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308060again:
8061 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008062 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308063 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008064 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008065 end = min(page_end,
8066 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008067 /*
8068 * IO on this page will never be started, so we need
8069 * to account for any ordered extents now
8070 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008071 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308072 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008073 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008074 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008075 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008076 /*
8077 * whoever cleared the private bit is responsible
8078 * for the finish_ordered_io
8079 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008080 if (TestClearPagePrivate2(page)) {
8081 struct btrfs_ordered_inode_tree *tree;
8082 u64 new_len;
8083
8084 tree = &BTRFS_I(inode)->ordered_tree;
8085
8086 spin_lock_irq(&tree->lock);
8087 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308088 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008089 if (new_len < ordered->truncated_len)
8090 ordered->truncated_len = new_len;
8091 spin_unlock_irq(&tree->lock);
8092
8093 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308094 start,
8095 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008096 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008097 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008098 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008099 if (!inode_evicting) {
8100 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308101 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008102 &cached_state);
8103 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308104
8105 start = end + 1;
8106 if (start < page_end)
8107 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008108 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008109
Qu Wenruob9d0b382015-09-29 10:35:16 +08008110 /*
8111 * Qgroup reserved space handler
8112 * Page here will be either
8113 * 1) Already written to disk
8114 * In this case, its reserved space is released from data rsv map
8115 * and will be freed by delayed_ref handler finally.
8116 * So even we call qgroup_free_data(), it won't decrease reserved
8117 * space.
8118 * 2) Not written to disk
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008119 * This means the reserved space should be freed here. However,
8120 * if a truncate invalidates the page (by clearing PageDirty)
8121 * and the page is accounted for while allocating extent
8122 * in btrfs_check_data_free_space() we let delayed_ref to
8123 * free the entire extent.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008124 */
Goldwyn Rodrigues0b34c262016-09-30 10:40:52 -05008125 if (PageDirty(page))
Qu Wenruobc42bda2017-02-27 15:10:39 +08008126 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008127 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008128 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008129 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8130 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008131 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008132
8133 __btrfs_releasepage(page, GFP_NOFS);
8134 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008135
Chris Mason4a096752008-07-21 10:29:44 -04008136 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008137 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008138}
8139
Chris Mason9ebefb182007-06-15 13:50:00 -04008140/*
8141 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8142 * called from a page fault handler when a page is first dirtied. Hence we must
8143 * be careful to check for EOF conditions here. We set the page up correctly
8144 * for a written page which means we get ENOSPC checking when writing into
8145 * holes and correct delalloc and unwritten extent mapping on filesystems that
8146 * support these features.
8147 *
8148 * We are not allowed to take the i_mutex here so we have to play games to
8149 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008150 * truncate_setsize() writes the inode size before removing pages, once we have
8151 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008152 * beyond EOF, then the page is guaranteed safe against truncation until we
8153 * unlock the page.
8154 */
Souptick Joardera528a242018-06-06 19:54:44 +05308155vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008156{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008157 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008158 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008159 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008160 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8161 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008162 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008163 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008164 char *kaddr;
8165 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008166 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308167 vm_fault_t ret;
8168 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008169 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308170 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008171 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008172 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308173 u64 end;
8174
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008175 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008176
Jan Karab2b5ef52012-06-12 16:20:45 +02008177 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008178 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008179 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308180 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008181
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308182 /*
8183 * Reserving delalloc space after obtaining the page lock can lead to
8184 * deadlock. For example, if a dirty page is locked by this function
8185 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8186 * dirty page write out, then the btrfs_writepage() function could
8187 * end up waiting indefinitely to get a lock on the page currently
8188 * being processed by btrfs_page_mkwrite() function.
8189 */
Souptick Joardera528a242018-06-06 19:54:44 +05308190 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308191 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308192 if (!ret2) {
8193 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008194 reserved = 1;
8195 }
Souptick Joardera528a242018-06-06 19:54:44 +05308196 if (ret2) {
8197 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008198 if (reserved)
8199 goto out;
8200 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008201 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008202
Nick Piggin56a76f82009-03-31 15:23:23 -07008203 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008204again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008205 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008206 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008207
Chris Mason9ebefb182007-06-15 13:50:00 -04008208 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008209 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008210 /* page got truncated out from underneath us */
8211 goto out_unlock;
8212 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008213 wait_on_page_writeback(page);
8214
David Sterbaff13db42015-12-03 14:30:40 +01008215 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008216 set_page_extent_mapped(page);
8217
Chris Masoneb84ae02008-07-17 13:53:27 -04008218 /*
8219 * we can't set the delalloc bits if there are pending ordered
8220 * extents. Drop our locks and wait for them to finish
8221 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008222 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8223 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008224 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008225 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008226 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008227 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008228 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008229 btrfs_put_ordered_extent(ordered);
8230 goto again;
8231 }
8232
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008233 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008234 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008235 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008236 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308237 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008238 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008239 page_start, PAGE_SIZE - reserved_space,
8240 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308241 }
8242 }
8243
Josef Bacikfbf19082009-10-01 17:10:23 -04008244 /*
Liu Bo54160342016-12-13 12:15:19 -08008245 * page_mkwrite gets called when the page is firstly dirtied after it's
8246 * faulted in, but write(2) could also dirty a page and set delalloc
8247 * bits, thus in this case for space account reason, we still need to
8248 * clear any delalloc bits within this page range since we have to
8249 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008250 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308251 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008252 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8253 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008254
Souptick Joardera528a242018-06-06 19:54:44 +05308255 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008256 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308257 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008258 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008259 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008260 ret = VM_FAULT_SIGBUS;
8261 goto out_unlock;
8262 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008263
8264 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008265 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008266 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008267 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008268 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008269
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008270 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008271 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008272 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008273 flush_dcache_page(page);
8274 kunmap(page);
8275 }
Chris Mason247e7432008-07-17 12:53:51 -04008276 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008277 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008278 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008279
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008280 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008281 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008282 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008283
David Sterbae43bbe52017-12-12 21:43:52 +01008284 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008285
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008286 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8287 sb_end_pagefault(inode->i_sb);
8288 extent_changeset_free(data_reserved);
8289 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008290
8291out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008292 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008293out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008294 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008295 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008296 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008297out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008298 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008299 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008300 return ret;
8301}
8302
Filipe Manana213e8c52018-02-06 20:40:31 +00008303static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008304{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008305 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008306 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008307 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008308 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008309 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008310 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008311 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008312
Filipe Manana213e8c52018-02-06 20:40:31 +00008313 if (!skip_writeback) {
8314 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8315 (u64)-1);
8316 if (ret)
8317 return ret;
8318 }
Chris Mason39279cc2007-06-12 06:35:45 -04008319
Josef Bacikfcb80c22011-05-03 10:40:22 -04008320 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008321 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8322 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008323 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008324 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008325 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008326 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008327 * be free'd up by the truncate operation, but also have some slack
8328 * space reserved in case it uses space during the truncate (thank you
8329 * very much snapshotting).
8330 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008331 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008332 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008333 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008334 * doesn't end up using space reserved for updating the inode. We also
8335 * need to be able to stop the transaction and start a new one, which
8336 * means we need to be able to update the inode several times, and we
8337 * have no idea of knowing how many times that will be, so we can't just
8338 * reserve 1 item for the entirety of the operation, so that has to be
8339 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008340 *
8341 * So that leaves us with
8342 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008343 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008344 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008345 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008346 * updating the inode.
8347 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008348 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008349 if (!rsv)
8350 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008351 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008352 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008353
Josef Bacik907cbce2011-08-08 13:46:15 -04008354 /*
Josef Bacik07127182011-08-19 10:29:59 -04008355 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008356 * 1 for updating the inode.
8357 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008358 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008359 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008360 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008361 goto out;
8362 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008363
Josef Bacik907cbce2011-08-08 13:46:15 -04008364 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008365 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008366 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008367 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008368
Chris Mason5a3f23d2009-03-31 13:27:11 -04008369 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008370 * So if we truncate and then write and fsync we normally would just
8371 * write the extents that changed, which is a problem if we need to
8372 * first truncate that entire inode. So set this flag so we write out
8373 * all of the extents in the inode to the sync log so we're completely
8374 * safe.
8375 */
8376 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008377 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008378
Yan, Zheng80825102009-11-12 09:35:36 +00008379 while (1) {
8380 ret = btrfs_truncate_inode_items(trans, root, inode,
8381 inode->i_size,
8382 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008383 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008384 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008385 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008386
Yan, Zheng80825102009-11-12 09:35:36 +00008387 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008388 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008389 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008390
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008391 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008392 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008393
8394 trans = btrfs_start_transaction(root, 2);
8395 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008396 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008397 trans = NULL;
8398 break;
8399 }
8400
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008401 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008402 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008403 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008404 BUG_ON(ret); /* shouldn't happen */
8405 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008406 }
8407
Josef Bacikddfae632017-10-19 14:16:02 -04008408 /*
8409 * We can't call btrfs_truncate_block inside a trans handle as we could
8410 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8411 * we've truncated everything except the last little bit, and can do
8412 * btrfs_truncate_block and then update the disk_i_size.
8413 */
8414 if (ret == NEED_TRUNCATE_BLOCK) {
8415 btrfs_end_transaction(trans);
8416 btrfs_btree_balance_dirty(fs_info);
8417
8418 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8419 if (ret)
8420 goto out;
8421 trans = btrfs_start_transaction(root, 1);
8422 if (IS_ERR(trans)) {
8423 ret = PTR_ERR(trans);
8424 goto out;
8425 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008426 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008427 }
8428
Chris Mason917c16b2011-11-08 14:49:59 -05008429 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008430 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008431
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008432 trans->block_rsv = &fs_info->trans_block_rsv;
8433 ret2 = btrfs_update_inode(trans, root, inode);
8434 if (ret2 && !ret)
8435 ret = ret2;
8436
8437 ret2 = btrfs_end_transaction(trans);
8438 if (ret2 && !ret)
8439 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008440 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008441 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008442out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008443 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008444
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008445 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008446}
8447
Sven Wegener3b963622008-06-09 21:57:42 -04008448/*
Chris Masond352ac62008-09-29 15:18:18 -04008449 * create a new subvolume directory/inode (helper for the ioctl).
8450 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008451int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008452 struct btrfs_root *new_root,
8453 struct btrfs_root *parent_root,
8454 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008455{
Chris Mason39279cc2007-06-12 06:35:45 -04008456 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008457 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008458 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008459
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008460 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8461 new_dirid, new_dirid,
8462 S_IFDIR | (~current_umask() & S_IRWXUGO),
8463 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008464 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008465 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008466 inode->i_op = &btrfs_dir_inode_operations;
8467 inode->i_fop = &btrfs_dir_file_operations;
8468
Miklos Szeredibfe86842011-10-28 14:13:29 +02008469 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008470 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008471 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008472
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008473 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8474 if (err)
8475 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008476 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008477 new_root->root_key.objectid, err);
8478
Yan, Zheng76dda932009-09-21 16:00:26 -04008479 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008480
Yan, Zheng76dda932009-09-21 16:00:26 -04008481 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008482 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008483}
8484
Chris Mason39279cc2007-06-12 06:35:45 -04008485struct inode *btrfs_alloc_inode(struct super_block *sb)
8486{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008487 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008488 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008489 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008490
David Sterba712e36c2017-10-31 17:08:27 +01008491 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008492 if (!ei)
8493 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008494
8495 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008496 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008497 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008498 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008499 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008500 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008501 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008502 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008503 ei->disk_i_size = 0;
8504 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008505 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008506 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008507 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008508 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008509 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008510
Josef Bacik9e0baf62011-07-15 15:16:44 +00008511 spin_lock_init(&ei->lock);
8512 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008513 if (sb->s_magic != BTRFS_TEST_MAGIC)
8514 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8515 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008516 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008517 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008518 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008519
Miao Xie16cdcec2011-04-22 18:12:22 +08008520 ei->delayed_node = NULL;
8521
chandan r9cc97d62012-07-04 12:48:07 +05308522 ei->i_otime.tv_sec = 0;
8523 ei->i_otime.tv_nsec = 0;
8524
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008525 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008526 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008527 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8528 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8529 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008530 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8531 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008532 ei->io_tree.track_uptodate = true;
8533 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008534 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008535 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008536 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008537 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008538 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008539 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008540 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008541
8542 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008543}
8544
Josef Bacikaaedb552013-10-11 14:44:09 -04008545#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8546void btrfs_test_destroy_inode(struct inode *inode)
8547{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008548 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008549 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8550}
8551#endif
8552
Al Viro26602ca2019-04-10 15:14:41 -04008553void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008554{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008555 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8556}
8557
Chris Mason39279cc2007-06-12 06:35:45 -04008558void btrfs_destroy_inode(struct inode *inode)
8559{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008560 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008561 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008562 struct btrfs_root *root = BTRFS_I(inode)->root;
8563
Al Virob3d9b7a2012-06-09 13:51:19 -04008564 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008565 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008566 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8567 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008568 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008569 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008570 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008571 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008572 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008573
Chris Mason5a3f23d2009-03-31 13:27:11 -04008574 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008575 * This can happen where we create an inode, but somebody else also
8576 * created the same inode and we need to destroy the one we already
8577 * created.
8578 */
8579 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008580 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008581
Chris Masond3977122009-01-05 21:25:51 -05008582 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008583 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8584 if (!ordered)
8585 break;
8586 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008587 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008588 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008589 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008590 btrfs_remove_ordered_extent(inode, ordered);
8591 btrfs_put_ordered_extent(ordered);
8592 btrfs_put_ordered_extent(ordered);
8593 }
8594 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008595 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008596 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008597 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008598 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008599 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008600}
8601
Al Viro45321ac2010-06-07 13:43:19 -04008602int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008603{
8604 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008605
Naohiro Aota6379ef92013-06-06 09:56:34 +00008606 if (root == NULL)
8607 return 1;
8608
Liu Bofa6ac872013-02-20 14:10:23 +00008609 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008610 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008611 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008612 else
Al Viro45321ac2010-06-07 13:43:19 -04008613 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008614}
8615
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008616static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008617{
8618 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8619
8620 inode_init_once(&ei->vfs_inode);
8621}
8622
David Sterbae67c7182018-02-19 17:24:18 +01008623void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008624{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008625 /*
8626 * Make sure all delayed rcu free inodes are flushed before we
8627 * destroy cache.
8628 */
8629 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008630 kmem_cache_destroy(btrfs_inode_cachep);
8631 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008632 kmem_cache_destroy(btrfs_path_cachep);
8633 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008634 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008635}
8636
Liu Bof5c29bd2017-11-02 17:21:50 -06008637int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008638{
David Sterba837e1972012-09-07 03:00:48 -06008639 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008640 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008641 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8642 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008643 if (!btrfs_inode_cachep)
8644 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008645
David Sterba837e1972012-09-07 03:00:48 -06008646 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008647 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008648 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008649 if (!btrfs_trans_handle_cachep)
8650 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008651
David Sterba837e1972012-09-07 03:00:48 -06008652 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008653 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008654 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008655 if (!btrfs_path_cachep)
8656 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008657
David Sterba837e1972012-09-07 03:00:48 -06008658 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008659 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008660 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008661 if (!btrfs_free_space_cachep)
8662 goto fail;
8663
Christophe Leroy3acd4852019-08-21 15:05:55 +00008664 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8665 PAGE_SIZE, PAGE_SIZE,
8666 SLAB_RED_ZONE, NULL);
8667 if (!btrfs_free_space_bitmap_cachep)
8668 goto fail;
8669
Chris Mason39279cc2007-06-12 06:35:45 -04008670 return 0;
8671fail:
8672 btrfs_destroy_cachep();
8673 return -ENOMEM;
8674}
8675
David Howellsa528d352017-01-31 16:46:22 +00008676static int btrfs_getattr(const struct path *path, struct kstat *stat,
8677 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008678{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008679 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008680 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008681 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008682 u32 bi_flags = BTRFS_I(inode)->flags;
8683
8684 stat->result_mask |= STATX_BTIME;
8685 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8686 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8687 if (bi_flags & BTRFS_INODE_APPEND)
8688 stat->attributes |= STATX_ATTR_APPEND;
8689 if (bi_flags & BTRFS_INODE_COMPRESS)
8690 stat->attributes |= STATX_ATTR_COMPRESSED;
8691 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8692 stat->attributes |= STATX_ATTR_IMMUTABLE;
8693 if (bi_flags & BTRFS_INODE_NODUMP)
8694 stat->attributes |= STATX_ATTR_NODUMP;
8695
8696 stat->attributes_mask |= (STATX_ATTR_APPEND |
8697 STATX_ATTR_COMPRESSED |
8698 STATX_ATTR_IMMUTABLE |
8699 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008700
Chris Mason39279cc2007-06-12 06:35:45 -04008701 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008702 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008703
8704 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008705 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008706 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008707 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008708 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008709 return 0;
8710}
8711
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008712static int btrfs_rename_exchange(struct inode *old_dir,
8713 struct dentry *old_dentry,
8714 struct inode *new_dir,
8715 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008716{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008717 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008718 struct btrfs_trans_handle *trans;
8719 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008720 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008721 struct inode *new_inode = new_dentry->d_inode;
8722 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008723 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008724 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008725 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8726 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008727 u64 old_idx = 0;
8728 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008729 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008730 bool root_log_pinned = false;
8731 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008732 struct btrfs_log_ctx ctx_root;
8733 struct btrfs_log_ctx ctx_dest;
8734 bool sync_log_root = false;
8735 bool sync_log_dest = false;
8736 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008737
8738 /* we only allow rename subvolume link between subvolumes */
8739 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8740 return -EXDEV;
8741
Filipe Mananad4682ba2018-06-11 19:24:28 +01008742 btrfs_init_log_ctx(&ctx_root, old_inode);
8743 btrfs_init_log_ctx(&ctx_dest, new_inode);
8744
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008745 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008746 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8747 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008748 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008749
8750 /*
8751 * We want to reserve the absolute worst case amount of items. So if
8752 * both inodes are subvols and we need to unlink them then that would
8753 * require 4 item modifications, but if they are both normal inodes it
8754 * would require 5 item modifications, so we'll assume their normal
8755 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8756 * should cover the worst case number of items we'll modify.
8757 */
8758 trans = btrfs_start_transaction(root, 12);
8759 if (IS_ERR(trans)) {
8760 ret = PTR_ERR(trans);
8761 goto out_notrans;
8762 }
8763
Josef Bacik3e174092019-11-15 15:43:06 -05008764 if (dest != root)
8765 btrfs_record_root_in_trans(trans, dest);
8766
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008767 /*
8768 * We need to find a free sequence number both in the source and
8769 * in the destination directory for the exchange.
8770 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008771 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008772 if (ret)
8773 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008774 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008775 if (ret)
8776 goto out_fail;
8777
8778 BTRFS_I(old_inode)->dir_index = 0ULL;
8779 BTRFS_I(new_inode)->dir_index = 0ULL;
8780
8781 /* Reference for the source. */
8782 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8783 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008784 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008785 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008786 btrfs_pin_log_trans(root);
8787 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008788 ret = btrfs_insert_inode_ref(trans, dest,
8789 new_dentry->d_name.name,
8790 new_dentry->d_name.len,
8791 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008792 btrfs_ino(BTRFS_I(new_dir)),
8793 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008794 if (ret)
8795 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008796 }
8797
8798 /* And now for the dest. */
8799 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8800 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008801 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008802 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008803 btrfs_pin_log_trans(dest);
8804 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008805 ret = btrfs_insert_inode_ref(trans, root,
8806 old_dentry->d_name.name,
8807 old_dentry->d_name.len,
8808 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008809 btrfs_ino(BTRFS_I(old_dir)),
8810 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008811 if (ret)
8812 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008813 }
8814
8815 /* Update inode version and ctime/mtime. */
8816 inode_inc_iversion(old_dir);
8817 inode_inc_iversion(new_dir);
8818 inode_inc_iversion(old_inode);
8819 inode_inc_iversion(new_inode);
8820 old_dir->i_ctime = old_dir->i_mtime = ctime;
8821 new_dir->i_ctime = new_dir->i_mtime = ctime;
8822 old_inode->i_ctime = ctime;
8823 new_inode->i_ctime = ctime;
8824
8825 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008826 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8827 BTRFS_I(old_inode), 1);
8828 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8829 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008830 }
8831
8832 /* src is a subvolume */
8833 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008834 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008835 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008836 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8837 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008838 old_dentry->d_name.name,
8839 old_dentry->d_name.len);
8840 if (!ret)
8841 ret = btrfs_update_inode(trans, root, old_inode);
8842 }
8843 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008844 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008845 goto out_fail;
8846 }
8847
8848 /* dest is a subvolume */
8849 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008850 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008851 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008852 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8853 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008854 new_dentry->d_name.name,
8855 new_dentry->d_name.len);
8856 if (!ret)
8857 ret = btrfs_update_inode(trans, dest, new_inode);
8858 }
8859 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008860 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008861 goto out_fail;
8862 }
8863
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008864 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008865 new_dentry->d_name.name,
8866 new_dentry->d_name.len, 0, old_idx);
8867 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008868 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008869 goto out_fail;
8870 }
8871
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008872 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008873 old_dentry->d_name.name,
8874 old_dentry->d_name.len, 0, new_idx);
8875 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008876 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008877 goto out_fail;
8878 }
8879
8880 if (old_inode->i_nlink == 1)
8881 BTRFS_I(old_inode)->dir_index = old_idx;
8882 if (new_inode->i_nlink == 1)
8883 BTRFS_I(new_inode)->dir_index = new_idx;
8884
Filipe Manana86e8aa02016-05-05 02:02:27 +01008885 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008886 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008887 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8888 BTRFS_I(old_dir), parent,
8889 false, &ctx_root);
8890 if (ret == BTRFS_NEED_LOG_SYNC)
8891 sync_log_root = true;
8892 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8893 commit_transaction = true;
8894 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008895 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008896 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008897 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008898 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008899 if (!commit_transaction) {
8900 parent = old_dentry->d_parent;
8901 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8902 BTRFS_I(new_dir), parent,
8903 false, &ctx_dest);
8904 if (ret == BTRFS_NEED_LOG_SYNC)
8905 sync_log_dest = true;
8906 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8907 commit_transaction = true;
8908 ret = 0;
8909 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008910 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008911 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008912 }
8913out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008914 /*
8915 * If we have pinned a log and an error happened, we unpin tasks
8916 * trying to sync the log and force them to fallback to a transaction
8917 * commit if the log currently contains any of the inodes involved in
8918 * this rename operation (to ensure we do not persist a log with an
8919 * inconsistent state for any of these inodes or leading to any
8920 * inconsistencies when replayed). If the transaction was aborted, the
8921 * abortion reason is propagated to userspace when attempting to commit
8922 * the transaction. If the log does not contain any of these inodes, we
8923 * allow the tasks to sync it.
8924 */
8925 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008926 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8927 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8928 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008929 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008930 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008931 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008932
8933 if (root_log_pinned) {
8934 btrfs_end_log_trans(root);
8935 root_log_pinned = false;
8936 }
8937 if (dest_log_pinned) {
8938 btrfs_end_log_trans(dest);
8939 dest_log_pinned = false;
8940 }
8941 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008942 if (!ret && sync_log_root && !commit_transaction) {
8943 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
8944 &ctx_root);
8945 if (ret)
8946 commit_transaction = true;
8947 }
8948 if (!ret && sync_log_dest && !commit_transaction) {
8949 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
8950 &ctx_dest);
8951 if (ret)
8952 commit_transaction = true;
8953 }
8954 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00008955 /*
8956 * We may have set commit_transaction when logging the new name
8957 * in the destination root, in which case we left the source
8958 * root context in the list of log contextes. So make sure we
8959 * remove it to avoid invalid memory accesses, since the context
8960 * was allocated in our stack frame.
8961 */
8962 if (sync_log_root) {
8963 mutex_lock(&root->log_mutex);
8964 list_del_init(&ctx_root.list);
8965 mutex_unlock(&root->log_mutex);
8966 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008967 ret = btrfs_commit_transaction(trans);
8968 } else {
8969 int ret2;
8970
8971 ret2 = btrfs_end_transaction(trans);
8972 ret = ret ? ret : ret2;
8973 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008974out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05008975 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
8976 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008977 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008978
Filipe Mananae6c61712019-11-08 16:11:56 +00008979 ASSERT(list_empty(&ctx_root.list));
8980 ASSERT(list_empty(&ctx_dest.list));
8981
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008982 return ret;
8983}
8984
8985static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
8986 struct btrfs_root *root,
8987 struct inode *dir,
8988 struct dentry *dentry)
8989{
8990 int ret;
8991 struct inode *inode;
8992 u64 objectid;
8993 u64 index;
8994
8995 ret = btrfs_find_free_ino(root, &objectid);
8996 if (ret)
8997 return ret;
8998
8999 inode = btrfs_new_inode(trans, root, dir,
9000 dentry->d_name.name,
9001 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009002 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009003 objectid,
9004 S_IFCHR | WHITEOUT_MODE,
9005 &index);
9006
9007 if (IS_ERR(inode)) {
9008 ret = PTR_ERR(inode);
9009 return ret;
9010 }
9011
9012 inode->i_op = &btrfs_special_inode_operations;
9013 init_special_inode(inode, inode->i_mode,
9014 WHITEOUT_DEV);
9015
9016 ret = btrfs_init_inode_security(trans, inode, dir,
9017 &dentry->d_name);
9018 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009019 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009020
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009021 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9022 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009023 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009024 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009025
9026 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009027out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009028 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009029 if (ret)
9030 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009031 iput(inode);
9032
Filipe Mananac9901612016-05-05 01:41:57 +01009033 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009034}
9035
Chris Mason39279cc2007-06-12 06:35:45 -04009036static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009037 struct inode *new_dir, struct dentry *new_dentry,
9038 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009039{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009040 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009041 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009042 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009043 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9044 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009045 struct inode *new_inode = d_inode(new_dentry);
9046 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009047 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009048 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009049 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009050 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009051 struct btrfs_log_ctx ctx;
9052 bool sync_log = false;
9053 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009054
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009055 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009056 return -EPERM;
9057
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009058 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009059 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009060 return -EXDEV;
9061
Li Zefan33345d012011-04-20 10:31:50 +08009062 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009063 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009064 return -ENOTEMPTY;
9065
Chris Mason39279cc2007-06-12 06:35:45 -04009066 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009067 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009068 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009069
9070
9071 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009072 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009073 new_dentry->d_name.name,
9074 new_dentry->d_name.len);
9075
9076 if (ret) {
9077 if (ret == -EEXIST) {
9078 /* we shouldn't get
9079 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309080 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009081 return ret;
9082 }
9083 } else {
9084 /* maybe -EOVERFLOW */
9085 return ret;
9086 }
9087 }
9088 ret = 0;
9089
Chris Mason5a3f23d2009-03-31 13:27:11 -04009090 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009091 * we're using rename to replace one file with another. Start IO on it
9092 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009093 */
Chris Mason8d875f92014-08-12 10:47:42 -07009094 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009095 filemap_flush(old_inode->i_mapping);
9096
Yan, Zheng76dda932009-09-21 16:00:26 -04009097 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009098 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009099 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009100 /*
9101 * We want to reserve the absolute worst case amount of items. So if
9102 * both inodes are subvols and we need to unlink them then that would
9103 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009104 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009105 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9106 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009107 * If our rename has the whiteout flag, we need more 5 units for the
9108 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9109 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009110 */
Filipe Manana5062af32016-05-05 10:26:26 +01009111 trans_num_items = 11;
9112 if (flags & RENAME_WHITEOUT)
9113 trans_num_items += 5;
9114 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009115 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009116 ret = PTR_ERR(trans);
9117 goto out_notrans;
9118 }
Chris Mason5f39d392007-10-15 16:14:19 -04009119
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009120 if (dest != root)
9121 btrfs_record_root_in_trans(trans, dest);
9122
Nikolay Borisov877574e2017-02-20 13:50:33 +02009123 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009124 if (ret)
9125 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009126
Miao Xie67de1172013-12-26 13:07:06 +08009127 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009128 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009129 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009130 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009131 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009132 btrfs_pin_log_trans(root);
9133 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009134 ret = btrfs_insert_inode_ref(trans, dest,
9135 new_dentry->d_name.name,
9136 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009137 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009138 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009139 if (ret)
9140 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009141 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009142
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009143 inode_inc_iversion(old_dir);
9144 inode_inc_iversion(new_dir);
9145 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009146 old_dir->i_ctime = old_dir->i_mtime =
9147 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009148 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009149
Chris Mason12fcfd22009-03-24 10:24:20 -04009150 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009151 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9152 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009153
Li Zefan33345d012011-04-20 10:31:50 +08009154 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009155 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009156 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009157 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9158 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009159 old_dentry->d_name.name,
9160 old_dentry->d_name.len);
9161 if (!ret)
9162 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009163 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009164 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009165 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009166 goto out_fail;
9167 }
Chris Mason39279cc2007-06-12 06:35:45 -04009168
9169 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009170 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009171 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009172 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009173 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009174 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009175 BUG_ON(new_inode->i_nlink == 0);
9176 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009177 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9178 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009179 new_dentry->d_name.name,
9180 new_dentry->d_name.len);
9181 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009182 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009183 ret = btrfs_orphan_add(trans,
9184 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009185 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009186 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009187 goto out_fail;
9188 }
Chris Mason39279cc2007-06-12 06:35:45 -04009189 }
Josef Bacikaec74772008-07-24 12:12:38 -04009190
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009191 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009192 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009193 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009194 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009195 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009196 goto out_fail;
9197 }
Chris Mason39279cc2007-06-12 06:35:45 -04009198
Miao Xie67de1172013-12-26 13:07:06 +08009199 if (old_inode->i_nlink == 1)
9200 BTRFS_I(old_inode)->dir_index = index;
9201
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009202 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009203 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009204
Filipe Mananad4682ba2018-06-11 19:24:28 +01009205 btrfs_init_log_ctx(&ctx, old_inode);
9206 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9207 BTRFS_I(old_dir), parent,
9208 false, &ctx);
9209 if (ret == BTRFS_NEED_LOG_SYNC)
9210 sync_log = true;
9211 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9212 commit_transaction = true;
9213 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009214 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009215 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009216 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009217
9218 if (flags & RENAME_WHITEOUT) {
9219 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9220 old_dentry);
9221
9222 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009223 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009224 goto out_fail;
9225 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009226 }
Chris Mason39279cc2007-06-12 06:35:45 -04009227out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009228 /*
9229 * If we have pinned the log and an error happened, we unpin tasks
9230 * trying to sync the log and force them to fallback to a transaction
9231 * commit if the log currently contains any of the inodes involved in
9232 * this rename operation (to ensure we do not persist a log with an
9233 * inconsistent state for any of these inodes or leading to any
9234 * inconsistencies when replayed). If the transaction was aborted, the
9235 * abortion reason is propagated to userspace when attempting to commit
9236 * the transaction. If the log does not contain any of these inodes, we
9237 * allow the tasks to sync it.
9238 */
9239 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009240 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9241 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9242 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009243 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009244 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009245 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009246
9247 btrfs_end_log_trans(root);
9248 log_pinned = false;
9249 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009250 if (!ret && sync_log) {
9251 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9252 if (ret)
9253 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009254 } else if (sync_log) {
9255 mutex_lock(&root->log_mutex);
9256 list_del(&ctx.list);
9257 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009258 }
9259 if (commit_transaction) {
9260 ret = btrfs_commit_transaction(trans);
9261 } else {
9262 int ret2;
9263
9264 ret2 = btrfs_end_transaction(trans);
9265 ret = ret ? ret : ret2;
9266 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009267out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009268 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009269 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009270
Chris Mason39279cc2007-06-12 06:35:45 -04009271 return ret;
9272}
9273
Miklos Szeredi80ace852014-07-23 15:15:32 +02009274static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9275 struct inode *new_dir, struct dentry *new_dentry,
9276 unsigned int flags)
9277{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009278 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009279 return -EINVAL;
9280
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009281 if (flags & RENAME_EXCHANGE)
9282 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9283 new_dentry);
9284
9285 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009286}
9287
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009288struct btrfs_delalloc_work {
9289 struct inode *inode;
9290 struct completion completion;
9291 struct list_head list;
9292 struct btrfs_work work;
9293};
9294
Miao Xie8ccf6f192012-10-25 09:28:04 +00009295static void btrfs_run_delalloc_work(struct btrfs_work *work)
9296{
9297 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009298 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009299
9300 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9301 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009302 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009303 filemap_flush(inode->i_mapping);
9304 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9305 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009306 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009307
Nikolay Borisov076da912018-04-23 10:54:16 +03009308 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009309 complete(&delalloc_work->completion);
9310}
9311
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009312static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009313{
9314 struct btrfs_delalloc_work *work;
9315
David Sterba100d5702015-12-08 14:39:32 +01009316 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009317 if (!work)
9318 return NULL;
9319
9320 init_completion(&work->completion);
9321 INIT_LIST_HEAD(&work->list);
9322 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009323 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009324
9325 return work;
9326}
9327
Chris Masond352ac62008-09-29 15:18:18 -04009328/*
9329 * some fairly slow code that needs optimization. This walks the list
9330 * of all the inodes with pending delalloc and forces them to disk.
9331 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009332static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009333{
Chris Masonea8c2812008-08-04 23:17:27 -04009334 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009335 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009336 struct btrfs_delalloc_work *work, *next;
9337 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009338 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009339 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009340
Miao Xie8ccf6f192012-10-25 09:28:04 +00009341 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009342 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009343
Miao Xie573bfb72014-03-06 13:55:03 +08009344 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009345 spin_lock(&root->delalloc_lock);
9346 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009347 while (!list_empty(&splice)) {
9348 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009349 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009350
Miao Xieeb73c1b2013-05-15 07:48:22 +00009351 list_move_tail(&binode->delalloc_inodes,
9352 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009353 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009354 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009355 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009356 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009357 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009358 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009359
Ethan Lien3cd24c62018-11-01 14:49:03 +08009360 if (snapshot)
9361 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9362 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009363 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009364 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009365 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009366 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009367 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009368 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009369 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009370 btrfs_queue_work(root->fs_info->flush_workers,
9371 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009372 ret++;
9373 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009374 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009375 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009376 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009377 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009378 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009379
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009380out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009381 list_for_each_entry_safe(work, next, &works, list) {
9382 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009383 wait_for_completion(&work->completion);
9384 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009385 }
9386
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009387 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009388 spin_lock(&root->delalloc_lock);
9389 list_splice_tail(&splice, &root->delalloc_inodes);
9390 spin_unlock(&root->delalloc_lock);
9391 }
Miao Xie573bfb72014-03-06 13:55:03 +08009392 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009393 return ret;
9394}
9395
Ethan Lien3cd24c62018-11-01 14:49:03 +08009396int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009397{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009398 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009399 int ret;
9400
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009401 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009402 return -EROFS;
9403
Ethan Lien3cd24c62018-11-01 14:49:03 +08009404 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009405 if (ret > 0)
9406 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009407 return ret;
9408}
9409
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009410int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009411{
9412 struct btrfs_root *root;
9413 struct list_head splice;
9414 int ret;
9415
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009416 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009417 return -EROFS;
9418
9419 INIT_LIST_HEAD(&splice);
9420
Miao Xie573bfb72014-03-06 13:55:03 +08009421 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009422 spin_lock(&fs_info->delalloc_root_lock);
9423 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009424 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009425 root = list_first_entry(&splice, struct btrfs_root,
9426 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009427 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009428 BUG_ON(!root);
9429 list_move_tail(&root->delalloc_root,
9430 &fs_info->delalloc_roots);
9431 spin_unlock(&fs_info->delalloc_root_lock);
9432
Ethan Lien3cd24c62018-11-01 14:49:03 +08009433 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009434 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009435 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009436 goto out;
9437
Miao Xie6c255e62014-03-06 13:55:01 +08009438 if (nr != -1) {
9439 nr -= ret;
9440 WARN_ON(nr < 0);
9441 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009442 spin_lock(&fs_info->delalloc_root_lock);
9443 }
9444 spin_unlock(&fs_info->delalloc_root_lock);
9445
Miao Xie6c255e62014-03-06 13:55:01 +08009446 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009447out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009448 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009449 spin_lock(&fs_info->delalloc_root_lock);
9450 list_splice_tail(&splice, &fs_info->delalloc_roots);
9451 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009452 }
Miao Xie573bfb72014-03-06 13:55:03 +08009453 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009454 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009455}
9456
Chris Mason39279cc2007-06-12 06:35:45 -04009457static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9458 const char *symname)
9459{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009460 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009461 struct btrfs_trans_handle *trans;
9462 struct btrfs_root *root = BTRFS_I(dir)->root;
9463 struct btrfs_path *path;
9464 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009465 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009466 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009467 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309468 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009469 int name_len;
9470 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009471 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009472 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009473 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009474
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009475 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009476 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009477 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009478
Josef Bacik9ed74f22009-09-11 16:12:44 -04009479 /*
9480 * 2 items for inode item and ref
9481 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009482 * 1 item for updating parent inode item
9483 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009484 * 1 item for xattr if selinux is on
9485 */
Filipe Manana9269d122015-12-31 18:16:29 +00009486 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009487 if (IS_ERR(trans))
9488 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009489
Li Zefan581bb052011-04-20 10:06:11 +08009490 err = btrfs_find_free_ino(root, &objectid);
9491 if (err)
9492 goto out_unlock;
9493
Josef Bacikaec74772008-07-24 12:12:38 -04009494 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009495 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9496 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009497 if (IS_ERR(inode)) {
9498 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009499 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009500 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009501 }
Chris Mason39279cc2007-06-12 06:35:45 -04009502
Casey Schauflerad19db72011-12-15 10:09:07 -05009503 /*
9504 * If the active LSM wants to access the inode during
9505 * d_instantiate it needs these. Smack checks to see
9506 * if the filesystem supports xattrs by looking at the
9507 * ops vector.
9508 */
9509 inode->i_fop = &btrfs_file_operations;
9510 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009511 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009512 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9513
9514 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9515 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009516 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009517
Chris Mason39279cc2007-06-12 06:35:45 -04009518 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009519 if (!path) {
9520 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009521 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009522 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009523 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009524 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009525 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009526 datasize = btrfs_file_extent_calc_inline_size(name_len);
9527 err = btrfs_insert_empty_item(trans, root, path, &key,
9528 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009529 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009530 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009531 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009532 }
Chris Mason5f39d392007-10-15 16:14:19 -04009533 leaf = path->nodes[0];
9534 ei = btrfs_item_ptr(leaf, path->slots[0],
9535 struct btrfs_file_extent_item);
9536 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9537 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009538 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009539 btrfs_set_file_extent_encryption(leaf, ei, 0);
9540 btrfs_set_file_extent_compression(leaf, ei, 0);
9541 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9542 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9543
Chris Mason39279cc2007-06-12 06:35:45 -04009544 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009545 write_extent_buffer(leaf, symname, ptr, name_len);
9546 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009547 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009548
Chris Mason39279cc2007-06-12 06:35:45 -04009549 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009550 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009551 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009552 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009553 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009554 /*
9555 * Last step, add directory indexes for our symlink inode. This is the
9556 * last step to avoid extra cleanup of these indexes if an error happens
9557 * elsewhere above.
9558 */
9559 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009560 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9561 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009562 if (err)
9563 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009564
Al Viro1e2e5472018-05-04 08:23:01 -04009565 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009566
9567out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009568 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009569 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009570 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009571 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009572 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009573 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009574 return err;
9575}
Chris Mason16432982008-04-10 10:23:21 -04009576
Josef Bacik0af3d002010-06-21 14:48:16 -04009577static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9578 u64 start, u64 num_bytes, u64 min_size,
9579 loff_t actual_len, u64 *alloc_hint,
9580 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009581{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009582 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009583 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9584 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009585 struct btrfs_root *root = BTRFS_I(inode)->root;
9586 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009587 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009588 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009589 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009590 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009591 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009592 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009593 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009594 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009595
Josef Bacik0af3d002010-06-21 14:48:16 -04009596 if (trans)
9597 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009598 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009599 if (own_trans) {
9600 trans = btrfs_start_transaction(root, 3);
9601 if (IS_ERR(trans)) {
9602 ret = PTR_ERR(trans);
9603 break;
9604 }
Yan Zhengd899e052008-10-30 14:25:28 -04009605 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009606
Byongho Leeee221842015-12-15 01:42:10 +09009607 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009608 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009609 /*
9610 * If we are severely fragmented we could end up with really
9611 * small allocations, so if the allocator is returning small
9612 * chunks lets make its job easier by only searching for those
9613 * sized chunks.
9614 */
9615 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009616 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9617 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009618 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009619 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009620 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009621 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009622 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009623
9624 /*
9625 * We've reserved this space, and thus converted it from
9626 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9627 * from here on out we will only need to clear our reservation
9628 * for the remaining unreserved area, so advance our
9629 * clear_offset by our extent size.
9630 */
9631 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009632 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009633
Josef Bacik0b670dc2015-09-23 17:11:16 -04009634 last_alloc = ins.offset;
Yan Zhengd899e052008-10-30 14:25:28 -04009635 ret = insert_reserved_file_extent(trans, inode,
9636 cur_offset, ins.objectid,
9637 ins.offset, ins.offset,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00009638 ins.offset, 0, 0, 0,
Yan Zhengd899e052008-10-30 14:25:28 -04009639 BTRFS_FILE_EXTENT_PREALLOC);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009640 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009641 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009642 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009643 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009644 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009645 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009646 break;
9647 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009648
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009649 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009650 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009651
Josef Bacik5dc562c2012-08-17 13:14:17 -04009652 em = alloc_extent_map();
9653 if (!em) {
9654 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9655 &BTRFS_I(inode)->runtime_flags);
9656 goto next;
9657 }
9658
9659 em->start = cur_offset;
9660 em->orig_start = cur_offset;
9661 em->len = ins.offset;
9662 em->block_start = ins.objectid;
9663 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009664 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009665 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009666 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9667 em->generation = trans->transid;
9668
9669 while (1) {
9670 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009671 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009672 write_unlock(&em_tree->lock);
9673 if (ret != -EEXIST)
9674 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009675 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009676 cur_offset + ins.offset - 1,
9677 0);
9678 }
9679 free_extent_map(em);
9680next:
Yan Zhengd899e052008-10-30 14:25:28 -04009681 num_bytes -= ins.offset;
9682 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009683 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009684
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009685 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009686 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009687 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009688 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009689 (actual_len > inode->i_size) &&
9690 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009691 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009692 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009693 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009694 i_size = cur_offset;
9695 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009696 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009697 }
9698
Yan Zhengd899e052008-10-30 14:25:28 -04009699 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009700
9701 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009702 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009703 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009704 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009705 break;
9706 }
Yan Zhengd899e052008-10-30 14:25:28 -04009707
Josef Bacik0af3d002010-06-21 14:48:16 -04009708 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009709 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009710 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009711 if (clear_offset < end)
9712 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
9713 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009714 return ret;
9715}
9716
Josef Bacik0af3d002010-06-21 14:48:16 -04009717int btrfs_prealloc_file_range(struct inode *inode, int mode,
9718 u64 start, u64 num_bytes, u64 min_size,
9719 loff_t actual_len, u64 *alloc_hint)
9720{
9721 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9722 min_size, actual_len, alloc_hint,
9723 NULL);
9724}
9725
9726int btrfs_prealloc_file_range_trans(struct inode *inode,
9727 struct btrfs_trans_handle *trans, int mode,
9728 u64 start, u64 num_bytes, u64 min_size,
9729 loff_t actual_len, u64 *alloc_hint)
9730{
9731 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9732 min_size, actual_len, alloc_hint, trans);
9733}
9734
Chris Masone6dcd2d2008-07-17 12:53:50 -04009735static int btrfs_set_page_dirty(struct page *page)
9736{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009737 return __set_page_dirty_nobuffers(page);
9738}
9739
Al Viro10556cb22011-06-20 19:28:19 -04009740static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009741{
Li Zefanb83cc962010-12-20 16:04:08 +08009742 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009743 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009744
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009745 if (mask & MAY_WRITE &&
9746 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9747 if (btrfs_root_readonly(root))
9748 return -EROFS;
9749 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9750 return -EACCES;
9751 }
Al Viro2830ba72011-06-20 19:16:29 -04009752 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009753}
Chris Mason39279cc2007-06-12 06:35:45 -04009754
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009755static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9756{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009757 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009758 struct btrfs_trans_handle *trans;
9759 struct btrfs_root *root = BTRFS_I(dir)->root;
9760 struct inode *inode = NULL;
9761 u64 objectid;
9762 u64 index;
9763 int ret = 0;
9764
9765 /*
9766 * 5 units required for adding orphan entry
9767 */
9768 trans = btrfs_start_transaction(root, 5);
9769 if (IS_ERR(trans))
9770 return PTR_ERR(trans);
9771
9772 ret = btrfs_find_free_ino(root, &objectid);
9773 if (ret)
9774 goto out;
9775
9776 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009777 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009778 if (IS_ERR(inode)) {
9779 ret = PTR_ERR(inode);
9780 inode = NULL;
9781 goto out;
9782 }
9783
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009784 inode->i_fop = &btrfs_file_operations;
9785 inode->i_op = &btrfs_file_inode_operations;
9786
9787 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009788 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9789
Chris Masonb0d5d102014-09-08 13:08:51 -07009790 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9791 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009792 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009793
9794 ret = btrfs_update_inode(trans, root, inode);
9795 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009796 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009797 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009798 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009799 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009800
Filipe Manana5762b5c2014-08-01 00:10:32 +01009801 /*
9802 * We set number of links to 0 in btrfs_new_inode(), and here we set
9803 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9804 * through:
9805 *
9806 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9807 */
9808 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009809 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009810 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009811 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009812out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009813 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009814 if (ret && inode)
9815 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009816 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009817 return ret;
9818}
9819
David Sterba5cdc84b2018-07-18 20:32:52 +02009820void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009821{
David Sterba5cdc84b2018-07-18 20:32:52 +02009822 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009823 unsigned long index = start >> PAGE_SHIFT;
9824 unsigned long end_index = end >> PAGE_SHIFT;
9825 struct page *page;
9826
9827 while (index <= end_index) {
9828 page = find_get_page(inode->i_mapping, index);
9829 ASSERT(page); /* Pages should be in the extent_io_tree */
9830 set_page_writeback(page);
9831 put_page(page);
9832 index++;
9833 }
9834}
9835
Omar Sandovaled46ff32016-11-03 10:28:14 -07009836#ifdef CONFIG_SWAP
9837/*
9838 * Add an entry indicating a block group or device which is pinned by a
9839 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9840 * negative errno on failure.
9841 */
9842static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9843 bool is_block_group)
9844{
9845 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9846 struct btrfs_swapfile_pin *sp, *entry;
9847 struct rb_node **p;
9848 struct rb_node *parent = NULL;
9849
9850 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9851 if (!sp)
9852 return -ENOMEM;
9853 sp->ptr = ptr;
9854 sp->inode = inode;
9855 sp->is_block_group = is_block_group;
9856
9857 spin_lock(&fs_info->swapfile_pins_lock);
9858 p = &fs_info->swapfile_pins.rb_node;
9859 while (*p) {
9860 parent = *p;
9861 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9862 if (sp->ptr < entry->ptr ||
9863 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9864 p = &(*p)->rb_left;
9865 } else if (sp->ptr > entry->ptr ||
9866 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9867 p = &(*p)->rb_right;
9868 } else {
9869 spin_unlock(&fs_info->swapfile_pins_lock);
9870 kfree(sp);
9871 return 1;
9872 }
9873 }
9874 rb_link_node(&sp->node, parent, p);
9875 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9876 spin_unlock(&fs_info->swapfile_pins_lock);
9877 return 0;
9878}
9879
9880/* Free all of the entries pinned by this swapfile. */
9881static void btrfs_free_swapfile_pins(struct inode *inode)
9882{
9883 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9884 struct btrfs_swapfile_pin *sp;
9885 struct rb_node *node, *next;
9886
9887 spin_lock(&fs_info->swapfile_pins_lock);
9888 node = rb_first(&fs_info->swapfile_pins);
9889 while (node) {
9890 next = rb_next(node);
9891 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9892 if (sp->inode == inode) {
9893 rb_erase(&sp->node, &fs_info->swapfile_pins);
9894 if (sp->is_block_group)
9895 btrfs_put_block_group(sp->ptr);
9896 kfree(sp);
9897 }
9898 node = next;
9899 }
9900 spin_unlock(&fs_info->swapfile_pins_lock);
9901}
9902
9903struct btrfs_swap_info {
9904 u64 start;
9905 u64 block_start;
9906 u64 block_len;
9907 u64 lowest_ppage;
9908 u64 highest_ppage;
9909 unsigned long nr_pages;
9910 int nr_extents;
9911};
9912
9913static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9914 struct btrfs_swap_info *bsi)
9915{
9916 unsigned long nr_pages;
9917 u64 first_ppage, first_ppage_reported, next_ppage;
9918 int ret;
9919
9920 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9921 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9922 PAGE_SIZE) >> PAGE_SHIFT;
9923
9924 if (first_ppage >= next_ppage)
9925 return 0;
9926 nr_pages = next_ppage - first_ppage;
9927
9928 first_ppage_reported = first_ppage;
9929 if (bsi->start == 0)
9930 first_ppage_reported++;
9931 if (bsi->lowest_ppage > first_ppage_reported)
9932 bsi->lowest_ppage = first_ppage_reported;
9933 if (bsi->highest_ppage < (next_ppage - 1))
9934 bsi->highest_ppage = next_ppage - 1;
9935
9936 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
9937 if (ret < 0)
9938 return ret;
9939 bsi->nr_extents += ret;
9940 bsi->nr_pages += nr_pages;
9941 return 0;
9942}
9943
9944static void btrfs_swap_deactivate(struct file *file)
9945{
9946 struct inode *inode = file_inode(file);
9947
9948 btrfs_free_swapfile_pins(inode);
9949 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
9950}
9951
9952static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
9953 sector_t *span)
9954{
9955 struct inode *inode = file_inode(file);
9956 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9957 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
9958 struct extent_state *cached_state = NULL;
9959 struct extent_map *em = NULL;
9960 struct btrfs_device *device = NULL;
9961 struct btrfs_swap_info bsi = {
9962 .lowest_ppage = (sector_t)-1ULL,
9963 };
9964 int ret = 0;
9965 u64 isize;
9966 u64 start;
9967
9968 /*
9969 * If the swap file was just created, make sure delalloc is done. If the
9970 * file changes again after this, the user is doing something stupid and
9971 * we don't really care.
9972 */
9973 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
9974 if (ret)
9975 return ret;
9976
9977 /*
9978 * The inode is locked, so these flags won't change after we check them.
9979 */
9980 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
9981 btrfs_warn(fs_info, "swapfile must not be compressed");
9982 return -EINVAL;
9983 }
9984 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
9985 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
9986 return -EINVAL;
9987 }
9988 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
9989 btrfs_warn(fs_info, "swapfile must not be checksummed");
9990 return -EINVAL;
9991 }
9992
9993 /*
9994 * Balance or device remove/replace/resize can move stuff around from
9995 * under us. The EXCL_OP flag makes sure they aren't running/won't run
9996 * concurrently while we are mapping the swap extents, and
9997 * fs_info->swapfile_pins prevents them from running while the swap file
9998 * is active and moving the extents. Note that this also prevents a
9999 * concurrent device add which isn't actually necessary, but it's not
10000 * really worth the trouble to allow it.
10001 */
10002 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10003 btrfs_warn(fs_info,
10004 "cannot activate swapfile while exclusive operation is running");
10005 return -EBUSY;
10006 }
10007 /*
10008 * Snapshots can create extents which require COW even if NODATACOW is
10009 * set. We use this counter to prevent snapshots. We must increment it
10010 * before walking the extents because we don't want a concurrent
10011 * snapshot to run after we've already checked the extents.
10012 */
10013 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10014
10015 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10016
10017 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10018 start = 0;
10019 while (start < isize) {
10020 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010021 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010022 u64 len = isize - start;
10023
Omar Sandoval39b07b52019-12-02 17:34:23 -080010024 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010025 if (IS_ERR(em)) {
10026 ret = PTR_ERR(em);
10027 goto out;
10028 }
10029
10030 if (em->block_start == EXTENT_MAP_HOLE) {
10031 btrfs_warn(fs_info, "swapfile must not have holes");
10032 ret = -EINVAL;
10033 goto out;
10034 }
10035 if (em->block_start == EXTENT_MAP_INLINE) {
10036 /*
10037 * It's unlikely we'll ever actually find ourselves
10038 * here, as a file small enough to fit inline won't be
10039 * big enough to store more than the swap header, but in
10040 * case something changes in the future, let's catch it
10041 * here rather than later.
10042 */
10043 btrfs_warn(fs_info, "swapfile must not be inline");
10044 ret = -EINVAL;
10045 goto out;
10046 }
10047 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10048 btrfs_warn(fs_info, "swapfile must not be compressed");
10049 ret = -EINVAL;
10050 goto out;
10051 }
10052
10053 logical_block_start = em->block_start + (start - em->start);
10054 len = min(len, em->len - (start - em->start));
10055 free_extent_map(em);
10056 em = NULL;
10057
10058 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10059 if (ret < 0) {
10060 goto out;
10061 } else if (ret) {
10062 ret = 0;
10063 } else {
10064 btrfs_warn(fs_info,
10065 "swapfile must not be copy-on-write");
10066 ret = -EINVAL;
10067 goto out;
10068 }
10069
10070 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10071 if (IS_ERR(em)) {
10072 ret = PTR_ERR(em);
10073 goto out;
10074 }
10075
10076 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10077 btrfs_warn(fs_info,
10078 "swapfile must have single data profile");
10079 ret = -EINVAL;
10080 goto out;
10081 }
10082
10083 if (device == NULL) {
10084 device = em->map_lookup->stripes[0].dev;
10085 ret = btrfs_add_swapfile_pin(inode, device, false);
10086 if (ret == 1)
10087 ret = 0;
10088 else if (ret)
10089 goto out;
10090 } else if (device != em->map_lookup->stripes[0].dev) {
10091 btrfs_warn(fs_info, "swapfile must be on one device");
10092 ret = -EINVAL;
10093 goto out;
10094 }
10095
10096 physical_block_start = (em->map_lookup->stripes[0].physical +
10097 (logical_block_start - em->start));
10098 len = min(len, em->len - (logical_block_start - em->start));
10099 free_extent_map(em);
10100 em = NULL;
10101
10102 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10103 if (!bg) {
10104 btrfs_warn(fs_info,
10105 "could not find block group containing swapfile");
10106 ret = -EINVAL;
10107 goto out;
10108 }
10109
10110 ret = btrfs_add_swapfile_pin(inode, bg, true);
10111 if (ret) {
10112 btrfs_put_block_group(bg);
10113 if (ret == 1)
10114 ret = 0;
10115 else
10116 goto out;
10117 }
10118
10119 if (bsi.block_len &&
10120 bsi.block_start + bsi.block_len == physical_block_start) {
10121 bsi.block_len += len;
10122 } else {
10123 if (bsi.block_len) {
10124 ret = btrfs_add_swap_extent(sis, &bsi);
10125 if (ret)
10126 goto out;
10127 }
10128 bsi.start = start;
10129 bsi.block_start = physical_block_start;
10130 bsi.block_len = len;
10131 }
10132
10133 start += len;
10134 }
10135
10136 if (bsi.block_len)
10137 ret = btrfs_add_swap_extent(sis, &bsi);
10138
10139out:
10140 if (!IS_ERR_OR_NULL(em))
10141 free_extent_map(em);
10142
10143 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10144
10145 if (ret)
10146 btrfs_swap_deactivate(file);
10147
10148 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10149
10150 if (ret)
10151 return ret;
10152
10153 if (device)
10154 sis->bdev = device->bdev;
10155 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10156 sis->max = bsi.nr_pages;
10157 sis->pages = bsi.nr_pages - 1;
10158 sis->highest_bit = bsi.nr_pages - 1;
10159 return bsi.nr_extents;
10160}
10161#else
10162static void btrfs_swap_deactivate(struct file *file)
10163{
10164}
10165
10166static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10167 sector_t *span)
10168{
10169 return -EOPNOTSUPP;
10170}
10171#endif
10172
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010173static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010174 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010175 .lookup = btrfs_lookup,
10176 .create = btrfs_create,
10177 .unlink = btrfs_unlink,
10178 .link = btrfs_link,
10179 .mkdir = btrfs_mkdir,
10180 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010181 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010182 .symlink = btrfs_symlink,
10183 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010184 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010185 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010186 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010187 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010188 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010189 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010190 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010191};
Yan, Zheng76dda932009-09-21 16:00:26 -040010192
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010193static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010194 .llseek = generic_file_llseek,
10195 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010196 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010197 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010198 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010199#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010200 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010201#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010202 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010203 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010204};
10205
David Sterba20e55062015-11-19 11:42:28 +010010206static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010207 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010208 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010209 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10210};
10211
Chris Mason35054392009-01-21 13:11:13 -050010212/*
10213 * btrfs doesn't support the bmap operation because swapfiles
10214 * use bmap to make a mapping of extents in the file. They assume
10215 * these extents won't change over the life of the file and they
10216 * use the bmap result to do IO directly to the drive.
10217 *
10218 * the btrfs bmap call would return logical addresses that aren't
10219 * suitable for IO and they also will change frequently as COW
10220 * operations happen. So, swapfile + btrfs == corruption.
10221 *
10222 * For now we're avoiding this by dropping bmap.
10223 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010224static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010225 .readpage = btrfs_readpage,
10226 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010227 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010228 .readahead = btrfs_readahead,
David Sterba55e20bd2020-06-09 19:56:06 +020010229 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010230 .invalidatepage = btrfs_invalidatepage,
10231 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010232#ifdef CONFIG_MIGRATION
10233 .migratepage = btrfs_migratepage,
10234#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010235 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010236 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010237 .swap_activate = btrfs_swap_activate,
10238 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010239};
10240
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010241static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010242 .getattr = btrfs_getattr,
10243 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010244 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010245 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010246 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010247 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010248 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010249 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010250};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010251static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010252 .getattr = btrfs_getattr,
10253 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010254 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010255 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010256 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010257 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010258 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010259};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010260static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010261 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010262 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010263 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010264 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010265 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010266 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010267};
Yan, Zheng76dda932009-09-21 16:00:26 -040010268
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010269const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010270 .d_delete = btrfs_dentry_delete,
10271};