blob: f700e389793740ff70102b07d2575690a1911602 [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 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300427static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
428 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800429{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300430 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800431
Nikolay Borisov808a1292020-06-03 08:55:27 +0300432 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000433 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
434 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300435 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000436 return 0;
437 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800438 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400439 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800440 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200441 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300442 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200443 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800444 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300445 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800446 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400447 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300448 inode->flags & BTRFS_INODE_COMPRESS ||
449 inode->prop_compress)
450 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800451 return 0;
452}
453
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200454static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800455 u64 start, u64 end, u64 num_bytes, u64 small_write)
456{
457 /* If this is a small write inside eof, kick off a defrag */
458 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200459 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800460 btrfs_add_inode_defrag(NULL, inode);
461}
462
Chris Masonc8b97812008-10-29 14:49:59 -0400463/*
Chris Mason771ed682008-11-06 22:02:51 -0500464 * we create compressed extents in two phases. The first
465 * phase compresses a range of pages that have already been
466 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400467 *
Chris Mason771ed682008-11-06 22:02:51 -0500468 * This is done inside an ordered work queue, and the compression
469 * is spread across many cpus. The actual IO submission is step
470 * two, and the ordered work queue takes care of making sure that
471 * happens in the same order things were put onto the queue by
472 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400473 *
Chris Mason771ed682008-11-06 22:02:51 -0500474 * If this code finds it can't get good compression, it puts an
475 * entry onto the work queue to write the uncompressed bytes. This
476 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300477 * are written in the same order that the flusher thread sent them
478 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400479 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300480static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400481{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200482 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400483 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400484 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200485 u64 start = async_chunk->start;
486 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400487 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400488 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400489 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400490 struct page **pages = NULL;
491 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400492 unsigned long total_compressed = 0;
493 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400494 int i;
495 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400496 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300497 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400498 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400499
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200500 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
501 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400502
Josef Bacikd98da492019-10-11 09:03:54 -0400503 /*
504 * We need to save i_size before now because it could change in between
505 * us evaluating the size and assigning it. This is because we lock and
506 * unlock the page in truncate and fallocate, and then modify the i_size
507 * later on.
508 *
509 * The barriers are to emulate READ_ONCE, remove that once i_size_read
510 * does that for us.
511 */
512 barrier();
513 i_size = i_size_read(inode);
514 barrier();
515 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400516again:
517 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300518 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100519 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
520 nr_pages = min_t(unsigned long, nr_pages,
521 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400522
Chris Masonf03d9301f2009-02-04 09:31:06 -0500523 /*
524 * we don't want to send crud past the end of i_size through
525 * compression, that's just a waste of CPU time. So, if the
526 * end of the file is before the start of our current
527 * requested range of bytes, we bail out to the uncompressed
528 * cleanup code that can deal with all of this.
529 *
530 * It isn't really the fastest way to fix things, but this is a
531 * very uncommon corner.
532 */
533 if (actual_end <= start)
534 goto cleanup_and_bail_uncompressed;
535
Chris Masonc8b97812008-10-29 14:49:59 -0400536 total_compressed = actual_end - start;
537
Shilong Wang4bcbb332014-10-07 18:44:35 -0400538 /*
539 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400540 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400541 */
542 if (total_compressed <= blocksize &&
543 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
544 goto cleanup_and_bail_uncompressed;
545
David Sterba069eac72017-02-14 19:36:54 +0100546 total_compressed = min_t(unsigned long, total_compressed,
547 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400548 total_in = 0;
549 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400550
Chris Mason771ed682008-11-06 22:02:51 -0500551 /*
552 * we do compression for mount -o compress and when the
553 * inode has not been flagged as nocompress. This flag can
554 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400555 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300556 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400557 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100558 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800559 if (!pages) {
560 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100561 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800562 goto cont;
563 }
Chris Mason179e29e2007-11-01 11:28:41 -0400564
David Sterbaeec63c62017-07-17 19:41:31 +0200565 if (BTRFS_I(inode)->defrag_compress)
566 compress_type = BTRFS_I(inode)->defrag_compress;
567 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200568 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800569
Chris Mason4adaa612013-03-26 13:07:00 -0400570 /*
571 * we need to call clear_page_dirty_for_io on each
572 * page in the range. Otherwise applications with the file
573 * mmap'd can wander in and change the page contents while
574 * we are compressing them.
575 *
576 * If the compression fails for any reason, we set the pages
577 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300578 *
579 * Note that the remaining part is redirtied, the start pointer
580 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400581 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300582 if (!redirty) {
583 extent_range_clear_dirty_for_io(inode, start, end);
584 redirty = 1;
585 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200586
587 /* Compression level is applied here and only here */
588 ret = btrfs_compress_pages(
589 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800590 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100591 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100592 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800593 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100594 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400595
596 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100597 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100598 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400599 char *kaddr;
600
601 /* zero the tail end of the last page, we might be
602 * sending it down to disk
603 */
604 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800605 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400606 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300607 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800608 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400609 }
610 will_compress = 1;
611 }
612 }
Li Zefan560f7d72011-09-08 10:22:01 +0800613cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400614 if (start == 0) {
615 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300616 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400617 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500618 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400619 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300620 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
621 0, BTRFS_COMPRESS_NONE,
622 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400623 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500624 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300625 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000626 total_compressed,
627 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400628 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100629 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400630 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400631 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
632 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100633 unsigned long page_error_op;
634
Filipe Mananae6eb4312014-10-10 10:45:12 +0100635 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400636
Chris Mason771ed682008-11-06 22:02:51 -0500637 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100638 * inline extent creation worked or returned error,
639 * we don't need to create any more async work items.
640 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400641 *
642 * We use DO_ACCOUNTING here because we need the
643 * delalloc_release_metadata to be done _after_ we drop
644 * our outstanding extent for clearing delalloc for this
645 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500646 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300647 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
648 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300649 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800650 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400651 PAGE_CLEAR_DIRTY |
652 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100653 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400654 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300655
656 for (i = 0; i < nr_pages; i++) {
657 WARN_ON(pages[i]->mapping);
658 put_page(pages[i]);
659 }
660 kfree(pages);
661
662 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400663 }
664 }
665
666 if (will_compress) {
667 /*
668 * we aren't doing an inline extent round the compressed size
669 * up to a block size boundary so the allocator does sane
670 * things
671 */
Qu Wenruofda28322013-02-26 08:10:22 +0000672 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400673
674 /*
675 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300676 * win, compare the page count read with the blocks on disk,
677 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400678 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300679 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300680 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300681 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700682
683 /*
684 * The async work queues will take care of doing actual
685 * allocation on disk for these compressed pages, and
686 * will submit them to the elevator.
687 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200688 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100689 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700690 compress_type);
691
Timofey Titovets11708622017-10-03 18:06:01 +0300692 if (start + total_in < end) {
693 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700694 pages = NULL;
695 cond_resched();
696 goto again;
697 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300698 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400699 }
700 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700701 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400702 /*
703 * the compression code ran but failed to make things smaller,
704 * free any pages it allocated and our page pointer array
705 */
David Sterba4d3a8002017-02-14 19:04:07 +0100706 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400707 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300708 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400709 }
710 kfree(pages);
711 pages = NULL;
712 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100713 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400714
715 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400716 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200717 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500718 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500719 }
Chris Masonc8b97812008-10-29 14:49:59 -0400720 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500721cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700722 /*
723 * No compression, but we still need to write the pages in the file
724 * we've been given so far. redirty the locked page if it corresponds
725 * to our extent and set things up for the async work queue to run
726 * cow_file_range to do the normal delalloc dance.
727 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700728 if (async_chunk->locked_page &&
729 (page_offset(async_chunk->locked_page) >= start &&
730 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200731 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700732 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700733 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700734
735 if (redirty)
736 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200737 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700738 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300739 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500740
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300741 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500742}
Chris Mason771ed682008-11-06 22:02:51 -0500743
Filipe Manana40ae8372014-10-06 22:14:24 +0100744static void free_async_extent_pages(struct async_extent *async_extent)
745{
746 int i;
747
748 if (!async_extent->pages)
749 return;
750
751 for (i = 0; i < async_extent->nr_pages; i++) {
752 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300753 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100754 }
755 kfree(async_extent->pages);
756 async_extent->nr_pages = 0;
757 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500758}
759
760/*
761 * phase two of compressed writeback. This is the ordered portion
762 * of the code, which only gets called in the order the work was
763 * queued. We walk all the async extents created by compress_file_range
764 * and send them down to the disk.
765 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200766static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500767{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300768 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
769 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500770 struct async_extent *async_extent;
771 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500772 struct btrfs_key ins;
773 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300774 struct btrfs_root *root = inode->root;
775 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500776 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500777
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200779 while (!list_empty(&async_chunk->extents)) {
780 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500781 struct async_extent, list);
782 list_del(&async_extent->list);
783
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500784retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200785 lock_extent(io_tree, async_extent->start,
786 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500787 /* did the compression code fall back to uncompressed IO? */
788 if (!async_extent->pages) {
789 int page_started = 0;
790 unsigned long nr_written = 0;
791
Chris Mason771ed682008-11-06 22:02:51 -0500792 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300793 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500794 async_extent->start,
795 async_extent->start +
796 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300797 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500798
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100799 /* JDM XXX */
800
Chris Mason771ed682008-11-06 22:02:51 -0500801 /*
802 * if page_started, cow_file_range inserted an
803 * inline extent and took care of all the unlocking
804 * and IO for us. Otherwise, we need to submit
805 * all those pages down to the drive.
806 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500807 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300808 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200809 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500810 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500811 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500812 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700813 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200814 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500815 kfree(async_extent);
816 cond_resched();
817 continue;
818 }
819
Wang Xiaoguang18513092016-07-25 15:51:40 +0800820 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500821 async_extent->compressed_size,
822 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800823 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500824 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100825 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400827 if (ret == -ENOSPC) {
828 unlock_extent(io_tree, async_extent->start,
829 async_extent->start +
830 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800831
832 /*
833 * we need to redirty the pages if we decide to
834 * fallback to uncompressed IO, otherwise we
835 * will not submit these pages down to lower
836 * layers.
837 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300838 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800839 async_extent->start,
840 async_extent->start +
841 async_extent->ram_size - 1);
842
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100843 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400844 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500845 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500846 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000847 /*
848 * here we're doing allocation and writeback of the
849 * compressed pages
850 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300851 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800852 async_extent->ram_size, /* len */
853 async_extent->start, /* orig_start */
854 ins.objectid, /* block_start */
855 ins.offset, /* block_len */
856 ins.offset, /* orig_block_len */
857 async_extent->ram_size, /* ram_bytes */
858 async_extent->compress_type,
859 BTRFS_ORDERED_COMPRESSED);
860 if (IS_ERR(em))
861 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500862 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800863 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500864
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300865 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800866 async_extent->start,
867 ins.objectid,
868 async_extent->ram_size,
869 ins.offset,
870 BTRFS_ORDERED_COMPRESSED,
871 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100872 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300873 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100874 async_extent->start +
875 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500876 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100877 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400878 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500879
Chris Mason771ed682008-11-06 22:02:51 -0500880 /*
881 * clear dirty, set writeback and unlock the pages.
882 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300883 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400884 async_extent->start +
885 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400886 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
887 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400888 PAGE_SET_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300889 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500890 async_extent->ram_size,
891 ins.objectid,
892 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600893 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700894 async_chunk->write_flags,
895 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100896 struct page *p = async_extent->pages[0];
897 const u64 start = async_extent->start;
898 const u64 end = start + async_extent->ram_size - 1;
899
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300900 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200901 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200902
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100903 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300904 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100905 PAGE_END_WRITEBACK |
906 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100907 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100908 }
Chris Mason771ed682008-11-06 22:02:51 -0500909 alloc_hint = ins.objectid + ins.offset;
910 kfree(async_extent);
911 cond_resched();
912 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100913 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500914out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400915 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400916 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100917out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300918 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500919 async_extent->start +
920 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400921 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100922 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400923 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
924 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100925 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
926 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100927 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100928 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500929 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500930}
931
Nikolay Borisov43c69842020-06-03 08:55:02 +0300932static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400933 u64 num_bytes)
934{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300935 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400936 struct extent_map *em;
937 u64 alloc_hint = 0;
938
939 read_lock(&em_tree->lock);
940 em = search_extent_mapping(em_tree, start, num_bytes);
941 if (em) {
942 /*
943 * if block start isn't an actual block number then find the
944 * first block in this inode and use that as a hint. If that
945 * block is also bogus then just don't worry about it.
946 */
947 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
948 free_extent_map(em);
949 em = search_extent_mapping(em_tree, 0, 0);
950 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
951 alloc_hint = em->block_start;
952 if (em)
953 free_extent_map(em);
954 } else {
955 alloc_hint = em->block_start;
956 free_extent_map(em);
957 }
958 }
959 read_unlock(&em_tree->lock);
960
961 return alloc_hint;
962}
963
Chris Mason771ed682008-11-06 22:02:51 -0500964/*
965 * when extent_io.c finds a delayed allocation range in the file,
966 * the call backs end up in this code. The basic idea is to
967 * allocate extents on disk for the range, and create ordered data structs
968 * in ram to track those extents.
969 *
970 * locked_page is the page that writepage had locked already. We use
971 * it to make sure we don't do extra locks or unlocks.
972 *
973 * *page_started is set to one if we unlock locked_page and do everything
974 * required to start IO on it. It may be clean and already done with
975 * IO when we return.
976 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300977static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -0400978 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300979 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300980 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500981{
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300982 struct btrfs_root *root = inode->root;
983 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500984 u64 alloc_hint = 0;
985 u64 num_bytes;
986 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000987 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +0100988 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400989 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500990 struct btrfs_key ins;
991 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000992 unsigned clear_bits;
993 unsigned long page_ops;
994 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500995 int ret = 0;
996
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300997 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400998 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500999 ret = -EINVAL;
1000 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001001 }
Chris Mason771ed682008-11-06 22:02:51 -05001002
Qu Wenruofda28322013-02-26 08:10:22 +00001003 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001004 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001005 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001006
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001007 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001008
Chris Mason771ed682008-11-06 22:02:51 -05001009 if (start == 0) {
1010 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001011 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001012 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001013 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001014 /*
1015 * We use DO_ACCOUNTING here because we need the
1016 * delalloc_release_metadata to be run _after_ we drop
1017 * our outstanding extent for clearing delalloc for this
1018 * range.
1019 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001020 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001021 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001022 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1023 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001024 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1025 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001026 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001027 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001028 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001029 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001030 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001032 }
1033 }
Chris Masonc8b97812008-10-29 14:49:59 -04001034
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001035 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1036 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001037
Filipe Manana432cd2a2020-06-08 13:32:55 +01001038 /*
1039 * Relocation relies on the relocated extents to have exactly the same
1040 * size as the original extents. Normally writeback for relocation data
1041 * extents follows a NOCOW path because relocation preallocates the
1042 * extents. However, due to an operation such as scrub turning a block
1043 * group to RO mode, it may fallback to COW mode, so we must make sure
1044 * an extent allocated during COW has exactly the requested size and can
1045 * not be split into smaller extents, otherwise relocation breaks and
1046 * fails during the stage where it updates the bytenr of file extent
1047 * items.
1048 */
1049 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1050 min_alloc_size = num_bytes;
1051 else
1052 min_alloc_size = fs_info->sectorsize;
1053
Anand Jain3752d222018-02-15 12:29:38 +08001054 while (num_bytes > 0) {
1055 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001056 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001057 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001058 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001059 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001060 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001061 cur_alloc_size = ins.offset;
1062 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001063
Chris Mason771ed682008-11-06 22:02:51 -05001064 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001065 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001066 start, /* orig_start */
1067 ins.objectid, /* block_start */
1068 ins.offset, /* block_len */
1069 ins.offset, /* orig_block_len */
1070 ram_size, /* ram_bytes */
1071 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001072 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001073 if (IS_ERR(em)) {
1074 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001075 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001076 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001077 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001078
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001079 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1080 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001081 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001082 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001083
Yan Zheng17d217f2008-12-12 10:03:38 -05001084 if (root->root_key.objectid ==
1085 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001086 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001087 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001088 /*
1089 * Only drop cache here, and process as normal.
1090 *
1091 * We must not allow extent_clear_unlock_delalloc()
1092 * at out_unlock label to free meta of this ordered
1093 * extent, as its meta should be freed by
1094 * btrfs_finish_ordered_io().
1095 *
1096 * So we must continue until @start is increased to
1097 * skip current ordered extent.
1098 */
Josef Bacik00361582013-08-14 14:02:47 -04001099 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001100 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001101 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001102 }
1103
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001104 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001105
Chris Masonc8b97812008-10-29 14:49:59 -04001106 /* we're not doing compressed IO, don't unlock the first
1107 * page (which the caller expects to stay locked), don't
1108 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001109 *
1110 * Do set the Private2 bit so we know this page was properly
1111 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001112 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001113 page_ops = unlock ? PAGE_UNLOCK : 0;
1114 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001115
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001116 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001117 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001118 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001119 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001120 if (num_bytes < cur_alloc_size)
1121 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001122 else
Anand Jain3752d222018-02-15 12:29:38 +08001123 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001124 alloc_hint = ins.objectid + ins.offset;
1125 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001126 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001127
1128 /*
1129 * btrfs_reloc_clone_csums() error, since start is increased
1130 * extent_clear_unlock_delalloc() at out_unlock label won't
1131 * free metadata of current ordered extent, we're OK to exit.
1132 */
1133 if (ret)
1134 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001135 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001136out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001137 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001138
Filipe Mananad9f85962014-08-25 10:43:00 +01001139out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001140 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001141out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001142 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001143 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001144out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001145 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1146 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001147 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1148 PAGE_END_WRITEBACK;
1149 /*
1150 * If we reserved an extent for our delalloc range (or a subrange) and
1151 * failed to create the respective ordered extent, then it means that
1152 * when we reserved the extent we decremented the extent's size from
1153 * the data space_info's bytes_may_use counter and incremented the
1154 * space_info's bytes_reserved counter by the same amount. We must make
1155 * sure extent_clear_unlock_delalloc() does not try to decrement again
1156 * the data space_info's bytes_may_use counter, therefore we do not pass
1157 * it the flag EXTENT_CLEAR_DATA_RESV.
1158 */
1159 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001160 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001161 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001162 locked_page,
1163 clear_bits,
1164 page_ops);
1165 start += cur_alloc_size;
1166 if (start >= end)
1167 goto out;
1168 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001169 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001170 clear_bits | EXTENT_CLEAR_DATA_RESV,
1171 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001172 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001173}
Chris Masonc8b97812008-10-29 14:49:59 -04001174
Chris Mason771ed682008-11-06 22:02:51 -05001175/*
1176 * work queue call back to started compression on a file and pages
1177 */
1178static noinline void async_cow_start(struct btrfs_work *work)
1179{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001180 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001181 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001182
Nikolay Borisovb5326272019-03-12 17:20:25 +02001183 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001184
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001185 compressed_extents = compress_file_range(async_chunk);
1186 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001187 btrfs_add_delayed_iput(async_chunk->inode);
1188 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001189 }
Chris Mason771ed682008-11-06 22:02:51 -05001190}
1191
1192/*
1193 * work queue call back to submit previously compressed pages
1194 */
1195static noinline void async_cow_submit(struct btrfs_work *work)
1196{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001197 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1198 work);
1199 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001200 unsigned long nr_pages;
1201
Nikolay Borisovb5326272019-03-12 17:20:25 +02001202 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001203 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001204
David Sterba093258e2018-02-26 16:15:17 +01001205 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001206 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001207 5 * SZ_1M)
1208 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001209
Nikolay Borisov4546d172019-01-03 10:50:03 +02001210 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001211 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001212 * in which case we don't have anything to submit, yet we need to
1213 * always adjust ->async_delalloc_pages as its paired with the init
1214 * happening in cow_file_range_async
1215 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001216 if (async_chunk->inode)
1217 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001218}
Chris Masonc8b97812008-10-29 14:49:59 -04001219
Chris Mason771ed682008-11-06 22:02:51 -05001220static noinline void async_cow_free(struct btrfs_work *work)
1221{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001222 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001223
Nikolay Borisovb5326272019-03-12 17:20:25 +02001224 async_chunk = container_of(work, struct async_chunk, work);
1225 if (async_chunk->inode)
1226 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001227 if (async_chunk->blkcg_css)
1228 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001229 /*
1230 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001231 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001232 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001233 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001234 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001235}
1236
Nikolay Borisov751b6432020-06-03 08:55:22 +03001237static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001238 struct writeback_control *wbc,
1239 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001240 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001241 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001242{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001243 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001244 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001245 struct async_cow *ctx;
1246 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001247 unsigned long nr_pages;
1248 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001249 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1250 int i;
1251 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001252 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001253 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001254
Nikolay Borisov751b6432020-06-03 08:55:22 +03001255 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001256
Nikolay Borisov751b6432020-06-03 08:55:22 +03001257 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001258 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1259 num_chunks = 1;
1260 should_compress = false;
1261 } else {
1262 should_compress = true;
1263 }
1264
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001265 nofs_flag = memalloc_nofs_save();
1266 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1267 memalloc_nofs_restore(nofs_flag);
1268
Nikolay Borisov97db1202019-03-12 17:20:24 +02001269 if (!ctx) {
1270 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1271 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1272 EXTENT_DO_ACCOUNTING;
1273 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1274 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1275 PAGE_SET_ERROR;
1276
Nikolay Borisov751b6432020-06-03 08:55:22 +03001277 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1278 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001279 return -ENOMEM;
1280 }
1281
1282 async_chunk = ctx->chunks;
1283 atomic_set(&ctx->num_chunks, num_chunks);
1284
1285 for (i = 0; i < num_chunks; i++) {
1286 if (should_compress)
1287 cur_end = min(end, start + SZ_512K - 1);
1288 else
1289 cur_end = end;
1290
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001291 /*
1292 * igrab is called higher up in the call chain, take only the
1293 * lightweight reference for the callback lifetime
1294 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001295 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001296 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001297 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001298 async_chunk[i].start = start;
1299 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001300 async_chunk[i].write_flags = write_flags;
1301 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001302
Chris Mason1d53c9e2019-07-10 12:28:16 -07001303 /*
1304 * The locked_page comes all the way from writepage and its
1305 * the original page we were actually given. As we spread
1306 * this large delalloc region across multiple async_chunk
1307 * structs, only the first struct needs a pointer to locked_page
1308 *
1309 * This way we don't need racey decisions about who is supposed
1310 * to unlock it.
1311 */
1312 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001313 /*
1314 * Depending on the compressibility, the pages might or
1315 * might not go through async. We want all of them to
1316 * be accounted against wbc once. Let's do it here
1317 * before the paths diverge. wbc accounting is used
1318 * only for foreign writeback detection and doesn't
1319 * need full accuracy. Just account the whole thing
1320 * against the first page.
1321 */
1322 wbc_account_cgroup_owner(wbc, locked_page,
1323 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001324 async_chunk[i].locked_page = locked_page;
1325 locked_page = NULL;
1326 } else {
1327 async_chunk[i].locked_page = NULL;
1328 }
1329
Chris Masonec39f762019-07-10 12:28:17 -07001330 if (blkcg_css != blkcg_root_css) {
1331 css_get(blkcg_css);
1332 async_chunk[i].blkcg_css = blkcg_css;
1333 } else {
1334 async_chunk[i].blkcg_css = NULL;
1335 }
1336
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001337 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1338 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001339
Nikolay Borisov97db1202019-03-12 17:20:24 +02001340 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001341 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001342
Nikolay Borisov97db1202019-03-12 17:20:24 +02001343 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001344
Chris Mason771ed682008-11-06 22:02:51 -05001345 *nr_written += nr_pages;
1346 start = cur_end + 1;
1347 }
1348 *page_started = 1;
1349 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001350}
1351
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001352static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001353 u64 bytenr, u64 num_bytes)
1354{
1355 int ret;
1356 struct btrfs_ordered_sum *sums;
1357 LIST_HEAD(list);
1358
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001359 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001360 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001361 if (ret == 0 && list_empty(&list))
1362 return 0;
1363
1364 while (!list_empty(&list)) {
1365 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1366 list_del(&sums->list);
1367 kfree(sums);
1368 }
Liu Bo58113752018-01-31 17:09:13 -07001369 if (ret < 0)
1370 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001371 return 1;
1372}
1373
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001374static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001375 const u64 start, const u64 end,
1376 int *page_started, unsigned long *nr_written)
1377{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001378 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1379 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001380 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001381 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001382 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001383 u64 range_start = start;
1384 u64 count;
1385
1386 /*
1387 * If EXTENT_NORESERVE is set it means that when the buffered write was
1388 * made we had not enough available data space and therefore we did not
1389 * reserve data space for it, since we though we could do NOCOW for the
1390 * respective file range (either there is prealloc extent or the inode
1391 * has the NOCOW bit set).
1392 *
1393 * However when we need to fallback to COW mode (because for example the
1394 * block group for the corresponding extent was turned to RO mode by a
1395 * scrub or relocation) we need to do the following:
1396 *
1397 * 1) We increment the bytes_may_use counter of the data space info.
1398 * If COW succeeds, it allocates a new data extent and after doing
1399 * that it decrements the space info's bytes_may_use counter and
1400 * increments its bytes_reserved counter by the same amount (we do
1401 * this at btrfs_add_reserved_bytes()). So we need to increment the
1402 * bytes_may_use counter to compensate (when space is reserved at
1403 * buffered write time, the bytes_may_use counter is incremented);
1404 *
1405 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1406 * that if the COW path fails for any reason, it decrements (through
1407 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1408 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001409 *
1410 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001411 * space cache inode or an inode of the data relocation tree, we must
1412 * also increment bytes_may_use of the data space_info for the same
1413 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001414 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001415 * group that contains that extent to RO mode and therefore force COW
1416 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001417 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001418 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001419 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001420 if (count > 0 || is_space_ino || is_reloc_ino) {
1421 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001422 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001423 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1424
Filipe Manana6bd335b2020-06-08 13:33:05 +01001425 if (is_space_ino || is_reloc_ino)
1426 bytes = range_bytes;
1427
Filipe Manana467dc472020-05-27 11:16:07 +01001428 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001429 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001430 spin_unlock(&sinfo->lock);
1431
Filipe Manana2166e5e2020-05-27 11:16:19 +01001432 if (count > 0)
1433 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1434 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001435 }
1436
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001437 return cow_file_range(inode, locked_page, start, end, page_started,
1438 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001439}
1440
Chris Masond352ac62008-09-29 15:18:18 -04001441/*
1442 * when nowcow writeback call back. This checks for snapshots or COW copies
1443 * of the extents that exist in the file, and COWs the file as required.
1444 *
1445 * If no cow copies or snapshots exist, we write directly to the existing
1446 * blocks on disk
1447 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001448static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001449 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001450 const u64 start, const u64 end,
1451 int *page_started, int force,
1452 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001453{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001454 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1455 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001456 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001457 u64 cow_start = (u64)-1;
1458 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001459 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001460 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001461 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1462 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001463 bool nocow = false;
1464 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001465
1466 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001467 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001468 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001469 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001470 EXTENT_DO_ACCOUNTING |
1471 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001472 PAGE_CLEAR_DIRTY |
1473 PAGE_SET_WRITEBACK |
1474 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001475 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001476 }
Li Zefan82d59022011-04-20 10:33:24 +08001477
Yan Zheng80ff3852008-10-30 14:20:02 -04001478 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001479 struct btrfs_key found_key;
1480 struct btrfs_file_extent_item *fi;
1481 struct extent_buffer *leaf;
1482 u64 extent_end;
1483 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001484 u64 num_bytes = 0;
1485 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001486 u64 ram_bytes;
1487 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001488
1489 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001490
Liu Boe4c3b2d2017-01-30 12:25:28 -08001491 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001492 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001493 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001494 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001495
1496 /*
1497 * If there is no extent for our range when doing the initial
1498 * search, then go back to the previous slot as it will be the
1499 * one containing the search offset
1500 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001501 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1502 leaf = path->nodes[0];
1503 btrfs_item_key_to_cpu(leaf, &found_key,
1504 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001505 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001506 found_key.type == BTRFS_EXTENT_DATA_KEY)
1507 path->slots[0]--;
1508 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001509 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001510next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001511 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001512 leaf = path->nodes[0];
1513 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1514 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001515 if (ret < 0) {
1516 if (cow_start != (u64)-1)
1517 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001518 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001519 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 if (ret > 0)
1521 break;
1522 leaf = path->nodes[0];
1523 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001524
Yan Zheng80ff3852008-10-30 14:20:02 -04001525 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001526
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001527 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001528 if (found_key.objectid > ino)
1529 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001530 /*
1531 * Keep searching until we find an EXTENT_ITEM or there are no
1532 * more extents for this inode
1533 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001534 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1535 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1536 path->slots[0]++;
1537 goto next_slot;
1538 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001539
1540 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001541 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001542 found_key.offset > end)
1543 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001544
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001545 /*
1546 * If the found extent starts after requested offset, then
1547 * adjust extent_end to be right before this extent begins
1548 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001549 if (found_key.offset > cur_offset) {
1550 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001551 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001552 goto out_check;
1553 }
Chris Masonc31f8832008-01-08 15:46:31 -05001554
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001555 /*
1556 * Found extent which begins before our range and potentially
1557 * intersect it
1558 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001559 fi = btrfs_item_ptr(leaf, path->slots[0],
1560 struct btrfs_file_extent_item);
1561 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001562
Josef Bacikcc95bef2013-04-04 14:31:27 -04001563 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001564 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1565 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001566 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001567 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001568 extent_end = found_key.offset +
1569 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001570 disk_num_bytes =
1571 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001572 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001573 * If the extent we got ends before our current offset,
1574 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001575 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001576 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001577 path->slots[0]++;
1578 goto next_slot;
1579 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001580 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001581 if (disk_bytenr == 0)
1582 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001583 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001584 if (btrfs_file_extent_compression(leaf, fi) ||
1585 btrfs_file_extent_encryption(leaf, fi) ||
1586 btrfs_file_extent_other_encoding(leaf, fi))
1587 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001588 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001589 * If extent is created before the last volume's snapshot
1590 * this implies the extent is shared, hence we can't do
1591 * nocow. This is the same check as in
1592 * btrfs_cross_ref_exist but without calling
1593 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001594 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001595 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001596 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001597 btrfs_root_last_snapshot(&root->root_item))
1598 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001599 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1600 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001601 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001602 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001603 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001604 ret = btrfs_cross_ref_exist(root, ino,
1605 found_key.offset -
1606 extent_offset, disk_bytenr);
1607 if (ret) {
1608 /*
1609 * ret could be -EIO if the above fails to read
1610 * metadata.
1611 */
1612 if (ret < 0) {
1613 if (cow_start != (u64)-1)
1614 cur_offset = cow_start;
1615 goto error;
1616 }
1617
Nikolay Borisov3e024842019-08-21 10:42:03 +03001618 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001619 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001620 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001621 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001622 disk_bytenr += cur_offset - found_key.offset;
1623 num_bytes = min(end + 1, extent_end) - cur_offset;
1624 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001625 * If there are pending snapshots for this root, we
1626 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001627 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001628 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001629 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001630 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001631 * force cow if csum exists in the range.
1632 * this ensure that csum for a given extent are
1633 * either valid or do not exist.
1634 */
Liu Bo58113752018-01-31 17:09:13 -07001635 ret = csum_exist_in_range(fs_info, disk_bytenr,
1636 num_bytes);
1637 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001638 /*
1639 * ret could be -EIO if the above fails to read
1640 * metadata.
1641 */
1642 if (ret < 0) {
1643 if (cow_start != (u64)-1)
1644 cur_offset = cow_start;
1645 goto error;
1646 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001647 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001648 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001649 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001650 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001651 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001652 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001653 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001654 extent_end = found_key.offset + ram_bytes;
1655 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001656 /* Skip extents outside of our requested range */
1657 if (extent_end <= start) {
1658 path->slots[0]++;
1659 goto next_slot;
1660 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001661 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001662 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001663 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001664 }
1665out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001666 /*
1667 * If nocow is false then record the beginning of the range
1668 * that needs to be COWed
1669 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001670 if (!nocow) {
1671 if (cow_start == (u64)-1)
1672 cow_start = cur_offset;
1673 cur_offset = extent_end;
1674 if (cur_offset > end)
1675 break;
1676 path->slots[0]++;
1677 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001678 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001679
David Sterbab3b4aa72011-04-21 01:20:15 +02001680 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001681
1682 /*
1683 * COW range from cow_start to found_key.offset - 1. As the key
1684 * will contain the beginning of the first extent that can be
1685 * NOCOW, following one which needs to be COW'ed
1686 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001687 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001688 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001689 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001690 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001691 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001692 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001693 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001694 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001695
Yan Zhengd899e052008-10-30 14:25:28 -04001696 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001697 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001698 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001699
Nikolay Borisov968322c2020-06-03 08:55:21 +03001700 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001701 orig_start,
1702 disk_bytenr, /* block_start */
1703 num_bytes, /* block_len */
1704 disk_num_bytes, /* orig_block_len */
1705 ram_bytes, BTRFS_COMPRESS_NONE,
1706 BTRFS_ORDERED_PREALLOC);
1707 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001708 ret = PTR_ERR(em);
1709 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001710 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001711 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001712 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001713 disk_bytenr, num_bytes,
1714 num_bytes,
1715 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001716 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001717 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001718 cur_offset + num_bytes - 1,
1719 0);
1720 goto error;
1721 }
Yan Zhengd899e052008-10-30 14:25:28 -04001722 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001723 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001724 disk_bytenr, num_bytes,
1725 num_bytes,
1726 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001727 if (ret)
1728 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001729 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001730
Filipe Mananaf78c4362016-05-09 13:15:41 +01001731 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001732 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001733 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001734
Yan, Zhengefa56462010-05-16 10:49:59 -04001735 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001736 BTRFS_DATA_RELOC_TREE_OBJECTID)
1737 /*
1738 * Error handled later, as we must prevent
1739 * extent_clear_unlock_delalloc() in error handler
1740 * from freeing metadata of created ordered extent.
1741 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001742 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001743 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001744
Nikolay Borisov968322c2020-06-03 08:55:21 +03001745 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001746 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001747 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001748 EXTENT_DELALLOC |
1749 EXTENT_CLEAR_DATA_RESV,
1750 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1751
Yan Zheng80ff3852008-10-30 14:20:02 -04001752 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001753
1754 /*
1755 * btrfs_reloc_clone_csums() error, now we're OK to call error
1756 * handler, as metadata for created ordered extent will only
1757 * be freed by btrfs_finish_ordered_io().
1758 */
1759 if (ret)
1760 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001761 if (cur_offset > end)
1762 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001763 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001764 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001765
Robbie Ko506481b2018-10-30 18:04:04 +08001766 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001767 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001768
Yan Zheng80ff3852008-10-30 14:20:02 -04001769 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001770 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001771 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1772 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001773 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001774 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001775 }
1776
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001777error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001778 if (nocow)
1779 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1780
Josef Bacik17ca04a2012-05-31 15:58:55 -04001781 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001782 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001783 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001784 EXTENT_DELALLOC | EXTENT_DEFRAG |
1785 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1786 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001787 PAGE_SET_WRITEBACK |
1788 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001789 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001790 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001791}
1792
Nikolay Borisov0c494222020-06-03 08:55:28 +03001793static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001794{
1795
Nikolay Borisov0c494222020-06-03 08:55:28 +03001796 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1797 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001798 return 0;
1799
1800 /*
1801 * @defrag_bytes is a hint value, no spinlock held here,
1802 * if is not zero, it means the file is defragging.
1803 * Force cow if given extent needs to be defragged.
1804 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001805 if (inode->defrag_bytes &&
1806 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001807 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 Borisov98456b92020-06-03 08:55:29 +03001816int btrfs_run_delalloc_range(struct btrfs_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;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001821 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001822
Nikolay Borisov98456b92020-06-03 08:55:29 +03001823 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1824 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001825 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001826 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1827 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001828 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001829 } else if (!inode_can_compress(inode) ||
1830 !inode_need_compress(inode, start, end)) {
1831 ret = cow_file_range(inode, locked_page, start, end,
1832 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001833 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001834 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1835 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001836 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001837 }
Qu Wenruo524272602017-03-08 10:25:52 +08001838 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001839 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001840 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001841 return ret;
1842}
1843
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001844void btrfs_split_delalloc_extent(struct inode *inode,
1845 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001846{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001847 u64 size;
1848
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001849 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001850 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001851 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001852
Josef Bacikdcab6a32015-02-11 15:08:59 -05001853 size = orig->end - orig->start + 1;
1854 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001855 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001856 u64 new_size;
1857
1858 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001859 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001860 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001861 */
1862 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001863 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001864 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001865 num_extents += count_max_extents(new_size);
1866 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001867 return;
1868 }
1869
Josef Bacik9e0baf62011-07-15 15:16:44 +00001870 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001871 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001872 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001873}
1874
1875/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001876 * Handle merged delayed allocation extents so we can keep track of new extents
1877 * that are just merged onto old extents, such as when we are doing sequential
1878 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001879 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001880void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1881 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001882{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001883 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001884 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001885
Josef Bacik9ed74f22009-09-11 16:12:44 -04001886 /* not delalloc, ignore it */
1887 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001888 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001889
Josef Bacik8461a3d2015-03-13 15:12:08 -04001890 if (new->start > other->start)
1891 new_size = new->end - other->start + 1;
1892 else
1893 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001894
1895 /* we're not bigger than the max, unreserve the space and go */
1896 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1897 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001898 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001899 spin_unlock(&BTRFS_I(inode)->lock);
1900 return;
1901 }
1902
1903 /*
Josef Bacikba117212015-03-13 15:01:24 -04001904 * We have to add up either side to figure out how many extents were
1905 * accounted for before we merged into one big extent. If the number of
1906 * extents we accounted for is <= the amount we need for the new range
1907 * then we can return, otherwise drop. Think of it like this
1908 *
1909 * [ 4k][MAX_SIZE]
1910 *
1911 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1912 * need 2 outstanding extents, on one side we have 1 and the other side
1913 * we have 1 so they are == and we can return. But in this case
1914 *
1915 * [MAX_SIZE+4k][MAX_SIZE+4k]
1916 *
1917 * Each range on their own accounts for 2 extents, but merged together
1918 * they are only 3 extents worth of accounting, so we need to drop in
1919 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001920 */
Josef Bacikba117212015-03-13 15:01:24 -04001921 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001922 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001923 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001924 num_extents += count_max_extents(old_size);
1925 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001926 return;
1927
Josef Bacik9e0baf62011-07-15 15:16:44 +00001928 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001929 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001930 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001931}
1932
Miao Xieeb73c1b2013-05-15 07:48:22 +00001933static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1934 struct inode *inode)
1935{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001936 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1937
Miao Xieeb73c1b2013-05-15 07:48:22 +00001938 spin_lock(&root->delalloc_lock);
1939 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1940 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1941 &root->delalloc_inodes);
1942 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1943 &BTRFS_I(inode)->runtime_flags);
1944 root->nr_delalloc_inodes++;
1945 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001946 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001947 BUG_ON(!list_empty(&root->delalloc_root));
1948 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001949 &fs_info->delalloc_roots);
1950 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001951 }
1952 }
1953 spin_unlock(&root->delalloc_lock);
1954}
1955
Nikolay Borisov2b877332018-04-27 12:21:51 +03001956
1957void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1958 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001959{
David Sterba3ffbd682018-06-29 10:56:42 +02001960 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001961
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001962 if (!list_empty(&inode->delalloc_inodes)) {
1963 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001964 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001965 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001966 root->nr_delalloc_inodes--;
1967 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001968 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001969 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001970 BUG_ON(list_empty(&root->delalloc_root));
1971 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001972 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001973 }
1974 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001975}
1976
1977static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1978 struct btrfs_inode *inode)
1979{
1980 spin_lock(&root->delalloc_lock);
1981 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001982 spin_unlock(&root->delalloc_lock);
1983}
1984
Chris Masond352ac62008-09-29 15:18:18 -04001985/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001986 * Properly track delayed allocation bytes in the inode and to maintain the
1987 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001988 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001989void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1990 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001991{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001992 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1993
Wang Shilong47059d92014-07-03 18:22:07 +08001994 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1995 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001996 /*
1997 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001998 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001999 * bit, which is only set or cleared with irqs on
2000 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002001 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002002 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002003 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002004 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002005 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002006
Josef Bacik8b62f872017-10-19 14:15:55 -04002007 spin_lock(&BTRFS_I(inode)->lock);
2008 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2009 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002010
Josef Bacik6a3891c2015-03-16 17:38:52 -04002011 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002012 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002013 return;
2014
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002015 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2016 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002017 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002018 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002019 if (*bits & EXTENT_DEFRAG)
2020 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002021 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002022 &BTRFS_I(inode)->runtime_flags))
2023 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002024 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002025 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002026
2027 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2028 (*bits & EXTENT_DELALLOC_NEW)) {
2029 spin_lock(&BTRFS_I(inode)->lock);
2030 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2031 state->start;
2032 spin_unlock(&BTRFS_I(inode)->lock);
2033 }
Chris Mason291d6732008-01-29 15:55:23 -05002034}
2035
Chris Masond352ac62008-09-29 15:18:18 -04002036/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002037 * Once a range is no longer delalloc this function ensures that proper
2038 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002039 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002040void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2041 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002042{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002043 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2044 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002045 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002046 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002047
Filipe Manana4a4b9642017-07-27 19:52:55 +01002048 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2049 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002050 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002051 spin_unlock(&inode->lock);
2052 }
Wang Shilong47059d92014-07-03 18:22:07 +08002053
Chris Mason75eff682008-12-15 15:54:40 -05002054 /*
2055 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002056 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002057 * bit, which is only set or cleared with irqs on
2058 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002059 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002060 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002061 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002062
Josef Bacik8b62f872017-10-19 14:15:55 -04002063 spin_lock(&inode->lock);
2064 btrfs_mod_outstanding_extents(inode, -num_extents);
2065 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002066
Josef Bacikb6d08f02013-09-27 14:57:43 -04002067 /*
2068 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002069 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002070 * error.
2071 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002072 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002073 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002074 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002075
Josef Bacik6a3891c2015-03-16 17:38:52 -04002076 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002077 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002078 return;
2079
Filipe Mananaa315e682017-03-06 23:04:20 +00002080 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2081 do_list && !(state->state & EXTENT_NORESERVE) &&
2082 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002083 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002084
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002085 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2086 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002087 spin_lock(&inode->lock);
2088 inode->delalloc_bytes -= len;
2089 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002090 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002091 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002092 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002093 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002094 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002095
2096 if ((state->state & EXTENT_DELALLOC_NEW) &&
2097 (*bits & EXTENT_DELALLOC_NEW)) {
2098 spin_lock(&inode->lock);
2099 ASSERT(inode->new_delalloc_bytes >= len);
2100 inode->new_delalloc_bytes -= len;
2101 spin_unlock(&inode->lock);
2102 }
Chris Mason291d6732008-01-29 15:55:23 -05002103}
2104
Chris Masond352ac62008-09-29 15:18:18 -04002105/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002106 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2107 * in a chunk's stripe. This function ensures that bios do not span a
2108 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002109 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002110 * @page - The page we are about to add to the bio
2111 * @size - size we want to add to the bio
2112 * @bio - bio we want to ensure is smaller than a stripe
2113 * @bio_flags - flags of the bio
2114 *
2115 * return 1 if page cannot be added to the bio
2116 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002117 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002118 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002119int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2120 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002121{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002122 struct inode *inode = page->mapping->host;
2123 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002124 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002125 u64 length = 0;
2126 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002127 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002128 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002129
Chris Mason771ed682008-11-06 22:02:51 -05002130 if (bio_flags & EXTENT_BIO_COMPRESSED)
2131 return 0;
2132
Kent Overstreet4f024f32013-10-11 15:44:27 -07002133 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002134 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002135 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2136 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002137 if (ret < 0)
2138 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002139
2140 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002141 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002142 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002143}
2144
Chris Masond352ac62008-09-29 15:18:18 -04002145/*
2146 * in order to insert checksums into the metadata in large chunks,
2147 * we wait until bio submission time. All the pages in the bio are
2148 * checksummed and sums are attached onto the ordered extent record.
2149 *
2150 * At IO completion time the cums attached on the ordered extent record
2151 * are inserted into the btree
2152 */
David Sterbad0ee3932018-03-08 14:35:48 +01002153static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002154 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002155{
Josef Bacikc6100a42017-05-05 11:57:13 -04002156 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002157 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002158
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002159 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002160 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002161 return 0;
2162}
Chris Masone0156402008-04-16 11:15:20 -04002163
Chris Mason4a69a412008-11-06 22:03:00 -05002164/*
Chris Masoncad321a2008-12-17 14:51:42 -05002165 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002166 * on write, or reading the csums from the tree before a read.
2167 *
2168 * Rules about async/sync submit,
2169 * a) read: sync submit
2170 *
2171 * b) write without checksum: sync submit
2172 *
2173 * c) write with checksum:
2174 * c-1) if bio is issued by fsync: sync submit
2175 * (sync_writers != 0)
2176 *
2177 * c-2) if root is reloc root: sync submit
2178 * (only in case of buffered IO)
2179 *
2180 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002181 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002182static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002183 int mirror_num,
2184 unsigned long bio_flags)
2185
Chris Mason44b8bd72008-04-16 11:14:51 -04002186{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002187 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002188 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302189 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002190 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002191 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002192 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002193
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002194 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002195
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002196 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302197 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002198
Mike Christie37226b22016-06-05 14:31:52 -05002199 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002200 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002201 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002202 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002203
Chris Masond20f7042008-12-08 16:58:54 -05002204 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002205 ret = btrfs_submit_compressed_read(inode, bio,
2206 mirror_num,
2207 bio_flags);
2208 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002209 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002210 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002211 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002212 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002213 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002214 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002215 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002216 /* csum items have already been cloned */
2217 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2218 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002219 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002220 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002221 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002222 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002223 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002224 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002225 if (ret)
2226 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002227 }
2228
Chris Mason0b86a832008-03-24 15:01:56 -04002229mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002230 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002231
2232out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002233 if (ret) {
2234 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002235 bio_endio(bio);
2236 }
Stefan Behrens61891922012-11-05 18:51:52 +01002237 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002238}
Chris Mason6885f302008-02-20 16:11:05 -05002239
Chris Masond352ac62008-09-29 15:18:18 -04002240/*
2241 * given a list of ordered sums record them in the inode. This happens
2242 * at IO completion time based on sums calculated at bio submission time.
2243 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002244static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002245 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002246{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002247 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002248 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002249
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002250 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002251 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002252 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002253 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002254 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002255 if (ret)
2256 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002257 }
2258 return 0;
2259}
2260
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002261int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002262 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002263 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002264{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002265 WARN_ON(PAGE_ALIGNED(end));
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002266 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2267 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002268}
2269
Chris Masond352ac62008-09-29 15:18:18 -04002270/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002271struct btrfs_writepage_fixup {
2272 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002273 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002274 struct btrfs_work work;
2275};
2276
Christoph Hellwigb2950862008-12-02 09:54:17 -05002277static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002278{
2279 struct btrfs_writepage_fixup *fixup;
2280 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002281 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002282 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002283 struct page *page;
2284 struct inode *inode;
2285 u64 page_start;
2286 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002287 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002288 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002289
2290 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2291 page = fixup->page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002292 inode = fixup->inode;
2293 page_start = page_offset(page);
2294 page_end = page_offset(page) + PAGE_SIZE - 1;
2295
2296 /*
2297 * This is similar to page_mkwrite, we need to reserve the space before
2298 * we take the page lock.
2299 */
2300 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2301 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002302again:
Chris Mason247e7432008-07-17 12:53:51 -04002303 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002304
Chris Mason25f3c502020-01-21 11:51:42 -05002305 /*
2306 * Before we queued this fixup, we took a reference on the page.
2307 * page->mapping may go NULL, but it shouldn't be moved to a different
2308 * address space.
2309 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002310 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2311 /*
2312 * Unfortunately this is a little tricky, either
2313 *
2314 * 1) We got here and our page had already been dealt with and
2315 * we reserved our space, thus ret == 0, so we need to just
2316 * drop our space reservation and bail. This can happen the
2317 * first time we come into the fixup worker, or could happen
2318 * while waiting for the ordered extent.
2319 * 2) Our page was already dealt with, but we happened to get an
2320 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2321 * this case we obviously don't have anything to release, but
2322 * because the page was already dealt with we don't want to
2323 * mark the page with an error, so make sure we're resetting
2324 * ret to 0. This is why we have this check _before_ the ret
2325 * check, because we do not want to have a surprise ENOSPC
2326 * when the page was already properly dealt with.
2327 */
2328 if (!ret) {
2329 btrfs_delalloc_release_extents(BTRFS_I(inode),
2330 PAGE_SIZE);
2331 btrfs_delalloc_release_space(inode, data_reserved,
2332 page_start, PAGE_SIZE,
2333 true);
2334 }
2335 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002336 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002337 }
Chris Mason25f3c502020-01-21 11:51:42 -05002338
2339 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002340 * We can't mess with the page state unless it is locked, so now that
2341 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002342 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002343 if (ret)
2344 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002345
David Sterbaff13db42015-12-03 14:30:40 +01002346 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002347 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002348
2349 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002350 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002351 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002352
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002353 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002354 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002355 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002356 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002357 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002358 unlock_page(page);
2359 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002360 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002361 goto again;
2362 }
Chris Mason247e7432008-07-17 12:53:51 -04002363
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002364 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002365 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002366 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002367 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002368
Chris Mason25f3c502020-01-21 11:51:42 -05002369 /*
2370 * Everything went as planned, we're now the owner of a dirty page with
2371 * delayed allocation bits set and space reserved for our COW
2372 * destination.
2373 *
2374 * The page was dirty when we started, nothing should have cleaned it.
2375 */
2376 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002377 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002378out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002379 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002380 if (free_delalloc_space)
Filipe Manana53687002019-10-09 17:43:59 +01002381 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2382 PAGE_SIZE, true);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002383 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002384 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002385out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002386 if (ret) {
2387 /*
2388 * We hit ENOSPC or other errors. Update the mapping and page
2389 * to reflect the errors and clean the page.
2390 */
2391 mapping_set_error(page->mapping, ret);
2392 end_extent_writepage(page, ret, page_start, page_end);
2393 clear_page_dirty_for_io(page);
2394 SetPageError(page);
2395 }
2396 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002397 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002398 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002399 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002400 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002401 /*
2402 * As a precaution, do a delayed iput in case it would be the last iput
2403 * that could need flushing space. Recursing back to fixup worker would
2404 * deadlock.
2405 */
2406 btrfs_add_delayed_iput(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002407}
2408
2409/*
2410 * There are a few paths in the higher layers of the kernel that directly
2411 * set the page dirty bit without asking the filesystem if it is a
2412 * good idea. This causes problems because we want to make sure COW
2413 * properly happens and the data=ordered rules are followed.
2414 *
Chris Masonc8b97812008-10-29 14:49:59 -04002415 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002416 * hasn't been properly setup for IO. We kick off an async process
2417 * to fix it up. The async helper will wait for ordered extents, set
2418 * the delalloc bit and make it safe to write the page.
2419 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002420int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002421{
2422 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002423 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002424 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002425
Chris Mason8b62b722009-09-02 16:53:46 -04002426 /* this page is properly in the ordered list */
2427 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002428 return 0;
2429
Chris Mason25f3c502020-01-21 11:51:42 -05002430 /*
2431 * PageChecked is set below when we create a fixup worker for this page,
2432 * don't try to create another one if we're already PageChecked()
2433 *
2434 * The extent_io writepage code will redirty the page if we send back
2435 * EAGAIN.
2436 */
Chris Mason247e7432008-07-17 12:53:51 -04002437 if (PageChecked(page))
2438 return -EAGAIN;
2439
2440 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2441 if (!fixup)
2442 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002443
Josef Bacikf4b13632020-01-21 14:34:52 -05002444 /*
2445 * We are already holding a reference to this inode from
2446 * write_cache_pages. We need to hold it because the space reservation
2447 * takes place outside of the page lock, and we can't trust
2448 * page->mapping outside of the page lock.
2449 */
2450 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002451 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002452 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002453 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002454 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002455 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002456 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002457
2458 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002459}
2460
Yan Zhengd899e052008-10-30 14:25:28 -04002461static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002462 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002463 struct btrfs_file_extent_item *stack_fi,
2464 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002465{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002466 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002467 struct btrfs_path *path;
2468 struct extent_buffer *leaf;
2469 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002470 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2471 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2472 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2473 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002474 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002475 int ret;
2476
2477 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002478 if (!path)
2479 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002480
Chris Masona1ed8352009-09-11 12:27:37 -04002481 /*
2482 * we may be replacing one extent in the tree with another.
2483 * The new extent is pinned in the extent map, and we don't want
2484 * to drop it from the cache until it is completely in the btree.
2485 *
2486 * So, tell btrfs_drop_extents to leave this extent in the cache.
2487 * the caller is expected to unpin it and allow it to be merged
2488 * with the others.
2489 */
Nikolay Borisovc553f942020-06-03 08:55:19 +03002490 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002491 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002492 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002493 if (ret)
2494 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002495
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002496 if (!extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002497 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002498 ins.offset = file_pos;
2499 ins.type = BTRFS_EXTENT_DATA_KEY;
2500
2501 path->leave_spinning = 1;
2502 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002503 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002504 if (ret)
2505 goto out;
2506 }
Yan Zhengd899e052008-10-30 14:25:28 -04002507 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002508 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2509 write_extent_buffer(leaf, stack_fi,
2510 btrfs_item_ptr_offset(leaf, path->slots[0]),
2511 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002512
Yan Zhengd899e052008-10-30 14:25:28 -04002513 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002514 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002515
Nikolay Borisovc553f942020-06-03 08:55:19 +03002516 inode_add_bytes(&inode->vfs_inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002517
2518 ins.objectid = disk_bytenr;
2519 ins.offset = disk_num_bytes;
2520 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002521
Nikolay Borisovc553f942020-06-03 08:55:19 +03002522 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002523 if (ret)
2524 goto out;
2525
Nikolay Borisovc553f942020-06-03 08:55:19 +03002526 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002527 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002528out:
Yan Zhengd899e052008-10-30 14:25:28 -04002529 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002530
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002531 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002532}
2533
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002534static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002535 u64 start, u64 len)
2536{
David Sterba32da53862019-10-29 19:20:18 +01002537 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002538
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002539 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002540 ASSERT(cache);
2541
2542 spin_lock(&cache->lock);
2543 cache->delalloc_bytes -= len;
2544 spin_unlock(&cache->lock);
2545
2546 btrfs_put_block_group(cache);
2547}
2548
Qu Wenruo203f44c52020-06-10 09:04:40 +08002549static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2550 struct inode *inode,
2551 struct btrfs_ordered_extent *oe)
2552{
2553 struct btrfs_file_extent_item stack_fi;
2554 u64 logical_len;
2555
2556 memset(&stack_fi, 0, sizeof(stack_fi));
2557 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2558 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2559 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2560 oe->disk_num_bytes);
2561 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2562 logical_len = oe->truncated_len;
2563 else
2564 logical_len = oe->num_bytes;
2565 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2566 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2567 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2568 /* Encryption and other encoding is reserved and all 0 */
2569
Nikolay Borisovc553f942020-06-03 08:55:19 +03002570 return insert_reserved_file_extent(trans, BTRFS_I(inode), oe->file_offset,
Qu Wenruo7dbeaad2020-06-10 09:04:43 +08002571 &stack_fi, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002572}
2573
2574/*
2575 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002576 * an ordered extent if the range of bytes in the file it covers are
2577 * fully written.
2578 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002579static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002580{
Josef Bacik5fd02042012-05-02 14:00:54 -04002581 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002582 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002583 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002584 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002585 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002586 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002587 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002588 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002589 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002590 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002591 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002592 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002593 bool range_locked = false;
2594 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002595 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002596 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002597
Omar Sandovalbffe6332019-12-02 17:34:19 -08002598 start = ordered_extent->file_offset;
2599 end = start + ordered_extent->num_bytes - 1;
2600
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002601 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2602 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2603 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2604 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002605
Nikolay Borisov8d510122019-10-08 20:43:06 +03002606 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002607
Josef Bacik5fd02042012-05-02 14:00:54 -04002608 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2609 ret = -EIO;
2610 goto out;
2611 }
2612
Omar Sandovalbffe6332019-12-02 17:34:19 -08002613 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002614
Josef Bacik77cef2e2013-08-29 13:57:21 -04002615 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2616 truncated = true;
2617 logical_len = ordered_extent->truncated_len;
2618 /* Truncated the entire extent, don't bother adding */
2619 if (!logical_len)
2620 goto out;
2621 }
2622
Yan, Zhengc2167752009-11-12 09:34:21 +00002623 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002624 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002625
Josef Bacikd923afe2020-01-17 09:02:23 -05002626 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002627 if (freespace_inode)
2628 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002629 else
2630 trans = btrfs_join_transaction(root);
2631 if (IS_ERR(trans)) {
2632 ret = PTR_ERR(trans);
2633 trans = NULL;
2634 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002635 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002636 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002637 ret = btrfs_update_inode_fallback(trans, root, inode);
2638 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002639 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002640 goto out;
2641 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002642
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002643 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002644 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645
Nikolay Borisov8d510122019-10-08 20:43:06 +03002646 if (freespace_inode)
2647 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002648 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002649 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002650 if (IS_ERR(trans)) {
2651 ret = PTR_ERR(trans);
2652 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002653 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002654 }
Chris Masona79b7d42014-05-22 16:18:52 -07002655
Josef Bacik69fe2d72017-10-19 14:15:57 -04002656 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002657
Chris Masonc8b97812008-10-29 14:49:59 -04002658 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002659 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002660 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002661 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002662 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002663 ordered_extent->file_offset,
2664 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002665 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002666 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002667 BUG_ON(root == fs_info->tree_root);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002668 ret = insert_ordered_extent_file_extent(trans, inode,
2669 ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002670 if (!ret) {
2671 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002672 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002673 ordered_extent->disk_bytenr,
2674 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002675 }
Yan Zhengd899e052008-10-30 14:25:28 -04002676 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002677 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002678 ordered_extent->file_offset,
2679 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002680 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002681 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002682 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002683 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002684
Nikolay Borisovac01f262018-01-08 10:59:43 +02002685 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2686 if (ret) {
2687 btrfs_abort_transaction(trans, ret);
2688 goto out;
2689 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002690
Josef Bacikd923afe2020-01-17 09:02:23 -05002691 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002692 ret = btrfs_update_inode_fallback(trans, root, inode);
2693 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002694 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002695 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002696 }
2697 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002698out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002699 clear_bits = EXTENT_DEFRAG;
2700 if (range_locked)
2701 clear_bits |= EXTENT_LOCKED;
2702 if (clear_new_delalloc_bytes)
2703 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002704 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2705 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002706 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002707
Miao Xiea698d0752012-09-20 01:51:59 -06002708 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002709 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002710
Josef Bacik77cef2e2013-08-29 13:57:21 -04002711 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002712 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002713
2714 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002715 unwritten_start += logical_len;
2716 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002717
2718 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002719 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002720
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002721 /*
2722 * If the ordered extent had an IOERR or something else went
2723 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002724 * back to the allocator. We only free the extent in the
2725 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002726 *
2727 * If we made it past insert_reserved_file_extent before we
2728 * errored out then we don't need to do this as the accounting
2729 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002730 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002731 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002732 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002733 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002734 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2735 /*
2736 * Discard the range before returning it back to the
2737 * free space pool
2738 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002739 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002740 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002741 ordered_extent->disk_bytenr,
2742 ordered_extent->disk_num_bytes,
2743 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002744 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002745 ordered_extent->disk_bytenr,
2746 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002747 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002748 }
2749
Josef Bacik5fd02042012-05-02 14:00:54 -04002750 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002751 * This needs to be done to make sure anybody waiting knows we are done
2752 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002753 */
2754 btrfs_remove_ordered_extent(inode, ordered_extent);
2755
Chris Masone6dcd2d2008-07-17 12:53:50 -04002756 /* once for us */
2757 btrfs_put_ordered_extent(ordered_extent);
2758 /* once for the tree */
2759 btrfs_put_ordered_extent(ordered_extent);
2760
Josef Bacik5fd02042012-05-02 14:00:54 -04002761 return ret;
2762}
2763
2764static void finish_ordered_fn(struct btrfs_work *work)
2765{
2766 struct btrfs_ordered_extent *ordered_extent;
2767 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2768 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002769}
2770
Nikolay Borisovc6297322018-11-08 10:18:08 +02002771void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2772 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002773{
Josef Bacik5fd02042012-05-02 14:00:54 -04002774 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002775 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002776 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002777 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002778
liubo1abe9b82011-03-24 11:18:59 +00002779 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2780
Chris Mason8b62b722009-09-02 16:53:46 -04002781 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002782 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2783 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002784 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002785
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002786 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002787 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002788 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002789 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002790
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002791 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002792 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002793}
2794
Omar Sandoval47df7762020-04-16 14:46:17 -07002795static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2796 int icsum, struct page *page, int pgoff, u64 start,
2797 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002798{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2800 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002801 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002802 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2803 u8 *csum_expected;
2804 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002805
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002806 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002807
2808 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002809 shash->tfm = fs_info->csum_shash;
2810
Eric Biggersfd080012020-04-30 23:51:59 -07002811 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002812
2813 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002814 goto zeroit;
2815
2816 kunmap_atomic(kaddr);
2817 return 0;
2818zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002819 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2820 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002821 memset(kaddr + pgoff, 1, len);
2822 flush_dcache_page(page);
2823 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002824 return -EIO;
2825}
2826
Chris Masond352ac62008-09-29 15:18:18 -04002827/*
Chris Masond352ac62008-09-29 15:18:18 -04002828 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002829 * if there's a match, we allow the bio to finish. If not, the code in
2830 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002831 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002832static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2833 u64 phy_offset, struct page *page,
2834 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002835{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002836 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002837 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002838 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002839 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002840
Chris Masond20f7042008-12-08 16:58:54 -05002841 if (PageChecked(page)) {
2842 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002843 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002844 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002845
2846 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002847 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002848
Yan Zheng17d217f2008-12-12 10:03:38 -05002849 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002850 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002851 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002852 return 0;
2853 }
2854
Miao Xiefacc8a222013-07-25 19:22:34 +08002855 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002856 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2857 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002858}
Chris Masonb888db22007-08-27 16:49:44 -04002859
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002860/*
2861 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2862 *
2863 * @inode: The inode we want to perform iput on
2864 *
2865 * This function uses the generic vfs_inode::i_count to track whether we should
2866 * just decrement it (in case it's > 1) or if this is the last iput then link
2867 * the inode to the delayed iput machinery. Delayed iputs are processed at
2868 * transaction commit time/superblock commit/cleaner kthread.
2869 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002870void btrfs_add_delayed_iput(struct inode *inode)
2871{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002872 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002873 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002874
2875 if (atomic_add_unless(&inode->i_count, -1, 1))
2876 return;
2877
Josef Bacik034f7842018-12-03 11:06:52 -05002878 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002879 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002880 ASSERT(list_empty(&binode->delayed_iput));
2881 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002882 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002883 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2884 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002885}
2886
Josef Bacik63611e72019-06-18 10:59:18 -04002887static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2888 struct btrfs_inode *inode)
2889{
2890 list_del_init(&inode->delayed_iput);
2891 spin_unlock(&fs_info->delayed_iput_lock);
2892 iput(&inode->vfs_inode);
2893 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2894 wake_up(&fs_info->delayed_iputs_wait);
2895 spin_lock(&fs_info->delayed_iput_lock);
2896}
2897
2898static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2899 struct btrfs_inode *inode)
2900{
2901 if (!list_empty(&inode->delayed_iput)) {
2902 spin_lock(&fs_info->delayed_iput_lock);
2903 if (!list_empty(&inode->delayed_iput))
2904 run_delayed_iput_locked(fs_info, inode);
2905 spin_unlock(&fs_info->delayed_iput_lock);
2906 }
2907}
2908
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002909void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002910{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002911
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002912 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002913 while (!list_empty(&fs_info->delayed_iputs)) {
2914 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002915
David Sterba8089fe62015-11-19 14:15:51 +01002916 inode = list_first_entry(&fs_info->delayed_iputs,
2917 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002918 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002919 }
David Sterba8089fe62015-11-19 14:15:51 +01002920 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002921}
2922
Josef Bacik034f7842018-12-03 11:06:52 -05002923/**
2924 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2925 * @fs_info - the fs_info for this fs
2926 * @return - EINTR if we were killed, 0 if nothing's pending
2927 *
2928 * This will wait on any delayed iputs that are currently running with KILLABLE
2929 * set. Once they are all done running we will return, unless we are killed in
2930 * which case we return EINTR. This helps in user operations like fallocate etc
2931 * that might get blocked on the iputs.
2932 */
2933int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2934{
2935 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2936 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2937 if (ret)
2938 return -EINTR;
2939 return 0;
2940}
2941
Yan, Zhengd68fc572010-05-16 10:49:58 -04002942/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002943 * This creates an orphan entry for the given inode in case something goes wrong
2944 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002945 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002946int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002947 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002948{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002949 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002950
Omar Sandoval27919062018-05-11 13:13:37 -07002951 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2952 if (ret && ret != -EEXIST) {
2953 btrfs_abort_transaction(trans, ret);
2954 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002955 }
2956
Yan, Zhengd68fc572010-05-16 10:49:58 -04002957 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002958}
2959
2960/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002961 * We have done the delete so we can go ahead and remove the orphan item for
2962 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002963 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002964static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002965 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002966{
Omar Sandoval27919062018-05-11 13:13:37 -07002967 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002968}
2969
2970/*
2971 * this cleans up any orphans that may be left on the list from the last use
2972 * of this root.
2973 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002974int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002975{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002976 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002977 struct btrfs_path *path;
2978 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002979 struct btrfs_key key, found_key;
2980 struct btrfs_trans_handle *trans;
2981 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002982 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002983 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002984
Yan, Zhengd68fc572010-05-16 10:49:58 -04002985 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002986 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002987
2988 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002989 if (!path) {
2990 ret = -ENOMEM;
2991 goto out;
2992 }
David Sterbae4058b52015-11-27 16:31:35 +01002993 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002994
2995 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002996 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04002997 key.offset = (u64)-1;
2998
Josef Bacik7b128762008-07-24 12:17:14 -04002999 while (1) {
3000 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003001 if (ret < 0)
3002 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003003
3004 /*
3005 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003006 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003007 * find the key and see if we have stuff that matches
3008 */
3009 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003010 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003011 if (path->slots[0] == 0)
3012 break;
3013 path->slots[0]--;
3014 }
3015
3016 /* pull out the item */
3017 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003018 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3019
3020 /* make sure the item matches what we want */
3021 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3022 break;
David Sterba962a2982014-06-04 18:41:45 +02003023 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003024 break;
3025
3026 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003027 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003028
3029 /*
3030 * this is where we are basically btrfs_lookup, without the
3031 * crossing root thing. we store the inode number in the
3032 * offset of the orphan item.
3033 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003034
3035 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003036 btrfs_err(fs_info,
3037 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003038 ret = -EINVAL;
3039 goto out;
3040 }
3041
3042 last_objectid = found_key.offset;
3043
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003044 found_key.objectid = found_key.offset;
3045 found_key.type = BTRFS_INODE_ITEM_KEY;
3046 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003047 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303048 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003049 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003050 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003051
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003052 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003053 struct btrfs_root *dead_root;
3054 struct btrfs_fs_info *fs_info = root->fs_info;
3055 int is_dead_root = 0;
3056
3057 /*
3058 * this is an orphan in the tree root. Currently these
3059 * could come from 2 sources:
3060 * a) a snapshot deletion in progress
3061 * b) a free space cache inode
3062 * We need to distinguish those two, as the snapshot
3063 * orphan must not get deleted.
3064 * find_dead_roots already ran before us, so if this
3065 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003066 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003067 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003068
3069 spin_lock(&fs_info->fs_roots_radix_lock);
3070 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3071 (unsigned long)found_key.objectid);
3072 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3073 is_dead_root = 1;
3074 spin_unlock(&fs_info->fs_roots_radix_lock);
3075
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003076 if (is_dead_root) {
3077 /* prevent this orphan from being found again */
3078 key.offset = found_key.objectid - 1;
3079 continue;
3080 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003081
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003082 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003083
Josef Bacika8c9e572011-09-21 16:55:59 -04003084 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003085 * If we have an inode with links, there are a couple of
3086 * possibilities. Old kernels (before v3.12) used to create an
3087 * orphan item for truncate indicating that there were possibly
3088 * extent items past i_size that needed to be deleted. In v3.12,
3089 * truncate was changed to update i_size in sync with the extent
3090 * items, but the (useless) orphan item was still created. Since
3091 * v4.18, we don't create the orphan item for truncate at all.
3092 *
3093 * So, this item could mean that we need to do a truncate, but
3094 * only if this filesystem was last used on a pre-v3.12 kernel
3095 * and was not cleanly unmounted. The odds of that are quite
3096 * slim, and it's a pain to do the truncate now, so just delete
3097 * the orphan item.
3098 *
3099 * It's also possible that this orphan item was supposed to be
3100 * deleted but wasn't. The inode number may have been reused,
3101 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003102 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003103 if (ret == -ENOENT || inode->i_nlink) {
3104 if (!ret)
3105 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003106 trans = btrfs_start_transaction(root, 1);
3107 if (IS_ERR(trans)) {
3108 ret = PTR_ERR(trans);
3109 goto out;
3110 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003111 btrfs_debug(fs_info, "auto deleting %Lu",
3112 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003113 ret = btrfs_del_orphan_item(trans, root,
3114 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003115 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003116 if (ret)
3117 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003118 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003119 }
Josef Bacik7b128762008-07-24 12:17:14 -04003120
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003121 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003122
3123 /* this will do delete_inode and everything for us */
3124 iput(inode);
3125 }
Miao Xie3254c872011-11-10 20:45:05 -05003126 /* release the path since we're done with it */
3127 btrfs_release_path(path);
3128
Yan, Zhengd68fc572010-05-16 10:49:58 -04003129 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3130
Omar Sandovala575cee2018-05-11 13:13:38 -07003131 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003132 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003133 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003134 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003135 }
Josef Bacik7b128762008-07-24 12:17:14 -04003136
3137 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003138 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003139
3140out:
3141 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003142 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003143 btrfs_free_path(path);
3144 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003145}
3146
Chris Masond352ac62008-09-29 15:18:18 -04003147/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003148 * very simple check to peek ahead in the leaf looking for xattrs. If we
3149 * don't find any xattrs, we know there can't be any acls.
3150 *
3151 * slot is the slot the inode is in, objectid is the objectid of the inode
3152 */
3153static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003154 int slot, u64 objectid,
3155 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003156{
3157 u32 nritems = btrfs_header_nritems(leaf);
3158 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003159 static u64 xattr_access = 0;
3160 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003161 int scanned = 0;
3162
Josef Bacikf23b5a52013-06-19 10:16:26 -04003163 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003164 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3165 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3166 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3167 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003168 }
3169
Chris Mason46a53cc2009-04-27 11:47:50 -04003170 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003171 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003172 while (slot < nritems) {
3173 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3174
3175 /* we found a different objectid, there must not be acls */
3176 if (found_key.objectid != objectid)
3177 return 0;
3178
3179 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003180 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003181 if (*first_xattr_slot == -1)
3182 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003183 if (found_key.offset == xattr_access ||
3184 found_key.offset == xattr_default)
3185 return 1;
3186 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003187
3188 /*
3189 * we found a key greater than an xattr key, there can't
3190 * be any acls later on
3191 */
3192 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3193 return 0;
3194
3195 slot++;
3196 scanned++;
3197
3198 /*
3199 * it goes inode, inode backrefs, xattrs, extents,
3200 * so if there are a ton of hard links to an inode there can
3201 * be a lot of backrefs. Don't waste time searching too hard,
3202 * this is just an optimization
3203 */
3204 if (scanned >= 8)
3205 break;
3206 }
3207 /* we hit the end of the leaf before we found an xattr or
3208 * something larger than an xattr. We have to assume the inode
3209 * has acls
3210 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003211 if (*first_xattr_slot == -1)
3212 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003213 return 1;
3214}
3215
3216/*
Chris Masond352ac62008-09-29 15:18:18 -04003217 * read an inode from the btree into the in-memory inode
3218 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003219static int btrfs_read_locked_inode(struct inode *inode,
3220 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003221{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003222 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003223 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003224 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003225 struct btrfs_inode_item *inode_item;
3226 struct btrfs_root *root = BTRFS_I(inode)->root;
3227 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003228 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003229 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003230 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003231 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003232 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003233 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003234
3235 ret = btrfs_fill_inode(inode, &rdev);
3236 if (!ret)
3237 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003238
Filipe Manana4222ea72018-10-24 10:13:03 +01003239 if (!path) {
3240 path = btrfs_alloc_path();
3241 if (!path)
3242 return -ENOMEM;
3243 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003244
Chris Mason39279cc2007-06-12 06:35:45 -04003245 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003246
Chris Mason39279cc2007-06-12 06:35:45 -04003247 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003248 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003249 if (path != in_path)
3250 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003251 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003252 }
Chris Mason39279cc2007-06-12 06:35:45 -04003253
Chris Mason5f39d392007-10-15 16:14:19 -04003254 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003255
3256 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003257 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003258
Chris Mason5f39d392007-10-15 16:14:19 -04003259 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3260 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003261 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003262 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003263 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3264 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003265 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003266 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3267 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003268
David Sterbaa937b972014-12-12 17:39:12 +01003269 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3270 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003271
David Sterbaa937b972014-12-12 17:39:12 +01003272 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3273 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003274
David Sterbaa937b972014-12-12 17:39:12 +01003275 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3276 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003277
chandan r9cc97d62012-07-04 12:48:07 +05303278 BTRFS_I(inode)->i_otime.tv_sec =
3279 btrfs_timespec_sec(leaf, &inode_item->otime);
3280 BTRFS_I(inode)->i_otime.tv_nsec =
3281 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003282
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003283 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003284 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003285 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3286
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003287 inode_set_iversion_queried(inode,
3288 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003289 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003290 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003291 rdev = btrfs_inode_rdev(leaf, inode_item);
3292
Josef Bacikaec74772008-07-24 12:12:38 -04003293 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003294 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003295
3296cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003297 /*
3298 * If we were modified in the current generation and evicted from memory
3299 * and then re-read we need to do a full sync since we don't have any
3300 * idea about which extents were modified before we were evicted from
3301 * cache.
3302 *
3303 * This is required for both inode re-read from disk and delayed inode
3304 * in delayed_nodes_tree.
3305 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003306 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003307 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3308 &BTRFS_I(inode)->runtime_flags);
3309
Filipe Mananabde6c242015-07-24 00:00:19 +01003310 /*
3311 * We don't persist the id of the transaction where an unlink operation
3312 * against the inode was last made. So here we assume the inode might
3313 * have been evicted, and therefore the exact value of last_unlink_trans
3314 * lost, and set it to last_trans to avoid metadata inconsistencies
3315 * between the inode and its parent if the inode is fsync'ed and the log
3316 * replayed. For example, in the scenario:
3317 *
3318 * touch mydir/foo
3319 * ln mydir/foo mydir/bar
3320 * sync
3321 * unlink mydir/bar
3322 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3323 * xfs_io -c fsync mydir/foo
3324 * <power failure>
3325 * mount fs, triggers fsync log replay
3326 *
3327 * We must make sure that when we fsync our inode foo we also log its
3328 * parent inode, otherwise after log replay the parent still has the
3329 * dentry with the "bar" name but our inode foo has a link count of 1
3330 * and doesn't have an inode ref with the name "bar" anymore.
3331 *
3332 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003333 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003334 * transaction commits on fsync if our inode is a directory, or if our
3335 * inode is not a directory, logging its parent unnecessarily.
3336 */
3337 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3338
Miao Xie67de1172013-12-26 13:07:06 +08003339 path->slots[0]++;
3340 if (inode->i_nlink != 1 ||
3341 path->slots[0] >= btrfs_header_nritems(leaf))
3342 goto cache_acl;
3343
3344 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003345 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003346 goto cache_acl;
3347
3348 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3349 if (location.type == BTRFS_INODE_REF_KEY) {
3350 struct btrfs_inode_ref *ref;
3351
3352 ref = (struct btrfs_inode_ref *)ptr;
3353 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3354 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3355 struct btrfs_inode_extref *extref;
3356
3357 extref = (struct btrfs_inode_extref *)ptr;
3358 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3359 extref);
3360 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003361cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003362 /*
3363 * try to precache a NULL acl entry for files that don't have
3364 * any xattrs or acls
3365 */
Li Zefan33345d012011-04-20 10:31:50 +08003366 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003367 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003368 if (first_xattr_slot != -1) {
3369 path->slots[0] = first_xattr_slot;
3370 ret = btrfs_load_inode_props(inode, path);
3371 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003372 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003373 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003374 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003375 root->root_key.objectid, ret);
3376 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003377 if (path != in_path)
3378 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003379
Al Viro72c04902009-06-24 16:58:48 -04003380 if (!maybe_acls)
3381 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003382
Chris Mason39279cc2007-06-12 06:35:45 -04003383 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003384 case S_IFREG:
3385 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003386 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003387 inode->i_fop = &btrfs_file_operations;
3388 inode->i_op = &btrfs_file_inode_operations;
3389 break;
3390 case S_IFDIR:
3391 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003392 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003393 break;
3394 case S_IFLNK:
3395 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003396 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003397 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003398 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003399 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003400 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003401 init_special_inode(inode, inode->i_mode, rdev);
3402 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003403 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003404
David Sterba7b6a2212018-03-26 18:40:21 +02003405 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003406 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003407}
3408
Chris Masond352ac62008-09-29 15:18:18 -04003409/*
3410 * given a leaf and an inode, copy the inode fields into the leaf
3411 */
Chris Masone02119d2008-09-05 16:13:11 -04003412static void fill_inode_item(struct btrfs_trans_handle *trans,
3413 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003414 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003415 struct inode *inode)
3416{
Liu Bo51fab692012-12-27 09:01:21 +00003417 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003418
David Sterbac82f8232019-08-09 17:48:21 +02003419 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003420
David Sterbacc4c13d2020-04-29 02:15:56 +02003421 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3422 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3423 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3424 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3425 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003426
David Sterbacc4c13d2020-04-29 02:15:56 +02003427 btrfs_set_token_timespec_sec(&token, &item->atime,
3428 inode->i_atime.tv_sec);
3429 btrfs_set_token_timespec_nsec(&token, &item->atime,
3430 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003431
David Sterbacc4c13d2020-04-29 02:15:56 +02003432 btrfs_set_token_timespec_sec(&token, &item->mtime,
3433 inode->i_mtime.tv_sec);
3434 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3435 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003436
David Sterbacc4c13d2020-04-29 02:15:56 +02003437 btrfs_set_token_timespec_sec(&token, &item->ctime,
3438 inode->i_ctime.tv_sec);
3439 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3440 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003441
David Sterbacc4c13d2020-04-29 02:15:56 +02003442 btrfs_set_token_timespec_sec(&token, &item->otime,
3443 BTRFS_I(inode)->i_otime.tv_sec);
3444 btrfs_set_token_timespec_nsec(&token, &item->otime,
3445 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303446
David Sterbacc4c13d2020-04-29 02:15:56 +02003447 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3448 btrfs_set_token_inode_generation(&token, item,
3449 BTRFS_I(inode)->generation);
3450 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3451 btrfs_set_token_inode_transid(&token, item, trans->transid);
3452 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3453 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3454 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003455}
3456
Chris Masond352ac62008-09-29 15:18:18 -04003457/*
3458 * copy everything in the in-memory inode into the btree.
3459 */
Chris Mason21151332011-11-10 20:39:08 -05003460static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003461 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003462{
3463 struct btrfs_inode_item *inode_item;
3464 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003465 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003466 int ret;
3467
3468 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003469 if (!path)
3470 return -ENOMEM;
3471
Chris Masonb9473432009-03-13 11:00:37 -04003472 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003473 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3474 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003475 if (ret) {
3476 if (ret > 0)
3477 ret = -ENOENT;
3478 goto failed;
3479 }
3480
Chris Mason5f39d392007-10-15 16:14:19 -04003481 leaf = path->nodes[0];
3482 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003483 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003484
Chris Masone02119d2008-09-05 16:13:11 -04003485 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003486 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003487 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003488 ret = 0;
3489failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003490 btrfs_free_path(path);
3491 return ret;
3492}
3493
Chris Masond352ac62008-09-29 15:18:18 -04003494/*
Chris Mason21151332011-11-10 20:39:08 -05003495 * copy everything in the in-memory inode into the btree.
3496 */
3497noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3498 struct btrfs_root *root, struct inode *inode)
3499{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003500 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003501 int ret;
3502
3503 /*
3504 * If the inode is a free space inode, we can deadlock during commit
3505 * if we put it into the delayed code.
3506 *
3507 * The data relocation inode should also be directly updated
3508 * without delay
3509 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003510 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003511 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003512 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003513 btrfs_update_root_times(trans, root);
3514
Chris Mason21151332011-11-10 20:39:08 -05003515 ret = btrfs_delayed_update_inode(trans, root, inode);
3516 if (!ret)
3517 btrfs_set_inode_last_trans(trans, inode);
3518 return ret;
3519 }
3520
3521 return btrfs_update_inode_item(trans, root, inode);
3522}
3523
Josef Bacikbe6aef62012-10-22 15:43:12 -04003524noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3525 struct btrfs_root *root,
3526 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003527{
3528 int ret;
3529
3530 ret = btrfs_update_inode(trans, root, inode);
3531 if (ret == -ENOSPC)
3532 return btrfs_update_inode_item(trans, root, inode);
3533 return ret;
3534}
3535
3536/*
Chris Masond352ac62008-09-29 15:18:18 -04003537 * unlink helper that gets used here in inode.c and in the tree logging
3538 * recovery code. It remove a link in a directory with a given name, and
3539 * also drops the back refs in the inode to the directory
3540 */
Al Viro92986792011-03-04 17:14:37 +00003541static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3542 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003543 struct btrfs_inode *dir,
3544 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003545 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003546{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003547 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003548 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003549 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003550 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003551 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003552 u64 ino = btrfs_ino(inode);
3553 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003554
3555 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003556 if (!path) {
3557 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003558 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003559 }
3560
Chris Masonb9473432009-03-13 11:00:37 -04003561 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003562 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003563 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003564 if (IS_ERR_OR_NULL(di)) {
3565 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003566 goto err;
3567 }
Chris Mason39279cc2007-06-12 06:35:45 -04003568 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003569 if (ret)
3570 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003571 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003572
Miao Xie67de1172013-12-26 13:07:06 +08003573 /*
3574 * If we don't have dir index, we have to get it by looking up
3575 * the inode ref, since we get the inode ref, remove it directly,
3576 * it is unnecessary to do delayed deletion.
3577 *
3578 * But if we have dir index, needn't search inode ref to get it.
3579 * Since the inode ref is close to the inode item, it is better
3580 * that we delay to delete it, and just do this deletion when
3581 * we update the inode item.
3582 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003583 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003584 ret = btrfs_delayed_delete_inode_ref(inode);
3585 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003586 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003587 goto skip_backref;
3588 }
3589 }
3590
Li Zefan33345d012011-04-20 10:31:50 +08003591 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3592 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003593 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003594 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003595 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003596 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003597 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003598 goto err;
3599 }
Miao Xie67de1172013-12-26 13:07:06 +08003600skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003601 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003602 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003603 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003604 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003605 }
Chris Mason39279cc2007-06-12 06:35:45 -04003606
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003607 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3608 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003609 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003610 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003611 goto err;
3612 }
Chris Masone02119d2008-09-05 16:13:11 -04003613
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003614 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3615 index);
Chris Mason6418c962010-10-30 07:34:24 -04003616 if (ret == -ENOENT)
3617 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003618 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003619 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003620
3621 /*
3622 * If we have a pending delayed iput we could end up with the final iput
3623 * being run in btrfs-cleaner context. If we have enough of these built
3624 * up we can end up burning a lot of time in btrfs-cleaner without any
3625 * way to throttle the unlinks. Since we're currently holding a ref on
3626 * the inode we can run the delayed iput here without any issues as the
3627 * final iput won't be done until after we drop the ref we're currently
3628 * holding.
3629 */
3630 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003631err:
3632 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003633 if (ret)
3634 goto out;
3635
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003636 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003637 inode_inc_iversion(&inode->vfs_inode);
3638 inode_inc_iversion(&dir->vfs_inode);
3639 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3640 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3641 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003642out:
Chris Mason39279cc2007-06-12 06:35:45 -04003643 return ret;
3644}
3645
Al Viro92986792011-03-04 17:14:37 +00003646int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3647 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003648 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003649 const char *name, int name_len)
3650{
3651 int ret;
3652 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3653 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003654 drop_nlink(&inode->vfs_inode);
3655 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003656 }
3657 return ret;
3658}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003659
3660/*
3661 * helper to start transaction for unlink and rmdir.
3662 *
Josef Bacikd52be812013-05-29 14:54:47 -04003663 * unlink and rmdir are special in btrfs, they do not always free space, so
3664 * if we cannot make our reservations the normal way try and see if there is
3665 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3666 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003667 */
Josef Bacikd52be812013-05-29 14:54:47 -04003668static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003669{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003670 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003671
Josef Bacike70bea52011-10-11 14:18:24 -04003672 /*
3673 * 1 for the possible orphan item
3674 * 1 for the dir item
3675 * 1 for the dir index
3676 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003677 * 1 for the inode
3678 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003679 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003680}
3681
Chris Mason39279cc2007-06-12 06:35:45 -04003682static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3683{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003684 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003685 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003686 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003687 int ret;
3688
Josef Bacikd52be812013-05-29 14:54:47 -04003689 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003690 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003691 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003692
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003693 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3694 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003695
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003696 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3697 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3698 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003699 if (ret)
3700 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003701
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003703 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003704 if (ret)
3705 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003706 }
Josef Bacik7b128762008-07-24 12:17:14 -04003707
Tsutomu Itohb5324022011-07-19 07:27:20 +00003708out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003709 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003710 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003711 return ret;
3712}
3713
Misono Tomohirof60a2362018-04-18 11:34:52 +09003714static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003715 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003716{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003717 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003718 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003719 struct btrfs_path *path;
3720 struct extent_buffer *leaf;
3721 struct btrfs_dir_item *di;
3722 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003723 const char *name = dentry->d_name.name;
3724 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003725 u64 index;
3726 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003727 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003728 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003729
Josef Bacik045d3962019-12-18 17:20:27 -05003730 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3731 objectid = inode->root->root_key.objectid;
3732 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3733 objectid = inode->location.objectid;
3734 } else {
3735 WARN_ON(1);
3736 return -EINVAL;
3737 }
3738
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003739 path = btrfs_alloc_path();
3740 if (!path)
3741 return -ENOMEM;
3742
Li Zefan33345d012011-04-20 10:31:50 +08003743 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003744 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003745 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003746 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003747 goto out;
3748 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003749
3750 leaf = path->nodes[0];
3751 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3752 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3753 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003755 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003756 goto out;
3757 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003758 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003759
Josef Bacikd49d3282019-12-18 17:20:28 -05003760 /*
3761 * This is a placeholder inode for a subvolume we didn't have a
3762 * reference to at the time of the snapshot creation. In the meantime
3763 * we could have renamed the real subvol link into our snapshot, so
3764 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3765 * Instead simply lookup the dir_index_item for this entry so we can
3766 * remove it. Otherwise we know we have a ref to the root and we can
3767 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3768 */
3769 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003770 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003771 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003772 if (IS_ERR_OR_NULL(di)) {
3773 if (!di)
3774 ret = -ENOENT;
3775 else
3776 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003777 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003778 goto out;
3779 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003780
3781 leaf = path->nodes[0];
3782 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003783 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003784 btrfs_release_path(path);
3785 } else {
3786 ret = btrfs_del_root_ref(trans, objectid,
3787 root->root_key.objectid, dir_ino,
3788 &index, name, name_len);
3789 if (ret) {
3790 btrfs_abort_transaction(trans, ret);
3791 goto out;
3792 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003793 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003794
Lu Fengqi9add2942018-08-01 11:32:26 +08003795 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003796 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003797 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003798 goto out;
3799 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003800
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003801 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003802 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003803 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003804 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003806 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003807out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003808 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003809 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003810}
3811
Misono Tomohiroec42f162018-04-18 11:34:13 +09003812/*
3813 * Helper to check if the subvolume references other subvolumes or if it's
3814 * default.
3815 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003816static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003817{
3818 struct btrfs_fs_info *fs_info = root->fs_info;
3819 struct btrfs_path *path;
3820 struct btrfs_dir_item *di;
3821 struct btrfs_key key;
3822 u64 dir_id;
3823 int ret;
3824
3825 path = btrfs_alloc_path();
3826 if (!path)
3827 return -ENOMEM;
3828
3829 /* Make sure this root isn't set as the default subvol */
3830 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3831 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3832 dir_id, "default", 7, 0);
3833 if (di && !IS_ERR(di)) {
3834 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3835 if (key.objectid == root->root_key.objectid) {
3836 ret = -EPERM;
3837 btrfs_err(fs_info,
3838 "deleting default subvolume %llu is not allowed",
3839 key.objectid);
3840 goto out;
3841 }
3842 btrfs_release_path(path);
3843 }
3844
3845 key.objectid = root->root_key.objectid;
3846 key.type = BTRFS_ROOT_REF_KEY;
3847 key.offset = (u64)-1;
3848
3849 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3850 if (ret < 0)
3851 goto out;
3852 BUG_ON(ret == 0);
3853
3854 ret = 0;
3855 if (path->slots[0] > 0) {
3856 path->slots[0]--;
3857 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3858 if (key.objectid == root->root_key.objectid &&
3859 key.type == BTRFS_ROOT_REF_KEY)
3860 ret = -ENOTEMPTY;
3861 }
3862out:
3863 btrfs_free_path(path);
3864 return ret;
3865}
3866
Nikolay Borisov20a68002018-04-27 14:36:24 +03003867/* Delete all dentries for inodes belonging to the root */
3868static void btrfs_prune_dentries(struct btrfs_root *root)
3869{
3870 struct btrfs_fs_info *fs_info = root->fs_info;
3871 struct rb_node *node;
3872 struct rb_node *prev;
3873 struct btrfs_inode *entry;
3874 struct inode *inode;
3875 u64 objectid = 0;
3876
3877 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3878 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3879
3880 spin_lock(&root->inode_lock);
3881again:
3882 node = root->inode_tree.rb_node;
3883 prev = NULL;
3884 while (node) {
3885 prev = node;
3886 entry = rb_entry(node, struct btrfs_inode, rb_node);
3887
David Sterba37508512018-06-29 10:56:40 +02003888 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003889 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003890 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003891 node = node->rb_right;
3892 else
3893 break;
3894 }
3895 if (!node) {
3896 while (prev) {
3897 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003898 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003899 node = prev;
3900 break;
3901 }
3902 prev = rb_next(prev);
3903 }
3904 }
3905 while (node) {
3906 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003907 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003908 inode = igrab(&entry->vfs_inode);
3909 if (inode) {
3910 spin_unlock(&root->inode_lock);
3911 if (atomic_read(&inode->i_count) > 1)
3912 d_prune_aliases(inode);
3913 /*
3914 * btrfs_drop_inode will have it removed from the inode
3915 * cache when its usage count hits zero.
3916 */
3917 iput(inode);
3918 cond_resched();
3919 spin_lock(&root->inode_lock);
3920 goto again;
3921 }
3922
3923 if (cond_resched_lock(&root->inode_lock))
3924 goto again;
3925
3926 node = rb_next(node);
3927 }
3928 spin_unlock(&root->inode_lock);
3929}
3930
Misono Tomohirof60a2362018-04-18 11:34:52 +09003931int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3932{
3933 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3934 struct btrfs_root *root = BTRFS_I(dir)->root;
3935 struct inode *inode = d_inode(dentry);
3936 struct btrfs_root *dest = BTRFS_I(inode)->root;
3937 struct btrfs_trans_handle *trans;
3938 struct btrfs_block_rsv block_rsv;
3939 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003940 int ret;
3941 int err;
3942
3943 /*
3944 * Don't allow to delete a subvolume with send in progress. This is
3945 * inside the inode lock so the error handling that has to drop the bit
3946 * again is not run concurrently.
3947 */
3948 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003949 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003950 spin_unlock(&dest->root_item_lock);
3951 btrfs_warn(fs_info,
3952 "attempt to delete subvolume %llu during send",
3953 dest->root_key.objectid);
3954 return -EPERM;
3955 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003956 root_flags = btrfs_root_flags(&dest->root_item);
3957 btrfs_set_root_flags(&dest->root_item,
3958 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3959 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003960
3961 down_write(&fs_info->subvol_sem);
3962
3963 err = may_destroy_subvol(dest);
3964 if (err)
3965 goto out_up_write;
3966
3967 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3968 /*
3969 * One for dir inode,
3970 * two for dir entries,
3971 * two for root ref/backref.
3972 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003973 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003974 if (err)
3975 goto out_up_write;
3976
3977 trans = btrfs_start_transaction(root, 0);
3978 if (IS_ERR(trans)) {
3979 err = PTR_ERR(trans);
3980 goto out_release;
3981 }
3982 trans->block_rsv = &block_rsv;
3983 trans->bytes_reserved = block_rsv.size;
3984
3985 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3986
Josef Bacik045d3962019-12-18 17:20:27 -05003987 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003988 if (ret) {
3989 err = ret;
3990 btrfs_abort_transaction(trans, ret);
3991 goto out_end_trans;
3992 }
3993
3994 btrfs_record_root_in_trans(trans, dest);
3995
3996 memset(&dest->root_item.drop_progress, 0,
3997 sizeof(dest->root_item.drop_progress));
3998 dest->root_item.drop_level = 0;
3999 btrfs_set_root_refs(&dest->root_item, 0);
4000
4001 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4002 ret = btrfs_insert_orphan_item(trans,
4003 fs_info->tree_root,
4004 dest->root_key.objectid);
4005 if (ret) {
4006 btrfs_abort_transaction(trans, ret);
4007 err = ret;
4008 goto out_end_trans;
4009 }
4010 }
4011
Lu Fengqid1957792018-05-29 15:01:54 +08004012 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004013 BTRFS_UUID_KEY_SUBVOL,
4014 dest->root_key.objectid);
4015 if (ret && ret != -ENOENT) {
4016 btrfs_abort_transaction(trans, ret);
4017 err = ret;
4018 goto out_end_trans;
4019 }
4020 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004021 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004022 dest->root_item.received_uuid,
4023 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4024 dest->root_key.objectid);
4025 if (ret && ret != -ENOENT) {
4026 btrfs_abort_transaction(trans, ret);
4027 err = ret;
4028 goto out_end_trans;
4029 }
4030 }
4031
4032out_end_trans:
4033 trans->block_rsv = NULL;
4034 trans->bytes_reserved = 0;
4035 ret = btrfs_end_transaction(trans);
4036 if (ret && !err)
4037 err = ret;
4038 inode->i_flags |= S_DEAD;
4039out_release:
4040 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4041out_up_write:
4042 up_write(&fs_info->subvol_sem);
4043 if (err) {
4044 spin_lock(&dest->root_item_lock);
4045 root_flags = btrfs_root_flags(&dest->root_item);
4046 btrfs_set_root_flags(&dest->root_item,
4047 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4048 spin_unlock(&dest->root_item_lock);
4049 } else {
4050 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004051 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004052 ASSERT(dest->send_in_progress == 0);
4053
4054 /* the last ref */
4055 if (dest->ino_cache_inode) {
4056 iput(dest->ino_cache_inode);
4057 dest->ino_cache_inode = NULL;
4058 }
4059 }
4060
4061 return err;
4062}
4063
Chris Mason39279cc2007-06-12 06:35:45 -04004064static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4065{
David Howells2b0143b2015-03-17 22:25:59 +00004066 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004067 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004068 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004069 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004070 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004071
David Sterbab3ae2442012-09-13 16:04:34 -06004072 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004073 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004074 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004075 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004076
Josef Bacikd52be812013-05-29 14:54:47 -04004077 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004078 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004079 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004080
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004081 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004082 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004083 goto out;
4084 }
4085
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004086 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004087 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004088 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004089
Filipe Manana44f714d2016-06-06 16:11:13 +01004090 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4091
Chris Mason39279cc2007-06-12 06:35:45 -04004092 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004093 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4094 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4095 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004096 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004097 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004098 /*
4099 * Propagate the last_unlink_trans value of the deleted dir to
4100 * its parent directory. This is to prevent an unrecoverable
4101 * log tree in the case we do something like this:
4102 * 1) create dir foo
4103 * 2) create snapshot under dir foo
4104 * 3) delete the snapshot
4105 * 4) rmdir foo
4106 * 5) mkdir foo
4107 * 6) fsync foo or some file inside foo
4108 */
4109 if (last_unlink_trans >= trans->transid)
4110 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4111 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004112out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004113 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004114 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004115
Chris Mason39279cc2007-06-12 06:35:45 -04004116 return err;
4117}
4118
Josef Bacikddfae632017-10-19 14:16:02 -04004119/*
4120 * Return this if we need to call truncate_block for the last bit of the
4121 * truncate.
4122 */
4123#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004124
Chris Mason323ac952008-10-01 19:05:46 -04004125/*
Chris Mason39279cc2007-06-12 06:35:45 -04004126 * this can truncate away extent items, csum items and directory items.
4127 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004128 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004129 *
4130 * csum items that cross the new i_size are truncated to the new size
4131 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004132 *
4133 * min_type is the minimum key type to truncate down to. If set to 0, this
4134 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004135 */
Yan, Zheng80825102009-11-12 09:35:36 +00004136int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4137 struct btrfs_root *root,
4138 struct inode *inode,
4139 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004140{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004141 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004142 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004143 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004144 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004145 struct btrfs_key key;
4146 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004147 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004148 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004149 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004150 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004151 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004152 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004153 int found_extent;
4154 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004155 int pending_del_nr = 0;
4156 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004157 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004158 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004159 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004160 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004161 bool be_nice = false;
4162 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004163 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4164 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004165
4166 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004167
Chris Mason28ed1342014-12-17 09:41:04 -08004168 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004169 * For non-free space inodes and non-shareable roots, we want to back
4170 * off from time to time. This means all inodes in subvolume roots,
4171 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004172 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004173 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004174 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004175 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004176
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004177 path = btrfs_alloc_path();
4178 if (!path)
4179 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004180 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004181
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004182 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004183 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4184 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004185
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004186 /*
4187 * We want to drop from the next block forward in case this
4188 * new size is not block aligned since we will be keeping the
4189 * last block of the extent just the way it is.
4190 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004191 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004192 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004193 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004194 }
Yan, Zheng80825102009-11-12 09:35:36 +00004195
Miao Xie16cdcec2011-04-22 18:12:22 +08004196 /*
4197 * This function is also used to drop the items in the log tree before
4198 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004199 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004200 * items.
4201 */
4202 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004203 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004204
Li Zefan33345d012011-04-20 10:31:50 +08004205 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004206 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004207 key.type = (u8)-1;
4208
Chris Mason85e21ba2008-01-29 15:11:36 -05004209search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004210 /*
4211 * with a 16K leaf size and 128MB extents, you can actually queue
4212 * up a huge file in a single leaf. Most of the time that
4213 * bytes_deleted is > 0, it will be huge by the time we get here
4214 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004215 if (be_nice && bytes_deleted > SZ_32M &&
4216 btrfs_should_end_transaction(trans)) {
4217 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004218 goto out;
4219 }
Chris Masond3977122009-01-05 21:25:51 -05004220
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004221 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4222 if (ret < 0)
4223 goto out;
4224
Chris Mason85e21ba2008-01-29 15:11:36 -05004225 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004226 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004227 /* there are no items in the tree for us to truncate, we're
4228 * done
4229 */
Yan, Zheng80825102009-11-12 09:35:36 +00004230 if (path->slots[0] == 0)
4231 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004232 path->slots[0]--;
4233 }
4234
Chris Masond3977122009-01-05 21:25:51 -05004235 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004236 u64 clear_start = 0, clear_len = 0;
4237
Chris Mason39279cc2007-06-12 06:35:45 -04004238 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004239 leaf = path->nodes[0];
4240 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004241 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004242
Li Zefan33345d012011-04-20 10:31:50 +08004243 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004244 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004245
Chris Mason85e21ba2008-01-29 15:11:36 -05004246 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004247 break;
4248
Chris Mason5f39d392007-10-15 16:14:19 -04004249 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004250 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004251 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004252 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004253 extent_type = btrfs_file_extent_type(leaf, fi);
4254 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004255 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004256 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004257
4258 trace_btrfs_truncate_show_fi_regular(
4259 BTRFS_I(inode), leaf, fi,
4260 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004261 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004262 item_end += btrfs_file_extent_ram_bytes(leaf,
4263 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004264
4265 trace_btrfs_truncate_show_fi_inline(
4266 BTRFS_I(inode), leaf, fi, path->slots[0],
4267 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004268 }
Yan008630c2007-11-07 13:31:09 -05004269 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004270 }
Yan, Zheng80825102009-11-12 09:35:36 +00004271 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004272 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004273 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004274 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004275 break;
4276 if (found_key.offset >= new_size)
4277 del_item = 1;
4278 else
4279 del_item = 0;
4280 }
Chris Mason39279cc2007-06-12 06:35:45 -04004281 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004282 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004283 if (found_type != BTRFS_EXTENT_DATA_KEY)
4284 goto delete;
4285
4286 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004287 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004288
4289 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004290 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004291 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004292 u64 orig_num_bytes =
4293 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004294 extent_num_bytes = ALIGN(new_size -
4295 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004296 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004297 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004298 btrfs_set_file_extent_num_bytes(leaf, fi,
4299 extent_num_bytes);
4300 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004301 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004302 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004303 &root->state) &&
4304 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004305 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004306 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004307 } else {
Chris Masondb945352007-10-15 16:15:53 -04004308 extent_num_bytes =
4309 btrfs_file_extent_disk_num_bytes(leaf,
4310 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004311 extent_offset = found_key.offset -
4312 btrfs_file_extent_offset(leaf, fi);
4313
Chris Mason39279cc2007-06-12 06:35:45 -04004314 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004315 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004316 if (extent_start != 0) {
4317 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004318 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004319 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004320 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004321 }
4322 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004323 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004324 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004325 /*
4326 * we can't truncate inline items that have had
4327 * special encodings
4328 */
4329 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004330 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004331 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4332 btrfs_file_extent_compression(leaf, fi) == 0) {
4333 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004334
Josef Bacikddfae632017-10-19 14:16:02 -04004335 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4336 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004337 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004338 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004339 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004340 * We have to bail so the last_size is set to
4341 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004342 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004343 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004344 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004345 } else {
4346 /*
4347 * Inline extents are special, we just treat
4348 * them as a full sector worth in the file
4349 * extent tree just for simplicity sake.
4350 */
4351 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004352 }
Josef Bacikddfae632017-10-19 14:16:02 -04004353
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004354 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004355 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004356 }
Chris Mason179e29e2007-11-01 11:28:41 -04004357delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004358 /*
4359 * We use btrfs_truncate_inode_items() to clean up log trees for
4360 * multiple fsyncs, and in this case we don't want to clear the
4361 * file extent range because it's just the log.
4362 */
4363 if (root == BTRFS_I(inode)->root) {
4364 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4365 clear_start, clear_len);
4366 if (ret) {
4367 btrfs_abort_transaction(trans, ret);
4368 break;
4369 }
4370 }
4371
Josef Bacikddfae632017-10-19 14:16:02 -04004372 if (del_item)
4373 last_size = found_key.offset;
4374 else
4375 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004376 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004377 if (!pending_del_nr) {
4378 /* no pending yet, add ourselves */
4379 pending_del_slot = path->slots[0];
4380 pending_del_nr = 1;
4381 } else if (pending_del_nr &&
4382 path->slots[0] + 1 == pending_del_slot) {
4383 /* hop on the pending chunk */
4384 pending_del_nr++;
4385 pending_del_slot = path->slots[0];
4386 } else {
Chris Masond3977122009-01-05 21:25:51 -05004387 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004388 }
Chris Mason39279cc2007-06-12 06:35:45 -04004389 } else {
4390 break;
4391 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004392 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004393
Miao Xie27cdeb72014-04-02 19:51:05 +08004394 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004395 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004396 struct btrfs_ref ref = { 0 };
4397
Chris Mason28ed1342014-12-17 09:41:04 -08004398 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004399
4400 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4401 extent_start, extent_num_bytes, 0);
4402 ref.real_root = root->root_key.objectid;
4403 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4404 ino, extent_offset);
4405 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004406 if (ret) {
4407 btrfs_abort_transaction(trans, ret);
4408 break;
4409 }
Chris Mason28f75a02015-02-04 06:59:29 -08004410 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004411 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004412 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004413 }
Chris Mason39279cc2007-06-12 06:35:45 -04004414 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004415
Yan, Zheng80825102009-11-12 09:35:36 +00004416 if (found_type == BTRFS_INODE_ITEM_KEY)
4417 break;
4418
4419 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004420 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004421 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004422 if (pending_del_nr) {
4423 ret = btrfs_del_items(trans, root, path,
4424 pending_del_slot,
4425 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004426 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004427 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004428 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004429 }
Yan, Zheng80825102009-11-12 09:35:36 +00004430 pending_del_nr = 0;
4431 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004432 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004433
Chris Mason28f75a02015-02-04 06:59:29 -08004434 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004435 * We can generate a lot of delayed refs, so we need to
4436 * throttle every once and a while and make sure we're
4437 * adding enough space to keep up with the work we are
4438 * generating. Since we hold a transaction here we
4439 * can't flush, and we don't want to FLUSH_LIMIT because
4440 * we could have generated too many delayed refs to
4441 * actually allocate, so just bail if we're short and
4442 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004443 */
Josef Bacik28bad212018-12-03 10:20:38 -05004444 if (should_throttle) {
4445 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4446 BTRFS_RESERVE_NO_FLUSH);
4447 if (ret) {
4448 ret = -EAGAIN;
4449 break;
4450 }
Chris Mason28f75a02015-02-04 06:59:29 -08004451 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004452 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004453 } else {
4454 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004455 }
Chris Mason39279cc2007-06-12 06:35:45 -04004456 }
Yan, Zheng80825102009-11-12 09:35:36 +00004457out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004458 if (ret >= 0 && pending_del_nr) {
4459 int err;
4460
4461 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004462 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004463 if (err) {
4464 btrfs_abort_transaction(trans, err);
4465 ret = err;
4466 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004467 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004468 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4469 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004470 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004471 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004472 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004473 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4474 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004475 }
Chris Mason28ed1342014-12-17 09:41:04 -08004476
Chris Mason39279cc2007-06-12 06:35:45 -04004477 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004478 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004479}
4480
Chris Masona52d9a82007-08-27 16:49:44 -04004481/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304482 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004483 * @inode - inode that we're zeroing
4484 * @from - the offset to start zeroing
4485 * @len - the length to zero, 0 to zero the entire range respective to the
4486 * offset
4487 * @front - zero up to the offset instead of from the offset on
4488 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304489 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004490 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004491 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304492int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004493 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004494{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004495 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004496 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004497 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4498 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004499 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004500 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004501 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004502 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004503 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004504 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304505 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004506 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004507 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004508 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004509 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304510 u64 block_start;
4511 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004512
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004513 if (IS_ALIGNED(offset, blocksize) &&
4514 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004515 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304516
Josef Bacik8b62f872017-10-19 14:15:55 -04004517 block_start = round_down(from, blocksize);
4518 block_end = block_start + blocksize - 1;
4519
Chris Masona52d9a82007-08-27 16:49:44 -04004520
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004521 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4522 blocksize);
4523 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004524 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4525 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004526 /* For nocow case, no need to reserve data space */
4527 only_release_metadata = true;
4528 } else {
4529 goto out;
4530 }
4531 }
4532 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4533 if (ret < 0) {
4534 if (!only_release_metadata)
4535 btrfs_free_reserved_data_space(inode, data_reserved,
4536 block_start, blocksize);
4537 goto out;
4538 }
Chris Mason211c17f2008-05-15 09:13:45 -04004539again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004540 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004541 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004542 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004543 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004544 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004545 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004546 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004547 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004548
Chris Masona52d9a82007-08-27 16:49:44 -04004549 if (!PageUptodate(page)) {
4550 ret = btrfs_readpage(NULL, page);
4551 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004552 if (page->mapping != mapping) {
4553 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004554 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004555 goto again;
4556 }
Chris Masona52d9a82007-08-27 16:49:44 -04004557 if (!PageUptodate(page)) {
4558 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004559 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004560 }
4561 }
Chris Mason211c17f2008-05-15 09:13:45 -04004562 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004563
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304564 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004565 set_page_extent_mapped(page);
4566
Nikolay Borisovc3504372020-06-03 08:55:03 +03004567 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004568 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304569 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004570 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004571 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004572 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004573 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004574 btrfs_put_ordered_extent(ordered);
4575 goto again;
4576 }
4577
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304578 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004579 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4580 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004581
Nikolay Borisovc2566f22020-06-03 08:55:35 +03004582 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004583 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004584 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304585 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004586 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004587 goto out_unlock;
4588 }
4589
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304590 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004591 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304592 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004593 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004594 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304595 memset(kaddr + (block_start - page_offset(page)),
4596 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004597 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304598 memset(kaddr + (block_start - page_offset(page)) + offset,
4599 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004600 flush_dcache_page(page);
4601 kunmap(page);
4602 }
Chris Mason247e7432008-07-17 12:53:51 -04004603 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004604 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004605 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004606
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004607 if (only_release_metadata)
4608 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4609 block_end, EXTENT_NORESERVE, NULL, NULL,
4610 GFP_NOFS);
4611
Chris Mason89642222008-07-24 09:41:53 -04004612out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004613 if (ret) {
4614 if (only_release_metadata)
4615 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4616 blocksize, true);
4617 else
4618 btrfs_delalloc_release_space(inode, data_reserved,
4619 block_start, blocksize, true);
4620 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004621 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004622 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004623 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004624out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004625 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004626 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004627 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004628 return ret;
4629}
4630
Josef Bacik16e75492013-10-22 12:18:51 -04004631static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4632 u64 offset, u64 len)
4633{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004634 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004635 struct btrfs_trans_handle *trans;
4636 int ret;
4637
4638 /*
4639 * Still need to make sure the inode looks like it's been updated so
4640 * that any holes get logged if we fsync.
4641 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004642 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4643 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004644 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4645 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4646 return 0;
4647 }
4648
4649 /*
4650 * 1 - for the one we're dropping
4651 * 1 - for the one we're adding
4652 * 1 - for updating the inode.
4653 */
4654 trans = btrfs_start_transaction(root, 3);
4655 if (IS_ERR(trans))
4656 return PTR_ERR(trans);
4657
4658 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4659 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004660 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004661 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004662 return ret;
4663 }
4664
David Sterbaf85b7372017-01-20 14:54:07 +01004665 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4666 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004667 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004668 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004669 else
4670 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004671 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004672 return ret;
4673}
4674
Josef Bacik695a0d02011-03-04 15:46:53 -05004675/*
4676 * This function puts in dummy file extents for the area we're creating a hole
4677 * for. So if we are truncating this file to a larger size we need to insert
4678 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4679 * the range between oldsize and size
4680 */
Josef Bacika41ad392011-01-31 15:30:16 -05004681int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004682{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004683 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004684 struct btrfs_root *root = BTRFS_I(inode)->root;
4685 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004686 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004687 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004688 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004689 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4690 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004691 u64 last_byte;
4692 u64 cur_offset;
4693 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004694 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004695
Josef Bacika71754f2013-06-17 17:14:39 -04004696 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304697 * If our size started in the middle of a block we need to zero out the
4698 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004699 * expose stale data.
4700 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304701 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004702 if (err)
4703 return err;
4704
Yan Zheng9036c102008-10-30 14:19:41 -04004705 if (size <= hole_start)
4706 return 0;
4707
David Sterbab272ae22020-02-05 19:09:33 +01004708 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004709 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004710 cur_offset = hole_start;
4711 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004712 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004713 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004714 if (IS_ERR(em)) {
4715 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004716 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004717 break;
4718 }
Yan Zheng9036c102008-10-30 14:19:41 -04004719 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004720 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004721 hole_size = last_byte - cur_offset;
4722
Yan, Zheng80825102009-11-12 09:35:36 +00004723 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004724 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004725
Josef Bacik16e75492013-10-22 12:18:51 -04004726 err = maybe_insert_hole(root, inode, cur_offset,
4727 hole_size);
4728 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004729 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004730
4731 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4732 cur_offset, hole_size);
4733 if (err)
4734 break;
4735
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004736 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004737 cur_offset + hole_size - 1, 0);
4738 hole_em = alloc_extent_map();
4739 if (!hole_em) {
4740 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4741 &BTRFS_I(inode)->runtime_flags);
4742 goto next;
4743 }
4744 hole_em->start = cur_offset;
4745 hole_em->len = hole_size;
4746 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004747
Josef Bacik5dc562c2012-08-17 13:14:17 -04004748 hole_em->block_start = EXTENT_MAP_HOLE;
4749 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004750 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004751 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004752 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004753 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004754
4755 while (1) {
4756 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004757 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004758 write_unlock(&em_tree->lock);
4759 if (err != -EEXIST)
4760 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004761 btrfs_drop_extent_cache(BTRFS_I(inode),
4762 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004763 cur_offset +
4764 hole_size - 1, 0);
4765 }
4766 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004767 } else {
4768 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4769 cur_offset, hole_size);
4770 if (err)
4771 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004772 }
Josef Bacik16e75492013-10-22 12:18:51 -04004773next:
Yan Zheng9036c102008-10-30 14:19:41 -04004774 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004775 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004776 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004777 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004778 break;
4779 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004780 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004781 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004782 return err;
4783}
4784
Eric Sandeen3972f262013-01-12 02:57:22 +00004785static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004786{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004787 struct btrfs_root *root = BTRFS_I(inode)->root;
4788 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004789 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004790 loff_t newsize = attr->ia_size;
4791 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004792 int ret;
4793
Eric Sandeen3972f262013-01-12 02:57:22 +00004794 /*
4795 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4796 * special case where we need to update the times despite not having
4797 * these flags set. For all other operations the VFS set these flags
4798 * explicitly if it wants a timestamp update.
4799 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004800 if (newsize != oldsize) {
4801 inode_inc_iversion(inode);
4802 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4803 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004804 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004805 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004806
Josef Bacika41ad392011-01-31 15:30:16 -05004807 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004808 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004809 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004810 * This is to ensure the snapshot captures a fully consistent
4811 * state of this file - if the snapshot captures this expanding
4812 * truncation, it must capture all writes that happened before
4813 * this truncation.
4814 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004815 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004816 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004817 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004818 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004819 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004820 }
Yan, Zheng80825102009-11-12 09:35:36 +00004821
Miao Xief4a2f4c2011-12-14 20:12:01 -05004822 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004823 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004824 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004825 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004826 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004827
4828 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004829 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304830 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004831 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004832 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004833 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004834 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004835
Josef Bacika41ad392011-01-31 15:30:16 -05004836 /*
4837 * We're truncating a file that used to have good data down to
4838 * zero. Make sure it gets into the ordered flush list so that
4839 * any new writes get down to disk quickly.
4840 */
4841 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004842 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4843 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004844
Josef Bacika41ad392011-01-31 15:30:16 -05004845 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004846
David Sterba8e0fa5d2020-06-09 19:21:48 +02004847 /* Disable nonlocked read DIO to avoid the endless truncate */
4848 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004849 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004850 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004851
Filipe Manana213e8c52018-02-06 20:40:31 +00004852 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004853 if (ret && inode->i_nlink) {
4854 int err;
4855
4856 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004857 * Truncate failed, so fix up the in-memory size. We
4858 * adjusted disk_i_size down as we removed extents, so
4859 * wait for disk_i_size to be stable and then update the
4860 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004861 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004862 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004863 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004864 return err;
4865 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004866 }
Yan, Zheng80825102009-11-12 09:35:36 +00004867 }
4868
Josef Bacika41ad392011-01-31 15:30:16 -05004869 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004870}
4871
Chris Mason39279cc2007-06-12 06:35:45 -04004872static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4873{
David Howells2b0143b2015-03-17 22:25:59 +00004874 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004875 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004876 int err;
4877
Li Zefanb83cc962010-12-20 16:04:08 +08004878 if (btrfs_root_readonly(root))
4879 return -EROFS;
4880
Jan Kara31051c82016-05-26 16:55:18 +02004881 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004882 if (err)
4883 return err;
4884
Chris Mason5a3f23d2009-03-31 13:27:11 -04004885 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004886 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004887 if (err)
4888 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004889 }
Yan Zheng9036c102008-10-30 14:19:41 -04004890
Christoph Hellwig10257742010-06-04 11:30:02 +02004891 if (attr->ia_valid) {
4892 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004893 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004894 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004895
Josef Bacik22c44fe2011-11-30 10:45:38 -05004896 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004897 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004898 }
4899
Chris Mason39279cc2007-06-12 06:35:45 -04004900 return err;
4901}
Chris Mason61295eb2008-01-14 16:24:38 -05004902
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004903/*
4904 * While truncating the inode pages during eviction, we get the VFS calling
4905 * btrfs_invalidatepage() against each page of the inode. This is slow because
4906 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4907 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4908 * extent_state structures over and over, wasting lots of time.
4909 *
4910 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4911 * those expensive operations on a per page basis and do only the ordered io
4912 * finishing, while we release here the extent_map and extent_state structures,
4913 * without the excessive merging and splitting.
4914 */
4915static void evict_inode_truncate_pages(struct inode *inode)
4916{
4917 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4918 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4919 struct rb_node *node;
4920
4921 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004922 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004923
4924 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004925 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004926 struct extent_map *em;
4927
Liu Bo07e1ce02018-08-23 03:51:52 +08004928 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004929 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004930 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4931 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004932 remove_extent_mapping(map_tree, em);
4933 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004934 if (need_resched()) {
4935 write_unlock(&map_tree->lock);
4936 cond_resched();
4937 write_lock(&map_tree->lock);
4938 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004939 }
4940 write_unlock(&map_tree->lock);
4941
Filipe Manana6ca07092015-05-26 00:55:42 +01004942 /*
4943 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004944 * We can have ongoing bios started by readahead that have
4945 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004946 * still in progress (unlocked the pages in the bio but did not yet
4947 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004948 * ranges can still be locked and eviction started because before
4949 * submitting those bios, which are executed by a separate task (work
4950 * queue kthread), inode references (inode->i_count) were not taken
4951 * (which would be dropped in the end io callback of each bio).
4952 * Therefore here we effectively end up waiting for those bios and
4953 * anyone else holding locked ranges without having bumped the inode's
4954 * reference count - if we don't do it, when they access the inode's
4955 * io_tree to unlock a range it may be too late, leading to an
4956 * use-after-free issue.
4957 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004958 spin_lock(&io_tree->lock);
4959 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4960 struct extent_state *state;
4961 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004962 u64 start;
4963 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004964 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004965
4966 node = rb_first(&io_tree->state);
4967 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004968 start = state->start;
4969 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004970 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004971 spin_unlock(&io_tree->lock);
4972
David Sterbaff13db42015-12-03 14:30:40 +01004973 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004974
4975 /*
4976 * If still has DELALLOC flag, the extent didn't reach disk,
4977 * and its reserved space won't be freed by delayed_ref.
4978 * So we need to free its reserved space here.
4979 * (Refer to comment in btrfs_invalidatepage, case 2)
4980 *
4981 * Note, end is the bytenr of last byte, so we need + 1 here.
4982 */
Filipe Manana421f0922018-10-12 13:02:48 +01004983 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03004984 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
4985 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004986
Filipe Manana6ca07092015-05-26 00:55:42 +01004987 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004988 EXTENT_LOCKED | EXTENT_DELALLOC |
4989 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4990 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004991
Filipe Manana7064dd52014-08-08 02:47:05 +01004992 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004993 spin_lock(&io_tree->lock);
4994 }
4995 spin_unlock(&io_tree->lock);
4996}
4997
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004998static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04004999 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005000{
5001 struct btrfs_fs_info *fs_info = root->fs_info;
5002 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005003 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005004 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005005 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005006
Josef Bacikd3984c92019-08-01 18:19:37 -04005007 /*
5008 * Eviction should be taking place at some place safe because of our
5009 * delayed iputs. However the normal flushing code will run delayed
5010 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5011 *
5012 * We reserve the delayed_refs_extra here again because we can't use
5013 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5014 * above. We reserve our extra bit here because we generate a ton of
5015 * delayed refs activity by truncating.
5016 *
5017 * If we cannot make our reservation we'll attempt to steal from the
5018 * global reserve, because we really want to be able to free up space.
5019 */
5020 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5021 BTRFS_RESERVE_FLUSH_EVICT);
5022 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005023 /*
5024 * Try to steal from the global reserve if there is space for
5025 * it.
5026 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005027 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5028 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5029 btrfs_warn(fs_info,
5030 "could not allocate space for delete; will truncate on mount");
5031 return ERR_PTR(-ENOSPC);
5032 }
5033 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005034 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005035
5036 trans = btrfs_join_transaction(root);
5037 if (IS_ERR(trans))
5038 return trans;
5039
5040 if (delayed_refs_extra) {
5041 trans->block_rsv = &fs_info->trans_block_rsv;
5042 trans->bytes_reserved = delayed_refs_extra;
5043 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5044 delayed_refs_extra, 1);
5045 }
5046 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005047}
5048
Al Virobd555972010-06-07 11:35:40 -04005049void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005050{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005051 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005052 struct btrfs_trans_handle *trans;
5053 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005054 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005055 int ret;
5056
liubo1abe9b82011-03-24 11:18:59 +00005057 trace_btrfs_inode_evict(inode);
5058
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005059 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005060 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005061 return;
5062 }
5063
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005064 evict_inode_truncate_pages(inode);
5065
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005066 if (inode->i_nlink &&
5067 ((btrfs_root_refs(&root->root_item) != 0 &&
5068 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005069 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005070 goto no_delete;
5071
Omar Sandoval27919062018-05-11 13:13:37 -07005072 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005073 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005074
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005075 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005076
Omar Sandoval7b40b692018-05-11 13:13:33 -07005077 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005078 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005079
Yan, Zheng76dda932009-09-21 16:00:26 -04005080 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005081 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5082 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005083 goto no_delete;
5084 }
5085
Nikolay Borisovaa790212017-01-10 20:35:40 +02005086 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005087 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005088 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005089
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005090 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005091 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005092 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005093 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005094 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005095
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005096 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005097
Yan, Zheng80825102009-11-12 09:35:36 +00005098 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005099 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005100 if (IS_ERR(trans))
5101 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005102
5103 trans->block_rsv = rsv;
5104
Yan, Zhengd68fc572010-05-16 10:49:58 -04005105 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005106 trans->block_rsv = &fs_info->trans_block_rsv;
5107 btrfs_end_transaction(trans);
5108 btrfs_btree_balance_dirty(fs_info);
5109 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5110 goto free_rsv;
5111 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005112 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005113 }
5114
Josef Bacik4ef31a42013-08-13 14:10:08 -04005115 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005116 * Errors here aren't a big deal, it just means we leave orphan items in
5117 * the tree. They will be cleaned up on the next mount. If the inode
5118 * number gets reused, cleanup deletes the orphan item without doing
5119 * anything, and unlink reuses the existing orphan item.
5120 *
5121 * If it turns out that we are dropping too many of these, we might want
5122 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005123 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005124 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005125 if (!IS_ERR(trans)) {
5126 trans->block_rsv = rsv;
5127 btrfs_orphan_del(trans, BTRFS_I(inode));
5128 trans->block_rsv = &fs_info->trans_block_rsv;
5129 btrfs_end_transaction(trans);
5130 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005131
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005132 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005133 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005134 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005135
Omar Sandoval27919062018-05-11 13:13:37 -07005136free_rsv:
5137 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005138no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005139 /*
5140 * If we didn't successfully delete, the orphan item will still be in
5141 * the tree and we'll retry on the next mount. Again, we might also want
5142 * to retry these periodically in the future.
5143 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005144 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005145 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005146}
5147
5148/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005149 * Return the key found in the dir entry in the location pointer, fill @type
5150 * with BTRFS_FT_*, and return 0.
5151 *
Su Yue005d6712018-03-05 17:13:37 +08005152 * If no dir entries were found, returns -ENOENT.
5153 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005154 */
5155static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005156 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005157{
5158 const char *name = dentry->d_name.name;
5159 int namelen = dentry->d_name.len;
5160 struct btrfs_dir_item *di;
5161 struct btrfs_path *path;
5162 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005163 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005164
5165 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005166 if (!path)
5167 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005168
David Sterbaf85b7372017-01-20 14:54:07 +01005169 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5170 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005171 if (IS_ERR_OR_NULL(di)) {
5172 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005173 goto out;
5174 }
Chris Masond3977122009-01-05 21:25:51 -05005175
Chris Mason5f39d392007-10-15 16:14:19 -04005176 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005177 if (location->type != BTRFS_INODE_ITEM_KEY &&
5178 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005179 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005180 btrfs_warn(root->fs_info,
5181"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5182 __func__, name, btrfs_ino(BTRFS_I(dir)),
5183 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005184 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005185 if (!ret)
5186 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005187out:
Chris Mason39279cc2007-06-12 06:35:45 -04005188 btrfs_free_path(path);
5189 return ret;
5190}
5191
5192/*
5193 * when we hit a tree root in a directory, the btrfs part of the inode
5194 * needs to be changed to reflect the root directory of the tree root. This
5195 * is kind of like crossing a mount point.
5196 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005197static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005198 struct inode *dir,
5199 struct dentry *dentry,
5200 struct btrfs_key *location,
5201 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005202{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005203 struct btrfs_path *path;
5204 struct btrfs_root *new_root;
5205 struct btrfs_root_ref *ref;
5206 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005207 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005208 int ret;
5209 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005210
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005211 path = btrfs_alloc_path();
5212 if (!path) {
5213 err = -ENOMEM;
5214 goto out;
5215 }
Chris Mason39279cc2007-06-12 06:35:45 -04005216
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005217 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005218 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5219 key.type = BTRFS_ROOT_REF_KEY;
5220 key.offset = location->objectid;
5221
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005222 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005223 if (ret) {
5224 if (ret < 0)
5225 err = ret;
5226 goto out;
5227 }
Chris Mason39279cc2007-06-12 06:35:45 -04005228
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005229 leaf = path->nodes[0];
5230 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005231 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005232 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5233 goto out;
5234
5235 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5236 (unsigned long)(ref + 1),
5237 dentry->d_name.len);
5238 if (ret)
5239 goto out;
5240
David Sterbab3b4aa72011-04-21 01:20:15 +02005241 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005242
David Sterba56e93572020-05-15 19:35:55 +02005243 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005244 if (IS_ERR(new_root)) {
5245 err = PTR_ERR(new_root);
5246 goto out;
5247 }
5248
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005249 *sub_root = new_root;
5250 location->objectid = btrfs_root_dirid(&new_root->root_item);
5251 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005252 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005253 err = 0;
5254out:
5255 btrfs_free_path(path);
5256 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005257}
5258
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005259static void inode_tree_add(struct inode *inode)
5260{
5261 struct btrfs_root *root = BTRFS_I(inode)->root;
5262 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005263 struct rb_node **p;
5264 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005265 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005266 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005267
Al Viro1d3382cb2010-10-23 15:19:20 -04005268 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005269 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005270 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005271 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005272 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005273 while (*p) {
5274 parent = *p;
5275 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5276
David Sterba37508512018-06-29 10:56:40 +02005277 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005278 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005279 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005280 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005281 else {
5282 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005283 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005284 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005285 RB_CLEAR_NODE(parent);
5286 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005287 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005288 }
5289 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005290 rb_link_node(new, parent, p);
5291 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005292 spin_unlock(&root->inode_lock);
5293}
5294
5295static void inode_tree_del(struct inode *inode)
5296{
5297 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005298 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005299
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005300 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005301 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005302 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005303 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005304 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005305 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005306 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005307
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005308 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005309 spin_lock(&root->inode_lock);
5310 empty = RB_EMPTY_ROOT(&root->inode_tree);
5311 spin_unlock(&root->inode_lock);
5312 if (empty)
5313 btrfs_add_dead_root(root);
5314 }
5315}
5316
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005317
Chris Masone02119d2008-09-05 16:13:11 -04005318static int btrfs_init_locked_inode(struct inode *inode, void *p)
5319{
5320 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005321
5322 inode->i_ino = args->ino;
5323 BTRFS_I(inode)->location.objectid = args->ino;
5324 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5325 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005326 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5327 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005328 return 0;
5329}
5330
5331static int btrfs_find_actor(struct inode *inode, void *opaque)
5332{
5333 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005334
5335 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005336 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005337}
5338
David Sterba0202e832020-05-15 19:35:59 +02005339static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005340 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005341{
5342 struct inode *inode;
5343 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005344 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005345
David Sterba0202e832020-05-15 19:35:59 +02005346 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005347 args.root = root;
5348
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005349 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005350 btrfs_init_locked_inode,
5351 (void *)&args);
5352 return inode;
5353}
5354
David Sterba4c66e0d2019-10-03 19:09:35 +02005355/*
David Sterba0202e832020-05-15 19:35:59 +02005356 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005357 * Path can be preallocated to prevent recursing back to iget through
5358 * allocator. NULL is also valid but may require an additional allocation
5359 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305360 */
David Sterba0202e832020-05-15 19:35:59 +02005361struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005362 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305363{
5364 struct inode *inode;
5365
David Sterba0202e832020-05-15 19:35:59 +02005366 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305367 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005368 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305369
5370 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005371 int ret;
5372
Filipe Manana4222ea72018-10-24 10:13:03 +01005373 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005374 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005375 inode_tree_add(inode);
5376 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005377 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005378 iget_failed(inode);
5379 /*
5380 * ret > 0 can come from btrfs_search_slot called by
5381 * btrfs_read_locked_inode, this means the inode item
5382 * was not found.
5383 */
5384 if (ret > 0)
5385 ret = -ENOENT;
5386 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005387 }
5388 }
5389
Balaji Rao1a54ef82008-07-21 02:01:04 +05305390 return inode;
5391}
5392
David Sterba0202e832020-05-15 19:35:59 +02005393struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005394{
David Sterba0202e832020-05-15 19:35:59 +02005395 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005396}
5397
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005398static struct inode *new_simple_dir(struct super_block *s,
5399 struct btrfs_key *key,
5400 struct btrfs_root *root)
5401{
5402 struct inode *inode = new_inode(s);
5403
5404 if (!inode)
5405 return ERR_PTR(-ENOMEM);
5406
Josef Bacik5c8fd992020-02-14 16:11:43 -05005407 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005408 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005409 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005410
5411 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005412 /*
5413 * We only need lookup, the rest is read-only and there's no inode
5414 * associated with the dentry
5415 */
5416 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005417 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005418 inode->i_fop = &simple_dir_operations;
5419 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005420 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305421 inode->i_atime = inode->i_mtime;
5422 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005423 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005424
5425 return inode;
5426}
5427
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005428static inline u8 btrfs_inode_type(struct inode *inode)
5429{
5430 /*
5431 * Compile-time asserts that generic FT_* types still match
5432 * BTRFS_FT_* types
5433 */
5434 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5435 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5436 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5437 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5438 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5439 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5440 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5441 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5442
5443 return fs_umode_to_ftype(inode->i_mode);
5444}
5445
Chris Mason3de45862008-11-17 21:02:50 -05005446struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005447{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005448 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005449 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005450 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005451 struct btrfs_root *sub_root = root;
5452 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005453 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005454 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005455
5456 if (dentry->d_name.len > BTRFS_NAME_LEN)
5457 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005458
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005459 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005460 if (ret < 0)
5461 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005462
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005463 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005464 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005465 if (IS_ERR(inode))
5466 return inode;
5467
5468 /* Do extra check against inode mode with di_type */
5469 if (btrfs_inode_type(inode) != di_type) {
5470 btrfs_crit(fs_info,
5471"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5472 inode->i_mode, btrfs_inode_type(inode),
5473 di_type);
5474 iput(inode);
5475 return ERR_PTR(-EUCLEAN);
5476 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005477 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005478 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005479
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005480 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005481 &location, &sub_root);
5482 if (ret < 0) {
5483 if (ret != -ENOENT)
5484 inode = ERR_PTR(ret);
5485 else
5486 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5487 } else {
David Sterba0202e832020-05-15 19:35:59 +02005488 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005489 }
Josef Bacik87270022020-01-24 09:32:31 -05005490 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005491 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005492
Julia Lawall34d19ba2011-01-24 19:55:19 +00005493 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005494 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005495 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005496 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005497 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005498 if (ret) {
5499 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005500 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005501 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005502 }
5503
Chris Mason3de45862008-11-17 21:02:50 -05005504 return inode;
5505}
5506
Nick Pigginfe15ce42011-01-07 17:49:23 +11005507static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005508{
5509 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005510 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005511
Li Zefan848cce02012-02-21 17:04:28 +08005512 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005513 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005514
Li Zefan848cce02012-02-21 17:04:28 +08005515 if (inode) {
5516 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005517 if (btrfs_root_refs(&root->root_item) == 0)
5518 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005519
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005520 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005521 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005522 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005523 return 0;
5524}
5525
Chris Mason3de45862008-11-17 21:02:50 -05005526static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005527 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005528{
Al Viro3837d202018-10-10 16:38:27 -04005529 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005530
Al Viro3837d202018-10-10 16:38:27 -04005531 if (inode == ERR_PTR(-ENOENT))
5532 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005533 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005534}
5535
Josef Bacik23b5ec72017-07-24 15:14:25 -04005536/*
5537 * All this infrastructure exists because dir_emit can fault, and we are holding
5538 * the tree lock when doing readdir. For now just allocate a buffer and copy
5539 * our information into that, and then dir_emit from the buffer. This is
5540 * similar to what NFS does, only we don't keep the buffer around in pagecache
5541 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5542 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5543 * tree lock.
5544 */
5545static int btrfs_opendir(struct inode *inode, struct file *file)
5546{
5547 struct btrfs_file_private *private;
5548
5549 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5550 if (!private)
5551 return -ENOMEM;
5552 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5553 if (!private->filldir_buf) {
5554 kfree(private);
5555 return -ENOMEM;
5556 }
5557 file->private_data = private;
5558 return 0;
5559}
5560
5561struct dir_entry {
5562 u64 ino;
5563 u64 offset;
5564 unsigned type;
5565 int name_len;
5566};
5567
5568static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5569{
5570 while (entries--) {
5571 struct dir_entry *entry = addr;
5572 char *name = (char *)(entry + 1);
5573
David Sterba92d32172018-04-16 21:10:14 +02005574 ctx->pos = get_unaligned(&entry->offset);
5575 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5576 get_unaligned(&entry->ino),
5577 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005578 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005579 addr += sizeof(struct dir_entry) +
5580 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005581 ctx->pos++;
5582 }
5583 return 0;
5584}
5585
Al Viro9cdda8d2013-05-22 16:48:09 -04005586static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005587{
Al Viro9cdda8d2013-05-22 16:48:09 -04005588 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005589 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005590 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005591 struct btrfs_dir_item *di;
5592 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005593 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005594 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005595 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005596 struct list_head ins_list;
5597 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005598 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005599 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005600 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005601 char *name_ptr;
5602 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005603 int entries = 0;
5604 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005605 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005606 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005607
Al Viro9cdda8d2013-05-22 16:48:09 -04005608 if (!dir_emit_dots(file, ctx))
5609 return 0;
5610
David Woodhouse49593bf2008-08-17 17:08:36 +01005611 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005612 if (!path)
5613 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005614
Josef Bacik23b5ec72017-07-24 15:14:25 -04005615 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005616 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005617
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005618 INIT_LIST_HEAD(&ins_list);
5619 INIT_LIST_HEAD(&del_list);
5620 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005621
Josef Bacik23b5ec72017-07-24 15:14:25 -04005622again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005623 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005624 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005625 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005626
Chris Mason39279cc2007-06-12 06:35:45 -04005627 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5628 if (ret < 0)
5629 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005630
5631 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005632 struct dir_entry *entry;
5633
Chris Mason5f39d392007-10-15 16:14:19 -04005634 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005635 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005636 if (slot >= btrfs_header_nritems(leaf)) {
5637 ret = btrfs_next_leaf(root, path);
5638 if (ret < 0)
5639 goto err;
5640 else if (ret > 0)
5641 break;
5642 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005643 }
Chris Mason3de45862008-11-17 21:02:50 -05005644
Chris Mason5f39d392007-10-15 16:14:19 -04005645 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5646
5647 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005648 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005649 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005650 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005651 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005652 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005653 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005654 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005655 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005656 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005657 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5658 PAGE_SIZE) {
5659 btrfs_release_path(path);
5660 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5661 if (ret)
5662 goto nopos;
5663 addr = private->filldir_buf;
5664 entries = 0;
5665 total_len = 0;
5666 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005667 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005668
5669 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005670 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005671 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005672 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5673 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005674 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005675 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005676 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005677 put_unaligned(location.objectid, &entry->ino);
5678 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005679 entries++;
5680 addr += sizeof(struct dir_entry) + name_len;
5681 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005682next:
5683 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005684 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005685 btrfs_release_path(path);
5686
5687 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5688 if (ret)
5689 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005690
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005691 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005692 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005693 goto nopos;
5694
Zach Browndb62efb2013-07-11 16:19:42 -07005695 /*
5696 * Stop new entries from being returned after we return the last
5697 * entry.
5698 *
5699 * New directory entries are assigned a strictly increasing
5700 * offset. This means that new entries created during readdir
5701 * are *guaranteed* to be seen in the future by that readdir.
5702 * This has broken buggy programs which operate on names as
5703 * they're returned by readdir. Until we re-use freed offsets
5704 * we have this hack to stop new entries from being returned
5705 * under the assumption that they'll never reach this huge
5706 * offset.
5707 *
5708 * This is being careful not to overflow 32bit loff_t unless the
5709 * last entry requires it because doing so has broken 32bit apps
5710 * in the past.
5711 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005712 if (ctx->pos >= INT_MAX)
5713 ctx->pos = LLONG_MAX;
5714 else
5715 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005716nopos:
5717 ret = 0;
5718err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005719 if (put)
5720 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005721 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005722 return ret;
5723}
5724
Chris Mason39279cc2007-06-12 06:35:45 -04005725/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005726 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005727 * inode changes. But, it is most likely to find the inode in cache.
5728 * FIXME, needs more benchmarking...there are no reasons other than performance
5729 * to keep or drop this code.
5730 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005731static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005732{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005734 struct btrfs_root *root = BTRFS_I(inode)->root;
5735 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005736 int ret;
5737
Josef Bacik72ac3c02012-05-23 14:13:11 -04005738 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005739 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005740
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005741 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005742 if (IS_ERR(trans))
5743 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005744
5745 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005746 if (ret && ret == -ENOSPC) {
5747 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005748 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005749 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005750 if (IS_ERR(trans))
5751 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005752
Chris Mason94b60442010-05-26 11:02:00 -04005753 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005754 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005755 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005756 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005757 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005758
5759 return ret;
5760}
5761
5762/*
5763 * This is a copy of file_update_time. We need this so we can return error on
5764 * ENOSPC for updating the inode in the case of file write and mmap writes.
5765 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005766static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005767 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005768{
Alexander Block2bc5565282012-06-15 09:49:33 +02005769 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005770 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005771
5772 if (btrfs_root_readonly(root))
5773 return -EROFS;
5774
Josef Bacike41f9412012-03-26 09:46:47 -04005775 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005776 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005777 if (flags & S_CTIME)
5778 inode->i_ctime = *now;
5779 if (flags & S_MTIME)
5780 inode->i_mtime = *now;
5781 if (flags & S_ATIME)
5782 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005783 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005784}
5785
Chris Masond352ac62008-09-29 15:18:18 -04005786/*
5787 * find the highest existing sequence number in a directory
5788 * and then set the in-memory index_cnt variable to reflect
5789 * free sequence numbers
5790 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005791static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005792{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005793 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005794 struct btrfs_key key, found_key;
5795 struct btrfs_path *path;
5796 struct extent_buffer *leaf;
5797 int ret;
5798
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005799 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005800 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005801 key.offset = (u64)-1;
5802
5803 path = btrfs_alloc_path();
5804 if (!path)
5805 return -ENOMEM;
5806
5807 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5808 if (ret < 0)
5809 goto out;
5810 /* FIXME: we should be able to handle this */
5811 if (ret == 0)
5812 goto out;
5813 ret = 0;
5814
5815 /*
5816 * MAGIC NUMBER EXPLANATION:
5817 * since we search a directory based on f_pos we have to start at 2
5818 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5819 * else has to start at 2
5820 */
5821 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005822 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005823 goto out;
5824 }
5825
5826 path->slots[0]--;
5827
5828 leaf = path->nodes[0];
5829 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5830
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005831 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005832 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005833 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005834 goto out;
5835 }
5836
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005837 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005838out:
5839 btrfs_free_path(path);
5840 return ret;
5841}
5842
Chris Masond352ac62008-09-29 15:18:18 -04005843/*
5844 * helper to find a free sequence number in a given directory. This current
5845 * code is very simple, later versions will do smarter things in the btree
5846 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005847int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005848{
5849 int ret = 0;
5850
Nikolay Borisov877574e2017-02-20 13:50:33 +02005851 if (dir->index_cnt == (u64)-1) {
5852 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005853 if (ret) {
5854 ret = btrfs_set_inode_index_count(dir);
5855 if (ret)
5856 return ret;
5857 }
Josef Bacikaec74772008-07-24 12:12:38 -04005858 }
5859
Nikolay Borisov877574e2017-02-20 13:50:33 +02005860 *index = dir->index_cnt;
5861 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005862
5863 return ret;
5864}
5865
Chris Masonb0d5d102014-09-08 13:08:51 -07005866static int btrfs_insert_inode_locked(struct inode *inode)
5867{
5868 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005869
5870 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005871 args.root = BTRFS_I(inode)->root;
5872
5873 return insert_inode_locked4(inode,
5874 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5875 btrfs_find_actor, &args);
5876}
5877
Anand Jain19aee8d2017-07-18 17:37:05 +08005878/*
5879 * Inherit flags from the parent inode.
5880 *
5881 * Currently only the compression flags and the cow flags are inherited.
5882 */
5883static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5884{
5885 unsigned int flags;
5886
5887 if (!dir)
5888 return;
5889
5890 flags = BTRFS_I(dir)->flags;
5891
5892 if (flags & BTRFS_INODE_NOCOMPRESS) {
5893 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5894 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5895 } else if (flags & BTRFS_INODE_COMPRESS) {
5896 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5897 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5898 }
5899
5900 if (flags & BTRFS_INODE_NODATACOW) {
5901 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5902 if (S_ISREG(inode->i_mode))
5903 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5904 }
5905
David Sterba7b6a2212018-03-26 18:40:21 +02005906 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005907}
5908
Chris Mason39279cc2007-06-12 06:35:45 -04005909static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5910 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005911 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005912 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005913 u64 ref_objectid, u64 objectid,
5914 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005915{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005916 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005917 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005918 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005919 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005920 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005921 struct btrfs_inode_ref *ref;
5922 struct btrfs_key key[2];
5923 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005924 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005925 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005926 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005927 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005928
Chris Mason5f39d392007-10-15 16:14:19 -04005929 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005930 if (!path)
5931 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005932
Josef Bacik11a19a92019-09-09 10:12:04 -04005933 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005934 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005935 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005936 if (!inode) {
5937 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005938 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005939 }
Chris Mason39279cc2007-06-12 06:35:45 -04005940
Li Zefan581bb052011-04-20 10:06:11 +08005941 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005942 * O_TMPFILE, set link count to 0, so that after this point,
5943 * we fill in an inode item with the correct link count.
5944 */
5945 if (!name)
5946 set_nlink(inode, 0);
5947
5948 /*
Li Zefan581bb052011-04-20 10:06:11 +08005949 * we have to initialize this early, so we can reclaim the inode
5950 * number if we fail afterwards in this function.
5951 */
5952 inode->i_ino = objectid;
5953
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005954 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005955 trace_btrfs_inode_request(dir);
5956
Nikolay Borisov877574e2017-02-20 13:50:33 +02005957 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005958 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005959 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005960 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005961 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005962 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005963 } else if (dir) {
5964 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005965 }
5966 /*
5967 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005968 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005969 * number
5970 */
5971 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005972 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005973 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005974 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005975 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005976
Josef Bacik5dc562c2012-08-17 13:14:17 -04005977 /*
5978 * We could have gotten an inode number from somebody who was fsynced
5979 * and then removed in this same transaction, so let's just set full
5980 * sync since it will be a full sync anyway and this will blow away the
5981 * old info in the log.
5982 */
5983 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5984
Chris Mason9c583092008-01-29 15:15:18 -05005985 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005986 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005987 key[0].offset = 0;
5988
Chris Mason9c583092008-01-29 15:15:18 -05005989 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005990
5991 if (name) {
5992 /*
5993 * Start new inodes with an inode_ref. This is slightly more
5994 * efficient for small numbers of hard links since they will
5995 * be packed into one item. Extended refs will kick in if we
5996 * add more hard links than can fit in the ref item.
5997 */
5998 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005999 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006000 key[1].offset = ref_objectid;
6001
6002 sizes[1] = name_len + sizeof(*ref);
6003 }
Chris Mason9c583092008-01-29 15:15:18 -05006004
Chris Masonb0d5d102014-09-08 13:08:51 -07006005 location = &BTRFS_I(inode)->location;
6006 location->objectid = objectid;
6007 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006008 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006009
6010 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006011 if (ret < 0) {
6012 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006013 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006014 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006015
Chris Masonb9473432009-03-13 11:00:37 -04006016 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006017 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006018 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006019 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006020
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006021 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006022 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306023
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006024 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306025 inode->i_atime = inode->i_mtime;
6026 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006027 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306028
Chris Mason5f39d392007-10-15 16:14:19 -04006029 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6030 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006031 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006032 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006033 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006034
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006035 if (name) {
6036 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6037 struct btrfs_inode_ref);
6038 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6039 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6040 ptr = (unsigned long)(ref + 1);
6041 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6042 }
Chris Mason9c583092008-01-29 15:15:18 -05006043
Chris Mason5f39d392007-10-15 16:14:19 -04006044 btrfs_mark_buffer_dirty(path->nodes[0]);
6045 btrfs_free_path(path);
6046
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006047 btrfs_inherit_iflags(inode, dir);
6048
Al Viro569254b2011-07-24 17:08:40 -04006049 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006050 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006051 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006052 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006053 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6054 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006055 }
6056
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006057 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006058
6059 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006060 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006061
Alexander Block8ea05e32012-07-25 17:35:53 +02006062 btrfs_update_root_times(trans, root);
6063
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006064 ret = btrfs_inode_inherit_props(trans, inode, dir);
6065 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006066 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006067 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006068 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006069
Chris Mason39279cc2007-06-12 06:35:45 -04006070 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006071
6072fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006073 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006074fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006075 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006076 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006077 btrfs_free_path(path);
6078 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006079}
6080
Chris Masond352ac62008-09-29 15:18:18 -04006081/*
6082 * utility function to add 'inode' into 'parent_inode' with
6083 * a give name and a given sequence number.
6084 * if 'add_backref' is true, also insert a backref from the
6085 * inode to the parent directory.
6086 */
Chris Masone02119d2008-09-05 16:13:11 -04006087int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006088 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006089 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006090{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006091 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006092 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006093 struct btrfs_root *root = parent_inode->root;
6094 u64 ino = btrfs_ino(inode);
6095 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006096
Li Zefan33345d012011-04-20 10:31:50 +08006097 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006098 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006099 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006100 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006101 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006102 key.offset = 0;
6103 }
Chris Mason39279cc2007-06-12 06:35:45 -04006104
Li Zefan33345d012011-04-20 10:31:50 +08006105 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006106 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006107 root->root_key.objectid, parent_ino,
6108 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006109 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006110 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6111 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006112 }
6113
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006114 /* Nothing to clean up yet */
6115 if (ret)
6116 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006117
Lu Fengqi684572d2018-08-04 21:10:57 +08006118 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006119 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006120 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006121 goto fail_dir_item;
6122 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006123 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006124 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006125 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006126
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006127 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006128 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006129 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006130 /*
6131 * If we are replaying a log tree, we do not want to update the mtime
6132 * and ctime of the parent directory with the current time, since the
6133 * log replay procedure is responsible for setting them to their correct
6134 * values (the ones it had when the fsync was done).
6135 */
6136 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6137 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6138
6139 parent_inode->vfs_inode.i_mtime = now;
6140 parent_inode->vfs_inode.i_ctime = now;
6141 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006142 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006143 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006144 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006145 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006146
6147fail_dir_item:
6148 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6149 u64 local_index;
6150 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006151 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006152 root->root_key.objectid, parent_ino,
6153 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006154 if (err)
6155 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006156 } else if (add_backref) {
6157 u64 local_index;
6158 int err;
6159
6160 err = btrfs_del_inode_ref(trans, root, name, name_len,
6161 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006162 if (err)
6163 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006164 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006165
6166 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006167 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006168}
6169
6170static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006171 struct btrfs_inode *dir, struct dentry *dentry,
6172 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006173{
Josef Bacika1b075d2010-11-19 20:36:11 +00006174 int err = btrfs_add_link(trans, dir, inode,
6175 dentry->d_name.name, dentry->d_name.len,
6176 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006177 if (err > 0)
6178 err = -EEXIST;
6179 return err;
6180}
6181
Josef Bacik618e21d2007-07-11 10:18:17 -04006182static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006183 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006184{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006185 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006186 struct btrfs_trans_handle *trans;
6187 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006188 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006189 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006190 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006191 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006192
Josef Bacik9ed74f22009-09-11 16:12:44 -04006193 /*
6194 * 2 for inode item and ref
6195 * 2 for dir items
6196 * 1 for xattr if selinux is on
6197 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006198 trans = btrfs_start_transaction(root, 5);
6199 if (IS_ERR(trans))
6200 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006201
Li Zefan581bb052011-04-20 10:06:11 +08006202 err = btrfs_find_free_ino(root, &objectid);
6203 if (err)
6204 goto out_unlock;
6205
Josef Bacikaec74772008-07-24 12:12:38 -04006206 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006207 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6208 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006209 if (IS_ERR(inode)) {
6210 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006211 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006212 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006213 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006214
Casey Schauflerad19db72011-12-15 10:09:07 -05006215 /*
6216 * If the active LSM wants to access the inode during
6217 * d_instantiate it needs these. Smack checks to see
6218 * if the filesystem supports xattrs by looking at the
6219 * ops vector.
6220 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006221 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006222 init_special_inode(inode, inode->i_mode, rdev);
6223
6224 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006225 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006226 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006227
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006228 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6229 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006230 if (err)
6231 goto out_unlock;
6232
6233 btrfs_update_inode(trans, root, inode);
6234 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006235
Josef Bacik618e21d2007-07-11 10:18:17 -04006236out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006237 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006238 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006239 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006240 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006241 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006242 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006243 return err;
6244}
6245
Chris Mason39279cc2007-06-12 06:35:45 -04006246static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006247 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006248{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006249 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006250 struct btrfs_trans_handle *trans;
6251 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006252 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006253 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006254 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006255 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006256
Josef Bacik9ed74f22009-09-11 16:12:44 -04006257 /*
6258 * 2 for inode item and ref
6259 * 2 for dir items
6260 * 1 for xattr if selinux is on
6261 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006262 trans = btrfs_start_transaction(root, 5);
6263 if (IS_ERR(trans))
6264 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006265
Li Zefan581bb052011-04-20 10:06:11 +08006266 err = btrfs_find_free_ino(root, &objectid);
6267 if (err)
6268 goto out_unlock;
6269
Josef Bacikaec74772008-07-24 12:12:38 -04006270 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006271 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6272 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006273 if (IS_ERR(inode)) {
6274 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006275 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006276 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006277 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006278 /*
6279 * If the active LSM wants to access the inode during
6280 * d_instantiate it needs these. Smack checks to see
6281 * if the filesystem supports xattrs by looking at the
6282 * ops vector.
6283 */
6284 inode->i_fop = &btrfs_file_operations;
6285 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006286 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006287
6288 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6289 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006290 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006291
6292 err = btrfs_update_inode(trans, root, inode);
6293 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006294 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006295
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006296 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6297 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006298 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006299 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006300
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006301 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006302 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006303
Chris Mason39279cc2007-06-12 06:35:45 -04006304out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006305 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006306 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006307 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006308 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006309 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006310 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006311 return err;
6312}
6313
6314static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6315 struct dentry *dentry)
6316{
Filipe Manana271dba452016-01-05 16:24:05 +00006317 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006318 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006319 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006320 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006321 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006322 int err;
6323 int drop_inode = 0;
6324
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006325 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006326 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006327 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006328
Mark Fashehf1863732012-08-08 11:32:27 -07006329 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006330 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006331
Nikolay Borisov877574e2017-02-20 13:50:33 +02006332 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006333 if (err)
6334 goto fail;
6335
Yan, Zhenga22285a2010-05-16 10:48:46 -04006336 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006337 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006338 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006339 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006340 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006341 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006342 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006343 if (IS_ERR(trans)) {
6344 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006345 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006346 goto fail;
6347 }
Chris Mason5f39d392007-10-15 16:14:19 -04006348
Miao Xie67de1172013-12-26 13:07:06 +08006349 /* There are several dir indexes for this inode, clear the cache. */
6350 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006351 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006352 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006353 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006354 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006355 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006356
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006357 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6358 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006359
Yan, Zhenga5719522009-09-24 09:17:31 -04006360 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006361 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006362 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006363 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006364 int ret;
6365
Yan, Zhenga5719522009-09-24 09:17:31 -04006366 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006367 if (err)
6368 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006369 if (inode->i_nlink == 1) {
6370 /*
6371 * If new hard link count is 1, it's a file created
6372 * with open(2) O_TMPFILE flag.
6373 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006374 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006375 if (err)
6376 goto fail;
6377 }
Al Viro08c422c2011-12-23 07:58:13 -05006378 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006379 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6380 true, NULL);
6381 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6382 err = btrfs_commit_transaction(trans);
6383 trans = NULL;
6384 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006385 }
Chris Mason39279cc2007-06-12 06:35:45 -04006386
Chris Mason1832a6d2007-12-21 16:27:21 -05006387fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006388 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006389 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006390 if (drop_inode) {
6391 inode_dec_link_count(inode);
6392 iput(inode);
6393 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006394 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006395 return err;
6396}
6397
Al Viro18bb1db2011-07-26 01:41:39 -04006398static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006399{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006400 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006401 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006402 struct btrfs_trans_handle *trans;
6403 struct btrfs_root *root = BTRFS_I(dir)->root;
6404 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006405 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006406 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006407
Josef Bacik9ed74f22009-09-11 16:12:44 -04006408 /*
6409 * 2 items for inode and ref
6410 * 2 items for dir items
6411 * 1 for xattr if selinux is on
6412 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006413 trans = btrfs_start_transaction(root, 5);
6414 if (IS_ERR(trans))
6415 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006416
Li Zefan581bb052011-04-20 10:06:11 +08006417 err = btrfs_find_free_ino(root, &objectid);
6418 if (err)
6419 goto out_fail;
6420
Josef Bacikaec74772008-07-24 12:12:38 -04006421 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006422 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6423 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006424 if (IS_ERR(inode)) {
6425 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006426 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006427 goto out_fail;
6428 }
Chris Mason5f39d392007-10-15 16:14:19 -04006429
Chris Masonb0d5d102014-09-08 13:08:51 -07006430 /* these must be set before we unlock the inode */
6431 inode->i_op = &btrfs_dir_inode_operations;
6432 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006433
Eric Paris2a7dba32011-02-01 11:05:39 -05006434 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006435 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006436 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006437
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006438 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006439 err = btrfs_update_inode(trans, root, inode);
6440 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006441 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006442
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006443 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6444 dentry->d_name.name,
6445 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006446 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006447 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006448
Al Viro1e2e5472018-05-04 08:23:01 -04006449 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006450
6451out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006452 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006453 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006454 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006455 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006456 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006457 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006458 return err;
6459}
6460
Chris Masonc8b97812008-10-29 14:49:59 -04006461static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006462 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006463 size_t pg_offset, u64 extent_offset,
6464 struct btrfs_file_extent_item *item)
6465{
6466 int ret;
6467 struct extent_buffer *leaf = path->nodes[0];
6468 char *tmp;
6469 size_t max_size;
6470 unsigned long inline_size;
6471 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006472 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006473
6474 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006475 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006476 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6477 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006478 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006479 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006480 if (!tmp)
6481 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006482 ptr = btrfs_file_extent_inline_start(item);
6483
6484 read_extent_buffer(leaf, tmp, ptr, inline_size);
6485
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006486 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006487 ret = btrfs_decompress(compress_type, tmp, page,
6488 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006489
6490 /*
6491 * decompression code contains a memset to fill in any space between the end
6492 * of the uncompressed data and the end of max_size in case the decompressed
6493 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6494 * the end of an inline extent and the beginning of the next block, so we
6495 * cover that region here.
6496 */
6497
6498 if (max_size + pg_offset < PAGE_SIZE) {
6499 char *map = kmap(page);
6500 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6501 kunmap(page);
6502 }
Chris Masonc8b97812008-10-29 14:49:59 -04006503 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006504 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006505}
6506
Omar Sandoval39b07b52019-12-02 17:34:23 -08006507/**
6508 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6509 * @inode: file to search in
6510 * @page: page to read extent data into if the extent is inline
6511 * @pg_offset: offset into @page to copy to
6512 * @start: file offset
6513 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006514 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006515 * This returns the first &struct extent_map which overlaps with the given
6516 * range, reading it from the B-tree and caching it if necessary. Note that
6517 * there may be more extents which overlap the given range after the returned
6518 * extent_map.
6519 *
6520 * If @page is not NULL and the extent is inline, this also reads the extent
6521 * data directly into the page and marks the extent up to date in the io_tree.
6522 *
6523 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006524 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006525struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006526 struct page *page, size_t pg_offset,
6527 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006528{
David Sterba3ffbd682018-06-29 10:56:42 +02006529 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006530 int ret;
6531 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006532 u64 extent_start = 0;
6533 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006534 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006535 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006536 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006537 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006538 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006539 struct extent_buffer *leaf;
6540 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006541 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006542 struct extent_map_tree *em_tree = &inode->extent_tree;
6543 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006544
Chris Mason890871b2009-09-02 16:24:52 -04006545 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006546 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006547 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006548
Chris Masona52d9a82007-08-27 16:49:44 -04006549 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006550 if (em->start > start || em->start + em->len <= start)
6551 free_extent_map(em);
6552 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006553 free_extent_map(em);
6554 else
6555 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006556 }
David Sterba172ddd62011-04-21 00:48:27 +02006557 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006558 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006559 err = -ENOMEM;
6560 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006561 }
Chris Masond1310b22008-01-24 16:13:08 -05006562 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006563 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006564 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006565 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006566
Liu Bobee6ec82018-08-17 05:05:28 +08006567 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006568 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006569 err = -ENOMEM;
6570 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006571 }
6572
Liu Bobee6ec82018-08-17 05:05:28 +08006573 /* Chances are we'll be called again, so go ahead and do readahead */
6574 path->reada = READA_FORWARD;
6575
Liu Boe49aabd2018-08-25 13:47:09 +08006576 /*
6577 * Unless we're going to uncompress the inline extent, no sleep would
6578 * happen.
6579 */
6580 path->leave_spinning = 1;
6581
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006582 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006583 if (ret < 0) {
6584 err = ret;
6585 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006586 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006587 if (path->slots[0] == 0)
6588 goto not_found;
6589 path->slots[0]--;
6590 }
6591
Chris Mason5f39d392007-10-15 16:14:19 -04006592 leaf = path->nodes[0];
6593 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006594 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006595 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006596 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006597 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006598 /*
6599 * If we backup past the first extent we want to move forward
6600 * and see if there is an extent in front of us, otherwise we'll
6601 * say there is a hole for our whole search range which can
6602 * cause problems.
6603 */
6604 extent_end = start;
6605 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006606 }
6607
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006608 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006609 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006610 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006611 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6612 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006613 /* Only regular file could have regular/prealloc extent */
6614 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6615 ret = -EUCLEAN;
6616 btrfs_crit(fs_info,
6617 "regular/prealloc extent found for non-regular inode %llu",
6618 btrfs_ino(inode));
6619 goto out;
6620 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006621 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6622 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006623 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006624 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6625 path->slots[0],
6626 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006627 }
Josef Bacik25a50342013-10-14 12:08:38 -04006628next:
Yan Zheng9036c102008-10-30 14:19:41 -04006629 if (start >= extent_end) {
6630 path->slots[0]++;
6631 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6632 ret = btrfs_next_leaf(root, path);
6633 if (ret < 0) {
6634 err = ret;
6635 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006636 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006637 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006638 }
Yan Zheng9036c102008-10-30 14:19:41 -04006639 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006640 }
Yan Zheng9036c102008-10-30 14:19:41 -04006641 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6642 if (found_key.objectid != objectid ||
6643 found_key.type != BTRFS_EXTENT_DATA_KEY)
6644 goto not_found;
6645 if (start + len <= found_key.offset)
6646 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006647 if (start > found_key.offset)
6648 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006649
6650 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006651 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006652 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006653 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006654 em->block_start = EXTENT_MAP_HOLE;
6655 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006656 }
6657
Omar Sandoval39b07b52019-12-02 17:34:23 -08006658 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006659
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006660 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6661 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006662 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006663 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006664 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006665 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006666 size_t size;
6667 size_t extent_offset;
6668 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006669
Omar Sandoval39b07b52019-12-02 17:34:23 -08006670 if (!page)
Yan689f9342007-10-29 11:41:07 -04006671 goto out;
Yan689f9342007-10-29 11:41:07 -04006672
Qu Wenruoe41ca582018-06-06 15:41:49 +08006673 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006674 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006675 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6676 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006677 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006678 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006679 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006680 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006681 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006682
6683 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006684 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006685 if (btrfs_file_extent_compression(leaf, item) !=
6686 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006687 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006688 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006689 if (ret) {
6690 err = ret;
6691 goto out;
6692 }
Chris Masonc8b97812008-10-29 14:49:59 -04006693 } else {
6694 map = kmap(page);
6695 read_extent_buffer(leaf, map + pg_offset, ptr,
6696 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006697 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006698 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006699 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006700 copy_size);
6701 }
Chris Masonc8b97812008-10-29 14:49:59 -04006702 kunmap(page);
6703 }
Chris Mason179e29e2007-11-01 11:28:41 -04006704 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006705 }
Chris Masond1310b22008-01-24 16:13:08 -05006706 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006707 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006708 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006709 }
6710not_found:
6711 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006712 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006713 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006714 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006715insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006716 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006717 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006718 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006719 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6720 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006721 err = -EIO;
6722 goto out;
6723 }
Chris Masond1310b22008-01-24 16:13:08 -05006724
6725 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006726 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006727 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006728 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006729out:
Liu Boc6414282018-08-23 07:36:17 +08006730 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006731
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006732 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006733
Chris Masona52d9a82007-08-27 16:49:44 -04006734 if (err) {
6735 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006736 return ERR_PTR(err);
6737 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006738 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006739 return em;
6740}
6741
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006742struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006743 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006744{
6745 struct extent_map *em;
6746 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006747 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006748 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006749 u64 delalloc_len;
6750 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006751 int err = 0;
6752
Omar Sandoval39b07b52019-12-02 17:34:23 -08006753 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006754 if (IS_ERR(em))
6755 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006756 /*
6757 * If our em maps to:
6758 * - a hole or
6759 * - a pre-alloc extent,
6760 * there might actually be delalloc bytes behind it.
6761 */
6762 if (em->block_start != EXTENT_MAP_HOLE &&
6763 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6764 return em;
6765 else
6766 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006767
6768 /* check to see if we've wrapped (len == -1 or similar) */
6769 end = start + len;
6770 if (end < start)
6771 end = (u64)-1;
6772 else
6773 end -= 1;
6774
6775 em = NULL;
6776
6777 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006778 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006779 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006780 delalloc_end = delalloc_start + delalloc_len;
6781 if (delalloc_end < delalloc_start)
6782 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006783
6784 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006785 * We didn't find anything useful, return the original results from
6786 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006787 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006788 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006789 em = hole_em;
6790 hole_em = NULL;
6791 goto out;
6792 }
6793
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006794 /*
6795 * Adjust the delalloc_start to make sure it doesn't go backwards from
6796 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006797 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006798 delalloc_start = max(start, delalloc_start);
6799 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006800
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006801 if (delalloc_len > 0) {
6802 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006803 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006804 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006805
David Sterba172ddd62011-04-21 00:48:27 +02006806 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006807 if (!em) {
6808 err = -ENOMEM;
6809 goto out;
6810 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006811
6812 ASSERT(hole_em);
6813 /*
6814 * When btrfs_get_extent can't find anything it returns one
6815 * huge hole
6816 *
6817 * Make sure what it found really fits our range, and adjust to
6818 * make sure it is based on the start from the caller
6819 */
6820 if (hole_end <= start || hole_em->start > end) {
6821 free_extent_map(hole_em);
6822 hole_em = NULL;
6823 } else {
6824 hole_start = max(hole_em->start, start);
6825 hole_len = hole_end - hole_start;
6826 }
6827
6828 if (hole_em && delalloc_start > hole_start) {
6829 /*
6830 * Our hole starts before our delalloc, so we have to
6831 * return just the parts of the hole that go until the
6832 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006833 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006834 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006835 em->start = hole_start;
6836 em->orig_start = hole_start;
6837 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006838 * Don't adjust block start at all, it is fixed at
6839 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006840 */
6841 em->block_start = hole_em->block_start;
6842 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006843 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6844 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006845 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006846 /*
6847 * Hole is out of passed range or it starts after
6848 * delalloc range
6849 */
6850 em->start = delalloc_start;
6851 em->len = delalloc_len;
6852 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006853 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006854 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006855 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006856 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006857 return hole_em;
6858 }
6859out:
6860
6861 free_extent_map(hole_em);
6862 if (err) {
6863 free_extent_map(em);
6864 return ERR_PTR(err);
6865 }
6866 return em;
6867}
6868
Nikolay Borisov64f54182020-06-03 08:55:31 +03006869static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006870 const u64 start,
6871 const u64 len,
6872 const u64 orig_start,
6873 const u64 block_start,
6874 const u64 block_len,
6875 const u64 orig_block_len,
6876 const u64 ram_bytes,
6877 const int type)
6878{
6879 struct extent_map *em = NULL;
6880 int ret;
6881
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006882 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03006883 em = create_io_em(inode, start, len, orig_start, block_start,
6884 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08006885 BTRFS_COMPRESS_NONE, /* compress_type */
6886 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006887 if (IS_ERR(em))
6888 goto out;
6889 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03006890 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
6891 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006892 if (ret) {
6893 if (em) {
6894 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03006895 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006896 }
6897 em = ERR_PTR(ret);
6898 }
6899 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006900
6901 return em;
6902}
6903
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006904static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006905 u64 start, u64 len)
6906{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006907 struct btrfs_root *root = inode->root;
6908 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04006909 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006910 struct btrfs_key ins;
6911 u64 alloc_hint;
6912 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006913
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006914 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006915 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006916 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006917 if (ret)
6918 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006919
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006920 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006921 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006922 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006923 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006924 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006925 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
6926 1);
Josef Bacik00361582013-08-14 14:02:47 -04006927
Josef Bacik4b46fce2010-05-23 11:00:55 -04006928 return em;
6929}
6930
Chris Mason46bfbb52010-05-26 11:04:10 -04006931/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006932 * Check if we can do nocow write into the range [@offset, @offset + @len)
6933 *
6934 * @offset: File offset
6935 * @len: The length to write, will be updated to the nocow writeable
6936 * range
6937 * @orig_start: (optional) Return the original file offset of the file extent
6938 * @orig_len: (optional) Return the original on-disk length of the file extent
6939 * @ram_bytes: (optional) Return the ram_bytes of the file extent
6940 *
6941 * This function will flush ordered extents in the range to ensure proper
6942 * nocow checks for (nowait == false) case.
6943 *
6944 * Return:
6945 * >0 and update @len if we can do nocow write
6946 * 0 if we can't do nocow write
6947 * <0 if error happened
6948 *
6949 * NOTE: This only checks the file extents, caller is responsible to wait for
6950 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04006951 */
Josef Bacik00361582013-08-14 14:02:47 -04006952noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006953 u64 *orig_start, u64 *orig_block_len,
6954 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006955{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006956 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006957 struct btrfs_path *path;
6958 int ret;
6959 struct extent_buffer *leaf;
6960 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006961 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006962 struct btrfs_file_extent_item *fi;
6963 struct btrfs_key key;
6964 u64 disk_bytenr;
6965 u64 backref_offset;
6966 u64 extent_end;
6967 u64 num_bytes;
6968 int slot;
6969 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006970 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006971
Chris Mason46bfbb52010-05-26 11:04:10 -04006972 path = btrfs_alloc_path();
6973 if (!path)
6974 return -ENOMEM;
6975
David Sterbaf85b7372017-01-20 14:54:07 +01006976 ret = btrfs_lookup_file_extent(NULL, root, path,
6977 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006978 if (ret < 0)
6979 goto out;
6980
6981 slot = path->slots[0];
6982 if (ret == 1) {
6983 if (slot == 0) {
6984 /* can't find the item, must cow */
6985 ret = 0;
6986 goto out;
6987 }
6988 slot--;
6989 }
6990 ret = 0;
6991 leaf = path->nodes[0];
6992 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006993 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006994 key.type != BTRFS_EXTENT_DATA_KEY) {
6995 /* not our file or wrong item type, must cow */
6996 goto out;
6997 }
6998
6999 if (key.offset > offset) {
7000 /* Wrong offset, must cow */
7001 goto out;
7002 }
7003
7004 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7005 found_type = btrfs_file_extent_type(leaf, fi);
7006 if (found_type != BTRFS_FILE_EXTENT_REG &&
7007 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7008 /* not a regular extent, must cow */
7009 goto out;
7010 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007011
7012 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7013 goto out;
7014
Miao Xiee77751a2013-12-27 21:11:50 +08007015 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7016 if (extent_end <= offset)
7017 goto out;
7018
Chris Mason46bfbb52010-05-26 11:04:10 -04007019 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007020 if (disk_bytenr == 0)
7021 goto out;
7022
7023 if (btrfs_file_extent_compression(leaf, fi) ||
7024 btrfs_file_extent_encryption(leaf, fi) ||
7025 btrfs_file_extent_other_encoding(leaf, fi))
7026 goto out;
7027
Ethan Lien78d42952018-05-17 14:58:29 +08007028 /*
7029 * Do the same check as in btrfs_cross_ref_exist but without the
7030 * unnecessary search.
7031 */
7032 if (btrfs_file_extent_generation(leaf, fi) <=
7033 btrfs_root_last_snapshot(&root->root_item))
7034 goto out;
7035
Chris Mason46bfbb52010-05-26 11:04:10 -04007036 backref_offset = btrfs_file_extent_offset(leaf, fi);
7037
Josef Bacik7ee9e442013-06-21 16:37:03 -04007038 if (orig_start) {
7039 *orig_start = key.offset - backref_offset;
7040 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7041 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7042 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007043
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007044 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007045 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007046
7047 num_bytes = min(offset + *len, extent_end) - offset;
7048 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7049 u64 range_end;
7050
Jeff Mahoneyda170662016-06-15 09:22:56 -04007051 range_end = round_up(offset + num_bytes,
7052 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007053 ret = test_range_bit(io_tree, offset, range_end,
7054 EXTENT_DELALLOC, 0, NULL);
7055 if (ret) {
7056 ret = -EAGAIN;
7057 goto out;
7058 }
7059 }
7060
Josef Bacik1bda19e2013-10-18 12:10:36 -04007061 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007062
7063 /*
7064 * look for other files referencing this extent, if we
7065 * find any we must cow
7066 */
Josef Bacik00361582013-08-14 14:02:47 -04007067
Liu Boe4c3b2d2017-01-30 12:25:28 -08007068 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007069 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007070 if (ret) {
7071 ret = 0;
7072 goto out;
7073 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007074
7075 /*
7076 * adjust disk_bytenr and num_bytes to cover just the bytes
7077 * in this extent we are about to write. If there
7078 * are any csums in that range we have to cow in order
7079 * to keep the csums correct
7080 */
7081 disk_bytenr += backref_offset;
7082 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007083 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7084 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007085 /*
7086 * all of the above have passed, it is safe to overwrite this extent
7087 * without cow
7088 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007089 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007090 ret = 1;
7091out:
7092 btrfs_free_path(path);
7093 return ret;
7094}
7095
Josef Bacikeb838e72012-07-31 16:28:48 -04007096static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
David Sterba55e20bd2020-06-09 19:56:06 +02007097 struct extent_state **cached_state, int writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007098{
7099 struct btrfs_ordered_extent *ordered;
7100 int ret = 0;
7101
7102 while (1) {
7103 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007104 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007105 /*
7106 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007107 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007108 * extents in this range.
7109 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007110 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007111 lockend - lockstart + 1);
7112
7113 /*
7114 * We need to make sure there are no buffered pages in this
7115 * range either, we could have raced between the invalidate in
7116 * generic_file_direct_write and locking the extent. The
7117 * invalidate needs to happen so that reads after a write do not
7118 * get stale data.
7119 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007120 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007121 (!writing || !filemap_range_has_page(inode->i_mapping,
7122 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007123 break;
7124
7125 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007126 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007127
7128 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007129 /*
7130 * If we are doing a DIO read and the ordered extent we
7131 * found is for a buffered write, we can not wait for it
7132 * to complete and retry, because if we do so we can
7133 * deadlock with concurrent buffered writes on page
7134 * locks. This happens only if our DIO read covers more
7135 * than one extent map, if at this point has already
7136 * created an ordered extent for a previous extent map
7137 * and locked its range in the inode's io tree, and a
7138 * concurrent write against that previous extent map's
7139 * range and this range started (we unlock the ranges
7140 * in the io tree only when the bios complete and
7141 * buffered writes always lock pages before attempting
7142 * to lock range in the io tree).
7143 */
7144 if (writing ||
7145 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7146 btrfs_start_ordered_extent(inode, ordered, 1);
7147 else
7148 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007149 btrfs_put_ordered_extent(ordered);
7150 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007151 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007152 * We could trigger writeback for this range (and wait
7153 * for it to complete) and then invalidate the pages for
7154 * this range (through invalidate_inode_pages2_range()),
7155 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007156 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007157 * triggered a readahead) on a page lock due to an
7158 * ordered dio extent we created before but did not have
7159 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007160 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007161 * ordered extent to complete while holding a lock on
7162 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007163 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007164 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007165 }
7166
Filipe Mananaade77022016-02-18 14:28:55 +00007167 if (ret)
7168 break;
7169
Josef Bacikeb838e72012-07-31 16:28:48 -04007170 cond_resched();
7171 }
7172
7173 return ret;
7174}
7175
Liu Bo6f9994d2017-01-31 07:50:22 -08007176/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007177static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7178 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007179 u64 block_len, u64 orig_block_len,
7180 u64 ram_bytes, int compress_type,
7181 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007182{
7183 struct extent_map_tree *em_tree;
7184 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007185 int ret;
7186
Liu Bo6f9994d2017-01-31 07:50:22 -08007187 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7188 type == BTRFS_ORDERED_COMPRESSED ||
7189 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007190 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007191
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007192 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007193 em = alloc_extent_map();
7194 if (!em)
7195 return ERR_PTR(-ENOMEM);
7196
7197 em->start = start;
7198 em->orig_start = orig_start;
7199 em->len = len;
7200 em->block_len = block_len;
7201 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007202 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007203 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007204 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007205 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007206 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007207 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007208 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007209 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7210 em->compress_type = compress_type;
7211 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007212
7213 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007214 btrfs_drop_extent_cache(inode, em->start,
7215 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007216 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007217 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007218 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007219 /*
7220 * The caller has taken lock_extent(), who could race with us
7221 * to add em?
7222 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007223 } while (ret == -EEXIST);
7224
7225 if (ret) {
7226 free_extent_map(em);
7227 return ERR_PTR(ret);
7228 }
7229
Liu Bo6f9994d2017-01-31 07:50:22 -08007230 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007231 return em;
7232}
7233
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007234
David Sterba55e20bd2020-06-09 19:56:06 +02007235static int btrfs_get_blocks_direct_read(struct extent_map *em,
7236 struct buffer_head *bh_result,
7237 struct inode *inode,
7238 u64 start, u64 len)
7239{
7240 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7241
7242 if (em->block_start == EXTENT_MAP_HOLE ||
7243 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7244 return -ENOENT;
7245
7246 len = min(len, em->len - (start - em->start));
7247
7248 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7249 inode->i_blkbits;
7250 bh_result->b_size = len;
7251 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7252 set_buffer_mapped(bh_result);
7253
7254 return 0;
7255}
7256
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007257static int btrfs_get_blocks_direct_write(struct extent_map **map,
David Sterba55e20bd2020-06-09 19:56:06 +02007258 struct buffer_head *bh_result,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007259 struct inode *inode,
7260 struct btrfs_dio_data *dio_data,
7261 u64 start, u64 len)
7262{
7263 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7264 struct extent_map *em = *map;
7265 int ret = 0;
7266
7267 /*
7268 * We don't allocate a new extent in the following cases
7269 *
7270 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7271 * existing extent.
7272 * 2) The extent is marked as PREALLOC. We're good to go here and can
7273 * just use the extent.
7274 *
7275 */
7276 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7277 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7278 em->block_start != EXTENT_MAP_HOLE)) {
7279 int type;
7280 u64 block_start, orig_start, orig_block_len, ram_bytes;
7281
7282 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7283 type = BTRFS_ORDERED_PREALLOC;
7284 else
7285 type = BTRFS_ORDERED_NOCOW;
7286 len = min(len, em->len - (start - em->start));
7287 block_start = em->block_start + (start - em->start);
7288
7289 if (can_nocow_extent(inode, start, &len, &orig_start,
7290 &orig_block_len, &ram_bytes) == 1 &&
7291 btrfs_inc_nocow_writers(fs_info, block_start)) {
7292 struct extent_map *em2;
7293
Nikolay Borisov64f54182020-06-03 08:55:31 +03007294 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007295 orig_start, block_start,
7296 len, orig_block_len,
7297 ram_bytes, type);
7298 btrfs_dec_nocow_writers(fs_info, block_start);
7299 if (type == BTRFS_ORDERED_PREALLOC) {
7300 free_extent_map(em);
7301 *map = em = em2;
7302 }
7303
7304 if (em2 && IS_ERR(em2)) {
7305 ret = PTR_ERR(em2);
7306 goto out;
7307 }
7308 /*
7309 * For inode marked NODATACOW or extent marked PREALLOC,
7310 * use the existing or preallocated extent, so does not
7311 * need to adjust btrfs_space_info's bytes_may_use.
7312 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007313 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007314 goto skip_cow;
7315 }
7316 }
7317
7318 /* this will cow the extent */
David Sterba55e20bd2020-06-09 19:56:06 +02007319 len = bh_result->b_size;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007320 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007321 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007322 if (IS_ERR(em)) {
7323 ret = PTR_ERR(em);
7324 goto out;
7325 }
7326
7327 len = min(len, em->len - (start - em->start));
7328
7329skip_cow:
David Sterba55e20bd2020-06-09 19:56:06 +02007330 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7331 inode->i_blkbits;
7332 bh_result->b_size = len;
7333 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7334 set_buffer_mapped(bh_result);
7335
7336 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7337 set_buffer_new(bh_result);
7338
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007339 /*
7340 * Need to update the i_size under the extent lock so buffered
7341 * readers will get the updated i_size when we unlock.
7342 */
David Sterba55e20bd2020-06-09 19:56:06 +02007343 if (!dio_data->overwrite && start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007344 i_size_write(inode, start + len);
7345
David Sterba55e20bd2020-06-09 19:56:06 +02007346 WARN_ON(dio_data->reserve < len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007347 dio_data->reserve -= len;
David Sterba55e20bd2020-06-09 19:56:06 +02007348 dio_data->unsubmitted_oe_range_end = start + len;
7349 current->journal_info = dio_data;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007350out:
7351 return ret;
7352}
7353
David Sterba55e20bd2020-06-09 19:56:06 +02007354static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7355 struct buffer_head *bh_result, int create)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007356{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007357 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007358 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007359 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307360 struct btrfs_dio_data *dio_data = NULL;
David Sterba55e20bd2020-06-09 19:56:06 +02007361 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007362 u64 lockstart, lockend;
David Sterba55e20bd2020-06-09 19:56:06 +02007363 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007364 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007365
David Sterba55e20bd2020-06-09 19:56:06 +02007366 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007367 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007368
Josef Bacikc3298612012-08-03 16:49:19 -04007369 lockstart = start;
7370 lockend = start + len - 1;
7371
David Sterba55e20bd2020-06-09 19:56:06 +02007372 if (current->journal_info) {
7373 /*
7374 * Need to pull our outstanding extents and set journal_info to NULL so
7375 * that anything that needs to check if there's a transaction doesn't get
7376 * confused.
7377 */
7378 dio_data = current->journal_info;
7379 current->journal_info = NULL;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007380 }
7381
Josef Bacikeb838e72012-07-31 16:28:48 -04007382 /*
7383 * If this errors out it's because we couldn't invalidate pagecache for
7384 * this range and we need to fallback to buffered.
7385 */
David Sterba55e20bd2020-06-09 19:56:06 +02007386 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7387 create)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007388 ret = -ENOTBLK;
7389 goto err;
7390 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007391
Omar Sandoval39b07b52019-12-02 17:34:23 -08007392 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007393 if (IS_ERR(em)) {
7394 ret = PTR_ERR(em);
7395 goto unlock_err;
7396 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007397
7398 /*
7399 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7400 * io. INLINE is special, and we could probably kludge it in here, but
7401 * it's still buffered so for safety lets just fall back to the generic
7402 * buffered path.
7403 *
7404 * For COMPRESSED we _have_ to read the entire extent in so we can
7405 * decompress it, so there will be buffering required no matter what we
7406 * do, so go ahead and fallback to buffered.
7407 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007408 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007409 * to buffered IO. Don't blame me, this is the price we pay for using
7410 * the generic code.
7411 */
7412 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7413 em->block_start == EXTENT_MAP_INLINE) {
7414 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007415 ret = -ENOTBLK;
7416 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007417 }
7418
David Sterba55e20bd2020-06-09 19:56:06 +02007419 if (create) {
7420 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7421 dio_data, start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007422 if (ret < 0)
7423 goto unlock_err;
David Sterba55e20bd2020-06-09 19:56:06 +02007424
7425 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7426 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007427 } else {
David Sterba55e20bd2020-06-09 19:56:06 +02007428 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7429 start, len);
7430 /* Can be negative only if we read from a hole */
7431 if (ret < 0) {
7432 ret = 0;
7433 free_extent_map(em);
7434 goto unlock_err;
7435 }
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007436 /*
7437 * We need to unlock only the end area that we aren't using.
7438 * The rest is going to be unlocked by the endio routine.
7439 */
David Sterba55e20bd2020-06-09 19:56:06 +02007440 lockstart = start + bh_result->b_size;
7441 if (lockstart < lockend) {
7442 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7443 lockstart, lockend, &cached_state);
7444 } else {
7445 free_extent_state(cached_state);
7446 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007447 }
7448
Josef Bacik4b46fce2010-05-23 11:00:55 -04007449 free_extent_map(em);
7450
7451 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007452
7453unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007454 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7455 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007456err:
David Sterba55e20bd2020-06-09 19:56:06 +02007457 if (dio_data)
7458 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007459 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007460}
7461
Omar Sandoval769b4f22020-04-16 14:46:22 -07007462static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7463{
7464 /*
7465 * This implies a barrier so that stores to dio_bio->bi_status before
7466 * this and loads of dio_bio->bi_status after this are fully ordered.
7467 */
7468 if (!refcount_dec_and_test(&dip->refs))
7469 return;
7470
7471 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007472 __endio_write_update_ordered(BTRFS_I(dip->inode),
7473 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007474 dip->bytes,
7475 !dip->dio_bio->bi_status);
7476 } else {
7477 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7478 dip->logical_offset,
7479 dip->logical_offset + dip->bytes - 1);
7480 }
7481
David Sterba55e20bd2020-06-09 19:56:06 +02007482 dio_end_io(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007483 kfree(dip);
7484}
7485
Omar Sandoval77d5d682020-04-16 14:46:25 -07007486static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7487 int mirror_num,
7488 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007489{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007490 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007491 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007492 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007493
Mike Christie37226b22016-06-05 14:31:52 -05007494 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007495
Omar Sandoval5c047a62020-04-16 14:46:24 -07007496 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007497 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007498 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007499
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007500 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007501 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007502 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007503 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007504 return ret;
7505}
7506
Omar Sandoval769b4f22020-04-16 14:46:22 -07007507static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7508 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007509 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007510{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007511 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7512 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007513 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007514 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7515 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007516 struct bio_vec bvec;
7517 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007518 u64 start = io_bio->logical;
7519 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007520 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007521
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007522 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7523 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307524
Liu Bo17347ce2017-05-15 15:33:27 -07007525 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7526 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007527 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007528 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007529 if (uptodate &&
7530 (!csum || !check_data_csum(inode, io_bio, icsum,
7531 bvec.bv_page, pgoff,
7532 start, sectorsize))) {
7533 clean_io_failure(fs_info, failure_tree, io_tree,
7534 start, bvec.bv_page,
7535 btrfs_ino(BTRFS_I(inode)),
7536 pgoff);
7537 } else {
7538 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007539
Omar Sandoval77d5d682020-04-16 14:46:25 -07007540 status = btrfs_submit_read_repair(inode,
7541 &io_bio->bio,
7542 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007543 bvec.bv_page, pgoff,
7544 start,
7545 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007546 io_bio->mirror_num,
7547 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007548 if (status)
7549 err = status;
7550 }
7551 start += sectorsize;
7552 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307553 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307554 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007555 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007556 return err;
7557}
7558
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007559static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007560 const u64 offset, const u64 bytes,
7561 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007562{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007563 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007564 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007565 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007566 u64 ordered_offset = offset;
7567 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007568 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007569
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007570 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007571 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007572 else
Qu Wenruo524272602017-03-08 10:25:52 +08007573 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007574
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007575 while (ordered_offset < offset + bytes) {
7576 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007577 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007578 &ordered_offset,
7579 ordered_bytes,
7580 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007581 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7582 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007583 btrfs_queue_work(wq, &ordered->work);
7584 }
7585 /*
7586 * If btrfs_dec_test_ordered_pending does not find any ordered
7587 * extent in the range, we can exit.
7588 */
7589 if (ordered_offset == last_offset)
7590 return;
7591 /*
7592 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007593 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007594 */
7595 if (ordered_offset < offset + bytes) {
7596 ordered_bytes = offset + bytes - ordered_offset;
7597 ordered = NULL;
7598 }
Chris Mason163cf092010-11-28 19:56:33 -05007599 }
Filipe Manana14543772015-11-24 16:23:54 +00007600}
7601
David Sterbad0ee3932018-03-08 14:35:48 +01007602static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007603 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007604{
Josef Bacikc6100a42017-05-05 11:57:13 -04007605 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007606 blk_status_t ret;
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007607 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007608 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007609 return 0;
7610}
7611
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007612static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007613{
7614 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007615 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007616
Miao Xie8b110e32014-09-12 18:44:03 +08007617 if (err)
7618 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007619 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007620 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7621 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007622 (unsigned long long)bio->bi_iter.bi_sector,
7623 bio->bi_iter.bi_size, err);
7624
Omar Sandoval769b4f22020-04-16 14:46:22 -07007625 if (bio_op(bio) == REQ_OP_READ) {
7626 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007627 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007628 }
7629
Omar Sandoval769b4f22020-04-16 14:46:22 -07007630 if (err)
7631 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007632
Miao Xiee65e1532010-11-22 03:04:43 +00007633 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007634 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007635}
7636
David Sterbad0ee3932018-03-08 14:35:48 +01007637static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7638 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007639{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007640 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007641 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007642 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007643 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007644
Liu Bo4c274bc2017-11-01 17:19:27 -06007645 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007646 if (async_submit)
7647 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7648
Josef Bacik5fd02042012-05-02 14:00:54 -04007649 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007650 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007651 if (ret)
7652 goto err;
7653 }
Miao Xiee65e1532010-11-22 03:04:43 +00007654
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007655 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007656 goto map;
7657
7658 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007659 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7660 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007661 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007662 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007663 } else if (write) {
7664 /*
7665 * If we aren't doing async submit, calculate the csum of the
7666 * bio now.
7667 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007668 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007669 if (ret)
7670 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007671 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007672 u64 csum_offset;
7673
7674 csum_offset = file_offset - dip->logical_offset;
7675 csum_offset >>= inode->i_sb->s_blocksize_bits;
7676 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7677 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007678 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007679map:
Chris Mason08635ba2019-07-10 12:28:14 -07007680 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007681err:
Miao Xiee65e1532010-11-22 03:04:43 +00007682 return ret;
7683}
7684
Omar Sandovalc36cac22020-04-16 14:46:13 -07007685/*
7686 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7687 * or ordered extents whether or not we submit any bios.
7688 */
7689static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7690 struct inode *inode,
7691 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007692{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007693 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007694 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7695 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007696 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007697
Omar Sandoval85879572020-04-16 14:46:21 -07007698 dip_size = sizeof(*dip);
7699 if (!write && csum) {
7700 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7701 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7702 size_t nblocks;
7703
7704 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7705 dip_size += csum_size * nblocks;
7706 }
7707
7708 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007709 if (!dip)
7710 return NULL;
7711
Omar Sandovalc36cac22020-04-16 14:46:13 -07007712 dip->inode = inode;
7713 dip->logical_offset = file_offset;
7714 dip->bytes = dio_bio->bi_iter.bi_size;
7715 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007716 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007717 refcount_set(&dip->refs, 1);
David Sterba55e20bd2020-06-09 19:56:06 +02007718
7719 if (write) {
7720 struct btrfs_dio_data *dio_data = current->journal_info;
7721
7722 /*
7723 * Setting range start and end to the same value means that
7724 * no cleanup will happen in btrfs_direct_IO
7725 */
7726 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7727 dip->bytes;
7728 dio_data->unsubmitted_oe_range_start =
7729 dio_data->unsubmitted_oe_range_end;
7730 }
Omar Sandovalc36cac22020-04-16 14:46:13 -07007731 return dip;
7732}
7733
David Sterba55e20bd2020-06-09 19:56:06 +02007734static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7735 loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007736{
7737 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007738 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007739 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007740 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7741 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007742 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007743 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007744 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007745 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007746 u64 submit_len;
7747 int clone_offset = 0;
7748 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307749 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007750 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007751 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007752
Omar Sandovalc36cac22020-04-16 14:46:13 -07007753 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7754 if (!dip) {
7755 if (!write) {
7756 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7757 file_offset + dio_bio->bi_iter.bi_size - 1);
7758 }
7759 dio_bio->bi_status = BLK_STS_RESOURCE;
David Sterba55e20bd2020-06-09 19:56:06 +02007760 dio_end_io(dio_bio);
7761 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04007762 }
7763
Omar Sandoval85879572020-04-16 14:46:21 -07007764 if (!write && csum) {
7765 /*
7766 * Load the csums up front to reduce csum tree searches and
7767 * contention when submitting bios.
7768 */
7769 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7770 dip->csums);
7771 if (status != BLK_STS_OK)
7772 goto out_err;
7773 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007774
Omar Sandoval769b4f22020-04-16 14:46:22 -07007775 start_sector = dio_bio->bi_iter.bi_sector;
7776 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007777
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007778 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007779 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7780 start_sector << 9, submit_len,
7781 &geom);
7782 if (ret) {
7783 status = errno_to_blk_status(ret);
7784 goto out_err;
7785 }
7786 ASSERT(geom.len <= INT_MAX);
7787
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007788 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007789
Liu Bo725130b2017-05-16 09:51:39 -07007790 /*
7791 * This will never fail as it's passing GPF_NOFS and
7792 * the allocation is backed by btrfs_bioset.
7793 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007794 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007795 bio->bi_private = dip;
7796 bio->bi_end_io = btrfs_end_dio_bio;
7797 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007798
Liu Bo725130b2017-05-16 09:51:39 -07007799 ASSERT(submit_len >= clone_len);
7800 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007801
Liu Bo725130b2017-05-16 09:51:39 -07007802 /*
7803 * Increase the count before we submit the bio so we know
7804 * the end IO handler won't happen before we increase the
7805 * count. Otherwise, the dip might get freed before we're
7806 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007807 *
7808 * We transfer the initial reference to the last bio, so we
7809 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007810 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007811 if (submit_len > 0) {
7812 refcount_inc(&dip->refs);
7813 /*
7814 * If we are submitting more than one bio, submit them
7815 * all asynchronously. The exception is RAID 5 or 6, as
7816 * asynchronous checksums make it difficult to collect
7817 * full stripe writes.
7818 */
7819 if (!raid56)
7820 async_submit = 1;
7821 }
Miao Xiee65e1532010-11-22 03:04:43 +00007822
David Sterbad0ee3932018-03-08 14:35:48 +01007823 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007824 async_submit);
7825 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007826 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007827 if (submit_len > 0)
7828 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007829 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007830 }
Liu Bo725130b2017-05-16 09:51:39 -07007831
7832 clone_offset += clone_len;
7833 start_sector += clone_len >> 9;
7834 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007835 } while (submit_len > 0);
David Sterba55e20bd2020-06-09 19:56:06 +02007836 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007837
Miao Xiee65e1532010-11-22 03:04:43 +00007838out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007839 dip->dio_bio->bi_status = status;
7840 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007841}
7842
David Sterbaf4c48b42020-06-09 19:19:27 +02007843static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7844 const struct iov_iter *iter, loff_t offset)
7845{
7846 int seg;
7847 int i;
7848 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7849 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007850
David Sterbaf4c48b42020-06-09 19:19:27 +02007851 if (offset & blocksize_mask)
7852 goto out;
7853
7854 if (iov_iter_alignment(iter) & blocksize_mask)
7855 goto out;
7856
7857 /* If this is a write we don't need to check anymore */
7858 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7859 return 0;
7860 /*
7861 * Check to make sure we don't have duplicate iov_base's in this
7862 * iovec, if so return EINVAL, otherwise we'll get csum errors
7863 * when reading back.
7864 */
7865 for (seg = 0; seg < iter->nr_segs; seg++) {
7866 for (i = seg + 1; i < iter->nr_segs; i++) {
7867 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7868 goto out;
7869 }
7870 }
7871 retval = 0;
7872out:
7873 return retval;
7874}
7875
David Sterba55e20bd2020-06-09 19:56:06 +02007876static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007877{
7878 struct file *file = iocb->ki_filp;
7879 struct inode *inode = file->f_mapping->host;
7880 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba55e20bd2020-06-09 19:56:06 +02007881 struct btrfs_dio_data dio_data = { 0 };
David Sterbaf4c48b42020-06-09 19:19:27 +02007882 struct extent_changeset *data_reserved = NULL;
7883 loff_t offset = iocb->ki_pos;
7884 size_t count = 0;
David Sterba55e20bd2020-06-09 19:56:06 +02007885 int flags = 0;
7886 bool wakeup = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007887 bool relock = false;
7888 ssize_t ret;
7889
7890 if (check_direct_IO(fs_info, iter, offset))
7891 return 0;
7892
David Sterba55e20bd2020-06-09 19:56:06 +02007893 inode_dio_begin(inode);
7894
7895 /*
7896 * The generic stuff only does filemap_write_and_wait_range, which
7897 * isn't enough if we've written compressed pages to this area, so
7898 * we need to flush the dirty pages again to make absolutely sure
7899 * that any outstanding dirty pages are on disk.
7900 */
David Sterbaf4c48b42020-06-09 19:19:27 +02007901 count = iov_iter_count(iter);
David Sterba55e20bd2020-06-09 19:56:06 +02007902 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7903 &BTRFS_I(inode)->runtime_flags))
7904 filemap_fdatawrite_range(inode->i_mapping, offset,
7905 offset + count - 1);
7906
David Sterbaf4c48b42020-06-09 19:19:27 +02007907 if (iov_iter_rw(iter) == WRITE) {
7908 /*
7909 * If the write DIO is beyond the EOF, we need update
7910 * the isize, but it is protected by i_mutex. So we can
7911 * not unlock the i_mutex at this case.
7912 */
7913 if (offset + count <= inode->i_size) {
David Sterba55e20bd2020-06-09 19:56:06 +02007914 dio_data.overwrite = 1;
David Sterbaf4c48b42020-06-09 19:19:27 +02007915 inode_unlock(inode);
7916 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007917 }
David Sterba55e20bd2020-06-09 19:56:06 +02007918 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
7919 offset, count);
7920 if (ret)
7921 goto out;
7922
7923 /*
7924 * We need to know how many extents we reserved so that we can
7925 * do the accounting properly if we go over the number we
7926 * originally calculated. Abuse current->journal_info for this.
7927 */
7928 dio_data.reserve = round_up(count,
7929 fs_info->sectorsize);
7930 dio_data.unsubmitted_oe_range_start = (u64)offset;
7931 dio_data.unsubmitted_oe_range_end = (u64)offset;
7932 current->journal_info = &dio_data;
David Sterbaf4c48b42020-06-09 19:19:27 +02007933 down_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007934 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7935 &BTRFS_I(inode)->runtime_flags)) {
7936 inode_dio_end(inode);
7937 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7938 wakeup = false;
David Sterbaf4c48b42020-06-09 19:19:27 +02007939 }
7940
David Sterba55e20bd2020-06-09 19:56:06 +02007941 ret = __blockdev_direct_IO(iocb, inode,
7942 fs_info->fs_devices->latest_bdev,
7943 iter, btrfs_get_blocks_direct, NULL,
7944 btrfs_submit_direct, flags);
David Sterbaf4c48b42020-06-09 19:19:27 +02007945 if (iov_iter_rw(iter) == WRITE) {
7946 up_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007947 current->journal_info = NULL;
7948 if (ret < 0 && ret != -EIOCBQUEUED) {
7949 if (dio_data.reserve)
7950 btrfs_delalloc_release_space(inode, data_reserved,
7951 offset, dio_data.reserve, true);
7952 /*
7953 * On error we might have left some ordered extents
7954 * without submitting corresponding bios for them, so
7955 * cleanup them up to avoid other tasks getting them
7956 * and waiting for them to complete forever.
7957 */
7958 if (dio_data.unsubmitted_oe_range_start <
7959 dio_data.unsubmitted_oe_range_end)
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007960 __endio_write_update_ordered(BTRFS_I(inode),
David Sterba55e20bd2020-06-09 19:56:06 +02007961 dio_data.unsubmitted_oe_range_start,
7962 dio_data.unsubmitted_oe_range_end -
7963 dio_data.unsubmitted_oe_range_start,
7964 false);
7965 } else if (ret >= 0 && (size_t)ret < count)
7966 btrfs_delalloc_release_space(inode, data_reserved,
7967 offset, count - (size_t)ret, true);
7968 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
David Sterbaf4c48b42020-06-09 19:19:27 +02007969 }
7970out:
David Sterba55e20bd2020-06-09 19:56:06 +02007971 if (wakeup)
7972 inode_dio_end(inode);
David Sterbaf4c48b42020-06-09 19:19:27 +02007973 if (relock)
7974 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02007975
David Sterbaf4c48b42020-06-09 19:19:27 +02007976 extent_changeset_free(data_reserved);
7977 return ret;
7978}
Chris Mason16432982008-04-10 10:23:21 -04007979
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007980static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02007981 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007982{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007983 int ret;
7984
Christoph Hellwig45dd0522020-05-23 09:30:14 +02007985 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007986 if (ret)
7987 return ret;
7988
David Sterba2135fb92017-06-23 04:09:57 +02007989 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007990}
7991
Chris Mason9ebefb182007-06-15 13:50:00 -04007992int btrfs_readpage(struct file *file, struct page *page)
7993{
David Sterba71ad38b2020-02-05 19:09:35 +01007994 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007995}
Chris Mason1832a6d2007-12-21 16:27:21 -05007996
Chris Mason39279cc2007-06-12 06:35:45 -04007997static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
7998{
Josef Bacikbe7bd732015-10-22 15:05:09 -04007999 struct inode *inode = page->mapping->host;
8000 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008001
8002 if (current->flags & PF_MEMALLOC) {
8003 redirty_page_for_writepage(wbc, page);
8004 unlock_page(page);
8005 return 0;
8006 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008007
8008 /*
8009 * If we are under memory pressure we will call this directly from the
8010 * VM, we need to make sure we have the inode referenced for the ordered
8011 * extent. If not just return like we didn't do anything.
8012 */
8013 if (!igrab(inode)) {
8014 redirty_page_for_writepage(wbc, page);
8015 return AOP_WRITEPAGE_ACTIVATE;
8016 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008017 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008018 btrfs_add_delayed_iput(inode);
8019 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008020}
Chris Mason39279cc2007-06-12 06:35:45 -04008021
Eric Sandeen48a3b632013-04-25 20:41:01 +00008022static int btrfs_writepages(struct address_space *mapping,
8023 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008024{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008025 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008026}
8027
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008028static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008029{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008030 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008031}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008032
Chris Masone6dcd2d2008-07-17 12:53:50 -04008033static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008034{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008035 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008036 if (ret == 1)
8037 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008038 return ret;
8039}
Chris Mason39279cc2007-06-12 06:35:45 -04008040
Chris Masone6dcd2d2008-07-17 12:53:50 -04008041static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8042{
Chris Mason98509cf2008-09-11 15:51:43 -04008043 if (PageWriteback(page) || PageDirty(page))
8044 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008045 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008046}
8047
Roman Gushchinf8e66082020-03-04 16:57:35 -08008048#ifdef CONFIG_MIGRATION
8049static int btrfs_migratepage(struct address_space *mapping,
8050 struct page *newpage, struct page *page,
8051 enum migrate_mode mode)
8052{
8053 int ret;
8054
8055 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8056 if (ret != MIGRATEPAGE_SUCCESS)
8057 return ret;
8058
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008059 if (page_has_private(page))
8060 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008061
8062 if (PagePrivate2(page)) {
8063 ClearPagePrivate2(page);
8064 SetPagePrivate2(newpage);
8065 }
8066
8067 if (mode != MIGRATE_SYNC_NO_COPY)
8068 migrate_page_copy(newpage, page);
8069 else
8070 migrate_page_states(newpage, page);
8071 return MIGRATEPAGE_SUCCESS;
8072}
8073#endif
8074
Lukas Czernerd47992f2013-05-21 23:17:23 -04008075static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8076 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008077{
Josef Bacik5fd02042012-05-02 14:00:54 -04008078 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008079 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008080 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008081 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008082 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008083 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308084 u64 start;
8085 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008086 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008087
Chris Mason8b62b722009-09-02 16:53:46 -04008088 /*
8089 * we have the page locked, so new writeback can't start,
8090 * and the dirty bit won't be cleared while we are here.
8091 *
8092 * Wait for IO on this page so that we can safely clear
8093 * the PagePrivate2 bit and do ordered accounting
8094 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008095 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008096
Josef Bacik5fd02042012-05-02 14:00:54 -04008097 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008098 if (offset) {
8099 btrfs_releasepage(page, GFP_NOFS);
8100 return;
8101 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008102
8103 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008104 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308105again:
8106 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008107 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308108 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008109 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008110 end = min(page_end,
8111 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008112 /*
8113 * IO on this page will never be started, so we need
8114 * to account for any ordered extents now
8115 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008116 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308117 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008118 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008119 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008120 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008121 /*
8122 * whoever cleared the private bit is responsible
8123 * for the finish_ordered_io
8124 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008125 if (TestClearPagePrivate2(page)) {
8126 struct btrfs_ordered_inode_tree *tree;
8127 u64 new_len;
8128
8129 tree = &BTRFS_I(inode)->ordered_tree;
8130
8131 spin_lock_irq(&tree->lock);
8132 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308133 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008134 if (new_len < ordered->truncated_len)
8135 ordered->truncated_len = new_len;
8136 spin_unlock_irq(&tree->lock);
8137
8138 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308139 start,
8140 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008141 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008142 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008143 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008144 if (!inode_evicting) {
8145 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308146 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008147 &cached_state);
8148 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308149
8150 start = end + 1;
8151 if (start < page_end)
8152 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008153 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008154
Qu Wenruob9d0b382015-09-29 10:35:16 +08008155 /*
8156 * Qgroup reserved space handler
8157 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008158 * 1) Already written to disk or ordered extent already submitted
8159 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8160 * Qgroup will be handled by its qgroup_record then.
8161 * btrfs_qgroup_free_data() call will do nothing here.
8162 *
8163 * 2) Not written to disk yet
8164 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8165 * bit of its io_tree, and free the qgroup reserved data space.
8166 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008167 */
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03008168 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008169 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008170 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008171 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8172 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008173 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008174
8175 __btrfs_releasepage(page, GFP_NOFS);
8176 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008177
Chris Mason4a096752008-07-21 10:29:44 -04008178 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008179 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008180}
8181
Chris Mason9ebefb182007-06-15 13:50:00 -04008182/*
8183 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8184 * called from a page fault handler when a page is first dirtied. Hence we must
8185 * be careful to check for EOF conditions here. We set the page up correctly
8186 * for a written page which means we get ENOSPC checking when writing into
8187 * holes and correct delalloc and unwritten extent mapping on filesystems that
8188 * support these features.
8189 *
8190 * We are not allowed to take the i_mutex here so we have to play games to
8191 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008192 * truncate_setsize() writes the inode size before removing pages, once we have
8193 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008194 * beyond EOF, then the page is guaranteed safe against truncation until we
8195 * unlock the page.
8196 */
Souptick Joardera528a242018-06-06 19:54:44 +05308197vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008198{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008199 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008200 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008201 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008202 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8203 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008204 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008205 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008206 char *kaddr;
8207 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008208 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308209 vm_fault_t ret;
8210 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008211 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308212 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008213 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008214 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308215 u64 end;
8216
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008217 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008218
Jan Karab2b5ef52012-06-12 16:20:45 +02008219 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008220 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008221 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308222 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008223
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308224 /*
8225 * Reserving delalloc space after obtaining the page lock can lead to
8226 * deadlock. For example, if a dirty page is locked by this function
8227 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8228 * dirty page write out, then the btrfs_writepage() function could
8229 * end up waiting indefinitely to get a lock on the page currently
8230 * being processed by btrfs_page_mkwrite() function.
8231 */
Souptick Joardera528a242018-06-06 19:54:44 +05308232 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308233 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308234 if (!ret2) {
8235 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008236 reserved = 1;
8237 }
Souptick Joardera528a242018-06-06 19:54:44 +05308238 if (ret2) {
8239 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008240 if (reserved)
8241 goto out;
8242 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008243 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008244
Nick Piggin56a76f82009-03-31 15:23:23 -07008245 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008246again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008247 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008248 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008249
Chris Mason9ebefb182007-06-15 13:50:00 -04008250 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008251 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008252 /* page got truncated out from underneath us */
8253 goto out_unlock;
8254 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008255 wait_on_page_writeback(page);
8256
David Sterbaff13db42015-12-03 14:30:40 +01008257 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008258 set_page_extent_mapped(page);
8259
Chris Masoneb84ae02008-07-17 13:53:27 -04008260 /*
8261 * we can't set the delalloc bits if there are pending ordered
8262 * extents. Drop our locks and wait for them to finish
8263 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008264 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8265 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008266 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008267 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008268 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008269 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008270 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008271 btrfs_put_ordered_extent(ordered);
8272 goto again;
8273 }
8274
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008275 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008276 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008277 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008278 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308279 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008280 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008281 page_start, PAGE_SIZE - reserved_space,
8282 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308283 }
8284 }
8285
Josef Bacikfbf19082009-10-01 17:10:23 -04008286 /*
Liu Bo54160342016-12-13 12:15:19 -08008287 * page_mkwrite gets called when the page is firstly dirtied after it's
8288 * faulted in, but write(2) could also dirty a page and set delalloc
8289 * bits, thus in this case for space account reason, we still need to
8290 * clear any delalloc bits within this page range since we have to
8291 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008292 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308293 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008294 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8295 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008296
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008297 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008298 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308299 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008300 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008301 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008302 ret = VM_FAULT_SIGBUS;
8303 goto out_unlock;
8304 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008305
8306 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008307 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008308 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008309 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008310 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008311
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008312 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008313 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008314 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008315 flush_dcache_page(page);
8316 kunmap(page);
8317 }
Chris Mason247e7432008-07-17 12:53:51 -04008318 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008319 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008320 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008321
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008322 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008323 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008324 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008325
David Sterbae43bbe52017-12-12 21:43:52 +01008326 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008327
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008328 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8329 sb_end_pagefault(inode->i_sb);
8330 extent_changeset_free(data_reserved);
8331 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008332
8333out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008334 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008335out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008336 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008337 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008338 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008339out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008340 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008341 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008342 return ret;
8343}
8344
Filipe Manana213e8c52018-02-06 20:40:31 +00008345static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008346{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008347 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008348 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008349 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008350 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008351 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008352 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008353 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008354
Filipe Manana213e8c52018-02-06 20:40:31 +00008355 if (!skip_writeback) {
8356 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8357 (u64)-1);
8358 if (ret)
8359 return ret;
8360 }
Chris Mason39279cc2007-06-12 06:35:45 -04008361
Josef Bacikfcb80c22011-05-03 10:40:22 -04008362 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008363 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8364 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008365 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008366 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008367 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008368 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008369 * be free'd up by the truncate operation, but also have some slack
8370 * space reserved in case it uses space during the truncate (thank you
8371 * very much snapshotting).
8372 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008373 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008374 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008375 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008376 * doesn't end up using space reserved for updating the inode. We also
8377 * need to be able to stop the transaction and start a new one, which
8378 * means we need to be able to update the inode several times, and we
8379 * have no idea of knowing how many times that will be, so we can't just
8380 * reserve 1 item for the entirety of the operation, so that has to be
8381 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008382 *
8383 * So that leaves us with
8384 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008385 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008386 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008387 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008388 * updating the inode.
8389 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008390 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008391 if (!rsv)
8392 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008393 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008394 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008395
Josef Bacik907cbce2011-08-08 13:46:15 -04008396 /*
Josef Bacik07127182011-08-19 10:29:59 -04008397 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008398 * 1 for updating the inode.
8399 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008400 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008401 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008402 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008403 goto out;
8404 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008405
Josef Bacik907cbce2011-08-08 13:46:15 -04008406 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008407 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008408 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008409 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008410
Chris Mason5a3f23d2009-03-31 13:27:11 -04008411 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008412 * So if we truncate and then write and fsync we normally would just
8413 * write the extents that changed, which is a problem if we need to
8414 * first truncate that entire inode. So set this flag so we write out
8415 * all of the extents in the inode to the sync log so we're completely
8416 * safe.
8417 */
8418 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008419 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008420
Yan, Zheng80825102009-11-12 09:35:36 +00008421 while (1) {
8422 ret = btrfs_truncate_inode_items(trans, root, inode,
8423 inode->i_size,
8424 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008425 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008426 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008427 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008428
Yan, Zheng80825102009-11-12 09:35:36 +00008429 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008430 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008431 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008432
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008433 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008434 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008435
8436 trans = btrfs_start_transaction(root, 2);
8437 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008438 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008439 trans = NULL;
8440 break;
8441 }
8442
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008443 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008444 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008445 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008446 BUG_ON(ret); /* shouldn't happen */
8447 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008448 }
8449
Josef Bacikddfae632017-10-19 14:16:02 -04008450 /*
8451 * We can't call btrfs_truncate_block inside a trans handle as we could
8452 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8453 * we've truncated everything except the last little bit, and can do
8454 * btrfs_truncate_block and then update the disk_i_size.
8455 */
8456 if (ret == NEED_TRUNCATE_BLOCK) {
8457 btrfs_end_transaction(trans);
8458 btrfs_btree_balance_dirty(fs_info);
8459
8460 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8461 if (ret)
8462 goto out;
8463 trans = btrfs_start_transaction(root, 1);
8464 if (IS_ERR(trans)) {
8465 ret = PTR_ERR(trans);
8466 goto out;
8467 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008468 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008469 }
8470
Chris Mason917c16b2011-11-08 14:49:59 -05008471 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008472 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008473
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008474 trans->block_rsv = &fs_info->trans_block_rsv;
8475 ret2 = btrfs_update_inode(trans, root, inode);
8476 if (ret2 && !ret)
8477 ret = ret2;
8478
8479 ret2 = btrfs_end_transaction(trans);
8480 if (ret2 && !ret)
8481 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008482 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008483 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008484out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008485 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008486
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008487 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008488}
8489
Sven Wegener3b963622008-06-09 21:57:42 -04008490/*
Chris Masond352ac62008-09-29 15:18:18 -04008491 * create a new subvolume directory/inode (helper for the ioctl).
8492 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008493int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008494 struct btrfs_root *new_root,
8495 struct btrfs_root *parent_root,
8496 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008497{
Chris Mason39279cc2007-06-12 06:35:45 -04008498 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008499 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008500 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008501
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008502 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8503 new_dirid, new_dirid,
8504 S_IFDIR | (~current_umask() & S_IRWXUGO),
8505 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008506 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008507 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008508 inode->i_op = &btrfs_dir_inode_operations;
8509 inode->i_fop = &btrfs_dir_file_operations;
8510
Miklos Szeredibfe86842011-10-28 14:13:29 +02008511 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008512 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008513 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008514
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008515 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8516 if (err)
8517 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008518 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008519 new_root->root_key.objectid, err);
8520
Yan, Zheng76dda932009-09-21 16:00:26 -04008521 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008522
Yan, Zheng76dda932009-09-21 16:00:26 -04008523 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008524 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008525}
8526
Chris Mason39279cc2007-06-12 06:35:45 -04008527struct inode *btrfs_alloc_inode(struct super_block *sb)
8528{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008529 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008530 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008531 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008532
David Sterba712e36c2017-10-31 17:08:27 +01008533 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008534 if (!ei)
8535 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008536
8537 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008538 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008539 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008540 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008541 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008542 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008543 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008544 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008545 ei->disk_i_size = 0;
8546 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008547 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008548 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008549 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008550 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008551 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008552
Josef Bacik9e0baf62011-07-15 15:16:44 +00008553 spin_lock_init(&ei->lock);
8554 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008555 if (sb->s_magic != BTRFS_TEST_MAGIC)
8556 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8557 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008558 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008559 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008560 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008561
Miao Xie16cdcec2011-04-22 18:12:22 +08008562 ei->delayed_node = NULL;
8563
chandan r9cc97d62012-07-04 12:48:07 +05308564 ei->i_otime.tv_sec = 0;
8565 ei->i_otime.tv_nsec = 0;
8566
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008567 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008568 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008569 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8570 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8571 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008572 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8573 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008574 ei->io_tree.track_uptodate = true;
8575 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008576 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008577 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008578 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008579 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008580 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008581 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008582 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008583
8584 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008585}
8586
Josef Bacikaaedb552013-10-11 14:44:09 -04008587#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8588void btrfs_test_destroy_inode(struct inode *inode)
8589{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008590 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008591 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8592}
8593#endif
8594
Al Viro26602ca2019-04-10 15:14:41 -04008595void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008596{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008597 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8598}
8599
Chris Mason39279cc2007-06-12 06:35:45 -04008600void btrfs_destroy_inode(struct inode *inode)
8601{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008602 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008603 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008604 struct btrfs_root *root = BTRFS_I(inode)->root;
8605
Al Virob3d9b7a2012-06-09 13:51:19 -04008606 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008607 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008608 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8609 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008610 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008611 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008612 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008613 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008614 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008615
Chris Mason5a3f23d2009-03-31 13:27:11 -04008616 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008617 * This can happen where we create an inode, but somebody else also
8618 * created the same inode and we need to destroy the one we already
8619 * created.
8620 */
8621 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008622 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008623
Chris Masond3977122009-01-05 21:25:51 -05008624 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008625 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8626 if (!ordered)
8627 break;
8628 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008629 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008630 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008631 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008632 btrfs_remove_ordered_extent(inode, ordered);
8633 btrfs_put_ordered_extent(ordered);
8634 btrfs_put_ordered_extent(ordered);
8635 }
8636 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008637 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008638 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008639 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008640 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008641 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008642}
8643
Al Viro45321ac2010-06-07 13:43:19 -04008644int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008645{
8646 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008647
Naohiro Aota6379ef92013-06-06 09:56:34 +00008648 if (root == NULL)
8649 return 1;
8650
Liu Bofa6ac872013-02-20 14:10:23 +00008651 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008652 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008653 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008654 else
Al Viro45321ac2010-06-07 13:43:19 -04008655 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008656}
8657
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008658static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008659{
8660 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8661
8662 inode_init_once(&ei->vfs_inode);
8663}
8664
David Sterbae67c7182018-02-19 17:24:18 +01008665void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008666{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008667 /*
8668 * Make sure all delayed rcu free inodes are flushed before we
8669 * destroy cache.
8670 */
8671 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008672 kmem_cache_destroy(btrfs_inode_cachep);
8673 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008674 kmem_cache_destroy(btrfs_path_cachep);
8675 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008676 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008677}
8678
Liu Bof5c29bd2017-11-02 17:21:50 -06008679int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008680{
David Sterba837e1972012-09-07 03:00:48 -06008681 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008682 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008683 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8684 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008685 if (!btrfs_inode_cachep)
8686 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008687
David Sterba837e1972012-09-07 03:00:48 -06008688 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008689 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008690 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008691 if (!btrfs_trans_handle_cachep)
8692 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008693
David Sterba837e1972012-09-07 03:00:48 -06008694 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008695 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008696 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008697 if (!btrfs_path_cachep)
8698 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008699
David Sterba837e1972012-09-07 03:00:48 -06008700 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008701 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008702 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008703 if (!btrfs_free_space_cachep)
8704 goto fail;
8705
Christophe Leroy3acd4852019-08-21 15:05:55 +00008706 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8707 PAGE_SIZE, PAGE_SIZE,
8708 SLAB_RED_ZONE, NULL);
8709 if (!btrfs_free_space_bitmap_cachep)
8710 goto fail;
8711
Chris Mason39279cc2007-06-12 06:35:45 -04008712 return 0;
8713fail:
8714 btrfs_destroy_cachep();
8715 return -ENOMEM;
8716}
8717
David Howellsa528d352017-01-31 16:46:22 +00008718static int btrfs_getattr(const struct path *path, struct kstat *stat,
8719 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008720{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008721 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008722 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008723 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008724 u32 bi_flags = BTRFS_I(inode)->flags;
8725
8726 stat->result_mask |= STATX_BTIME;
8727 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8728 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8729 if (bi_flags & BTRFS_INODE_APPEND)
8730 stat->attributes |= STATX_ATTR_APPEND;
8731 if (bi_flags & BTRFS_INODE_COMPRESS)
8732 stat->attributes |= STATX_ATTR_COMPRESSED;
8733 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8734 stat->attributes |= STATX_ATTR_IMMUTABLE;
8735 if (bi_flags & BTRFS_INODE_NODUMP)
8736 stat->attributes |= STATX_ATTR_NODUMP;
8737
8738 stat->attributes_mask |= (STATX_ATTR_APPEND |
8739 STATX_ATTR_COMPRESSED |
8740 STATX_ATTR_IMMUTABLE |
8741 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008742
Chris Mason39279cc2007-06-12 06:35:45 -04008743 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008744 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008745
8746 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008747 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008748 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008749 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008750 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008751 return 0;
8752}
8753
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008754static int btrfs_rename_exchange(struct inode *old_dir,
8755 struct dentry *old_dentry,
8756 struct inode *new_dir,
8757 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008758{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008759 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008760 struct btrfs_trans_handle *trans;
8761 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008762 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008763 struct inode *new_inode = new_dentry->d_inode;
8764 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008765 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008766 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008767 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8768 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008769 u64 old_idx = 0;
8770 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008771 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008772 bool root_log_pinned = false;
8773 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008774 struct btrfs_log_ctx ctx_root;
8775 struct btrfs_log_ctx ctx_dest;
8776 bool sync_log_root = false;
8777 bool sync_log_dest = false;
8778 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008779
8780 /* we only allow rename subvolume link between subvolumes */
8781 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8782 return -EXDEV;
8783
Filipe Mananad4682ba2018-06-11 19:24:28 +01008784 btrfs_init_log_ctx(&ctx_root, old_inode);
8785 btrfs_init_log_ctx(&ctx_dest, new_inode);
8786
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008787 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008788 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8789 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008790 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008791
8792 /*
8793 * We want to reserve the absolute worst case amount of items. So if
8794 * both inodes are subvols and we need to unlink them then that would
8795 * require 4 item modifications, but if they are both normal inodes it
8796 * would require 5 item modifications, so we'll assume their normal
8797 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8798 * should cover the worst case number of items we'll modify.
8799 */
8800 trans = btrfs_start_transaction(root, 12);
8801 if (IS_ERR(trans)) {
8802 ret = PTR_ERR(trans);
8803 goto out_notrans;
8804 }
8805
Josef Bacik3e174092019-11-15 15:43:06 -05008806 if (dest != root)
8807 btrfs_record_root_in_trans(trans, dest);
8808
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008809 /*
8810 * We need to find a free sequence number both in the source and
8811 * in the destination directory for the exchange.
8812 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008813 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008814 if (ret)
8815 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008816 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008817 if (ret)
8818 goto out_fail;
8819
8820 BTRFS_I(old_inode)->dir_index = 0ULL;
8821 BTRFS_I(new_inode)->dir_index = 0ULL;
8822
8823 /* Reference for the source. */
8824 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8825 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008826 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008827 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008828 btrfs_pin_log_trans(root);
8829 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008830 ret = btrfs_insert_inode_ref(trans, dest,
8831 new_dentry->d_name.name,
8832 new_dentry->d_name.len,
8833 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008834 btrfs_ino(BTRFS_I(new_dir)),
8835 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008836 if (ret)
8837 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008838 }
8839
8840 /* And now for the dest. */
8841 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8842 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008843 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008844 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008845 btrfs_pin_log_trans(dest);
8846 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008847 ret = btrfs_insert_inode_ref(trans, root,
8848 old_dentry->d_name.name,
8849 old_dentry->d_name.len,
8850 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008851 btrfs_ino(BTRFS_I(old_dir)),
8852 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008853 if (ret)
8854 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008855 }
8856
8857 /* Update inode version and ctime/mtime. */
8858 inode_inc_iversion(old_dir);
8859 inode_inc_iversion(new_dir);
8860 inode_inc_iversion(old_inode);
8861 inode_inc_iversion(new_inode);
8862 old_dir->i_ctime = old_dir->i_mtime = ctime;
8863 new_dir->i_ctime = new_dir->i_mtime = ctime;
8864 old_inode->i_ctime = ctime;
8865 new_inode->i_ctime = ctime;
8866
8867 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008868 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8869 BTRFS_I(old_inode), 1);
8870 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8871 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008872 }
8873
8874 /* src is a subvolume */
8875 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008876 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008877 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008878 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8879 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008880 old_dentry->d_name.name,
8881 old_dentry->d_name.len);
8882 if (!ret)
8883 ret = btrfs_update_inode(trans, root, old_inode);
8884 }
8885 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008886 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008887 goto out_fail;
8888 }
8889
8890 /* dest is a subvolume */
8891 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008892 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008893 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008894 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8895 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008896 new_dentry->d_name.name,
8897 new_dentry->d_name.len);
8898 if (!ret)
8899 ret = btrfs_update_inode(trans, dest, new_inode);
8900 }
8901 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008902 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008903 goto out_fail;
8904 }
8905
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008906 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008907 new_dentry->d_name.name,
8908 new_dentry->d_name.len, 0, old_idx);
8909 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008910 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008911 goto out_fail;
8912 }
8913
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008914 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008915 old_dentry->d_name.name,
8916 old_dentry->d_name.len, 0, new_idx);
8917 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008918 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008919 goto out_fail;
8920 }
8921
8922 if (old_inode->i_nlink == 1)
8923 BTRFS_I(old_inode)->dir_index = old_idx;
8924 if (new_inode->i_nlink == 1)
8925 BTRFS_I(new_inode)->dir_index = new_idx;
8926
Filipe Manana86e8aa02016-05-05 02:02:27 +01008927 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008928 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008929 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8930 BTRFS_I(old_dir), parent,
8931 false, &ctx_root);
8932 if (ret == BTRFS_NEED_LOG_SYNC)
8933 sync_log_root = true;
8934 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8935 commit_transaction = true;
8936 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008937 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008938 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008939 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008940 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008941 if (!commit_transaction) {
8942 parent = old_dentry->d_parent;
8943 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8944 BTRFS_I(new_dir), parent,
8945 false, &ctx_dest);
8946 if (ret == BTRFS_NEED_LOG_SYNC)
8947 sync_log_dest = true;
8948 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8949 commit_transaction = true;
8950 ret = 0;
8951 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008952 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008953 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008954 }
8955out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008956 /*
8957 * If we have pinned a log and an error happened, we unpin tasks
8958 * trying to sync the log and force them to fallback to a transaction
8959 * commit if the log currently contains any of the inodes involved in
8960 * this rename operation (to ensure we do not persist a log with an
8961 * inconsistent state for any of these inodes or leading to any
8962 * inconsistencies when replayed). If the transaction was aborted, the
8963 * abortion reason is propagated to userspace when attempting to commit
8964 * the transaction. If the log does not contain any of these inodes, we
8965 * allow the tasks to sync it.
8966 */
8967 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008968 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8969 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8970 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008971 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008972 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008973 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008974
8975 if (root_log_pinned) {
8976 btrfs_end_log_trans(root);
8977 root_log_pinned = false;
8978 }
8979 if (dest_log_pinned) {
8980 btrfs_end_log_trans(dest);
8981 dest_log_pinned = false;
8982 }
8983 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008984 if (!ret && sync_log_root && !commit_transaction) {
8985 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
8986 &ctx_root);
8987 if (ret)
8988 commit_transaction = true;
8989 }
8990 if (!ret && sync_log_dest && !commit_transaction) {
8991 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
8992 &ctx_dest);
8993 if (ret)
8994 commit_transaction = true;
8995 }
8996 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00008997 /*
8998 * We may have set commit_transaction when logging the new name
8999 * in the destination root, in which case we left the source
9000 * root context in the list of log contextes. So make sure we
9001 * remove it to avoid invalid memory accesses, since the context
9002 * was allocated in our stack frame.
9003 */
9004 if (sync_log_root) {
9005 mutex_lock(&root->log_mutex);
9006 list_del_init(&ctx_root.list);
9007 mutex_unlock(&root->log_mutex);
9008 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009009 ret = btrfs_commit_transaction(trans);
9010 } else {
9011 int ret2;
9012
9013 ret2 = btrfs_end_transaction(trans);
9014 ret = ret ? ret : ret2;
9015 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009016out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009017 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9018 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009019 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009020
Filipe Mananae6c61712019-11-08 16:11:56 +00009021 ASSERT(list_empty(&ctx_root.list));
9022 ASSERT(list_empty(&ctx_dest.list));
9023
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009024 return ret;
9025}
9026
9027static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9028 struct btrfs_root *root,
9029 struct inode *dir,
9030 struct dentry *dentry)
9031{
9032 int ret;
9033 struct inode *inode;
9034 u64 objectid;
9035 u64 index;
9036
9037 ret = btrfs_find_free_ino(root, &objectid);
9038 if (ret)
9039 return ret;
9040
9041 inode = btrfs_new_inode(trans, root, dir,
9042 dentry->d_name.name,
9043 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009044 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009045 objectid,
9046 S_IFCHR | WHITEOUT_MODE,
9047 &index);
9048
9049 if (IS_ERR(inode)) {
9050 ret = PTR_ERR(inode);
9051 return ret;
9052 }
9053
9054 inode->i_op = &btrfs_special_inode_operations;
9055 init_special_inode(inode, inode->i_mode,
9056 WHITEOUT_DEV);
9057
9058 ret = btrfs_init_inode_security(trans, inode, dir,
9059 &dentry->d_name);
9060 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009061 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009062
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009063 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9064 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009065 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009066 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009067
9068 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009069out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009070 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009071 if (ret)
9072 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009073 iput(inode);
9074
Filipe Mananac9901612016-05-05 01:41:57 +01009075 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009076}
9077
Chris Mason39279cc2007-06-12 06:35:45 -04009078static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009079 struct inode *new_dir, struct dentry *new_dentry,
9080 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009082 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009083 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009084 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009085 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9086 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009087 struct inode *new_inode = d_inode(new_dentry);
9088 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009089 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009090 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009091 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009092 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009093 struct btrfs_log_ctx ctx;
9094 bool sync_log = false;
9095 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009096
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009097 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009098 return -EPERM;
9099
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009100 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009101 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009102 return -EXDEV;
9103
Li Zefan33345d012011-04-20 10:31:50 +08009104 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009105 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009106 return -ENOTEMPTY;
9107
Chris Mason39279cc2007-06-12 06:35:45 -04009108 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009109 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009110 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009111
9112
9113 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009114 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009115 new_dentry->d_name.name,
9116 new_dentry->d_name.len);
9117
9118 if (ret) {
9119 if (ret == -EEXIST) {
9120 /* we shouldn't get
9121 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309122 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009123 return ret;
9124 }
9125 } else {
9126 /* maybe -EOVERFLOW */
9127 return ret;
9128 }
9129 }
9130 ret = 0;
9131
Chris Mason5a3f23d2009-03-31 13:27:11 -04009132 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009133 * we're using rename to replace one file with another. Start IO on it
9134 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009135 */
Chris Mason8d875f92014-08-12 10:47:42 -07009136 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009137 filemap_flush(old_inode->i_mapping);
9138
Yan, Zheng76dda932009-09-21 16:00:26 -04009139 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009140 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009141 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009142 /*
9143 * We want to reserve the absolute worst case amount of items. So if
9144 * both inodes are subvols and we need to unlink them then that would
9145 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009146 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009147 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9148 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009149 * If our rename has the whiteout flag, we need more 5 units for the
9150 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9151 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009152 */
Filipe Manana5062af32016-05-05 10:26:26 +01009153 trans_num_items = 11;
9154 if (flags & RENAME_WHITEOUT)
9155 trans_num_items += 5;
9156 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009157 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009158 ret = PTR_ERR(trans);
9159 goto out_notrans;
9160 }
Chris Mason5f39d392007-10-15 16:14:19 -04009161
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009162 if (dest != root)
9163 btrfs_record_root_in_trans(trans, dest);
9164
Nikolay Borisov877574e2017-02-20 13:50:33 +02009165 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009166 if (ret)
9167 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009168
Miao Xie67de1172013-12-26 13:07:06 +08009169 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009170 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009171 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009172 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009173 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009174 btrfs_pin_log_trans(root);
9175 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009176 ret = btrfs_insert_inode_ref(trans, dest,
9177 new_dentry->d_name.name,
9178 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009179 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009180 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009181 if (ret)
9182 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009183 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009184
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009185 inode_inc_iversion(old_dir);
9186 inode_inc_iversion(new_dir);
9187 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009188 old_dir->i_ctime = old_dir->i_mtime =
9189 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009190 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009191
Chris Mason12fcfd22009-03-24 10:24:20 -04009192 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009193 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9194 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009195
Li Zefan33345d012011-04-20 10:31:50 +08009196 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009197 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009198 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009199 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9200 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009201 old_dentry->d_name.name,
9202 old_dentry->d_name.len);
9203 if (!ret)
9204 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009205 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009206 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009207 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009208 goto out_fail;
9209 }
Chris Mason39279cc2007-06-12 06:35:45 -04009210
9211 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009212 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009213 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009214 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009215 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009216 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009217 BUG_ON(new_inode->i_nlink == 0);
9218 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009219 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9220 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009221 new_dentry->d_name.name,
9222 new_dentry->d_name.len);
9223 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009224 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009225 ret = btrfs_orphan_add(trans,
9226 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009227 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009228 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009229 goto out_fail;
9230 }
Chris Mason39279cc2007-06-12 06:35:45 -04009231 }
Josef Bacikaec74772008-07-24 12:12:38 -04009232
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009233 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009234 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009235 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009236 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009237 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009238 goto out_fail;
9239 }
Chris Mason39279cc2007-06-12 06:35:45 -04009240
Miao Xie67de1172013-12-26 13:07:06 +08009241 if (old_inode->i_nlink == 1)
9242 BTRFS_I(old_inode)->dir_index = index;
9243
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009244 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009245 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009246
Filipe Mananad4682ba2018-06-11 19:24:28 +01009247 btrfs_init_log_ctx(&ctx, old_inode);
9248 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9249 BTRFS_I(old_dir), parent,
9250 false, &ctx);
9251 if (ret == BTRFS_NEED_LOG_SYNC)
9252 sync_log = true;
9253 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9254 commit_transaction = true;
9255 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009256 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009257 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009258 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009259
9260 if (flags & RENAME_WHITEOUT) {
9261 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9262 old_dentry);
9263
9264 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009265 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009266 goto out_fail;
9267 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009268 }
Chris Mason39279cc2007-06-12 06:35:45 -04009269out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009270 /*
9271 * If we have pinned the log and an error happened, we unpin tasks
9272 * trying to sync the log and force them to fallback to a transaction
9273 * commit if the log currently contains any of the inodes involved in
9274 * this rename operation (to ensure we do not persist a log with an
9275 * inconsistent state for any of these inodes or leading to any
9276 * inconsistencies when replayed). If the transaction was aborted, the
9277 * abortion reason is propagated to userspace when attempting to commit
9278 * the transaction. If the log does not contain any of these inodes, we
9279 * allow the tasks to sync it.
9280 */
9281 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009282 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9283 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9284 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009285 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009286 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009287 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009288
9289 btrfs_end_log_trans(root);
9290 log_pinned = false;
9291 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009292 if (!ret && sync_log) {
9293 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9294 if (ret)
9295 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009296 } else if (sync_log) {
9297 mutex_lock(&root->log_mutex);
9298 list_del(&ctx.list);
9299 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009300 }
9301 if (commit_transaction) {
9302 ret = btrfs_commit_transaction(trans);
9303 } else {
9304 int ret2;
9305
9306 ret2 = btrfs_end_transaction(trans);
9307 ret = ret ? ret : ret2;
9308 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009309out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009310 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009311 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009312
Chris Mason39279cc2007-06-12 06:35:45 -04009313 return ret;
9314}
9315
Miklos Szeredi80ace852014-07-23 15:15:32 +02009316static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9317 struct inode *new_dir, struct dentry *new_dentry,
9318 unsigned int flags)
9319{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009320 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009321 return -EINVAL;
9322
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009323 if (flags & RENAME_EXCHANGE)
9324 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9325 new_dentry);
9326
9327 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009328}
9329
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009330struct btrfs_delalloc_work {
9331 struct inode *inode;
9332 struct completion completion;
9333 struct list_head list;
9334 struct btrfs_work work;
9335};
9336
Miao Xie8ccf6f192012-10-25 09:28:04 +00009337static void btrfs_run_delalloc_work(struct btrfs_work *work)
9338{
9339 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009340 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009341
9342 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9343 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009344 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009345 filemap_flush(inode->i_mapping);
9346 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9347 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009348 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009349
Nikolay Borisov076da912018-04-23 10:54:16 +03009350 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009351 complete(&delalloc_work->completion);
9352}
9353
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009354static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009355{
9356 struct btrfs_delalloc_work *work;
9357
David Sterba100d5702015-12-08 14:39:32 +01009358 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009359 if (!work)
9360 return NULL;
9361
9362 init_completion(&work->completion);
9363 INIT_LIST_HEAD(&work->list);
9364 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009365 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009366
9367 return work;
9368}
9369
Chris Masond352ac62008-09-29 15:18:18 -04009370/*
9371 * some fairly slow code that needs optimization. This walks the list
9372 * of all the inodes with pending delalloc and forces them to disk.
9373 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009374static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009375{
Chris Masonea8c2812008-08-04 23:17:27 -04009376 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009377 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009378 struct btrfs_delalloc_work *work, *next;
9379 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009380 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009381 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009382
Miao Xie8ccf6f192012-10-25 09:28:04 +00009383 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009384 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009385
Miao Xie573bfb72014-03-06 13:55:03 +08009386 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009387 spin_lock(&root->delalloc_lock);
9388 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009389 while (!list_empty(&splice)) {
9390 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009391 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009392
Miao Xieeb73c1b2013-05-15 07:48:22 +00009393 list_move_tail(&binode->delalloc_inodes,
9394 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009395 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009396 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009397 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009398 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009399 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009400 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009401
Ethan Lien3cd24c62018-11-01 14:49:03 +08009402 if (snapshot)
9403 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9404 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009405 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009406 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009407 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009408 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009409 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009410 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009411 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009412 btrfs_queue_work(root->fs_info->flush_workers,
9413 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009414 ret++;
9415 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009416 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009417 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009418 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009419 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009420 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009421
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009422out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009423 list_for_each_entry_safe(work, next, &works, list) {
9424 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009425 wait_for_completion(&work->completion);
9426 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009427 }
9428
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009429 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009430 spin_lock(&root->delalloc_lock);
9431 list_splice_tail(&splice, &root->delalloc_inodes);
9432 spin_unlock(&root->delalloc_lock);
9433 }
Miao Xie573bfb72014-03-06 13:55:03 +08009434 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009435 return ret;
9436}
9437
Ethan Lien3cd24c62018-11-01 14:49:03 +08009438int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009439{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009440 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009441 int ret;
9442
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009443 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009444 return -EROFS;
9445
Ethan Lien3cd24c62018-11-01 14:49:03 +08009446 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009447 if (ret > 0)
9448 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009449 return ret;
9450}
9451
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009452int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009453{
9454 struct btrfs_root *root;
9455 struct list_head splice;
9456 int ret;
9457
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009458 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009459 return -EROFS;
9460
9461 INIT_LIST_HEAD(&splice);
9462
Miao Xie573bfb72014-03-06 13:55:03 +08009463 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009464 spin_lock(&fs_info->delalloc_root_lock);
9465 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009466 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009467 root = list_first_entry(&splice, struct btrfs_root,
9468 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009469 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009470 BUG_ON(!root);
9471 list_move_tail(&root->delalloc_root,
9472 &fs_info->delalloc_roots);
9473 spin_unlock(&fs_info->delalloc_root_lock);
9474
Ethan Lien3cd24c62018-11-01 14:49:03 +08009475 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009476 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009477 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009478 goto out;
9479
Miao Xie6c255e62014-03-06 13:55:01 +08009480 if (nr != -1) {
9481 nr -= ret;
9482 WARN_ON(nr < 0);
9483 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009484 spin_lock(&fs_info->delalloc_root_lock);
9485 }
9486 spin_unlock(&fs_info->delalloc_root_lock);
9487
Miao Xie6c255e62014-03-06 13:55:01 +08009488 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009489out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009490 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009491 spin_lock(&fs_info->delalloc_root_lock);
9492 list_splice_tail(&splice, &fs_info->delalloc_roots);
9493 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009494 }
Miao Xie573bfb72014-03-06 13:55:03 +08009495 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009496 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009497}
9498
Chris Mason39279cc2007-06-12 06:35:45 -04009499static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9500 const char *symname)
9501{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009502 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009503 struct btrfs_trans_handle *trans;
9504 struct btrfs_root *root = BTRFS_I(dir)->root;
9505 struct btrfs_path *path;
9506 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009507 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009508 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009509 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309510 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009511 int name_len;
9512 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009513 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009514 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009515 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009516
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009517 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009518 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009519 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009520
Josef Bacik9ed74f22009-09-11 16:12:44 -04009521 /*
9522 * 2 items for inode item and ref
9523 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009524 * 1 item for updating parent inode item
9525 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009526 * 1 item for xattr if selinux is on
9527 */
Filipe Manana9269d122015-12-31 18:16:29 +00009528 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009529 if (IS_ERR(trans))
9530 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009531
Li Zefan581bb052011-04-20 10:06:11 +08009532 err = btrfs_find_free_ino(root, &objectid);
9533 if (err)
9534 goto out_unlock;
9535
Josef Bacikaec74772008-07-24 12:12:38 -04009536 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009537 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9538 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009539 if (IS_ERR(inode)) {
9540 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009541 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009542 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009543 }
Chris Mason39279cc2007-06-12 06:35:45 -04009544
Casey Schauflerad19db72011-12-15 10:09:07 -05009545 /*
9546 * If the active LSM wants to access the inode during
9547 * d_instantiate it needs these. Smack checks to see
9548 * if the filesystem supports xattrs by looking at the
9549 * ops vector.
9550 */
9551 inode->i_fop = &btrfs_file_operations;
9552 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009553 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009554 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9555
9556 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9557 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009558 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009559
Chris Mason39279cc2007-06-12 06:35:45 -04009560 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009561 if (!path) {
9562 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009563 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009564 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009565 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009566 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009567 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009568 datasize = btrfs_file_extent_calc_inline_size(name_len);
9569 err = btrfs_insert_empty_item(trans, root, path, &key,
9570 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009571 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009572 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009573 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009574 }
Chris Mason5f39d392007-10-15 16:14:19 -04009575 leaf = path->nodes[0];
9576 ei = btrfs_item_ptr(leaf, path->slots[0],
9577 struct btrfs_file_extent_item);
9578 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9579 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009580 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009581 btrfs_set_file_extent_encryption(leaf, ei, 0);
9582 btrfs_set_file_extent_compression(leaf, ei, 0);
9583 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9584 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9585
Chris Mason39279cc2007-06-12 06:35:45 -04009586 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009587 write_extent_buffer(leaf, symname, ptr, name_len);
9588 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009589 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009590
Chris Mason39279cc2007-06-12 06:35:45 -04009591 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009592 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009593 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009594 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009595 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009596 /*
9597 * Last step, add directory indexes for our symlink inode. This is the
9598 * last step to avoid extra cleanup of these indexes if an error happens
9599 * elsewhere above.
9600 */
9601 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009602 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9603 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009604 if (err)
9605 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009606
Al Viro1e2e5472018-05-04 08:23:01 -04009607 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009608
9609out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009610 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009611 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009612 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009613 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009614 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009615 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009616 return err;
9617}
Chris Mason16432982008-04-10 10:23:21 -04009618
Qu Wenruo203f44c52020-06-10 09:04:40 +08009619static int insert_prealloc_file_extent(struct btrfs_trans_handle *trans,
9620 struct inode *inode, struct btrfs_key *ins,
9621 u64 file_offset)
9622{
9623 struct btrfs_file_extent_item stack_fi;
9624 u64 start = ins->objectid;
9625 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009626 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009627
9628 memset(&stack_fi, 0, sizeof(stack_fi));
9629
9630 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9631 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9632 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9633 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9634 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9635 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9636 /* Encryption and other encoding is reserved and all 0 */
9637
Nikolay Borisov72b7d152020-06-03 08:55:18 +03009638 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009639 if (ret < 0)
9640 return ret;
Nikolay Borisovc553f942020-06-03 08:55:19 +03009641 return insert_reserved_file_extent(trans, BTRFS_I(inode), file_offset,
Qu Wenruo9729f102020-06-10 09:04:41 +08009642 &stack_fi, ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009643}
Josef Bacik0af3d002010-06-21 14:48:16 -04009644static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9645 u64 start, u64 num_bytes, u64 min_size,
9646 loff_t actual_len, u64 *alloc_hint,
9647 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009648{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009649 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009650 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9651 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009652 struct btrfs_root *root = BTRFS_I(inode)->root;
9653 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009654 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009655 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009656 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009657 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009658 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009659 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009660 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009661 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009662
Josef Bacik0af3d002010-06-21 14:48:16 -04009663 if (trans)
9664 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009665 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009666 if (own_trans) {
9667 trans = btrfs_start_transaction(root, 3);
9668 if (IS_ERR(trans)) {
9669 ret = PTR_ERR(trans);
9670 break;
9671 }
Yan Zhengd899e052008-10-30 14:25:28 -04009672 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009673
Byongho Leeee221842015-12-15 01:42:10 +09009674 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009675 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009676 /*
9677 * If we are severely fragmented we could end up with really
9678 * small allocations, so if the allocator is returning small
9679 * chunks lets make its job easier by only searching for those
9680 * sized chunks.
9681 */
9682 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009683 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9684 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009685 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009686 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009687 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009688 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009689 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009690
9691 /*
9692 * We've reserved this space, and thus converted it from
9693 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9694 * from here on out we will only need to clear our reservation
9695 * for the remaining unreserved area, so advance our
9696 * clear_offset by our extent size.
9697 */
9698 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009699 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009700
Josef Bacik0b670dc2015-09-23 17:11:16 -04009701 last_alloc = ins.offset;
Nikolay Borisovc553f942020-06-03 08:55:19 +03009702 ret = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009703 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009704 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009705 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009706 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009707 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009708 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009709 break;
9710 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009711
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009712 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009713 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009714
Josef Bacik5dc562c2012-08-17 13:14:17 -04009715 em = alloc_extent_map();
9716 if (!em) {
9717 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9718 &BTRFS_I(inode)->runtime_flags);
9719 goto next;
9720 }
9721
9722 em->start = cur_offset;
9723 em->orig_start = cur_offset;
9724 em->len = ins.offset;
9725 em->block_start = ins.objectid;
9726 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009727 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009728 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009729 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9730 em->generation = trans->transid;
9731
9732 while (1) {
9733 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009734 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009735 write_unlock(&em_tree->lock);
9736 if (ret != -EEXIST)
9737 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009738 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009739 cur_offset + ins.offset - 1,
9740 0);
9741 }
9742 free_extent_map(em);
9743next:
Yan Zhengd899e052008-10-30 14:25:28 -04009744 num_bytes -= ins.offset;
9745 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009746 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009747
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009748 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009749 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009750 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009751 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009752 (actual_len > inode->i_size) &&
9753 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009754 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009755 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009756 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009757 i_size = cur_offset;
9758 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009759 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009760 }
9761
Yan Zhengd899e052008-10-30 14:25:28 -04009762 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009763
9764 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009765 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009766 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009767 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009768 break;
9769 }
Yan Zhengd899e052008-10-30 14:25:28 -04009770
Josef Bacik0af3d002010-06-21 14:48:16 -04009771 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009772 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009773 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009774 if (clear_offset < end)
9775 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
9776 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009777 return ret;
9778}
9779
Josef Bacik0af3d002010-06-21 14:48:16 -04009780int btrfs_prealloc_file_range(struct inode *inode, int mode,
9781 u64 start, u64 num_bytes, u64 min_size,
9782 loff_t actual_len, u64 *alloc_hint)
9783{
9784 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9785 min_size, actual_len, alloc_hint,
9786 NULL);
9787}
9788
9789int btrfs_prealloc_file_range_trans(struct inode *inode,
9790 struct btrfs_trans_handle *trans, int mode,
9791 u64 start, u64 num_bytes, u64 min_size,
9792 loff_t actual_len, u64 *alloc_hint)
9793{
9794 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9795 min_size, actual_len, alloc_hint, trans);
9796}
9797
Chris Masone6dcd2d2008-07-17 12:53:50 -04009798static int btrfs_set_page_dirty(struct page *page)
9799{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009800 return __set_page_dirty_nobuffers(page);
9801}
9802
Al Viro10556cb22011-06-20 19:28:19 -04009803static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009804{
Li Zefanb83cc962010-12-20 16:04:08 +08009805 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009806 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009807
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009808 if (mask & MAY_WRITE &&
9809 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9810 if (btrfs_root_readonly(root))
9811 return -EROFS;
9812 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9813 return -EACCES;
9814 }
Al Viro2830ba72011-06-20 19:16:29 -04009815 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009816}
Chris Mason39279cc2007-06-12 06:35:45 -04009817
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009818static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9819{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009820 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009821 struct btrfs_trans_handle *trans;
9822 struct btrfs_root *root = BTRFS_I(dir)->root;
9823 struct inode *inode = NULL;
9824 u64 objectid;
9825 u64 index;
9826 int ret = 0;
9827
9828 /*
9829 * 5 units required for adding orphan entry
9830 */
9831 trans = btrfs_start_transaction(root, 5);
9832 if (IS_ERR(trans))
9833 return PTR_ERR(trans);
9834
9835 ret = btrfs_find_free_ino(root, &objectid);
9836 if (ret)
9837 goto out;
9838
9839 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009840 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009841 if (IS_ERR(inode)) {
9842 ret = PTR_ERR(inode);
9843 inode = NULL;
9844 goto out;
9845 }
9846
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009847 inode->i_fop = &btrfs_file_operations;
9848 inode->i_op = &btrfs_file_inode_operations;
9849
9850 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009851 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9852
Chris Masonb0d5d102014-09-08 13:08:51 -07009853 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9854 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009855 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009856
9857 ret = btrfs_update_inode(trans, root, inode);
9858 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009859 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009860 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009861 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009862 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009863
Filipe Manana5762b5c2014-08-01 00:10:32 +01009864 /*
9865 * We set number of links to 0 in btrfs_new_inode(), and here we set
9866 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9867 * through:
9868 *
9869 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9870 */
9871 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009872 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009873 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009874 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009875out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009876 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009877 if (ret && inode)
9878 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009879 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009880 return ret;
9881}
9882
David Sterba5cdc84b2018-07-18 20:32:52 +02009883void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009884{
David Sterba5cdc84b2018-07-18 20:32:52 +02009885 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009886 unsigned long index = start >> PAGE_SHIFT;
9887 unsigned long end_index = end >> PAGE_SHIFT;
9888 struct page *page;
9889
9890 while (index <= end_index) {
9891 page = find_get_page(inode->i_mapping, index);
9892 ASSERT(page); /* Pages should be in the extent_io_tree */
9893 set_page_writeback(page);
9894 put_page(page);
9895 index++;
9896 }
9897}
9898
Omar Sandovaled46ff32016-11-03 10:28:14 -07009899#ifdef CONFIG_SWAP
9900/*
9901 * Add an entry indicating a block group or device which is pinned by a
9902 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9903 * negative errno on failure.
9904 */
9905static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9906 bool is_block_group)
9907{
9908 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9909 struct btrfs_swapfile_pin *sp, *entry;
9910 struct rb_node **p;
9911 struct rb_node *parent = NULL;
9912
9913 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9914 if (!sp)
9915 return -ENOMEM;
9916 sp->ptr = ptr;
9917 sp->inode = inode;
9918 sp->is_block_group = is_block_group;
9919
9920 spin_lock(&fs_info->swapfile_pins_lock);
9921 p = &fs_info->swapfile_pins.rb_node;
9922 while (*p) {
9923 parent = *p;
9924 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9925 if (sp->ptr < entry->ptr ||
9926 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9927 p = &(*p)->rb_left;
9928 } else if (sp->ptr > entry->ptr ||
9929 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9930 p = &(*p)->rb_right;
9931 } else {
9932 spin_unlock(&fs_info->swapfile_pins_lock);
9933 kfree(sp);
9934 return 1;
9935 }
9936 }
9937 rb_link_node(&sp->node, parent, p);
9938 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9939 spin_unlock(&fs_info->swapfile_pins_lock);
9940 return 0;
9941}
9942
9943/* Free all of the entries pinned by this swapfile. */
9944static void btrfs_free_swapfile_pins(struct inode *inode)
9945{
9946 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9947 struct btrfs_swapfile_pin *sp;
9948 struct rb_node *node, *next;
9949
9950 spin_lock(&fs_info->swapfile_pins_lock);
9951 node = rb_first(&fs_info->swapfile_pins);
9952 while (node) {
9953 next = rb_next(node);
9954 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9955 if (sp->inode == inode) {
9956 rb_erase(&sp->node, &fs_info->swapfile_pins);
9957 if (sp->is_block_group)
9958 btrfs_put_block_group(sp->ptr);
9959 kfree(sp);
9960 }
9961 node = next;
9962 }
9963 spin_unlock(&fs_info->swapfile_pins_lock);
9964}
9965
9966struct btrfs_swap_info {
9967 u64 start;
9968 u64 block_start;
9969 u64 block_len;
9970 u64 lowest_ppage;
9971 u64 highest_ppage;
9972 unsigned long nr_pages;
9973 int nr_extents;
9974};
9975
9976static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9977 struct btrfs_swap_info *bsi)
9978{
9979 unsigned long nr_pages;
9980 u64 first_ppage, first_ppage_reported, next_ppage;
9981 int ret;
9982
9983 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9984 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9985 PAGE_SIZE) >> PAGE_SHIFT;
9986
9987 if (first_ppage >= next_ppage)
9988 return 0;
9989 nr_pages = next_ppage - first_ppage;
9990
9991 first_ppage_reported = first_ppage;
9992 if (bsi->start == 0)
9993 first_ppage_reported++;
9994 if (bsi->lowest_ppage > first_ppage_reported)
9995 bsi->lowest_ppage = first_ppage_reported;
9996 if (bsi->highest_ppage < (next_ppage - 1))
9997 bsi->highest_ppage = next_ppage - 1;
9998
9999 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10000 if (ret < 0)
10001 return ret;
10002 bsi->nr_extents += ret;
10003 bsi->nr_pages += nr_pages;
10004 return 0;
10005}
10006
10007static void btrfs_swap_deactivate(struct file *file)
10008{
10009 struct inode *inode = file_inode(file);
10010
10011 btrfs_free_swapfile_pins(inode);
10012 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10013}
10014
10015static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10016 sector_t *span)
10017{
10018 struct inode *inode = file_inode(file);
10019 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10020 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10021 struct extent_state *cached_state = NULL;
10022 struct extent_map *em = NULL;
10023 struct btrfs_device *device = NULL;
10024 struct btrfs_swap_info bsi = {
10025 .lowest_ppage = (sector_t)-1ULL,
10026 };
10027 int ret = 0;
10028 u64 isize;
10029 u64 start;
10030
10031 /*
10032 * If the swap file was just created, make sure delalloc is done. If the
10033 * file changes again after this, the user is doing something stupid and
10034 * we don't really care.
10035 */
10036 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10037 if (ret)
10038 return ret;
10039
10040 /*
10041 * The inode is locked, so these flags won't change after we check them.
10042 */
10043 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10044 btrfs_warn(fs_info, "swapfile must not be compressed");
10045 return -EINVAL;
10046 }
10047 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10048 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10049 return -EINVAL;
10050 }
10051 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10052 btrfs_warn(fs_info, "swapfile must not be checksummed");
10053 return -EINVAL;
10054 }
10055
10056 /*
10057 * Balance or device remove/replace/resize can move stuff around from
10058 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10059 * concurrently while we are mapping the swap extents, and
10060 * fs_info->swapfile_pins prevents them from running while the swap file
10061 * is active and moving the extents. Note that this also prevents a
10062 * concurrent device add which isn't actually necessary, but it's not
10063 * really worth the trouble to allow it.
10064 */
10065 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10066 btrfs_warn(fs_info,
10067 "cannot activate swapfile while exclusive operation is running");
10068 return -EBUSY;
10069 }
10070 /*
10071 * Snapshots can create extents which require COW even if NODATACOW is
10072 * set. We use this counter to prevent snapshots. We must increment it
10073 * before walking the extents because we don't want a concurrent
10074 * snapshot to run after we've already checked the extents.
10075 */
10076 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10077
10078 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10079
10080 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10081 start = 0;
10082 while (start < isize) {
10083 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010084 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010085 u64 len = isize - start;
10086
Omar Sandoval39b07b52019-12-02 17:34:23 -080010087 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010088 if (IS_ERR(em)) {
10089 ret = PTR_ERR(em);
10090 goto out;
10091 }
10092
10093 if (em->block_start == EXTENT_MAP_HOLE) {
10094 btrfs_warn(fs_info, "swapfile must not have holes");
10095 ret = -EINVAL;
10096 goto out;
10097 }
10098 if (em->block_start == EXTENT_MAP_INLINE) {
10099 /*
10100 * It's unlikely we'll ever actually find ourselves
10101 * here, as a file small enough to fit inline won't be
10102 * big enough to store more than the swap header, but in
10103 * case something changes in the future, let's catch it
10104 * here rather than later.
10105 */
10106 btrfs_warn(fs_info, "swapfile must not be inline");
10107 ret = -EINVAL;
10108 goto out;
10109 }
10110 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10111 btrfs_warn(fs_info, "swapfile must not be compressed");
10112 ret = -EINVAL;
10113 goto out;
10114 }
10115
10116 logical_block_start = em->block_start + (start - em->start);
10117 len = min(len, em->len - (start - em->start));
10118 free_extent_map(em);
10119 em = NULL;
10120
10121 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10122 if (ret < 0) {
10123 goto out;
10124 } else if (ret) {
10125 ret = 0;
10126 } else {
10127 btrfs_warn(fs_info,
10128 "swapfile must not be copy-on-write");
10129 ret = -EINVAL;
10130 goto out;
10131 }
10132
10133 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10134 if (IS_ERR(em)) {
10135 ret = PTR_ERR(em);
10136 goto out;
10137 }
10138
10139 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10140 btrfs_warn(fs_info,
10141 "swapfile must have single data profile");
10142 ret = -EINVAL;
10143 goto out;
10144 }
10145
10146 if (device == NULL) {
10147 device = em->map_lookup->stripes[0].dev;
10148 ret = btrfs_add_swapfile_pin(inode, device, false);
10149 if (ret == 1)
10150 ret = 0;
10151 else if (ret)
10152 goto out;
10153 } else if (device != em->map_lookup->stripes[0].dev) {
10154 btrfs_warn(fs_info, "swapfile must be on one device");
10155 ret = -EINVAL;
10156 goto out;
10157 }
10158
10159 physical_block_start = (em->map_lookup->stripes[0].physical +
10160 (logical_block_start - em->start));
10161 len = min(len, em->len - (logical_block_start - em->start));
10162 free_extent_map(em);
10163 em = NULL;
10164
10165 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10166 if (!bg) {
10167 btrfs_warn(fs_info,
10168 "could not find block group containing swapfile");
10169 ret = -EINVAL;
10170 goto out;
10171 }
10172
10173 ret = btrfs_add_swapfile_pin(inode, bg, true);
10174 if (ret) {
10175 btrfs_put_block_group(bg);
10176 if (ret == 1)
10177 ret = 0;
10178 else
10179 goto out;
10180 }
10181
10182 if (bsi.block_len &&
10183 bsi.block_start + bsi.block_len == physical_block_start) {
10184 bsi.block_len += len;
10185 } else {
10186 if (bsi.block_len) {
10187 ret = btrfs_add_swap_extent(sis, &bsi);
10188 if (ret)
10189 goto out;
10190 }
10191 bsi.start = start;
10192 bsi.block_start = physical_block_start;
10193 bsi.block_len = len;
10194 }
10195
10196 start += len;
10197 }
10198
10199 if (bsi.block_len)
10200 ret = btrfs_add_swap_extent(sis, &bsi);
10201
10202out:
10203 if (!IS_ERR_OR_NULL(em))
10204 free_extent_map(em);
10205
10206 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10207
10208 if (ret)
10209 btrfs_swap_deactivate(file);
10210
10211 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10212
10213 if (ret)
10214 return ret;
10215
10216 if (device)
10217 sis->bdev = device->bdev;
10218 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10219 sis->max = bsi.nr_pages;
10220 sis->pages = bsi.nr_pages - 1;
10221 sis->highest_bit = bsi.nr_pages - 1;
10222 return bsi.nr_extents;
10223}
10224#else
10225static void btrfs_swap_deactivate(struct file *file)
10226{
10227}
10228
10229static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10230 sector_t *span)
10231{
10232 return -EOPNOTSUPP;
10233}
10234#endif
10235
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010236static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010237 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010238 .lookup = btrfs_lookup,
10239 .create = btrfs_create,
10240 .unlink = btrfs_unlink,
10241 .link = btrfs_link,
10242 .mkdir = btrfs_mkdir,
10243 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010244 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010245 .symlink = btrfs_symlink,
10246 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010247 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010248 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010249 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010250 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010251 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010252 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010253 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010254};
Yan, Zheng76dda932009-09-21 16:00:26 -040010255
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010256static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010257 .llseek = generic_file_llseek,
10258 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010259 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010260 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010261 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010262#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010263 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010264#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010265 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010266 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010267};
10268
David Sterba20e55062015-11-19 11:42:28 +010010269static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010270 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010271 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010272 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10273};
10274
Chris Mason35054392009-01-21 13:11:13 -050010275/*
10276 * btrfs doesn't support the bmap operation because swapfiles
10277 * use bmap to make a mapping of extents in the file. They assume
10278 * these extents won't change over the life of the file and they
10279 * use the bmap result to do IO directly to the drive.
10280 *
10281 * the btrfs bmap call would return logical addresses that aren't
10282 * suitable for IO and they also will change frequently as COW
10283 * operations happen. So, swapfile + btrfs == corruption.
10284 *
10285 * For now we're avoiding this by dropping bmap.
10286 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010287static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010288 .readpage = btrfs_readpage,
10289 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010290 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010291 .readahead = btrfs_readahead,
David Sterba55e20bd2020-06-09 19:56:06 +020010292 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010293 .invalidatepage = btrfs_invalidatepage,
10294 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010295#ifdef CONFIG_MIGRATION
10296 .migratepage = btrfs_migratepage,
10297#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010298 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010299 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010300 .swap_activate = btrfs_swap_activate,
10301 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010302};
10303
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010304static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010305 .getattr = btrfs_getattr,
10306 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010307 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010308 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010309 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010310 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010311 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010312 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010313};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010314static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010315 .getattr = btrfs_getattr,
10316 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010317 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010318 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010319 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010320 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010321 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010322};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010323static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010324 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010325 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010326 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010327 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010328 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010329 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010330};
Yan, Zheng76dda932009-09-21 16:00:26 -040010331
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010332const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010333 .d_delete = btrfs_dentry_delete,
10334};