blob: 0d3f0b3c1621df5bca1df73a584941b4cad532cb [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Chris Mason8f18cf12008-04-25 16:53:30 -04006#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05007#include <linux/bio.h>
David Sterba55e20bd2020-06-09 19:56:06 +02008#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080031#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030032#include <linux/sched/mm.h>
David Sterba92d32172018-04-16 21:10:14 +020033#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020034#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040035#include "ctree.h"
36#include "disk-io.h"
37#include "transaction.h"
38#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040040#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040041#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040042#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020043#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040044#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050045#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050046#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080047#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000048#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080049#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040050#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040051#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010052#include "space-info.h"
Chris Mason39279cc2007-06-12 06:35:45 -040053
54struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020055 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040056 struct btrfs_root *root;
57};
58
Filipe Mananaf28a4922015-12-08 19:23:20 +000059struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000060 u64 reserve;
David Sterba55e20bd2020-06-09 19:56:06 +020061 u64 unsubmitted_oe_range_start;
62 u64 unsubmitted_oe_range_end;
63 int overwrite;
Filipe Mananaf28a4922015-12-08 19:23:20 +000064};
65
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070066static const struct inode_operations btrfs_dir_inode_operations;
67static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070071static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010072static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040073
74static struct kmem_cache *btrfs_inode_cachep;
75struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000078struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
Eric Sandeen3972f262013-01-12 02:57:22 +000080static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000081static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040082static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030083static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050084 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030085 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030086 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030087static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
88 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080089 u64 block_len, u64 orig_block_len,
90 u64 ram_bytes, int compress_type,
91 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040092
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030093static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080094 const u64 offset, const u64 bytes,
95 const bool uptodate);
96
97/*
98 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +010099 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800100 *
101 * NOTE: caller must ensure that when an error happens, it can not call
102 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
103 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
104 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200105 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800106 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300107static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200108 struct page *locked_page,
109 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800110{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900111 unsigned long index = offset >> PAGE_SHIFT;
112 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200113 u64 page_start = page_offset(locked_page);
114 u64 page_end = page_start + PAGE_SIZE - 1;
115
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900116 struct page *page;
117
118 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300119 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900120 index++;
121 if (!page)
122 continue;
123 ClearPagePrivate2(page);
124 put_page(page);
125 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200126
127 /*
128 * In case this page belongs to the delalloc range being instantiated
129 * then skip it, since the first page of a range is going to be
130 * properly cleaned up by the caller of run_delalloc_range
131 */
132 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
133 offset += PAGE_SIZE;
134 bytes -= PAGE_SIZE;
135 }
136
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300137 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800138}
139
Eric Sandeen48a3b632013-04-25 20:41:01 +0000140static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400141
Josef Bacik6a3891c2015-03-16 17:38:52 -0400142#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
143void btrfs_test_inode_set_ops(struct inode *inode)
144{
145 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
146}
147#endif
148
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000149static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500150 struct inode *inode, struct inode *dir,
151 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500152{
153 int err;
154
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000155 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500156 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500157 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500158 return err;
159}
160
Chris Masond352ac62008-09-29 15:18:18 -0400161/*
Chris Masonc8b97812008-10-29 14:49:59 -0400162 * this does all the hard work for inserting an inline extent into
163 * the btree. The caller should have done a btrfs_drop_extents so that
164 * no overlapping inline items exist in the btree
165 */
Chris Mason40f76582014-05-21 13:35:51 -0700166static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000167 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 struct btrfs_root *root, struct inode *inode,
169 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000170 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400171 struct page **compressed_pages)
172{
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct extent_buffer *leaf;
174 struct page *page = NULL;
175 char *kaddr;
176 unsigned long ptr;
177 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400178 int ret;
179 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400180 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400181
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800182 ASSERT((compressed_size > 0 && compressed_pages) ||
183 (compressed_size == 0 && !compressed_pages));
184
Li Zefanfe3f5662011-03-28 08:30:38 +0000185 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400186 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400187
Chris Masonc8b97812008-10-29 14:49:59 -0400188 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000189
190 if (!extent_inserted) {
191 struct btrfs_key key;
192 size_t datasize;
193
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200194 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000195 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200196 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000197
198 datasize = btrfs_file_extent_calc_inline_size(cur_size);
199 path->leave_spinning = 1;
200 ret = btrfs_insert_empty_item(trans, root, path, &key,
201 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200202 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000203 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400204 }
205 leaf = path->nodes[0];
206 ei = btrfs_item_ptr(leaf, path->slots[0],
207 struct btrfs_file_extent_item);
208 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
209 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
210 btrfs_set_file_extent_encryption(leaf, ei, 0);
211 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
212 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
213 ptr = btrfs_file_extent_inline_start(ei);
214
Li Zefan261507a02010-12-17 14:21:50 +0800215 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400216 struct page *cpage;
217 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500218 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400219 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500220 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300221 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400222
Cong Wang7ac687d2011-11-25 23:14:28 +0800223 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400224 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800225 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400226
227 i++;
228 ptr += cur_size;
229 compressed_size -= cur_size;
230 }
231 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800232 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400233 } else {
234 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300235 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800237 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100238 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400239 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800240 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300241 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400242 }
243 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000244 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400245
Yan, Zhengc2167752009-11-12 09:34:21 +0000246 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500247 * We align size to sectorsize for inline extents just for simplicity
248 * sake.
249 */
250 size = ALIGN(size, root->fs_info->sectorsize);
251 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
252 if (ret)
253 goto fail;
254
255 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000256 * we're an inline extent, so nobody can
257 * extend the file past i_size without locking
258 * a page we already have locked.
259 *
260 * We must do any isize and inode updates
261 * before we unlock the pages. Otherwise we
262 * could end up racing with unlink.
263 */
Chris Masonc8b97812008-10-29 14:49:59 -0400264 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100265 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000266
Chris Masonc8b97812008-10-29 14:49:59 -0400267fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200268 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400269}
270
271
272/*
273 * conditionally insert an inline extent into the file. This
274 * does the checks required to make sure the data is small enough
275 * to fit as an inline extent.
276 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300277static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400278 u64 end, size_t compressed_size,
279 int compress_type,
280 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400281{
Nikolay Borisova0349402020-06-03 08:55:12 +0300282 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400283 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400284 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300285 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400286 u64 actual_end = min(end + 1, isize);
287 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400288 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400289 u64 data_len = inline_len;
290 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000291 struct btrfs_path *path;
292 int extent_inserted = 0;
293 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400294
295 if (compressed_size)
296 data_len = compressed_size;
297
298 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400299 actual_end > fs_info->sectorsize ||
300 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400301 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400302 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400305 return 1;
306 }
307
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000308 path = btrfs_alloc_path();
309 if (!path)
310 return -ENOMEM;
311
Josef Bacik00361582013-08-14 14:02:47 -0400312 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000313 if (IS_ERR(trans)) {
314 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400315 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000316 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300317 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400318
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000319 if (compressed_size && compressed_pages)
320 extent_item_size = btrfs_file_extent_calc_inline_size(
321 compressed_size);
322 else
323 extent_item_size = btrfs_file_extent_calc_inline_size(
324 inline_len);
325
Nikolay Borisova0349402020-06-03 08:55:12 +0300326 ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end,
327 NULL, 1, 1, extent_item_size,
328 &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400329 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400330 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400331 goto out;
332 }
Chris Masonc8b97812008-10-29 14:49:59 -0400333
334 if (isize > actual_end)
335 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000336 ret = insert_inline_extent(trans, path, extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300337 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400338 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000339 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400340 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400341 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400342 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400343 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400344 ret = 1;
345 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100346 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400347
Nikolay Borisova0349402020-06-03 08:55:12 +0300348 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
349 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400350out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800351 /*
352 * Don't forget to free the reserved space, as for inlined extent
353 * it won't count as data extent, free them directly here.
354 * And at reserve time, it's always aligned to page size, so
355 * just free one page here.
356 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300357 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000358 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400359 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400360 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400361}
362
Chris Mason771ed682008-11-06 22:02:51 -0500363struct async_extent {
364 u64 start;
365 u64 ram_size;
366 u64 compressed_size;
367 struct page **pages;
368 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800369 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500370 struct list_head list;
371};
372
Nikolay Borisov97db1202019-03-12 17:20:24 +0200373struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500374 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500375 struct page *locked_page;
376 u64 start;
377 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600378 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500379 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700380 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500381 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200382 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500383};
384
Nikolay Borisov97db1202019-03-12 17:20:24 +0200385struct async_cow {
386 /* Number of chunks in flight; must be first in the structure */
387 atomic_t num_chunks;
388 struct async_chunk chunks[];
389};
390
391static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500392 u64 start, u64 ram_size,
393 u64 compressed_size,
394 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800395 unsigned long nr_pages,
396 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500397{
398 struct async_extent *async_extent;
399
400 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100401 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500402 async_extent->start = start;
403 async_extent->ram_size = ram_size;
404 async_extent->compressed_size = compressed_size;
405 async_extent->pages = pages;
406 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800407 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500408 list_add_tail(&async_extent->list, &cow->extents);
409 return 0;
410}
411
Qu Wenruo42c16da2019-07-01 05:12:46 +0000412/*
413 * Check if the inode has flags compatible with compression
414 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300415static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000416{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300417 if (inode->flags & BTRFS_INODE_NODATACOW ||
418 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000419 return false;
420 return true;
421}
422
423/*
424 * Check if the inode needs to be submitted to compression, based on mount
425 * options, defragmentation, properties or heuristics.
426 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300427static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400429 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800430
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300431 if (!inode_can_compress(BTRFS_I(inode))) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000432 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
433 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
434 btrfs_ino(BTRFS_I(inode)));
435 return 0;
436 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800437 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400438 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800439 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200440 /* defrag ioctl */
441 if (BTRFS_I(inode)->defrag_compress)
442 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800443 /* bad compression ratios */
444 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
445 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400446 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800447 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200448 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300449 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800450 return 0;
451}
452
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200453static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800454 u64 start, u64 end, u64 num_bytes, u64 small_write)
455{
456 /* If this is a small write inside eof, kick off a defrag */
457 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200458 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800459 btrfs_add_inode_defrag(NULL, inode);
460}
461
Chris Masonc8b97812008-10-29 14:49:59 -0400462/*
Chris Mason771ed682008-11-06 22:02:51 -0500463 * we create compressed extents in two phases. The first
464 * phase compresses a range of pages that have already been
465 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400466 *
Chris Mason771ed682008-11-06 22:02:51 -0500467 * This is done inside an ordered work queue, and the compression
468 * is spread across many cpus. The actual IO submission is step
469 * two, and the ordered work queue takes care of making sure that
470 * happens in the same order things were put onto the queue by
471 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400472 *
Chris Mason771ed682008-11-06 22:02:51 -0500473 * If this code finds it can't get good compression, it puts an
474 * entry onto the work queue to write the uncompressed bytes. This
475 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300476 * are written in the same order that the flusher thread sent them
477 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400478 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300479static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400480{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200481 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400482 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400483 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200484 u64 start = async_chunk->start;
485 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400486 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400487 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400488 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400489 struct page **pages = NULL;
490 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400491 unsigned long total_compressed = 0;
492 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400493 int i;
494 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400495 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300496 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400497 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400498
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200499 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
500 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400501
Josef Bacikd98da492019-10-11 09:03:54 -0400502 /*
503 * We need to save i_size before now because it could change in between
504 * us evaluating the size and assigning it. This is because we lock and
505 * unlock the page in truncate and fallocate, and then modify the i_size
506 * later on.
507 *
508 * The barriers are to emulate READ_ONCE, remove that once i_size_read
509 * does that for us.
510 */
511 barrier();
512 i_size = i_size_read(inode);
513 barrier();
514 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400515again:
516 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300517 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100518 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
519 nr_pages = min_t(unsigned long, nr_pages,
520 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400521
Chris Masonf03d9301f2009-02-04 09:31:06 -0500522 /*
523 * we don't want to send crud past the end of i_size through
524 * compression, that's just a waste of CPU time. So, if the
525 * end of the file is before the start of our current
526 * requested range of bytes, we bail out to the uncompressed
527 * cleanup code that can deal with all of this.
528 *
529 * It isn't really the fastest way to fix things, but this is a
530 * very uncommon corner.
531 */
532 if (actual_end <= start)
533 goto cleanup_and_bail_uncompressed;
534
Chris Masonc8b97812008-10-29 14:49:59 -0400535 total_compressed = actual_end - start;
536
Shilong Wang4bcbb332014-10-07 18:44:35 -0400537 /*
538 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400539 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400540 */
541 if (total_compressed <= blocksize &&
542 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
543 goto cleanup_and_bail_uncompressed;
544
David Sterba069eac72017-02-14 19:36:54 +0100545 total_compressed = min_t(unsigned long, total_compressed,
546 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400547 total_in = 0;
548 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400549
Chris Mason771ed682008-11-06 22:02:51 -0500550 /*
551 * we do compression for mount -o compress and when the
552 * inode has not been flagged as nocompress. This flag can
553 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400554 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300555 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400556 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100557 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800558 if (!pages) {
559 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100560 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800561 goto cont;
562 }
Chris Mason179e29e2007-11-01 11:28:41 -0400563
David Sterbaeec63c62017-07-17 19:41:31 +0200564 if (BTRFS_I(inode)->defrag_compress)
565 compress_type = BTRFS_I(inode)->defrag_compress;
566 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200567 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800568
Chris Mason4adaa612013-03-26 13:07:00 -0400569 /*
570 * we need to call clear_page_dirty_for_io on each
571 * page in the range. Otherwise applications with the file
572 * mmap'd can wander in and change the page contents while
573 * we are compressing them.
574 *
575 * If the compression fails for any reason, we set the pages
576 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300577 *
578 * Note that the remaining part is redirtied, the start pointer
579 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400580 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300581 if (!redirty) {
582 extent_range_clear_dirty_for_io(inode, start, end);
583 redirty = 1;
584 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200585
586 /* Compression level is applied here and only here */
587 ret = btrfs_compress_pages(
588 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800589 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100590 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100591 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800592 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100593 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400594
595 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100596 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100597 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400598 char *kaddr;
599
600 /* zero the tail end of the last page, we might be
601 * sending it down to disk
602 */
603 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800604 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400605 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300606 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800607 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400608 }
609 will_compress = 1;
610 }
611 }
Li Zefan560f7d72011-09-08 10:22:01 +0800612cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400613 if (start == 0) {
614 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300615 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400616 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500617 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400618 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300619 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
620 0, BTRFS_COMPRESS_NONE,
621 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 Borisova0349402020-06-03 08:55:12 +0300624 ret = cow_file_range_inline(BTRFS_I(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 Borisovad7ff172020-06-03 08:55:06 +0300646 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
647 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300648 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800649 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400650 PAGE_CLEAR_DIRTY |
651 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100652 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400653 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300654
655 for (i = 0; i < nr_pages; i++) {
656 WARN_ON(pages[i]->mapping);
657 put_page(pages[i]);
658 }
659 kfree(pages);
660
661 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400662 }
663 }
664
665 if (will_compress) {
666 /*
667 * we aren't doing an inline extent round the compressed size
668 * up to a block size boundary so the allocator does sane
669 * things
670 */
Qu Wenruofda28322013-02-26 08:10:22 +0000671 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400672
673 /*
674 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300675 * win, compare the page count read with the blocks on disk,
676 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400677 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300678 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300679 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300680 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700681
682 /*
683 * The async work queues will take care of doing actual
684 * allocation on disk for these compressed pages, and
685 * will submit them to the elevator.
686 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200687 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100688 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700689 compress_type);
690
Timofey Titovets11708622017-10-03 18:06:01 +0300691 if (start + total_in < end) {
692 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700693 pages = NULL;
694 cond_resched();
695 goto again;
696 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300697 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400698 }
699 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700700 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400701 /*
702 * the compression code ran but failed to make things smaller,
703 * free any pages it allocated and our page pointer array
704 */
David Sterba4d3a8002017-02-14 19:04:07 +0100705 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400706 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300707 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400708 }
709 kfree(pages);
710 pages = NULL;
711 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100712 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400713
714 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400715 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200716 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500717 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500718 }
Chris Masonc8b97812008-10-29 14:49:59 -0400719 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500720cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700721 /*
722 * No compression, but we still need to write the pages in the file
723 * we've been given so far. redirty the locked page if it corresponds
724 * to our extent and set things up for the async work queue to run
725 * cow_file_range to do the normal delalloc dance.
726 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700727 if (async_chunk->locked_page &&
728 (page_offset(async_chunk->locked_page) >= start &&
729 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200730 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700731 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700732 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700733
734 if (redirty)
735 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200736 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700737 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300738 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500739
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300740 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500741}
Chris Mason771ed682008-11-06 22:02:51 -0500742
Filipe Manana40ae8372014-10-06 22:14:24 +0100743static void free_async_extent_pages(struct async_extent *async_extent)
744{
745 int i;
746
747 if (!async_extent->pages)
748 return;
749
750 for (i = 0; i < async_extent->nr_pages; i++) {
751 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300752 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100753 }
754 kfree(async_extent->pages);
755 async_extent->nr_pages = 0;
756 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500757}
758
759/*
760 * phase two of compressed writeback. This is the ordered portion
761 * of the code, which only gets called in the order the work was
762 * queued. We walk all the async extents created by compress_file_range
763 * and send them down to the disk.
764 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200765static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500766{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300767 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
768 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500769 struct async_extent *async_extent;
770 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500771 struct btrfs_key ins;
772 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300773 struct btrfs_root *root = inode->root;
774 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500775 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500776
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500777again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200778 while (!list_empty(&async_chunk->extents)) {
779 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500780 struct async_extent, list);
781 list_del(&async_extent->list);
782
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500783retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200784 lock_extent(io_tree, async_extent->start,
785 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500786 /* did the compression code fall back to uncompressed IO? */
787 if (!async_extent->pages) {
788 int page_started = 0;
789 unsigned long nr_written = 0;
790
Chris Mason771ed682008-11-06 22:02:51 -0500791 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300792 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500793 async_extent->start,
794 async_extent->start +
795 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300796 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500797
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100798 /* JDM XXX */
799
Chris Mason771ed682008-11-06 22:02:51 -0500800 /*
801 * if page_started, cow_file_range inserted an
802 * inline extent and took care of all the unlocking
803 * and IO for us. Otherwise, we need to submit
804 * all those pages down to the drive.
805 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500806 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300807 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200808 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500809 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500810 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500811 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700812 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200813 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500814 kfree(async_extent);
815 cond_resched();
816 continue;
817 }
818
Wang Xiaoguang18513092016-07-25 15:51:40 +0800819 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500820 async_extent->compressed_size,
821 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800822 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500823 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100824 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500825
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400826 if (ret == -ENOSPC) {
827 unlock_extent(io_tree, async_extent->start,
828 async_extent->start +
829 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800830
831 /*
832 * we need to redirty the pages if we decide to
833 * fallback to uncompressed IO, otherwise we
834 * will not submit these pages down to lower
835 * layers.
836 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300837 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800838 async_extent->start,
839 async_extent->start +
840 async_extent->ram_size - 1);
841
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100842 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400843 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500844 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500845 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000846 /*
847 * here we're doing allocation and writeback of the
848 * compressed pages
849 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300850 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800851 async_extent->ram_size, /* len */
852 async_extent->start, /* orig_start */
853 ins.objectid, /* block_start */
854 ins.offset, /* block_len */
855 ins.offset, /* orig_block_len */
856 async_extent->ram_size, /* ram_bytes */
857 async_extent->compress_type,
858 BTRFS_ORDERED_COMPRESSED);
859 if (IS_ERR(em))
860 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500861 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800862 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500863
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300864 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800865 async_extent->start,
866 ins.objectid,
867 async_extent->ram_size,
868 ins.offset,
869 BTRFS_ORDERED_COMPRESSED,
870 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100871 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300872 btrfs_drop_extent_cache(inode, 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 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300882 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);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300888 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500889 async_extent->ram_size,
890 ins.objectid,
891 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600892 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700893 async_chunk->write_flags,
894 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100895 struct page *p = async_extent->pages[0];
896 const u64 start = async_extent->start;
897 const u64 end = start + async_extent->ram_size - 1;
898
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300899 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200900 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200901
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100902 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300903 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100904 PAGE_END_WRITEBACK |
905 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100906 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100907 }
Chris Mason771ed682008-11-06 22:02:51 -0500908 alloc_hint = ins.objectid + ins.offset;
909 kfree(async_extent);
910 cond_resched();
911 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100912 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500913out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400914 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400915 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100916out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300917 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500918 async_extent->start +
919 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400920 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100921 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400922 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
923 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100924 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
925 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100926 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100927 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500928 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500929}
930
Nikolay Borisov43c69842020-06-03 08:55:02 +0300931static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400932 u64 num_bytes)
933{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300934 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400935 struct extent_map *em;
936 u64 alloc_hint = 0;
937
938 read_lock(&em_tree->lock);
939 em = search_extent_mapping(em_tree, start, num_bytes);
940 if (em) {
941 /*
942 * if block start isn't an actual block number then find the
943 * first block in this inode and use that as a hint. If that
944 * block is also bogus then just don't worry about it.
945 */
946 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
947 free_extent_map(em);
948 em = search_extent_mapping(em_tree, 0, 0);
949 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
950 alloc_hint = em->block_start;
951 if (em)
952 free_extent_map(em);
953 } else {
954 alloc_hint = em->block_start;
955 free_extent_map(em);
956 }
957 }
958 read_unlock(&em_tree->lock);
959
960 return alloc_hint;
961}
962
Chris Mason771ed682008-11-06 22:02:51 -0500963/*
964 * when extent_io.c finds a delayed allocation range in the file,
965 * the call backs end up in this code. The basic idea is to
966 * allocate extents on disk for the range, and create ordered data structs
967 * in ram to track those extents.
968 *
969 * locked_page is the page that writepage had locked already. We use
970 * it to make sure we don't do extra locks or unlocks.
971 *
972 * *page_started is set to one if we unlock locked_page and do everything
973 * required to start IO on it. It may be clean and already done with
974 * IO when we return.
975 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300976static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -0400977 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300978 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300979 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500980{
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300981 struct btrfs_root *root = inode->root;
982 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500983 u64 alloc_hint = 0;
984 u64 num_bytes;
985 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000986 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +0100987 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400988 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500989 struct btrfs_key ins;
990 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000991 unsigned clear_bits;
992 unsigned long page_ops;
993 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500994 int ret = 0;
995
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300996 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400997 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500998 ret = -EINVAL;
999 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001000 }
Chris Mason771ed682008-11-06 22:02:51 -05001001
Qu Wenruofda28322013-02-26 08:10:22 +00001002 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001003 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001004 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001005
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001006 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001007
Chris Mason771ed682008-11-06 22:02:51 -05001008 if (start == 0) {
1009 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001010 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001011 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001012 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001013 /*
1014 * We use DO_ACCOUNTING here because we need the
1015 * delalloc_release_metadata to be run _after_ we drop
1016 * our outstanding extent for clearing delalloc for this
1017 * range.
1018 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001019 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001020 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001021 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1022 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001023 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1024 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001025 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001026 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001027 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001028 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001029 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001030 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001031 }
1032 }
Chris Masonc8b97812008-10-29 14:49:59 -04001033
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001034 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1035 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001036
Filipe Manana432cd2a2020-06-08 13:32:55 +01001037 /*
1038 * Relocation relies on the relocated extents to have exactly the same
1039 * size as the original extents. Normally writeback for relocation data
1040 * extents follows a NOCOW path because relocation preallocates the
1041 * extents. However, due to an operation such as scrub turning a block
1042 * group to RO mode, it may fallback to COW mode, so we must make sure
1043 * an extent allocated during COW has exactly the requested size and can
1044 * not be split into smaller extents, otherwise relocation breaks and
1045 * fails during the stage where it updates the bytenr of file extent
1046 * items.
1047 */
1048 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1049 min_alloc_size = num_bytes;
1050 else
1051 min_alloc_size = fs_info->sectorsize;
1052
Anand Jain3752d222018-02-15 12:29:38 +08001053 while (num_bytes > 0) {
1054 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001055 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001056 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001057 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001058 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001059 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001060 cur_alloc_size = ins.offset;
1061 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001062
Chris Mason771ed682008-11-06 22:02:51 -05001063 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001064 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001065 start, /* orig_start */
1066 ins.objectid, /* block_start */
1067 ins.offset, /* block_len */
1068 ins.offset, /* orig_block_len */
1069 ram_size, /* ram_bytes */
1070 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001071 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001072 if (IS_ERR(em)) {
1073 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001074 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001075 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001076 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001077
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001078 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1079 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001080 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001081 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001082
Yan Zheng17d217f2008-12-12 10:03:38 -05001083 if (root->root_key.objectid ==
1084 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001085 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001086 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001087 /*
1088 * Only drop cache here, and process as normal.
1089 *
1090 * We must not allow extent_clear_unlock_delalloc()
1091 * at out_unlock label to free meta of this ordered
1092 * extent, as its meta should be freed by
1093 * btrfs_finish_ordered_io().
1094 *
1095 * So we must continue until @start is increased to
1096 * skip current ordered extent.
1097 */
Josef Bacik00361582013-08-14 14:02:47 -04001098 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001099 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001100 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001101 }
1102
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001103 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001104
Chris Masonc8b97812008-10-29 14:49:59 -04001105 /* we're not doing compressed IO, don't unlock the first
1106 * page (which the caller expects to stay locked), don't
1107 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001108 *
1109 * Do set the Private2 bit so we know this page was properly
1110 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001111 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001112 page_ops = unlock ? PAGE_UNLOCK : 0;
1113 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001114
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001115 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001116 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001117 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001118 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001119 if (num_bytes < cur_alloc_size)
1120 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001121 else
Anand Jain3752d222018-02-15 12:29:38 +08001122 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001123 alloc_hint = ins.objectid + ins.offset;
1124 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001125 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001126
1127 /*
1128 * btrfs_reloc_clone_csums() error, since start is increased
1129 * extent_clear_unlock_delalloc() at out_unlock label won't
1130 * free metadata of current ordered extent, we're OK to exit.
1131 */
1132 if (ret)
1133 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001134 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001135out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001136 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001137
Filipe Mananad9f85962014-08-25 10:43:00 +01001138out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001139 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001140out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001141 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001142 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001143out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001144 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1145 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001146 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1147 PAGE_END_WRITEBACK;
1148 /*
1149 * If we reserved an extent for our delalloc range (or a subrange) and
1150 * failed to create the respective ordered extent, then it means that
1151 * when we reserved the extent we decremented the extent's size from
1152 * the data space_info's bytes_may_use counter and incremented the
1153 * space_info's bytes_reserved counter by the same amount. We must make
1154 * sure extent_clear_unlock_delalloc() does not try to decrement again
1155 * the data space_info's bytes_may_use counter, therefore we do not pass
1156 * it the flag EXTENT_CLEAR_DATA_RESV.
1157 */
1158 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001159 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001160 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001161 locked_page,
1162 clear_bits,
1163 page_ops);
1164 start += cur_alloc_size;
1165 if (start >= end)
1166 goto out;
1167 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001168 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001169 clear_bits | EXTENT_CLEAR_DATA_RESV,
1170 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001171 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001172}
Chris Masonc8b97812008-10-29 14:49:59 -04001173
Chris Mason771ed682008-11-06 22:02:51 -05001174/*
1175 * work queue call back to started compression on a file and pages
1176 */
1177static noinline void async_cow_start(struct btrfs_work *work)
1178{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001179 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001180 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001181
Nikolay Borisovb5326272019-03-12 17:20:25 +02001182 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001183
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001184 compressed_extents = compress_file_range(async_chunk);
1185 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001186 btrfs_add_delayed_iput(async_chunk->inode);
1187 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001188 }
Chris Mason771ed682008-11-06 22:02:51 -05001189}
1190
1191/*
1192 * work queue call back to submit previously compressed pages
1193 */
1194static noinline void async_cow_submit(struct btrfs_work *work)
1195{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001196 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1197 work);
1198 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001199 unsigned long nr_pages;
1200
Nikolay Borisovb5326272019-03-12 17:20:25 +02001201 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001202 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001203
David Sterba093258e2018-02-26 16:15:17 +01001204 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001205 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001206 5 * SZ_1M)
1207 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001208
Nikolay Borisov4546d172019-01-03 10:50:03 +02001209 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001210 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001211 * in which case we don't have anything to submit, yet we need to
1212 * always adjust ->async_delalloc_pages as its paired with the init
1213 * happening in cow_file_range_async
1214 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001215 if (async_chunk->inode)
1216 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001217}
Chris Masonc8b97812008-10-29 14:49:59 -04001218
Chris Mason771ed682008-11-06 22:02:51 -05001219static noinline void async_cow_free(struct btrfs_work *work)
1220{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001221 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001222
Nikolay Borisovb5326272019-03-12 17:20:25 +02001223 async_chunk = container_of(work, struct async_chunk, work);
1224 if (async_chunk->inode)
1225 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001226 if (async_chunk->blkcg_css)
1227 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001228 /*
1229 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001230 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001231 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001232 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001233 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001234}
1235
Nikolay Borisov751b6432020-06-03 08:55:22 +03001236static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001237 struct writeback_control *wbc,
1238 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001239 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001240 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001241{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001242 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001243 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001244 struct async_cow *ctx;
1245 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001246 unsigned long nr_pages;
1247 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001248 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1249 int i;
1250 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001251 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001252 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001253
Nikolay Borisov751b6432020-06-03 08:55:22 +03001254 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001255
Nikolay Borisov751b6432020-06-03 08:55:22 +03001256 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001257 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1258 num_chunks = 1;
1259 should_compress = false;
1260 } else {
1261 should_compress = true;
1262 }
1263
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001264 nofs_flag = memalloc_nofs_save();
1265 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1266 memalloc_nofs_restore(nofs_flag);
1267
Nikolay Borisov97db1202019-03-12 17:20:24 +02001268 if (!ctx) {
1269 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1270 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1271 EXTENT_DO_ACCOUNTING;
1272 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1273 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1274 PAGE_SET_ERROR;
1275
Nikolay Borisov751b6432020-06-03 08:55:22 +03001276 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1277 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001278 return -ENOMEM;
1279 }
1280
1281 async_chunk = ctx->chunks;
1282 atomic_set(&ctx->num_chunks, num_chunks);
1283
1284 for (i = 0; i < num_chunks; i++) {
1285 if (should_compress)
1286 cur_end = min(end, start + SZ_512K - 1);
1287 else
1288 cur_end = end;
1289
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001290 /*
1291 * igrab is called higher up in the call chain, take only the
1292 * lightweight reference for the callback lifetime
1293 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001294 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001295 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001296 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001297 async_chunk[i].start = start;
1298 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001299 async_chunk[i].write_flags = write_flags;
1300 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001301
Chris Mason1d53c9e2019-07-10 12:28:16 -07001302 /*
1303 * The locked_page comes all the way from writepage and its
1304 * the original page we were actually given. As we spread
1305 * this large delalloc region across multiple async_chunk
1306 * structs, only the first struct needs a pointer to locked_page
1307 *
1308 * This way we don't need racey decisions about who is supposed
1309 * to unlock it.
1310 */
1311 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001312 /*
1313 * Depending on the compressibility, the pages might or
1314 * might not go through async. We want all of them to
1315 * be accounted against wbc once. Let's do it here
1316 * before the paths diverge. wbc accounting is used
1317 * only for foreign writeback detection and doesn't
1318 * need full accuracy. Just account the whole thing
1319 * against the first page.
1320 */
1321 wbc_account_cgroup_owner(wbc, locked_page,
1322 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001323 async_chunk[i].locked_page = locked_page;
1324 locked_page = NULL;
1325 } else {
1326 async_chunk[i].locked_page = NULL;
1327 }
1328
Chris Masonec39f762019-07-10 12:28:17 -07001329 if (blkcg_css != blkcg_root_css) {
1330 css_get(blkcg_css);
1331 async_chunk[i].blkcg_css = blkcg_css;
1332 } else {
1333 async_chunk[i].blkcg_css = NULL;
1334 }
1335
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001336 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1337 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001338
Nikolay Borisov97db1202019-03-12 17:20:24 +02001339 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001340 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001341
Nikolay Borisov97db1202019-03-12 17:20:24 +02001342 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001343
Chris Mason771ed682008-11-06 22:02:51 -05001344 *nr_written += nr_pages;
1345 start = cur_end + 1;
1346 }
1347 *page_started = 1;
1348 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001349}
1350
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001351static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001352 u64 bytenr, u64 num_bytes)
1353{
1354 int ret;
1355 struct btrfs_ordered_sum *sums;
1356 LIST_HEAD(list);
1357
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001358 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001359 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001360 if (ret == 0 && list_empty(&list))
1361 return 0;
1362
1363 while (!list_empty(&list)) {
1364 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1365 list_del(&sums->list);
1366 kfree(sums);
1367 }
Liu Bo58113752018-01-31 17:09:13 -07001368 if (ret < 0)
1369 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001370 return 1;
1371}
1372
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001373static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001374 const u64 start, const u64 end,
1375 int *page_started, unsigned long *nr_written)
1376{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001377 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1378 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001379 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001380 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001381 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001382 u64 range_start = start;
1383 u64 count;
1384
1385 /*
1386 * If EXTENT_NORESERVE is set it means that when the buffered write was
1387 * made we had not enough available data space and therefore we did not
1388 * reserve data space for it, since we though we could do NOCOW for the
1389 * respective file range (either there is prealloc extent or the inode
1390 * has the NOCOW bit set).
1391 *
1392 * However when we need to fallback to COW mode (because for example the
1393 * block group for the corresponding extent was turned to RO mode by a
1394 * scrub or relocation) we need to do the following:
1395 *
1396 * 1) We increment the bytes_may_use counter of the data space info.
1397 * If COW succeeds, it allocates a new data extent and after doing
1398 * that it decrements the space info's bytes_may_use counter and
1399 * increments its bytes_reserved counter by the same amount (we do
1400 * this at btrfs_add_reserved_bytes()). So we need to increment the
1401 * bytes_may_use counter to compensate (when space is reserved at
1402 * buffered write time, the bytes_may_use counter is incremented);
1403 *
1404 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1405 * that if the COW path fails for any reason, it decrements (through
1406 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1407 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001408 *
1409 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001410 * space cache inode or an inode of the data relocation tree, we must
1411 * also increment bytes_may_use of the data space_info for the same
1412 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001413 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001414 * group that contains that extent to RO mode and therefore force COW
1415 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001416 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001417 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001418 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001419 if (count > 0 || is_space_ino || is_reloc_ino) {
1420 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001421 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001422 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1423
Filipe Manana6bd335b2020-06-08 13:33:05 +01001424 if (is_space_ino || is_reloc_ino)
1425 bytes = range_bytes;
1426
Filipe Manana467dc472020-05-27 11:16:07 +01001427 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001428 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001429 spin_unlock(&sinfo->lock);
1430
Filipe Manana2166e5e2020-05-27 11:16:19 +01001431 if (count > 0)
1432 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1433 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001434 }
1435
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001436 return cow_file_range(inode, locked_page, start, end, page_started,
1437 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001438}
1439
Chris Masond352ac62008-09-29 15:18:18 -04001440/*
1441 * when nowcow writeback call back. This checks for snapshots or COW copies
1442 * of the extents that exist in the file, and COWs the file as required.
1443 *
1444 * If no cow copies or snapshots exist, we write directly to the existing
1445 * blocks on disk
1446 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001447static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001448 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001449 const u64 start, const u64 end,
1450 int *page_started, int force,
1451 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001452{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001453 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1454 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001455 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001456 u64 cow_start = (u64)-1;
1457 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001458 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001459 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001460 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1461 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001462 bool nocow = false;
1463 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001464
1465 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001466 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001467 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001468 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001469 EXTENT_DO_ACCOUNTING |
1470 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001471 PAGE_CLEAR_DIRTY |
1472 PAGE_SET_WRITEBACK |
1473 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001474 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001475 }
Li Zefan82d59022011-04-20 10:33:24 +08001476
Yan Zheng80ff3852008-10-30 14:20:02 -04001477 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001478 struct btrfs_key found_key;
1479 struct btrfs_file_extent_item *fi;
1480 struct extent_buffer *leaf;
1481 u64 extent_end;
1482 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001483 u64 num_bytes = 0;
1484 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001485 u64 ram_bytes;
1486 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001487
1488 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001489
Liu Boe4c3b2d2017-01-30 12:25:28 -08001490 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001491 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001492 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001493 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001494
1495 /*
1496 * If there is no extent for our range when doing the initial
1497 * search, then go back to the previous slot as it will be the
1498 * one containing the search offset
1499 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001500 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1501 leaf = path->nodes[0];
1502 btrfs_item_key_to_cpu(leaf, &found_key,
1503 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001504 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001505 found_key.type == BTRFS_EXTENT_DATA_KEY)
1506 path->slots[0]--;
1507 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001508 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001509next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001510 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001511 leaf = path->nodes[0];
1512 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1513 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001514 if (ret < 0) {
1515 if (cow_start != (u64)-1)
1516 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001517 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001518 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001519 if (ret > 0)
1520 break;
1521 leaf = path->nodes[0];
1522 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001523
Yan Zheng80ff3852008-10-30 14:20:02 -04001524 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001525
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001526 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001527 if (found_key.objectid > ino)
1528 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001529 /*
1530 * Keep searching until we find an EXTENT_ITEM or there are no
1531 * more extents for this inode
1532 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001533 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1534 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1535 path->slots[0]++;
1536 goto next_slot;
1537 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001538
1539 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001540 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001541 found_key.offset > end)
1542 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001543
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001544 /*
1545 * If the found extent starts after requested offset, then
1546 * adjust extent_end to be right before this extent begins
1547 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 if (found_key.offset > cur_offset) {
1549 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001550 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001551 goto out_check;
1552 }
Chris Masonc31f8832008-01-08 15:46:31 -05001553
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001554 /*
1555 * Found extent which begins before our range and potentially
1556 * intersect it
1557 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001558 fi = btrfs_item_ptr(leaf, path->slots[0],
1559 struct btrfs_file_extent_item);
1560 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001561
Josef Bacikcc95bef2013-04-04 14:31:27 -04001562 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001563 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1564 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001565 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001566 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001567 extent_end = found_key.offset +
1568 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001569 disk_num_bytes =
1570 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001571 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001572 * If the extent we got ends before our current offset,
1573 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001574 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001575 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001576 path->slots[0]++;
1577 goto next_slot;
1578 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001579 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001580 if (disk_bytenr == 0)
1581 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001582 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001583 if (btrfs_file_extent_compression(leaf, fi) ||
1584 btrfs_file_extent_encryption(leaf, fi) ||
1585 btrfs_file_extent_other_encoding(leaf, fi))
1586 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001587 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001588 * If extent is created before the last volume's snapshot
1589 * this implies the extent is shared, hence we can't do
1590 * nocow. This is the same check as in
1591 * btrfs_cross_ref_exist but without calling
1592 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001593 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001594 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001595 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001596 btrfs_root_last_snapshot(&root->root_item))
1597 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001598 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1599 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001600 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001601 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001602 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001603 ret = btrfs_cross_ref_exist(root, ino,
1604 found_key.offset -
1605 extent_offset, disk_bytenr);
1606 if (ret) {
1607 /*
1608 * ret could be -EIO if the above fails to read
1609 * metadata.
1610 */
1611 if (ret < 0) {
1612 if (cow_start != (u64)-1)
1613 cur_offset = cow_start;
1614 goto error;
1615 }
1616
Nikolay Borisov3e024842019-08-21 10:42:03 +03001617 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001618 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001619 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001620 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001621 disk_bytenr += cur_offset - found_key.offset;
1622 num_bytes = min(end + 1, extent_end) - cur_offset;
1623 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001624 * If there are pending snapshots for this root, we
1625 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001626 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001627 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001628 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001629 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001630 * force cow if csum exists in the range.
1631 * this ensure that csum for a given extent are
1632 * either valid or do not exist.
1633 */
Liu Bo58113752018-01-31 17:09:13 -07001634 ret = csum_exist_in_range(fs_info, disk_bytenr,
1635 num_bytes);
1636 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001637 /*
1638 * ret could be -EIO if the above fails to read
1639 * metadata.
1640 */
1641 if (ret < 0) {
1642 if (cow_start != (u64)-1)
1643 cur_offset = cow_start;
1644 goto error;
1645 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001646 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001647 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001648 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001649 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001650 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001651 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001652 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001653 extent_end = found_key.offset + ram_bytes;
1654 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001655 /* Skip extents outside of our requested range */
1656 if (extent_end <= start) {
1657 path->slots[0]++;
1658 goto next_slot;
1659 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001660 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001661 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001662 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001663 }
1664out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001665 /*
1666 * If nocow is false then record the beginning of the range
1667 * that needs to be COWed
1668 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001669 if (!nocow) {
1670 if (cow_start == (u64)-1)
1671 cow_start = cur_offset;
1672 cur_offset = extent_end;
1673 if (cur_offset > end)
1674 break;
1675 path->slots[0]++;
1676 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001677 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001678
David Sterbab3b4aa72011-04-21 01:20:15 +02001679 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001680
1681 /*
1682 * COW range from cow_start to found_key.offset - 1. As the key
1683 * will contain the beginning of the first extent that can be
1684 * NOCOW, following one which needs to be COW'ed
1685 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001686 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001687 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001688 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001689 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001690 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001691 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001692 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001693 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001694
Yan Zhengd899e052008-10-30 14:25:28 -04001695 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001696 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001697 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001698
Nikolay Borisov968322c2020-06-03 08:55:21 +03001699 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001700 orig_start,
1701 disk_bytenr, /* block_start */
1702 num_bytes, /* block_len */
1703 disk_num_bytes, /* orig_block_len */
1704 ram_bytes, BTRFS_COMPRESS_NONE,
1705 BTRFS_ORDERED_PREALLOC);
1706 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001707 ret = PTR_ERR(em);
1708 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001709 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001710 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001711 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001712 disk_bytenr, num_bytes,
1713 num_bytes,
1714 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001715 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001716 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001717 cur_offset + num_bytes - 1,
1718 0);
1719 goto error;
1720 }
Yan Zhengd899e052008-10-30 14:25:28 -04001721 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001722 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001723 disk_bytenr, num_bytes,
1724 num_bytes,
1725 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001726 if (ret)
1727 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001728 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001729
Filipe Mananaf78c4362016-05-09 13:15:41 +01001730 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001731 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001732 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001733
Yan, Zhengefa56462010-05-16 10:49:59 -04001734 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001735 BTRFS_DATA_RELOC_TREE_OBJECTID)
1736 /*
1737 * Error handled later, as we must prevent
1738 * extent_clear_unlock_delalloc() in error handler
1739 * from freeing metadata of created ordered extent.
1740 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001741 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001742 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001743
Nikolay Borisov968322c2020-06-03 08:55:21 +03001744 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001745 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001746 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001747 EXTENT_DELALLOC |
1748 EXTENT_CLEAR_DATA_RESV,
1749 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1750
Yan Zheng80ff3852008-10-30 14:20:02 -04001751 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001752
1753 /*
1754 * btrfs_reloc_clone_csums() error, now we're OK to call error
1755 * handler, as metadata for created ordered extent will only
1756 * be freed by btrfs_finish_ordered_io().
1757 */
1758 if (ret)
1759 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001760 if (cur_offset > end)
1761 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001762 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001763 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001764
Robbie Ko506481b2018-10-30 18:04:04 +08001765 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001766 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001767
Yan Zheng80ff3852008-10-30 14:20:02 -04001768 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001769 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001770 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1771 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001772 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001773 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001774 }
1775
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001776error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001777 if (nocow)
1778 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1779
Josef Bacik17ca04a2012-05-31 15:58:55 -04001780 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001781 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001782 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001783 EXTENT_DELALLOC | EXTENT_DEFRAG |
1784 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1785 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001786 PAGE_SET_WRITEBACK |
1787 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001788 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001789 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001790}
1791
Wang Shilong47059d92014-07-03 18:22:07 +08001792static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1793{
1794
1795 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1796 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1797 return 0;
1798
1799 /*
1800 * @defrag_bytes is a hint value, no spinlock held here,
1801 * if is not zero, it means the file is defragging.
1802 * Force cow if given extent needs to be defragged.
1803 */
1804 if (BTRFS_I(inode)->defrag_bytes &&
1805 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1806 EXTENT_DEFRAG, 0, NULL))
1807 return 1;
1808
1809 return 0;
1810}
1811
Chris Masond352ac62008-09-29 15:18:18 -04001812/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001813 * Function to process delayed allocation (create CoW) for ranges which are
1814 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001815 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001816int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001817 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1818 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001819{
Chris Masonbe20aa92007-12-17 20:14:01 -05001820 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001821 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001822
Wang Shilong47059d92014-07-03 18:22:07 +08001823 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001824 ret = run_delalloc_nocow(BTRFS_I(inode), locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001825 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001826 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001827 ret = run_delalloc_nocow(BTRFS_I(inode), locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001828 page_started, 0, nr_written);
Nikolay Borisov99c88dc2020-06-03 08:55:26 +03001829 } else if (!inode_can_compress(BTRFS_I(inode)) ||
Qu Wenruo42c16da2019-07-01 05:12:46 +00001830 !inode_need_compress(inode, start, end)) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001831 ret = cow_file_range(BTRFS_I(inode), locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001832 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001833 } else {
1834 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1835 &BTRFS_I(inode)->runtime_flags);
Nikolay Borisov751b6432020-06-03 08:55:22 +03001836 ret = cow_file_range_async(BTRFS_I(inode), wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001837 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001838 }
Qu Wenruo524272602017-03-08 10:25:52 +08001839 if (ret)
Nikolay Borisov64e1db52020-06-03 08:55:25 +03001840 btrfs_cleanup_ordered_extents(BTRFS_I(inode), locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001841 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001842 return ret;
1843}
1844
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001845void btrfs_split_delalloc_extent(struct inode *inode,
1846 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001847{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001848 u64 size;
1849
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001850 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001851 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001852 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001853
Josef Bacikdcab6a32015-02-11 15:08:59 -05001854 size = orig->end - orig->start + 1;
1855 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001856 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001857 u64 new_size;
1858
1859 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001860 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001861 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001862 */
1863 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001864 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001865 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001866 num_extents += count_max_extents(new_size);
1867 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001868 return;
1869 }
1870
Josef Bacik9e0baf62011-07-15 15:16:44 +00001871 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001872 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001873 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001874}
1875
1876/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001877 * Handle merged delayed allocation extents so we can keep track of new extents
1878 * that are just merged onto old extents, such as when we are doing sequential
1879 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001880 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001881void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1882 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001883{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001884 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001885 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001886
Josef Bacik9ed74f22009-09-11 16:12:44 -04001887 /* not delalloc, ignore it */
1888 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001889 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001890
Josef Bacik8461a3d2015-03-13 15:12:08 -04001891 if (new->start > other->start)
1892 new_size = new->end - other->start + 1;
1893 else
1894 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001895
1896 /* we're not bigger than the max, unreserve the space and go */
1897 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1898 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001899 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001900 spin_unlock(&BTRFS_I(inode)->lock);
1901 return;
1902 }
1903
1904 /*
Josef Bacikba117212015-03-13 15:01:24 -04001905 * We have to add up either side to figure out how many extents were
1906 * accounted for before we merged into one big extent. If the number of
1907 * extents we accounted for is <= the amount we need for the new range
1908 * then we can return, otherwise drop. Think of it like this
1909 *
1910 * [ 4k][MAX_SIZE]
1911 *
1912 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1913 * need 2 outstanding extents, on one side we have 1 and the other side
1914 * we have 1 so they are == and we can return. But in this case
1915 *
1916 * [MAX_SIZE+4k][MAX_SIZE+4k]
1917 *
1918 * Each range on their own accounts for 2 extents, but merged together
1919 * they are only 3 extents worth of accounting, so we need to drop in
1920 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001921 */
Josef Bacikba117212015-03-13 15:01:24 -04001922 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001923 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001924 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001925 num_extents += count_max_extents(old_size);
1926 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001927 return;
1928
Josef Bacik9e0baf62011-07-15 15:16:44 +00001929 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001930 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001931 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001932}
1933
Miao Xieeb73c1b2013-05-15 07:48:22 +00001934static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1935 struct inode *inode)
1936{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001937 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1938
Miao Xieeb73c1b2013-05-15 07:48:22 +00001939 spin_lock(&root->delalloc_lock);
1940 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1941 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1942 &root->delalloc_inodes);
1943 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1944 &BTRFS_I(inode)->runtime_flags);
1945 root->nr_delalloc_inodes++;
1946 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001947 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001948 BUG_ON(!list_empty(&root->delalloc_root));
1949 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001950 &fs_info->delalloc_roots);
1951 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001952 }
1953 }
1954 spin_unlock(&root->delalloc_lock);
1955}
1956
Nikolay Borisov2b877332018-04-27 12:21:51 +03001957
1958void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1959 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001960{
David Sterba3ffbd682018-06-29 10:56:42 +02001961 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001962
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001963 if (!list_empty(&inode->delalloc_inodes)) {
1964 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001965 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001966 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001967 root->nr_delalloc_inodes--;
1968 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001969 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001970 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001971 BUG_ON(list_empty(&root->delalloc_root));
1972 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001973 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001974 }
1975 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001976}
1977
1978static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1979 struct btrfs_inode *inode)
1980{
1981 spin_lock(&root->delalloc_lock);
1982 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001983 spin_unlock(&root->delalloc_lock);
1984}
1985
Chris Masond352ac62008-09-29 15:18:18 -04001986/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001987 * Properly track delayed allocation bytes in the inode and to maintain the
1988 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001989 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001990void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1991 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001992{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001993 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1994
Wang Shilong47059d92014-07-03 18:22:07 +08001995 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1996 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001997 /*
1998 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001999 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002000 * bit, which is only set or cleared with irqs on
2001 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002002 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002003 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002004 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002005 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002006 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002007
Josef Bacik8b62f872017-10-19 14:15:55 -04002008 spin_lock(&BTRFS_I(inode)->lock);
2009 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2010 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002011
Josef Bacik6a3891c2015-03-16 17:38:52 -04002012 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002013 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002014 return;
2015
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002016 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2017 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002018 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002019 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002020 if (*bits & EXTENT_DEFRAG)
2021 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002022 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002023 &BTRFS_I(inode)->runtime_flags))
2024 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002025 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002026 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002027
2028 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2029 (*bits & EXTENT_DELALLOC_NEW)) {
2030 spin_lock(&BTRFS_I(inode)->lock);
2031 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2032 state->start;
2033 spin_unlock(&BTRFS_I(inode)->lock);
2034 }
Chris Mason291d6732008-01-29 15:55:23 -05002035}
2036
Chris Masond352ac62008-09-29 15:18:18 -04002037/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002038 * Once a range is no longer delalloc this function ensures that proper
2039 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002040 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002041void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2042 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002043{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002044 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2045 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002046 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002047 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002048
Filipe Manana4a4b9642017-07-27 19:52:55 +01002049 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2050 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002051 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002052 spin_unlock(&inode->lock);
2053 }
Wang Shilong47059d92014-07-03 18:22:07 +08002054
Chris Mason75eff682008-12-15 15:54:40 -05002055 /*
2056 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002057 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002058 * bit, which is only set or cleared with irqs on
2059 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002060 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002061 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002062 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002063
Josef Bacik8b62f872017-10-19 14:15:55 -04002064 spin_lock(&inode->lock);
2065 btrfs_mod_outstanding_extents(inode, -num_extents);
2066 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002067
Josef Bacikb6d08f02013-09-27 14:57:43 -04002068 /*
2069 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002070 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002071 * error.
2072 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002073 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002074 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002075 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002076
Josef Bacik6a3891c2015-03-16 17:38:52 -04002077 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002078 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002079 return;
2080
Filipe Mananaa315e682017-03-06 23:04:20 +00002081 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2082 do_list && !(state->state & EXTENT_NORESERVE) &&
2083 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002084 btrfs_free_reserved_data_space_noquota(
2085 &inode->vfs_inode,
Filipe Manana46d4dac2020-06-09 11:19:33 +01002086 len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002087
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002088 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2089 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002090 spin_lock(&inode->lock);
2091 inode->delalloc_bytes -= len;
2092 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002093 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002094 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002095 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002096 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002097 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002098
2099 if ((state->state & EXTENT_DELALLOC_NEW) &&
2100 (*bits & EXTENT_DELALLOC_NEW)) {
2101 spin_lock(&inode->lock);
2102 ASSERT(inode->new_delalloc_bytes >= len);
2103 inode->new_delalloc_bytes -= len;
2104 spin_unlock(&inode->lock);
2105 }
Chris Mason291d6732008-01-29 15:55:23 -05002106}
2107
Chris Masond352ac62008-09-29 15:18:18 -04002108/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002109 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2110 * in a chunk's stripe. This function ensures that bios do not span a
2111 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002112 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002113 * @page - The page we are about to add to the bio
2114 * @size - size we want to add to the bio
2115 * @bio - bio we want to ensure is smaller than a stripe
2116 * @bio_flags - flags of the bio
2117 *
2118 * return 1 if page cannot be added to the bio
2119 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002120 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002121 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002122int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2123 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002124{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002125 struct inode *inode = page->mapping->host;
2126 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002127 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002128 u64 length = 0;
2129 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002130 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002131 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002132
Chris Mason771ed682008-11-06 22:02:51 -05002133 if (bio_flags & EXTENT_BIO_COMPRESSED)
2134 return 0;
2135
Kent Overstreet4f024f32013-10-11 15:44:27 -07002136 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002137 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002138 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2139 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002140 if (ret < 0)
2141 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002142
2143 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002144 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002145 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002146}
2147
Chris Masond352ac62008-09-29 15:18:18 -04002148/*
2149 * in order to insert checksums into the metadata in large chunks,
2150 * we wait until bio submission time. All the pages in the bio are
2151 * checksummed and sums are attached onto the ordered extent record.
2152 *
2153 * At IO completion time the cums attached on the ordered extent record
2154 * are inserted into the btree
2155 */
David Sterbad0ee3932018-03-08 14:35:48 +01002156static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002157 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002158{
Josef Bacikc6100a42017-05-05 11:57:13 -04002159 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002160 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002161
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002162 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002163 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002164 return 0;
2165}
Chris Masone0156402008-04-16 11:15:20 -04002166
Chris Mason4a69a412008-11-06 22:03:00 -05002167/*
Chris Masoncad321a2008-12-17 14:51:42 -05002168 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002169 * on write, or reading the csums from the tree before a read.
2170 *
2171 * Rules about async/sync submit,
2172 * a) read: sync submit
2173 *
2174 * b) write without checksum: sync submit
2175 *
2176 * c) write with checksum:
2177 * c-1) if bio is issued by fsync: sync submit
2178 * (sync_writers != 0)
2179 *
2180 * c-2) if root is reloc root: sync submit
2181 * (only in case of buffered IO)
2182 *
2183 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002184 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002185static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002186 int mirror_num,
2187 unsigned long bio_flags)
2188
Chris Mason44b8bd72008-04-16 11:14:51 -04002189{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002190 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002191 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302192 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002193 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002194 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002195 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002196
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002197 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002198
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002199 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302200 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002201
Mike Christie37226b22016-06-05 14:31:52 -05002202 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002203 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002204 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002205 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002206
Chris Masond20f7042008-12-08 16:58:54 -05002207 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002208 ret = btrfs_submit_compressed_read(inode, bio,
2209 mirror_num,
2210 bio_flags);
2211 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002212 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002213 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002214 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002215 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002216 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002217 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002218 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002219 /* csum items have already been cloned */
2220 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2221 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002222 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002223 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002224 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002225 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002226 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002227 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002228 if (ret)
2229 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002230 }
2231
Chris Mason0b86a832008-03-24 15:01:56 -04002232mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002233 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002234
2235out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002236 if (ret) {
2237 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002238 bio_endio(bio);
2239 }
Stefan Behrens61891922012-11-05 18:51:52 +01002240 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002241}
Chris Mason6885f302008-02-20 16:11:05 -05002242
Chris Masond352ac62008-09-29 15:18:18 -04002243/*
2244 * given a list of ordered sums record them in the inode. This happens
2245 * at IO completion time based on sums calculated at bio submission time.
2246 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002247static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002248 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002249{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002250 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002251 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002252
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002253 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002254 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002255 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002256 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002257 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002258 if (ret)
2259 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002260 }
2261 return 0;
2262}
2263
Josef Bacik2ac55d42010-02-03 19:33:23 +00002264int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002265 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002266 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002267{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002268 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002269 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002270 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002271}
2272
Chris Masond352ac62008-09-29 15:18:18 -04002273/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002274struct btrfs_writepage_fixup {
2275 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002276 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002277 struct btrfs_work work;
2278};
2279
Christoph Hellwigb2950862008-12-02 09:54:17 -05002280static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002281{
2282 struct btrfs_writepage_fixup *fixup;
2283 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002284 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002285 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002286 struct page *page;
2287 struct inode *inode;
2288 u64 page_start;
2289 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002290 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002291 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002292
2293 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2294 page = fixup->page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002295 inode = fixup->inode;
2296 page_start = page_offset(page);
2297 page_end = page_offset(page) + PAGE_SIZE - 1;
2298
2299 /*
2300 * This is similar to page_mkwrite, we need to reserve the space before
2301 * we take the page lock.
2302 */
2303 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2304 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002305again:
Chris Mason247e7432008-07-17 12:53:51 -04002306 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002307
Chris Mason25f3c502020-01-21 11:51:42 -05002308 /*
2309 * Before we queued this fixup, we took a reference on the page.
2310 * page->mapping may go NULL, but it shouldn't be moved to a different
2311 * address space.
2312 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002313 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2314 /*
2315 * Unfortunately this is a little tricky, either
2316 *
2317 * 1) We got here and our page had already been dealt with and
2318 * we reserved our space, thus ret == 0, so we need to just
2319 * drop our space reservation and bail. This can happen the
2320 * first time we come into the fixup worker, or could happen
2321 * while waiting for the ordered extent.
2322 * 2) Our page was already dealt with, but we happened to get an
2323 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2324 * this case we obviously don't have anything to release, but
2325 * because the page was already dealt with we don't want to
2326 * mark the page with an error, so make sure we're resetting
2327 * ret to 0. This is why we have this check _before_ the ret
2328 * check, because we do not want to have a surprise ENOSPC
2329 * when the page was already properly dealt with.
2330 */
2331 if (!ret) {
2332 btrfs_delalloc_release_extents(BTRFS_I(inode),
2333 PAGE_SIZE);
2334 btrfs_delalloc_release_space(inode, data_reserved,
2335 page_start, PAGE_SIZE,
2336 true);
2337 }
2338 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002339 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002340 }
Chris Mason25f3c502020-01-21 11:51:42 -05002341
2342 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002343 * We can't mess with the page state unless it is locked, so now that
2344 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002345 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002346 if (ret)
2347 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002348
David Sterbaff13db42015-12-03 14:30:40 +01002349 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002350 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002351
2352 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002353 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002354 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002355
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002356 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002357 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002358 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002359 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002360 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002361 unlock_page(page);
2362 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002363 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002364 goto again;
2365 }
Chris Mason247e7432008-07-17 12:53:51 -04002366
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002367 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002368 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002369 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002370 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002371
Chris Mason25f3c502020-01-21 11:51:42 -05002372 /*
2373 * Everything went as planned, we're now the owner of a dirty page with
2374 * delayed allocation bits set and space reserved for our COW
2375 * destination.
2376 *
2377 * The page was dirty when we started, nothing should have cleaned it.
2378 */
2379 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002380 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002381out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002382 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002383 if (free_delalloc_space)
Filipe Manana53687002019-10-09 17:43:59 +01002384 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2385 PAGE_SIZE, true);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002386 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002387 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002388out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002389 if (ret) {
2390 /*
2391 * We hit ENOSPC or other errors. Update the mapping and page
2392 * to reflect the errors and clean the page.
2393 */
2394 mapping_set_error(page->mapping, ret);
2395 end_extent_writepage(page, ret, page_start, page_end);
2396 clear_page_dirty_for_io(page);
2397 SetPageError(page);
2398 }
2399 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002400 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002401 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002402 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002403 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002404 /*
2405 * As a precaution, do a delayed iput in case it would be the last iput
2406 * that could need flushing space. Recursing back to fixup worker would
2407 * deadlock.
2408 */
2409 btrfs_add_delayed_iput(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002410}
2411
2412/*
2413 * There are a few paths in the higher layers of the kernel that directly
2414 * set the page dirty bit without asking the filesystem if it is a
2415 * good idea. This causes problems because we want to make sure COW
2416 * properly happens and the data=ordered rules are followed.
2417 *
Chris Masonc8b97812008-10-29 14:49:59 -04002418 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002419 * hasn't been properly setup for IO. We kick off an async process
2420 * to fix it up. The async helper will wait for ordered extents, set
2421 * the delalloc bit and make it safe to write the page.
2422 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002423int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002424{
2425 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002426 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002427 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002428
Chris Mason8b62b722009-09-02 16:53:46 -04002429 /* this page is properly in the ordered list */
2430 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002431 return 0;
2432
Chris Mason25f3c502020-01-21 11:51:42 -05002433 /*
2434 * PageChecked is set below when we create a fixup worker for this page,
2435 * don't try to create another one if we're already PageChecked()
2436 *
2437 * The extent_io writepage code will redirty the page if we send back
2438 * EAGAIN.
2439 */
Chris Mason247e7432008-07-17 12:53:51 -04002440 if (PageChecked(page))
2441 return -EAGAIN;
2442
2443 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2444 if (!fixup)
2445 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002446
Josef Bacikf4b13632020-01-21 14:34:52 -05002447 /*
2448 * We are already holding a reference to this inode from
2449 * write_cache_pages. We need to hold it because the space reservation
2450 * takes place outside of the page lock, and we can't trust
2451 * page->mapping outside of the page lock.
2452 */
2453 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002454 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002455 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002456 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002457 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002458 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002459 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002460
2461 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002462}
2463
Yan Zhengd899e052008-10-30 14:25:28 -04002464static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002465 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002466 struct btrfs_file_extent_item *stack_fi,
2467 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002468{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002469 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002470 struct btrfs_path *path;
2471 struct extent_buffer *leaf;
2472 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002473 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2474 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2475 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2476 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002477 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002478 int ret;
2479
2480 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002481 if (!path)
2482 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002483
Chris Masona1ed8352009-09-11 12:27:37 -04002484 /*
2485 * we may be replacing one extent in the tree with another.
2486 * The new extent is pinned in the extent map, and we don't want
2487 * to drop it from the cache until it is completely in the btree.
2488 *
2489 * So, tell btrfs_drop_extents to leave this extent in the cache.
2490 * the caller is expected to unpin it and allow it to be merged
2491 * with the others.
2492 */
Nikolay Borisovc553f942020-06-03 08:55:19 +03002493 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002494 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002495 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002496 if (ret)
2497 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002498
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002499 if (!extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002500 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002501 ins.offset = file_pos;
2502 ins.type = BTRFS_EXTENT_DATA_KEY;
2503
2504 path->leave_spinning = 1;
2505 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002506 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002507 if (ret)
2508 goto out;
2509 }
Yan Zhengd899e052008-10-30 14:25:28 -04002510 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002511 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2512 write_extent_buffer(leaf, stack_fi,
2513 btrfs_item_ptr_offset(leaf, path->slots[0]),
2514 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002515
Yan Zhengd899e052008-10-30 14:25:28 -04002516 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002517 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002518
Nikolay Borisovc553f942020-06-03 08:55:19 +03002519 inode_add_bytes(&inode->vfs_inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002520
2521 ins.objectid = disk_bytenr;
2522 ins.offset = disk_num_bytes;
2523 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002524
Nikolay Borisovc553f942020-06-03 08:55:19 +03002525 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002526 if (ret)
2527 goto out;
2528
Nikolay Borisovc553f942020-06-03 08:55:19 +03002529 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002530 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002531out:
Yan Zhengd899e052008-10-30 14:25:28 -04002532 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002533
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002534 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002535}
2536
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002537static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002538 u64 start, u64 len)
2539{
David Sterba32da53862019-10-29 19:20:18 +01002540 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002541
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002542 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002543 ASSERT(cache);
2544
2545 spin_lock(&cache->lock);
2546 cache->delalloc_bytes -= len;
2547 spin_unlock(&cache->lock);
2548
2549 btrfs_put_block_group(cache);
2550}
2551
Qu Wenruo203f44c52020-06-10 09:04:40 +08002552static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2553 struct inode *inode,
2554 struct btrfs_ordered_extent *oe)
2555{
2556 struct btrfs_file_extent_item stack_fi;
2557 u64 logical_len;
2558
2559 memset(&stack_fi, 0, sizeof(stack_fi));
2560 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2561 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2562 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2563 oe->disk_num_bytes);
2564 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2565 logical_len = oe->truncated_len;
2566 else
2567 logical_len = oe->num_bytes;
2568 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2569 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2570 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2571 /* Encryption and other encoding is reserved and all 0 */
2572
Nikolay Borisovc553f942020-06-03 08:55:19 +03002573 return insert_reserved_file_extent(trans, BTRFS_I(inode), oe->file_offset,
Qu Wenruo7dbeaad2020-06-10 09:04:43 +08002574 &stack_fi, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002575}
2576
2577/*
2578 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002579 * an ordered extent if the range of bytes in the file it covers are
2580 * fully written.
2581 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002582static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002583{
Josef Bacik5fd02042012-05-02 14:00:54 -04002584 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002585 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002586 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002587 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002588 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002589 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002590 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002591 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002592 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002593 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002594 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002595 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002596 bool range_locked = false;
2597 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002598 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002599 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002600
Omar Sandovalbffe6332019-12-02 17:34:19 -08002601 start = ordered_extent->file_offset;
2602 end = start + ordered_extent->num_bytes - 1;
2603
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002604 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2605 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2606 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2607 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002608
Nikolay Borisov8d510122019-10-08 20:43:06 +03002609 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002610
Josef Bacik5fd02042012-05-02 14:00:54 -04002611 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2612 ret = -EIO;
2613 goto out;
2614 }
2615
Omar Sandovalbffe6332019-12-02 17:34:19 -08002616 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002617
Josef Bacik77cef2e2013-08-29 13:57:21 -04002618 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2619 truncated = true;
2620 logical_len = ordered_extent->truncated_len;
2621 /* Truncated the entire extent, don't bother adding */
2622 if (!logical_len)
2623 goto out;
2624 }
2625
Yan, Zhengc2167752009-11-12 09:34:21 +00002626 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002627 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002628
Josef Bacikd923afe2020-01-17 09:02:23 -05002629 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002630 if (freespace_inode)
2631 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002632 else
2633 trans = btrfs_join_transaction(root);
2634 if (IS_ERR(trans)) {
2635 ret = PTR_ERR(trans);
2636 trans = NULL;
2637 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002638 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002639 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002640 ret = btrfs_update_inode_fallback(trans, root, inode);
2641 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002642 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002643 goto out;
2644 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002646 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002647 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002648
Nikolay Borisov8d510122019-10-08 20:43:06 +03002649 if (freespace_inode)
2650 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002651 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002652 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002653 if (IS_ERR(trans)) {
2654 ret = PTR_ERR(trans);
2655 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002656 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002657 }
Chris Masona79b7d42014-05-22 16:18:52 -07002658
Josef Bacik69fe2d72017-10-19 14:15:57 -04002659 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002660
Chris Masonc8b97812008-10-29 14:49:59 -04002661 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002662 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002663 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002664 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002665 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002666 ordered_extent->file_offset,
2667 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002668 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002669 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002670 BUG_ON(root == fs_info->tree_root);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002671 ret = insert_ordered_extent_file_extent(trans, inode,
2672 ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002673 if (!ret) {
2674 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002675 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002676 ordered_extent->disk_bytenr,
2677 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002678 }
Yan Zhengd899e052008-10-30 14:25:28 -04002679 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002680 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002681 ordered_extent->file_offset,
2682 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002683 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002684 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002685 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002686 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002687
Nikolay Borisovac01f262018-01-08 10:59:43 +02002688 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2689 if (ret) {
2690 btrfs_abort_transaction(trans, ret);
2691 goto out;
2692 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002693
Josef Bacikd923afe2020-01-17 09:02:23 -05002694 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002695 ret = btrfs_update_inode_fallback(trans, root, inode);
2696 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002697 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002698 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002699 }
2700 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002701out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002702 clear_bits = EXTENT_DEFRAG;
2703 if (range_locked)
2704 clear_bits |= EXTENT_LOCKED;
2705 if (clear_new_delalloc_bytes)
2706 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002707 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2708 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002709 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002710
Miao Xiea698d0752012-09-20 01:51:59 -06002711 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002712 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002713
Josef Bacik77cef2e2013-08-29 13:57:21 -04002714 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002715 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002716
2717 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002718 unwritten_start += logical_len;
2719 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002720
2721 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002722 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002723
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002724 /*
2725 * If the ordered extent had an IOERR or something else went
2726 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002727 * back to the allocator. We only free the extent in the
2728 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002729 *
2730 * If we made it past insert_reserved_file_extent before we
2731 * errored out then we don't need to do this as the accounting
2732 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002733 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002734 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002735 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002736 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002737 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2738 /*
2739 * Discard the range before returning it back to the
2740 * free space pool
2741 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002742 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002743 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002744 ordered_extent->disk_bytenr,
2745 ordered_extent->disk_num_bytes,
2746 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002747 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002748 ordered_extent->disk_bytenr,
2749 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002750 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002751 }
2752
Josef Bacik5fd02042012-05-02 14:00:54 -04002753 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002754 * This needs to be done to make sure anybody waiting knows we are done
2755 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002756 */
2757 btrfs_remove_ordered_extent(inode, ordered_extent);
2758
Chris Masone6dcd2d2008-07-17 12:53:50 -04002759 /* once for us */
2760 btrfs_put_ordered_extent(ordered_extent);
2761 /* once for the tree */
2762 btrfs_put_ordered_extent(ordered_extent);
2763
Josef Bacik5fd02042012-05-02 14:00:54 -04002764 return ret;
2765}
2766
2767static void finish_ordered_fn(struct btrfs_work *work)
2768{
2769 struct btrfs_ordered_extent *ordered_extent;
2770 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2771 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002772}
2773
Nikolay Borisovc6297322018-11-08 10:18:08 +02002774void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2775 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002776{
Josef Bacik5fd02042012-05-02 14:00:54 -04002777 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002778 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002779 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002780 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002781
liubo1abe9b82011-03-24 11:18:59 +00002782 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2783
Chris Mason8b62b722009-09-02 16:53:46 -04002784 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002785 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2786 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002787 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002788
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002789 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002790 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002791 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002792 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002793
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002794 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002795 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002796}
2797
Omar Sandoval47df7762020-04-16 14:46:17 -07002798static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2799 int icsum, struct page *page, int pgoff, u64 start,
2800 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002801{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002802 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2803 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002804 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002805 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2806 u8 *csum_expected;
2807 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002808
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002809 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002810
2811 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002812 shash->tfm = fs_info->csum_shash;
2813
Eric Biggersfd080012020-04-30 23:51:59 -07002814 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002815
2816 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002817 goto zeroit;
2818
2819 kunmap_atomic(kaddr);
2820 return 0;
2821zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002822 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2823 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002824 memset(kaddr + pgoff, 1, len);
2825 flush_dcache_page(page);
2826 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002827 return -EIO;
2828}
2829
Chris Masond352ac62008-09-29 15:18:18 -04002830/*
Chris Masond352ac62008-09-29 15:18:18 -04002831 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002832 * if there's a match, we allow the bio to finish. If not, the code in
2833 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002834 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002835static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2836 u64 phy_offset, struct page *page,
2837 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002838{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002839 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002840 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002841 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002842 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002843
Chris Masond20f7042008-12-08 16:58:54 -05002844 if (PageChecked(page)) {
2845 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002846 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002847 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002848
2849 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002850 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002851
Yan Zheng17d217f2008-12-12 10:03:38 -05002852 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002853 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002854 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002855 return 0;
2856 }
2857
Miao Xiefacc8a222013-07-25 19:22:34 +08002858 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002859 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2860 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002861}
Chris Masonb888db22007-08-27 16:49:44 -04002862
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002863/*
2864 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2865 *
2866 * @inode: The inode we want to perform iput on
2867 *
2868 * This function uses the generic vfs_inode::i_count to track whether we should
2869 * just decrement it (in case it's > 1) or if this is the last iput then link
2870 * the inode to the delayed iput machinery. Delayed iputs are processed at
2871 * transaction commit time/superblock commit/cleaner kthread.
2872 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002873void btrfs_add_delayed_iput(struct inode *inode)
2874{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002875 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002876 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002877
2878 if (atomic_add_unless(&inode->i_count, -1, 1))
2879 return;
2880
Josef Bacik034f7842018-12-03 11:06:52 -05002881 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002882 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002883 ASSERT(list_empty(&binode->delayed_iput));
2884 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002885 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002886 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2887 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002888}
2889
Josef Bacik63611e72019-06-18 10:59:18 -04002890static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2891 struct btrfs_inode *inode)
2892{
2893 list_del_init(&inode->delayed_iput);
2894 spin_unlock(&fs_info->delayed_iput_lock);
2895 iput(&inode->vfs_inode);
2896 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2897 wake_up(&fs_info->delayed_iputs_wait);
2898 spin_lock(&fs_info->delayed_iput_lock);
2899}
2900
2901static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2902 struct btrfs_inode *inode)
2903{
2904 if (!list_empty(&inode->delayed_iput)) {
2905 spin_lock(&fs_info->delayed_iput_lock);
2906 if (!list_empty(&inode->delayed_iput))
2907 run_delayed_iput_locked(fs_info, inode);
2908 spin_unlock(&fs_info->delayed_iput_lock);
2909 }
2910}
2911
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002912void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002913{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002914
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002915 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002916 while (!list_empty(&fs_info->delayed_iputs)) {
2917 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002918
David Sterba8089fe62015-11-19 14:15:51 +01002919 inode = list_first_entry(&fs_info->delayed_iputs,
2920 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002921 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002922 }
David Sterba8089fe62015-11-19 14:15:51 +01002923 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002924}
2925
Josef Bacik034f7842018-12-03 11:06:52 -05002926/**
2927 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2928 * @fs_info - the fs_info for this fs
2929 * @return - EINTR if we were killed, 0 if nothing's pending
2930 *
2931 * This will wait on any delayed iputs that are currently running with KILLABLE
2932 * set. Once they are all done running we will return, unless we are killed in
2933 * which case we return EINTR. This helps in user operations like fallocate etc
2934 * that might get blocked on the iputs.
2935 */
2936int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2937{
2938 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2939 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2940 if (ret)
2941 return -EINTR;
2942 return 0;
2943}
2944
Yan, Zhengd68fc572010-05-16 10:49:58 -04002945/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002946 * This creates an orphan entry for the given inode in case something goes wrong
2947 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002948 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002949int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002950 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002951{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002952 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002953
Omar Sandoval27919062018-05-11 13:13:37 -07002954 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2955 if (ret && ret != -EEXIST) {
2956 btrfs_abort_transaction(trans, ret);
2957 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002958 }
2959
Yan, Zhengd68fc572010-05-16 10:49:58 -04002960 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002961}
2962
2963/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002964 * We have done the delete so we can go ahead and remove the orphan item for
2965 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002966 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002967static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002968 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002969{
Omar Sandoval27919062018-05-11 13:13:37 -07002970 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002971}
2972
2973/*
2974 * this cleans up any orphans that may be left on the list from the last use
2975 * of this root.
2976 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002977int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002978{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002979 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002980 struct btrfs_path *path;
2981 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002982 struct btrfs_key key, found_key;
2983 struct btrfs_trans_handle *trans;
2984 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002985 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002986 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002987
Yan, Zhengd68fc572010-05-16 10:49:58 -04002988 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002989 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002990
2991 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002992 if (!path) {
2993 ret = -ENOMEM;
2994 goto out;
2995 }
David Sterbae4058b52015-11-27 16:31:35 +01002996 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002997
2998 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002999 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003000 key.offset = (u64)-1;
3001
Josef Bacik7b128762008-07-24 12:17:14 -04003002 while (1) {
3003 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003004 if (ret < 0)
3005 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003006
3007 /*
3008 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003009 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003010 * find the key and see if we have stuff that matches
3011 */
3012 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003013 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003014 if (path->slots[0] == 0)
3015 break;
3016 path->slots[0]--;
3017 }
3018
3019 /* pull out the item */
3020 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003021 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3022
3023 /* make sure the item matches what we want */
3024 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3025 break;
David Sterba962a2982014-06-04 18:41:45 +02003026 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003027 break;
3028
3029 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003030 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003031
3032 /*
3033 * this is where we are basically btrfs_lookup, without the
3034 * crossing root thing. we store the inode number in the
3035 * offset of the orphan item.
3036 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003037
3038 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003039 btrfs_err(fs_info,
3040 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003041 ret = -EINVAL;
3042 goto out;
3043 }
3044
3045 last_objectid = found_key.offset;
3046
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003047 found_key.objectid = found_key.offset;
3048 found_key.type = BTRFS_INODE_ITEM_KEY;
3049 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003050 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303051 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003052 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003053 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003054
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003055 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003056 struct btrfs_root *dead_root;
3057 struct btrfs_fs_info *fs_info = root->fs_info;
3058 int is_dead_root = 0;
3059
3060 /*
3061 * this is an orphan in the tree root. Currently these
3062 * could come from 2 sources:
3063 * a) a snapshot deletion in progress
3064 * b) a free space cache inode
3065 * We need to distinguish those two, as the snapshot
3066 * orphan must not get deleted.
3067 * find_dead_roots already ran before us, so if this
3068 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003069 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003070 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003071
3072 spin_lock(&fs_info->fs_roots_radix_lock);
3073 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3074 (unsigned long)found_key.objectid);
3075 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3076 is_dead_root = 1;
3077 spin_unlock(&fs_info->fs_roots_radix_lock);
3078
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003079 if (is_dead_root) {
3080 /* prevent this orphan from being found again */
3081 key.offset = found_key.objectid - 1;
3082 continue;
3083 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003084
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003085 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003086
Josef Bacika8c9e572011-09-21 16:55:59 -04003087 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003088 * If we have an inode with links, there are a couple of
3089 * possibilities. Old kernels (before v3.12) used to create an
3090 * orphan item for truncate indicating that there were possibly
3091 * extent items past i_size that needed to be deleted. In v3.12,
3092 * truncate was changed to update i_size in sync with the extent
3093 * items, but the (useless) orphan item was still created. Since
3094 * v4.18, we don't create the orphan item for truncate at all.
3095 *
3096 * So, this item could mean that we need to do a truncate, but
3097 * only if this filesystem was last used on a pre-v3.12 kernel
3098 * and was not cleanly unmounted. The odds of that are quite
3099 * slim, and it's a pain to do the truncate now, so just delete
3100 * the orphan item.
3101 *
3102 * It's also possible that this orphan item was supposed to be
3103 * deleted but wasn't. The inode number may have been reused,
3104 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003105 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003106 if (ret == -ENOENT || inode->i_nlink) {
3107 if (!ret)
3108 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003109 trans = btrfs_start_transaction(root, 1);
3110 if (IS_ERR(trans)) {
3111 ret = PTR_ERR(trans);
3112 goto out;
3113 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003114 btrfs_debug(fs_info, "auto deleting %Lu",
3115 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003116 ret = btrfs_del_orphan_item(trans, root,
3117 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003118 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003119 if (ret)
3120 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003121 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003122 }
Josef Bacik7b128762008-07-24 12:17:14 -04003123
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003124 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003125
3126 /* this will do delete_inode and everything for us */
3127 iput(inode);
3128 }
Miao Xie3254c872011-11-10 20:45:05 -05003129 /* release the path since we're done with it */
3130 btrfs_release_path(path);
3131
Yan, Zhengd68fc572010-05-16 10:49:58 -04003132 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3133
Omar Sandovala575cee2018-05-11 13:13:38 -07003134 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003135 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003136 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003137 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003138 }
Josef Bacik7b128762008-07-24 12:17:14 -04003139
3140 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003141 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003142
3143out:
3144 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003145 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003146 btrfs_free_path(path);
3147 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003148}
3149
Chris Masond352ac62008-09-29 15:18:18 -04003150/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003151 * very simple check to peek ahead in the leaf looking for xattrs. If we
3152 * don't find any xattrs, we know there can't be any acls.
3153 *
3154 * slot is the slot the inode is in, objectid is the objectid of the inode
3155 */
3156static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003157 int slot, u64 objectid,
3158 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003159{
3160 u32 nritems = btrfs_header_nritems(leaf);
3161 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003162 static u64 xattr_access = 0;
3163 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003164 int scanned = 0;
3165
Josef Bacikf23b5a52013-06-19 10:16:26 -04003166 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003167 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3168 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3169 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3170 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003171 }
3172
Chris Mason46a53cc2009-04-27 11:47:50 -04003173 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003174 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003175 while (slot < nritems) {
3176 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3177
3178 /* we found a different objectid, there must not be acls */
3179 if (found_key.objectid != objectid)
3180 return 0;
3181
3182 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003183 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003184 if (*first_xattr_slot == -1)
3185 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003186 if (found_key.offset == xattr_access ||
3187 found_key.offset == xattr_default)
3188 return 1;
3189 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003190
3191 /*
3192 * we found a key greater than an xattr key, there can't
3193 * be any acls later on
3194 */
3195 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3196 return 0;
3197
3198 slot++;
3199 scanned++;
3200
3201 /*
3202 * it goes inode, inode backrefs, xattrs, extents,
3203 * so if there are a ton of hard links to an inode there can
3204 * be a lot of backrefs. Don't waste time searching too hard,
3205 * this is just an optimization
3206 */
3207 if (scanned >= 8)
3208 break;
3209 }
3210 /* we hit the end of the leaf before we found an xattr or
3211 * something larger than an xattr. We have to assume the inode
3212 * has acls
3213 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003214 if (*first_xattr_slot == -1)
3215 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003216 return 1;
3217}
3218
3219/*
Chris Masond352ac62008-09-29 15:18:18 -04003220 * read an inode from the btree into the in-memory inode
3221 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003222static int btrfs_read_locked_inode(struct inode *inode,
3223 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003224{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003225 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003226 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003227 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003228 struct btrfs_inode_item *inode_item;
3229 struct btrfs_root *root = BTRFS_I(inode)->root;
3230 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003231 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003232 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003233 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003234 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003235 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003236 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003237
3238 ret = btrfs_fill_inode(inode, &rdev);
3239 if (!ret)
3240 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003241
Filipe Manana4222ea72018-10-24 10:13:03 +01003242 if (!path) {
3243 path = btrfs_alloc_path();
3244 if (!path)
3245 return -ENOMEM;
3246 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003247
Chris Mason39279cc2007-06-12 06:35:45 -04003248 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003249
Chris Mason39279cc2007-06-12 06:35:45 -04003250 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003251 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003252 if (path != in_path)
3253 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003254 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003255 }
Chris Mason39279cc2007-06-12 06:35:45 -04003256
Chris Mason5f39d392007-10-15 16:14:19 -04003257 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003258
3259 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003260 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003261
Chris Mason5f39d392007-10-15 16:14:19 -04003262 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3263 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003264 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003265 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003266 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3267 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003268 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003269 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3270 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003271
David Sterbaa937b972014-12-12 17:39:12 +01003272 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3273 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003274
David Sterbaa937b972014-12-12 17:39:12 +01003275 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3276 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003277
David Sterbaa937b972014-12-12 17:39:12 +01003278 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3279 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003280
chandan r9cc97d62012-07-04 12:48:07 +05303281 BTRFS_I(inode)->i_otime.tv_sec =
3282 btrfs_timespec_sec(leaf, &inode_item->otime);
3283 BTRFS_I(inode)->i_otime.tv_nsec =
3284 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003285
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003286 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003287 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003288 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3289
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003290 inode_set_iversion_queried(inode,
3291 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003292 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003293 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003294 rdev = btrfs_inode_rdev(leaf, inode_item);
3295
Josef Bacikaec74772008-07-24 12:12:38 -04003296 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003297 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003298
3299cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003300 /*
3301 * If we were modified in the current generation and evicted from memory
3302 * and then re-read we need to do a full sync since we don't have any
3303 * idea about which extents were modified before we were evicted from
3304 * cache.
3305 *
3306 * This is required for both inode re-read from disk and delayed inode
3307 * in delayed_nodes_tree.
3308 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003309 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003310 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3311 &BTRFS_I(inode)->runtime_flags);
3312
Filipe Mananabde6c242015-07-24 00:00:19 +01003313 /*
3314 * We don't persist the id of the transaction where an unlink operation
3315 * against the inode was last made. So here we assume the inode might
3316 * have been evicted, and therefore the exact value of last_unlink_trans
3317 * lost, and set it to last_trans to avoid metadata inconsistencies
3318 * between the inode and its parent if the inode is fsync'ed and the log
3319 * replayed. For example, in the scenario:
3320 *
3321 * touch mydir/foo
3322 * ln mydir/foo mydir/bar
3323 * sync
3324 * unlink mydir/bar
3325 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3326 * xfs_io -c fsync mydir/foo
3327 * <power failure>
3328 * mount fs, triggers fsync log replay
3329 *
3330 * We must make sure that when we fsync our inode foo we also log its
3331 * parent inode, otherwise after log replay the parent still has the
3332 * dentry with the "bar" name but our inode foo has a link count of 1
3333 * and doesn't have an inode ref with the name "bar" anymore.
3334 *
3335 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003336 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003337 * transaction commits on fsync if our inode is a directory, or if our
3338 * inode is not a directory, logging its parent unnecessarily.
3339 */
3340 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3341
Miao Xie67de1172013-12-26 13:07:06 +08003342 path->slots[0]++;
3343 if (inode->i_nlink != 1 ||
3344 path->slots[0] >= btrfs_header_nritems(leaf))
3345 goto cache_acl;
3346
3347 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003348 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003349 goto cache_acl;
3350
3351 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3352 if (location.type == BTRFS_INODE_REF_KEY) {
3353 struct btrfs_inode_ref *ref;
3354
3355 ref = (struct btrfs_inode_ref *)ptr;
3356 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3357 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3358 struct btrfs_inode_extref *extref;
3359
3360 extref = (struct btrfs_inode_extref *)ptr;
3361 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3362 extref);
3363 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003364cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003365 /*
3366 * try to precache a NULL acl entry for files that don't have
3367 * any xattrs or acls
3368 */
Li Zefan33345d012011-04-20 10:31:50 +08003369 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003370 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003371 if (first_xattr_slot != -1) {
3372 path->slots[0] = first_xattr_slot;
3373 ret = btrfs_load_inode_props(inode, path);
3374 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003375 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003376 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003377 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003378 root->root_key.objectid, ret);
3379 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003380 if (path != in_path)
3381 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003382
Al Viro72c04902009-06-24 16:58:48 -04003383 if (!maybe_acls)
3384 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003385
Chris Mason39279cc2007-06-12 06:35:45 -04003386 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003387 case S_IFREG:
3388 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003389 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003390 inode->i_fop = &btrfs_file_operations;
3391 inode->i_op = &btrfs_file_inode_operations;
3392 break;
3393 case S_IFDIR:
3394 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003395 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003396 break;
3397 case S_IFLNK:
3398 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003399 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003400 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003401 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003402 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003403 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003404 init_special_inode(inode, inode->i_mode, rdev);
3405 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003406 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003407
David Sterba7b6a2212018-03-26 18:40:21 +02003408 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003409 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003410}
3411
Chris Masond352ac62008-09-29 15:18:18 -04003412/*
3413 * given a leaf and an inode, copy the inode fields into the leaf
3414 */
Chris Masone02119d2008-09-05 16:13:11 -04003415static void fill_inode_item(struct btrfs_trans_handle *trans,
3416 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003417 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003418 struct inode *inode)
3419{
Liu Bo51fab692012-12-27 09:01:21 +00003420 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003421
David Sterbac82f8232019-08-09 17:48:21 +02003422 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003423
David Sterbacc4c13d2020-04-29 02:15:56 +02003424 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3425 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3426 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3427 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3428 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003429
David Sterbacc4c13d2020-04-29 02:15:56 +02003430 btrfs_set_token_timespec_sec(&token, &item->atime,
3431 inode->i_atime.tv_sec);
3432 btrfs_set_token_timespec_nsec(&token, &item->atime,
3433 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003434
David Sterbacc4c13d2020-04-29 02:15:56 +02003435 btrfs_set_token_timespec_sec(&token, &item->mtime,
3436 inode->i_mtime.tv_sec);
3437 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3438 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003439
David Sterbacc4c13d2020-04-29 02:15:56 +02003440 btrfs_set_token_timespec_sec(&token, &item->ctime,
3441 inode->i_ctime.tv_sec);
3442 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3443 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003444
David Sterbacc4c13d2020-04-29 02:15:56 +02003445 btrfs_set_token_timespec_sec(&token, &item->otime,
3446 BTRFS_I(inode)->i_otime.tv_sec);
3447 btrfs_set_token_timespec_nsec(&token, &item->otime,
3448 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303449
David Sterbacc4c13d2020-04-29 02:15:56 +02003450 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3451 btrfs_set_token_inode_generation(&token, item,
3452 BTRFS_I(inode)->generation);
3453 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3454 btrfs_set_token_inode_transid(&token, item, trans->transid);
3455 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3456 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3457 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003458}
3459
Chris Masond352ac62008-09-29 15:18:18 -04003460/*
3461 * copy everything in the in-memory inode into the btree.
3462 */
Chris Mason21151332011-11-10 20:39:08 -05003463static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003464 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003465{
3466 struct btrfs_inode_item *inode_item;
3467 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003468 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003469 int ret;
3470
3471 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003472 if (!path)
3473 return -ENOMEM;
3474
Chris Masonb9473432009-03-13 11:00:37 -04003475 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003476 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3477 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003478 if (ret) {
3479 if (ret > 0)
3480 ret = -ENOENT;
3481 goto failed;
3482 }
3483
Chris Mason5f39d392007-10-15 16:14:19 -04003484 leaf = path->nodes[0];
3485 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003486 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003487
Chris Masone02119d2008-09-05 16:13:11 -04003488 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003489 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003490 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003491 ret = 0;
3492failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003493 btrfs_free_path(path);
3494 return ret;
3495}
3496
Chris Masond352ac62008-09-29 15:18:18 -04003497/*
Chris Mason21151332011-11-10 20:39:08 -05003498 * copy everything in the in-memory inode into the btree.
3499 */
3500noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3501 struct btrfs_root *root, struct inode *inode)
3502{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003503 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003504 int ret;
3505
3506 /*
3507 * If the inode is a free space inode, we can deadlock during commit
3508 * if we put it into the delayed code.
3509 *
3510 * The data relocation inode should also be directly updated
3511 * without delay
3512 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003513 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003514 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003515 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003516 btrfs_update_root_times(trans, root);
3517
Chris Mason21151332011-11-10 20:39:08 -05003518 ret = btrfs_delayed_update_inode(trans, root, inode);
3519 if (!ret)
3520 btrfs_set_inode_last_trans(trans, inode);
3521 return ret;
3522 }
3523
3524 return btrfs_update_inode_item(trans, root, inode);
3525}
3526
Josef Bacikbe6aef62012-10-22 15:43:12 -04003527noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3528 struct btrfs_root *root,
3529 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003530{
3531 int ret;
3532
3533 ret = btrfs_update_inode(trans, root, inode);
3534 if (ret == -ENOSPC)
3535 return btrfs_update_inode_item(trans, root, inode);
3536 return ret;
3537}
3538
3539/*
Chris Masond352ac62008-09-29 15:18:18 -04003540 * unlink helper that gets used here in inode.c and in the tree logging
3541 * recovery code. It remove a link in a directory with a given name, and
3542 * also drops the back refs in the inode to the directory
3543 */
Al Viro92986792011-03-04 17:14:37 +00003544static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3545 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003546 struct btrfs_inode *dir,
3547 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003548 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003549{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003550 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003551 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003552 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003553 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003554 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003555 u64 ino = btrfs_ino(inode);
3556 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003557
3558 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003559 if (!path) {
3560 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003561 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003562 }
3563
Chris Masonb9473432009-03-13 11:00:37 -04003564 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003565 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003566 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003567 if (IS_ERR_OR_NULL(di)) {
3568 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003569 goto err;
3570 }
Chris Mason39279cc2007-06-12 06:35:45 -04003571 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003572 if (ret)
3573 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003574 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003575
Miao Xie67de1172013-12-26 13:07:06 +08003576 /*
3577 * If we don't have dir index, we have to get it by looking up
3578 * the inode ref, since we get the inode ref, remove it directly,
3579 * it is unnecessary to do delayed deletion.
3580 *
3581 * But if we have dir index, needn't search inode ref to get it.
3582 * Since the inode ref is close to the inode item, it is better
3583 * that we delay to delete it, and just do this deletion when
3584 * we update the inode item.
3585 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003586 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003587 ret = btrfs_delayed_delete_inode_ref(inode);
3588 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003589 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003590 goto skip_backref;
3591 }
3592 }
3593
Li Zefan33345d012011-04-20 10:31:50 +08003594 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3595 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003596 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003597 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003598 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003599 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003600 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003601 goto err;
3602 }
Miao Xie67de1172013-12-26 13:07:06 +08003603skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003604 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003605 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003606 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003607 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003608 }
Chris Mason39279cc2007-06-12 06:35:45 -04003609
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003610 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3611 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003612 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003613 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003614 goto err;
3615 }
Chris Masone02119d2008-09-05 16:13:11 -04003616
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003617 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3618 index);
Chris Mason6418c962010-10-30 07:34:24 -04003619 if (ret == -ENOENT)
3620 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003621 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003622 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003623
3624 /*
3625 * If we have a pending delayed iput we could end up with the final iput
3626 * being run in btrfs-cleaner context. If we have enough of these built
3627 * up we can end up burning a lot of time in btrfs-cleaner without any
3628 * way to throttle the unlinks. Since we're currently holding a ref on
3629 * the inode we can run the delayed iput here without any issues as the
3630 * final iput won't be done until after we drop the ref we're currently
3631 * holding.
3632 */
3633 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003634err:
3635 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003636 if (ret)
3637 goto out;
3638
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003639 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003640 inode_inc_iversion(&inode->vfs_inode);
3641 inode_inc_iversion(&dir->vfs_inode);
3642 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3643 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3644 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003645out:
Chris Mason39279cc2007-06-12 06:35:45 -04003646 return ret;
3647}
3648
Al Viro92986792011-03-04 17:14:37 +00003649int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3650 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003651 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003652 const char *name, int name_len)
3653{
3654 int ret;
3655 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3656 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003657 drop_nlink(&inode->vfs_inode);
3658 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003659 }
3660 return ret;
3661}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003662
3663/*
3664 * helper to start transaction for unlink and rmdir.
3665 *
Josef Bacikd52be812013-05-29 14:54:47 -04003666 * unlink and rmdir are special in btrfs, they do not always free space, so
3667 * if we cannot make our reservations the normal way try and see if there is
3668 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3669 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003670 */
Josef Bacikd52be812013-05-29 14:54:47 -04003671static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003672{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003673 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003674
Josef Bacike70bea52011-10-11 14:18:24 -04003675 /*
3676 * 1 for the possible orphan item
3677 * 1 for the dir item
3678 * 1 for the dir index
3679 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003680 * 1 for the inode
3681 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003682 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003683}
3684
Chris Mason39279cc2007-06-12 06:35:45 -04003685static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3686{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003687 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003688 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003689 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003690 int ret;
3691
Josef Bacikd52be812013-05-29 14:54:47 -04003692 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003694 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003695
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003696 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3697 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003698
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003699 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3700 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3701 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003702 if (ret)
3703 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003704
Yan, Zhenga22285a2010-05-16 10:48:46 -04003705 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003706 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003707 if (ret)
3708 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003709 }
Josef Bacik7b128762008-07-24 12:17:14 -04003710
Tsutomu Itohb5324022011-07-19 07:27:20 +00003711out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003712 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003713 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003714 return ret;
3715}
3716
Misono Tomohirof60a2362018-04-18 11:34:52 +09003717static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003718 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003719{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003720 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003721 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003722 struct btrfs_path *path;
3723 struct extent_buffer *leaf;
3724 struct btrfs_dir_item *di;
3725 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003726 const char *name = dentry->d_name.name;
3727 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003728 u64 index;
3729 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003730 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003731 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003732
Josef Bacik045d3962019-12-18 17:20:27 -05003733 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3734 objectid = inode->root->root_key.objectid;
3735 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3736 objectid = inode->location.objectid;
3737 } else {
3738 WARN_ON(1);
3739 return -EINVAL;
3740 }
3741
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003742 path = btrfs_alloc_path();
3743 if (!path)
3744 return -ENOMEM;
3745
Li Zefan33345d012011-04-20 10:31:50 +08003746 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003747 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003748 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003749 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003750 goto out;
3751 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003752
3753 leaf = path->nodes[0];
3754 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3755 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3756 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003757 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003758 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003759 goto out;
3760 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003761 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003762
Josef Bacikd49d3282019-12-18 17:20:28 -05003763 /*
3764 * This is a placeholder inode for a subvolume we didn't have a
3765 * reference to at the time of the snapshot creation. In the meantime
3766 * we could have renamed the real subvol link into our snapshot, so
3767 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3768 * Instead simply lookup the dir_index_item for this entry so we can
3769 * remove it. Otherwise we know we have a ref to the root and we can
3770 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3771 */
3772 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003773 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003774 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003775 if (IS_ERR_OR_NULL(di)) {
3776 if (!di)
3777 ret = -ENOENT;
3778 else
3779 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003780 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003781 goto out;
3782 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003783
3784 leaf = path->nodes[0];
3785 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003786 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003787 btrfs_release_path(path);
3788 } else {
3789 ret = btrfs_del_root_ref(trans, objectid,
3790 root->root_key.objectid, dir_ino,
3791 &index, name, name_len);
3792 if (ret) {
3793 btrfs_abort_transaction(trans, ret);
3794 goto out;
3795 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003796 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003797
Lu Fengqi9add2942018-08-01 11:32:26 +08003798 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003799 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003800 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003801 goto out;
3802 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003803
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003804 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003805 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003806 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003807 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003808 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003809 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003810out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003811 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003812 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003813}
3814
Misono Tomohiroec42f162018-04-18 11:34:13 +09003815/*
3816 * Helper to check if the subvolume references other subvolumes or if it's
3817 * default.
3818 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003819static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003820{
3821 struct btrfs_fs_info *fs_info = root->fs_info;
3822 struct btrfs_path *path;
3823 struct btrfs_dir_item *di;
3824 struct btrfs_key key;
3825 u64 dir_id;
3826 int ret;
3827
3828 path = btrfs_alloc_path();
3829 if (!path)
3830 return -ENOMEM;
3831
3832 /* Make sure this root isn't set as the default subvol */
3833 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3834 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3835 dir_id, "default", 7, 0);
3836 if (di && !IS_ERR(di)) {
3837 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3838 if (key.objectid == root->root_key.objectid) {
3839 ret = -EPERM;
3840 btrfs_err(fs_info,
3841 "deleting default subvolume %llu is not allowed",
3842 key.objectid);
3843 goto out;
3844 }
3845 btrfs_release_path(path);
3846 }
3847
3848 key.objectid = root->root_key.objectid;
3849 key.type = BTRFS_ROOT_REF_KEY;
3850 key.offset = (u64)-1;
3851
3852 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3853 if (ret < 0)
3854 goto out;
3855 BUG_ON(ret == 0);
3856
3857 ret = 0;
3858 if (path->slots[0] > 0) {
3859 path->slots[0]--;
3860 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3861 if (key.objectid == root->root_key.objectid &&
3862 key.type == BTRFS_ROOT_REF_KEY)
3863 ret = -ENOTEMPTY;
3864 }
3865out:
3866 btrfs_free_path(path);
3867 return ret;
3868}
3869
Nikolay Borisov20a68002018-04-27 14:36:24 +03003870/* Delete all dentries for inodes belonging to the root */
3871static void btrfs_prune_dentries(struct btrfs_root *root)
3872{
3873 struct btrfs_fs_info *fs_info = root->fs_info;
3874 struct rb_node *node;
3875 struct rb_node *prev;
3876 struct btrfs_inode *entry;
3877 struct inode *inode;
3878 u64 objectid = 0;
3879
3880 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3881 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3882
3883 spin_lock(&root->inode_lock);
3884again:
3885 node = root->inode_tree.rb_node;
3886 prev = NULL;
3887 while (node) {
3888 prev = node;
3889 entry = rb_entry(node, struct btrfs_inode, rb_node);
3890
David Sterba37508512018-06-29 10:56:40 +02003891 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003892 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003893 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003894 node = node->rb_right;
3895 else
3896 break;
3897 }
3898 if (!node) {
3899 while (prev) {
3900 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003901 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003902 node = prev;
3903 break;
3904 }
3905 prev = rb_next(prev);
3906 }
3907 }
3908 while (node) {
3909 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003910 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003911 inode = igrab(&entry->vfs_inode);
3912 if (inode) {
3913 spin_unlock(&root->inode_lock);
3914 if (atomic_read(&inode->i_count) > 1)
3915 d_prune_aliases(inode);
3916 /*
3917 * btrfs_drop_inode will have it removed from the inode
3918 * cache when its usage count hits zero.
3919 */
3920 iput(inode);
3921 cond_resched();
3922 spin_lock(&root->inode_lock);
3923 goto again;
3924 }
3925
3926 if (cond_resched_lock(&root->inode_lock))
3927 goto again;
3928
3929 node = rb_next(node);
3930 }
3931 spin_unlock(&root->inode_lock);
3932}
3933
Misono Tomohirof60a2362018-04-18 11:34:52 +09003934int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3935{
3936 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3937 struct btrfs_root *root = BTRFS_I(dir)->root;
3938 struct inode *inode = d_inode(dentry);
3939 struct btrfs_root *dest = BTRFS_I(inode)->root;
3940 struct btrfs_trans_handle *trans;
3941 struct btrfs_block_rsv block_rsv;
3942 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003943 int ret;
3944 int err;
3945
3946 /*
3947 * Don't allow to delete a subvolume with send in progress. This is
3948 * inside the inode lock so the error handling that has to drop the bit
3949 * again is not run concurrently.
3950 */
3951 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003952 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003953 spin_unlock(&dest->root_item_lock);
3954 btrfs_warn(fs_info,
3955 "attempt to delete subvolume %llu during send",
3956 dest->root_key.objectid);
3957 return -EPERM;
3958 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003959 root_flags = btrfs_root_flags(&dest->root_item);
3960 btrfs_set_root_flags(&dest->root_item,
3961 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3962 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003963
3964 down_write(&fs_info->subvol_sem);
3965
3966 err = may_destroy_subvol(dest);
3967 if (err)
3968 goto out_up_write;
3969
3970 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3971 /*
3972 * One for dir inode,
3973 * two for dir entries,
3974 * two for root ref/backref.
3975 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003976 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003977 if (err)
3978 goto out_up_write;
3979
3980 trans = btrfs_start_transaction(root, 0);
3981 if (IS_ERR(trans)) {
3982 err = PTR_ERR(trans);
3983 goto out_release;
3984 }
3985 trans->block_rsv = &block_rsv;
3986 trans->bytes_reserved = block_rsv.size;
3987
3988 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3989
Josef Bacik045d3962019-12-18 17:20:27 -05003990 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003991 if (ret) {
3992 err = ret;
3993 btrfs_abort_transaction(trans, ret);
3994 goto out_end_trans;
3995 }
3996
3997 btrfs_record_root_in_trans(trans, dest);
3998
3999 memset(&dest->root_item.drop_progress, 0,
4000 sizeof(dest->root_item.drop_progress));
4001 dest->root_item.drop_level = 0;
4002 btrfs_set_root_refs(&dest->root_item, 0);
4003
4004 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4005 ret = btrfs_insert_orphan_item(trans,
4006 fs_info->tree_root,
4007 dest->root_key.objectid);
4008 if (ret) {
4009 btrfs_abort_transaction(trans, ret);
4010 err = ret;
4011 goto out_end_trans;
4012 }
4013 }
4014
Lu Fengqid1957792018-05-29 15:01:54 +08004015 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004016 BTRFS_UUID_KEY_SUBVOL,
4017 dest->root_key.objectid);
4018 if (ret && ret != -ENOENT) {
4019 btrfs_abort_transaction(trans, ret);
4020 err = ret;
4021 goto out_end_trans;
4022 }
4023 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004024 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004025 dest->root_item.received_uuid,
4026 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4027 dest->root_key.objectid);
4028 if (ret && ret != -ENOENT) {
4029 btrfs_abort_transaction(trans, ret);
4030 err = ret;
4031 goto out_end_trans;
4032 }
4033 }
4034
4035out_end_trans:
4036 trans->block_rsv = NULL;
4037 trans->bytes_reserved = 0;
4038 ret = btrfs_end_transaction(trans);
4039 if (ret && !err)
4040 err = ret;
4041 inode->i_flags |= S_DEAD;
4042out_release:
4043 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4044out_up_write:
4045 up_write(&fs_info->subvol_sem);
4046 if (err) {
4047 spin_lock(&dest->root_item_lock);
4048 root_flags = btrfs_root_flags(&dest->root_item);
4049 btrfs_set_root_flags(&dest->root_item,
4050 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4051 spin_unlock(&dest->root_item_lock);
4052 } else {
4053 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004054 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004055 ASSERT(dest->send_in_progress == 0);
4056
4057 /* the last ref */
4058 if (dest->ino_cache_inode) {
4059 iput(dest->ino_cache_inode);
4060 dest->ino_cache_inode = NULL;
4061 }
4062 }
4063
4064 return err;
4065}
4066
Chris Mason39279cc2007-06-12 06:35:45 -04004067static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4068{
David Howells2b0143b2015-03-17 22:25:59 +00004069 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004070 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004071 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004072 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004073 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004074
David Sterbab3ae2442012-09-13 16:04:34 -06004075 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004076 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004077 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004078 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004079
Josef Bacikd52be812013-05-29 14:54:47 -04004080 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004081 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004082 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004083
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004084 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004085 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004086 goto out;
4087 }
4088
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004089 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004090 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004091 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004092
Filipe Manana44f714d2016-06-06 16:11:13 +01004093 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4094
Chris Mason39279cc2007-06-12 06:35:45 -04004095 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004096 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4097 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4098 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004099 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004100 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004101 /*
4102 * Propagate the last_unlink_trans value of the deleted dir to
4103 * its parent directory. This is to prevent an unrecoverable
4104 * log tree in the case we do something like this:
4105 * 1) create dir foo
4106 * 2) create snapshot under dir foo
4107 * 3) delete the snapshot
4108 * 4) rmdir foo
4109 * 5) mkdir foo
4110 * 6) fsync foo or some file inside foo
4111 */
4112 if (last_unlink_trans >= trans->transid)
4113 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4114 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004115out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004116 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004117 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004118
Chris Mason39279cc2007-06-12 06:35:45 -04004119 return err;
4120}
4121
Josef Bacikddfae632017-10-19 14:16:02 -04004122/*
4123 * Return this if we need to call truncate_block for the last bit of the
4124 * truncate.
4125 */
4126#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004127
Chris Mason323ac952008-10-01 19:05:46 -04004128/*
Chris Mason39279cc2007-06-12 06:35:45 -04004129 * this can truncate away extent items, csum items and directory items.
4130 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004131 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004132 *
4133 * csum items that cross the new i_size are truncated to the new size
4134 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004135 *
4136 * min_type is the minimum key type to truncate down to. If set to 0, this
4137 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004138 */
Yan, Zheng80825102009-11-12 09:35:36 +00004139int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4140 struct btrfs_root *root,
4141 struct inode *inode,
4142 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004143{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004144 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004145 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004146 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004147 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004148 struct btrfs_key key;
4149 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004150 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004151 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004152 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004153 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004154 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004155 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004156 int found_extent;
4157 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004158 int pending_del_nr = 0;
4159 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004160 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004161 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004162 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004163 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004164 bool be_nice = false;
4165 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004166 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4167 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004168
4169 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004170
Chris Mason28ed1342014-12-17 09:41:04 -08004171 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004172 * For non-free space inodes and non-shareable roots, we want to back
4173 * off from time to time. This means all inodes in subvolume roots,
4174 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004175 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004176 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004177 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004178 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004179
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004180 path = btrfs_alloc_path();
4181 if (!path)
4182 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004183 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004184
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004185 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004186 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4187 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004188
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004189 /*
4190 * We want to drop from the next block forward in case this
4191 * new size is not block aligned since we will be keeping the
4192 * last block of the extent just the way it is.
4193 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004194 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004195 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004196 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004197 }
Yan, Zheng80825102009-11-12 09:35:36 +00004198
Miao Xie16cdcec2011-04-22 18:12:22 +08004199 /*
4200 * This function is also used to drop the items in the log tree before
4201 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004202 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004203 * items.
4204 */
4205 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004206 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004207
Li Zefan33345d012011-04-20 10:31:50 +08004208 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004209 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004210 key.type = (u8)-1;
4211
Chris Mason85e21ba2008-01-29 15:11:36 -05004212search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004213 /*
4214 * with a 16K leaf size and 128MB extents, you can actually queue
4215 * up a huge file in a single leaf. Most of the time that
4216 * bytes_deleted is > 0, it will be huge by the time we get here
4217 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004218 if (be_nice && bytes_deleted > SZ_32M &&
4219 btrfs_should_end_transaction(trans)) {
4220 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004221 goto out;
4222 }
Chris Masond3977122009-01-05 21:25:51 -05004223
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004224 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4225 if (ret < 0)
4226 goto out;
4227
Chris Mason85e21ba2008-01-29 15:11:36 -05004228 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004229 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004230 /* there are no items in the tree for us to truncate, we're
4231 * done
4232 */
Yan, Zheng80825102009-11-12 09:35:36 +00004233 if (path->slots[0] == 0)
4234 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004235 path->slots[0]--;
4236 }
4237
Chris Masond3977122009-01-05 21:25:51 -05004238 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004239 u64 clear_start = 0, clear_len = 0;
4240
Chris Mason39279cc2007-06-12 06:35:45 -04004241 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004242 leaf = path->nodes[0];
4243 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004244 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004245
Li Zefan33345d012011-04-20 10:31:50 +08004246 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004247 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004248
Chris Mason85e21ba2008-01-29 15:11:36 -05004249 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004250 break;
4251
Chris Mason5f39d392007-10-15 16:14:19 -04004252 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004253 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004254 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004255 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004256 extent_type = btrfs_file_extent_type(leaf, fi);
4257 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004258 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004259 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004260
4261 trace_btrfs_truncate_show_fi_regular(
4262 BTRFS_I(inode), leaf, fi,
4263 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004264 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004265 item_end += btrfs_file_extent_ram_bytes(leaf,
4266 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004267
4268 trace_btrfs_truncate_show_fi_inline(
4269 BTRFS_I(inode), leaf, fi, path->slots[0],
4270 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004271 }
Yan008630c2007-11-07 13:31:09 -05004272 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004273 }
Yan, Zheng80825102009-11-12 09:35:36 +00004274 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004275 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004276 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004277 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004278 break;
4279 if (found_key.offset >= new_size)
4280 del_item = 1;
4281 else
4282 del_item = 0;
4283 }
Chris Mason39279cc2007-06-12 06:35:45 -04004284 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004285 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004286 if (found_type != BTRFS_EXTENT_DATA_KEY)
4287 goto delete;
4288
4289 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004290 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004291
4292 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004293 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004294 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004295 u64 orig_num_bytes =
4296 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004297 extent_num_bytes = ALIGN(new_size -
4298 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004299 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004300 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004301 btrfs_set_file_extent_num_bytes(leaf, fi,
4302 extent_num_bytes);
4303 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004304 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004305 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004306 &root->state) &&
4307 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004308 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004309 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004310 } else {
Chris Masondb945352007-10-15 16:15:53 -04004311 extent_num_bytes =
4312 btrfs_file_extent_disk_num_bytes(leaf,
4313 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004314 extent_offset = found_key.offset -
4315 btrfs_file_extent_offset(leaf, fi);
4316
Chris Mason39279cc2007-06-12 06:35:45 -04004317 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004318 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004319 if (extent_start != 0) {
4320 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004321 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004322 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004323 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004324 }
4325 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004326 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004327 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004328 /*
4329 * we can't truncate inline items that have had
4330 * special encodings
4331 */
4332 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004333 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004334 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4335 btrfs_file_extent_compression(leaf, fi) == 0) {
4336 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004337
Josef Bacikddfae632017-10-19 14:16:02 -04004338 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4339 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004340 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004341 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004342 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004343 * We have to bail so the last_size is set to
4344 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004345 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004346 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004347 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004348 } else {
4349 /*
4350 * Inline extents are special, we just treat
4351 * them as a full sector worth in the file
4352 * extent tree just for simplicity sake.
4353 */
4354 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004355 }
Josef Bacikddfae632017-10-19 14:16:02 -04004356
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004357 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004358 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004359 }
Chris Mason179e29e2007-11-01 11:28:41 -04004360delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004361 /*
4362 * We use btrfs_truncate_inode_items() to clean up log trees for
4363 * multiple fsyncs, and in this case we don't want to clear the
4364 * file extent range because it's just the log.
4365 */
4366 if (root == BTRFS_I(inode)->root) {
4367 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4368 clear_start, clear_len);
4369 if (ret) {
4370 btrfs_abort_transaction(trans, ret);
4371 break;
4372 }
4373 }
4374
Josef Bacikddfae632017-10-19 14:16:02 -04004375 if (del_item)
4376 last_size = found_key.offset;
4377 else
4378 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004379 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004380 if (!pending_del_nr) {
4381 /* no pending yet, add ourselves */
4382 pending_del_slot = path->slots[0];
4383 pending_del_nr = 1;
4384 } else if (pending_del_nr &&
4385 path->slots[0] + 1 == pending_del_slot) {
4386 /* hop on the pending chunk */
4387 pending_del_nr++;
4388 pending_del_slot = path->slots[0];
4389 } else {
Chris Masond3977122009-01-05 21:25:51 -05004390 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004391 }
Chris Mason39279cc2007-06-12 06:35:45 -04004392 } else {
4393 break;
4394 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004395 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004396
Miao Xie27cdeb72014-04-02 19:51:05 +08004397 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004398 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004399 struct btrfs_ref ref = { 0 };
4400
Chris Mason28ed1342014-12-17 09:41:04 -08004401 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004402
4403 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4404 extent_start, extent_num_bytes, 0);
4405 ref.real_root = root->root_key.objectid;
4406 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4407 ino, extent_offset);
4408 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004409 if (ret) {
4410 btrfs_abort_transaction(trans, ret);
4411 break;
4412 }
Chris Mason28f75a02015-02-04 06:59:29 -08004413 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004414 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004415 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004416 }
Chris Mason39279cc2007-06-12 06:35:45 -04004417 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004418
Yan, Zheng80825102009-11-12 09:35:36 +00004419 if (found_type == BTRFS_INODE_ITEM_KEY)
4420 break;
4421
4422 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004423 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004424 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004425 if (pending_del_nr) {
4426 ret = btrfs_del_items(trans, root, path,
4427 pending_del_slot,
4428 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004429 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004430 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004431 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004432 }
Yan, Zheng80825102009-11-12 09:35:36 +00004433 pending_del_nr = 0;
4434 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004435 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004436
Chris Mason28f75a02015-02-04 06:59:29 -08004437 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004438 * We can generate a lot of delayed refs, so we need to
4439 * throttle every once and a while and make sure we're
4440 * adding enough space to keep up with the work we are
4441 * generating. Since we hold a transaction here we
4442 * can't flush, and we don't want to FLUSH_LIMIT because
4443 * we could have generated too many delayed refs to
4444 * actually allocate, so just bail if we're short and
4445 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004446 */
Josef Bacik28bad212018-12-03 10:20:38 -05004447 if (should_throttle) {
4448 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4449 BTRFS_RESERVE_NO_FLUSH);
4450 if (ret) {
4451 ret = -EAGAIN;
4452 break;
4453 }
Chris Mason28f75a02015-02-04 06:59:29 -08004454 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004455 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004456 } else {
4457 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004458 }
Chris Mason39279cc2007-06-12 06:35:45 -04004459 }
Yan, Zheng80825102009-11-12 09:35:36 +00004460out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004461 if (ret >= 0 && pending_del_nr) {
4462 int err;
4463
4464 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004465 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004466 if (err) {
4467 btrfs_abort_transaction(trans, err);
4468 ret = err;
4469 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004470 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004471 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4472 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004473 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004474 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004475 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004476 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4477 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004478 }
Chris Mason28ed1342014-12-17 09:41:04 -08004479
Chris Mason39279cc2007-06-12 06:35:45 -04004480 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004481 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004482}
4483
Chris Masona52d9a82007-08-27 16:49:44 -04004484/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304485 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004486 * @inode - inode that we're zeroing
4487 * @from - the offset to start zeroing
4488 * @len - the length to zero, 0 to zero the entire range respective to the
4489 * offset
4490 * @front - zero up to the offset instead of from the offset on
4491 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304492 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004493 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004494 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304495int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004496 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004497{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004498 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004499 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004500 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4501 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004502 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004503 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004504 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004505 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004506 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004507 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304508 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004509 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004510 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004511 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004512 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304513 u64 block_start;
4514 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004515
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004516 if (IS_ALIGNED(offset, blocksize) &&
4517 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004518 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304519
Josef Bacik8b62f872017-10-19 14:15:55 -04004520 block_start = round_down(from, blocksize);
4521 block_end = block_start + blocksize - 1;
4522
Chris Masona52d9a82007-08-27 16:49:44 -04004523
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004524 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4525 blocksize);
4526 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004527 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4528 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004529 /* For nocow case, no need to reserve data space */
4530 only_release_metadata = true;
4531 } else {
4532 goto out;
4533 }
4534 }
4535 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4536 if (ret < 0) {
4537 if (!only_release_metadata)
4538 btrfs_free_reserved_data_space(inode, data_reserved,
4539 block_start, blocksize);
4540 goto out;
4541 }
Chris Mason211c17f2008-05-15 09:13:45 -04004542again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004543 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004544 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004545 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004546 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004547 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004548 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004549 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004550 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004551
Chris Masona52d9a82007-08-27 16:49:44 -04004552 if (!PageUptodate(page)) {
4553 ret = btrfs_readpage(NULL, page);
4554 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004555 if (page->mapping != mapping) {
4556 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004557 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004558 goto again;
4559 }
Chris Masona52d9a82007-08-27 16:49:44 -04004560 if (!PageUptodate(page)) {
4561 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004562 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004563 }
4564 }
Chris Mason211c17f2008-05-15 09:13:45 -04004565 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004566
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304567 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004568 set_page_extent_mapped(page);
4569
Nikolay Borisovc3504372020-06-03 08:55:03 +03004570 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004571 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304572 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004573 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004574 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004575 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004576 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004577 btrfs_put_ordered_extent(ordered);
4578 goto again;
4579 }
4580
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304581 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004582 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4583 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004584
Filipe Mananae3b8a482017-11-04 00:16:59 +00004585 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004586 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004587 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304588 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004589 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004590 goto out_unlock;
4591 }
4592
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304593 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004594 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304595 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004596 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004597 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304598 memset(kaddr + (block_start - page_offset(page)),
4599 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004600 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304601 memset(kaddr + (block_start - page_offset(page)) + offset,
4602 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004603 flush_dcache_page(page);
4604 kunmap(page);
4605 }
Chris Mason247e7432008-07-17 12:53:51 -04004606 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004607 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004608 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004609
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004610 if (only_release_metadata)
4611 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4612 block_end, EXTENT_NORESERVE, NULL, NULL,
4613 GFP_NOFS);
4614
Chris Mason89642222008-07-24 09:41:53 -04004615out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004616 if (ret) {
4617 if (only_release_metadata)
4618 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4619 blocksize, true);
4620 else
4621 btrfs_delalloc_release_space(inode, data_reserved,
4622 block_start, blocksize, true);
4623 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004624 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004625 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004626 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004627out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004628 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004629 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004630 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004631 return ret;
4632}
4633
Josef Bacik16e75492013-10-22 12:18:51 -04004634static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4635 u64 offset, u64 len)
4636{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004637 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004638 struct btrfs_trans_handle *trans;
4639 int ret;
4640
4641 /*
4642 * Still need to make sure the inode looks like it's been updated so
4643 * that any holes get logged if we fsync.
4644 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004645 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4646 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004647 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4648 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4649 return 0;
4650 }
4651
4652 /*
4653 * 1 - for the one we're dropping
4654 * 1 - for the one we're adding
4655 * 1 - for updating the inode.
4656 */
4657 trans = btrfs_start_transaction(root, 3);
4658 if (IS_ERR(trans))
4659 return PTR_ERR(trans);
4660
4661 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4662 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004663 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004664 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004665 return ret;
4666 }
4667
David Sterbaf85b7372017-01-20 14:54:07 +01004668 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4669 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004670 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004671 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004672 else
4673 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004674 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004675 return ret;
4676}
4677
Josef Bacik695a0d02011-03-04 15:46:53 -05004678/*
4679 * This function puts in dummy file extents for the area we're creating a hole
4680 * for. So if we are truncating this file to a larger size we need to insert
4681 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4682 * the range between oldsize and size
4683 */
Josef Bacika41ad392011-01-31 15:30:16 -05004684int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004685{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004686 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004687 struct btrfs_root *root = BTRFS_I(inode)->root;
4688 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004689 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004690 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004691 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004692 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4693 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004694 u64 last_byte;
4695 u64 cur_offset;
4696 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004697 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004698
Josef Bacika71754f2013-06-17 17:14:39 -04004699 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304700 * If our size started in the middle of a block we need to zero out the
4701 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004702 * expose stale data.
4703 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304704 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004705 if (err)
4706 return err;
4707
Yan Zheng9036c102008-10-30 14:19:41 -04004708 if (size <= hole_start)
4709 return 0;
4710
David Sterbab272ae22020-02-05 19:09:33 +01004711 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004712 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004713 cur_offset = hole_start;
4714 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004715 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004716 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004717 if (IS_ERR(em)) {
4718 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004719 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004720 break;
4721 }
Yan Zheng9036c102008-10-30 14:19:41 -04004722 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004723 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004724 hole_size = last_byte - cur_offset;
4725
Yan, Zheng80825102009-11-12 09:35:36 +00004726 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004727 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004728
Josef Bacik16e75492013-10-22 12:18:51 -04004729 err = maybe_insert_hole(root, inode, cur_offset,
4730 hole_size);
4731 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004732 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004733
4734 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4735 cur_offset, hole_size);
4736 if (err)
4737 break;
4738
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004739 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004740 cur_offset + hole_size - 1, 0);
4741 hole_em = alloc_extent_map();
4742 if (!hole_em) {
4743 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4744 &BTRFS_I(inode)->runtime_flags);
4745 goto next;
4746 }
4747 hole_em->start = cur_offset;
4748 hole_em->len = hole_size;
4749 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004750
Josef Bacik5dc562c2012-08-17 13:14:17 -04004751 hole_em->block_start = EXTENT_MAP_HOLE;
4752 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004753 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004754 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004755 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004756 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004757
4758 while (1) {
4759 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004760 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004761 write_unlock(&em_tree->lock);
4762 if (err != -EEXIST)
4763 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004764 btrfs_drop_extent_cache(BTRFS_I(inode),
4765 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004766 cur_offset +
4767 hole_size - 1, 0);
4768 }
4769 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004770 } else {
4771 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4772 cur_offset, hole_size);
4773 if (err)
4774 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004775 }
Josef Bacik16e75492013-10-22 12:18:51 -04004776next:
Yan Zheng9036c102008-10-30 14:19:41 -04004777 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004778 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004779 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004780 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004781 break;
4782 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004783 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004784 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004785 return err;
4786}
4787
Eric Sandeen3972f262013-01-12 02:57:22 +00004788static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004789{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004790 struct btrfs_root *root = BTRFS_I(inode)->root;
4791 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004792 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004793 loff_t newsize = attr->ia_size;
4794 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004795 int ret;
4796
Eric Sandeen3972f262013-01-12 02:57:22 +00004797 /*
4798 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4799 * special case where we need to update the times despite not having
4800 * these flags set. For all other operations the VFS set these flags
4801 * explicitly if it wants a timestamp update.
4802 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004803 if (newsize != oldsize) {
4804 inode_inc_iversion(inode);
4805 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4806 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004807 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004808 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004809
Josef Bacika41ad392011-01-31 15:30:16 -05004810 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004811 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004812 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004813 * This is to ensure the snapshot captures a fully consistent
4814 * state of this file - if the snapshot captures this expanding
4815 * truncation, it must capture all writes that happened before
4816 * this truncation.
4817 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004818 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004819 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004820 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004821 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004822 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004823 }
Yan, Zheng80825102009-11-12 09:35:36 +00004824
Miao Xief4a2f4c2011-12-14 20:12:01 -05004825 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004826 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004827 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004828 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004829 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004830
4831 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004832 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304833 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004834 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004835 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004836 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004837 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004838
Josef Bacika41ad392011-01-31 15:30:16 -05004839 /*
4840 * We're truncating a file that used to have good data down to
4841 * zero. Make sure it gets into the ordered flush list so that
4842 * any new writes get down to disk quickly.
4843 */
4844 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004845 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4846 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004847
Josef Bacika41ad392011-01-31 15:30:16 -05004848 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004849
David Sterba8e0fa5d2020-06-09 19:21:48 +02004850 /* Disable nonlocked read DIO to avoid the endless truncate */
4851 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004852 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004853 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004854
Filipe Manana213e8c52018-02-06 20:40:31 +00004855 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004856 if (ret && inode->i_nlink) {
4857 int err;
4858
4859 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004860 * Truncate failed, so fix up the in-memory size. We
4861 * adjusted disk_i_size down as we removed extents, so
4862 * wait for disk_i_size to be stable and then update the
4863 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004864 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004865 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004866 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004867 return err;
4868 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004869 }
Yan, Zheng80825102009-11-12 09:35:36 +00004870 }
4871
Josef Bacika41ad392011-01-31 15:30:16 -05004872 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004873}
4874
Chris Mason39279cc2007-06-12 06:35:45 -04004875static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4876{
David Howells2b0143b2015-03-17 22:25:59 +00004877 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004878 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004879 int err;
4880
Li Zefanb83cc962010-12-20 16:04:08 +08004881 if (btrfs_root_readonly(root))
4882 return -EROFS;
4883
Jan Kara31051c82016-05-26 16:55:18 +02004884 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004885 if (err)
4886 return err;
4887
Chris Mason5a3f23d2009-03-31 13:27:11 -04004888 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004889 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004890 if (err)
4891 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004892 }
Yan Zheng9036c102008-10-30 14:19:41 -04004893
Christoph Hellwig10257742010-06-04 11:30:02 +02004894 if (attr->ia_valid) {
4895 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004896 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004897 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004898
Josef Bacik22c44fe2011-11-30 10:45:38 -05004899 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004900 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004901 }
4902
Chris Mason39279cc2007-06-12 06:35:45 -04004903 return err;
4904}
Chris Mason61295eb2008-01-14 16:24:38 -05004905
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004906/*
4907 * While truncating the inode pages during eviction, we get the VFS calling
4908 * btrfs_invalidatepage() against each page of the inode. This is slow because
4909 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4910 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4911 * extent_state structures over and over, wasting lots of time.
4912 *
4913 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4914 * those expensive operations on a per page basis and do only the ordered io
4915 * finishing, while we release here the extent_map and extent_state structures,
4916 * without the excessive merging and splitting.
4917 */
4918static void evict_inode_truncate_pages(struct inode *inode)
4919{
4920 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4921 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4922 struct rb_node *node;
4923
4924 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004925 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004926
4927 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004928 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004929 struct extent_map *em;
4930
Liu Bo07e1ce02018-08-23 03:51:52 +08004931 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004932 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004933 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4934 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004935 remove_extent_mapping(map_tree, em);
4936 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004937 if (need_resched()) {
4938 write_unlock(&map_tree->lock);
4939 cond_resched();
4940 write_lock(&map_tree->lock);
4941 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004942 }
4943 write_unlock(&map_tree->lock);
4944
Filipe Manana6ca07092015-05-26 00:55:42 +01004945 /*
4946 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004947 * We can have ongoing bios started by readahead that have
4948 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004949 * still in progress (unlocked the pages in the bio but did not yet
4950 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004951 * ranges can still be locked and eviction started because before
4952 * submitting those bios, which are executed by a separate task (work
4953 * queue kthread), inode references (inode->i_count) were not taken
4954 * (which would be dropped in the end io callback of each bio).
4955 * Therefore here we effectively end up waiting for those bios and
4956 * anyone else holding locked ranges without having bumped the inode's
4957 * reference count - if we don't do it, when they access the inode's
4958 * io_tree to unlock a range it may be too late, leading to an
4959 * use-after-free issue.
4960 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004961 spin_lock(&io_tree->lock);
4962 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4963 struct extent_state *state;
4964 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004965 u64 start;
4966 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004967 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004968
4969 node = rb_first(&io_tree->state);
4970 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004971 start = state->start;
4972 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004973 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004974 spin_unlock(&io_tree->lock);
4975
David Sterbaff13db42015-12-03 14:30:40 +01004976 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004977
4978 /*
4979 * If still has DELALLOC flag, the extent didn't reach disk,
4980 * and its reserved space won't be freed by delayed_ref.
4981 * So we need to free its reserved space here.
4982 * (Refer to comment in btrfs_invalidatepage, case 2)
4983 *
4984 * Note, end is the bytenr of last byte, so we need + 1 here.
4985 */
Filipe Manana421f0922018-10-12 13:02:48 +01004986 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03004987 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
4988 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004989
Filipe Manana6ca07092015-05-26 00:55:42 +01004990 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004991 EXTENT_LOCKED | EXTENT_DELALLOC |
4992 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4993 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004994
Filipe Manana7064dd52014-08-08 02:47:05 +01004995 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004996 spin_lock(&io_tree->lock);
4997 }
4998 spin_unlock(&io_tree->lock);
4999}
5000
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005001static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005002 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005003{
5004 struct btrfs_fs_info *fs_info = root->fs_info;
5005 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005006 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005007 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005008 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005009
Josef Bacikd3984c92019-08-01 18:19:37 -04005010 /*
5011 * Eviction should be taking place at some place safe because of our
5012 * delayed iputs. However the normal flushing code will run delayed
5013 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5014 *
5015 * We reserve the delayed_refs_extra here again because we can't use
5016 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5017 * above. We reserve our extra bit here because we generate a ton of
5018 * delayed refs activity by truncating.
5019 *
5020 * If we cannot make our reservation we'll attempt to steal from the
5021 * global reserve, because we really want to be able to free up space.
5022 */
5023 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5024 BTRFS_RESERVE_FLUSH_EVICT);
5025 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005026 /*
5027 * Try to steal from the global reserve if there is space for
5028 * it.
5029 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005030 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5031 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5032 btrfs_warn(fs_info,
5033 "could not allocate space for delete; will truncate on mount");
5034 return ERR_PTR(-ENOSPC);
5035 }
5036 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005037 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005038
5039 trans = btrfs_join_transaction(root);
5040 if (IS_ERR(trans))
5041 return trans;
5042
5043 if (delayed_refs_extra) {
5044 trans->block_rsv = &fs_info->trans_block_rsv;
5045 trans->bytes_reserved = delayed_refs_extra;
5046 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5047 delayed_refs_extra, 1);
5048 }
5049 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005050}
5051
Al Virobd555972010-06-07 11:35:40 -04005052void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005053{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005054 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005055 struct btrfs_trans_handle *trans;
5056 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005057 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005058 int ret;
5059
liubo1abe9b82011-03-24 11:18:59 +00005060 trace_btrfs_inode_evict(inode);
5061
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005062 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005063 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005064 return;
5065 }
5066
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005067 evict_inode_truncate_pages(inode);
5068
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005069 if (inode->i_nlink &&
5070 ((btrfs_root_refs(&root->root_item) != 0 &&
5071 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005072 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005073 goto no_delete;
5074
Omar Sandoval27919062018-05-11 13:13:37 -07005075 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005076 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005077
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005078 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005079
Omar Sandoval7b40b692018-05-11 13:13:33 -07005080 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005081 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005082
Yan, Zheng76dda932009-09-21 16:00:26 -04005083 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005084 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5085 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005086 goto no_delete;
5087 }
5088
Nikolay Borisovaa790212017-01-10 20:35:40 +02005089 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005090 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005091 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005092
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005093 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005094 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005095 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005096 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005097 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005098
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005099 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005100
Yan, Zheng80825102009-11-12 09:35:36 +00005101 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005102 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005103 if (IS_ERR(trans))
5104 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005105
5106 trans->block_rsv = rsv;
5107
Yan, Zhengd68fc572010-05-16 10:49:58 -04005108 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005109 trans->block_rsv = &fs_info->trans_block_rsv;
5110 btrfs_end_transaction(trans);
5111 btrfs_btree_balance_dirty(fs_info);
5112 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5113 goto free_rsv;
5114 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005115 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005116 }
5117
Josef Bacik4ef31a42013-08-13 14:10:08 -04005118 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005119 * Errors here aren't a big deal, it just means we leave orphan items in
5120 * the tree. They will be cleaned up on the next mount. If the inode
5121 * number gets reused, cleanup deletes the orphan item without doing
5122 * anything, and unlink reuses the existing orphan item.
5123 *
5124 * If it turns out that we are dropping too many of these, we might want
5125 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005126 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005127 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005128 if (!IS_ERR(trans)) {
5129 trans->block_rsv = rsv;
5130 btrfs_orphan_del(trans, BTRFS_I(inode));
5131 trans->block_rsv = &fs_info->trans_block_rsv;
5132 btrfs_end_transaction(trans);
5133 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005134
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005135 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005136 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005137 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005138
Omar Sandoval27919062018-05-11 13:13:37 -07005139free_rsv:
5140 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005141no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005142 /*
5143 * If we didn't successfully delete, the orphan item will still be in
5144 * the tree and we'll retry on the next mount. Again, we might also want
5145 * to retry these periodically in the future.
5146 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005147 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005148 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005149}
5150
5151/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005152 * Return the key found in the dir entry in the location pointer, fill @type
5153 * with BTRFS_FT_*, and return 0.
5154 *
Su Yue005d6712018-03-05 17:13:37 +08005155 * If no dir entries were found, returns -ENOENT.
5156 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005157 */
5158static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005159 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005160{
5161 const char *name = dentry->d_name.name;
5162 int namelen = dentry->d_name.len;
5163 struct btrfs_dir_item *di;
5164 struct btrfs_path *path;
5165 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005166 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005167
5168 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005169 if (!path)
5170 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005171
David Sterbaf85b7372017-01-20 14:54:07 +01005172 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5173 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005174 if (IS_ERR_OR_NULL(di)) {
5175 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005176 goto out;
5177 }
Chris Masond3977122009-01-05 21:25:51 -05005178
Chris Mason5f39d392007-10-15 16:14:19 -04005179 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005180 if (location->type != BTRFS_INODE_ITEM_KEY &&
5181 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005182 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005183 btrfs_warn(root->fs_info,
5184"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5185 __func__, name, btrfs_ino(BTRFS_I(dir)),
5186 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005187 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005188 if (!ret)
5189 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005190out:
Chris Mason39279cc2007-06-12 06:35:45 -04005191 btrfs_free_path(path);
5192 return ret;
5193}
5194
5195/*
5196 * when we hit a tree root in a directory, the btrfs part of the inode
5197 * needs to be changed to reflect the root directory of the tree root. This
5198 * is kind of like crossing a mount point.
5199 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005200static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005201 struct inode *dir,
5202 struct dentry *dentry,
5203 struct btrfs_key *location,
5204 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005205{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005206 struct btrfs_path *path;
5207 struct btrfs_root *new_root;
5208 struct btrfs_root_ref *ref;
5209 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005210 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005211 int ret;
5212 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005213
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005214 path = btrfs_alloc_path();
5215 if (!path) {
5216 err = -ENOMEM;
5217 goto out;
5218 }
Chris Mason39279cc2007-06-12 06:35:45 -04005219
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005220 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005221 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5222 key.type = BTRFS_ROOT_REF_KEY;
5223 key.offset = location->objectid;
5224
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005225 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005226 if (ret) {
5227 if (ret < 0)
5228 err = ret;
5229 goto out;
5230 }
Chris Mason39279cc2007-06-12 06:35:45 -04005231
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005232 leaf = path->nodes[0];
5233 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005234 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005235 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5236 goto out;
5237
5238 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5239 (unsigned long)(ref + 1),
5240 dentry->d_name.len);
5241 if (ret)
5242 goto out;
5243
David Sterbab3b4aa72011-04-21 01:20:15 +02005244 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005245
David Sterba56e93572020-05-15 19:35:55 +02005246 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005247 if (IS_ERR(new_root)) {
5248 err = PTR_ERR(new_root);
5249 goto out;
5250 }
5251
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005252 *sub_root = new_root;
5253 location->objectid = btrfs_root_dirid(&new_root->root_item);
5254 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005255 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005256 err = 0;
5257out:
5258 btrfs_free_path(path);
5259 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005260}
5261
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005262static void inode_tree_add(struct inode *inode)
5263{
5264 struct btrfs_root *root = BTRFS_I(inode)->root;
5265 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005266 struct rb_node **p;
5267 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005268 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005269 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005270
Al Viro1d3382cb2010-10-23 15:19:20 -04005271 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005272 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005273 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005274 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005275 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005276 while (*p) {
5277 parent = *p;
5278 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5279
David Sterba37508512018-06-29 10:56:40 +02005280 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005281 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005282 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005283 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005284 else {
5285 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005286 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005287 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005288 RB_CLEAR_NODE(parent);
5289 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005290 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005291 }
5292 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005293 rb_link_node(new, parent, p);
5294 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005295 spin_unlock(&root->inode_lock);
5296}
5297
5298static void inode_tree_del(struct inode *inode)
5299{
5300 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005301 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005302
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005303 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005304 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005305 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005306 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005307 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005308 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005309 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005310
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005311 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005312 spin_lock(&root->inode_lock);
5313 empty = RB_EMPTY_ROOT(&root->inode_tree);
5314 spin_unlock(&root->inode_lock);
5315 if (empty)
5316 btrfs_add_dead_root(root);
5317 }
5318}
5319
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005320
Chris Masone02119d2008-09-05 16:13:11 -04005321static int btrfs_init_locked_inode(struct inode *inode, void *p)
5322{
5323 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005324
5325 inode->i_ino = args->ino;
5326 BTRFS_I(inode)->location.objectid = args->ino;
5327 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5328 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005329 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5330 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005331 return 0;
5332}
5333
5334static int btrfs_find_actor(struct inode *inode, void *opaque)
5335{
5336 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005337
5338 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005339 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005340}
5341
David Sterba0202e832020-05-15 19:35:59 +02005342static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005343 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005344{
5345 struct inode *inode;
5346 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005347 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005348
David Sterba0202e832020-05-15 19:35:59 +02005349 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005350 args.root = root;
5351
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005352 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005353 btrfs_init_locked_inode,
5354 (void *)&args);
5355 return inode;
5356}
5357
David Sterba4c66e0d2019-10-03 19:09:35 +02005358/*
David Sterba0202e832020-05-15 19:35:59 +02005359 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005360 * Path can be preallocated to prevent recursing back to iget through
5361 * allocator. NULL is also valid but may require an additional allocation
5362 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305363 */
David Sterba0202e832020-05-15 19:35:59 +02005364struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005365 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305366{
5367 struct inode *inode;
5368
David Sterba0202e832020-05-15 19:35:59 +02005369 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305370 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005371 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305372
5373 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005374 int ret;
5375
Filipe Manana4222ea72018-10-24 10:13:03 +01005376 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005377 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005378 inode_tree_add(inode);
5379 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005380 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005381 iget_failed(inode);
5382 /*
5383 * ret > 0 can come from btrfs_search_slot called by
5384 * btrfs_read_locked_inode, this means the inode item
5385 * was not found.
5386 */
5387 if (ret > 0)
5388 ret = -ENOENT;
5389 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005390 }
5391 }
5392
Balaji Rao1a54ef82008-07-21 02:01:04 +05305393 return inode;
5394}
5395
David Sterba0202e832020-05-15 19:35:59 +02005396struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005397{
David Sterba0202e832020-05-15 19:35:59 +02005398 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005399}
5400
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005401static struct inode *new_simple_dir(struct super_block *s,
5402 struct btrfs_key *key,
5403 struct btrfs_root *root)
5404{
5405 struct inode *inode = new_inode(s);
5406
5407 if (!inode)
5408 return ERR_PTR(-ENOMEM);
5409
Josef Bacik5c8fd992020-02-14 16:11:43 -05005410 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005411 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005412 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005413
5414 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005415 /*
5416 * We only need lookup, the rest is read-only and there's no inode
5417 * associated with the dentry
5418 */
5419 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005420 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005421 inode->i_fop = &simple_dir_operations;
5422 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005423 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305424 inode->i_atime = inode->i_mtime;
5425 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005426 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005427
5428 return inode;
5429}
5430
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005431static inline u8 btrfs_inode_type(struct inode *inode)
5432{
5433 /*
5434 * Compile-time asserts that generic FT_* types still match
5435 * BTRFS_FT_* types
5436 */
5437 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5438 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5439 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5440 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5441 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5442 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5443 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5444 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5445
5446 return fs_umode_to_ftype(inode->i_mode);
5447}
5448
Chris Mason3de45862008-11-17 21:02:50 -05005449struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005450{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005451 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005452 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005453 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005454 struct btrfs_root *sub_root = root;
5455 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005456 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005457 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005458
5459 if (dentry->d_name.len > BTRFS_NAME_LEN)
5460 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005461
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005462 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005463 if (ret < 0)
5464 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005465
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005466 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005467 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005468 if (IS_ERR(inode))
5469 return inode;
5470
5471 /* Do extra check against inode mode with di_type */
5472 if (btrfs_inode_type(inode) != di_type) {
5473 btrfs_crit(fs_info,
5474"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5475 inode->i_mode, btrfs_inode_type(inode),
5476 di_type);
5477 iput(inode);
5478 return ERR_PTR(-EUCLEAN);
5479 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005480 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005481 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005482
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005483 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005484 &location, &sub_root);
5485 if (ret < 0) {
5486 if (ret != -ENOENT)
5487 inode = ERR_PTR(ret);
5488 else
5489 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5490 } else {
David Sterba0202e832020-05-15 19:35:59 +02005491 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005492 }
Josef Bacik87270022020-01-24 09:32:31 -05005493 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005494 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005495
Julia Lawall34d19ba2011-01-24 19:55:19 +00005496 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005497 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005498 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005499 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005500 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005501 if (ret) {
5502 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005503 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005504 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005505 }
5506
Chris Mason3de45862008-11-17 21:02:50 -05005507 return inode;
5508}
5509
Nick Pigginfe15ce42011-01-07 17:49:23 +11005510static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005511{
5512 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005513 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005514
Li Zefan848cce02012-02-21 17:04:28 +08005515 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005516 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005517
Li Zefan848cce02012-02-21 17:04:28 +08005518 if (inode) {
5519 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005520 if (btrfs_root_refs(&root->root_item) == 0)
5521 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005522
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005523 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005524 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005525 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005526 return 0;
5527}
5528
Chris Mason3de45862008-11-17 21:02:50 -05005529static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005530 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005531{
Al Viro3837d202018-10-10 16:38:27 -04005532 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005533
Al Viro3837d202018-10-10 16:38:27 -04005534 if (inode == ERR_PTR(-ENOENT))
5535 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005536 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005537}
5538
Josef Bacik23b5ec72017-07-24 15:14:25 -04005539/*
5540 * All this infrastructure exists because dir_emit can fault, and we are holding
5541 * the tree lock when doing readdir. For now just allocate a buffer and copy
5542 * our information into that, and then dir_emit from the buffer. This is
5543 * similar to what NFS does, only we don't keep the buffer around in pagecache
5544 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5545 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5546 * tree lock.
5547 */
5548static int btrfs_opendir(struct inode *inode, struct file *file)
5549{
5550 struct btrfs_file_private *private;
5551
5552 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5553 if (!private)
5554 return -ENOMEM;
5555 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5556 if (!private->filldir_buf) {
5557 kfree(private);
5558 return -ENOMEM;
5559 }
5560 file->private_data = private;
5561 return 0;
5562}
5563
5564struct dir_entry {
5565 u64 ino;
5566 u64 offset;
5567 unsigned type;
5568 int name_len;
5569};
5570
5571static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5572{
5573 while (entries--) {
5574 struct dir_entry *entry = addr;
5575 char *name = (char *)(entry + 1);
5576
David Sterba92d32172018-04-16 21:10:14 +02005577 ctx->pos = get_unaligned(&entry->offset);
5578 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5579 get_unaligned(&entry->ino),
5580 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005581 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005582 addr += sizeof(struct dir_entry) +
5583 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005584 ctx->pos++;
5585 }
5586 return 0;
5587}
5588
Al Viro9cdda8d2013-05-22 16:48:09 -04005589static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005590{
Al Viro9cdda8d2013-05-22 16:48:09 -04005591 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005592 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005593 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005594 struct btrfs_dir_item *di;
5595 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005596 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005597 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005598 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005599 struct list_head ins_list;
5600 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005601 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005602 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005603 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005604 char *name_ptr;
5605 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005606 int entries = 0;
5607 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005608 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005609 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005610
Al Viro9cdda8d2013-05-22 16:48:09 -04005611 if (!dir_emit_dots(file, ctx))
5612 return 0;
5613
David Woodhouse49593bf2008-08-17 17:08:36 +01005614 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005615 if (!path)
5616 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005617
Josef Bacik23b5ec72017-07-24 15:14:25 -04005618 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005619 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005620
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005621 INIT_LIST_HEAD(&ins_list);
5622 INIT_LIST_HEAD(&del_list);
5623 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005624
Josef Bacik23b5ec72017-07-24 15:14:25 -04005625again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005626 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005627 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005628 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005629
Chris Mason39279cc2007-06-12 06:35:45 -04005630 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5631 if (ret < 0)
5632 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005633
5634 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005635 struct dir_entry *entry;
5636
Chris Mason5f39d392007-10-15 16:14:19 -04005637 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005638 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005639 if (slot >= btrfs_header_nritems(leaf)) {
5640 ret = btrfs_next_leaf(root, path);
5641 if (ret < 0)
5642 goto err;
5643 else if (ret > 0)
5644 break;
5645 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005646 }
Chris Mason3de45862008-11-17 21:02:50 -05005647
Chris Mason5f39d392007-10-15 16:14:19 -04005648 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5649
5650 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005651 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005652 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005653 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005654 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005655 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005656 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005657 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005658 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005659 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005660 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5661 PAGE_SIZE) {
5662 btrfs_release_path(path);
5663 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5664 if (ret)
5665 goto nopos;
5666 addr = private->filldir_buf;
5667 entries = 0;
5668 total_len = 0;
5669 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005670 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005671
5672 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005673 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005674 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005675 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5676 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005677 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005678 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005679 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005680 put_unaligned(location.objectid, &entry->ino);
5681 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005682 entries++;
5683 addr += sizeof(struct dir_entry) + name_len;
5684 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005685next:
5686 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005687 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005688 btrfs_release_path(path);
5689
5690 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5691 if (ret)
5692 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005693
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005694 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005695 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005696 goto nopos;
5697
Zach Browndb62efb2013-07-11 16:19:42 -07005698 /*
5699 * Stop new entries from being returned after we return the last
5700 * entry.
5701 *
5702 * New directory entries are assigned a strictly increasing
5703 * offset. This means that new entries created during readdir
5704 * are *guaranteed* to be seen in the future by that readdir.
5705 * This has broken buggy programs which operate on names as
5706 * they're returned by readdir. Until we re-use freed offsets
5707 * we have this hack to stop new entries from being returned
5708 * under the assumption that they'll never reach this huge
5709 * offset.
5710 *
5711 * This is being careful not to overflow 32bit loff_t unless the
5712 * last entry requires it because doing so has broken 32bit apps
5713 * in the past.
5714 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005715 if (ctx->pos >= INT_MAX)
5716 ctx->pos = LLONG_MAX;
5717 else
5718 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005719nopos:
5720 ret = 0;
5721err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005722 if (put)
5723 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005724 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005725 return ret;
5726}
5727
Chris Mason39279cc2007-06-12 06:35:45 -04005728/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005729 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005730 * inode changes. But, it is most likely to find the inode in cache.
5731 * FIXME, needs more benchmarking...there are no reasons other than performance
5732 * to keep or drop this code.
5733 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005734static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005735{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005736 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005737 struct btrfs_root *root = BTRFS_I(inode)->root;
5738 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005739 int ret;
5740
Josef Bacik72ac3c02012-05-23 14:13:11 -04005741 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005742 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005743
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005744 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005745 if (IS_ERR(trans))
5746 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005747
5748 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005749 if (ret && ret == -ENOSPC) {
5750 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005751 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005752 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005753 if (IS_ERR(trans))
5754 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005755
Chris Mason94b60442010-05-26 11:02:00 -04005756 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005757 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005758 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005759 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005760 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005761
5762 return ret;
5763}
5764
5765/*
5766 * This is a copy of file_update_time. We need this so we can return error on
5767 * ENOSPC for updating the inode in the case of file write and mmap writes.
5768 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005769static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005770 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005771{
Alexander Block2bc5565282012-06-15 09:49:33 +02005772 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005773 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005774
5775 if (btrfs_root_readonly(root))
5776 return -EROFS;
5777
Josef Bacike41f9412012-03-26 09:46:47 -04005778 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005779 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005780 if (flags & S_CTIME)
5781 inode->i_ctime = *now;
5782 if (flags & S_MTIME)
5783 inode->i_mtime = *now;
5784 if (flags & S_ATIME)
5785 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005786 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005787}
5788
Chris Masond352ac62008-09-29 15:18:18 -04005789/*
5790 * find the highest existing sequence number in a directory
5791 * and then set the in-memory index_cnt variable to reflect
5792 * free sequence numbers
5793 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005794static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005795{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005796 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005797 struct btrfs_key key, found_key;
5798 struct btrfs_path *path;
5799 struct extent_buffer *leaf;
5800 int ret;
5801
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005802 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005803 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005804 key.offset = (u64)-1;
5805
5806 path = btrfs_alloc_path();
5807 if (!path)
5808 return -ENOMEM;
5809
5810 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5811 if (ret < 0)
5812 goto out;
5813 /* FIXME: we should be able to handle this */
5814 if (ret == 0)
5815 goto out;
5816 ret = 0;
5817
5818 /*
5819 * MAGIC NUMBER EXPLANATION:
5820 * since we search a directory based on f_pos we have to start at 2
5821 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5822 * else has to start at 2
5823 */
5824 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005825 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005826 goto out;
5827 }
5828
5829 path->slots[0]--;
5830
5831 leaf = path->nodes[0];
5832 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5833
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005834 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005835 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005836 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005837 goto out;
5838 }
5839
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005840 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005841out:
5842 btrfs_free_path(path);
5843 return ret;
5844}
5845
Chris Masond352ac62008-09-29 15:18:18 -04005846/*
5847 * helper to find a free sequence number in a given directory. This current
5848 * code is very simple, later versions will do smarter things in the btree
5849 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005850int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005851{
5852 int ret = 0;
5853
Nikolay Borisov877574e2017-02-20 13:50:33 +02005854 if (dir->index_cnt == (u64)-1) {
5855 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005856 if (ret) {
5857 ret = btrfs_set_inode_index_count(dir);
5858 if (ret)
5859 return ret;
5860 }
Josef Bacikaec74772008-07-24 12:12:38 -04005861 }
5862
Nikolay Borisov877574e2017-02-20 13:50:33 +02005863 *index = dir->index_cnt;
5864 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005865
5866 return ret;
5867}
5868
Chris Masonb0d5d102014-09-08 13:08:51 -07005869static int btrfs_insert_inode_locked(struct inode *inode)
5870{
5871 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005872
5873 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005874 args.root = BTRFS_I(inode)->root;
5875
5876 return insert_inode_locked4(inode,
5877 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5878 btrfs_find_actor, &args);
5879}
5880
Anand Jain19aee8d2017-07-18 17:37:05 +08005881/*
5882 * Inherit flags from the parent inode.
5883 *
5884 * Currently only the compression flags and the cow flags are inherited.
5885 */
5886static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5887{
5888 unsigned int flags;
5889
5890 if (!dir)
5891 return;
5892
5893 flags = BTRFS_I(dir)->flags;
5894
5895 if (flags & BTRFS_INODE_NOCOMPRESS) {
5896 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5897 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5898 } else if (flags & BTRFS_INODE_COMPRESS) {
5899 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5900 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5901 }
5902
5903 if (flags & BTRFS_INODE_NODATACOW) {
5904 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5905 if (S_ISREG(inode->i_mode))
5906 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5907 }
5908
David Sterba7b6a2212018-03-26 18:40:21 +02005909 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005910}
5911
Chris Mason39279cc2007-06-12 06:35:45 -04005912static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5913 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005914 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005915 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005916 u64 ref_objectid, u64 objectid,
5917 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005918{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005919 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005920 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005921 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005922 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005923 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005924 struct btrfs_inode_ref *ref;
5925 struct btrfs_key key[2];
5926 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005927 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005928 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005929 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005930 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005931
Chris Mason5f39d392007-10-15 16:14:19 -04005932 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005933 if (!path)
5934 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005935
Josef Bacik11a19a92019-09-09 10:12:04 -04005936 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005937 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005938 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005939 if (!inode) {
5940 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005941 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005942 }
Chris Mason39279cc2007-06-12 06:35:45 -04005943
Li Zefan581bb052011-04-20 10:06:11 +08005944 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005945 * O_TMPFILE, set link count to 0, so that after this point,
5946 * we fill in an inode item with the correct link count.
5947 */
5948 if (!name)
5949 set_nlink(inode, 0);
5950
5951 /*
Li Zefan581bb052011-04-20 10:06:11 +08005952 * we have to initialize this early, so we can reclaim the inode
5953 * number if we fail afterwards in this function.
5954 */
5955 inode->i_ino = objectid;
5956
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005957 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005958 trace_btrfs_inode_request(dir);
5959
Nikolay Borisov877574e2017-02-20 13:50:33 +02005960 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005961 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005962 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005963 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005964 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005965 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005966 } else if (dir) {
5967 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005968 }
5969 /*
5970 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005971 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005972 * number
5973 */
5974 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005975 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005976 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005977 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005978 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005979
Josef Bacik5dc562c2012-08-17 13:14:17 -04005980 /*
5981 * We could have gotten an inode number from somebody who was fsynced
5982 * and then removed in this same transaction, so let's just set full
5983 * sync since it will be a full sync anyway and this will blow away the
5984 * old info in the log.
5985 */
5986 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5987
Chris Mason9c583092008-01-29 15:15:18 -05005988 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005989 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005990 key[0].offset = 0;
5991
Chris Mason9c583092008-01-29 15:15:18 -05005992 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005993
5994 if (name) {
5995 /*
5996 * Start new inodes with an inode_ref. This is slightly more
5997 * efficient for small numbers of hard links since they will
5998 * be packed into one item. Extended refs will kick in if we
5999 * add more hard links than can fit in the ref item.
6000 */
6001 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006002 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006003 key[1].offset = ref_objectid;
6004
6005 sizes[1] = name_len + sizeof(*ref);
6006 }
Chris Mason9c583092008-01-29 15:15:18 -05006007
Chris Masonb0d5d102014-09-08 13:08:51 -07006008 location = &BTRFS_I(inode)->location;
6009 location->objectid = objectid;
6010 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006011 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006012
6013 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006014 if (ret < 0) {
6015 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006016 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006017 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006018
Chris Masonb9473432009-03-13 11:00:37 -04006019 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006020 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006021 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006022 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006023
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006024 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006025 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306026
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006027 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306028 inode->i_atime = inode->i_mtime;
6029 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006030 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306031
Chris Mason5f39d392007-10-15 16:14:19 -04006032 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6033 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006034 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006035 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006036 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006037
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006038 if (name) {
6039 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6040 struct btrfs_inode_ref);
6041 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6042 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6043 ptr = (unsigned long)(ref + 1);
6044 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6045 }
Chris Mason9c583092008-01-29 15:15:18 -05006046
Chris Mason5f39d392007-10-15 16:14:19 -04006047 btrfs_mark_buffer_dirty(path->nodes[0]);
6048 btrfs_free_path(path);
6049
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006050 btrfs_inherit_iflags(inode, dir);
6051
Al Viro569254b2011-07-24 17:08:40 -04006052 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006053 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006054 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006055 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006056 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6057 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006058 }
6059
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006060 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006061
6062 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006063 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006064
Alexander Block8ea05e32012-07-25 17:35:53 +02006065 btrfs_update_root_times(trans, root);
6066
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006067 ret = btrfs_inode_inherit_props(trans, inode, dir);
6068 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006069 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006070 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006071 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006072
Chris Mason39279cc2007-06-12 06:35:45 -04006073 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006074
6075fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006076 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006077fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006078 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006079 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006080 btrfs_free_path(path);
6081 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006082}
6083
Chris Masond352ac62008-09-29 15:18:18 -04006084/*
6085 * utility function to add 'inode' into 'parent_inode' with
6086 * a give name and a given sequence number.
6087 * if 'add_backref' is true, also insert a backref from the
6088 * inode to the parent directory.
6089 */
Chris Masone02119d2008-09-05 16:13:11 -04006090int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006091 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006092 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006093{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006094 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006095 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006096 struct btrfs_root *root = parent_inode->root;
6097 u64 ino = btrfs_ino(inode);
6098 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006099
Li Zefan33345d012011-04-20 10:31:50 +08006100 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006101 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006102 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006103 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006104 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006105 key.offset = 0;
6106 }
Chris Mason39279cc2007-06-12 06:35:45 -04006107
Li Zefan33345d012011-04-20 10:31:50 +08006108 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006109 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006110 root->root_key.objectid, parent_ino,
6111 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006112 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006113 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6114 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006115 }
6116
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006117 /* Nothing to clean up yet */
6118 if (ret)
6119 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006120
Lu Fengqi684572d2018-08-04 21:10:57 +08006121 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006122 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006123 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006124 goto fail_dir_item;
6125 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006126 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006127 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006128 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006129
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006130 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006131 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006132 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006133 /*
6134 * If we are replaying a log tree, we do not want to update the mtime
6135 * and ctime of the parent directory with the current time, since the
6136 * log replay procedure is responsible for setting them to their correct
6137 * values (the ones it had when the fsync was done).
6138 */
6139 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6140 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6141
6142 parent_inode->vfs_inode.i_mtime = now;
6143 parent_inode->vfs_inode.i_ctime = now;
6144 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006145 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006146 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006147 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006148 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006149
6150fail_dir_item:
6151 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6152 u64 local_index;
6153 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006154 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006155 root->root_key.objectid, parent_ino,
6156 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006157 if (err)
6158 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006159 } else if (add_backref) {
6160 u64 local_index;
6161 int err;
6162
6163 err = btrfs_del_inode_ref(trans, root, name, name_len,
6164 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006165 if (err)
6166 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006167 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006168
6169 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006170 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006171}
6172
6173static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006174 struct btrfs_inode *dir, struct dentry *dentry,
6175 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006176{
Josef Bacika1b075d2010-11-19 20:36:11 +00006177 int err = btrfs_add_link(trans, dir, inode,
6178 dentry->d_name.name, dentry->d_name.len,
6179 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006180 if (err > 0)
6181 err = -EEXIST;
6182 return err;
6183}
6184
Josef Bacik618e21d2007-07-11 10:18:17 -04006185static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006186 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006187{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006188 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006189 struct btrfs_trans_handle *trans;
6190 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006191 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006192 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006193 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006194 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006195
Josef Bacik9ed74f22009-09-11 16:12:44 -04006196 /*
6197 * 2 for inode item and ref
6198 * 2 for dir items
6199 * 1 for xattr if selinux is on
6200 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006201 trans = btrfs_start_transaction(root, 5);
6202 if (IS_ERR(trans))
6203 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006204
Li Zefan581bb052011-04-20 10:06:11 +08006205 err = btrfs_find_free_ino(root, &objectid);
6206 if (err)
6207 goto out_unlock;
6208
Josef Bacikaec74772008-07-24 12:12:38 -04006209 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006210 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6211 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006212 if (IS_ERR(inode)) {
6213 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006214 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006215 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006216 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006217
Casey Schauflerad19db72011-12-15 10:09:07 -05006218 /*
6219 * If the active LSM wants to access the inode during
6220 * d_instantiate it needs these. Smack checks to see
6221 * if the filesystem supports xattrs by looking at the
6222 * ops vector.
6223 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006224 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006225 init_special_inode(inode, inode->i_mode, rdev);
6226
6227 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006228 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006229 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006230
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006231 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6232 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006233 if (err)
6234 goto out_unlock;
6235
6236 btrfs_update_inode(trans, root, inode);
6237 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006238
Josef Bacik618e21d2007-07-11 10:18:17 -04006239out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006240 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006241 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006242 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006243 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006244 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006245 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006246 return err;
6247}
6248
Chris Mason39279cc2007-06-12 06:35:45 -04006249static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006250 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006251{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006252 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006253 struct btrfs_trans_handle *trans;
6254 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006255 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006256 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006257 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006258 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006259
Josef Bacik9ed74f22009-09-11 16:12:44 -04006260 /*
6261 * 2 for inode item and ref
6262 * 2 for dir items
6263 * 1 for xattr if selinux is on
6264 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006265 trans = btrfs_start_transaction(root, 5);
6266 if (IS_ERR(trans))
6267 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006268
Li Zefan581bb052011-04-20 10:06:11 +08006269 err = btrfs_find_free_ino(root, &objectid);
6270 if (err)
6271 goto out_unlock;
6272
Josef Bacikaec74772008-07-24 12:12:38 -04006273 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006274 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6275 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006276 if (IS_ERR(inode)) {
6277 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006278 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006279 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006280 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006281 /*
6282 * If the active LSM wants to access the inode during
6283 * d_instantiate it needs these. Smack checks to see
6284 * if the filesystem supports xattrs by looking at the
6285 * ops vector.
6286 */
6287 inode->i_fop = &btrfs_file_operations;
6288 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006289 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006290
6291 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6292 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006293 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006294
6295 err = btrfs_update_inode(trans, root, inode);
6296 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006297 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006298
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006299 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6300 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006301 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006302 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006303
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006304 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006305 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006306
Chris Mason39279cc2007-06-12 06:35:45 -04006307out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006308 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006309 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006310 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006311 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006312 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006313 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006314 return err;
6315}
6316
6317static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6318 struct dentry *dentry)
6319{
Filipe Manana271dba452016-01-05 16:24:05 +00006320 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006321 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006322 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006323 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006324 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006325 int err;
6326 int drop_inode = 0;
6327
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006328 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006329 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006330 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006331
Mark Fashehf1863732012-08-08 11:32:27 -07006332 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006333 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006334
Nikolay Borisov877574e2017-02-20 13:50:33 +02006335 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006336 if (err)
6337 goto fail;
6338
Yan, Zhenga22285a2010-05-16 10:48:46 -04006339 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006340 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006341 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006342 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006343 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006344 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006345 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006346 if (IS_ERR(trans)) {
6347 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006348 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006349 goto fail;
6350 }
Chris Mason5f39d392007-10-15 16:14:19 -04006351
Miao Xie67de1172013-12-26 13:07:06 +08006352 /* There are several dir indexes for this inode, clear the cache. */
6353 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006354 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006355 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006356 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006357 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006358 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006359
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006360 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6361 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006362
Yan, Zhenga5719522009-09-24 09:17:31 -04006363 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006364 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006365 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006366 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006367 int ret;
6368
Yan, Zhenga5719522009-09-24 09:17:31 -04006369 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006370 if (err)
6371 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006372 if (inode->i_nlink == 1) {
6373 /*
6374 * If new hard link count is 1, it's a file created
6375 * with open(2) O_TMPFILE flag.
6376 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006377 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006378 if (err)
6379 goto fail;
6380 }
Al Viro08c422c2011-12-23 07:58:13 -05006381 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006382 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6383 true, NULL);
6384 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6385 err = btrfs_commit_transaction(trans);
6386 trans = NULL;
6387 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006388 }
Chris Mason39279cc2007-06-12 06:35:45 -04006389
Chris Mason1832a6d2007-12-21 16:27:21 -05006390fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006391 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006392 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006393 if (drop_inode) {
6394 inode_dec_link_count(inode);
6395 iput(inode);
6396 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006397 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006398 return err;
6399}
6400
Al Viro18bb1db2011-07-26 01:41:39 -04006401static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006402{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006403 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006404 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006405 struct btrfs_trans_handle *trans;
6406 struct btrfs_root *root = BTRFS_I(dir)->root;
6407 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006408 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006409 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006410
Josef Bacik9ed74f22009-09-11 16:12:44 -04006411 /*
6412 * 2 items for inode and ref
6413 * 2 items for dir items
6414 * 1 for xattr if selinux is on
6415 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006416 trans = btrfs_start_transaction(root, 5);
6417 if (IS_ERR(trans))
6418 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006419
Li Zefan581bb052011-04-20 10:06:11 +08006420 err = btrfs_find_free_ino(root, &objectid);
6421 if (err)
6422 goto out_fail;
6423
Josef Bacikaec74772008-07-24 12:12:38 -04006424 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006425 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6426 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006427 if (IS_ERR(inode)) {
6428 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006429 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006430 goto out_fail;
6431 }
Chris Mason5f39d392007-10-15 16:14:19 -04006432
Chris Masonb0d5d102014-09-08 13:08:51 -07006433 /* these must be set before we unlock the inode */
6434 inode->i_op = &btrfs_dir_inode_operations;
6435 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006436
Eric Paris2a7dba32011-02-01 11:05:39 -05006437 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006438 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006439 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006440
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006441 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006442 err = btrfs_update_inode(trans, root, inode);
6443 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006444 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006445
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006446 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6447 dentry->d_name.name,
6448 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006449 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006450 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006451
Al Viro1e2e5472018-05-04 08:23:01 -04006452 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006453
6454out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006455 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006456 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006457 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006458 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006459 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006460 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006461 return err;
6462}
6463
Chris Masonc8b97812008-10-29 14:49:59 -04006464static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006465 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006466 size_t pg_offset, u64 extent_offset,
6467 struct btrfs_file_extent_item *item)
6468{
6469 int ret;
6470 struct extent_buffer *leaf = path->nodes[0];
6471 char *tmp;
6472 size_t max_size;
6473 unsigned long inline_size;
6474 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006475 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006476
6477 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006478 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006479 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6480 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006481 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006482 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006483 if (!tmp)
6484 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006485 ptr = btrfs_file_extent_inline_start(item);
6486
6487 read_extent_buffer(leaf, tmp, ptr, inline_size);
6488
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006489 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006490 ret = btrfs_decompress(compress_type, tmp, page,
6491 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006492
6493 /*
6494 * decompression code contains a memset to fill in any space between the end
6495 * of the uncompressed data and the end of max_size in case the decompressed
6496 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6497 * the end of an inline extent and the beginning of the next block, so we
6498 * cover that region here.
6499 */
6500
6501 if (max_size + pg_offset < PAGE_SIZE) {
6502 char *map = kmap(page);
6503 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6504 kunmap(page);
6505 }
Chris Masonc8b97812008-10-29 14:49:59 -04006506 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006507 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006508}
6509
Omar Sandoval39b07b52019-12-02 17:34:23 -08006510/**
6511 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6512 * @inode: file to search in
6513 * @page: page to read extent data into if the extent is inline
6514 * @pg_offset: offset into @page to copy to
6515 * @start: file offset
6516 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006517 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006518 * This returns the first &struct extent_map which overlaps with the given
6519 * range, reading it from the B-tree and caching it if necessary. Note that
6520 * there may be more extents which overlap the given range after the returned
6521 * extent_map.
6522 *
6523 * If @page is not NULL and the extent is inline, this also reads the extent
6524 * data directly into the page and marks the extent up to date in the io_tree.
6525 *
6526 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006527 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006528struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006529 struct page *page, size_t pg_offset,
6530 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006531{
David Sterba3ffbd682018-06-29 10:56:42 +02006532 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006533 int ret;
6534 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006535 u64 extent_start = 0;
6536 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006537 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006538 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006539 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006540 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006541 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006542 struct extent_buffer *leaf;
6543 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006544 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006545 struct extent_map_tree *em_tree = &inode->extent_tree;
6546 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006547
Chris Mason890871b2009-09-02 16:24:52 -04006548 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006549 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006550 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006551
Chris Masona52d9a82007-08-27 16:49:44 -04006552 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006553 if (em->start > start || em->start + em->len <= start)
6554 free_extent_map(em);
6555 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006556 free_extent_map(em);
6557 else
6558 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006559 }
David Sterba172ddd62011-04-21 00:48:27 +02006560 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006561 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006562 err = -ENOMEM;
6563 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006564 }
Chris Masond1310b22008-01-24 16:13:08 -05006565 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006566 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006567 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006568 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006569
Liu Bobee6ec82018-08-17 05:05:28 +08006570 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006571 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006572 err = -ENOMEM;
6573 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006574 }
6575
Liu Bobee6ec82018-08-17 05:05:28 +08006576 /* Chances are we'll be called again, so go ahead and do readahead */
6577 path->reada = READA_FORWARD;
6578
Liu Boe49aabd2018-08-25 13:47:09 +08006579 /*
6580 * Unless we're going to uncompress the inline extent, no sleep would
6581 * happen.
6582 */
6583 path->leave_spinning = 1;
6584
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006585 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006586 if (ret < 0) {
6587 err = ret;
6588 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006589 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006590 if (path->slots[0] == 0)
6591 goto not_found;
6592 path->slots[0]--;
6593 }
6594
Chris Mason5f39d392007-10-15 16:14:19 -04006595 leaf = path->nodes[0];
6596 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006597 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006598 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006599 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006600 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006601 /*
6602 * If we backup past the first extent we want to move forward
6603 * and see if there is an extent in front of us, otherwise we'll
6604 * say there is a hole for our whole search range which can
6605 * cause problems.
6606 */
6607 extent_end = start;
6608 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006609 }
6610
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006611 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006612 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006613 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006614 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6615 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006616 /* Only regular file could have regular/prealloc extent */
6617 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6618 ret = -EUCLEAN;
6619 btrfs_crit(fs_info,
6620 "regular/prealloc extent found for non-regular inode %llu",
6621 btrfs_ino(inode));
6622 goto out;
6623 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006624 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6625 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006626 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006627 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6628 path->slots[0],
6629 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006630 }
Josef Bacik25a50342013-10-14 12:08:38 -04006631next:
Yan Zheng9036c102008-10-30 14:19:41 -04006632 if (start >= extent_end) {
6633 path->slots[0]++;
6634 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6635 ret = btrfs_next_leaf(root, path);
6636 if (ret < 0) {
6637 err = ret;
6638 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006639 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006640 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006641 }
Yan Zheng9036c102008-10-30 14:19:41 -04006642 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006643 }
Yan Zheng9036c102008-10-30 14:19:41 -04006644 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6645 if (found_key.objectid != objectid ||
6646 found_key.type != BTRFS_EXTENT_DATA_KEY)
6647 goto not_found;
6648 if (start + len <= found_key.offset)
6649 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006650 if (start > found_key.offset)
6651 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006652
6653 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006654 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006655 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006656 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006657 em->block_start = EXTENT_MAP_HOLE;
6658 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006659 }
6660
Omar Sandoval39b07b52019-12-02 17:34:23 -08006661 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006662
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006663 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6664 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006665 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006666 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006667 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006668 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006669 size_t size;
6670 size_t extent_offset;
6671 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006672
Omar Sandoval39b07b52019-12-02 17:34:23 -08006673 if (!page)
Yan689f9342007-10-29 11:41:07 -04006674 goto out;
Yan689f9342007-10-29 11:41:07 -04006675
Qu Wenruoe41ca582018-06-06 15:41:49 +08006676 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006677 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006678 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6679 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006680 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006681 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006682 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006683 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006684 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006685
6686 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006687 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006688 if (btrfs_file_extent_compression(leaf, item) !=
6689 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006690 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006691 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006692 if (ret) {
6693 err = ret;
6694 goto out;
6695 }
Chris Masonc8b97812008-10-29 14:49:59 -04006696 } else {
6697 map = kmap(page);
6698 read_extent_buffer(leaf, map + pg_offset, ptr,
6699 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006700 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006701 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006702 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006703 copy_size);
6704 }
Chris Masonc8b97812008-10-29 14:49:59 -04006705 kunmap(page);
6706 }
Chris Mason179e29e2007-11-01 11:28:41 -04006707 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006708 }
Chris Masond1310b22008-01-24 16:13:08 -05006709 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006710 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006711 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006712 }
6713not_found:
6714 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006715 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006716 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006717 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006718insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006719 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006720 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006721 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006722 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6723 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006724 err = -EIO;
6725 goto out;
6726 }
Chris Masond1310b22008-01-24 16:13:08 -05006727
6728 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006729 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006730 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006731 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006732out:
Liu Boc6414282018-08-23 07:36:17 +08006733 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006734
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006735 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006736
Chris Masona52d9a82007-08-27 16:49:44 -04006737 if (err) {
6738 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006739 return ERR_PTR(err);
6740 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006741 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006742 return em;
6743}
6744
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006745struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006746 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006747{
6748 struct extent_map *em;
6749 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006750 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006751 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006752 u64 delalloc_len;
6753 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006754 int err = 0;
6755
Omar Sandoval39b07b52019-12-02 17:34:23 -08006756 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006757 if (IS_ERR(em))
6758 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006759 /*
6760 * If our em maps to:
6761 * - a hole or
6762 * - a pre-alloc extent,
6763 * there might actually be delalloc bytes behind it.
6764 */
6765 if (em->block_start != EXTENT_MAP_HOLE &&
6766 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6767 return em;
6768 else
6769 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006770
6771 /* check to see if we've wrapped (len == -1 or similar) */
6772 end = start + len;
6773 if (end < start)
6774 end = (u64)-1;
6775 else
6776 end -= 1;
6777
6778 em = NULL;
6779
6780 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006781 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006782 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006783 delalloc_end = delalloc_start + delalloc_len;
6784 if (delalloc_end < delalloc_start)
6785 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006786
6787 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006788 * We didn't find anything useful, return the original results from
6789 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006790 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006791 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006792 em = hole_em;
6793 hole_em = NULL;
6794 goto out;
6795 }
6796
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006797 /*
6798 * Adjust the delalloc_start to make sure it doesn't go backwards from
6799 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006800 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006801 delalloc_start = max(start, delalloc_start);
6802 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006803
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006804 if (delalloc_len > 0) {
6805 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006806 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006807 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006808
David Sterba172ddd62011-04-21 00:48:27 +02006809 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006810 if (!em) {
6811 err = -ENOMEM;
6812 goto out;
6813 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006814
6815 ASSERT(hole_em);
6816 /*
6817 * When btrfs_get_extent can't find anything it returns one
6818 * huge hole
6819 *
6820 * Make sure what it found really fits our range, and adjust to
6821 * make sure it is based on the start from the caller
6822 */
6823 if (hole_end <= start || hole_em->start > end) {
6824 free_extent_map(hole_em);
6825 hole_em = NULL;
6826 } else {
6827 hole_start = max(hole_em->start, start);
6828 hole_len = hole_end - hole_start;
6829 }
6830
6831 if (hole_em && delalloc_start > hole_start) {
6832 /*
6833 * Our hole starts before our delalloc, so we have to
6834 * return just the parts of the hole that go until the
6835 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006836 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006837 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006838 em->start = hole_start;
6839 em->orig_start = hole_start;
6840 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006841 * Don't adjust block start at all, it is fixed at
6842 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006843 */
6844 em->block_start = hole_em->block_start;
6845 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006846 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6847 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006848 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006849 /*
6850 * Hole is out of passed range or it starts after
6851 * delalloc range
6852 */
6853 em->start = delalloc_start;
6854 em->len = delalloc_len;
6855 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006856 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006857 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006858 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006859 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006860 return hole_em;
6861 }
6862out:
6863
6864 free_extent_map(hole_em);
6865 if (err) {
6866 free_extent_map(em);
6867 return ERR_PTR(err);
6868 }
6869 return em;
6870}
6871
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006872static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
6873 const u64 start,
6874 const u64 len,
6875 const u64 orig_start,
6876 const u64 block_start,
6877 const u64 block_len,
6878 const u64 orig_block_len,
6879 const u64 ram_bytes,
6880 const int type)
6881{
6882 struct extent_map *em = NULL;
6883 int ret;
6884
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006885 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03006886 em = create_io_em(BTRFS_I(inode), start, len, orig_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08006887 block_start, block_len, orig_block_len,
6888 ram_bytes,
6889 BTRFS_COMPRESS_NONE, /* compress_type */
6890 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006891 if (IS_ERR(em))
6892 goto out;
6893 }
6894 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
6895 len, block_len, type);
6896 if (ret) {
6897 if (em) {
6898 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02006899 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006900 start + len - 1, 0);
6901 }
6902 em = ERR_PTR(ret);
6903 }
6904 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006905
6906 return em;
6907}
6908
Josef Bacik4b46fce2010-05-23 11:00:55 -04006909static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6910 u64 start, u64 len)
6911{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006912 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006913 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006914 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006915 struct btrfs_key ins;
6916 u64 alloc_hint;
6917 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006918
Nikolay Borisov43c69842020-06-03 08:55:02 +03006919 alloc_hint = get_extent_allocation_hint(BTRFS_I(inode), start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006920 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006921 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006922 if (ret)
6923 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006924
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006925 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
6926 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006927 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006928 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006929 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006930 btrfs_free_reserved_extent(fs_info, ins.objectid,
6931 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006932
Josef Bacik4b46fce2010-05-23 11:00:55 -04006933 return em;
6934}
6935
Chris Mason46bfbb52010-05-26 11:04:10 -04006936/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006937 * Check if we can do nocow write into the range [@offset, @offset + @len)
6938 *
6939 * @offset: File offset
6940 * @len: The length to write, will be updated to the nocow writeable
6941 * range
6942 * @orig_start: (optional) Return the original file offset of the file extent
6943 * @orig_len: (optional) Return the original on-disk length of the file extent
6944 * @ram_bytes: (optional) Return the ram_bytes of the file extent
6945 *
6946 * This function will flush ordered extents in the range to ensure proper
6947 * nocow checks for (nowait == false) case.
6948 *
6949 * Return:
6950 * >0 and update @len if we can do nocow write
6951 * 0 if we can't do nocow write
6952 * <0 if error happened
6953 *
6954 * NOTE: This only checks the file extents, caller is responsible to wait for
6955 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04006956 */
Josef Bacik00361582013-08-14 14:02:47 -04006957noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006958 u64 *orig_start, u64 *orig_block_len,
6959 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006960{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006961 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006962 struct btrfs_path *path;
6963 int ret;
6964 struct extent_buffer *leaf;
6965 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006966 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006967 struct btrfs_file_extent_item *fi;
6968 struct btrfs_key key;
6969 u64 disk_bytenr;
6970 u64 backref_offset;
6971 u64 extent_end;
6972 u64 num_bytes;
6973 int slot;
6974 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006975 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006976
Chris Mason46bfbb52010-05-26 11:04:10 -04006977 path = btrfs_alloc_path();
6978 if (!path)
6979 return -ENOMEM;
6980
David Sterbaf85b7372017-01-20 14:54:07 +01006981 ret = btrfs_lookup_file_extent(NULL, root, path,
6982 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006983 if (ret < 0)
6984 goto out;
6985
6986 slot = path->slots[0];
6987 if (ret == 1) {
6988 if (slot == 0) {
6989 /* can't find the item, must cow */
6990 ret = 0;
6991 goto out;
6992 }
6993 slot--;
6994 }
6995 ret = 0;
6996 leaf = path->nodes[0];
6997 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006998 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006999 key.type != BTRFS_EXTENT_DATA_KEY) {
7000 /* not our file or wrong item type, must cow */
7001 goto out;
7002 }
7003
7004 if (key.offset > offset) {
7005 /* Wrong offset, must cow */
7006 goto out;
7007 }
7008
7009 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7010 found_type = btrfs_file_extent_type(leaf, fi);
7011 if (found_type != BTRFS_FILE_EXTENT_REG &&
7012 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7013 /* not a regular extent, must cow */
7014 goto out;
7015 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007016
7017 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7018 goto out;
7019
Miao Xiee77751a2013-12-27 21:11:50 +08007020 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7021 if (extent_end <= offset)
7022 goto out;
7023
Chris Mason46bfbb52010-05-26 11:04:10 -04007024 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007025 if (disk_bytenr == 0)
7026 goto out;
7027
7028 if (btrfs_file_extent_compression(leaf, fi) ||
7029 btrfs_file_extent_encryption(leaf, fi) ||
7030 btrfs_file_extent_other_encoding(leaf, fi))
7031 goto out;
7032
Ethan Lien78d42952018-05-17 14:58:29 +08007033 /*
7034 * Do the same check as in btrfs_cross_ref_exist but without the
7035 * unnecessary search.
7036 */
7037 if (btrfs_file_extent_generation(leaf, fi) <=
7038 btrfs_root_last_snapshot(&root->root_item))
7039 goto out;
7040
Chris Mason46bfbb52010-05-26 11:04:10 -04007041 backref_offset = btrfs_file_extent_offset(leaf, fi);
7042
Josef Bacik7ee9e442013-06-21 16:37:03 -04007043 if (orig_start) {
7044 *orig_start = key.offset - backref_offset;
7045 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7046 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7047 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007048
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007049 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007050 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007051
7052 num_bytes = min(offset + *len, extent_end) - offset;
7053 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7054 u64 range_end;
7055
Jeff Mahoneyda170662016-06-15 09:22:56 -04007056 range_end = round_up(offset + num_bytes,
7057 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007058 ret = test_range_bit(io_tree, offset, range_end,
7059 EXTENT_DELALLOC, 0, NULL);
7060 if (ret) {
7061 ret = -EAGAIN;
7062 goto out;
7063 }
7064 }
7065
Josef Bacik1bda19e2013-10-18 12:10:36 -04007066 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007067
7068 /*
7069 * look for other files referencing this extent, if we
7070 * find any we must cow
7071 */
Josef Bacik00361582013-08-14 14:02:47 -04007072
Liu Boe4c3b2d2017-01-30 12:25:28 -08007073 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007074 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007075 if (ret) {
7076 ret = 0;
7077 goto out;
7078 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007079
7080 /*
7081 * adjust disk_bytenr and num_bytes to cover just the bytes
7082 * in this extent we are about to write. If there
7083 * are any csums in that range we have to cow in order
7084 * to keep the csums correct
7085 */
7086 disk_bytenr += backref_offset;
7087 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007088 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7089 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007090 /*
7091 * all of the above have passed, it is safe to overwrite this extent
7092 * without cow
7093 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007094 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007095 ret = 1;
7096out:
7097 btrfs_free_path(path);
7098 return ret;
7099}
7100
Josef Bacikeb838e72012-07-31 16:28:48 -04007101static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
David Sterba55e20bd2020-06-09 19:56:06 +02007102 struct extent_state **cached_state, int writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007103{
7104 struct btrfs_ordered_extent *ordered;
7105 int ret = 0;
7106
7107 while (1) {
7108 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007109 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007110 /*
7111 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007112 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007113 * extents in this range.
7114 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007115 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007116 lockend - lockstart + 1);
7117
7118 /*
7119 * We need to make sure there are no buffered pages in this
7120 * range either, we could have raced between the invalidate in
7121 * generic_file_direct_write and locking the extent. The
7122 * invalidate needs to happen so that reads after a write do not
7123 * get stale data.
7124 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007125 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007126 (!writing || !filemap_range_has_page(inode->i_mapping,
7127 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007128 break;
7129
7130 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007131 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007132
7133 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007134 /*
7135 * If we are doing a DIO read and the ordered extent we
7136 * found is for a buffered write, we can not wait for it
7137 * to complete and retry, because if we do so we can
7138 * deadlock with concurrent buffered writes on page
7139 * locks. This happens only if our DIO read covers more
7140 * than one extent map, if at this point has already
7141 * created an ordered extent for a previous extent map
7142 * and locked its range in the inode's io tree, and a
7143 * concurrent write against that previous extent map's
7144 * range and this range started (we unlock the ranges
7145 * in the io tree only when the bios complete and
7146 * buffered writes always lock pages before attempting
7147 * to lock range in the io tree).
7148 */
7149 if (writing ||
7150 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7151 btrfs_start_ordered_extent(inode, ordered, 1);
7152 else
7153 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007154 btrfs_put_ordered_extent(ordered);
7155 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007156 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007157 * We could trigger writeback for this range (and wait
7158 * for it to complete) and then invalidate the pages for
7159 * this range (through invalidate_inode_pages2_range()),
7160 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007161 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007162 * triggered a readahead) on a page lock due to an
7163 * ordered dio extent we created before but did not have
7164 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007165 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007166 * ordered extent to complete while holding a lock on
7167 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007168 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007169 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007170 }
7171
Filipe Mananaade77022016-02-18 14:28:55 +00007172 if (ret)
7173 break;
7174
Josef Bacikeb838e72012-07-31 16:28:48 -04007175 cond_resched();
7176 }
7177
7178 return ret;
7179}
7180
Liu Bo6f9994d2017-01-31 07:50:22 -08007181/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007182static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7183 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007184 u64 block_len, u64 orig_block_len,
7185 u64 ram_bytes, int compress_type,
7186 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007187{
7188 struct extent_map_tree *em_tree;
7189 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007190 int ret;
7191
Liu Bo6f9994d2017-01-31 07:50:22 -08007192 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7193 type == BTRFS_ORDERED_COMPRESSED ||
7194 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007195 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007196
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007197 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007198 em = alloc_extent_map();
7199 if (!em)
7200 return ERR_PTR(-ENOMEM);
7201
7202 em->start = start;
7203 em->orig_start = orig_start;
7204 em->len = len;
7205 em->block_len = block_len;
7206 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007207 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007208 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007209 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007210 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007211 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007212 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007213 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007214 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7215 em->compress_type = compress_type;
7216 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007217
7218 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007219 btrfs_drop_extent_cache(inode, em->start,
7220 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007221 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007222 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007223 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007224 /*
7225 * The caller has taken lock_extent(), who could race with us
7226 * to add em?
7227 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007228 } while (ret == -EEXIST);
7229
7230 if (ret) {
7231 free_extent_map(em);
7232 return ERR_PTR(ret);
7233 }
7234
Liu Bo6f9994d2017-01-31 07:50:22 -08007235 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007236 return em;
7237}
7238
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007239
David Sterba55e20bd2020-06-09 19:56:06 +02007240static int btrfs_get_blocks_direct_read(struct extent_map *em,
7241 struct buffer_head *bh_result,
7242 struct inode *inode,
7243 u64 start, u64 len)
7244{
7245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7246
7247 if (em->block_start == EXTENT_MAP_HOLE ||
7248 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7249 return -ENOENT;
7250
7251 len = min(len, em->len - (start - em->start));
7252
7253 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7254 inode->i_blkbits;
7255 bh_result->b_size = len;
7256 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7257 set_buffer_mapped(bh_result);
7258
7259 return 0;
7260}
7261
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007262static int btrfs_get_blocks_direct_write(struct extent_map **map,
David Sterba55e20bd2020-06-09 19:56:06 +02007263 struct buffer_head *bh_result,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007264 struct inode *inode,
7265 struct btrfs_dio_data *dio_data,
7266 u64 start, u64 len)
7267{
7268 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7269 struct extent_map *em = *map;
7270 int ret = 0;
7271
7272 /*
7273 * We don't allocate a new extent in the following cases
7274 *
7275 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7276 * existing extent.
7277 * 2) The extent is marked as PREALLOC. We're good to go here and can
7278 * just use the extent.
7279 *
7280 */
7281 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7282 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7283 em->block_start != EXTENT_MAP_HOLE)) {
7284 int type;
7285 u64 block_start, orig_start, orig_block_len, ram_bytes;
7286
7287 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7288 type = BTRFS_ORDERED_PREALLOC;
7289 else
7290 type = BTRFS_ORDERED_NOCOW;
7291 len = min(len, em->len - (start - em->start));
7292 block_start = em->block_start + (start - em->start);
7293
7294 if (can_nocow_extent(inode, start, &len, &orig_start,
7295 &orig_block_len, &ram_bytes) == 1 &&
7296 btrfs_inc_nocow_writers(fs_info, block_start)) {
7297 struct extent_map *em2;
7298
7299 em2 = btrfs_create_dio_extent(inode, start, len,
7300 orig_start, block_start,
7301 len, orig_block_len,
7302 ram_bytes, type);
7303 btrfs_dec_nocow_writers(fs_info, block_start);
7304 if (type == BTRFS_ORDERED_PREALLOC) {
7305 free_extent_map(em);
7306 *map = em = em2;
7307 }
7308
7309 if (em2 && IS_ERR(em2)) {
7310 ret = PTR_ERR(em2);
7311 goto out;
7312 }
7313 /*
7314 * For inode marked NODATACOW or extent marked PREALLOC,
7315 * use the existing or preallocated extent, so does not
7316 * need to adjust btrfs_space_info's bytes_may_use.
7317 */
Filipe Manana46d4dac2020-06-09 11:19:33 +01007318 btrfs_free_reserved_data_space_noquota(inode, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007319 goto skip_cow;
7320 }
7321 }
7322
7323 /* this will cow the extent */
David Sterba55e20bd2020-06-09 19:56:06 +02007324 len = bh_result->b_size;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007325 free_extent_map(em);
7326 *map = em = btrfs_new_extent_direct(inode, start, len);
7327 if (IS_ERR(em)) {
7328 ret = PTR_ERR(em);
7329 goto out;
7330 }
7331
7332 len = min(len, em->len - (start - em->start));
7333
7334skip_cow:
David Sterba55e20bd2020-06-09 19:56:06 +02007335 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7336 inode->i_blkbits;
7337 bh_result->b_size = len;
7338 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7339 set_buffer_mapped(bh_result);
7340
7341 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7342 set_buffer_new(bh_result);
7343
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007344 /*
7345 * Need to update the i_size under the extent lock so buffered
7346 * readers will get the updated i_size when we unlock.
7347 */
David Sterba55e20bd2020-06-09 19:56:06 +02007348 if (!dio_data->overwrite && start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007349 i_size_write(inode, start + len);
7350
David Sterba55e20bd2020-06-09 19:56:06 +02007351 WARN_ON(dio_data->reserve < len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007352 dio_data->reserve -= len;
David Sterba55e20bd2020-06-09 19:56:06 +02007353 dio_data->unsubmitted_oe_range_end = start + len;
7354 current->journal_info = dio_data;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007355out:
7356 return ret;
7357}
7358
David Sterba55e20bd2020-06-09 19:56:06 +02007359static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7360 struct buffer_head *bh_result, int create)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007361{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007362 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007363 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007364 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307365 struct btrfs_dio_data *dio_data = NULL;
David Sterba55e20bd2020-06-09 19:56:06 +02007366 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007367 u64 lockstart, lockend;
David Sterba55e20bd2020-06-09 19:56:06 +02007368 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007369 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007370
David Sterba55e20bd2020-06-09 19:56:06 +02007371 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007372 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007373
Josef Bacikc3298612012-08-03 16:49:19 -04007374 lockstart = start;
7375 lockend = start + len - 1;
7376
David Sterba55e20bd2020-06-09 19:56:06 +02007377 if (current->journal_info) {
7378 /*
7379 * Need to pull our outstanding extents and set journal_info to NULL so
7380 * that anything that needs to check if there's a transaction doesn't get
7381 * confused.
7382 */
7383 dio_data = current->journal_info;
7384 current->journal_info = NULL;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007385 }
7386
Josef Bacikeb838e72012-07-31 16:28:48 -04007387 /*
7388 * If this errors out it's because we couldn't invalidate pagecache for
7389 * this range and we need to fallback to buffered.
7390 */
David Sterba55e20bd2020-06-09 19:56:06 +02007391 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7392 create)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007393 ret = -ENOTBLK;
7394 goto err;
7395 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007396
Omar Sandoval39b07b52019-12-02 17:34:23 -08007397 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007398 if (IS_ERR(em)) {
7399 ret = PTR_ERR(em);
7400 goto unlock_err;
7401 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007402
7403 /*
7404 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7405 * io. INLINE is special, and we could probably kludge it in here, but
7406 * it's still buffered so for safety lets just fall back to the generic
7407 * buffered path.
7408 *
7409 * For COMPRESSED we _have_ to read the entire extent in so we can
7410 * decompress it, so there will be buffering required no matter what we
7411 * do, so go ahead and fallback to buffered.
7412 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007413 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007414 * to buffered IO. Don't blame me, this is the price we pay for using
7415 * the generic code.
7416 */
7417 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7418 em->block_start == EXTENT_MAP_INLINE) {
7419 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007420 ret = -ENOTBLK;
7421 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007422 }
7423
David Sterba55e20bd2020-06-09 19:56:06 +02007424 if (create) {
7425 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7426 dio_data, start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007427 if (ret < 0)
7428 goto unlock_err;
David Sterba55e20bd2020-06-09 19:56:06 +02007429
7430 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7431 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007432 } else {
David Sterba55e20bd2020-06-09 19:56:06 +02007433 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7434 start, len);
7435 /* Can be negative only if we read from a hole */
7436 if (ret < 0) {
7437 ret = 0;
7438 free_extent_map(em);
7439 goto unlock_err;
7440 }
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007441 /*
7442 * We need to unlock only the end area that we aren't using.
7443 * The rest is going to be unlocked by the endio routine.
7444 */
David Sterba55e20bd2020-06-09 19:56:06 +02007445 lockstart = start + bh_result->b_size;
7446 if (lockstart < lockend) {
7447 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7448 lockstart, lockend, &cached_state);
7449 } else {
7450 free_extent_state(cached_state);
7451 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007452 }
7453
Josef Bacik4b46fce2010-05-23 11:00:55 -04007454 free_extent_map(em);
7455
7456 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007457
7458unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007459 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7460 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007461err:
David Sterba55e20bd2020-06-09 19:56:06 +02007462 if (dio_data)
7463 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007464 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007465}
7466
Omar Sandoval769b4f22020-04-16 14:46:22 -07007467static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7468{
7469 /*
7470 * This implies a barrier so that stores to dio_bio->bi_status before
7471 * this and loads of dio_bio->bi_status after this are fully ordered.
7472 */
7473 if (!refcount_dec_and_test(&dip->refs))
7474 return;
7475
7476 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007477 __endio_write_update_ordered(BTRFS_I(dip->inode),
7478 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007479 dip->bytes,
7480 !dip->dio_bio->bi_status);
7481 } else {
7482 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7483 dip->logical_offset,
7484 dip->logical_offset + dip->bytes - 1);
7485 }
7486
David Sterba55e20bd2020-06-09 19:56:06 +02007487 dio_end_io(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007488 kfree(dip);
7489}
7490
Omar Sandoval77d5d682020-04-16 14:46:25 -07007491static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7492 int mirror_num,
7493 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007494{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007495 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007496 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007497 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007498
Mike Christie37226b22016-06-05 14:31:52 -05007499 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007500
Omar Sandoval5c047a62020-04-16 14:46:24 -07007501 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007502 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007503 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007504
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007505 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007506 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007507 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007508 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007509 return ret;
7510}
7511
Omar Sandoval769b4f22020-04-16 14:46:22 -07007512static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7513 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007514 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007515{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007516 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7517 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007518 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007519 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7520 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007521 struct bio_vec bvec;
7522 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007523 u64 start = io_bio->logical;
7524 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007525 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007526
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007527 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7528 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307529
Liu Bo17347ce2017-05-15 15:33:27 -07007530 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7531 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007532 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007533 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007534 if (uptodate &&
7535 (!csum || !check_data_csum(inode, io_bio, icsum,
7536 bvec.bv_page, pgoff,
7537 start, sectorsize))) {
7538 clean_io_failure(fs_info, failure_tree, io_tree,
7539 start, bvec.bv_page,
7540 btrfs_ino(BTRFS_I(inode)),
7541 pgoff);
7542 } else {
7543 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007544
Omar Sandoval77d5d682020-04-16 14:46:25 -07007545 status = btrfs_submit_read_repair(inode,
7546 &io_bio->bio,
7547 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007548 bvec.bv_page, pgoff,
7549 start,
7550 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007551 io_bio->mirror_num,
7552 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007553 if (status)
7554 err = status;
7555 }
7556 start += sectorsize;
7557 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307558 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307559 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007560 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007561 return err;
7562}
7563
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007564static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007565 const u64 offset, const u64 bytes,
7566 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007567{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007568 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007569 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007570 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007571 u64 ordered_offset = offset;
7572 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007573 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007574
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007575 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007576 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007577 else
Qu Wenruo524272602017-03-08 10:25:52 +08007578 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007579
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007580 while (ordered_offset < offset + bytes) {
7581 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007582 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007583 &ordered_offset,
7584 ordered_bytes,
7585 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007586 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7587 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007588 btrfs_queue_work(wq, &ordered->work);
7589 }
7590 /*
7591 * If btrfs_dec_test_ordered_pending does not find any ordered
7592 * extent in the range, we can exit.
7593 */
7594 if (ordered_offset == last_offset)
7595 return;
7596 /*
7597 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007598 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007599 */
7600 if (ordered_offset < offset + bytes) {
7601 ordered_bytes = offset + bytes - ordered_offset;
7602 ordered = NULL;
7603 }
Chris Mason163cf092010-11-28 19:56:33 -05007604 }
Filipe Manana14543772015-11-24 16:23:54 +00007605}
7606
David Sterbad0ee3932018-03-08 14:35:48 +01007607static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007608 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007609{
Josef Bacikc6100a42017-05-05 11:57:13 -04007610 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007611 blk_status_t ret;
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007612 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007613 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007614 return 0;
7615}
7616
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007617static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007618{
7619 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007620 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007621
Miao Xie8b110e32014-09-12 18:44:03 +08007622 if (err)
7623 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007624 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007625 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7626 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007627 (unsigned long long)bio->bi_iter.bi_sector,
7628 bio->bi_iter.bi_size, err);
7629
Omar Sandoval769b4f22020-04-16 14:46:22 -07007630 if (bio_op(bio) == REQ_OP_READ) {
7631 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007632 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007633 }
7634
Omar Sandoval769b4f22020-04-16 14:46:22 -07007635 if (err)
7636 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007637
Miao Xiee65e1532010-11-22 03:04:43 +00007638 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007639 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007640}
7641
David Sterbad0ee3932018-03-08 14:35:48 +01007642static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7643 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007644{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007645 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007646 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007647 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007648 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007649
Liu Bo4c274bc2017-11-01 17:19:27 -06007650 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007651 if (async_submit)
7652 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7653
Josef Bacik5fd02042012-05-02 14:00:54 -04007654 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007655 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007656 if (ret)
7657 goto err;
7658 }
Miao Xiee65e1532010-11-22 03:04:43 +00007659
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007660 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007661 goto map;
7662
7663 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007664 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7665 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007666 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007667 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007668 } else if (write) {
7669 /*
7670 * If we aren't doing async submit, calculate the csum of the
7671 * bio now.
7672 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007673 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007674 if (ret)
7675 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007676 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007677 u64 csum_offset;
7678
7679 csum_offset = file_offset - dip->logical_offset;
7680 csum_offset >>= inode->i_sb->s_blocksize_bits;
7681 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7682 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007683 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007684map:
Chris Mason08635ba2019-07-10 12:28:14 -07007685 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007686err:
Miao Xiee65e1532010-11-22 03:04:43 +00007687 return ret;
7688}
7689
Omar Sandovalc36cac22020-04-16 14:46:13 -07007690/*
7691 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7692 * or ordered extents whether or not we submit any bios.
7693 */
7694static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7695 struct inode *inode,
7696 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007697{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007698 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007699 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7700 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007701 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007702
Omar Sandoval85879572020-04-16 14:46:21 -07007703 dip_size = sizeof(*dip);
7704 if (!write && csum) {
7705 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7706 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7707 size_t nblocks;
7708
7709 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7710 dip_size += csum_size * nblocks;
7711 }
7712
7713 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007714 if (!dip)
7715 return NULL;
7716
Omar Sandovalc36cac22020-04-16 14:46:13 -07007717 dip->inode = inode;
7718 dip->logical_offset = file_offset;
7719 dip->bytes = dio_bio->bi_iter.bi_size;
7720 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007721 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007722 refcount_set(&dip->refs, 1);
David Sterba55e20bd2020-06-09 19:56:06 +02007723
7724 if (write) {
7725 struct btrfs_dio_data *dio_data = current->journal_info;
7726
7727 /*
7728 * Setting range start and end to the same value means that
7729 * no cleanup will happen in btrfs_direct_IO
7730 */
7731 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7732 dip->bytes;
7733 dio_data->unsubmitted_oe_range_start =
7734 dio_data->unsubmitted_oe_range_end;
7735 }
Omar Sandovalc36cac22020-04-16 14:46:13 -07007736 return dip;
7737}
7738
David Sterba55e20bd2020-06-09 19:56:06 +02007739static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7740 loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007741{
7742 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007743 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007744 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007745 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7746 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007747 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007748 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007749 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007750 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007751 u64 submit_len;
7752 int clone_offset = 0;
7753 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307754 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007755 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007756 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007757
Omar Sandovalc36cac22020-04-16 14:46:13 -07007758 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7759 if (!dip) {
7760 if (!write) {
7761 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7762 file_offset + dio_bio->bi_iter.bi_size - 1);
7763 }
7764 dio_bio->bi_status = BLK_STS_RESOURCE;
David Sterba55e20bd2020-06-09 19:56:06 +02007765 dio_end_io(dio_bio);
7766 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04007767 }
7768
Omar Sandoval85879572020-04-16 14:46:21 -07007769 if (!write && csum) {
7770 /*
7771 * Load the csums up front to reduce csum tree searches and
7772 * contention when submitting bios.
7773 */
7774 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7775 dip->csums);
7776 if (status != BLK_STS_OK)
7777 goto out_err;
7778 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007779
Omar Sandoval769b4f22020-04-16 14:46:22 -07007780 start_sector = dio_bio->bi_iter.bi_sector;
7781 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007782
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007783 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007784 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7785 start_sector << 9, submit_len,
7786 &geom);
7787 if (ret) {
7788 status = errno_to_blk_status(ret);
7789 goto out_err;
7790 }
7791 ASSERT(geom.len <= INT_MAX);
7792
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007793 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007794
Liu Bo725130b2017-05-16 09:51:39 -07007795 /*
7796 * This will never fail as it's passing GPF_NOFS and
7797 * the allocation is backed by btrfs_bioset.
7798 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007799 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007800 bio->bi_private = dip;
7801 bio->bi_end_io = btrfs_end_dio_bio;
7802 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007803
Liu Bo725130b2017-05-16 09:51:39 -07007804 ASSERT(submit_len >= clone_len);
7805 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007806
Liu Bo725130b2017-05-16 09:51:39 -07007807 /*
7808 * Increase the count before we submit the bio so we know
7809 * the end IO handler won't happen before we increase the
7810 * count. Otherwise, the dip might get freed before we're
7811 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007812 *
7813 * We transfer the initial reference to the last bio, so we
7814 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007815 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007816 if (submit_len > 0) {
7817 refcount_inc(&dip->refs);
7818 /*
7819 * If we are submitting more than one bio, submit them
7820 * all asynchronously. The exception is RAID 5 or 6, as
7821 * asynchronous checksums make it difficult to collect
7822 * full stripe writes.
7823 */
7824 if (!raid56)
7825 async_submit = 1;
7826 }
Miao Xiee65e1532010-11-22 03:04:43 +00007827
David Sterbad0ee3932018-03-08 14:35:48 +01007828 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007829 async_submit);
7830 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007831 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007832 if (submit_len > 0)
7833 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007834 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007835 }
Liu Bo725130b2017-05-16 09:51:39 -07007836
7837 clone_offset += clone_len;
7838 start_sector += clone_len >> 9;
7839 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007840 } while (submit_len > 0);
David Sterba55e20bd2020-06-09 19:56:06 +02007841 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007842
Miao Xiee65e1532010-11-22 03:04:43 +00007843out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007844 dip->dio_bio->bi_status = status;
7845 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007846}
7847
David Sterbaf4c48b42020-06-09 19:19:27 +02007848static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7849 const struct iov_iter *iter, loff_t offset)
7850{
7851 int seg;
7852 int i;
7853 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7854 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007855
David Sterbaf4c48b42020-06-09 19:19:27 +02007856 if (offset & blocksize_mask)
7857 goto out;
7858
7859 if (iov_iter_alignment(iter) & blocksize_mask)
7860 goto out;
7861
7862 /* If this is a write we don't need to check anymore */
7863 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7864 return 0;
7865 /*
7866 * Check to make sure we don't have duplicate iov_base's in this
7867 * iovec, if so return EINVAL, otherwise we'll get csum errors
7868 * when reading back.
7869 */
7870 for (seg = 0; seg < iter->nr_segs; seg++) {
7871 for (i = seg + 1; i < iter->nr_segs; i++) {
7872 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7873 goto out;
7874 }
7875 }
7876 retval = 0;
7877out:
7878 return retval;
7879}
7880
David Sterba55e20bd2020-06-09 19:56:06 +02007881static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007882{
7883 struct file *file = iocb->ki_filp;
7884 struct inode *inode = file->f_mapping->host;
7885 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba55e20bd2020-06-09 19:56:06 +02007886 struct btrfs_dio_data dio_data = { 0 };
David Sterbaf4c48b42020-06-09 19:19:27 +02007887 struct extent_changeset *data_reserved = NULL;
7888 loff_t offset = iocb->ki_pos;
7889 size_t count = 0;
David Sterba55e20bd2020-06-09 19:56:06 +02007890 int flags = 0;
7891 bool wakeup = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007892 bool relock = false;
7893 ssize_t ret;
7894
7895 if (check_direct_IO(fs_info, iter, offset))
7896 return 0;
7897
David Sterba55e20bd2020-06-09 19:56:06 +02007898 inode_dio_begin(inode);
7899
7900 /*
7901 * The generic stuff only does filemap_write_and_wait_range, which
7902 * isn't enough if we've written compressed pages to this area, so
7903 * we need to flush the dirty pages again to make absolutely sure
7904 * that any outstanding dirty pages are on disk.
7905 */
David Sterbaf4c48b42020-06-09 19:19:27 +02007906 count = iov_iter_count(iter);
David Sterba55e20bd2020-06-09 19:56:06 +02007907 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7908 &BTRFS_I(inode)->runtime_flags))
7909 filemap_fdatawrite_range(inode->i_mapping, offset,
7910 offset + count - 1);
7911
David Sterbaf4c48b42020-06-09 19:19:27 +02007912 if (iov_iter_rw(iter) == WRITE) {
7913 /*
7914 * If the write DIO is beyond the EOF, we need update
7915 * the isize, but it is protected by i_mutex. So we can
7916 * not unlock the i_mutex at this case.
7917 */
7918 if (offset + count <= inode->i_size) {
David Sterba55e20bd2020-06-09 19:56:06 +02007919 dio_data.overwrite = 1;
David Sterbaf4c48b42020-06-09 19:19:27 +02007920 inode_unlock(inode);
7921 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007922 }
David Sterba55e20bd2020-06-09 19:56:06 +02007923 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
7924 offset, count);
7925 if (ret)
7926 goto out;
7927
7928 /*
7929 * We need to know how many extents we reserved so that we can
7930 * do the accounting properly if we go over the number we
7931 * originally calculated. Abuse current->journal_info for this.
7932 */
7933 dio_data.reserve = round_up(count,
7934 fs_info->sectorsize);
7935 dio_data.unsubmitted_oe_range_start = (u64)offset;
7936 dio_data.unsubmitted_oe_range_end = (u64)offset;
7937 current->journal_info = &dio_data;
David Sterbaf4c48b42020-06-09 19:19:27 +02007938 down_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007939 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7940 &BTRFS_I(inode)->runtime_flags)) {
7941 inode_dio_end(inode);
7942 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7943 wakeup = false;
David Sterbaf4c48b42020-06-09 19:19:27 +02007944 }
7945
David Sterba55e20bd2020-06-09 19:56:06 +02007946 ret = __blockdev_direct_IO(iocb, inode,
7947 fs_info->fs_devices->latest_bdev,
7948 iter, btrfs_get_blocks_direct, NULL,
7949 btrfs_submit_direct, flags);
David Sterbaf4c48b42020-06-09 19:19:27 +02007950 if (iov_iter_rw(iter) == WRITE) {
7951 up_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007952 current->journal_info = NULL;
7953 if (ret < 0 && ret != -EIOCBQUEUED) {
7954 if (dio_data.reserve)
7955 btrfs_delalloc_release_space(inode, data_reserved,
7956 offset, dio_data.reserve, true);
7957 /*
7958 * On error we might have left some ordered extents
7959 * without submitting corresponding bios for them, so
7960 * cleanup them up to avoid other tasks getting them
7961 * and waiting for them to complete forever.
7962 */
7963 if (dio_data.unsubmitted_oe_range_start <
7964 dio_data.unsubmitted_oe_range_end)
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007965 __endio_write_update_ordered(BTRFS_I(inode),
David Sterba55e20bd2020-06-09 19:56:06 +02007966 dio_data.unsubmitted_oe_range_start,
7967 dio_data.unsubmitted_oe_range_end -
7968 dio_data.unsubmitted_oe_range_start,
7969 false);
7970 } else if (ret >= 0 && (size_t)ret < count)
7971 btrfs_delalloc_release_space(inode, data_reserved,
7972 offset, count - (size_t)ret, true);
7973 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
David Sterbaf4c48b42020-06-09 19:19:27 +02007974 }
7975out:
David Sterba55e20bd2020-06-09 19:56:06 +02007976 if (wakeup)
7977 inode_dio_end(inode);
David Sterbaf4c48b42020-06-09 19:19:27 +02007978 if (relock)
7979 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02007980
David Sterbaf4c48b42020-06-09 19:19:27 +02007981 extent_changeset_free(data_reserved);
7982 return ret;
7983}
Chris Mason16432982008-04-10 10:23:21 -04007984
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007985static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02007986 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007987{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007988 int ret;
7989
Christoph Hellwig45dd0522020-05-23 09:30:14 +02007990 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007991 if (ret)
7992 return ret;
7993
David Sterba2135fb92017-06-23 04:09:57 +02007994 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007995}
7996
Chris Mason9ebefb182007-06-15 13:50:00 -04007997int btrfs_readpage(struct file *file, struct page *page)
7998{
David Sterba71ad38b2020-02-05 19:09:35 +01007999 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008000}
Chris Mason1832a6d2007-12-21 16:27:21 -05008001
Chris Mason39279cc2007-06-12 06:35:45 -04008002static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8003{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008004 struct inode *inode = page->mapping->host;
8005 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008006
8007 if (current->flags & PF_MEMALLOC) {
8008 redirty_page_for_writepage(wbc, page);
8009 unlock_page(page);
8010 return 0;
8011 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008012
8013 /*
8014 * If we are under memory pressure we will call this directly from the
8015 * VM, we need to make sure we have the inode referenced for the ordered
8016 * extent. If not just return like we didn't do anything.
8017 */
8018 if (!igrab(inode)) {
8019 redirty_page_for_writepage(wbc, page);
8020 return AOP_WRITEPAGE_ACTIVATE;
8021 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008022 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008023 btrfs_add_delayed_iput(inode);
8024 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008025}
Chris Mason39279cc2007-06-12 06:35:45 -04008026
Eric Sandeen48a3b632013-04-25 20:41:01 +00008027static int btrfs_writepages(struct address_space *mapping,
8028 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008029{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008030 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008031}
8032
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008033static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008034{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008035 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008036}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008037
Chris Masone6dcd2d2008-07-17 12:53:50 -04008038static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008039{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008040 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008041 if (ret == 1)
8042 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008043 return ret;
8044}
Chris Mason39279cc2007-06-12 06:35:45 -04008045
Chris Masone6dcd2d2008-07-17 12:53:50 -04008046static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8047{
Chris Mason98509cf2008-09-11 15:51:43 -04008048 if (PageWriteback(page) || PageDirty(page))
8049 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008050 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008051}
8052
Roman Gushchinf8e66082020-03-04 16:57:35 -08008053#ifdef CONFIG_MIGRATION
8054static int btrfs_migratepage(struct address_space *mapping,
8055 struct page *newpage, struct page *page,
8056 enum migrate_mode mode)
8057{
8058 int ret;
8059
8060 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8061 if (ret != MIGRATEPAGE_SUCCESS)
8062 return ret;
8063
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008064 if (page_has_private(page))
8065 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008066
8067 if (PagePrivate2(page)) {
8068 ClearPagePrivate2(page);
8069 SetPagePrivate2(newpage);
8070 }
8071
8072 if (mode != MIGRATE_SYNC_NO_COPY)
8073 migrate_page_copy(newpage, page);
8074 else
8075 migrate_page_states(newpage, page);
8076 return MIGRATEPAGE_SUCCESS;
8077}
8078#endif
8079
Lukas Czernerd47992f2013-05-21 23:17:23 -04008080static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8081 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008082{
Josef Bacik5fd02042012-05-02 14:00:54 -04008083 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008084 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008085 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008086 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008087 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008088 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308089 u64 start;
8090 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008091 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008092
Chris Mason8b62b722009-09-02 16:53:46 -04008093 /*
8094 * we have the page locked, so new writeback can't start,
8095 * and the dirty bit won't be cleared while we are here.
8096 *
8097 * Wait for IO on this page so that we can safely clear
8098 * the PagePrivate2 bit and do ordered accounting
8099 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008100 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008101
Josef Bacik5fd02042012-05-02 14:00:54 -04008102 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008103 if (offset) {
8104 btrfs_releasepage(page, GFP_NOFS);
8105 return;
8106 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008107
8108 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008109 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308110again:
8111 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008112 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308113 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008114 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008115 end = min(page_end,
8116 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008117 /*
8118 * IO on this page will never be started, so we need
8119 * to account for any ordered extents now
8120 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008121 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308122 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008123 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008124 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008125 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008126 /*
8127 * whoever cleared the private bit is responsible
8128 * for the finish_ordered_io
8129 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008130 if (TestClearPagePrivate2(page)) {
8131 struct btrfs_ordered_inode_tree *tree;
8132 u64 new_len;
8133
8134 tree = &BTRFS_I(inode)->ordered_tree;
8135
8136 spin_lock_irq(&tree->lock);
8137 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308138 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008139 if (new_len < ordered->truncated_len)
8140 ordered->truncated_len = new_len;
8141 spin_unlock_irq(&tree->lock);
8142
8143 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308144 start,
8145 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008146 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008147 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008148 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008149 if (!inode_evicting) {
8150 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308151 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008152 &cached_state);
8153 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308154
8155 start = end + 1;
8156 if (start < page_end)
8157 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008158 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008159
Qu Wenruob9d0b382015-09-29 10:35:16 +08008160 /*
8161 * Qgroup reserved space handler
8162 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008163 * 1) Already written to disk or ordered extent already submitted
8164 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8165 * Qgroup will be handled by its qgroup_record then.
8166 * btrfs_qgroup_free_data() call will do nothing here.
8167 *
8168 * 2) Not written to disk yet
8169 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8170 * bit of its io_tree, and free the qgroup reserved data space.
8171 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008172 */
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03008173 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008174 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008175 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008176 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8177 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008178 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008179
8180 __btrfs_releasepage(page, GFP_NOFS);
8181 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008182
Chris Mason4a096752008-07-21 10:29:44 -04008183 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008184 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008185}
8186
Chris Mason9ebefb182007-06-15 13:50:00 -04008187/*
8188 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8189 * called from a page fault handler when a page is first dirtied. Hence we must
8190 * be careful to check for EOF conditions here. We set the page up correctly
8191 * for a written page which means we get ENOSPC checking when writing into
8192 * holes and correct delalloc and unwritten extent mapping on filesystems that
8193 * support these features.
8194 *
8195 * We are not allowed to take the i_mutex here so we have to play games to
8196 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008197 * truncate_setsize() writes the inode size before removing pages, once we have
8198 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008199 * beyond EOF, then the page is guaranteed safe against truncation until we
8200 * unlock the page.
8201 */
Souptick Joardera528a242018-06-06 19:54:44 +05308202vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008203{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008204 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008205 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008206 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008207 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8208 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008209 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008210 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008211 char *kaddr;
8212 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008213 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308214 vm_fault_t ret;
8215 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008216 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308217 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008218 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008219 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308220 u64 end;
8221
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008222 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008223
Jan Karab2b5ef52012-06-12 16:20:45 +02008224 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008225 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008226 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308227 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008228
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308229 /*
8230 * Reserving delalloc space after obtaining the page lock can lead to
8231 * deadlock. For example, if a dirty page is locked by this function
8232 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8233 * dirty page write out, then the btrfs_writepage() function could
8234 * end up waiting indefinitely to get a lock on the page currently
8235 * being processed by btrfs_page_mkwrite() function.
8236 */
Souptick Joardera528a242018-06-06 19:54:44 +05308237 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308238 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308239 if (!ret2) {
8240 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008241 reserved = 1;
8242 }
Souptick Joardera528a242018-06-06 19:54:44 +05308243 if (ret2) {
8244 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008245 if (reserved)
8246 goto out;
8247 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008248 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008249
Nick Piggin56a76f82009-03-31 15:23:23 -07008250 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008251again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008252 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008253 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008254
Chris Mason9ebefb182007-06-15 13:50:00 -04008255 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008256 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008257 /* page got truncated out from underneath us */
8258 goto out_unlock;
8259 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008260 wait_on_page_writeback(page);
8261
David Sterbaff13db42015-12-03 14:30:40 +01008262 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008263 set_page_extent_mapped(page);
8264
Chris Masoneb84ae02008-07-17 13:53:27 -04008265 /*
8266 * we can't set the delalloc bits if there are pending ordered
8267 * extents. Drop our locks and wait for them to finish
8268 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008269 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8270 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008271 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008272 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008273 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008274 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008275 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008276 btrfs_put_ordered_extent(ordered);
8277 goto again;
8278 }
8279
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008280 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008281 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008282 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008283 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308284 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008285 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008286 page_start, PAGE_SIZE - reserved_space,
8287 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308288 }
8289 }
8290
Josef Bacikfbf19082009-10-01 17:10:23 -04008291 /*
Liu Bo54160342016-12-13 12:15:19 -08008292 * page_mkwrite gets called when the page is firstly dirtied after it's
8293 * faulted in, but write(2) could also dirty a page and set delalloc
8294 * bits, thus in this case for space account reason, we still need to
8295 * clear any delalloc bits within this page range since we have to
8296 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008297 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308298 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008299 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8300 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008301
Souptick Joardera528a242018-06-06 19:54:44 +05308302 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008303 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308304 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008305 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008306 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008307 ret = VM_FAULT_SIGBUS;
8308 goto out_unlock;
8309 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008310
8311 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008312 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008313 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008314 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008315 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008316
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008317 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008318 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008319 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008320 flush_dcache_page(page);
8321 kunmap(page);
8322 }
Chris Mason247e7432008-07-17 12:53:51 -04008323 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008324 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008325 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008326
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008327 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008328 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008329 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008330
David Sterbae43bbe52017-12-12 21:43:52 +01008331 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008332
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008333 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8334 sb_end_pagefault(inode->i_sb);
8335 extent_changeset_free(data_reserved);
8336 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008337
8338out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008339 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008340out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008341 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008342 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008343 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008344out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008345 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008346 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008347 return ret;
8348}
8349
Filipe Manana213e8c52018-02-06 20:40:31 +00008350static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008351{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008352 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008353 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008354 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008355 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008356 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008357 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008358 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008359
Filipe Manana213e8c52018-02-06 20:40:31 +00008360 if (!skip_writeback) {
8361 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8362 (u64)-1);
8363 if (ret)
8364 return ret;
8365 }
Chris Mason39279cc2007-06-12 06:35:45 -04008366
Josef Bacikfcb80c22011-05-03 10:40:22 -04008367 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008368 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8369 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008370 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008371 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008372 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008373 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008374 * be free'd up by the truncate operation, but also have some slack
8375 * space reserved in case it uses space during the truncate (thank you
8376 * very much snapshotting).
8377 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008378 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008379 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008380 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008381 * doesn't end up using space reserved for updating the inode. We also
8382 * need to be able to stop the transaction and start a new one, which
8383 * means we need to be able to update the inode several times, and we
8384 * have no idea of knowing how many times that will be, so we can't just
8385 * reserve 1 item for the entirety of the operation, so that has to be
8386 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008387 *
8388 * So that leaves us with
8389 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008390 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008391 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008392 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008393 * updating the inode.
8394 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008395 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008396 if (!rsv)
8397 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008398 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008399 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008400
Josef Bacik907cbce2011-08-08 13:46:15 -04008401 /*
Josef Bacik07127182011-08-19 10:29:59 -04008402 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008403 * 1 for updating the inode.
8404 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008405 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008406 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008407 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008408 goto out;
8409 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008410
Josef Bacik907cbce2011-08-08 13:46:15 -04008411 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008412 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008413 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008414 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008415
Chris Mason5a3f23d2009-03-31 13:27:11 -04008416 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008417 * So if we truncate and then write and fsync we normally would just
8418 * write the extents that changed, which is a problem if we need to
8419 * first truncate that entire inode. So set this flag so we write out
8420 * all of the extents in the inode to the sync log so we're completely
8421 * safe.
8422 */
8423 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008424 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008425
Yan, Zheng80825102009-11-12 09:35:36 +00008426 while (1) {
8427 ret = btrfs_truncate_inode_items(trans, root, inode,
8428 inode->i_size,
8429 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008430 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008431 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008432 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008433
Yan, Zheng80825102009-11-12 09:35:36 +00008434 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008435 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008436 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008437
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008438 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008439 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008440
8441 trans = btrfs_start_transaction(root, 2);
8442 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008443 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008444 trans = NULL;
8445 break;
8446 }
8447
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008448 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008449 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008450 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008451 BUG_ON(ret); /* shouldn't happen */
8452 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008453 }
8454
Josef Bacikddfae632017-10-19 14:16:02 -04008455 /*
8456 * We can't call btrfs_truncate_block inside a trans handle as we could
8457 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8458 * we've truncated everything except the last little bit, and can do
8459 * btrfs_truncate_block and then update the disk_i_size.
8460 */
8461 if (ret == NEED_TRUNCATE_BLOCK) {
8462 btrfs_end_transaction(trans);
8463 btrfs_btree_balance_dirty(fs_info);
8464
8465 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8466 if (ret)
8467 goto out;
8468 trans = btrfs_start_transaction(root, 1);
8469 if (IS_ERR(trans)) {
8470 ret = PTR_ERR(trans);
8471 goto out;
8472 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008473 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008474 }
8475
Chris Mason917c16b2011-11-08 14:49:59 -05008476 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008477 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008478
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008479 trans->block_rsv = &fs_info->trans_block_rsv;
8480 ret2 = btrfs_update_inode(trans, root, inode);
8481 if (ret2 && !ret)
8482 ret = ret2;
8483
8484 ret2 = btrfs_end_transaction(trans);
8485 if (ret2 && !ret)
8486 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008487 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008488 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008489out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008490 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008491
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008492 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008493}
8494
Sven Wegener3b963622008-06-09 21:57:42 -04008495/*
Chris Masond352ac62008-09-29 15:18:18 -04008496 * create a new subvolume directory/inode (helper for the ioctl).
8497 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008498int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008499 struct btrfs_root *new_root,
8500 struct btrfs_root *parent_root,
8501 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008502{
Chris Mason39279cc2007-06-12 06:35:45 -04008503 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008504 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008505 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008506
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008507 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8508 new_dirid, new_dirid,
8509 S_IFDIR | (~current_umask() & S_IRWXUGO),
8510 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008511 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008512 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008513 inode->i_op = &btrfs_dir_inode_operations;
8514 inode->i_fop = &btrfs_dir_file_operations;
8515
Miklos Szeredibfe86842011-10-28 14:13:29 +02008516 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008517 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008518 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008519
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008520 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8521 if (err)
8522 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008523 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008524 new_root->root_key.objectid, err);
8525
Yan, Zheng76dda932009-09-21 16:00:26 -04008526 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008527
Yan, Zheng76dda932009-09-21 16:00:26 -04008528 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008529 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008530}
8531
Chris Mason39279cc2007-06-12 06:35:45 -04008532struct inode *btrfs_alloc_inode(struct super_block *sb)
8533{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008534 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008535 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008536 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008537
David Sterba712e36c2017-10-31 17:08:27 +01008538 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008539 if (!ei)
8540 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008541
8542 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008543 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008544 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008545 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008546 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008547 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008548 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008549 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008550 ei->disk_i_size = 0;
8551 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008552 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008553 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008554 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008555 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008556 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008557
Josef Bacik9e0baf62011-07-15 15:16:44 +00008558 spin_lock_init(&ei->lock);
8559 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008560 if (sb->s_magic != BTRFS_TEST_MAGIC)
8561 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8562 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008563 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008564 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008565 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008566
Miao Xie16cdcec2011-04-22 18:12:22 +08008567 ei->delayed_node = NULL;
8568
chandan r9cc97d62012-07-04 12:48:07 +05308569 ei->i_otime.tv_sec = 0;
8570 ei->i_otime.tv_nsec = 0;
8571
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008572 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008573 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008574 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8575 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8576 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008577 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8578 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008579 ei->io_tree.track_uptodate = true;
8580 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008581 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008582 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008583 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008584 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008585 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008586 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008587 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008588
8589 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008590}
8591
Josef Bacikaaedb552013-10-11 14:44:09 -04008592#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8593void btrfs_test_destroy_inode(struct inode *inode)
8594{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008595 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008596 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8597}
8598#endif
8599
Al Viro26602ca2019-04-10 15:14:41 -04008600void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008601{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008602 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8603}
8604
Chris Mason39279cc2007-06-12 06:35:45 -04008605void btrfs_destroy_inode(struct inode *inode)
8606{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008607 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008608 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008609 struct btrfs_root *root = BTRFS_I(inode)->root;
8610
Al Virob3d9b7a2012-06-09 13:51:19 -04008611 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008612 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008613 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8614 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008615 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008616 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008617 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008618 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008619 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008620
Chris Mason5a3f23d2009-03-31 13:27:11 -04008621 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008622 * This can happen where we create an inode, but somebody else also
8623 * created the same inode and we need to destroy the one we already
8624 * created.
8625 */
8626 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008627 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008628
Chris Masond3977122009-01-05 21:25:51 -05008629 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008630 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8631 if (!ordered)
8632 break;
8633 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008634 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008635 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008636 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008637 btrfs_remove_ordered_extent(inode, ordered);
8638 btrfs_put_ordered_extent(ordered);
8639 btrfs_put_ordered_extent(ordered);
8640 }
8641 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008642 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008643 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008644 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008645 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008646 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008647}
8648
Al Viro45321ac2010-06-07 13:43:19 -04008649int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008650{
8651 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008652
Naohiro Aota6379ef92013-06-06 09:56:34 +00008653 if (root == NULL)
8654 return 1;
8655
Liu Bofa6ac872013-02-20 14:10:23 +00008656 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008657 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008658 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008659 else
Al Viro45321ac2010-06-07 13:43:19 -04008660 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008661}
8662
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008663static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008664{
8665 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8666
8667 inode_init_once(&ei->vfs_inode);
8668}
8669
David Sterbae67c7182018-02-19 17:24:18 +01008670void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008671{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008672 /*
8673 * Make sure all delayed rcu free inodes are flushed before we
8674 * destroy cache.
8675 */
8676 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008677 kmem_cache_destroy(btrfs_inode_cachep);
8678 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008679 kmem_cache_destroy(btrfs_path_cachep);
8680 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008681 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008682}
8683
Liu Bof5c29bd2017-11-02 17:21:50 -06008684int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008685{
David Sterba837e1972012-09-07 03:00:48 -06008686 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008687 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008688 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8689 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008690 if (!btrfs_inode_cachep)
8691 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008692
David Sterba837e1972012-09-07 03:00:48 -06008693 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008694 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008695 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008696 if (!btrfs_trans_handle_cachep)
8697 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008698
David Sterba837e1972012-09-07 03:00:48 -06008699 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008700 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008701 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008702 if (!btrfs_path_cachep)
8703 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008704
David Sterba837e1972012-09-07 03:00:48 -06008705 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008706 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008707 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008708 if (!btrfs_free_space_cachep)
8709 goto fail;
8710
Christophe Leroy3acd4852019-08-21 15:05:55 +00008711 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8712 PAGE_SIZE, PAGE_SIZE,
8713 SLAB_RED_ZONE, NULL);
8714 if (!btrfs_free_space_bitmap_cachep)
8715 goto fail;
8716
Chris Mason39279cc2007-06-12 06:35:45 -04008717 return 0;
8718fail:
8719 btrfs_destroy_cachep();
8720 return -ENOMEM;
8721}
8722
David Howellsa528d352017-01-31 16:46:22 +00008723static int btrfs_getattr(const struct path *path, struct kstat *stat,
8724 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008725{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008726 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008727 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008728 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008729 u32 bi_flags = BTRFS_I(inode)->flags;
8730
8731 stat->result_mask |= STATX_BTIME;
8732 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8733 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8734 if (bi_flags & BTRFS_INODE_APPEND)
8735 stat->attributes |= STATX_ATTR_APPEND;
8736 if (bi_flags & BTRFS_INODE_COMPRESS)
8737 stat->attributes |= STATX_ATTR_COMPRESSED;
8738 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8739 stat->attributes |= STATX_ATTR_IMMUTABLE;
8740 if (bi_flags & BTRFS_INODE_NODUMP)
8741 stat->attributes |= STATX_ATTR_NODUMP;
8742
8743 stat->attributes_mask |= (STATX_ATTR_APPEND |
8744 STATX_ATTR_COMPRESSED |
8745 STATX_ATTR_IMMUTABLE |
8746 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008747
Chris Mason39279cc2007-06-12 06:35:45 -04008748 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008749 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008750
8751 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008752 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008753 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008754 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008755 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008756 return 0;
8757}
8758
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008759static int btrfs_rename_exchange(struct inode *old_dir,
8760 struct dentry *old_dentry,
8761 struct inode *new_dir,
8762 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008763{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008764 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008765 struct btrfs_trans_handle *trans;
8766 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008767 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008768 struct inode *new_inode = new_dentry->d_inode;
8769 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008770 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008771 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008772 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8773 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008774 u64 old_idx = 0;
8775 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008776 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008777 bool root_log_pinned = false;
8778 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008779 struct btrfs_log_ctx ctx_root;
8780 struct btrfs_log_ctx ctx_dest;
8781 bool sync_log_root = false;
8782 bool sync_log_dest = false;
8783 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008784
8785 /* we only allow rename subvolume link between subvolumes */
8786 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8787 return -EXDEV;
8788
Filipe Mananad4682ba2018-06-11 19:24:28 +01008789 btrfs_init_log_ctx(&ctx_root, old_inode);
8790 btrfs_init_log_ctx(&ctx_dest, new_inode);
8791
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008792 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008793 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8794 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008795 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008796
8797 /*
8798 * We want to reserve the absolute worst case amount of items. So if
8799 * both inodes are subvols and we need to unlink them then that would
8800 * require 4 item modifications, but if they are both normal inodes it
8801 * would require 5 item modifications, so we'll assume their normal
8802 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8803 * should cover the worst case number of items we'll modify.
8804 */
8805 trans = btrfs_start_transaction(root, 12);
8806 if (IS_ERR(trans)) {
8807 ret = PTR_ERR(trans);
8808 goto out_notrans;
8809 }
8810
Josef Bacik3e174092019-11-15 15:43:06 -05008811 if (dest != root)
8812 btrfs_record_root_in_trans(trans, dest);
8813
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008814 /*
8815 * We need to find a free sequence number both in the source and
8816 * in the destination directory for the exchange.
8817 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008818 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008819 if (ret)
8820 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008821 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008822 if (ret)
8823 goto out_fail;
8824
8825 BTRFS_I(old_inode)->dir_index = 0ULL;
8826 BTRFS_I(new_inode)->dir_index = 0ULL;
8827
8828 /* Reference for the source. */
8829 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8830 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008831 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008832 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008833 btrfs_pin_log_trans(root);
8834 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008835 ret = btrfs_insert_inode_ref(trans, dest,
8836 new_dentry->d_name.name,
8837 new_dentry->d_name.len,
8838 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008839 btrfs_ino(BTRFS_I(new_dir)),
8840 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008841 if (ret)
8842 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008843 }
8844
8845 /* And now for the dest. */
8846 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8847 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008848 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008849 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008850 btrfs_pin_log_trans(dest);
8851 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008852 ret = btrfs_insert_inode_ref(trans, root,
8853 old_dentry->d_name.name,
8854 old_dentry->d_name.len,
8855 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008856 btrfs_ino(BTRFS_I(old_dir)),
8857 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008858 if (ret)
8859 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008860 }
8861
8862 /* Update inode version and ctime/mtime. */
8863 inode_inc_iversion(old_dir);
8864 inode_inc_iversion(new_dir);
8865 inode_inc_iversion(old_inode);
8866 inode_inc_iversion(new_inode);
8867 old_dir->i_ctime = old_dir->i_mtime = ctime;
8868 new_dir->i_ctime = new_dir->i_mtime = ctime;
8869 old_inode->i_ctime = ctime;
8870 new_inode->i_ctime = ctime;
8871
8872 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008873 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8874 BTRFS_I(old_inode), 1);
8875 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8876 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008877 }
8878
8879 /* src is a subvolume */
8880 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008881 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008882 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008883 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8884 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008885 old_dentry->d_name.name,
8886 old_dentry->d_name.len);
8887 if (!ret)
8888 ret = btrfs_update_inode(trans, root, old_inode);
8889 }
8890 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008891 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008892 goto out_fail;
8893 }
8894
8895 /* dest is a subvolume */
8896 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008897 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008898 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008899 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8900 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008901 new_dentry->d_name.name,
8902 new_dentry->d_name.len);
8903 if (!ret)
8904 ret = btrfs_update_inode(trans, dest, new_inode);
8905 }
8906 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008907 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008908 goto out_fail;
8909 }
8910
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008911 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008912 new_dentry->d_name.name,
8913 new_dentry->d_name.len, 0, old_idx);
8914 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008915 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008916 goto out_fail;
8917 }
8918
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008919 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008920 old_dentry->d_name.name,
8921 old_dentry->d_name.len, 0, new_idx);
8922 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008923 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008924 goto out_fail;
8925 }
8926
8927 if (old_inode->i_nlink == 1)
8928 BTRFS_I(old_inode)->dir_index = old_idx;
8929 if (new_inode->i_nlink == 1)
8930 BTRFS_I(new_inode)->dir_index = new_idx;
8931
Filipe Manana86e8aa02016-05-05 02:02:27 +01008932 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008933 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008934 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8935 BTRFS_I(old_dir), parent,
8936 false, &ctx_root);
8937 if (ret == BTRFS_NEED_LOG_SYNC)
8938 sync_log_root = true;
8939 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8940 commit_transaction = true;
8941 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008942 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008943 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008944 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008945 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008946 if (!commit_transaction) {
8947 parent = old_dentry->d_parent;
8948 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8949 BTRFS_I(new_dir), parent,
8950 false, &ctx_dest);
8951 if (ret == BTRFS_NEED_LOG_SYNC)
8952 sync_log_dest = true;
8953 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8954 commit_transaction = true;
8955 ret = 0;
8956 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008957 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008958 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008959 }
8960out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008961 /*
8962 * If we have pinned a log and an error happened, we unpin tasks
8963 * trying to sync the log and force them to fallback to a transaction
8964 * commit if the log currently contains any of the inodes involved in
8965 * this rename operation (to ensure we do not persist a log with an
8966 * inconsistent state for any of these inodes or leading to any
8967 * inconsistencies when replayed). If the transaction was aborted, the
8968 * abortion reason is propagated to userspace when attempting to commit
8969 * the transaction. If the log does not contain any of these inodes, we
8970 * allow the tasks to sync it.
8971 */
8972 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008973 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8974 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8975 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008976 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008977 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008978 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008979
8980 if (root_log_pinned) {
8981 btrfs_end_log_trans(root);
8982 root_log_pinned = false;
8983 }
8984 if (dest_log_pinned) {
8985 btrfs_end_log_trans(dest);
8986 dest_log_pinned = false;
8987 }
8988 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008989 if (!ret && sync_log_root && !commit_transaction) {
8990 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
8991 &ctx_root);
8992 if (ret)
8993 commit_transaction = true;
8994 }
8995 if (!ret && sync_log_dest && !commit_transaction) {
8996 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
8997 &ctx_dest);
8998 if (ret)
8999 commit_transaction = true;
9000 }
9001 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009002 /*
9003 * We may have set commit_transaction when logging the new name
9004 * in the destination root, in which case we left the source
9005 * root context in the list of log contextes. So make sure we
9006 * remove it to avoid invalid memory accesses, since the context
9007 * was allocated in our stack frame.
9008 */
9009 if (sync_log_root) {
9010 mutex_lock(&root->log_mutex);
9011 list_del_init(&ctx_root.list);
9012 mutex_unlock(&root->log_mutex);
9013 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009014 ret = btrfs_commit_transaction(trans);
9015 } else {
9016 int ret2;
9017
9018 ret2 = btrfs_end_transaction(trans);
9019 ret = ret ? ret : ret2;
9020 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009021out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009022 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9023 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009024 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009025
Filipe Mananae6c61712019-11-08 16:11:56 +00009026 ASSERT(list_empty(&ctx_root.list));
9027 ASSERT(list_empty(&ctx_dest.list));
9028
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009029 return ret;
9030}
9031
9032static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9033 struct btrfs_root *root,
9034 struct inode *dir,
9035 struct dentry *dentry)
9036{
9037 int ret;
9038 struct inode *inode;
9039 u64 objectid;
9040 u64 index;
9041
9042 ret = btrfs_find_free_ino(root, &objectid);
9043 if (ret)
9044 return ret;
9045
9046 inode = btrfs_new_inode(trans, root, dir,
9047 dentry->d_name.name,
9048 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009049 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009050 objectid,
9051 S_IFCHR | WHITEOUT_MODE,
9052 &index);
9053
9054 if (IS_ERR(inode)) {
9055 ret = PTR_ERR(inode);
9056 return ret;
9057 }
9058
9059 inode->i_op = &btrfs_special_inode_operations;
9060 init_special_inode(inode, inode->i_mode,
9061 WHITEOUT_DEV);
9062
9063 ret = btrfs_init_inode_security(trans, inode, dir,
9064 &dentry->d_name);
9065 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009066 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009067
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009068 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9069 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009070 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009071 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009072
9073 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009074out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009075 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009076 if (ret)
9077 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009078 iput(inode);
9079
Filipe Mananac9901612016-05-05 01:41:57 +01009080 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009081}
9082
Chris Mason39279cc2007-06-12 06:35:45 -04009083static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009084 struct inode *new_dir, struct dentry *new_dentry,
9085 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009086{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009087 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009088 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009089 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009090 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9091 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009092 struct inode *new_inode = d_inode(new_dentry);
9093 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009094 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009095 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009096 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009097 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009098 struct btrfs_log_ctx ctx;
9099 bool sync_log = false;
9100 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009101
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009102 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009103 return -EPERM;
9104
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009105 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009106 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009107 return -EXDEV;
9108
Li Zefan33345d012011-04-20 10:31:50 +08009109 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009110 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009111 return -ENOTEMPTY;
9112
Chris Mason39279cc2007-06-12 06:35:45 -04009113 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009114 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009115 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009116
9117
9118 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009119 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009120 new_dentry->d_name.name,
9121 new_dentry->d_name.len);
9122
9123 if (ret) {
9124 if (ret == -EEXIST) {
9125 /* we shouldn't get
9126 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309127 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009128 return ret;
9129 }
9130 } else {
9131 /* maybe -EOVERFLOW */
9132 return ret;
9133 }
9134 }
9135 ret = 0;
9136
Chris Mason5a3f23d2009-03-31 13:27:11 -04009137 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009138 * we're using rename to replace one file with another. Start IO on it
9139 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009140 */
Chris Mason8d875f92014-08-12 10:47:42 -07009141 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009142 filemap_flush(old_inode->i_mapping);
9143
Yan, Zheng76dda932009-09-21 16:00:26 -04009144 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009145 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009146 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009147 /*
9148 * We want to reserve the absolute worst case amount of items. So if
9149 * both inodes are subvols and we need to unlink them then that would
9150 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009151 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009152 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9153 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009154 * If our rename has the whiteout flag, we need more 5 units for the
9155 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9156 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009157 */
Filipe Manana5062af32016-05-05 10:26:26 +01009158 trans_num_items = 11;
9159 if (flags & RENAME_WHITEOUT)
9160 trans_num_items += 5;
9161 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009162 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009163 ret = PTR_ERR(trans);
9164 goto out_notrans;
9165 }
Chris Mason5f39d392007-10-15 16:14:19 -04009166
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009167 if (dest != root)
9168 btrfs_record_root_in_trans(trans, dest);
9169
Nikolay Borisov877574e2017-02-20 13:50:33 +02009170 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009171 if (ret)
9172 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009173
Miao Xie67de1172013-12-26 13:07:06 +08009174 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009175 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009176 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009177 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009178 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009179 btrfs_pin_log_trans(root);
9180 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009181 ret = btrfs_insert_inode_ref(trans, dest,
9182 new_dentry->d_name.name,
9183 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009184 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009185 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009186 if (ret)
9187 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009188 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009189
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009190 inode_inc_iversion(old_dir);
9191 inode_inc_iversion(new_dir);
9192 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009193 old_dir->i_ctime = old_dir->i_mtime =
9194 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009195 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009196
Chris Mason12fcfd22009-03-24 10:24:20 -04009197 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009198 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9199 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009200
Li Zefan33345d012011-04-20 10:31:50 +08009201 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009202 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009203 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009204 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9205 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009206 old_dentry->d_name.name,
9207 old_dentry->d_name.len);
9208 if (!ret)
9209 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009210 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009211 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009212 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009213 goto out_fail;
9214 }
Chris Mason39279cc2007-06-12 06:35:45 -04009215
9216 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009217 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009218 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009219 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009220 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009221 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009222 BUG_ON(new_inode->i_nlink == 0);
9223 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009224 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9225 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009226 new_dentry->d_name.name,
9227 new_dentry->d_name.len);
9228 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009229 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009230 ret = btrfs_orphan_add(trans,
9231 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009232 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009233 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009234 goto out_fail;
9235 }
Chris Mason39279cc2007-06-12 06:35:45 -04009236 }
Josef Bacikaec74772008-07-24 12:12:38 -04009237
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009238 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009239 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009240 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009241 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009242 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009243 goto out_fail;
9244 }
Chris Mason39279cc2007-06-12 06:35:45 -04009245
Miao Xie67de1172013-12-26 13:07:06 +08009246 if (old_inode->i_nlink == 1)
9247 BTRFS_I(old_inode)->dir_index = index;
9248
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009249 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009250 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009251
Filipe Mananad4682ba2018-06-11 19:24:28 +01009252 btrfs_init_log_ctx(&ctx, old_inode);
9253 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9254 BTRFS_I(old_dir), parent,
9255 false, &ctx);
9256 if (ret == BTRFS_NEED_LOG_SYNC)
9257 sync_log = true;
9258 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9259 commit_transaction = true;
9260 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009261 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009262 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009263 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009264
9265 if (flags & RENAME_WHITEOUT) {
9266 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9267 old_dentry);
9268
9269 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009270 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009271 goto out_fail;
9272 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009273 }
Chris Mason39279cc2007-06-12 06:35:45 -04009274out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009275 /*
9276 * If we have pinned the log and an error happened, we unpin tasks
9277 * trying to sync the log and force them to fallback to a transaction
9278 * commit if the log currently contains any of the inodes involved in
9279 * this rename operation (to ensure we do not persist a log with an
9280 * inconsistent state for any of these inodes or leading to any
9281 * inconsistencies when replayed). If the transaction was aborted, the
9282 * abortion reason is propagated to userspace when attempting to commit
9283 * the transaction. If the log does not contain any of these inodes, we
9284 * allow the tasks to sync it.
9285 */
9286 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009287 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9288 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9289 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009290 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009291 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009292 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009293
9294 btrfs_end_log_trans(root);
9295 log_pinned = false;
9296 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009297 if (!ret && sync_log) {
9298 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9299 if (ret)
9300 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009301 } else if (sync_log) {
9302 mutex_lock(&root->log_mutex);
9303 list_del(&ctx.list);
9304 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009305 }
9306 if (commit_transaction) {
9307 ret = btrfs_commit_transaction(trans);
9308 } else {
9309 int ret2;
9310
9311 ret2 = btrfs_end_transaction(trans);
9312 ret = ret ? ret : ret2;
9313 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009314out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009315 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009316 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009317
Chris Mason39279cc2007-06-12 06:35:45 -04009318 return ret;
9319}
9320
Miklos Szeredi80ace852014-07-23 15:15:32 +02009321static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9322 struct inode *new_dir, struct dentry *new_dentry,
9323 unsigned int flags)
9324{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009325 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009326 return -EINVAL;
9327
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009328 if (flags & RENAME_EXCHANGE)
9329 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9330 new_dentry);
9331
9332 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009333}
9334
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009335struct btrfs_delalloc_work {
9336 struct inode *inode;
9337 struct completion completion;
9338 struct list_head list;
9339 struct btrfs_work work;
9340};
9341
Miao Xie8ccf6f192012-10-25 09:28:04 +00009342static void btrfs_run_delalloc_work(struct btrfs_work *work)
9343{
9344 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009345 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009346
9347 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9348 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009349 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009350 filemap_flush(inode->i_mapping);
9351 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9352 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009353 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009354
Nikolay Borisov076da912018-04-23 10:54:16 +03009355 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009356 complete(&delalloc_work->completion);
9357}
9358
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009359static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009360{
9361 struct btrfs_delalloc_work *work;
9362
David Sterba100d5702015-12-08 14:39:32 +01009363 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009364 if (!work)
9365 return NULL;
9366
9367 init_completion(&work->completion);
9368 INIT_LIST_HEAD(&work->list);
9369 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009370 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009371
9372 return work;
9373}
9374
Chris Masond352ac62008-09-29 15:18:18 -04009375/*
9376 * some fairly slow code that needs optimization. This walks the list
9377 * of all the inodes with pending delalloc and forces them to disk.
9378 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009379static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009380{
Chris Masonea8c2812008-08-04 23:17:27 -04009381 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009382 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009383 struct btrfs_delalloc_work *work, *next;
9384 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009385 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009386 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009387
Miao Xie8ccf6f192012-10-25 09:28:04 +00009388 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009389 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009390
Miao Xie573bfb72014-03-06 13:55:03 +08009391 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009392 spin_lock(&root->delalloc_lock);
9393 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009394 while (!list_empty(&splice)) {
9395 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009396 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009397
Miao Xieeb73c1b2013-05-15 07:48:22 +00009398 list_move_tail(&binode->delalloc_inodes,
9399 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009400 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009401 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009402 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009403 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009404 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009405 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009406
Ethan Lien3cd24c62018-11-01 14:49:03 +08009407 if (snapshot)
9408 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9409 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009410 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009411 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009412 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009413 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009414 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009415 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009416 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009417 btrfs_queue_work(root->fs_info->flush_workers,
9418 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009419 ret++;
9420 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009421 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009422 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009423 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009424 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009425 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009426
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009427out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009428 list_for_each_entry_safe(work, next, &works, list) {
9429 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009430 wait_for_completion(&work->completion);
9431 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009432 }
9433
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009434 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009435 spin_lock(&root->delalloc_lock);
9436 list_splice_tail(&splice, &root->delalloc_inodes);
9437 spin_unlock(&root->delalloc_lock);
9438 }
Miao Xie573bfb72014-03-06 13:55:03 +08009439 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009440 return ret;
9441}
9442
Ethan Lien3cd24c62018-11-01 14:49:03 +08009443int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009444{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009445 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009446 int ret;
9447
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009448 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009449 return -EROFS;
9450
Ethan Lien3cd24c62018-11-01 14:49:03 +08009451 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009452 if (ret > 0)
9453 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009454 return ret;
9455}
9456
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009457int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009458{
9459 struct btrfs_root *root;
9460 struct list_head splice;
9461 int ret;
9462
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009463 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009464 return -EROFS;
9465
9466 INIT_LIST_HEAD(&splice);
9467
Miao Xie573bfb72014-03-06 13:55:03 +08009468 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009469 spin_lock(&fs_info->delalloc_root_lock);
9470 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009471 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009472 root = list_first_entry(&splice, struct btrfs_root,
9473 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009474 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009475 BUG_ON(!root);
9476 list_move_tail(&root->delalloc_root,
9477 &fs_info->delalloc_roots);
9478 spin_unlock(&fs_info->delalloc_root_lock);
9479
Ethan Lien3cd24c62018-11-01 14:49:03 +08009480 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009481 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009482 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009483 goto out;
9484
Miao Xie6c255e62014-03-06 13:55:01 +08009485 if (nr != -1) {
9486 nr -= ret;
9487 WARN_ON(nr < 0);
9488 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009489 spin_lock(&fs_info->delalloc_root_lock);
9490 }
9491 spin_unlock(&fs_info->delalloc_root_lock);
9492
Miao Xie6c255e62014-03-06 13:55:01 +08009493 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009494out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009495 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009496 spin_lock(&fs_info->delalloc_root_lock);
9497 list_splice_tail(&splice, &fs_info->delalloc_roots);
9498 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009499 }
Miao Xie573bfb72014-03-06 13:55:03 +08009500 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009501 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009502}
9503
Chris Mason39279cc2007-06-12 06:35:45 -04009504static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9505 const char *symname)
9506{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009507 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009508 struct btrfs_trans_handle *trans;
9509 struct btrfs_root *root = BTRFS_I(dir)->root;
9510 struct btrfs_path *path;
9511 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009512 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009513 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009514 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309515 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009516 int name_len;
9517 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009518 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009519 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009520 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009521
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009522 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009523 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009524 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009525
Josef Bacik9ed74f22009-09-11 16:12:44 -04009526 /*
9527 * 2 items for inode item and ref
9528 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009529 * 1 item for updating parent inode item
9530 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009531 * 1 item for xattr if selinux is on
9532 */
Filipe Manana9269d122015-12-31 18:16:29 +00009533 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009534 if (IS_ERR(trans))
9535 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009536
Li Zefan581bb052011-04-20 10:06:11 +08009537 err = btrfs_find_free_ino(root, &objectid);
9538 if (err)
9539 goto out_unlock;
9540
Josef Bacikaec74772008-07-24 12:12:38 -04009541 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009542 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9543 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009544 if (IS_ERR(inode)) {
9545 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009546 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009547 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009548 }
Chris Mason39279cc2007-06-12 06:35:45 -04009549
Casey Schauflerad19db72011-12-15 10:09:07 -05009550 /*
9551 * If the active LSM wants to access the inode during
9552 * d_instantiate it needs these. Smack checks to see
9553 * if the filesystem supports xattrs by looking at the
9554 * ops vector.
9555 */
9556 inode->i_fop = &btrfs_file_operations;
9557 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009558 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009559 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9560
9561 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9562 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009563 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009564
Chris Mason39279cc2007-06-12 06:35:45 -04009565 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009566 if (!path) {
9567 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009568 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009569 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009570 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009571 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009572 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009573 datasize = btrfs_file_extent_calc_inline_size(name_len);
9574 err = btrfs_insert_empty_item(trans, root, path, &key,
9575 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009576 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009577 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009578 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009579 }
Chris Mason5f39d392007-10-15 16:14:19 -04009580 leaf = path->nodes[0];
9581 ei = btrfs_item_ptr(leaf, path->slots[0],
9582 struct btrfs_file_extent_item);
9583 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9584 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009585 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009586 btrfs_set_file_extent_encryption(leaf, ei, 0);
9587 btrfs_set_file_extent_compression(leaf, ei, 0);
9588 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9589 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9590
Chris Mason39279cc2007-06-12 06:35:45 -04009591 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009592 write_extent_buffer(leaf, symname, ptr, name_len);
9593 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009594 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009595
Chris Mason39279cc2007-06-12 06:35:45 -04009596 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009597 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009598 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009599 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009600 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009601 /*
9602 * Last step, add directory indexes for our symlink inode. This is the
9603 * last step to avoid extra cleanup of these indexes if an error happens
9604 * elsewhere above.
9605 */
9606 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009607 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9608 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009609 if (err)
9610 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009611
Al Viro1e2e5472018-05-04 08:23:01 -04009612 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009613
9614out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009615 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009616 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009617 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009618 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009619 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009620 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009621 return err;
9622}
Chris Mason16432982008-04-10 10:23:21 -04009623
Qu Wenruo203f44c52020-06-10 09:04:40 +08009624static int insert_prealloc_file_extent(struct btrfs_trans_handle *trans,
9625 struct inode *inode, struct btrfs_key *ins,
9626 u64 file_offset)
9627{
9628 struct btrfs_file_extent_item stack_fi;
9629 u64 start = ins->objectid;
9630 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009631 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009632
9633 memset(&stack_fi, 0, sizeof(stack_fi));
9634
9635 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9636 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9637 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9638 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9639 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9640 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9641 /* Encryption and other encoding is reserved and all 0 */
9642
Nikolay Borisov72b7d152020-06-03 08:55:18 +03009643 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009644 if (ret < 0)
9645 return ret;
Nikolay Borisovc553f942020-06-03 08:55:19 +03009646 return insert_reserved_file_extent(trans, BTRFS_I(inode), file_offset,
Qu Wenruo9729f102020-06-10 09:04:41 +08009647 &stack_fi, ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009648}
Josef Bacik0af3d002010-06-21 14:48:16 -04009649static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9650 u64 start, u64 num_bytes, u64 min_size,
9651 loff_t actual_len, u64 *alloc_hint,
9652 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009653{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009654 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009655 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9656 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009657 struct btrfs_root *root = BTRFS_I(inode)->root;
9658 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009659 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009660 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009661 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009662 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009663 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009664 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009665 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009666 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009667
Josef Bacik0af3d002010-06-21 14:48:16 -04009668 if (trans)
9669 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009670 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009671 if (own_trans) {
9672 trans = btrfs_start_transaction(root, 3);
9673 if (IS_ERR(trans)) {
9674 ret = PTR_ERR(trans);
9675 break;
9676 }
Yan Zhengd899e052008-10-30 14:25:28 -04009677 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009678
Byongho Leeee221842015-12-15 01:42:10 +09009679 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009680 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009681 /*
9682 * If we are severely fragmented we could end up with really
9683 * small allocations, so if the allocator is returning small
9684 * chunks lets make its job easier by only searching for those
9685 * sized chunks.
9686 */
9687 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009688 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9689 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009690 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009691 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009692 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009693 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009694 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009695
9696 /*
9697 * We've reserved this space, and thus converted it from
9698 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9699 * from here on out we will only need to clear our reservation
9700 * for the remaining unreserved area, so advance our
9701 * clear_offset by our extent size.
9702 */
9703 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009704 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009705
Josef Bacik0b670dc2015-09-23 17:11:16 -04009706 last_alloc = ins.offset;
Nikolay Borisovc553f942020-06-03 08:55:19 +03009707 ret = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009708 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009709 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009710 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009711 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009712 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009713 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009714 break;
9715 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009716
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009717 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009718 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009719
Josef Bacik5dc562c2012-08-17 13:14:17 -04009720 em = alloc_extent_map();
9721 if (!em) {
9722 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9723 &BTRFS_I(inode)->runtime_flags);
9724 goto next;
9725 }
9726
9727 em->start = cur_offset;
9728 em->orig_start = cur_offset;
9729 em->len = ins.offset;
9730 em->block_start = ins.objectid;
9731 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009732 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009733 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009734 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9735 em->generation = trans->transid;
9736
9737 while (1) {
9738 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009739 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009740 write_unlock(&em_tree->lock);
9741 if (ret != -EEXIST)
9742 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009743 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009744 cur_offset + ins.offset - 1,
9745 0);
9746 }
9747 free_extent_map(em);
9748next:
Yan Zhengd899e052008-10-30 14:25:28 -04009749 num_bytes -= ins.offset;
9750 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009751 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009752
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009753 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009754 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009755 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009756 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009757 (actual_len > inode->i_size) &&
9758 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009759 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009760 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009761 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009762 i_size = cur_offset;
9763 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009764 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009765 }
9766
Yan Zhengd899e052008-10-30 14:25:28 -04009767 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009768
9769 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009770 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009771 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009772 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009773 break;
9774 }
Yan Zhengd899e052008-10-30 14:25:28 -04009775
Josef Bacik0af3d002010-06-21 14:48:16 -04009776 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009777 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009778 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009779 if (clear_offset < end)
9780 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
9781 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009782 return ret;
9783}
9784
Josef Bacik0af3d002010-06-21 14:48:16 -04009785int btrfs_prealloc_file_range(struct inode *inode, int mode,
9786 u64 start, u64 num_bytes, u64 min_size,
9787 loff_t actual_len, u64 *alloc_hint)
9788{
9789 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9790 min_size, actual_len, alloc_hint,
9791 NULL);
9792}
9793
9794int btrfs_prealloc_file_range_trans(struct inode *inode,
9795 struct btrfs_trans_handle *trans, int mode,
9796 u64 start, u64 num_bytes, u64 min_size,
9797 loff_t actual_len, u64 *alloc_hint)
9798{
9799 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9800 min_size, actual_len, alloc_hint, trans);
9801}
9802
Chris Masone6dcd2d2008-07-17 12:53:50 -04009803static int btrfs_set_page_dirty(struct page *page)
9804{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009805 return __set_page_dirty_nobuffers(page);
9806}
9807
Al Viro10556cb22011-06-20 19:28:19 -04009808static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009809{
Li Zefanb83cc962010-12-20 16:04:08 +08009810 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009811 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009812
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009813 if (mask & MAY_WRITE &&
9814 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9815 if (btrfs_root_readonly(root))
9816 return -EROFS;
9817 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9818 return -EACCES;
9819 }
Al Viro2830ba72011-06-20 19:16:29 -04009820 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009821}
Chris Mason39279cc2007-06-12 06:35:45 -04009822
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009823static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9824{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009825 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009826 struct btrfs_trans_handle *trans;
9827 struct btrfs_root *root = BTRFS_I(dir)->root;
9828 struct inode *inode = NULL;
9829 u64 objectid;
9830 u64 index;
9831 int ret = 0;
9832
9833 /*
9834 * 5 units required for adding orphan entry
9835 */
9836 trans = btrfs_start_transaction(root, 5);
9837 if (IS_ERR(trans))
9838 return PTR_ERR(trans);
9839
9840 ret = btrfs_find_free_ino(root, &objectid);
9841 if (ret)
9842 goto out;
9843
9844 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009845 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009846 if (IS_ERR(inode)) {
9847 ret = PTR_ERR(inode);
9848 inode = NULL;
9849 goto out;
9850 }
9851
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009852 inode->i_fop = &btrfs_file_operations;
9853 inode->i_op = &btrfs_file_inode_operations;
9854
9855 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009856 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9857
Chris Masonb0d5d102014-09-08 13:08:51 -07009858 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9859 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009860 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009861
9862 ret = btrfs_update_inode(trans, root, inode);
9863 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009864 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009865 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009866 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009867 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009868
Filipe Manana5762b5c2014-08-01 00:10:32 +01009869 /*
9870 * We set number of links to 0 in btrfs_new_inode(), and here we set
9871 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9872 * through:
9873 *
9874 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9875 */
9876 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009877 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009878 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009879 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009880out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009881 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009882 if (ret && inode)
9883 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009884 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009885 return ret;
9886}
9887
David Sterba5cdc84b2018-07-18 20:32:52 +02009888void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009889{
David Sterba5cdc84b2018-07-18 20:32:52 +02009890 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009891 unsigned long index = start >> PAGE_SHIFT;
9892 unsigned long end_index = end >> PAGE_SHIFT;
9893 struct page *page;
9894
9895 while (index <= end_index) {
9896 page = find_get_page(inode->i_mapping, index);
9897 ASSERT(page); /* Pages should be in the extent_io_tree */
9898 set_page_writeback(page);
9899 put_page(page);
9900 index++;
9901 }
9902}
9903
Omar Sandovaled46ff32016-11-03 10:28:14 -07009904#ifdef CONFIG_SWAP
9905/*
9906 * Add an entry indicating a block group or device which is pinned by a
9907 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9908 * negative errno on failure.
9909 */
9910static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9911 bool is_block_group)
9912{
9913 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9914 struct btrfs_swapfile_pin *sp, *entry;
9915 struct rb_node **p;
9916 struct rb_node *parent = NULL;
9917
9918 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9919 if (!sp)
9920 return -ENOMEM;
9921 sp->ptr = ptr;
9922 sp->inode = inode;
9923 sp->is_block_group = is_block_group;
9924
9925 spin_lock(&fs_info->swapfile_pins_lock);
9926 p = &fs_info->swapfile_pins.rb_node;
9927 while (*p) {
9928 parent = *p;
9929 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9930 if (sp->ptr < entry->ptr ||
9931 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9932 p = &(*p)->rb_left;
9933 } else if (sp->ptr > entry->ptr ||
9934 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9935 p = &(*p)->rb_right;
9936 } else {
9937 spin_unlock(&fs_info->swapfile_pins_lock);
9938 kfree(sp);
9939 return 1;
9940 }
9941 }
9942 rb_link_node(&sp->node, parent, p);
9943 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9944 spin_unlock(&fs_info->swapfile_pins_lock);
9945 return 0;
9946}
9947
9948/* Free all of the entries pinned by this swapfile. */
9949static void btrfs_free_swapfile_pins(struct inode *inode)
9950{
9951 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9952 struct btrfs_swapfile_pin *sp;
9953 struct rb_node *node, *next;
9954
9955 spin_lock(&fs_info->swapfile_pins_lock);
9956 node = rb_first(&fs_info->swapfile_pins);
9957 while (node) {
9958 next = rb_next(node);
9959 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9960 if (sp->inode == inode) {
9961 rb_erase(&sp->node, &fs_info->swapfile_pins);
9962 if (sp->is_block_group)
9963 btrfs_put_block_group(sp->ptr);
9964 kfree(sp);
9965 }
9966 node = next;
9967 }
9968 spin_unlock(&fs_info->swapfile_pins_lock);
9969}
9970
9971struct btrfs_swap_info {
9972 u64 start;
9973 u64 block_start;
9974 u64 block_len;
9975 u64 lowest_ppage;
9976 u64 highest_ppage;
9977 unsigned long nr_pages;
9978 int nr_extents;
9979};
9980
9981static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9982 struct btrfs_swap_info *bsi)
9983{
9984 unsigned long nr_pages;
9985 u64 first_ppage, first_ppage_reported, next_ppage;
9986 int ret;
9987
9988 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9989 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9990 PAGE_SIZE) >> PAGE_SHIFT;
9991
9992 if (first_ppage >= next_ppage)
9993 return 0;
9994 nr_pages = next_ppage - first_ppage;
9995
9996 first_ppage_reported = first_ppage;
9997 if (bsi->start == 0)
9998 first_ppage_reported++;
9999 if (bsi->lowest_ppage > first_ppage_reported)
10000 bsi->lowest_ppage = first_ppage_reported;
10001 if (bsi->highest_ppage < (next_ppage - 1))
10002 bsi->highest_ppage = next_ppage - 1;
10003
10004 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10005 if (ret < 0)
10006 return ret;
10007 bsi->nr_extents += ret;
10008 bsi->nr_pages += nr_pages;
10009 return 0;
10010}
10011
10012static void btrfs_swap_deactivate(struct file *file)
10013{
10014 struct inode *inode = file_inode(file);
10015
10016 btrfs_free_swapfile_pins(inode);
10017 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10018}
10019
10020static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10021 sector_t *span)
10022{
10023 struct inode *inode = file_inode(file);
10024 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10025 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10026 struct extent_state *cached_state = NULL;
10027 struct extent_map *em = NULL;
10028 struct btrfs_device *device = NULL;
10029 struct btrfs_swap_info bsi = {
10030 .lowest_ppage = (sector_t)-1ULL,
10031 };
10032 int ret = 0;
10033 u64 isize;
10034 u64 start;
10035
10036 /*
10037 * If the swap file was just created, make sure delalloc is done. If the
10038 * file changes again after this, the user is doing something stupid and
10039 * we don't really care.
10040 */
10041 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10042 if (ret)
10043 return ret;
10044
10045 /*
10046 * The inode is locked, so these flags won't change after we check them.
10047 */
10048 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10049 btrfs_warn(fs_info, "swapfile must not be compressed");
10050 return -EINVAL;
10051 }
10052 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10053 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10054 return -EINVAL;
10055 }
10056 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10057 btrfs_warn(fs_info, "swapfile must not be checksummed");
10058 return -EINVAL;
10059 }
10060
10061 /*
10062 * Balance or device remove/replace/resize can move stuff around from
10063 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10064 * concurrently while we are mapping the swap extents, and
10065 * fs_info->swapfile_pins prevents them from running while the swap file
10066 * is active and moving the extents. Note that this also prevents a
10067 * concurrent device add which isn't actually necessary, but it's not
10068 * really worth the trouble to allow it.
10069 */
10070 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10071 btrfs_warn(fs_info,
10072 "cannot activate swapfile while exclusive operation is running");
10073 return -EBUSY;
10074 }
10075 /*
10076 * Snapshots can create extents which require COW even if NODATACOW is
10077 * set. We use this counter to prevent snapshots. We must increment it
10078 * before walking the extents because we don't want a concurrent
10079 * snapshot to run after we've already checked the extents.
10080 */
10081 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10082
10083 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10084
10085 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10086 start = 0;
10087 while (start < isize) {
10088 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010089 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010090 u64 len = isize - start;
10091
Omar Sandoval39b07b52019-12-02 17:34:23 -080010092 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010093 if (IS_ERR(em)) {
10094 ret = PTR_ERR(em);
10095 goto out;
10096 }
10097
10098 if (em->block_start == EXTENT_MAP_HOLE) {
10099 btrfs_warn(fs_info, "swapfile must not have holes");
10100 ret = -EINVAL;
10101 goto out;
10102 }
10103 if (em->block_start == EXTENT_MAP_INLINE) {
10104 /*
10105 * It's unlikely we'll ever actually find ourselves
10106 * here, as a file small enough to fit inline won't be
10107 * big enough to store more than the swap header, but in
10108 * case something changes in the future, let's catch it
10109 * here rather than later.
10110 */
10111 btrfs_warn(fs_info, "swapfile must not be inline");
10112 ret = -EINVAL;
10113 goto out;
10114 }
10115 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10116 btrfs_warn(fs_info, "swapfile must not be compressed");
10117 ret = -EINVAL;
10118 goto out;
10119 }
10120
10121 logical_block_start = em->block_start + (start - em->start);
10122 len = min(len, em->len - (start - em->start));
10123 free_extent_map(em);
10124 em = NULL;
10125
10126 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10127 if (ret < 0) {
10128 goto out;
10129 } else if (ret) {
10130 ret = 0;
10131 } else {
10132 btrfs_warn(fs_info,
10133 "swapfile must not be copy-on-write");
10134 ret = -EINVAL;
10135 goto out;
10136 }
10137
10138 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10139 if (IS_ERR(em)) {
10140 ret = PTR_ERR(em);
10141 goto out;
10142 }
10143
10144 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10145 btrfs_warn(fs_info,
10146 "swapfile must have single data profile");
10147 ret = -EINVAL;
10148 goto out;
10149 }
10150
10151 if (device == NULL) {
10152 device = em->map_lookup->stripes[0].dev;
10153 ret = btrfs_add_swapfile_pin(inode, device, false);
10154 if (ret == 1)
10155 ret = 0;
10156 else if (ret)
10157 goto out;
10158 } else if (device != em->map_lookup->stripes[0].dev) {
10159 btrfs_warn(fs_info, "swapfile must be on one device");
10160 ret = -EINVAL;
10161 goto out;
10162 }
10163
10164 physical_block_start = (em->map_lookup->stripes[0].physical +
10165 (logical_block_start - em->start));
10166 len = min(len, em->len - (logical_block_start - em->start));
10167 free_extent_map(em);
10168 em = NULL;
10169
10170 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10171 if (!bg) {
10172 btrfs_warn(fs_info,
10173 "could not find block group containing swapfile");
10174 ret = -EINVAL;
10175 goto out;
10176 }
10177
10178 ret = btrfs_add_swapfile_pin(inode, bg, true);
10179 if (ret) {
10180 btrfs_put_block_group(bg);
10181 if (ret == 1)
10182 ret = 0;
10183 else
10184 goto out;
10185 }
10186
10187 if (bsi.block_len &&
10188 bsi.block_start + bsi.block_len == physical_block_start) {
10189 bsi.block_len += len;
10190 } else {
10191 if (bsi.block_len) {
10192 ret = btrfs_add_swap_extent(sis, &bsi);
10193 if (ret)
10194 goto out;
10195 }
10196 bsi.start = start;
10197 bsi.block_start = physical_block_start;
10198 bsi.block_len = len;
10199 }
10200
10201 start += len;
10202 }
10203
10204 if (bsi.block_len)
10205 ret = btrfs_add_swap_extent(sis, &bsi);
10206
10207out:
10208 if (!IS_ERR_OR_NULL(em))
10209 free_extent_map(em);
10210
10211 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10212
10213 if (ret)
10214 btrfs_swap_deactivate(file);
10215
10216 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10217
10218 if (ret)
10219 return ret;
10220
10221 if (device)
10222 sis->bdev = device->bdev;
10223 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10224 sis->max = bsi.nr_pages;
10225 sis->pages = bsi.nr_pages - 1;
10226 sis->highest_bit = bsi.nr_pages - 1;
10227 return bsi.nr_extents;
10228}
10229#else
10230static void btrfs_swap_deactivate(struct file *file)
10231{
10232}
10233
10234static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10235 sector_t *span)
10236{
10237 return -EOPNOTSUPP;
10238}
10239#endif
10240
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010241static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010242 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010243 .lookup = btrfs_lookup,
10244 .create = btrfs_create,
10245 .unlink = btrfs_unlink,
10246 .link = btrfs_link,
10247 .mkdir = btrfs_mkdir,
10248 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010249 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010250 .symlink = btrfs_symlink,
10251 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010252 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010253 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010254 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010255 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010256 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010257 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010258 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010259};
Yan, Zheng76dda932009-09-21 16:00:26 -040010260
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010261static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010262 .llseek = generic_file_llseek,
10263 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010264 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010265 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010266 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010267#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010268 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010269#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010270 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010271 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010272};
10273
David Sterba20e55062015-11-19 11:42:28 +010010274static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010275 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010276 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010277 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10278};
10279
Chris Mason35054392009-01-21 13:11:13 -050010280/*
10281 * btrfs doesn't support the bmap operation because swapfiles
10282 * use bmap to make a mapping of extents in the file. They assume
10283 * these extents won't change over the life of the file and they
10284 * use the bmap result to do IO directly to the drive.
10285 *
10286 * the btrfs bmap call would return logical addresses that aren't
10287 * suitable for IO and they also will change frequently as COW
10288 * operations happen. So, swapfile + btrfs == corruption.
10289 *
10290 * For now we're avoiding this by dropping bmap.
10291 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010292static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010293 .readpage = btrfs_readpage,
10294 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010295 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010296 .readahead = btrfs_readahead,
David Sterba55e20bd2020-06-09 19:56:06 +020010297 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010298 .invalidatepage = btrfs_invalidatepage,
10299 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010300#ifdef CONFIG_MIGRATION
10301 .migratepage = btrfs_migratepage,
10302#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010303 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010304 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010305 .swap_activate = btrfs_swap_activate,
10306 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010307};
10308
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010309static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010310 .getattr = btrfs_getattr,
10311 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010312 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010313 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010314 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010315 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010316 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010317 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010318};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010319static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010320 .getattr = btrfs_getattr,
10321 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010322 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010323 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010324 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010325 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010326 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010327};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010328static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010329 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010330 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010331 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010332 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010333 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010334 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010335};
Yan, Zheng76dda932009-09-21 16:00:26 -040010336
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010337const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010338 .d_delete = btrfs_dentry_delete,
10339};