blob: 5a1dfbb457341527409daaec47ecb0501425203c [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);
Nikolay Borisov86d52922020-06-03 08:55:40 +03002331 btrfs_delalloc_release_space(BTRFS_I(inode),
2332 data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002333 page_start, PAGE_SIZE,
2334 true);
2335 }
2336 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002337 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002338 }
Chris Mason25f3c502020-01-21 11:51:42 -05002339
2340 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002341 * We can't mess with the page state unless it is locked, so now that
2342 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002343 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002344 if (ret)
2345 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002346
David Sterbaff13db42015-12-03 14:30:40 +01002347 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002348 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002349
2350 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002351 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002352 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002353
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002354 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002355 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002356 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002357 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002358 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002359 unlock_page(page);
2360 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002361 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002362 goto again;
2363 }
Chris Mason247e7432008-07-17 12:53:51 -04002364
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002365 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002366 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002367 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002368 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002369
Chris Mason25f3c502020-01-21 11:51:42 -05002370 /*
2371 * Everything went as planned, we're now the owner of a dirty page with
2372 * delayed allocation bits set and space reserved for our COW
2373 * destination.
2374 *
2375 * The page was dirty when we started, nothing should have cleaned it.
2376 */
2377 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002378 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002379out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002380 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002381 if (free_delalloc_space)
Nikolay Borisov86d52922020-06-03 08:55:40 +03002382 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
2383 page_start, PAGE_SIZE, true);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002384 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002385 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002386out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002387 if (ret) {
2388 /*
2389 * We hit ENOSPC or other errors. Update the mapping and page
2390 * to reflect the errors and clean the page.
2391 */
2392 mapping_set_error(page->mapping, ret);
2393 end_extent_writepage(page, ret, page_start, page_end);
2394 clear_page_dirty_for_io(page);
2395 SetPageError(page);
2396 }
2397 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002398 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002399 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002400 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002401 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002402 /*
2403 * As a precaution, do a delayed iput in case it would be the last iput
2404 * that could need flushing space. Recursing back to fixup worker would
2405 * deadlock.
2406 */
2407 btrfs_add_delayed_iput(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002408}
2409
2410/*
2411 * There are a few paths in the higher layers of the kernel that directly
2412 * set the page dirty bit without asking the filesystem if it is a
2413 * good idea. This causes problems because we want to make sure COW
2414 * properly happens and the data=ordered rules are followed.
2415 *
Chris Masonc8b97812008-10-29 14:49:59 -04002416 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002417 * hasn't been properly setup for IO. We kick off an async process
2418 * to fix it up. The async helper will wait for ordered extents, set
2419 * the delalloc bit and make it safe to write the page.
2420 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002421int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002422{
2423 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002425 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002426
Chris Mason8b62b722009-09-02 16:53:46 -04002427 /* this page is properly in the ordered list */
2428 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002429 return 0;
2430
Chris Mason25f3c502020-01-21 11:51:42 -05002431 /*
2432 * PageChecked is set below when we create a fixup worker for this page,
2433 * don't try to create another one if we're already PageChecked()
2434 *
2435 * The extent_io writepage code will redirty the page if we send back
2436 * EAGAIN.
2437 */
Chris Mason247e7432008-07-17 12:53:51 -04002438 if (PageChecked(page))
2439 return -EAGAIN;
2440
2441 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2442 if (!fixup)
2443 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002444
Josef Bacikf4b13632020-01-21 14:34:52 -05002445 /*
2446 * We are already holding a reference to this inode from
2447 * write_cache_pages. We need to hold it because the space reservation
2448 * takes place outside of the page lock, and we can't trust
2449 * page->mapping outside of the page lock.
2450 */
2451 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002452 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002453 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002454 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002455 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002456 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002457 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002458
2459 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002460}
2461
Yan Zhengd899e052008-10-30 14:25:28 -04002462static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002463 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002464 struct btrfs_file_extent_item *stack_fi,
2465 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002466{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002467 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002468 struct btrfs_path *path;
2469 struct extent_buffer *leaf;
2470 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002471 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2472 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2473 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2474 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002475 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002476 int ret;
2477
2478 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002479 if (!path)
2480 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002481
Chris Masona1ed8352009-09-11 12:27:37 -04002482 /*
2483 * we may be replacing one extent in the tree with another.
2484 * The new extent is pinned in the extent map, and we don't want
2485 * to drop it from the cache until it is completely in the btree.
2486 *
2487 * So, tell btrfs_drop_extents to leave this extent in the cache.
2488 * the caller is expected to unpin it and allow it to be merged
2489 * with the others.
2490 */
Nikolay Borisovc553f942020-06-03 08:55:19 +03002491 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002492 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002493 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002494 if (ret)
2495 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002496
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002497 if (!extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002498 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002499 ins.offset = file_pos;
2500 ins.type = BTRFS_EXTENT_DATA_KEY;
2501
2502 path->leave_spinning = 1;
2503 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002504 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002505 if (ret)
2506 goto out;
2507 }
Yan Zhengd899e052008-10-30 14:25:28 -04002508 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002509 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2510 write_extent_buffer(leaf, stack_fi,
2511 btrfs_item_ptr_offset(leaf, path->slots[0]),
2512 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002513
Yan Zhengd899e052008-10-30 14:25:28 -04002514 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002515 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002516
Nikolay Borisovc553f942020-06-03 08:55:19 +03002517 inode_add_bytes(&inode->vfs_inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002518
2519 ins.objectid = disk_bytenr;
2520 ins.offset = disk_num_bytes;
2521 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002522
Nikolay Borisovc553f942020-06-03 08:55:19 +03002523 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002524 if (ret)
2525 goto out;
2526
Nikolay Borisovc553f942020-06-03 08:55:19 +03002527 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002528 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002529out:
Yan Zhengd899e052008-10-30 14:25:28 -04002530 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002531
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002532 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002533}
2534
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002535static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002536 u64 start, u64 len)
2537{
David Sterba32da53862019-10-29 19:20:18 +01002538 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002539
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002540 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002541 ASSERT(cache);
2542
2543 spin_lock(&cache->lock);
2544 cache->delalloc_bytes -= len;
2545 spin_unlock(&cache->lock);
2546
2547 btrfs_put_block_group(cache);
2548}
2549
Qu Wenruo203f44c52020-06-10 09:04:40 +08002550static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2551 struct inode *inode,
2552 struct btrfs_ordered_extent *oe)
2553{
2554 struct btrfs_file_extent_item stack_fi;
2555 u64 logical_len;
2556
2557 memset(&stack_fi, 0, sizeof(stack_fi));
2558 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2559 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2560 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2561 oe->disk_num_bytes);
2562 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2563 logical_len = oe->truncated_len;
2564 else
2565 logical_len = oe->num_bytes;
2566 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2567 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2568 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2569 /* Encryption and other encoding is reserved and all 0 */
2570
Nikolay Borisovc553f942020-06-03 08:55:19 +03002571 return insert_reserved_file_extent(trans, BTRFS_I(inode), oe->file_offset,
Qu Wenruo7dbeaad2020-06-10 09:04:43 +08002572 &stack_fi, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002573}
2574
2575/*
2576 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002577 * an ordered extent if the range of bytes in the file it covers are
2578 * fully written.
2579 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002580static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002581{
Josef Bacik5fd02042012-05-02 14:00:54 -04002582 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002583 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002584 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002585 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002586 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002587 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002588 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002589 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002590 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002591 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002592 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002593 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002594 bool range_locked = false;
2595 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002596 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002597 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002598
Omar Sandovalbffe6332019-12-02 17:34:19 -08002599 start = ordered_extent->file_offset;
2600 end = start + ordered_extent->num_bytes - 1;
2601
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002602 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2603 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2604 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2605 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002606
Nikolay Borisov8d510122019-10-08 20:43:06 +03002607 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002608
Josef Bacik5fd02042012-05-02 14:00:54 -04002609 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2610 ret = -EIO;
2611 goto out;
2612 }
2613
Omar Sandovalbffe6332019-12-02 17:34:19 -08002614 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002615
Josef Bacik77cef2e2013-08-29 13:57:21 -04002616 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2617 truncated = true;
2618 logical_len = ordered_extent->truncated_len;
2619 /* Truncated the entire extent, don't bother adding */
2620 if (!logical_len)
2621 goto out;
2622 }
2623
Yan, Zhengc2167752009-11-12 09:34:21 +00002624 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002625 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002626
Josef Bacikd923afe2020-01-17 09:02:23 -05002627 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002628 if (freespace_inode)
2629 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002630 else
2631 trans = btrfs_join_transaction(root);
2632 if (IS_ERR(trans)) {
2633 ret = PTR_ERR(trans);
2634 trans = NULL;
2635 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002636 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002637 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002638 ret = btrfs_update_inode_fallback(trans, root, inode);
2639 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002640 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002641 goto out;
2642 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002643
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002644 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002645 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002646
Nikolay Borisov8d510122019-10-08 20:43:06 +03002647 if (freespace_inode)
2648 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002649 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002650 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002651 if (IS_ERR(trans)) {
2652 ret = PTR_ERR(trans);
2653 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002654 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002655 }
Chris Masona79b7d42014-05-22 16:18:52 -07002656
Josef Bacik69fe2d72017-10-19 14:15:57 -04002657 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002658
Chris Masonc8b97812008-10-29 14:49:59 -04002659 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002660 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002661 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002662 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002663 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002664 ordered_extent->file_offset,
2665 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002666 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002667 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002668 BUG_ON(root == fs_info->tree_root);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002669 ret = insert_ordered_extent_file_extent(trans, inode,
2670 ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002671 if (!ret) {
2672 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002673 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002674 ordered_extent->disk_bytenr,
2675 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002676 }
Yan Zhengd899e052008-10-30 14:25:28 -04002677 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002678 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002679 ordered_extent->file_offset,
2680 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002681 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002682 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002683 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002684 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002685
Nikolay Borisovac01f262018-01-08 10:59:43 +02002686 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2687 if (ret) {
2688 btrfs_abort_transaction(trans, ret);
2689 goto out;
2690 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002691
Josef Bacikd923afe2020-01-17 09:02:23 -05002692 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002693 ret = btrfs_update_inode_fallback(trans, root, inode);
2694 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002695 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002696 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002697 }
2698 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002699out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002700 clear_bits = EXTENT_DEFRAG;
2701 if (range_locked)
2702 clear_bits |= EXTENT_LOCKED;
2703 if (clear_new_delalloc_bytes)
2704 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002705 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2706 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002707 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002708
Miao Xiea698d0752012-09-20 01:51:59 -06002709 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002710 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002711
Josef Bacik77cef2e2013-08-29 13:57:21 -04002712 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002713 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002714
2715 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002716 unwritten_start += logical_len;
2717 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002718
2719 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002720 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002721
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002722 /*
2723 * If the ordered extent had an IOERR or something else went
2724 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002725 * back to the allocator. We only free the extent in the
2726 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002727 *
2728 * If we made it past insert_reserved_file_extent before we
2729 * errored out then we don't need to do this as the accounting
2730 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002731 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002732 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002733 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002734 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002735 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2736 /*
2737 * Discard the range before returning it back to the
2738 * free space pool
2739 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002740 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002741 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002742 ordered_extent->disk_bytenr,
2743 ordered_extent->disk_num_bytes,
2744 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002745 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002746 ordered_extent->disk_bytenr,
2747 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002748 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002749 }
2750
Josef Bacik5fd02042012-05-02 14:00:54 -04002751 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002752 * This needs to be done to make sure anybody waiting knows we are done
2753 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002754 */
2755 btrfs_remove_ordered_extent(inode, ordered_extent);
2756
Chris Masone6dcd2d2008-07-17 12:53:50 -04002757 /* once for us */
2758 btrfs_put_ordered_extent(ordered_extent);
2759 /* once for the tree */
2760 btrfs_put_ordered_extent(ordered_extent);
2761
Josef Bacik5fd02042012-05-02 14:00:54 -04002762 return ret;
2763}
2764
2765static void finish_ordered_fn(struct btrfs_work *work)
2766{
2767 struct btrfs_ordered_extent *ordered_extent;
2768 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2769 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002770}
2771
Nikolay Borisovc6297322018-11-08 10:18:08 +02002772void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2773 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002774{
Josef Bacik5fd02042012-05-02 14:00:54 -04002775 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002776 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002777 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002778 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002779
liubo1abe9b82011-03-24 11:18:59 +00002780 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2781
Chris Mason8b62b722009-09-02 16:53:46 -04002782 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002783 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2784 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002785 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002786
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002787 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002788 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002789 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002790 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002791
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002792 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002793 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002794}
2795
Omar Sandoval47df7762020-04-16 14:46:17 -07002796static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2797 int icsum, struct page *page, int pgoff, u64 start,
2798 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002799{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002800 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2801 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002802 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002803 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2804 u8 *csum_expected;
2805 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002806
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002807 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002808
2809 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002810 shash->tfm = fs_info->csum_shash;
2811
Eric Biggersfd080012020-04-30 23:51:59 -07002812 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002813
2814 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002815 goto zeroit;
2816
2817 kunmap_atomic(kaddr);
2818 return 0;
2819zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002820 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2821 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002822 memset(kaddr + pgoff, 1, len);
2823 flush_dcache_page(page);
2824 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002825 return -EIO;
2826}
2827
Chris Masond352ac62008-09-29 15:18:18 -04002828/*
Chris Masond352ac62008-09-29 15:18:18 -04002829 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002830 * if there's a match, we allow the bio to finish. If not, the code in
2831 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002832 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002833static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2834 u64 phy_offset, struct page *page,
2835 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002836{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002837 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002838 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002839 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002840 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002841
Chris Masond20f7042008-12-08 16:58:54 -05002842 if (PageChecked(page)) {
2843 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002844 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002845 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002846
2847 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002848 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002849
Yan Zheng17d217f2008-12-12 10:03:38 -05002850 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002851 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002852 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002853 return 0;
2854 }
2855
Miao Xiefacc8a222013-07-25 19:22:34 +08002856 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002857 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2858 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002859}
Chris Masonb888db22007-08-27 16:49:44 -04002860
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002861/*
2862 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2863 *
2864 * @inode: The inode we want to perform iput on
2865 *
2866 * This function uses the generic vfs_inode::i_count to track whether we should
2867 * just decrement it (in case it's > 1) or if this is the last iput then link
2868 * the inode to the delayed iput machinery. Delayed iputs are processed at
2869 * transaction commit time/superblock commit/cleaner kthread.
2870 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002871void btrfs_add_delayed_iput(struct inode *inode)
2872{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002873 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002874 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002875
2876 if (atomic_add_unless(&inode->i_count, -1, 1))
2877 return;
2878
Josef Bacik034f7842018-12-03 11:06:52 -05002879 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002880 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002881 ASSERT(list_empty(&binode->delayed_iput));
2882 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002883 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002884 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2885 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002886}
2887
Josef Bacik63611e72019-06-18 10:59:18 -04002888static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2889 struct btrfs_inode *inode)
2890{
2891 list_del_init(&inode->delayed_iput);
2892 spin_unlock(&fs_info->delayed_iput_lock);
2893 iput(&inode->vfs_inode);
2894 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2895 wake_up(&fs_info->delayed_iputs_wait);
2896 spin_lock(&fs_info->delayed_iput_lock);
2897}
2898
2899static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2900 struct btrfs_inode *inode)
2901{
2902 if (!list_empty(&inode->delayed_iput)) {
2903 spin_lock(&fs_info->delayed_iput_lock);
2904 if (!list_empty(&inode->delayed_iput))
2905 run_delayed_iput_locked(fs_info, inode);
2906 spin_unlock(&fs_info->delayed_iput_lock);
2907 }
2908}
2909
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002910void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002911{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002912
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002913 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002914 while (!list_empty(&fs_info->delayed_iputs)) {
2915 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002916
David Sterba8089fe62015-11-19 14:15:51 +01002917 inode = list_first_entry(&fs_info->delayed_iputs,
2918 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002919 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002920 }
David Sterba8089fe62015-11-19 14:15:51 +01002921 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002922}
2923
Josef Bacik034f7842018-12-03 11:06:52 -05002924/**
2925 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2926 * @fs_info - the fs_info for this fs
2927 * @return - EINTR if we were killed, 0 if nothing's pending
2928 *
2929 * This will wait on any delayed iputs that are currently running with KILLABLE
2930 * set. Once they are all done running we will return, unless we are killed in
2931 * which case we return EINTR. This helps in user operations like fallocate etc
2932 * that might get blocked on the iputs.
2933 */
2934int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2935{
2936 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2937 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2938 if (ret)
2939 return -EINTR;
2940 return 0;
2941}
2942
Yan, Zhengd68fc572010-05-16 10:49:58 -04002943/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002944 * This creates an orphan entry for the given inode in case something goes wrong
2945 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002946 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002947int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002948 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002949{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002950 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002951
Omar Sandoval27919062018-05-11 13:13:37 -07002952 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2953 if (ret && ret != -EEXIST) {
2954 btrfs_abort_transaction(trans, ret);
2955 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002956 }
2957
Yan, Zhengd68fc572010-05-16 10:49:58 -04002958 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002959}
2960
2961/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002962 * We have done the delete so we can go ahead and remove the orphan item for
2963 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002964 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002965static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002966 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002967{
Omar Sandoval27919062018-05-11 13:13:37 -07002968 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002969}
2970
2971/*
2972 * this cleans up any orphans that may be left on the list from the last use
2973 * of this root.
2974 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002975int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002976{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002977 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002978 struct btrfs_path *path;
2979 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002980 struct btrfs_key key, found_key;
2981 struct btrfs_trans_handle *trans;
2982 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002983 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002984 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002985
Yan, Zhengd68fc572010-05-16 10:49:58 -04002986 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002987 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002988
2989 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002990 if (!path) {
2991 ret = -ENOMEM;
2992 goto out;
2993 }
David Sterbae4058b52015-11-27 16:31:35 +01002994 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002995
2996 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002997 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04002998 key.offset = (u64)-1;
2999
Josef Bacik7b128762008-07-24 12:17:14 -04003000 while (1) {
3001 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003002 if (ret < 0)
3003 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003004
3005 /*
3006 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003007 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003008 * find the key and see if we have stuff that matches
3009 */
3010 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003011 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003012 if (path->slots[0] == 0)
3013 break;
3014 path->slots[0]--;
3015 }
3016
3017 /* pull out the item */
3018 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003019 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3020
3021 /* make sure the item matches what we want */
3022 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3023 break;
David Sterba962a2982014-06-04 18:41:45 +02003024 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003025 break;
3026
3027 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003028 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003029
3030 /*
3031 * this is where we are basically btrfs_lookup, without the
3032 * crossing root thing. we store the inode number in the
3033 * offset of the orphan item.
3034 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003035
3036 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003037 btrfs_err(fs_info,
3038 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003039 ret = -EINVAL;
3040 goto out;
3041 }
3042
3043 last_objectid = found_key.offset;
3044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003045 found_key.objectid = found_key.offset;
3046 found_key.type = BTRFS_INODE_ITEM_KEY;
3047 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003048 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303049 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003050 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003051 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003052
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003053 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003054 struct btrfs_root *dead_root;
3055 struct btrfs_fs_info *fs_info = root->fs_info;
3056 int is_dead_root = 0;
3057
3058 /*
3059 * this is an orphan in the tree root. Currently these
3060 * could come from 2 sources:
3061 * a) a snapshot deletion in progress
3062 * b) a free space cache inode
3063 * We need to distinguish those two, as the snapshot
3064 * orphan must not get deleted.
3065 * find_dead_roots already ran before us, so if this
3066 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003067 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003068 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003069
3070 spin_lock(&fs_info->fs_roots_radix_lock);
3071 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3072 (unsigned long)found_key.objectid);
3073 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3074 is_dead_root = 1;
3075 spin_unlock(&fs_info->fs_roots_radix_lock);
3076
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003077 if (is_dead_root) {
3078 /* prevent this orphan from being found again */
3079 key.offset = found_key.objectid - 1;
3080 continue;
3081 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003082
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003083 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003084
Josef Bacika8c9e572011-09-21 16:55:59 -04003085 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003086 * If we have an inode with links, there are a couple of
3087 * possibilities. Old kernels (before v3.12) used to create an
3088 * orphan item for truncate indicating that there were possibly
3089 * extent items past i_size that needed to be deleted. In v3.12,
3090 * truncate was changed to update i_size in sync with the extent
3091 * items, but the (useless) orphan item was still created. Since
3092 * v4.18, we don't create the orphan item for truncate at all.
3093 *
3094 * So, this item could mean that we need to do a truncate, but
3095 * only if this filesystem was last used on a pre-v3.12 kernel
3096 * and was not cleanly unmounted. The odds of that are quite
3097 * slim, and it's a pain to do the truncate now, so just delete
3098 * the orphan item.
3099 *
3100 * It's also possible that this orphan item was supposed to be
3101 * deleted but wasn't. The inode number may have been reused,
3102 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003103 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003104 if (ret == -ENOENT || inode->i_nlink) {
3105 if (!ret)
3106 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003107 trans = btrfs_start_transaction(root, 1);
3108 if (IS_ERR(trans)) {
3109 ret = PTR_ERR(trans);
3110 goto out;
3111 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003112 btrfs_debug(fs_info, "auto deleting %Lu",
3113 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003114 ret = btrfs_del_orphan_item(trans, root,
3115 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003116 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003117 if (ret)
3118 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003119 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003120 }
Josef Bacik7b128762008-07-24 12:17:14 -04003121
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003122 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003123
3124 /* this will do delete_inode and everything for us */
3125 iput(inode);
3126 }
Miao Xie3254c872011-11-10 20:45:05 -05003127 /* release the path since we're done with it */
3128 btrfs_release_path(path);
3129
Yan, Zhengd68fc572010-05-16 10:49:58 -04003130 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3131
Omar Sandovala575cee2018-05-11 13:13:38 -07003132 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003133 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003134 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003135 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003136 }
Josef Bacik7b128762008-07-24 12:17:14 -04003137
3138 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003139 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003140
3141out:
3142 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003143 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003144 btrfs_free_path(path);
3145 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003146}
3147
Chris Masond352ac62008-09-29 15:18:18 -04003148/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003149 * very simple check to peek ahead in the leaf looking for xattrs. If we
3150 * don't find any xattrs, we know there can't be any acls.
3151 *
3152 * slot is the slot the inode is in, objectid is the objectid of the inode
3153 */
3154static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003155 int slot, u64 objectid,
3156 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003157{
3158 u32 nritems = btrfs_header_nritems(leaf);
3159 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003160 static u64 xattr_access = 0;
3161 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003162 int scanned = 0;
3163
Josef Bacikf23b5a52013-06-19 10:16:26 -04003164 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003165 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3166 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3167 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3168 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003169 }
3170
Chris Mason46a53cc2009-04-27 11:47:50 -04003171 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003172 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003173 while (slot < nritems) {
3174 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3175
3176 /* we found a different objectid, there must not be acls */
3177 if (found_key.objectid != objectid)
3178 return 0;
3179
3180 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003181 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003182 if (*first_xattr_slot == -1)
3183 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003184 if (found_key.offset == xattr_access ||
3185 found_key.offset == xattr_default)
3186 return 1;
3187 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003188
3189 /*
3190 * we found a key greater than an xattr key, there can't
3191 * be any acls later on
3192 */
3193 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3194 return 0;
3195
3196 slot++;
3197 scanned++;
3198
3199 /*
3200 * it goes inode, inode backrefs, xattrs, extents,
3201 * so if there are a ton of hard links to an inode there can
3202 * be a lot of backrefs. Don't waste time searching too hard,
3203 * this is just an optimization
3204 */
3205 if (scanned >= 8)
3206 break;
3207 }
3208 /* we hit the end of the leaf before we found an xattr or
3209 * something larger than an xattr. We have to assume the inode
3210 * has acls
3211 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003212 if (*first_xattr_slot == -1)
3213 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003214 return 1;
3215}
3216
3217/*
Chris Masond352ac62008-09-29 15:18:18 -04003218 * read an inode from the btree into the in-memory inode
3219 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003220static int btrfs_read_locked_inode(struct inode *inode,
3221 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003222{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003223 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003224 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003225 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003226 struct btrfs_inode_item *inode_item;
3227 struct btrfs_root *root = BTRFS_I(inode)->root;
3228 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003229 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003230 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003231 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003232 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003233 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003234 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003235
3236 ret = btrfs_fill_inode(inode, &rdev);
3237 if (!ret)
3238 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003239
Filipe Manana4222ea72018-10-24 10:13:03 +01003240 if (!path) {
3241 path = btrfs_alloc_path();
3242 if (!path)
3243 return -ENOMEM;
3244 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003245
Chris Mason39279cc2007-06-12 06:35:45 -04003246 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003247
Chris Mason39279cc2007-06-12 06:35:45 -04003248 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003249 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003250 if (path != in_path)
3251 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003252 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003253 }
Chris Mason39279cc2007-06-12 06:35:45 -04003254
Chris Mason5f39d392007-10-15 16:14:19 -04003255 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003256
3257 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003258 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003259
Chris Mason5f39d392007-10-15 16:14:19 -04003260 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3261 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003262 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003263 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003264 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3265 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003266 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003267 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3268 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003269
David Sterbaa937b972014-12-12 17:39:12 +01003270 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3271 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003272
David Sterbaa937b972014-12-12 17:39:12 +01003273 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3274 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003275
David Sterbaa937b972014-12-12 17:39:12 +01003276 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3277 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003278
chandan r9cc97d62012-07-04 12:48:07 +05303279 BTRFS_I(inode)->i_otime.tv_sec =
3280 btrfs_timespec_sec(leaf, &inode_item->otime);
3281 BTRFS_I(inode)->i_otime.tv_nsec =
3282 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003283
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003284 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003285 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003286 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3287
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003288 inode_set_iversion_queried(inode,
3289 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003290 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003291 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003292 rdev = btrfs_inode_rdev(leaf, inode_item);
3293
Josef Bacikaec74772008-07-24 12:12:38 -04003294 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003295 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003296
3297cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003298 /*
3299 * If we were modified in the current generation and evicted from memory
3300 * and then re-read we need to do a full sync since we don't have any
3301 * idea about which extents were modified before we were evicted from
3302 * cache.
3303 *
3304 * This is required for both inode re-read from disk and delayed inode
3305 * in delayed_nodes_tree.
3306 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003307 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003308 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3309 &BTRFS_I(inode)->runtime_flags);
3310
Filipe Mananabde6c242015-07-24 00:00:19 +01003311 /*
3312 * We don't persist the id of the transaction where an unlink operation
3313 * against the inode was last made. So here we assume the inode might
3314 * have been evicted, and therefore the exact value of last_unlink_trans
3315 * lost, and set it to last_trans to avoid metadata inconsistencies
3316 * between the inode and its parent if the inode is fsync'ed and the log
3317 * replayed. For example, in the scenario:
3318 *
3319 * touch mydir/foo
3320 * ln mydir/foo mydir/bar
3321 * sync
3322 * unlink mydir/bar
3323 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3324 * xfs_io -c fsync mydir/foo
3325 * <power failure>
3326 * mount fs, triggers fsync log replay
3327 *
3328 * We must make sure that when we fsync our inode foo we also log its
3329 * parent inode, otherwise after log replay the parent still has the
3330 * dentry with the "bar" name but our inode foo has a link count of 1
3331 * and doesn't have an inode ref with the name "bar" anymore.
3332 *
3333 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003334 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003335 * transaction commits on fsync if our inode is a directory, or if our
3336 * inode is not a directory, logging its parent unnecessarily.
3337 */
3338 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3339
Miao Xie67de1172013-12-26 13:07:06 +08003340 path->slots[0]++;
3341 if (inode->i_nlink != 1 ||
3342 path->slots[0] >= btrfs_header_nritems(leaf))
3343 goto cache_acl;
3344
3345 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003346 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003347 goto cache_acl;
3348
3349 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3350 if (location.type == BTRFS_INODE_REF_KEY) {
3351 struct btrfs_inode_ref *ref;
3352
3353 ref = (struct btrfs_inode_ref *)ptr;
3354 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3355 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3356 struct btrfs_inode_extref *extref;
3357
3358 extref = (struct btrfs_inode_extref *)ptr;
3359 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3360 extref);
3361 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003362cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003363 /*
3364 * try to precache a NULL acl entry for files that don't have
3365 * any xattrs or acls
3366 */
Li Zefan33345d012011-04-20 10:31:50 +08003367 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003368 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003369 if (first_xattr_slot != -1) {
3370 path->slots[0] = first_xattr_slot;
3371 ret = btrfs_load_inode_props(inode, path);
3372 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003373 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003374 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003375 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003376 root->root_key.objectid, ret);
3377 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003378 if (path != in_path)
3379 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003380
Al Viro72c04902009-06-24 16:58:48 -04003381 if (!maybe_acls)
3382 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003383
Chris Mason39279cc2007-06-12 06:35:45 -04003384 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003385 case S_IFREG:
3386 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003387 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003388 inode->i_fop = &btrfs_file_operations;
3389 inode->i_op = &btrfs_file_inode_operations;
3390 break;
3391 case S_IFDIR:
3392 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003393 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003394 break;
3395 case S_IFLNK:
3396 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003397 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003398 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003399 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003400 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003401 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003402 init_special_inode(inode, inode->i_mode, rdev);
3403 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003404 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003405
David Sterba7b6a2212018-03-26 18:40:21 +02003406 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003407 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003408}
3409
Chris Masond352ac62008-09-29 15:18:18 -04003410/*
3411 * given a leaf and an inode, copy the inode fields into the leaf
3412 */
Chris Masone02119d2008-09-05 16:13:11 -04003413static void fill_inode_item(struct btrfs_trans_handle *trans,
3414 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003415 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003416 struct inode *inode)
3417{
Liu Bo51fab692012-12-27 09:01:21 +00003418 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003419
David Sterbac82f8232019-08-09 17:48:21 +02003420 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003421
David Sterbacc4c13d2020-04-29 02:15:56 +02003422 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3423 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3424 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3425 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3426 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003427
David Sterbacc4c13d2020-04-29 02:15:56 +02003428 btrfs_set_token_timespec_sec(&token, &item->atime,
3429 inode->i_atime.tv_sec);
3430 btrfs_set_token_timespec_nsec(&token, &item->atime,
3431 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003432
David Sterbacc4c13d2020-04-29 02:15:56 +02003433 btrfs_set_token_timespec_sec(&token, &item->mtime,
3434 inode->i_mtime.tv_sec);
3435 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3436 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003437
David Sterbacc4c13d2020-04-29 02:15:56 +02003438 btrfs_set_token_timespec_sec(&token, &item->ctime,
3439 inode->i_ctime.tv_sec);
3440 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3441 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003442
David Sterbacc4c13d2020-04-29 02:15:56 +02003443 btrfs_set_token_timespec_sec(&token, &item->otime,
3444 BTRFS_I(inode)->i_otime.tv_sec);
3445 btrfs_set_token_timespec_nsec(&token, &item->otime,
3446 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303447
David Sterbacc4c13d2020-04-29 02:15:56 +02003448 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3449 btrfs_set_token_inode_generation(&token, item,
3450 BTRFS_I(inode)->generation);
3451 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3452 btrfs_set_token_inode_transid(&token, item, trans->transid);
3453 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3454 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3455 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003456}
3457
Chris Masond352ac62008-09-29 15:18:18 -04003458/*
3459 * copy everything in the in-memory inode into the btree.
3460 */
Chris Mason21151332011-11-10 20:39:08 -05003461static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003462 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003463{
3464 struct btrfs_inode_item *inode_item;
3465 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003466 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003467 int ret;
3468
3469 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003470 if (!path)
3471 return -ENOMEM;
3472
Chris Masonb9473432009-03-13 11:00:37 -04003473 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003474 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3475 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003476 if (ret) {
3477 if (ret > 0)
3478 ret = -ENOENT;
3479 goto failed;
3480 }
3481
Chris Mason5f39d392007-10-15 16:14:19 -04003482 leaf = path->nodes[0];
3483 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003484 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003485
Chris Masone02119d2008-09-05 16:13:11 -04003486 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003487 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003488 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003489 ret = 0;
3490failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003491 btrfs_free_path(path);
3492 return ret;
3493}
3494
Chris Masond352ac62008-09-29 15:18:18 -04003495/*
Chris Mason21151332011-11-10 20:39:08 -05003496 * copy everything in the in-memory inode into the btree.
3497 */
3498noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3499 struct btrfs_root *root, struct inode *inode)
3500{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003501 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003502 int ret;
3503
3504 /*
3505 * If the inode is a free space inode, we can deadlock during commit
3506 * if we put it into the delayed code.
3507 *
3508 * The data relocation inode should also be directly updated
3509 * without delay
3510 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003511 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003512 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003513 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003514 btrfs_update_root_times(trans, root);
3515
Chris Mason21151332011-11-10 20:39:08 -05003516 ret = btrfs_delayed_update_inode(trans, root, inode);
3517 if (!ret)
3518 btrfs_set_inode_last_trans(trans, inode);
3519 return ret;
3520 }
3521
3522 return btrfs_update_inode_item(trans, root, inode);
3523}
3524
Josef Bacikbe6aef62012-10-22 15:43:12 -04003525noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3526 struct btrfs_root *root,
3527 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003528{
3529 int ret;
3530
3531 ret = btrfs_update_inode(trans, root, inode);
3532 if (ret == -ENOSPC)
3533 return btrfs_update_inode_item(trans, root, inode);
3534 return ret;
3535}
3536
3537/*
Chris Masond352ac62008-09-29 15:18:18 -04003538 * unlink helper that gets used here in inode.c and in the tree logging
3539 * recovery code. It remove a link in a directory with a given name, and
3540 * also drops the back refs in the inode to the directory
3541 */
Al Viro92986792011-03-04 17:14:37 +00003542static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3543 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003544 struct btrfs_inode *dir,
3545 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003546 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003547{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003548 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003549 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003550 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003551 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003552 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003553 u64 ino = btrfs_ino(inode);
3554 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003555
3556 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003557 if (!path) {
3558 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003559 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003560 }
3561
Chris Masonb9473432009-03-13 11:00:37 -04003562 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003563 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003564 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003565 if (IS_ERR_OR_NULL(di)) {
3566 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003567 goto err;
3568 }
Chris Mason39279cc2007-06-12 06:35:45 -04003569 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003570 if (ret)
3571 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003572 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003573
Miao Xie67de1172013-12-26 13:07:06 +08003574 /*
3575 * If we don't have dir index, we have to get it by looking up
3576 * the inode ref, since we get the inode ref, remove it directly,
3577 * it is unnecessary to do delayed deletion.
3578 *
3579 * But if we have dir index, needn't search inode ref to get it.
3580 * Since the inode ref is close to the inode item, it is better
3581 * that we delay to delete it, and just do this deletion when
3582 * we update the inode item.
3583 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003584 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003585 ret = btrfs_delayed_delete_inode_ref(inode);
3586 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003587 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003588 goto skip_backref;
3589 }
3590 }
3591
Li Zefan33345d012011-04-20 10:31:50 +08003592 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3593 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003594 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003595 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003596 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003597 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003598 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003599 goto err;
3600 }
Miao Xie67de1172013-12-26 13:07:06 +08003601skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003602 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003603 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003604 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003605 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003606 }
Chris Mason39279cc2007-06-12 06:35:45 -04003607
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003608 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3609 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003610 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003611 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003612 goto err;
3613 }
Chris Masone02119d2008-09-05 16:13:11 -04003614
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003615 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3616 index);
Chris Mason6418c962010-10-30 07:34:24 -04003617 if (ret == -ENOENT)
3618 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003619 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003620 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003621
3622 /*
3623 * If we have a pending delayed iput we could end up with the final iput
3624 * being run in btrfs-cleaner context. If we have enough of these built
3625 * up we can end up burning a lot of time in btrfs-cleaner without any
3626 * way to throttle the unlinks. Since we're currently holding a ref on
3627 * the inode we can run the delayed iput here without any issues as the
3628 * final iput won't be done until after we drop the ref we're currently
3629 * holding.
3630 */
3631 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003632err:
3633 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003634 if (ret)
3635 goto out;
3636
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003637 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003638 inode_inc_iversion(&inode->vfs_inode);
3639 inode_inc_iversion(&dir->vfs_inode);
3640 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3641 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3642 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003643out:
Chris Mason39279cc2007-06-12 06:35:45 -04003644 return ret;
3645}
3646
Al Viro92986792011-03-04 17:14:37 +00003647int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3648 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003649 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003650 const char *name, int name_len)
3651{
3652 int ret;
3653 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3654 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003655 drop_nlink(&inode->vfs_inode);
3656 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003657 }
3658 return ret;
3659}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003660
3661/*
3662 * helper to start transaction for unlink and rmdir.
3663 *
Josef Bacikd52be812013-05-29 14:54:47 -04003664 * unlink and rmdir are special in btrfs, they do not always free space, so
3665 * if we cannot make our reservations the normal way try and see if there is
3666 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3667 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668 */
Josef Bacikd52be812013-05-29 14:54:47 -04003669static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003670{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003671 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003672
Josef Bacike70bea52011-10-11 14:18:24 -04003673 /*
3674 * 1 for the possible orphan item
3675 * 1 for the dir item
3676 * 1 for the dir index
3677 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003678 * 1 for the inode
3679 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003680 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681}
3682
Chris Mason39279cc2007-06-12 06:35:45 -04003683static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3684{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003685 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003686 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003687 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003688 int ret;
3689
Josef Bacikd52be812013-05-29 14:54:47 -04003690 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003691 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003692 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003693
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003694 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3695 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003696
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003697 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3698 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3699 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003700 if (ret)
3701 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003702
Yan, Zhenga22285a2010-05-16 10:48:46 -04003703 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003704 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003705 if (ret)
3706 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003707 }
Josef Bacik7b128762008-07-24 12:17:14 -04003708
Tsutomu Itohb5324022011-07-19 07:27:20 +00003709out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003710 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003711 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003712 return ret;
3713}
3714
Misono Tomohirof60a2362018-04-18 11:34:52 +09003715static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003716 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003717{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003718 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003719 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003720 struct btrfs_path *path;
3721 struct extent_buffer *leaf;
3722 struct btrfs_dir_item *di;
3723 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003724 const char *name = dentry->d_name.name;
3725 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003726 u64 index;
3727 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003728 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003729 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003730
Josef Bacik045d3962019-12-18 17:20:27 -05003731 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3732 objectid = inode->root->root_key.objectid;
3733 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3734 objectid = inode->location.objectid;
3735 } else {
3736 WARN_ON(1);
3737 return -EINVAL;
3738 }
3739
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003740 path = btrfs_alloc_path();
3741 if (!path)
3742 return -ENOMEM;
3743
Li Zefan33345d012011-04-20 10:31:50 +08003744 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003745 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003746 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003747 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003748 goto out;
3749 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003750
3751 leaf = path->nodes[0];
3752 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3753 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3754 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003755 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003756 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003757 goto out;
3758 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003759 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003760
Josef Bacikd49d3282019-12-18 17:20:28 -05003761 /*
3762 * This is a placeholder inode for a subvolume we didn't have a
3763 * reference to at the time of the snapshot creation. In the meantime
3764 * we could have renamed the real subvol link into our snapshot, so
3765 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3766 * Instead simply lookup the dir_index_item for this entry so we can
3767 * remove it. Otherwise we know we have a ref to the root and we can
3768 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3769 */
3770 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003771 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003772 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003773 if (IS_ERR_OR_NULL(di)) {
3774 if (!di)
3775 ret = -ENOENT;
3776 else
3777 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003778 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003779 goto out;
3780 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003781
3782 leaf = path->nodes[0];
3783 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003784 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003785 btrfs_release_path(path);
3786 } else {
3787 ret = btrfs_del_root_ref(trans, objectid,
3788 root->root_key.objectid, dir_ino,
3789 &index, name, name_len);
3790 if (ret) {
3791 btrfs_abort_transaction(trans, ret);
3792 goto out;
3793 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003794 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003795
Lu Fengqi9add2942018-08-01 11:32:26 +08003796 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003797 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003798 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003799 goto out;
3800 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003801
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003802 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003803 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003804 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003805 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003806 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003807 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003808out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003809 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003810 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003811}
3812
Misono Tomohiroec42f162018-04-18 11:34:13 +09003813/*
3814 * Helper to check if the subvolume references other subvolumes or if it's
3815 * default.
3816 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003817static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003818{
3819 struct btrfs_fs_info *fs_info = root->fs_info;
3820 struct btrfs_path *path;
3821 struct btrfs_dir_item *di;
3822 struct btrfs_key key;
3823 u64 dir_id;
3824 int ret;
3825
3826 path = btrfs_alloc_path();
3827 if (!path)
3828 return -ENOMEM;
3829
3830 /* Make sure this root isn't set as the default subvol */
3831 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3832 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3833 dir_id, "default", 7, 0);
3834 if (di && !IS_ERR(di)) {
3835 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3836 if (key.objectid == root->root_key.objectid) {
3837 ret = -EPERM;
3838 btrfs_err(fs_info,
3839 "deleting default subvolume %llu is not allowed",
3840 key.objectid);
3841 goto out;
3842 }
3843 btrfs_release_path(path);
3844 }
3845
3846 key.objectid = root->root_key.objectid;
3847 key.type = BTRFS_ROOT_REF_KEY;
3848 key.offset = (u64)-1;
3849
3850 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3851 if (ret < 0)
3852 goto out;
3853 BUG_ON(ret == 0);
3854
3855 ret = 0;
3856 if (path->slots[0] > 0) {
3857 path->slots[0]--;
3858 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3859 if (key.objectid == root->root_key.objectid &&
3860 key.type == BTRFS_ROOT_REF_KEY)
3861 ret = -ENOTEMPTY;
3862 }
3863out:
3864 btrfs_free_path(path);
3865 return ret;
3866}
3867
Nikolay Borisov20a68002018-04-27 14:36:24 +03003868/* Delete all dentries for inodes belonging to the root */
3869static void btrfs_prune_dentries(struct btrfs_root *root)
3870{
3871 struct btrfs_fs_info *fs_info = root->fs_info;
3872 struct rb_node *node;
3873 struct rb_node *prev;
3874 struct btrfs_inode *entry;
3875 struct inode *inode;
3876 u64 objectid = 0;
3877
3878 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3879 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3880
3881 spin_lock(&root->inode_lock);
3882again:
3883 node = root->inode_tree.rb_node;
3884 prev = NULL;
3885 while (node) {
3886 prev = node;
3887 entry = rb_entry(node, struct btrfs_inode, rb_node);
3888
David Sterba37508512018-06-29 10:56:40 +02003889 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003890 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003891 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003892 node = node->rb_right;
3893 else
3894 break;
3895 }
3896 if (!node) {
3897 while (prev) {
3898 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003899 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003900 node = prev;
3901 break;
3902 }
3903 prev = rb_next(prev);
3904 }
3905 }
3906 while (node) {
3907 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003908 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003909 inode = igrab(&entry->vfs_inode);
3910 if (inode) {
3911 spin_unlock(&root->inode_lock);
3912 if (atomic_read(&inode->i_count) > 1)
3913 d_prune_aliases(inode);
3914 /*
3915 * btrfs_drop_inode will have it removed from the inode
3916 * cache when its usage count hits zero.
3917 */
3918 iput(inode);
3919 cond_resched();
3920 spin_lock(&root->inode_lock);
3921 goto again;
3922 }
3923
3924 if (cond_resched_lock(&root->inode_lock))
3925 goto again;
3926
3927 node = rb_next(node);
3928 }
3929 spin_unlock(&root->inode_lock);
3930}
3931
Misono Tomohirof60a2362018-04-18 11:34:52 +09003932int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3933{
3934 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3935 struct btrfs_root *root = BTRFS_I(dir)->root;
3936 struct inode *inode = d_inode(dentry);
3937 struct btrfs_root *dest = BTRFS_I(inode)->root;
3938 struct btrfs_trans_handle *trans;
3939 struct btrfs_block_rsv block_rsv;
3940 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003941 int ret;
3942 int err;
3943
3944 /*
3945 * Don't allow to delete a subvolume with send in progress. This is
3946 * inside the inode lock so the error handling that has to drop the bit
3947 * again is not run concurrently.
3948 */
3949 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003950 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003951 spin_unlock(&dest->root_item_lock);
3952 btrfs_warn(fs_info,
3953 "attempt to delete subvolume %llu during send",
3954 dest->root_key.objectid);
3955 return -EPERM;
3956 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003957 root_flags = btrfs_root_flags(&dest->root_item);
3958 btrfs_set_root_flags(&dest->root_item,
3959 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3960 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003961
3962 down_write(&fs_info->subvol_sem);
3963
3964 err = may_destroy_subvol(dest);
3965 if (err)
3966 goto out_up_write;
3967
3968 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3969 /*
3970 * One for dir inode,
3971 * two for dir entries,
3972 * two for root ref/backref.
3973 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003974 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003975 if (err)
3976 goto out_up_write;
3977
3978 trans = btrfs_start_transaction(root, 0);
3979 if (IS_ERR(trans)) {
3980 err = PTR_ERR(trans);
3981 goto out_release;
3982 }
3983 trans->block_rsv = &block_rsv;
3984 trans->bytes_reserved = block_rsv.size;
3985
3986 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3987
Josef Bacik045d3962019-12-18 17:20:27 -05003988 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003989 if (ret) {
3990 err = ret;
3991 btrfs_abort_transaction(trans, ret);
3992 goto out_end_trans;
3993 }
3994
3995 btrfs_record_root_in_trans(trans, dest);
3996
3997 memset(&dest->root_item.drop_progress, 0,
3998 sizeof(dest->root_item.drop_progress));
3999 dest->root_item.drop_level = 0;
4000 btrfs_set_root_refs(&dest->root_item, 0);
4001
4002 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4003 ret = btrfs_insert_orphan_item(trans,
4004 fs_info->tree_root,
4005 dest->root_key.objectid);
4006 if (ret) {
4007 btrfs_abort_transaction(trans, ret);
4008 err = ret;
4009 goto out_end_trans;
4010 }
4011 }
4012
Lu Fengqid1957792018-05-29 15:01:54 +08004013 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004014 BTRFS_UUID_KEY_SUBVOL,
4015 dest->root_key.objectid);
4016 if (ret && ret != -ENOENT) {
4017 btrfs_abort_transaction(trans, ret);
4018 err = ret;
4019 goto out_end_trans;
4020 }
4021 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004022 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004023 dest->root_item.received_uuid,
4024 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4025 dest->root_key.objectid);
4026 if (ret && ret != -ENOENT) {
4027 btrfs_abort_transaction(trans, ret);
4028 err = ret;
4029 goto out_end_trans;
4030 }
4031 }
4032
4033out_end_trans:
4034 trans->block_rsv = NULL;
4035 trans->bytes_reserved = 0;
4036 ret = btrfs_end_transaction(trans);
4037 if (ret && !err)
4038 err = ret;
4039 inode->i_flags |= S_DEAD;
4040out_release:
4041 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4042out_up_write:
4043 up_write(&fs_info->subvol_sem);
4044 if (err) {
4045 spin_lock(&dest->root_item_lock);
4046 root_flags = btrfs_root_flags(&dest->root_item);
4047 btrfs_set_root_flags(&dest->root_item,
4048 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4049 spin_unlock(&dest->root_item_lock);
4050 } else {
4051 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004052 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004053 ASSERT(dest->send_in_progress == 0);
4054
4055 /* the last ref */
4056 if (dest->ino_cache_inode) {
4057 iput(dest->ino_cache_inode);
4058 dest->ino_cache_inode = NULL;
4059 }
4060 }
4061
4062 return err;
4063}
4064
Chris Mason39279cc2007-06-12 06:35:45 -04004065static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4066{
David Howells2b0143b2015-03-17 22:25:59 +00004067 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004068 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004069 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004070 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004071 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004072
David Sterbab3ae2442012-09-13 16:04:34 -06004073 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004074 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004075 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004076 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004077
Josef Bacikd52be812013-05-29 14:54:47 -04004078 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004079 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004080 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004081
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004082 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004083 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004084 goto out;
4085 }
4086
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004087 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004088 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004089 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004090
Filipe Manana44f714d2016-06-06 16:11:13 +01004091 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4092
Chris Mason39279cc2007-06-12 06:35:45 -04004093 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004094 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4095 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4096 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004097 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004098 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004099 /*
4100 * Propagate the last_unlink_trans value of the deleted dir to
4101 * its parent directory. This is to prevent an unrecoverable
4102 * log tree in the case we do something like this:
4103 * 1) create dir foo
4104 * 2) create snapshot under dir foo
4105 * 3) delete the snapshot
4106 * 4) rmdir foo
4107 * 5) mkdir foo
4108 * 6) fsync foo or some file inside foo
4109 */
4110 if (last_unlink_trans >= trans->transid)
4111 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4112 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004113out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004114 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004115 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004116
Chris Mason39279cc2007-06-12 06:35:45 -04004117 return err;
4118}
4119
Josef Bacikddfae632017-10-19 14:16:02 -04004120/*
4121 * Return this if we need to call truncate_block for the last bit of the
4122 * truncate.
4123 */
4124#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004125
Chris Mason323ac952008-10-01 19:05:46 -04004126/*
Chris Mason39279cc2007-06-12 06:35:45 -04004127 * this can truncate away extent items, csum items and directory items.
4128 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004129 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004130 *
4131 * csum items that cross the new i_size are truncated to the new size
4132 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004133 *
4134 * min_type is the minimum key type to truncate down to. If set to 0, this
4135 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004136 */
Yan, Zheng80825102009-11-12 09:35:36 +00004137int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4138 struct btrfs_root *root,
4139 struct inode *inode,
4140 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004141{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004142 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004143 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004144 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004145 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004146 struct btrfs_key key;
4147 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004148 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004149 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004150 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004151 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004152 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004153 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004154 int found_extent;
4155 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004156 int pending_del_nr = 0;
4157 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004158 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004159 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004160 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004161 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004162 bool be_nice = false;
4163 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004164 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4165 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004166
4167 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004168
Chris Mason28ed1342014-12-17 09:41:04 -08004169 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004170 * For non-free space inodes and non-shareable roots, we want to back
4171 * off from time to time. This means all inodes in subvolume roots,
4172 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004173 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004174 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004175 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004176 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004177
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004178 path = btrfs_alloc_path();
4179 if (!path)
4180 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004181 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004182
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004183 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004184 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4185 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004186
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004187 /*
4188 * We want to drop from the next block forward in case this
4189 * new size is not block aligned since we will be keeping the
4190 * last block of the extent just the way it is.
4191 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004192 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004193 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004194 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004195 }
Yan, Zheng80825102009-11-12 09:35:36 +00004196
Miao Xie16cdcec2011-04-22 18:12:22 +08004197 /*
4198 * This function is also used to drop the items in the log tree before
4199 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004200 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004201 * items.
4202 */
4203 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004204 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004205
Li Zefan33345d012011-04-20 10:31:50 +08004206 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004207 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004208 key.type = (u8)-1;
4209
Chris Mason85e21ba2008-01-29 15:11:36 -05004210search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004211 /*
4212 * with a 16K leaf size and 128MB extents, you can actually queue
4213 * up a huge file in a single leaf. Most of the time that
4214 * bytes_deleted is > 0, it will be huge by the time we get here
4215 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004216 if (be_nice && bytes_deleted > SZ_32M &&
4217 btrfs_should_end_transaction(trans)) {
4218 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004219 goto out;
4220 }
Chris Masond3977122009-01-05 21:25:51 -05004221
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004222 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4223 if (ret < 0)
4224 goto out;
4225
Chris Mason85e21ba2008-01-29 15:11:36 -05004226 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004227 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004228 /* there are no items in the tree for us to truncate, we're
4229 * done
4230 */
Yan, Zheng80825102009-11-12 09:35:36 +00004231 if (path->slots[0] == 0)
4232 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004233 path->slots[0]--;
4234 }
4235
Chris Masond3977122009-01-05 21:25:51 -05004236 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004237 u64 clear_start = 0, clear_len = 0;
4238
Chris Mason39279cc2007-06-12 06:35:45 -04004239 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004240 leaf = path->nodes[0];
4241 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004242 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004243
Li Zefan33345d012011-04-20 10:31:50 +08004244 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004245 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004246
Chris Mason85e21ba2008-01-29 15:11:36 -05004247 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004248 break;
4249
Chris Mason5f39d392007-10-15 16:14:19 -04004250 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004251 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004252 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004253 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004254 extent_type = btrfs_file_extent_type(leaf, fi);
4255 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004256 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004257 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004258
4259 trace_btrfs_truncate_show_fi_regular(
4260 BTRFS_I(inode), leaf, fi,
4261 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004262 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004263 item_end += btrfs_file_extent_ram_bytes(leaf,
4264 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004265
4266 trace_btrfs_truncate_show_fi_inline(
4267 BTRFS_I(inode), leaf, fi, path->slots[0],
4268 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004269 }
Yan008630c2007-11-07 13:31:09 -05004270 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004271 }
Yan, Zheng80825102009-11-12 09:35:36 +00004272 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004273 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004274 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004275 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004276 break;
4277 if (found_key.offset >= new_size)
4278 del_item = 1;
4279 else
4280 del_item = 0;
4281 }
Chris Mason39279cc2007-06-12 06:35:45 -04004282 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004283 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004284 if (found_type != BTRFS_EXTENT_DATA_KEY)
4285 goto delete;
4286
4287 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004288 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004289
4290 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004291 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004292 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004293 u64 orig_num_bytes =
4294 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004295 extent_num_bytes = ALIGN(new_size -
4296 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004297 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004298 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004299 btrfs_set_file_extent_num_bytes(leaf, fi,
4300 extent_num_bytes);
4301 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004302 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004303 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004304 &root->state) &&
4305 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004306 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004307 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004308 } else {
Chris Masondb945352007-10-15 16:15:53 -04004309 extent_num_bytes =
4310 btrfs_file_extent_disk_num_bytes(leaf,
4311 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004312 extent_offset = found_key.offset -
4313 btrfs_file_extent_offset(leaf, fi);
4314
Chris Mason39279cc2007-06-12 06:35:45 -04004315 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004316 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004317 if (extent_start != 0) {
4318 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004319 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004320 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004321 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004322 }
4323 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004324 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004325 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004326 /*
4327 * we can't truncate inline items that have had
4328 * special encodings
4329 */
4330 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004331 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004332 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4333 btrfs_file_extent_compression(leaf, fi) == 0) {
4334 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004335
Josef Bacikddfae632017-10-19 14:16:02 -04004336 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4337 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004338 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004339 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004340 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004341 * We have to bail so the last_size is set to
4342 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004343 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004344 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004345 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004346 } else {
4347 /*
4348 * Inline extents are special, we just treat
4349 * them as a full sector worth in the file
4350 * extent tree just for simplicity sake.
4351 */
4352 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004353 }
Josef Bacikddfae632017-10-19 14:16:02 -04004354
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004355 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004356 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004357 }
Chris Mason179e29e2007-11-01 11:28:41 -04004358delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004359 /*
4360 * We use btrfs_truncate_inode_items() to clean up log trees for
4361 * multiple fsyncs, and in this case we don't want to clear the
4362 * file extent range because it's just the log.
4363 */
4364 if (root == BTRFS_I(inode)->root) {
4365 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4366 clear_start, clear_len);
4367 if (ret) {
4368 btrfs_abort_transaction(trans, ret);
4369 break;
4370 }
4371 }
4372
Josef Bacikddfae632017-10-19 14:16:02 -04004373 if (del_item)
4374 last_size = found_key.offset;
4375 else
4376 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004377 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004378 if (!pending_del_nr) {
4379 /* no pending yet, add ourselves */
4380 pending_del_slot = path->slots[0];
4381 pending_del_nr = 1;
4382 } else if (pending_del_nr &&
4383 path->slots[0] + 1 == pending_del_slot) {
4384 /* hop on the pending chunk */
4385 pending_del_nr++;
4386 pending_del_slot = path->slots[0];
4387 } else {
Chris Masond3977122009-01-05 21:25:51 -05004388 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004389 }
Chris Mason39279cc2007-06-12 06:35:45 -04004390 } else {
4391 break;
4392 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004393 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004394
Miao Xie27cdeb72014-04-02 19:51:05 +08004395 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004396 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004397 struct btrfs_ref ref = { 0 };
4398
Chris Mason28ed1342014-12-17 09:41:04 -08004399 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004400
4401 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4402 extent_start, extent_num_bytes, 0);
4403 ref.real_root = root->root_key.objectid;
4404 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4405 ino, extent_offset);
4406 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004407 if (ret) {
4408 btrfs_abort_transaction(trans, ret);
4409 break;
4410 }
Chris Mason28f75a02015-02-04 06:59:29 -08004411 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004412 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004413 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004414 }
Chris Mason39279cc2007-06-12 06:35:45 -04004415 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004416
Yan, Zheng80825102009-11-12 09:35:36 +00004417 if (found_type == BTRFS_INODE_ITEM_KEY)
4418 break;
4419
4420 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004421 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004422 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004423 if (pending_del_nr) {
4424 ret = btrfs_del_items(trans, root, path,
4425 pending_del_slot,
4426 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004427 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004428 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004429 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004430 }
Yan, Zheng80825102009-11-12 09:35:36 +00004431 pending_del_nr = 0;
4432 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004433 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004434
Chris Mason28f75a02015-02-04 06:59:29 -08004435 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004436 * We can generate a lot of delayed refs, so we need to
4437 * throttle every once and a while and make sure we're
4438 * adding enough space to keep up with the work we are
4439 * generating. Since we hold a transaction here we
4440 * can't flush, and we don't want to FLUSH_LIMIT because
4441 * we could have generated too many delayed refs to
4442 * actually allocate, so just bail if we're short and
4443 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004444 */
Josef Bacik28bad212018-12-03 10:20:38 -05004445 if (should_throttle) {
4446 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4447 BTRFS_RESERVE_NO_FLUSH);
4448 if (ret) {
4449 ret = -EAGAIN;
4450 break;
4451 }
Chris Mason28f75a02015-02-04 06:59:29 -08004452 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004453 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004454 } else {
4455 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004456 }
Chris Mason39279cc2007-06-12 06:35:45 -04004457 }
Yan, Zheng80825102009-11-12 09:35:36 +00004458out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004459 if (ret >= 0 && pending_del_nr) {
4460 int err;
4461
4462 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004463 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004464 if (err) {
4465 btrfs_abort_transaction(trans, err);
4466 ret = err;
4467 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004468 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004469 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4470 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004471 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004472 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004473 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004474 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4475 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004476 }
Chris Mason28ed1342014-12-17 09:41:04 -08004477
Chris Mason39279cc2007-06-12 06:35:45 -04004478 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004479 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004480}
4481
Chris Masona52d9a82007-08-27 16:49:44 -04004482/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304483 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004484 * @inode - inode that we're zeroing
4485 * @from - the offset to start zeroing
4486 * @len - the length to zero, 0 to zero the entire range respective to the
4487 * offset
4488 * @front - zero up to the offset instead of from the offset on
4489 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304490 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004491 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004492 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304493int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004494 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004495{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004496 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004497 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004498 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4499 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004500 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004501 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004502 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004503 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004504 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004505 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304506 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004507 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004508 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004509 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004510 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304511 u64 block_start;
4512 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004513
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004514 if (IS_ALIGNED(offset, blocksize) &&
4515 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004516 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304517
Josef Bacik8b62f872017-10-19 14:15:55 -04004518 block_start = round_down(from, blocksize);
4519 block_end = block_start + blocksize - 1;
4520
Chris Masona52d9a82007-08-27 16:49:44 -04004521
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004522 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4523 blocksize);
4524 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004525 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4526 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004527 /* For nocow case, no need to reserve data space */
4528 only_release_metadata = true;
4529 } else {
4530 goto out;
4531 }
4532 }
4533 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4534 if (ret < 0) {
4535 if (!only_release_metadata)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03004536 btrfs_free_reserved_data_space(BTRFS_I(inode),
4537 data_reserved, block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004538 goto out;
4539 }
Chris Mason211c17f2008-05-15 09:13:45 -04004540again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004541 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004542 if (!page) {
Nikolay Borisov86d52922020-06-03 08:55:40 +03004543 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004544 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004545 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004546 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004547 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004548 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004549
Chris Masona52d9a82007-08-27 16:49:44 -04004550 if (!PageUptodate(page)) {
4551 ret = btrfs_readpage(NULL, page);
4552 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004553 if (page->mapping != mapping) {
4554 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004555 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004556 goto again;
4557 }
Chris Masona52d9a82007-08-27 16:49:44 -04004558 if (!PageUptodate(page)) {
4559 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004560 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004561 }
4562 }
Chris Mason211c17f2008-05-15 09:13:45 -04004563 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004564
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304565 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004566 set_page_extent_mapped(page);
4567
Nikolay Borisovc3504372020-06-03 08:55:03 +03004568 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004569 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304570 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004571 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004572 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004573 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004574 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004575 btrfs_put_ordered_extent(ordered);
4576 goto again;
4577 }
4578
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304579 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004580 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4581 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004582
Nikolay Borisovc2566f22020-06-03 08:55:35 +03004583 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004584 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004585 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304586 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004587 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004588 goto out_unlock;
4589 }
4590
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304591 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004592 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304593 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004594 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004595 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304596 memset(kaddr + (block_start - page_offset(page)),
4597 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004598 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304599 memset(kaddr + (block_start - page_offset(page)) + offset,
4600 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004601 flush_dcache_page(page);
4602 kunmap(page);
4603 }
Chris Mason247e7432008-07-17 12:53:51 -04004604 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004605 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004606 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004607
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004608 if (only_release_metadata)
4609 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4610 block_end, EXTENT_NORESERVE, NULL, NULL,
4611 GFP_NOFS);
4612
Chris Mason89642222008-07-24 09:41:53 -04004613out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004614 if (ret) {
4615 if (only_release_metadata)
4616 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4617 blocksize, true);
4618 else
Nikolay Borisov86d52922020-06-03 08:55:40 +03004619 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004620 block_start, blocksize, true);
4621 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004622 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004623 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004624 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004625out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004626 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004627 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004628 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004629 return ret;
4630}
4631
Josef Bacik16e75492013-10-22 12:18:51 -04004632static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4633 u64 offset, u64 len)
4634{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004635 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004636 struct btrfs_trans_handle *trans;
4637 int ret;
4638
4639 /*
4640 * Still need to make sure the inode looks like it's been updated so
4641 * that any holes get logged if we fsync.
4642 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004643 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4644 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004645 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4646 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4647 return 0;
4648 }
4649
4650 /*
4651 * 1 - for the one we're dropping
4652 * 1 - for the one we're adding
4653 * 1 - for updating the inode.
4654 */
4655 trans = btrfs_start_transaction(root, 3);
4656 if (IS_ERR(trans))
4657 return PTR_ERR(trans);
4658
4659 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4660 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004661 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004662 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004663 return ret;
4664 }
4665
David Sterbaf85b7372017-01-20 14:54:07 +01004666 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4667 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004668 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004669 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004670 else
4671 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004672 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004673 return ret;
4674}
4675
Josef Bacik695a0d02011-03-04 15:46:53 -05004676/*
4677 * This function puts in dummy file extents for the area we're creating a hole
4678 * for. So if we are truncating this file to a larger size we need to insert
4679 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4680 * the range between oldsize and size
4681 */
Josef Bacika41ad392011-01-31 15:30:16 -05004682int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004683{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004684 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004685 struct btrfs_root *root = BTRFS_I(inode)->root;
4686 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004687 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004688 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004689 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004690 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4691 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004692 u64 last_byte;
4693 u64 cur_offset;
4694 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004695 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004696
Josef Bacika71754f2013-06-17 17:14:39 -04004697 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304698 * If our size started in the middle of a block we need to zero out the
4699 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004700 * expose stale data.
4701 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304702 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004703 if (err)
4704 return err;
4705
Yan Zheng9036c102008-10-30 14:19:41 -04004706 if (size <= hole_start)
4707 return 0;
4708
David Sterbab272ae22020-02-05 19:09:33 +01004709 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004710 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004711 cur_offset = hole_start;
4712 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004713 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004714 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004715 if (IS_ERR(em)) {
4716 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004717 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004718 break;
4719 }
Yan Zheng9036c102008-10-30 14:19:41 -04004720 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004721 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004722 hole_size = last_byte - cur_offset;
4723
Yan, Zheng80825102009-11-12 09:35:36 +00004724 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004725 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004726
Josef Bacik16e75492013-10-22 12:18:51 -04004727 err = maybe_insert_hole(root, inode, cur_offset,
4728 hole_size);
4729 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004730 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004731
4732 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4733 cur_offset, hole_size);
4734 if (err)
4735 break;
4736
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004737 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004738 cur_offset + hole_size - 1, 0);
4739 hole_em = alloc_extent_map();
4740 if (!hole_em) {
4741 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4742 &BTRFS_I(inode)->runtime_flags);
4743 goto next;
4744 }
4745 hole_em->start = cur_offset;
4746 hole_em->len = hole_size;
4747 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004748
Josef Bacik5dc562c2012-08-17 13:14:17 -04004749 hole_em->block_start = EXTENT_MAP_HOLE;
4750 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004751 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004752 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004753 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004754 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004755
4756 while (1) {
4757 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004758 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004759 write_unlock(&em_tree->lock);
4760 if (err != -EEXIST)
4761 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004762 btrfs_drop_extent_cache(BTRFS_I(inode),
4763 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004764 cur_offset +
4765 hole_size - 1, 0);
4766 }
4767 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004768 } else {
4769 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4770 cur_offset, hole_size);
4771 if (err)
4772 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004773 }
Josef Bacik16e75492013-10-22 12:18:51 -04004774next:
Yan Zheng9036c102008-10-30 14:19:41 -04004775 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004776 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004777 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004778 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004779 break;
4780 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004781 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004782 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004783 return err;
4784}
4785
Eric Sandeen3972f262013-01-12 02:57:22 +00004786static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004787{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004788 struct btrfs_root *root = BTRFS_I(inode)->root;
4789 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004790 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004791 loff_t newsize = attr->ia_size;
4792 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004793 int ret;
4794
Eric Sandeen3972f262013-01-12 02:57:22 +00004795 /*
4796 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4797 * special case where we need to update the times despite not having
4798 * these flags set. For all other operations the VFS set these flags
4799 * explicitly if it wants a timestamp update.
4800 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004801 if (newsize != oldsize) {
4802 inode_inc_iversion(inode);
4803 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4804 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004805 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004806 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004807
Josef Bacika41ad392011-01-31 15:30:16 -05004808 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004809 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004810 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004811 * This is to ensure the snapshot captures a fully consistent
4812 * state of this file - if the snapshot captures this expanding
4813 * truncation, it must capture all writes that happened before
4814 * this truncation.
4815 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004816 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004817 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004818 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004819 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004820 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004821 }
Yan, Zheng80825102009-11-12 09:35:36 +00004822
Miao Xief4a2f4c2011-12-14 20:12:01 -05004823 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004824 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004825 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004826 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004827 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004828
4829 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004830 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304831 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004832 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004833 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004834 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004835 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004836
Josef Bacika41ad392011-01-31 15:30:16 -05004837 /*
4838 * We're truncating a file that used to have good data down to
4839 * zero. Make sure it gets into the ordered flush list so that
4840 * any new writes get down to disk quickly.
4841 */
4842 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004843 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4844 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004845
Josef Bacika41ad392011-01-31 15:30:16 -05004846 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004847
David Sterba8e0fa5d2020-06-09 19:21:48 +02004848 /* Disable nonlocked read DIO to avoid the endless truncate */
4849 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004850 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004851 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004852
Filipe Manana213e8c52018-02-06 20:40:31 +00004853 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004854 if (ret && inode->i_nlink) {
4855 int err;
4856
4857 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004858 * Truncate failed, so fix up the in-memory size. We
4859 * adjusted disk_i_size down as we removed extents, so
4860 * wait for disk_i_size to be stable and then update the
4861 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004862 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004863 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004864 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004865 return err;
4866 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004867 }
Yan, Zheng80825102009-11-12 09:35:36 +00004868 }
4869
Josef Bacika41ad392011-01-31 15:30:16 -05004870 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004871}
4872
Chris Mason39279cc2007-06-12 06:35:45 -04004873static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4874{
David Howells2b0143b2015-03-17 22:25:59 +00004875 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004876 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004877 int err;
4878
Li Zefanb83cc962010-12-20 16:04:08 +08004879 if (btrfs_root_readonly(root))
4880 return -EROFS;
4881
Jan Kara31051c82016-05-26 16:55:18 +02004882 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004883 if (err)
4884 return err;
4885
Chris Mason5a3f23d2009-03-31 13:27:11 -04004886 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004887 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004888 if (err)
4889 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004890 }
Yan Zheng9036c102008-10-30 14:19:41 -04004891
Christoph Hellwig10257742010-06-04 11:30:02 +02004892 if (attr->ia_valid) {
4893 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004894 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004895 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004896
Josef Bacik22c44fe2011-11-30 10:45:38 -05004897 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004898 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004899 }
4900
Chris Mason39279cc2007-06-12 06:35:45 -04004901 return err;
4902}
Chris Mason61295eb2008-01-14 16:24:38 -05004903
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004904/*
4905 * While truncating the inode pages during eviction, we get the VFS calling
4906 * btrfs_invalidatepage() against each page of the inode. This is slow because
4907 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4908 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4909 * extent_state structures over and over, wasting lots of time.
4910 *
4911 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4912 * those expensive operations on a per page basis and do only the ordered io
4913 * finishing, while we release here the extent_map and extent_state structures,
4914 * without the excessive merging and splitting.
4915 */
4916static void evict_inode_truncate_pages(struct inode *inode)
4917{
4918 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4919 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4920 struct rb_node *node;
4921
4922 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004923 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004924
4925 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004926 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004927 struct extent_map *em;
4928
Liu Bo07e1ce02018-08-23 03:51:52 +08004929 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004930 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004931 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4932 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004933 remove_extent_mapping(map_tree, em);
4934 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004935 if (need_resched()) {
4936 write_unlock(&map_tree->lock);
4937 cond_resched();
4938 write_lock(&map_tree->lock);
4939 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004940 }
4941 write_unlock(&map_tree->lock);
4942
Filipe Manana6ca07092015-05-26 00:55:42 +01004943 /*
4944 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004945 * We can have ongoing bios started by readahead that have
4946 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004947 * still in progress (unlocked the pages in the bio but did not yet
4948 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004949 * ranges can still be locked and eviction started because before
4950 * submitting those bios, which are executed by a separate task (work
4951 * queue kthread), inode references (inode->i_count) were not taken
4952 * (which would be dropped in the end io callback of each bio).
4953 * Therefore here we effectively end up waiting for those bios and
4954 * anyone else holding locked ranges without having bumped the inode's
4955 * reference count - if we don't do it, when they access the inode's
4956 * io_tree to unlock a range it may be too late, leading to an
4957 * use-after-free issue.
4958 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004959 spin_lock(&io_tree->lock);
4960 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4961 struct extent_state *state;
4962 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004963 u64 start;
4964 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004965 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004966
4967 node = rb_first(&io_tree->state);
4968 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004969 start = state->start;
4970 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004971 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004972 spin_unlock(&io_tree->lock);
4973
David Sterbaff13db42015-12-03 14:30:40 +01004974 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004975
4976 /*
4977 * If still has DELALLOC flag, the extent didn't reach disk,
4978 * and its reserved space won't be freed by delayed_ref.
4979 * So we need to free its reserved space here.
4980 * (Refer to comment in btrfs_invalidatepage, case 2)
4981 *
4982 * Note, end is the bytenr of last byte, so we need + 1 here.
4983 */
Filipe Manana421f0922018-10-12 13:02:48 +01004984 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03004985 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
4986 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004987
Filipe Manana6ca07092015-05-26 00:55:42 +01004988 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004989 EXTENT_LOCKED | EXTENT_DELALLOC |
4990 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4991 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004992
Filipe Manana7064dd52014-08-08 02:47:05 +01004993 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004994 spin_lock(&io_tree->lock);
4995 }
4996 spin_unlock(&io_tree->lock);
4997}
4998
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07004999static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005000 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005001{
5002 struct btrfs_fs_info *fs_info = root->fs_info;
5003 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005004 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005005 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005006 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005007
Josef Bacikd3984c92019-08-01 18:19:37 -04005008 /*
5009 * Eviction should be taking place at some place safe because of our
5010 * delayed iputs. However the normal flushing code will run delayed
5011 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5012 *
5013 * We reserve the delayed_refs_extra here again because we can't use
5014 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5015 * above. We reserve our extra bit here because we generate a ton of
5016 * delayed refs activity by truncating.
5017 *
5018 * If we cannot make our reservation we'll attempt to steal from the
5019 * global reserve, because we really want to be able to free up space.
5020 */
5021 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5022 BTRFS_RESERVE_FLUSH_EVICT);
5023 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005024 /*
5025 * Try to steal from the global reserve if there is space for
5026 * it.
5027 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005028 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5029 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5030 btrfs_warn(fs_info,
5031 "could not allocate space for delete; will truncate on mount");
5032 return ERR_PTR(-ENOSPC);
5033 }
5034 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005035 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005036
5037 trans = btrfs_join_transaction(root);
5038 if (IS_ERR(trans))
5039 return trans;
5040
5041 if (delayed_refs_extra) {
5042 trans->block_rsv = &fs_info->trans_block_rsv;
5043 trans->bytes_reserved = delayed_refs_extra;
5044 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5045 delayed_refs_extra, 1);
5046 }
5047 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005048}
5049
Al Virobd555972010-06-07 11:35:40 -04005050void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005051{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005052 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005053 struct btrfs_trans_handle *trans;
5054 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005055 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005056 int ret;
5057
liubo1abe9b82011-03-24 11:18:59 +00005058 trace_btrfs_inode_evict(inode);
5059
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005060 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005061 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005062 return;
5063 }
5064
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005065 evict_inode_truncate_pages(inode);
5066
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005067 if (inode->i_nlink &&
5068 ((btrfs_root_refs(&root->root_item) != 0 &&
5069 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005070 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005071 goto no_delete;
5072
Omar Sandoval27919062018-05-11 13:13:37 -07005073 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005074 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005075
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005076 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005077
Omar Sandoval7b40b692018-05-11 13:13:33 -07005078 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005079 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005080
Yan, Zheng76dda932009-09-21 16:00:26 -04005081 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005082 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5083 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005084 goto no_delete;
5085 }
5086
Nikolay Borisovaa790212017-01-10 20:35:40 +02005087 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005088 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005089 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005090
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005091 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005092 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005093 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005094 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005095 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005096
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005097 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005098
Yan, Zheng80825102009-11-12 09:35:36 +00005099 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005100 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005101 if (IS_ERR(trans))
5102 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005103
5104 trans->block_rsv = rsv;
5105
Yan, Zhengd68fc572010-05-16 10:49:58 -04005106 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005107 trans->block_rsv = &fs_info->trans_block_rsv;
5108 btrfs_end_transaction(trans);
5109 btrfs_btree_balance_dirty(fs_info);
5110 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5111 goto free_rsv;
5112 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005113 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005114 }
5115
Josef Bacik4ef31a42013-08-13 14:10:08 -04005116 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005117 * Errors here aren't a big deal, it just means we leave orphan items in
5118 * the tree. They will be cleaned up on the next mount. If the inode
5119 * number gets reused, cleanup deletes the orphan item without doing
5120 * anything, and unlink reuses the existing orphan item.
5121 *
5122 * If it turns out that we are dropping too many of these, we might want
5123 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005124 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005125 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005126 if (!IS_ERR(trans)) {
5127 trans->block_rsv = rsv;
5128 btrfs_orphan_del(trans, BTRFS_I(inode));
5129 trans->block_rsv = &fs_info->trans_block_rsv;
5130 btrfs_end_transaction(trans);
5131 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005132
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005133 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005134 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005135 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005136
Omar Sandoval27919062018-05-11 13:13:37 -07005137free_rsv:
5138 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005139no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005140 /*
5141 * If we didn't successfully delete, the orphan item will still be in
5142 * the tree and we'll retry on the next mount. Again, we might also want
5143 * to retry these periodically in the future.
5144 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005145 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005146 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005147}
5148
5149/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005150 * Return the key found in the dir entry in the location pointer, fill @type
5151 * with BTRFS_FT_*, and return 0.
5152 *
Su Yue005d6712018-03-05 17:13:37 +08005153 * If no dir entries were found, returns -ENOENT.
5154 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005155 */
5156static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005157 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005158{
5159 const char *name = dentry->d_name.name;
5160 int namelen = dentry->d_name.len;
5161 struct btrfs_dir_item *di;
5162 struct btrfs_path *path;
5163 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005164 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005165
5166 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005167 if (!path)
5168 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005169
David Sterbaf85b7372017-01-20 14:54:07 +01005170 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5171 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005172 if (IS_ERR_OR_NULL(di)) {
5173 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005174 goto out;
5175 }
Chris Masond3977122009-01-05 21:25:51 -05005176
Chris Mason5f39d392007-10-15 16:14:19 -04005177 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005178 if (location->type != BTRFS_INODE_ITEM_KEY &&
5179 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005180 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005181 btrfs_warn(root->fs_info,
5182"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5183 __func__, name, btrfs_ino(BTRFS_I(dir)),
5184 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005185 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005186 if (!ret)
5187 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005188out:
Chris Mason39279cc2007-06-12 06:35:45 -04005189 btrfs_free_path(path);
5190 return ret;
5191}
5192
5193/*
5194 * when we hit a tree root in a directory, the btrfs part of the inode
5195 * needs to be changed to reflect the root directory of the tree root. This
5196 * is kind of like crossing a mount point.
5197 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005198static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005199 struct inode *dir,
5200 struct dentry *dentry,
5201 struct btrfs_key *location,
5202 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005203{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005204 struct btrfs_path *path;
5205 struct btrfs_root *new_root;
5206 struct btrfs_root_ref *ref;
5207 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005208 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005209 int ret;
5210 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005211
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005212 path = btrfs_alloc_path();
5213 if (!path) {
5214 err = -ENOMEM;
5215 goto out;
5216 }
Chris Mason39279cc2007-06-12 06:35:45 -04005217
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005218 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005219 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5220 key.type = BTRFS_ROOT_REF_KEY;
5221 key.offset = location->objectid;
5222
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005223 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005224 if (ret) {
5225 if (ret < 0)
5226 err = ret;
5227 goto out;
5228 }
Chris Mason39279cc2007-06-12 06:35:45 -04005229
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005230 leaf = path->nodes[0];
5231 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005232 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005233 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5234 goto out;
5235
5236 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5237 (unsigned long)(ref + 1),
5238 dentry->d_name.len);
5239 if (ret)
5240 goto out;
5241
David Sterbab3b4aa72011-04-21 01:20:15 +02005242 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005243
David Sterba56e93572020-05-15 19:35:55 +02005244 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005245 if (IS_ERR(new_root)) {
5246 err = PTR_ERR(new_root);
5247 goto out;
5248 }
5249
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005250 *sub_root = new_root;
5251 location->objectid = btrfs_root_dirid(&new_root->root_item);
5252 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005253 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005254 err = 0;
5255out:
5256 btrfs_free_path(path);
5257 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005258}
5259
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005260static void inode_tree_add(struct inode *inode)
5261{
5262 struct btrfs_root *root = BTRFS_I(inode)->root;
5263 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005264 struct rb_node **p;
5265 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005266 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005267 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005268
Al Viro1d3382cb2010-10-23 15:19:20 -04005269 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005270 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005271 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005272 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005273 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005274 while (*p) {
5275 parent = *p;
5276 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5277
David Sterba37508512018-06-29 10:56:40 +02005278 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005279 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005280 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005281 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005282 else {
5283 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005284 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005285 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005286 RB_CLEAR_NODE(parent);
5287 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005288 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005289 }
5290 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005291 rb_link_node(new, parent, p);
5292 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005293 spin_unlock(&root->inode_lock);
5294}
5295
5296static void inode_tree_del(struct inode *inode)
5297{
5298 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005299 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005300
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005301 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005302 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005303 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005304 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005305 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005306 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005307 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005308
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005309 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005310 spin_lock(&root->inode_lock);
5311 empty = RB_EMPTY_ROOT(&root->inode_tree);
5312 spin_unlock(&root->inode_lock);
5313 if (empty)
5314 btrfs_add_dead_root(root);
5315 }
5316}
5317
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005318
Chris Masone02119d2008-09-05 16:13:11 -04005319static int btrfs_init_locked_inode(struct inode *inode, void *p)
5320{
5321 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005322
5323 inode->i_ino = args->ino;
5324 BTRFS_I(inode)->location.objectid = args->ino;
5325 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5326 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005327 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5328 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005329 return 0;
5330}
5331
5332static int btrfs_find_actor(struct inode *inode, void *opaque)
5333{
5334 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005335
5336 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005337 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005338}
5339
David Sterba0202e832020-05-15 19:35:59 +02005340static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005341 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005342{
5343 struct inode *inode;
5344 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005345 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005346
David Sterba0202e832020-05-15 19:35:59 +02005347 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005348 args.root = root;
5349
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005350 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005351 btrfs_init_locked_inode,
5352 (void *)&args);
5353 return inode;
5354}
5355
David Sterba4c66e0d2019-10-03 19:09:35 +02005356/*
David Sterba0202e832020-05-15 19:35:59 +02005357 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005358 * Path can be preallocated to prevent recursing back to iget through
5359 * allocator. NULL is also valid but may require an additional allocation
5360 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305361 */
David Sterba0202e832020-05-15 19:35:59 +02005362struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005363 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305364{
5365 struct inode *inode;
5366
David Sterba0202e832020-05-15 19:35:59 +02005367 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305368 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005369 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305370
5371 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005372 int ret;
5373
Filipe Manana4222ea72018-10-24 10:13:03 +01005374 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005375 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005376 inode_tree_add(inode);
5377 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005378 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005379 iget_failed(inode);
5380 /*
5381 * ret > 0 can come from btrfs_search_slot called by
5382 * btrfs_read_locked_inode, this means the inode item
5383 * was not found.
5384 */
5385 if (ret > 0)
5386 ret = -ENOENT;
5387 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005388 }
5389 }
5390
Balaji Rao1a54ef82008-07-21 02:01:04 +05305391 return inode;
5392}
5393
David Sterba0202e832020-05-15 19:35:59 +02005394struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005395{
David Sterba0202e832020-05-15 19:35:59 +02005396 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005397}
5398
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005399static struct inode *new_simple_dir(struct super_block *s,
5400 struct btrfs_key *key,
5401 struct btrfs_root *root)
5402{
5403 struct inode *inode = new_inode(s);
5404
5405 if (!inode)
5406 return ERR_PTR(-ENOMEM);
5407
Josef Bacik5c8fd992020-02-14 16:11:43 -05005408 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005409 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005410 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005411
5412 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005413 /*
5414 * We only need lookup, the rest is read-only and there's no inode
5415 * associated with the dentry
5416 */
5417 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005418 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005419 inode->i_fop = &simple_dir_operations;
5420 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005421 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305422 inode->i_atime = inode->i_mtime;
5423 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005424 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005425
5426 return inode;
5427}
5428
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005429static inline u8 btrfs_inode_type(struct inode *inode)
5430{
5431 /*
5432 * Compile-time asserts that generic FT_* types still match
5433 * BTRFS_FT_* types
5434 */
5435 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5436 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5437 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5438 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5439 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5440 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5441 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5442 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5443
5444 return fs_umode_to_ftype(inode->i_mode);
5445}
5446
Chris Mason3de45862008-11-17 21:02:50 -05005447struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005448{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005449 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005450 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005451 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005452 struct btrfs_root *sub_root = root;
5453 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005454 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005455 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005456
5457 if (dentry->d_name.len > BTRFS_NAME_LEN)
5458 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005459
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005460 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005461 if (ret < 0)
5462 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005463
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005464 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005465 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005466 if (IS_ERR(inode))
5467 return inode;
5468
5469 /* Do extra check against inode mode with di_type */
5470 if (btrfs_inode_type(inode) != di_type) {
5471 btrfs_crit(fs_info,
5472"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5473 inode->i_mode, btrfs_inode_type(inode),
5474 di_type);
5475 iput(inode);
5476 return ERR_PTR(-EUCLEAN);
5477 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005478 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005479 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005480
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005481 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005482 &location, &sub_root);
5483 if (ret < 0) {
5484 if (ret != -ENOENT)
5485 inode = ERR_PTR(ret);
5486 else
5487 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5488 } else {
David Sterba0202e832020-05-15 19:35:59 +02005489 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005490 }
Josef Bacik87270022020-01-24 09:32:31 -05005491 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005492 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005493
Julia Lawall34d19ba2011-01-24 19:55:19 +00005494 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005495 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005496 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005497 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005498 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005499 if (ret) {
5500 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005501 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005502 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005503 }
5504
Chris Mason3de45862008-11-17 21:02:50 -05005505 return inode;
5506}
5507
Nick Pigginfe15ce42011-01-07 17:49:23 +11005508static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005509{
5510 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005511 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005512
Li Zefan848cce02012-02-21 17:04:28 +08005513 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005514 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005515
Li Zefan848cce02012-02-21 17:04:28 +08005516 if (inode) {
5517 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005518 if (btrfs_root_refs(&root->root_item) == 0)
5519 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005520
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005521 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005522 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005523 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005524 return 0;
5525}
5526
Chris Mason3de45862008-11-17 21:02:50 -05005527static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005528 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005529{
Al Viro3837d202018-10-10 16:38:27 -04005530 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005531
Al Viro3837d202018-10-10 16:38:27 -04005532 if (inode == ERR_PTR(-ENOENT))
5533 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005534 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005535}
5536
Josef Bacik23b5ec72017-07-24 15:14:25 -04005537/*
5538 * All this infrastructure exists because dir_emit can fault, and we are holding
5539 * the tree lock when doing readdir. For now just allocate a buffer and copy
5540 * our information into that, and then dir_emit from the buffer. This is
5541 * similar to what NFS does, only we don't keep the buffer around in pagecache
5542 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5543 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5544 * tree lock.
5545 */
5546static int btrfs_opendir(struct inode *inode, struct file *file)
5547{
5548 struct btrfs_file_private *private;
5549
5550 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5551 if (!private)
5552 return -ENOMEM;
5553 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5554 if (!private->filldir_buf) {
5555 kfree(private);
5556 return -ENOMEM;
5557 }
5558 file->private_data = private;
5559 return 0;
5560}
5561
5562struct dir_entry {
5563 u64 ino;
5564 u64 offset;
5565 unsigned type;
5566 int name_len;
5567};
5568
5569static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5570{
5571 while (entries--) {
5572 struct dir_entry *entry = addr;
5573 char *name = (char *)(entry + 1);
5574
David Sterba92d32172018-04-16 21:10:14 +02005575 ctx->pos = get_unaligned(&entry->offset);
5576 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5577 get_unaligned(&entry->ino),
5578 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005579 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005580 addr += sizeof(struct dir_entry) +
5581 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005582 ctx->pos++;
5583 }
5584 return 0;
5585}
5586
Al Viro9cdda8d2013-05-22 16:48:09 -04005587static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005588{
Al Viro9cdda8d2013-05-22 16:48:09 -04005589 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005590 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005591 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005592 struct btrfs_dir_item *di;
5593 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005594 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005595 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005596 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005597 struct list_head ins_list;
5598 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005599 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005600 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005601 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005602 char *name_ptr;
5603 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005604 int entries = 0;
5605 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005606 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005607 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005608
Al Viro9cdda8d2013-05-22 16:48:09 -04005609 if (!dir_emit_dots(file, ctx))
5610 return 0;
5611
David Woodhouse49593bf2008-08-17 17:08:36 +01005612 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005613 if (!path)
5614 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005615
Josef Bacik23b5ec72017-07-24 15:14:25 -04005616 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005617 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005618
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005619 INIT_LIST_HEAD(&ins_list);
5620 INIT_LIST_HEAD(&del_list);
5621 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005622
Josef Bacik23b5ec72017-07-24 15:14:25 -04005623again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005624 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005625 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005626 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005627
Chris Mason39279cc2007-06-12 06:35:45 -04005628 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5629 if (ret < 0)
5630 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005631
5632 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005633 struct dir_entry *entry;
5634
Chris Mason5f39d392007-10-15 16:14:19 -04005635 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005636 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005637 if (slot >= btrfs_header_nritems(leaf)) {
5638 ret = btrfs_next_leaf(root, path);
5639 if (ret < 0)
5640 goto err;
5641 else if (ret > 0)
5642 break;
5643 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005644 }
Chris Mason3de45862008-11-17 21:02:50 -05005645
Chris Mason5f39d392007-10-15 16:14:19 -04005646 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5647
5648 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005649 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005650 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005651 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005652 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005653 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005654 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005655 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005656 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005657 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005658 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5659 PAGE_SIZE) {
5660 btrfs_release_path(path);
5661 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5662 if (ret)
5663 goto nopos;
5664 addr = private->filldir_buf;
5665 entries = 0;
5666 total_len = 0;
5667 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005668 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005669
5670 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005671 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005672 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005673 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5674 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005675 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005676 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005677 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005678 put_unaligned(location.objectid, &entry->ino);
5679 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005680 entries++;
5681 addr += sizeof(struct dir_entry) + name_len;
5682 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005683next:
5684 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005685 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005686 btrfs_release_path(path);
5687
5688 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5689 if (ret)
5690 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005691
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005692 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005693 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005694 goto nopos;
5695
Zach Browndb62efb2013-07-11 16:19:42 -07005696 /*
5697 * Stop new entries from being returned after we return the last
5698 * entry.
5699 *
5700 * New directory entries are assigned a strictly increasing
5701 * offset. This means that new entries created during readdir
5702 * are *guaranteed* to be seen in the future by that readdir.
5703 * This has broken buggy programs which operate on names as
5704 * they're returned by readdir. Until we re-use freed offsets
5705 * we have this hack to stop new entries from being returned
5706 * under the assumption that they'll never reach this huge
5707 * offset.
5708 *
5709 * This is being careful not to overflow 32bit loff_t unless the
5710 * last entry requires it because doing so has broken 32bit apps
5711 * in the past.
5712 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005713 if (ctx->pos >= INT_MAX)
5714 ctx->pos = LLONG_MAX;
5715 else
5716 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005717nopos:
5718 ret = 0;
5719err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005720 if (put)
5721 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005722 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005723 return ret;
5724}
5725
Chris Mason39279cc2007-06-12 06:35:45 -04005726/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005727 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005728 * inode changes. But, it is most likely to find the inode in cache.
5729 * FIXME, needs more benchmarking...there are no reasons other than performance
5730 * to keep or drop this code.
5731 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005732static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005733{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005734 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005735 struct btrfs_root *root = BTRFS_I(inode)->root;
5736 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005737 int ret;
5738
Josef Bacik72ac3c02012-05-23 14:13:11 -04005739 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005740 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005741
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005742 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005743 if (IS_ERR(trans))
5744 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005745
5746 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005747 if (ret && ret == -ENOSPC) {
5748 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005749 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005750 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005751 if (IS_ERR(trans))
5752 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005753
Chris Mason94b60442010-05-26 11:02:00 -04005754 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005755 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005756 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005757 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005758 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005759
5760 return ret;
5761}
5762
5763/*
5764 * This is a copy of file_update_time. We need this so we can return error on
5765 * ENOSPC for updating the inode in the case of file write and mmap writes.
5766 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005767static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005768 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005769{
Alexander Block2bc5565282012-06-15 09:49:33 +02005770 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005771 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005772
5773 if (btrfs_root_readonly(root))
5774 return -EROFS;
5775
Josef Bacike41f9412012-03-26 09:46:47 -04005776 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005777 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005778 if (flags & S_CTIME)
5779 inode->i_ctime = *now;
5780 if (flags & S_MTIME)
5781 inode->i_mtime = *now;
5782 if (flags & S_ATIME)
5783 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005784 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005785}
5786
Chris Masond352ac62008-09-29 15:18:18 -04005787/*
5788 * find the highest existing sequence number in a directory
5789 * and then set the in-memory index_cnt variable to reflect
5790 * free sequence numbers
5791 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005792static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005793{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005794 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005795 struct btrfs_key key, found_key;
5796 struct btrfs_path *path;
5797 struct extent_buffer *leaf;
5798 int ret;
5799
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005800 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005801 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005802 key.offset = (u64)-1;
5803
5804 path = btrfs_alloc_path();
5805 if (!path)
5806 return -ENOMEM;
5807
5808 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5809 if (ret < 0)
5810 goto out;
5811 /* FIXME: we should be able to handle this */
5812 if (ret == 0)
5813 goto out;
5814 ret = 0;
5815
5816 /*
5817 * MAGIC NUMBER EXPLANATION:
5818 * since we search a directory based on f_pos we have to start at 2
5819 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5820 * else has to start at 2
5821 */
5822 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005823 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005824 goto out;
5825 }
5826
5827 path->slots[0]--;
5828
5829 leaf = path->nodes[0];
5830 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5831
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005832 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005833 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005834 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005835 goto out;
5836 }
5837
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005838 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005839out:
5840 btrfs_free_path(path);
5841 return ret;
5842}
5843
Chris Masond352ac62008-09-29 15:18:18 -04005844/*
5845 * helper to find a free sequence number in a given directory. This current
5846 * code is very simple, later versions will do smarter things in the btree
5847 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005848int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005849{
5850 int ret = 0;
5851
Nikolay Borisov877574e2017-02-20 13:50:33 +02005852 if (dir->index_cnt == (u64)-1) {
5853 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005854 if (ret) {
5855 ret = btrfs_set_inode_index_count(dir);
5856 if (ret)
5857 return ret;
5858 }
Josef Bacikaec74772008-07-24 12:12:38 -04005859 }
5860
Nikolay Borisov877574e2017-02-20 13:50:33 +02005861 *index = dir->index_cnt;
5862 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005863
5864 return ret;
5865}
5866
Chris Masonb0d5d102014-09-08 13:08:51 -07005867static int btrfs_insert_inode_locked(struct inode *inode)
5868{
5869 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005870
5871 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005872 args.root = BTRFS_I(inode)->root;
5873
5874 return insert_inode_locked4(inode,
5875 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5876 btrfs_find_actor, &args);
5877}
5878
Anand Jain19aee8d2017-07-18 17:37:05 +08005879/*
5880 * Inherit flags from the parent inode.
5881 *
5882 * Currently only the compression flags and the cow flags are inherited.
5883 */
5884static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5885{
5886 unsigned int flags;
5887
5888 if (!dir)
5889 return;
5890
5891 flags = BTRFS_I(dir)->flags;
5892
5893 if (flags & BTRFS_INODE_NOCOMPRESS) {
5894 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5895 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5896 } else if (flags & BTRFS_INODE_COMPRESS) {
5897 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5898 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5899 }
5900
5901 if (flags & BTRFS_INODE_NODATACOW) {
5902 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5903 if (S_ISREG(inode->i_mode))
5904 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5905 }
5906
David Sterba7b6a2212018-03-26 18:40:21 +02005907 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005908}
5909
Chris Mason39279cc2007-06-12 06:35:45 -04005910static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5911 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005912 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005913 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005914 u64 ref_objectid, u64 objectid,
5915 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005916{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005917 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005918 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005919 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005920 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005921 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005922 struct btrfs_inode_ref *ref;
5923 struct btrfs_key key[2];
5924 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005925 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005926 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005927 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005928 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005929
Chris Mason5f39d392007-10-15 16:14:19 -04005930 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005931 if (!path)
5932 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005933
Josef Bacik11a19a92019-09-09 10:12:04 -04005934 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005935 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005936 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005937 if (!inode) {
5938 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005939 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005940 }
Chris Mason39279cc2007-06-12 06:35:45 -04005941
Li Zefan581bb052011-04-20 10:06:11 +08005942 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005943 * O_TMPFILE, set link count to 0, so that after this point,
5944 * we fill in an inode item with the correct link count.
5945 */
5946 if (!name)
5947 set_nlink(inode, 0);
5948
5949 /*
Li Zefan581bb052011-04-20 10:06:11 +08005950 * we have to initialize this early, so we can reclaim the inode
5951 * number if we fail afterwards in this function.
5952 */
5953 inode->i_ino = objectid;
5954
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005955 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005956 trace_btrfs_inode_request(dir);
5957
Nikolay Borisov877574e2017-02-20 13:50:33 +02005958 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005959 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005960 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005961 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005962 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005963 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005964 } else if (dir) {
5965 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005966 }
5967 /*
5968 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005969 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005970 * number
5971 */
5972 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005973 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005974 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005975 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005976 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005977
Josef Bacik5dc562c2012-08-17 13:14:17 -04005978 /*
5979 * We could have gotten an inode number from somebody who was fsynced
5980 * and then removed in this same transaction, so let's just set full
5981 * sync since it will be a full sync anyway and this will blow away the
5982 * old info in the log.
5983 */
5984 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5985
Chris Mason9c583092008-01-29 15:15:18 -05005986 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005987 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005988 key[0].offset = 0;
5989
Chris Mason9c583092008-01-29 15:15:18 -05005990 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005991
5992 if (name) {
5993 /*
5994 * Start new inodes with an inode_ref. This is slightly more
5995 * efficient for small numbers of hard links since they will
5996 * be packed into one item. Extended refs will kick in if we
5997 * add more hard links than can fit in the ref item.
5998 */
5999 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006000 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006001 key[1].offset = ref_objectid;
6002
6003 sizes[1] = name_len + sizeof(*ref);
6004 }
Chris Mason9c583092008-01-29 15:15:18 -05006005
Chris Masonb0d5d102014-09-08 13:08:51 -07006006 location = &BTRFS_I(inode)->location;
6007 location->objectid = objectid;
6008 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006009 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006010
6011 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006012 if (ret < 0) {
6013 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006014 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006015 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006016
Chris Masonb9473432009-03-13 11:00:37 -04006017 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006018 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006019 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006020 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006021
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006022 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006023 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306024
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006025 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306026 inode->i_atime = inode->i_mtime;
6027 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006028 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306029
Chris Mason5f39d392007-10-15 16:14:19 -04006030 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6031 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006032 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006033 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006034 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006035
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006036 if (name) {
6037 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6038 struct btrfs_inode_ref);
6039 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6040 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6041 ptr = (unsigned long)(ref + 1);
6042 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6043 }
Chris Mason9c583092008-01-29 15:15:18 -05006044
Chris Mason5f39d392007-10-15 16:14:19 -04006045 btrfs_mark_buffer_dirty(path->nodes[0]);
6046 btrfs_free_path(path);
6047
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006048 btrfs_inherit_iflags(inode, dir);
6049
Al Viro569254b2011-07-24 17:08:40 -04006050 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006051 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006052 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006053 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006054 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6055 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006056 }
6057
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006058 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006059
6060 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006061 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006062
Alexander Block8ea05e32012-07-25 17:35:53 +02006063 btrfs_update_root_times(trans, root);
6064
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006065 ret = btrfs_inode_inherit_props(trans, inode, dir);
6066 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006067 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006068 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006069 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006070
Chris Mason39279cc2007-06-12 06:35:45 -04006071 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006072
6073fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006074 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006075fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006076 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006077 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006078 btrfs_free_path(path);
6079 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006080}
6081
Chris Masond352ac62008-09-29 15:18:18 -04006082/*
6083 * utility function to add 'inode' into 'parent_inode' with
6084 * a give name and a given sequence number.
6085 * if 'add_backref' is true, also insert a backref from the
6086 * inode to the parent directory.
6087 */
Chris Masone02119d2008-09-05 16:13:11 -04006088int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006089 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006090 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006091{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006092 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006093 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006094 struct btrfs_root *root = parent_inode->root;
6095 u64 ino = btrfs_ino(inode);
6096 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006097
Li Zefan33345d012011-04-20 10:31:50 +08006098 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006099 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006100 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006101 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006102 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006103 key.offset = 0;
6104 }
Chris Mason39279cc2007-06-12 06:35:45 -04006105
Li Zefan33345d012011-04-20 10:31:50 +08006106 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006107 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006108 root->root_key.objectid, parent_ino,
6109 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006110 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006111 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6112 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006113 }
6114
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006115 /* Nothing to clean up yet */
6116 if (ret)
6117 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006118
Lu Fengqi684572d2018-08-04 21:10:57 +08006119 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006120 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006121 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006122 goto fail_dir_item;
6123 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006124 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006125 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006126 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006127
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006128 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006129 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006130 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006131 /*
6132 * If we are replaying a log tree, we do not want to update the mtime
6133 * and ctime of the parent directory with the current time, since the
6134 * log replay procedure is responsible for setting them to their correct
6135 * values (the ones it had when the fsync was done).
6136 */
6137 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6138 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6139
6140 parent_inode->vfs_inode.i_mtime = now;
6141 parent_inode->vfs_inode.i_ctime = now;
6142 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006143 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006144 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006145 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006146 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006147
6148fail_dir_item:
6149 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6150 u64 local_index;
6151 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006152 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006153 root->root_key.objectid, parent_ino,
6154 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006155 if (err)
6156 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006157 } else if (add_backref) {
6158 u64 local_index;
6159 int err;
6160
6161 err = btrfs_del_inode_ref(trans, root, name, name_len,
6162 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006163 if (err)
6164 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006165 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006166
6167 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006168 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006169}
6170
6171static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006172 struct btrfs_inode *dir, struct dentry *dentry,
6173 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006174{
Josef Bacika1b075d2010-11-19 20:36:11 +00006175 int err = btrfs_add_link(trans, dir, inode,
6176 dentry->d_name.name, dentry->d_name.len,
6177 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006178 if (err > 0)
6179 err = -EEXIST;
6180 return err;
6181}
6182
Josef Bacik618e21d2007-07-11 10:18:17 -04006183static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006184 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006185{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006186 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006187 struct btrfs_trans_handle *trans;
6188 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006189 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006190 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006191 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006192 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006193
Josef Bacik9ed74f22009-09-11 16:12:44 -04006194 /*
6195 * 2 for inode item and ref
6196 * 2 for dir items
6197 * 1 for xattr if selinux is on
6198 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006199 trans = btrfs_start_transaction(root, 5);
6200 if (IS_ERR(trans))
6201 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006202
Li Zefan581bb052011-04-20 10:06:11 +08006203 err = btrfs_find_free_ino(root, &objectid);
6204 if (err)
6205 goto out_unlock;
6206
Josef Bacikaec74772008-07-24 12:12:38 -04006207 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006208 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6209 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006210 if (IS_ERR(inode)) {
6211 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006212 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006213 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006214 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006215
Casey Schauflerad19db72011-12-15 10:09:07 -05006216 /*
6217 * If the active LSM wants to access the inode during
6218 * d_instantiate it needs these. Smack checks to see
6219 * if the filesystem supports xattrs by looking at the
6220 * ops vector.
6221 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006222 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006223 init_special_inode(inode, inode->i_mode, rdev);
6224
6225 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006226 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006227 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006228
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006229 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6230 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006231 if (err)
6232 goto out_unlock;
6233
6234 btrfs_update_inode(trans, root, inode);
6235 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006236
Josef Bacik618e21d2007-07-11 10:18:17 -04006237out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006238 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006239 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006240 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006241 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006242 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006243 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006244 return err;
6245}
6246
Chris Mason39279cc2007-06-12 06:35:45 -04006247static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006248 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006249{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006250 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006251 struct btrfs_trans_handle *trans;
6252 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006253 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006254 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006255 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006256 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006257
Josef Bacik9ed74f22009-09-11 16:12:44 -04006258 /*
6259 * 2 for inode item and ref
6260 * 2 for dir items
6261 * 1 for xattr if selinux is on
6262 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006263 trans = btrfs_start_transaction(root, 5);
6264 if (IS_ERR(trans))
6265 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006266
Li Zefan581bb052011-04-20 10:06:11 +08006267 err = btrfs_find_free_ino(root, &objectid);
6268 if (err)
6269 goto out_unlock;
6270
Josef Bacikaec74772008-07-24 12:12:38 -04006271 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006272 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6273 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006274 if (IS_ERR(inode)) {
6275 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006276 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006277 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006278 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006279 /*
6280 * If the active LSM wants to access the inode during
6281 * d_instantiate it needs these. Smack checks to see
6282 * if the filesystem supports xattrs by looking at the
6283 * ops vector.
6284 */
6285 inode->i_fop = &btrfs_file_operations;
6286 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006287 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006288
6289 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6290 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006291 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006292
6293 err = btrfs_update_inode(trans, root, inode);
6294 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006295 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006296
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006297 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6298 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006299 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006300 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006301
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006302 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006303 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006304
Chris Mason39279cc2007-06-12 06:35:45 -04006305out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006306 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006307 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006308 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006309 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006310 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006311 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006312 return err;
6313}
6314
6315static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6316 struct dentry *dentry)
6317{
Filipe Manana271dba452016-01-05 16:24:05 +00006318 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006319 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006320 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006321 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006322 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006323 int err;
6324 int drop_inode = 0;
6325
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006326 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006327 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006328 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006329
Mark Fashehf1863732012-08-08 11:32:27 -07006330 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006331 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006332
Nikolay Borisov877574e2017-02-20 13:50:33 +02006333 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006334 if (err)
6335 goto fail;
6336
Yan, Zhenga22285a2010-05-16 10:48:46 -04006337 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006338 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006339 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006340 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006341 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006342 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006343 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006344 if (IS_ERR(trans)) {
6345 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006346 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006347 goto fail;
6348 }
Chris Mason5f39d392007-10-15 16:14:19 -04006349
Miao Xie67de1172013-12-26 13:07:06 +08006350 /* There are several dir indexes for this inode, clear the cache. */
6351 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006352 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006353 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006354 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006355 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006356 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006357
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006358 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6359 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006360
Yan, Zhenga5719522009-09-24 09:17:31 -04006361 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006362 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006363 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006364 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006365 int ret;
6366
Yan, Zhenga5719522009-09-24 09:17:31 -04006367 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006368 if (err)
6369 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006370 if (inode->i_nlink == 1) {
6371 /*
6372 * If new hard link count is 1, it's a file created
6373 * with open(2) O_TMPFILE flag.
6374 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006375 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006376 if (err)
6377 goto fail;
6378 }
Al Viro08c422c2011-12-23 07:58:13 -05006379 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006380 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6381 true, NULL);
6382 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6383 err = btrfs_commit_transaction(trans);
6384 trans = NULL;
6385 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006386 }
Chris Mason39279cc2007-06-12 06:35:45 -04006387
Chris Mason1832a6d2007-12-21 16:27:21 -05006388fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006389 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006390 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006391 if (drop_inode) {
6392 inode_dec_link_count(inode);
6393 iput(inode);
6394 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006395 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006396 return err;
6397}
6398
Al Viro18bb1db2011-07-26 01:41:39 -04006399static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006400{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006401 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006402 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006403 struct btrfs_trans_handle *trans;
6404 struct btrfs_root *root = BTRFS_I(dir)->root;
6405 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006406 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006407 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006408
Josef Bacik9ed74f22009-09-11 16:12:44 -04006409 /*
6410 * 2 items for inode and ref
6411 * 2 items for dir items
6412 * 1 for xattr if selinux is on
6413 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006414 trans = btrfs_start_transaction(root, 5);
6415 if (IS_ERR(trans))
6416 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006417
Li Zefan581bb052011-04-20 10:06:11 +08006418 err = btrfs_find_free_ino(root, &objectid);
6419 if (err)
6420 goto out_fail;
6421
Josef Bacikaec74772008-07-24 12:12:38 -04006422 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006423 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6424 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006425 if (IS_ERR(inode)) {
6426 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006427 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006428 goto out_fail;
6429 }
Chris Mason5f39d392007-10-15 16:14:19 -04006430
Chris Masonb0d5d102014-09-08 13:08:51 -07006431 /* these must be set before we unlock the inode */
6432 inode->i_op = &btrfs_dir_inode_operations;
6433 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006434
Eric Paris2a7dba32011-02-01 11:05:39 -05006435 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006436 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006437 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006438
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006439 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006440 err = btrfs_update_inode(trans, root, inode);
6441 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006442 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006443
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006444 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6445 dentry->d_name.name,
6446 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006447 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006448 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006449
Al Viro1e2e5472018-05-04 08:23:01 -04006450 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006451
6452out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006453 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006454 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006455 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006456 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006457 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006458 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006459 return err;
6460}
6461
Chris Masonc8b97812008-10-29 14:49:59 -04006462static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006463 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006464 size_t pg_offset, u64 extent_offset,
6465 struct btrfs_file_extent_item *item)
6466{
6467 int ret;
6468 struct extent_buffer *leaf = path->nodes[0];
6469 char *tmp;
6470 size_t max_size;
6471 unsigned long inline_size;
6472 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006473 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006474
6475 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006476 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006477 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6478 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006479 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006480 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006481 if (!tmp)
6482 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006483 ptr = btrfs_file_extent_inline_start(item);
6484
6485 read_extent_buffer(leaf, tmp, ptr, inline_size);
6486
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006487 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006488 ret = btrfs_decompress(compress_type, tmp, page,
6489 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006490
6491 /*
6492 * decompression code contains a memset to fill in any space between the end
6493 * of the uncompressed data and the end of max_size in case the decompressed
6494 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6495 * the end of an inline extent and the beginning of the next block, so we
6496 * cover that region here.
6497 */
6498
6499 if (max_size + pg_offset < PAGE_SIZE) {
6500 char *map = kmap(page);
6501 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6502 kunmap(page);
6503 }
Chris Masonc8b97812008-10-29 14:49:59 -04006504 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006505 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006506}
6507
Omar Sandoval39b07b52019-12-02 17:34:23 -08006508/**
6509 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6510 * @inode: file to search in
6511 * @page: page to read extent data into if the extent is inline
6512 * @pg_offset: offset into @page to copy to
6513 * @start: file offset
6514 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006515 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006516 * This returns the first &struct extent_map which overlaps with the given
6517 * range, reading it from the B-tree and caching it if necessary. Note that
6518 * there may be more extents which overlap the given range after the returned
6519 * extent_map.
6520 *
6521 * If @page is not NULL and the extent is inline, this also reads the extent
6522 * data directly into the page and marks the extent up to date in the io_tree.
6523 *
6524 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006525 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006526struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006527 struct page *page, size_t pg_offset,
6528 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006529{
David Sterba3ffbd682018-06-29 10:56:42 +02006530 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006531 int ret;
6532 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006533 u64 extent_start = 0;
6534 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006535 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006536 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006537 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006538 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006539 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006540 struct extent_buffer *leaf;
6541 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006542 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006543 struct extent_map_tree *em_tree = &inode->extent_tree;
6544 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006545
Chris Mason890871b2009-09-02 16:24:52 -04006546 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006547 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006548 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006549
Chris Masona52d9a82007-08-27 16:49:44 -04006550 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006551 if (em->start > start || em->start + em->len <= start)
6552 free_extent_map(em);
6553 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006554 free_extent_map(em);
6555 else
6556 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006557 }
David Sterba172ddd62011-04-21 00:48:27 +02006558 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006559 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006560 err = -ENOMEM;
6561 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006562 }
Chris Masond1310b22008-01-24 16:13:08 -05006563 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006564 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006565 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006566 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006567
Liu Bobee6ec82018-08-17 05:05:28 +08006568 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006569 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006570 err = -ENOMEM;
6571 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006572 }
6573
Liu Bobee6ec82018-08-17 05:05:28 +08006574 /* Chances are we'll be called again, so go ahead and do readahead */
6575 path->reada = READA_FORWARD;
6576
Liu Boe49aabd2018-08-25 13:47:09 +08006577 /*
6578 * Unless we're going to uncompress the inline extent, no sleep would
6579 * happen.
6580 */
6581 path->leave_spinning = 1;
6582
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006583 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006584 if (ret < 0) {
6585 err = ret;
6586 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006587 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006588 if (path->slots[0] == 0)
6589 goto not_found;
6590 path->slots[0]--;
6591 }
6592
Chris Mason5f39d392007-10-15 16:14:19 -04006593 leaf = path->nodes[0];
6594 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006595 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006596 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006597 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006598 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006599 /*
6600 * If we backup past the first extent we want to move forward
6601 * and see if there is an extent in front of us, otherwise we'll
6602 * say there is a hole for our whole search range which can
6603 * cause problems.
6604 */
6605 extent_end = start;
6606 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006607 }
6608
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006609 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006610 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006611 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006612 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6613 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006614 /* Only regular file could have regular/prealloc extent */
6615 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6616 ret = -EUCLEAN;
6617 btrfs_crit(fs_info,
6618 "regular/prealloc extent found for non-regular inode %llu",
6619 btrfs_ino(inode));
6620 goto out;
6621 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006622 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6623 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006624 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006625 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6626 path->slots[0],
6627 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006628 }
Josef Bacik25a50342013-10-14 12:08:38 -04006629next:
Yan Zheng9036c102008-10-30 14:19:41 -04006630 if (start >= extent_end) {
6631 path->slots[0]++;
6632 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6633 ret = btrfs_next_leaf(root, path);
6634 if (ret < 0) {
6635 err = ret;
6636 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006637 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006638 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006639 }
Yan Zheng9036c102008-10-30 14:19:41 -04006640 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006641 }
Yan Zheng9036c102008-10-30 14:19:41 -04006642 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6643 if (found_key.objectid != objectid ||
6644 found_key.type != BTRFS_EXTENT_DATA_KEY)
6645 goto not_found;
6646 if (start + len <= found_key.offset)
6647 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006648 if (start > found_key.offset)
6649 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006650
6651 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006652 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006653 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006654 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006655 em->block_start = EXTENT_MAP_HOLE;
6656 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006657 }
6658
Omar Sandoval39b07b52019-12-02 17:34:23 -08006659 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006660
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006661 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6662 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006663 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006664 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006665 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006666 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006667 size_t size;
6668 size_t extent_offset;
6669 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006670
Omar Sandoval39b07b52019-12-02 17:34:23 -08006671 if (!page)
Yan689f9342007-10-29 11:41:07 -04006672 goto out;
Yan689f9342007-10-29 11:41:07 -04006673
Qu Wenruoe41ca582018-06-06 15:41:49 +08006674 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006675 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006676 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6677 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006678 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006679 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006680 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006681 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006682 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006683
6684 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006685 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006686 if (btrfs_file_extent_compression(leaf, item) !=
6687 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006688 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006689 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006690 if (ret) {
6691 err = ret;
6692 goto out;
6693 }
Chris Masonc8b97812008-10-29 14:49:59 -04006694 } else {
6695 map = kmap(page);
6696 read_extent_buffer(leaf, map + pg_offset, ptr,
6697 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006698 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006699 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006700 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006701 copy_size);
6702 }
Chris Masonc8b97812008-10-29 14:49:59 -04006703 kunmap(page);
6704 }
Chris Mason179e29e2007-11-01 11:28:41 -04006705 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006706 }
Chris Masond1310b22008-01-24 16:13:08 -05006707 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006708 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006709 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006710 }
6711not_found:
6712 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006713 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006714 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006715 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006716insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006717 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006718 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006719 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006720 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6721 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006722 err = -EIO;
6723 goto out;
6724 }
Chris Masond1310b22008-01-24 16:13:08 -05006725
6726 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006727 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006728 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006729 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006730out:
Liu Boc6414282018-08-23 07:36:17 +08006731 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006732
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006733 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006734
Chris Masona52d9a82007-08-27 16:49:44 -04006735 if (err) {
6736 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006737 return ERR_PTR(err);
6738 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006739 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006740 return em;
6741}
6742
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006743struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006744 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006745{
6746 struct extent_map *em;
6747 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006748 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006749 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006750 u64 delalloc_len;
6751 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006752 int err = 0;
6753
Omar Sandoval39b07b52019-12-02 17:34:23 -08006754 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006755 if (IS_ERR(em))
6756 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006757 /*
6758 * If our em maps to:
6759 * - a hole or
6760 * - a pre-alloc extent,
6761 * there might actually be delalloc bytes behind it.
6762 */
6763 if (em->block_start != EXTENT_MAP_HOLE &&
6764 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6765 return em;
6766 else
6767 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006768
6769 /* check to see if we've wrapped (len == -1 or similar) */
6770 end = start + len;
6771 if (end < start)
6772 end = (u64)-1;
6773 else
6774 end -= 1;
6775
6776 em = NULL;
6777
6778 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006779 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006780 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006781 delalloc_end = delalloc_start + delalloc_len;
6782 if (delalloc_end < delalloc_start)
6783 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006784
6785 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006786 * We didn't find anything useful, return the original results from
6787 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006788 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006789 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006790 em = hole_em;
6791 hole_em = NULL;
6792 goto out;
6793 }
6794
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006795 /*
6796 * Adjust the delalloc_start to make sure it doesn't go backwards from
6797 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006798 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006799 delalloc_start = max(start, delalloc_start);
6800 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006801
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006802 if (delalloc_len > 0) {
6803 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006804 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006805 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006806
David Sterba172ddd62011-04-21 00:48:27 +02006807 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006808 if (!em) {
6809 err = -ENOMEM;
6810 goto out;
6811 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006812
6813 ASSERT(hole_em);
6814 /*
6815 * When btrfs_get_extent can't find anything it returns one
6816 * huge hole
6817 *
6818 * Make sure what it found really fits our range, and adjust to
6819 * make sure it is based on the start from the caller
6820 */
6821 if (hole_end <= start || hole_em->start > end) {
6822 free_extent_map(hole_em);
6823 hole_em = NULL;
6824 } else {
6825 hole_start = max(hole_em->start, start);
6826 hole_len = hole_end - hole_start;
6827 }
6828
6829 if (hole_em && delalloc_start > hole_start) {
6830 /*
6831 * Our hole starts before our delalloc, so we have to
6832 * return just the parts of the hole that go until the
6833 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006834 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006835 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006836 em->start = hole_start;
6837 em->orig_start = hole_start;
6838 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006839 * Don't adjust block start at all, it is fixed at
6840 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006841 */
6842 em->block_start = hole_em->block_start;
6843 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006844 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6845 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006846 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006847 /*
6848 * Hole is out of passed range or it starts after
6849 * delalloc range
6850 */
6851 em->start = delalloc_start;
6852 em->len = delalloc_len;
6853 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006854 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006855 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006856 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006857 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006858 return hole_em;
6859 }
6860out:
6861
6862 free_extent_map(hole_em);
6863 if (err) {
6864 free_extent_map(em);
6865 return ERR_PTR(err);
6866 }
6867 return em;
6868}
6869
Nikolay Borisov64f54182020-06-03 08:55:31 +03006870static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006871 const u64 start,
6872 const u64 len,
6873 const u64 orig_start,
6874 const u64 block_start,
6875 const u64 block_len,
6876 const u64 orig_block_len,
6877 const u64 ram_bytes,
6878 const int type)
6879{
6880 struct extent_map *em = NULL;
6881 int ret;
6882
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006883 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03006884 em = create_io_em(inode, start, len, orig_start, block_start,
6885 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08006886 BTRFS_COMPRESS_NONE, /* compress_type */
6887 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006888 if (IS_ERR(em))
6889 goto out;
6890 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03006891 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
6892 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006893 if (ret) {
6894 if (em) {
6895 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03006896 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006897 }
6898 em = ERR_PTR(ret);
6899 }
6900 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006901
6902 return em;
6903}
6904
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006905static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006906 u64 start, u64 len)
6907{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006908 struct btrfs_root *root = inode->root;
6909 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04006910 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006911 struct btrfs_key ins;
6912 u64 alloc_hint;
6913 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006914
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006915 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006916 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006917 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006918 if (ret)
6919 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006920
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006921 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006922 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006923 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006924 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006925 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006926 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
6927 1);
Josef Bacik00361582013-08-14 14:02:47 -04006928
Josef Bacik4b46fce2010-05-23 11:00:55 -04006929 return em;
6930}
6931
Chris Mason46bfbb52010-05-26 11:04:10 -04006932/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006933 * Check if we can do nocow write into the range [@offset, @offset + @len)
6934 *
6935 * @offset: File offset
6936 * @len: The length to write, will be updated to the nocow writeable
6937 * range
6938 * @orig_start: (optional) Return the original file offset of the file extent
6939 * @orig_len: (optional) Return the original on-disk length of the file extent
6940 * @ram_bytes: (optional) Return the ram_bytes of the file extent
6941 *
6942 * This function will flush ordered extents in the range to ensure proper
6943 * nocow checks for (nowait == false) case.
6944 *
6945 * Return:
6946 * >0 and update @len if we can do nocow write
6947 * 0 if we can't do nocow write
6948 * <0 if error happened
6949 *
6950 * NOTE: This only checks the file extents, caller is responsible to wait for
6951 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04006952 */
Josef Bacik00361582013-08-14 14:02:47 -04006953noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006954 u64 *orig_start, u64 *orig_block_len,
6955 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006956{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006957 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006958 struct btrfs_path *path;
6959 int ret;
6960 struct extent_buffer *leaf;
6961 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006962 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006963 struct btrfs_file_extent_item *fi;
6964 struct btrfs_key key;
6965 u64 disk_bytenr;
6966 u64 backref_offset;
6967 u64 extent_end;
6968 u64 num_bytes;
6969 int slot;
6970 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006971 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006972
Chris Mason46bfbb52010-05-26 11:04:10 -04006973 path = btrfs_alloc_path();
6974 if (!path)
6975 return -ENOMEM;
6976
David Sterbaf85b7372017-01-20 14:54:07 +01006977 ret = btrfs_lookup_file_extent(NULL, root, path,
6978 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006979 if (ret < 0)
6980 goto out;
6981
6982 slot = path->slots[0];
6983 if (ret == 1) {
6984 if (slot == 0) {
6985 /* can't find the item, must cow */
6986 ret = 0;
6987 goto out;
6988 }
6989 slot--;
6990 }
6991 ret = 0;
6992 leaf = path->nodes[0];
6993 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006994 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006995 key.type != BTRFS_EXTENT_DATA_KEY) {
6996 /* not our file or wrong item type, must cow */
6997 goto out;
6998 }
6999
7000 if (key.offset > offset) {
7001 /* Wrong offset, must cow */
7002 goto out;
7003 }
7004
7005 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7006 found_type = btrfs_file_extent_type(leaf, fi);
7007 if (found_type != BTRFS_FILE_EXTENT_REG &&
7008 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7009 /* not a regular extent, must cow */
7010 goto out;
7011 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007012
7013 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7014 goto out;
7015
Miao Xiee77751a2013-12-27 21:11:50 +08007016 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7017 if (extent_end <= offset)
7018 goto out;
7019
Chris Mason46bfbb52010-05-26 11:04:10 -04007020 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007021 if (disk_bytenr == 0)
7022 goto out;
7023
7024 if (btrfs_file_extent_compression(leaf, fi) ||
7025 btrfs_file_extent_encryption(leaf, fi) ||
7026 btrfs_file_extent_other_encoding(leaf, fi))
7027 goto out;
7028
Ethan Lien78d42952018-05-17 14:58:29 +08007029 /*
7030 * Do the same check as in btrfs_cross_ref_exist but without the
7031 * unnecessary search.
7032 */
7033 if (btrfs_file_extent_generation(leaf, fi) <=
7034 btrfs_root_last_snapshot(&root->root_item))
7035 goto out;
7036
Chris Mason46bfbb52010-05-26 11:04:10 -04007037 backref_offset = btrfs_file_extent_offset(leaf, fi);
7038
Josef Bacik7ee9e442013-06-21 16:37:03 -04007039 if (orig_start) {
7040 *orig_start = key.offset - backref_offset;
7041 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7042 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7043 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007044
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007045 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007046 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007047
7048 num_bytes = min(offset + *len, extent_end) - offset;
7049 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7050 u64 range_end;
7051
Jeff Mahoneyda170662016-06-15 09:22:56 -04007052 range_end = round_up(offset + num_bytes,
7053 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007054 ret = test_range_bit(io_tree, offset, range_end,
7055 EXTENT_DELALLOC, 0, NULL);
7056 if (ret) {
7057 ret = -EAGAIN;
7058 goto out;
7059 }
7060 }
7061
Josef Bacik1bda19e2013-10-18 12:10:36 -04007062 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007063
7064 /*
7065 * look for other files referencing this extent, if we
7066 * find any we must cow
7067 */
Josef Bacik00361582013-08-14 14:02:47 -04007068
Liu Boe4c3b2d2017-01-30 12:25:28 -08007069 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007070 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007071 if (ret) {
7072 ret = 0;
7073 goto out;
7074 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007075
7076 /*
7077 * adjust disk_bytenr and num_bytes to cover just the bytes
7078 * in this extent we are about to write. If there
7079 * are any csums in that range we have to cow in order
7080 * to keep the csums correct
7081 */
7082 disk_bytenr += backref_offset;
7083 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007084 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7085 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007086 /*
7087 * all of the above have passed, it is safe to overwrite this extent
7088 * without cow
7089 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007090 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007091 ret = 1;
7092out:
7093 btrfs_free_path(path);
7094 return ret;
7095}
7096
Josef Bacikeb838e72012-07-31 16:28:48 -04007097static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
David Sterba55e20bd2020-06-09 19:56:06 +02007098 struct extent_state **cached_state, int writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007099{
7100 struct btrfs_ordered_extent *ordered;
7101 int ret = 0;
7102
7103 while (1) {
7104 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007105 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007106 /*
7107 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007108 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007109 * extents in this range.
7110 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007111 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007112 lockend - lockstart + 1);
7113
7114 /*
7115 * We need to make sure there are no buffered pages in this
7116 * range either, we could have raced between the invalidate in
7117 * generic_file_direct_write and locking the extent. The
7118 * invalidate needs to happen so that reads after a write do not
7119 * get stale data.
7120 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007121 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007122 (!writing || !filemap_range_has_page(inode->i_mapping,
7123 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007124 break;
7125
7126 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007127 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007128
7129 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007130 /*
7131 * If we are doing a DIO read and the ordered extent we
7132 * found is for a buffered write, we can not wait for it
7133 * to complete and retry, because if we do so we can
7134 * deadlock with concurrent buffered writes on page
7135 * locks. This happens only if our DIO read covers more
7136 * than one extent map, if at this point has already
7137 * created an ordered extent for a previous extent map
7138 * and locked its range in the inode's io tree, and a
7139 * concurrent write against that previous extent map's
7140 * range and this range started (we unlock the ranges
7141 * in the io tree only when the bios complete and
7142 * buffered writes always lock pages before attempting
7143 * to lock range in the io tree).
7144 */
7145 if (writing ||
7146 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7147 btrfs_start_ordered_extent(inode, ordered, 1);
7148 else
7149 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007150 btrfs_put_ordered_extent(ordered);
7151 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007152 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007153 * We could trigger writeback for this range (and wait
7154 * for it to complete) and then invalidate the pages for
7155 * this range (through invalidate_inode_pages2_range()),
7156 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007157 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007158 * triggered a readahead) on a page lock due to an
7159 * ordered dio extent we created before but did not have
7160 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007161 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007162 * ordered extent to complete while holding a lock on
7163 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007164 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007165 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007166 }
7167
Filipe Mananaade77022016-02-18 14:28:55 +00007168 if (ret)
7169 break;
7170
Josef Bacikeb838e72012-07-31 16:28:48 -04007171 cond_resched();
7172 }
7173
7174 return ret;
7175}
7176
Liu Bo6f9994d2017-01-31 07:50:22 -08007177/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007178static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7179 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007180 u64 block_len, u64 orig_block_len,
7181 u64 ram_bytes, int compress_type,
7182 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007183{
7184 struct extent_map_tree *em_tree;
7185 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007186 int ret;
7187
Liu Bo6f9994d2017-01-31 07:50:22 -08007188 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7189 type == BTRFS_ORDERED_COMPRESSED ||
7190 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007191 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007192
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007193 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007194 em = alloc_extent_map();
7195 if (!em)
7196 return ERR_PTR(-ENOMEM);
7197
7198 em->start = start;
7199 em->orig_start = orig_start;
7200 em->len = len;
7201 em->block_len = block_len;
7202 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007203 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007204 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007205 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007206 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007207 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007208 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007209 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007210 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7211 em->compress_type = compress_type;
7212 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007213
7214 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007215 btrfs_drop_extent_cache(inode, em->start,
7216 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007217 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007218 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007219 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007220 /*
7221 * The caller has taken lock_extent(), who could race with us
7222 * to add em?
7223 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007224 } while (ret == -EEXIST);
7225
7226 if (ret) {
7227 free_extent_map(em);
7228 return ERR_PTR(ret);
7229 }
7230
Liu Bo6f9994d2017-01-31 07:50:22 -08007231 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007232 return em;
7233}
7234
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007235
David Sterba55e20bd2020-06-09 19:56:06 +02007236static int btrfs_get_blocks_direct_read(struct extent_map *em,
7237 struct buffer_head *bh_result,
7238 struct inode *inode,
7239 u64 start, u64 len)
7240{
7241 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7242
7243 if (em->block_start == EXTENT_MAP_HOLE ||
7244 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7245 return -ENOENT;
7246
7247 len = min(len, em->len - (start - em->start));
7248
7249 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7250 inode->i_blkbits;
7251 bh_result->b_size = len;
7252 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7253 set_buffer_mapped(bh_result);
7254
7255 return 0;
7256}
7257
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007258static int btrfs_get_blocks_direct_write(struct extent_map **map,
David Sterba55e20bd2020-06-09 19:56:06 +02007259 struct buffer_head *bh_result,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007260 struct inode *inode,
7261 struct btrfs_dio_data *dio_data,
7262 u64 start, u64 len)
7263{
7264 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7265 struct extent_map *em = *map;
7266 int ret = 0;
7267
7268 /*
7269 * We don't allocate a new extent in the following cases
7270 *
7271 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7272 * existing extent.
7273 * 2) The extent is marked as PREALLOC. We're good to go here and can
7274 * just use the extent.
7275 *
7276 */
7277 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7278 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7279 em->block_start != EXTENT_MAP_HOLE)) {
7280 int type;
7281 u64 block_start, orig_start, orig_block_len, ram_bytes;
7282
7283 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7284 type = BTRFS_ORDERED_PREALLOC;
7285 else
7286 type = BTRFS_ORDERED_NOCOW;
7287 len = min(len, em->len - (start - em->start));
7288 block_start = em->block_start + (start - em->start);
7289
7290 if (can_nocow_extent(inode, start, &len, &orig_start,
7291 &orig_block_len, &ram_bytes) == 1 &&
7292 btrfs_inc_nocow_writers(fs_info, block_start)) {
7293 struct extent_map *em2;
7294
Nikolay Borisov64f54182020-06-03 08:55:31 +03007295 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007296 orig_start, block_start,
7297 len, orig_block_len,
7298 ram_bytes, type);
7299 btrfs_dec_nocow_writers(fs_info, block_start);
7300 if (type == BTRFS_ORDERED_PREALLOC) {
7301 free_extent_map(em);
7302 *map = em = em2;
7303 }
7304
7305 if (em2 && IS_ERR(em2)) {
7306 ret = PTR_ERR(em2);
7307 goto out;
7308 }
7309 /*
7310 * For inode marked NODATACOW or extent marked PREALLOC,
7311 * use the existing or preallocated extent, so does not
7312 * need to adjust btrfs_space_info's bytes_may_use.
7313 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007314 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007315 goto skip_cow;
7316 }
7317 }
7318
7319 /* this will cow the extent */
David Sterba55e20bd2020-06-09 19:56:06 +02007320 len = bh_result->b_size;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007321 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007322 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007323 if (IS_ERR(em)) {
7324 ret = PTR_ERR(em);
7325 goto out;
7326 }
7327
7328 len = min(len, em->len - (start - em->start));
7329
7330skip_cow:
David Sterba55e20bd2020-06-09 19:56:06 +02007331 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7332 inode->i_blkbits;
7333 bh_result->b_size = len;
7334 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7335 set_buffer_mapped(bh_result);
7336
7337 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7338 set_buffer_new(bh_result);
7339
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007340 /*
7341 * Need to update the i_size under the extent lock so buffered
7342 * readers will get the updated i_size when we unlock.
7343 */
David Sterba55e20bd2020-06-09 19:56:06 +02007344 if (!dio_data->overwrite && start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007345 i_size_write(inode, start + len);
7346
David Sterba55e20bd2020-06-09 19:56:06 +02007347 WARN_ON(dio_data->reserve < len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007348 dio_data->reserve -= len;
David Sterba55e20bd2020-06-09 19:56:06 +02007349 dio_data->unsubmitted_oe_range_end = start + len;
7350 current->journal_info = dio_data;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007351out:
7352 return ret;
7353}
7354
David Sterba55e20bd2020-06-09 19:56:06 +02007355static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7356 struct buffer_head *bh_result, int create)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007357{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007358 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007359 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007360 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307361 struct btrfs_dio_data *dio_data = NULL;
David Sterba55e20bd2020-06-09 19:56:06 +02007362 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007363 u64 lockstart, lockend;
David Sterba55e20bd2020-06-09 19:56:06 +02007364 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007365 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007366
David Sterba55e20bd2020-06-09 19:56:06 +02007367 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007368 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007369
Josef Bacikc3298612012-08-03 16:49:19 -04007370 lockstart = start;
7371 lockend = start + len - 1;
7372
David Sterba55e20bd2020-06-09 19:56:06 +02007373 if (current->journal_info) {
7374 /*
7375 * Need to pull our outstanding extents and set journal_info to NULL so
7376 * that anything that needs to check if there's a transaction doesn't get
7377 * confused.
7378 */
7379 dio_data = current->journal_info;
7380 current->journal_info = NULL;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007381 }
7382
Josef Bacikeb838e72012-07-31 16:28:48 -04007383 /*
7384 * If this errors out it's because we couldn't invalidate pagecache for
7385 * this range and we need to fallback to buffered.
7386 */
David Sterba55e20bd2020-06-09 19:56:06 +02007387 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7388 create)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007389 ret = -ENOTBLK;
7390 goto err;
7391 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007392
Omar Sandoval39b07b52019-12-02 17:34:23 -08007393 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007394 if (IS_ERR(em)) {
7395 ret = PTR_ERR(em);
7396 goto unlock_err;
7397 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007398
7399 /*
7400 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7401 * io. INLINE is special, and we could probably kludge it in here, but
7402 * it's still buffered so for safety lets just fall back to the generic
7403 * buffered path.
7404 *
7405 * For COMPRESSED we _have_ to read the entire extent in so we can
7406 * decompress it, so there will be buffering required no matter what we
7407 * do, so go ahead and fallback to buffered.
7408 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007409 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007410 * to buffered IO. Don't blame me, this is the price we pay for using
7411 * the generic code.
7412 */
7413 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7414 em->block_start == EXTENT_MAP_INLINE) {
7415 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007416 ret = -ENOTBLK;
7417 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007418 }
7419
David Sterba55e20bd2020-06-09 19:56:06 +02007420 if (create) {
7421 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7422 dio_data, start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007423 if (ret < 0)
7424 goto unlock_err;
David Sterba55e20bd2020-06-09 19:56:06 +02007425
7426 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7427 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007428 } else {
David Sterba55e20bd2020-06-09 19:56:06 +02007429 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7430 start, len);
7431 /* Can be negative only if we read from a hole */
7432 if (ret < 0) {
7433 ret = 0;
7434 free_extent_map(em);
7435 goto unlock_err;
7436 }
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007437 /*
7438 * We need to unlock only the end area that we aren't using.
7439 * The rest is going to be unlocked by the endio routine.
7440 */
David Sterba55e20bd2020-06-09 19:56:06 +02007441 lockstart = start + bh_result->b_size;
7442 if (lockstart < lockend) {
7443 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7444 lockstart, lockend, &cached_state);
7445 } else {
7446 free_extent_state(cached_state);
7447 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007448 }
7449
Josef Bacik4b46fce2010-05-23 11:00:55 -04007450 free_extent_map(em);
7451
7452 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007453
7454unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007455 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7456 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007457err:
David Sterba55e20bd2020-06-09 19:56:06 +02007458 if (dio_data)
7459 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007460 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007461}
7462
Omar Sandoval769b4f22020-04-16 14:46:22 -07007463static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7464{
7465 /*
7466 * This implies a barrier so that stores to dio_bio->bi_status before
7467 * this and loads of dio_bio->bi_status after this are fully ordered.
7468 */
7469 if (!refcount_dec_and_test(&dip->refs))
7470 return;
7471
7472 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007473 __endio_write_update_ordered(BTRFS_I(dip->inode),
7474 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007475 dip->bytes,
7476 !dip->dio_bio->bi_status);
7477 } else {
7478 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7479 dip->logical_offset,
7480 dip->logical_offset + dip->bytes - 1);
7481 }
7482
David Sterba55e20bd2020-06-09 19:56:06 +02007483 dio_end_io(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007484 kfree(dip);
7485}
7486
Omar Sandoval77d5d682020-04-16 14:46:25 -07007487static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7488 int mirror_num,
7489 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007490{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007491 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007492 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007493 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007494
Mike Christie37226b22016-06-05 14:31:52 -05007495 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007496
Omar Sandoval5c047a62020-04-16 14:46:24 -07007497 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007498 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007499 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007500
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007501 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007502 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007503 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007504 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007505 return ret;
7506}
7507
Omar Sandoval769b4f22020-04-16 14:46:22 -07007508static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7509 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007510 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007511{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007512 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7513 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007514 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007515 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7516 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007517 struct bio_vec bvec;
7518 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007519 u64 start = io_bio->logical;
7520 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007521 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007522
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007523 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7524 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307525
Liu Bo17347ce2017-05-15 15:33:27 -07007526 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7527 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007528 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007529 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007530 if (uptodate &&
7531 (!csum || !check_data_csum(inode, io_bio, icsum,
7532 bvec.bv_page, pgoff,
7533 start, sectorsize))) {
7534 clean_io_failure(fs_info, failure_tree, io_tree,
7535 start, bvec.bv_page,
7536 btrfs_ino(BTRFS_I(inode)),
7537 pgoff);
7538 } else {
7539 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007540
Omar Sandoval77d5d682020-04-16 14:46:25 -07007541 status = btrfs_submit_read_repair(inode,
7542 &io_bio->bio,
7543 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007544 bvec.bv_page, pgoff,
7545 start,
7546 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007547 io_bio->mirror_num,
7548 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007549 if (status)
7550 err = status;
7551 }
7552 start += sectorsize;
7553 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307554 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307555 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007556 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007557 return err;
7558}
7559
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007560static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007561 const u64 offset, const u64 bytes,
7562 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007563{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007564 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007565 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007566 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007567 u64 ordered_offset = offset;
7568 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007569 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007570
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007571 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007572 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007573 else
Qu Wenruo524272602017-03-08 10:25:52 +08007574 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007575
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007576 while (ordered_offset < offset + bytes) {
7577 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007578 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007579 &ordered_offset,
7580 ordered_bytes,
7581 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007582 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7583 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007584 btrfs_queue_work(wq, &ordered->work);
7585 }
7586 /*
7587 * If btrfs_dec_test_ordered_pending does not find any ordered
7588 * extent in the range, we can exit.
7589 */
7590 if (ordered_offset == last_offset)
7591 return;
7592 /*
7593 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007594 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007595 */
7596 if (ordered_offset < offset + bytes) {
7597 ordered_bytes = offset + bytes - ordered_offset;
7598 ordered = NULL;
7599 }
Chris Mason163cf092010-11-28 19:56:33 -05007600 }
Filipe Manana14543772015-11-24 16:23:54 +00007601}
7602
David Sterbad0ee3932018-03-08 14:35:48 +01007603static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007604 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007605{
Josef Bacikc6100a42017-05-05 11:57:13 -04007606 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007607 blk_status_t ret;
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007608 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007609 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007610 return 0;
7611}
7612
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007613static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007614{
7615 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007616 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007617
Miao Xie8b110e32014-09-12 18:44:03 +08007618 if (err)
7619 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007620 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007621 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7622 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007623 (unsigned long long)bio->bi_iter.bi_sector,
7624 bio->bi_iter.bi_size, err);
7625
Omar Sandoval769b4f22020-04-16 14:46:22 -07007626 if (bio_op(bio) == REQ_OP_READ) {
7627 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007628 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007629 }
7630
Omar Sandoval769b4f22020-04-16 14:46:22 -07007631 if (err)
7632 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007633
Miao Xiee65e1532010-11-22 03:04:43 +00007634 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007635 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007636}
7637
David Sterbad0ee3932018-03-08 14:35:48 +01007638static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7639 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007640{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007641 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007642 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007643 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007644 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007645
Liu Bo4c274bc2017-11-01 17:19:27 -06007646 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007647 if (async_submit)
7648 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7649
Josef Bacik5fd02042012-05-02 14:00:54 -04007650 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007651 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007652 if (ret)
7653 goto err;
7654 }
Miao Xiee65e1532010-11-22 03:04:43 +00007655
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007656 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007657 goto map;
7658
7659 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007660 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7661 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007662 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007663 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007664 } else if (write) {
7665 /*
7666 * If we aren't doing async submit, calculate the csum of the
7667 * bio now.
7668 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007669 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007670 if (ret)
7671 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007672 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007673 u64 csum_offset;
7674
7675 csum_offset = file_offset - dip->logical_offset;
7676 csum_offset >>= inode->i_sb->s_blocksize_bits;
7677 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7678 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007679 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007680map:
Chris Mason08635ba2019-07-10 12:28:14 -07007681 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007682err:
Miao Xiee65e1532010-11-22 03:04:43 +00007683 return ret;
7684}
7685
Omar Sandovalc36cac22020-04-16 14:46:13 -07007686/*
7687 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7688 * or ordered extents whether or not we submit any bios.
7689 */
7690static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7691 struct inode *inode,
7692 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007693{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007694 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007695 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7696 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007697 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007698
Omar Sandoval85879572020-04-16 14:46:21 -07007699 dip_size = sizeof(*dip);
7700 if (!write && csum) {
7701 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7702 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7703 size_t nblocks;
7704
7705 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7706 dip_size += csum_size * nblocks;
7707 }
7708
7709 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007710 if (!dip)
7711 return NULL;
7712
Omar Sandovalc36cac22020-04-16 14:46:13 -07007713 dip->inode = inode;
7714 dip->logical_offset = file_offset;
7715 dip->bytes = dio_bio->bi_iter.bi_size;
7716 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007717 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007718 refcount_set(&dip->refs, 1);
David Sterba55e20bd2020-06-09 19:56:06 +02007719
7720 if (write) {
7721 struct btrfs_dio_data *dio_data = current->journal_info;
7722
7723 /*
7724 * Setting range start and end to the same value means that
7725 * no cleanup will happen in btrfs_direct_IO
7726 */
7727 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7728 dip->bytes;
7729 dio_data->unsubmitted_oe_range_start =
7730 dio_data->unsubmitted_oe_range_end;
7731 }
Omar Sandovalc36cac22020-04-16 14:46:13 -07007732 return dip;
7733}
7734
David Sterba55e20bd2020-06-09 19:56:06 +02007735static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7736 loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007737{
7738 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007739 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007740 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007741 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7742 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007743 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007744 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007745 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007746 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007747 u64 submit_len;
7748 int clone_offset = 0;
7749 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307750 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007751 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007752 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007753
Omar Sandovalc36cac22020-04-16 14:46:13 -07007754 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7755 if (!dip) {
7756 if (!write) {
7757 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7758 file_offset + dio_bio->bi_iter.bi_size - 1);
7759 }
7760 dio_bio->bi_status = BLK_STS_RESOURCE;
David Sterba55e20bd2020-06-09 19:56:06 +02007761 dio_end_io(dio_bio);
7762 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04007763 }
7764
Omar Sandoval85879572020-04-16 14:46:21 -07007765 if (!write && csum) {
7766 /*
7767 * Load the csums up front to reduce csum tree searches and
7768 * contention when submitting bios.
7769 */
7770 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7771 dip->csums);
7772 if (status != BLK_STS_OK)
7773 goto out_err;
7774 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007775
Omar Sandoval769b4f22020-04-16 14:46:22 -07007776 start_sector = dio_bio->bi_iter.bi_sector;
7777 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007778
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007779 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007780 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7781 start_sector << 9, submit_len,
7782 &geom);
7783 if (ret) {
7784 status = errno_to_blk_status(ret);
7785 goto out_err;
7786 }
7787 ASSERT(geom.len <= INT_MAX);
7788
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007789 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007790
Liu Bo725130b2017-05-16 09:51:39 -07007791 /*
7792 * This will never fail as it's passing GPF_NOFS and
7793 * the allocation is backed by btrfs_bioset.
7794 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007795 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007796 bio->bi_private = dip;
7797 bio->bi_end_io = btrfs_end_dio_bio;
7798 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007799
Liu Bo725130b2017-05-16 09:51:39 -07007800 ASSERT(submit_len >= clone_len);
7801 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007802
Liu Bo725130b2017-05-16 09:51:39 -07007803 /*
7804 * Increase the count before we submit the bio so we know
7805 * the end IO handler won't happen before we increase the
7806 * count. Otherwise, the dip might get freed before we're
7807 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007808 *
7809 * We transfer the initial reference to the last bio, so we
7810 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007811 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007812 if (submit_len > 0) {
7813 refcount_inc(&dip->refs);
7814 /*
7815 * If we are submitting more than one bio, submit them
7816 * all asynchronously. The exception is RAID 5 or 6, as
7817 * asynchronous checksums make it difficult to collect
7818 * full stripe writes.
7819 */
7820 if (!raid56)
7821 async_submit = 1;
7822 }
Miao Xiee65e1532010-11-22 03:04:43 +00007823
David Sterbad0ee3932018-03-08 14:35:48 +01007824 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007825 async_submit);
7826 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007827 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007828 if (submit_len > 0)
7829 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007830 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007831 }
Liu Bo725130b2017-05-16 09:51:39 -07007832
7833 clone_offset += clone_len;
7834 start_sector += clone_len >> 9;
7835 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007836 } while (submit_len > 0);
David Sterba55e20bd2020-06-09 19:56:06 +02007837 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007838
Miao Xiee65e1532010-11-22 03:04:43 +00007839out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007840 dip->dio_bio->bi_status = status;
7841 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007842}
7843
David Sterbaf4c48b42020-06-09 19:19:27 +02007844static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7845 const struct iov_iter *iter, loff_t offset)
7846{
7847 int seg;
7848 int i;
7849 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7850 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007851
David Sterbaf4c48b42020-06-09 19:19:27 +02007852 if (offset & blocksize_mask)
7853 goto out;
7854
7855 if (iov_iter_alignment(iter) & blocksize_mask)
7856 goto out;
7857
7858 /* If this is a write we don't need to check anymore */
7859 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7860 return 0;
7861 /*
7862 * Check to make sure we don't have duplicate iov_base's in this
7863 * iovec, if so return EINVAL, otherwise we'll get csum errors
7864 * when reading back.
7865 */
7866 for (seg = 0; seg < iter->nr_segs; seg++) {
7867 for (i = seg + 1; i < iter->nr_segs; i++) {
7868 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7869 goto out;
7870 }
7871 }
7872 retval = 0;
7873out:
7874 return retval;
7875}
7876
David Sterba55e20bd2020-06-09 19:56:06 +02007877static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007878{
7879 struct file *file = iocb->ki_filp;
7880 struct inode *inode = file->f_mapping->host;
7881 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba55e20bd2020-06-09 19:56:06 +02007882 struct btrfs_dio_data dio_data = { 0 };
David Sterbaf4c48b42020-06-09 19:19:27 +02007883 struct extent_changeset *data_reserved = NULL;
7884 loff_t offset = iocb->ki_pos;
7885 size_t count = 0;
David Sterba55e20bd2020-06-09 19:56:06 +02007886 int flags = 0;
7887 bool wakeup = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007888 bool relock = false;
7889 ssize_t ret;
7890
7891 if (check_direct_IO(fs_info, iter, offset))
7892 return 0;
7893
David Sterba55e20bd2020-06-09 19:56:06 +02007894 inode_dio_begin(inode);
7895
7896 /*
7897 * The generic stuff only does filemap_write_and_wait_range, which
7898 * isn't enough if we've written compressed pages to this area, so
7899 * we need to flush the dirty pages again to make absolutely sure
7900 * that any outstanding dirty pages are on disk.
7901 */
David Sterbaf4c48b42020-06-09 19:19:27 +02007902 count = iov_iter_count(iter);
David Sterba55e20bd2020-06-09 19:56:06 +02007903 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7904 &BTRFS_I(inode)->runtime_flags))
7905 filemap_fdatawrite_range(inode->i_mapping, offset,
7906 offset + count - 1);
7907
David Sterbaf4c48b42020-06-09 19:19:27 +02007908 if (iov_iter_rw(iter) == WRITE) {
7909 /*
7910 * If the write DIO is beyond the EOF, we need update
7911 * the isize, but it is protected by i_mutex. So we can
7912 * not unlock the i_mutex at this case.
7913 */
7914 if (offset + count <= inode->i_size) {
David Sterba55e20bd2020-06-09 19:56:06 +02007915 dio_data.overwrite = 1;
David Sterbaf4c48b42020-06-09 19:19:27 +02007916 inode_unlock(inode);
7917 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007918 }
David Sterba55e20bd2020-06-09 19:56:06 +02007919 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
7920 offset, count);
7921 if (ret)
7922 goto out;
7923
7924 /*
7925 * We need to know how many extents we reserved so that we can
7926 * do the accounting properly if we go over the number we
7927 * originally calculated. Abuse current->journal_info for this.
7928 */
7929 dio_data.reserve = round_up(count,
7930 fs_info->sectorsize);
7931 dio_data.unsubmitted_oe_range_start = (u64)offset;
7932 dio_data.unsubmitted_oe_range_end = (u64)offset;
7933 current->journal_info = &dio_data;
David Sterbaf4c48b42020-06-09 19:19:27 +02007934 down_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007935 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7936 &BTRFS_I(inode)->runtime_flags)) {
7937 inode_dio_end(inode);
7938 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7939 wakeup = false;
David Sterbaf4c48b42020-06-09 19:19:27 +02007940 }
7941
David Sterba55e20bd2020-06-09 19:56:06 +02007942 ret = __blockdev_direct_IO(iocb, inode,
7943 fs_info->fs_devices->latest_bdev,
7944 iter, btrfs_get_blocks_direct, NULL,
7945 btrfs_submit_direct, flags);
David Sterbaf4c48b42020-06-09 19:19:27 +02007946 if (iov_iter_rw(iter) == WRITE) {
7947 up_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007948 current->journal_info = NULL;
7949 if (ret < 0 && ret != -EIOCBQUEUED) {
7950 if (dio_data.reserve)
Nikolay Borisov86d52922020-06-03 08:55:40 +03007951 btrfs_delalloc_release_space(BTRFS_I(inode),
7952 data_reserved, offset, dio_data.reserve,
7953 true);
David Sterba55e20bd2020-06-09 19:56:06 +02007954 /*
7955 * On error we might have left some ordered extents
7956 * without submitting corresponding bios for them, so
7957 * cleanup them up to avoid other tasks getting them
7958 * and waiting for them to complete forever.
7959 */
7960 if (dio_data.unsubmitted_oe_range_start <
7961 dio_data.unsubmitted_oe_range_end)
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007962 __endio_write_update_ordered(BTRFS_I(inode),
David Sterba55e20bd2020-06-09 19:56:06 +02007963 dio_data.unsubmitted_oe_range_start,
7964 dio_data.unsubmitted_oe_range_end -
7965 dio_data.unsubmitted_oe_range_start,
7966 false);
7967 } else if (ret >= 0 && (size_t)ret < count)
Nikolay Borisov86d52922020-06-03 08:55:40 +03007968 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
David Sterba55e20bd2020-06-09 19:56:06 +02007969 offset, count - (size_t)ret, true);
7970 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
David Sterbaf4c48b42020-06-09 19:19:27 +02007971 }
7972out:
David Sterba55e20bd2020-06-09 19:56:06 +02007973 if (wakeup)
7974 inode_dio_end(inode);
David Sterbaf4c48b42020-06-09 19:19:27 +02007975 if (relock)
7976 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02007977
David Sterbaf4c48b42020-06-09 19:19:27 +02007978 extent_changeset_free(data_reserved);
7979 return ret;
7980}
Chris Mason16432982008-04-10 10:23:21 -04007981
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007982static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02007983 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007984{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007985 int ret;
7986
Christoph Hellwig45dd0522020-05-23 09:30:14 +02007987 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007988 if (ret)
7989 return ret;
7990
David Sterba2135fb92017-06-23 04:09:57 +02007991 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007992}
7993
Chris Mason9ebefb182007-06-15 13:50:00 -04007994int btrfs_readpage(struct file *file, struct page *page)
7995{
David Sterba71ad38b2020-02-05 19:09:35 +01007996 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04007997}
Chris Mason1832a6d2007-12-21 16:27:21 -05007998
Chris Mason39279cc2007-06-12 06:35:45 -04007999static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8000{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008001 struct inode *inode = page->mapping->host;
8002 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008003
8004 if (current->flags & PF_MEMALLOC) {
8005 redirty_page_for_writepage(wbc, page);
8006 unlock_page(page);
8007 return 0;
8008 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008009
8010 /*
8011 * If we are under memory pressure we will call this directly from the
8012 * VM, we need to make sure we have the inode referenced for the ordered
8013 * extent. If not just return like we didn't do anything.
8014 */
8015 if (!igrab(inode)) {
8016 redirty_page_for_writepage(wbc, page);
8017 return AOP_WRITEPAGE_ACTIVATE;
8018 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008019 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008020 btrfs_add_delayed_iput(inode);
8021 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008022}
Chris Mason39279cc2007-06-12 06:35:45 -04008023
Eric Sandeen48a3b632013-04-25 20:41:01 +00008024static int btrfs_writepages(struct address_space *mapping,
8025 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008026{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008027 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008028}
8029
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008030static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008031{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008032 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008033}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008034
Chris Masone6dcd2d2008-07-17 12:53:50 -04008035static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008036{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008037 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008038 if (ret == 1)
8039 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008040 return ret;
8041}
Chris Mason39279cc2007-06-12 06:35:45 -04008042
Chris Masone6dcd2d2008-07-17 12:53:50 -04008043static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8044{
Chris Mason98509cf2008-09-11 15:51:43 -04008045 if (PageWriteback(page) || PageDirty(page))
8046 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008047 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008048}
8049
Roman Gushchinf8e66082020-03-04 16:57:35 -08008050#ifdef CONFIG_MIGRATION
8051static int btrfs_migratepage(struct address_space *mapping,
8052 struct page *newpage, struct page *page,
8053 enum migrate_mode mode)
8054{
8055 int ret;
8056
8057 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8058 if (ret != MIGRATEPAGE_SUCCESS)
8059 return ret;
8060
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008061 if (page_has_private(page))
8062 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008063
8064 if (PagePrivate2(page)) {
8065 ClearPagePrivate2(page);
8066 SetPagePrivate2(newpage);
8067 }
8068
8069 if (mode != MIGRATE_SYNC_NO_COPY)
8070 migrate_page_copy(newpage, page);
8071 else
8072 migrate_page_states(newpage, page);
8073 return MIGRATEPAGE_SUCCESS;
8074}
8075#endif
8076
Lukas Czernerd47992f2013-05-21 23:17:23 -04008077static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8078 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008079{
Josef Bacik5fd02042012-05-02 14:00:54 -04008080 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008081 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008082 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008083 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008084 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008085 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308086 u64 start;
8087 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008088 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008089
Chris Mason8b62b722009-09-02 16:53:46 -04008090 /*
8091 * we have the page locked, so new writeback can't start,
8092 * and the dirty bit won't be cleared while we are here.
8093 *
8094 * Wait for IO on this page so that we can safely clear
8095 * the PagePrivate2 bit and do ordered accounting
8096 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008097 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008098
Josef Bacik5fd02042012-05-02 14:00:54 -04008099 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008100 if (offset) {
8101 btrfs_releasepage(page, GFP_NOFS);
8102 return;
8103 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008104
8105 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008106 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308107again:
8108 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008109 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308110 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008111 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008112 end = min(page_end,
8113 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008114 /*
8115 * IO on this page will never be started, so we need
8116 * to account for any ordered extents now
8117 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008118 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308119 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008120 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008121 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008122 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008123 /*
8124 * whoever cleared the private bit is responsible
8125 * for the finish_ordered_io
8126 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008127 if (TestClearPagePrivate2(page)) {
8128 struct btrfs_ordered_inode_tree *tree;
8129 u64 new_len;
8130
8131 tree = &BTRFS_I(inode)->ordered_tree;
8132
8133 spin_lock_irq(&tree->lock);
8134 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308135 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008136 if (new_len < ordered->truncated_len)
8137 ordered->truncated_len = new_len;
8138 spin_unlock_irq(&tree->lock);
8139
8140 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308141 start,
8142 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008143 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008144 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008145 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008146 if (!inode_evicting) {
8147 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308148 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008149 &cached_state);
8150 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308151
8152 start = end + 1;
8153 if (start < page_end)
8154 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008155 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008156
Qu Wenruob9d0b382015-09-29 10:35:16 +08008157 /*
8158 * Qgroup reserved space handler
8159 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008160 * 1) Already written to disk or ordered extent already submitted
8161 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8162 * Qgroup will be handled by its qgroup_record then.
8163 * btrfs_qgroup_free_data() call will do nothing here.
8164 *
8165 * 2) Not written to disk yet
8166 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8167 * bit of its io_tree, and free the qgroup reserved data space.
8168 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008169 */
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03008170 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008171 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008172 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008173 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8174 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008175 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008176
8177 __btrfs_releasepage(page, GFP_NOFS);
8178 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008179
Chris Mason4a096752008-07-21 10:29:44 -04008180 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008181 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008182}
8183
Chris Mason9ebefb182007-06-15 13:50:00 -04008184/*
8185 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8186 * called from a page fault handler when a page is first dirtied. Hence we must
8187 * be careful to check for EOF conditions here. We set the page up correctly
8188 * for a written page which means we get ENOSPC checking when writing into
8189 * holes and correct delalloc and unwritten extent mapping on filesystems that
8190 * support these features.
8191 *
8192 * We are not allowed to take the i_mutex here so we have to play games to
8193 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008194 * truncate_setsize() writes the inode size before removing pages, once we have
8195 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008196 * beyond EOF, then the page is guaranteed safe against truncation until we
8197 * unlock the page.
8198 */
Souptick Joardera528a242018-06-06 19:54:44 +05308199vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008200{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008201 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008202 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008203 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008204 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8205 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008206 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008207 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008208 char *kaddr;
8209 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008210 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308211 vm_fault_t ret;
8212 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008213 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308214 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008215 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008216 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308217 u64 end;
8218
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008219 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008220
Jan Karab2b5ef52012-06-12 16:20:45 +02008221 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008222 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008223 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308224 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008225
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308226 /*
8227 * Reserving delalloc space after obtaining the page lock can lead to
8228 * deadlock. For example, if a dirty page is locked by this function
8229 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8230 * dirty page write out, then the btrfs_writepage() function could
8231 * end up waiting indefinitely to get a lock on the page currently
8232 * being processed by btrfs_page_mkwrite() function.
8233 */
Souptick Joardera528a242018-06-06 19:54:44 +05308234 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308235 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308236 if (!ret2) {
8237 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008238 reserved = 1;
8239 }
Souptick Joardera528a242018-06-06 19:54:44 +05308240 if (ret2) {
8241 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008242 if (reserved)
8243 goto out;
8244 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008245 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008246
Nick Piggin56a76f82009-03-31 15:23:23 -07008247 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008248again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008249 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008250 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008251
Chris Mason9ebefb182007-06-15 13:50:00 -04008252 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008253 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008254 /* page got truncated out from underneath us */
8255 goto out_unlock;
8256 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008257 wait_on_page_writeback(page);
8258
David Sterbaff13db42015-12-03 14:30:40 +01008259 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008260 set_page_extent_mapped(page);
8261
Chris Masoneb84ae02008-07-17 13:53:27 -04008262 /*
8263 * we can't set the delalloc bits if there are pending ordered
8264 * extents. Drop our locks and wait for them to finish
8265 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008266 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8267 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008268 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008269 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008270 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008271 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008272 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008273 btrfs_put_ordered_extent(ordered);
8274 goto again;
8275 }
8276
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008277 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008278 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008279 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008280 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308281 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008282 btrfs_delalloc_release_space(BTRFS_I(inode),
8283 data_reserved, page_start,
8284 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308285 }
8286 }
8287
Josef Bacikfbf19082009-10-01 17:10:23 -04008288 /*
Liu Bo54160342016-12-13 12:15:19 -08008289 * page_mkwrite gets called when the page is firstly dirtied after it's
8290 * faulted in, but write(2) could also dirty a page and set delalloc
8291 * bits, thus in this case for space account reason, we still need to
8292 * clear any delalloc bits within this page range since we have to
8293 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008294 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308295 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008296 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8297 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008298
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008299 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008300 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308301 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008302 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008303 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008304 ret = VM_FAULT_SIGBUS;
8305 goto out_unlock;
8306 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008307
8308 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008309 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008310 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008311 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008312 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008313
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008314 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008315 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008316 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008317 flush_dcache_page(page);
8318 kunmap(page);
8319 }
Chris Mason247e7432008-07-17 12:53:51 -04008320 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008321 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008322 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008323
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008324 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008325 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008326 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008327
David Sterbae43bbe52017-12-12 21:43:52 +01008328 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008329
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008330 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8331 sb_end_pagefault(inode->i_sb);
8332 extent_changeset_free(data_reserved);
8333 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008334
8335out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008336 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008337out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008338 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008339 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008340 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008341out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008342 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008343 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008344 return ret;
8345}
8346
Filipe Manana213e8c52018-02-06 20:40:31 +00008347static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008348{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008349 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008350 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008351 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008352 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008353 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008354 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008355 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008356
Filipe Manana213e8c52018-02-06 20:40:31 +00008357 if (!skip_writeback) {
8358 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8359 (u64)-1);
8360 if (ret)
8361 return ret;
8362 }
Chris Mason39279cc2007-06-12 06:35:45 -04008363
Josef Bacikfcb80c22011-05-03 10:40:22 -04008364 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008365 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8366 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008367 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008368 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008369 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008370 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008371 * be free'd up by the truncate operation, but also have some slack
8372 * space reserved in case it uses space during the truncate (thank you
8373 * very much snapshotting).
8374 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008375 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008376 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008377 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008378 * doesn't end up using space reserved for updating the inode. We also
8379 * need to be able to stop the transaction and start a new one, which
8380 * means we need to be able to update the inode several times, and we
8381 * have no idea of knowing how many times that will be, so we can't just
8382 * reserve 1 item for the entirety of the operation, so that has to be
8383 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008384 *
8385 * So that leaves us with
8386 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008387 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008388 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008389 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008390 * updating the inode.
8391 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008392 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008393 if (!rsv)
8394 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008395 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008396 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008397
Josef Bacik907cbce2011-08-08 13:46:15 -04008398 /*
Josef Bacik07127182011-08-19 10:29:59 -04008399 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008400 * 1 for updating the inode.
8401 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008402 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008403 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008404 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008405 goto out;
8406 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008407
Josef Bacik907cbce2011-08-08 13:46:15 -04008408 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008409 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008410 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008411 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008412
Chris Mason5a3f23d2009-03-31 13:27:11 -04008413 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008414 * So if we truncate and then write and fsync we normally would just
8415 * write the extents that changed, which is a problem if we need to
8416 * first truncate that entire inode. So set this flag so we write out
8417 * all of the extents in the inode to the sync log so we're completely
8418 * safe.
8419 */
8420 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008421 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008422
Yan, Zheng80825102009-11-12 09:35:36 +00008423 while (1) {
8424 ret = btrfs_truncate_inode_items(trans, root, inode,
8425 inode->i_size,
8426 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008427 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008428 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008429 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008430
Yan, Zheng80825102009-11-12 09:35:36 +00008431 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008432 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008433 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008434
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008435 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008436 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008437
8438 trans = btrfs_start_transaction(root, 2);
8439 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008440 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008441 trans = NULL;
8442 break;
8443 }
8444
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008445 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008446 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008447 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008448 BUG_ON(ret); /* shouldn't happen */
8449 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008450 }
8451
Josef Bacikddfae632017-10-19 14:16:02 -04008452 /*
8453 * We can't call btrfs_truncate_block inside a trans handle as we could
8454 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8455 * we've truncated everything except the last little bit, and can do
8456 * btrfs_truncate_block and then update the disk_i_size.
8457 */
8458 if (ret == NEED_TRUNCATE_BLOCK) {
8459 btrfs_end_transaction(trans);
8460 btrfs_btree_balance_dirty(fs_info);
8461
8462 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8463 if (ret)
8464 goto out;
8465 trans = btrfs_start_transaction(root, 1);
8466 if (IS_ERR(trans)) {
8467 ret = PTR_ERR(trans);
8468 goto out;
8469 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008470 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008471 }
8472
Chris Mason917c16b2011-11-08 14:49:59 -05008473 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008474 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008475
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008476 trans->block_rsv = &fs_info->trans_block_rsv;
8477 ret2 = btrfs_update_inode(trans, root, inode);
8478 if (ret2 && !ret)
8479 ret = ret2;
8480
8481 ret2 = btrfs_end_transaction(trans);
8482 if (ret2 && !ret)
8483 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008484 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008485 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008486out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008487 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008488
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008489 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008490}
8491
Sven Wegener3b963622008-06-09 21:57:42 -04008492/*
Chris Masond352ac62008-09-29 15:18:18 -04008493 * create a new subvolume directory/inode (helper for the ioctl).
8494 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008495int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008496 struct btrfs_root *new_root,
8497 struct btrfs_root *parent_root,
8498 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008499{
Chris Mason39279cc2007-06-12 06:35:45 -04008500 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008501 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008502 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008503
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008504 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8505 new_dirid, new_dirid,
8506 S_IFDIR | (~current_umask() & S_IRWXUGO),
8507 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008508 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008509 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008510 inode->i_op = &btrfs_dir_inode_operations;
8511 inode->i_fop = &btrfs_dir_file_operations;
8512
Miklos Szeredibfe86842011-10-28 14:13:29 +02008513 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008514 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008515 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008516
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008517 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8518 if (err)
8519 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008520 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008521 new_root->root_key.objectid, err);
8522
Yan, Zheng76dda932009-09-21 16:00:26 -04008523 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008524
Yan, Zheng76dda932009-09-21 16:00:26 -04008525 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008526 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008527}
8528
Chris Mason39279cc2007-06-12 06:35:45 -04008529struct inode *btrfs_alloc_inode(struct super_block *sb)
8530{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008531 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008532 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008533 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008534
David Sterba712e36c2017-10-31 17:08:27 +01008535 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008536 if (!ei)
8537 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008538
8539 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008540 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008541 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008542 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008543 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008544 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008545 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008546 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008547 ei->disk_i_size = 0;
8548 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008549 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008550 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008551 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008552 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008553 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008554
Josef Bacik9e0baf62011-07-15 15:16:44 +00008555 spin_lock_init(&ei->lock);
8556 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008557 if (sb->s_magic != BTRFS_TEST_MAGIC)
8558 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8559 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008560 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008561 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008562 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008563
Miao Xie16cdcec2011-04-22 18:12:22 +08008564 ei->delayed_node = NULL;
8565
chandan r9cc97d62012-07-04 12:48:07 +05308566 ei->i_otime.tv_sec = 0;
8567 ei->i_otime.tv_nsec = 0;
8568
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008569 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008570 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008571 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8572 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8573 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008574 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8575 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008576 ei->io_tree.track_uptodate = true;
8577 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008578 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008579 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008580 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008581 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008582 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008583 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008584 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008585
8586 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008587}
8588
Josef Bacikaaedb552013-10-11 14:44:09 -04008589#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8590void btrfs_test_destroy_inode(struct inode *inode)
8591{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008592 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008593 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8594}
8595#endif
8596
Al Viro26602ca2019-04-10 15:14:41 -04008597void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008598{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008599 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8600}
8601
Chris Mason39279cc2007-06-12 06:35:45 -04008602void btrfs_destroy_inode(struct inode *inode)
8603{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008604 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008605 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008606 struct btrfs_root *root = BTRFS_I(inode)->root;
8607
Al Virob3d9b7a2012-06-09 13:51:19 -04008608 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008609 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008610 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8611 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008612 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008613 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008614 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008615 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008616 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008617
Chris Mason5a3f23d2009-03-31 13:27:11 -04008618 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008619 * This can happen where we create an inode, but somebody else also
8620 * created the same inode and we need to destroy the one we already
8621 * created.
8622 */
8623 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008624 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008625
Chris Masond3977122009-01-05 21:25:51 -05008626 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008627 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8628 if (!ordered)
8629 break;
8630 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008631 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008632 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008633 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008634 btrfs_remove_ordered_extent(inode, ordered);
8635 btrfs_put_ordered_extent(ordered);
8636 btrfs_put_ordered_extent(ordered);
8637 }
8638 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008639 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008640 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008641 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008642 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008643 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008644}
8645
Al Viro45321ac2010-06-07 13:43:19 -04008646int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008647{
8648 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008649
Naohiro Aota6379ef92013-06-06 09:56:34 +00008650 if (root == NULL)
8651 return 1;
8652
Liu Bofa6ac872013-02-20 14:10:23 +00008653 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008654 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008655 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008656 else
Al Viro45321ac2010-06-07 13:43:19 -04008657 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008658}
8659
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008660static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008661{
8662 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8663
8664 inode_init_once(&ei->vfs_inode);
8665}
8666
David Sterbae67c7182018-02-19 17:24:18 +01008667void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008668{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008669 /*
8670 * Make sure all delayed rcu free inodes are flushed before we
8671 * destroy cache.
8672 */
8673 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008674 kmem_cache_destroy(btrfs_inode_cachep);
8675 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008676 kmem_cache_destroy(btrfs_path_cachep);
8677 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008678 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008679}
8680
Liu Bof5c29bd2017-11-02 17:21:50 -06008681int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008682{
David Sterba837e1972012-09-07 03:00:48 -06008683 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008684 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008685 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8686 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008687 if (!btrfs_inode_cachep)
8688 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008689
David Sterba837e1972012-09-07 03:00:48 -06008690 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008691 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008692 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008693 if (!btrfs_trans_handle_cachep)
8694 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008695
David Sterba837e1972012-09-07 03:00:48 -06008696 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008697 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008698 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008699 if (!btrfs_path_cachep)
8700 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008701
David Sterba837e1972012-09-07 03:00:48 -06008702 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008703 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008704 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008705 if (!btrfs_free_space_cachep)
8706 goto fail;
8707
Christophe Leroy3acd4852019-08-21 15:05:55 +00008708 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8709 PAGE_SIZE, PAGE_SIZE,
8710 SLAB_RED_ZONE, NULL);
8711 if (!btrfs_free_space_bitmap_cachep)
8712 goto fail;
8713
Chris Mason39279cc2007-06-12 06:35:45 -04008714 return 0;
8715fail:
8716 btrfs_destroy_cachep();
8717 return -ENOMEM;
8718}
8719
David Howellsa528d352017-01-31 16:46:22 +00008720static int btrfs_getattr(const struct path *path, struct kstat *stat,
8721 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008722{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008723 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008724 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008725 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008726 u32 bi_flags = BTRFS_I(inode)->flags;
8727
8728 stat->result_mask |= STATX_BTIME;
8729 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8730 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8731 if (bi_flags & BTRFS_INODE_APPEND)
8732 stat->attributes |= STATX_ATTR_APPEND;
8733 if (bi_flags & BTRFS_INODE_COMPRESS)
8734 stat->attributes |= STATX_ATTR_COMPRESSED;
8735 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8736 stat->attributes |= STATX_ATTR_IMMUTABLE;
8737 if (bi_flags & BTRFS_INODE_NODUMP)
8738 stat->attributes |= STATX_ATTR_NODUMP;
8739
8740 stat->attributes_mask |= (STATX_ATTR_APPEND |
8741 STATX_ATTR_COMPRESSED |
8742 STATX_ATTR_IMMUTABLE |
8743 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008744
Chris Mason39279cc2007-06-12 06:35:45 -04008745 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008746 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008747
8748 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008749 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008750 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008751 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008752 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008753 return 0;
8754}
8755
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008756static int btrfs_rename_exchange(struct inode *old_dir,
8757 struct dentry *old_dentry,
8758 struct inode *new_dir,
8759 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008760{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008761 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008762 struct btrfs_trans_handle *trans;
8763 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008764 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008765 struct inode *new_inode = new_dentry->d_inode;
8766 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008767 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008768 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008769 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8770 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008771 u64 old_idx = 0;
8772 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008773 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008774 bool root_log_pinned = false;
8775 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008776 struct btrfs_log_ctx ctx_root;
8777 struct btrfs_log_ctx ctx_dest;
8778 bool sync_log_root = false;
8779 bool sync_log_dest = false;
8780 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008781
8782 /* we only allow rename subvolume link between subvolumes */
8783 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8784 return -EXDEV;
8785
Filipe Mananad4682ba2018-06-11 19:24:28 +01008786 btrfs_init_log_ctx(&ctx_root, old_inode);
8787 btrfs_init_log_ctx(&ctx_dest, new_inode);
8788
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008789 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008790 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8791 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008792 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008793
8794 /*
8795 * We want to reserve the absolute worst case amount of items. So if
8796 * both inodes are subvols and we need to unlink them then that would
8797 * require 4 item modifications, but if they are both normal inodes it
8798 * would require 5 item modifications, so we'll assume their normal
8799 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8800 * should cover the worst case number of items we'll modify.
8801 */
8802 trans = btrfs_start_transaction(root, 12);
8803 if (IS_ERR(trans)) {
8804 ret = PTR_ERR(trans);
8805 goto out_notrans;
8806 }
8807
Josef Bacik3e174092019-11-15 15:43:06 -05008808 if (dest != root)
8809 btrfs_record_root_in_trans(trans, dest);
8810
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008811 /*
8812 * We need to find a free sequence number both in the source and
8813 * in the destination directory for the exchange.
8814 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008815 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008816 if (ret)
8817 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008818 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008819 if (ret)
8820 goto out_fail;
8821
8822 BTRFS_I(old_inode)->dir_index = 0ULL;
8823 BTRFS_I(new_inode)->dir_index = 0ULL;
8824
8825 /* Reference for the source. */
8826 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8827 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008828 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008829 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008830 btrfs_pin_log_trans(root);
8831 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008832 ret = btrfs_insert_inode_ref(trans, dest,
8833 new_dentry->d_name.name,
8834 new_dentry->d_name.len,
8835 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008836 btrfs_ino(BTRFS_I(new_dir)),
8837 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008838 if (ret)
8839 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008840 }
8841
8842 /* And now for the dest. */
8843 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8844 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008845 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008846 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008847 btrfs_pin_log_trans(dest);
8848 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008849 ret = btrfs_insert_inode_ref(trans, root,
8850 old_dentry->d_name.name,
8851 old_dentry->d_name.len,
8852 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008853 btrfs_ino(BTRFS_I(old_dir)),
8854 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008855 if (ret)
8856 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008857 }
8858
8859 /* Update inode version and ctime/mtime. */
8860 inode_inc_iversion(old_dir);
8861 inode_inc_iversion(new_dir);
8862 inode_inc_iversion(old_inode);
8863 inode_inc_iversion(new_inode);
8864 old_dir->i_ctime = old_dir->i_mtime = ctime;
8865 new_dir->i_ctime = new_dir->i_mtime = ctime;
8866 old_inode->i_ctime = ctime;
8867 new_inode->i_ctime = ctime;
8868
8869 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008870 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8871 BTRFS_I(old_inode), 1);
8872 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8873 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008874 }
8875
8876 /* src is a subvolume */
8877 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008878 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008879 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008880 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8881 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008882 old_dentry->d_name.name,
8883 old_dentry->d_name.len);
8884 if (!ret)
8885 ret = btrfs_update_inode(trans, root, old_inode);
8886 }
8887 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008888 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008889 goto out_fail;
8890 }
8891
8892 /* dest is a subvolume */
8893 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008894 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008895 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008896 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8897 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008898 new_dentry->d_name.name,
8899 new_dentry->d_name.len);
8900 if (!ret)
8901 ret = btrfs_update_inode(trans, dest, new_inode);
8902 }
8903 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008904 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008905 goto out_fail;
8906 }
8907
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008908 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008909 new_dentry->d_name.name,
8910 new_dentry->d_name.len, 0, old_idx);
8911 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008912 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008913 goto out_fail;
8914 }
8915
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008916 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008917 old_dentry->d_name.name,
8918 old_dentry->d_name.len, 0, new_idx);
8919 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008920 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008921 goto out_fail;
8922 }
8923
8924 if (old_inode->i_nlink == 1)
8925 BTRFS_I(old_inode)->dir_index = old_idx;
8926 if (new_inode->i_nlink == 1)
8927 BTRFS_I(new_inode)->dir_index = new_idx;
8928
Filipe Manana86e8aa02016-05-05 02:02:27 +01008929 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008930 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008931 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8932 BTRFS_I(old_dir), parent,
8933 false, &ctx_root);
8934 if (ret == BTRFS_NEED_LOG_SYNC)
8935 sync_log_root = true;
8936 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8937 commit_transaction = true;
8938 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008939 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008940 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008941 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008942 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008943 if (!commit_transaction) {
8944 parent = old_dentry->d_parent;
8945 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8946 BTRFS_I(new_dir), parent,
8947 false, &ctx_dest);
8948 if (ret == BTRFS_NEED_LOG_SYNC)
8949 sync_log_dest = true;
8950 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8951 commit_transaction = true;
8952 ret = 0;
8953 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008954 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008955 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008956 }
8957out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008958 /*
8959 * If we have pinned a log and an error happened, we unpin tasks
8960 * trying to sync the log and force them to fallback to a transaction
8961 * commit if the log currently contains any of the inodes involved in
8962 * this rename operation (to ensure we do not persist a log with an
8963 * inconsistent state for any of these inodes or leading to any
8964 * inconsistencies when replayed). If the transaction was aborted, the
8965 * abortion reason is propagated to userspace when attempting to commit
8966 * the transaction. If the log does not contain any of these inodes, we
8967 * allow the tasks to sync it.
8968 */
8969 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008970 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8971 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8972 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008973 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008974 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008975 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008976
8977 if (root_log_pinned) {
8978 btrfs_end_log_trans(root);
8979 root_log_pinned = false;
8980 }
8981 if (dest_log_pinned) {
8982 btrfs_end_log_trans(dest);
8983 dest_log_pinned = false;
8984 }
8985 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008986 if (!ret && sync_log_root && !commit_transaction) {
8987 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
8988 &ctx_root);
8989 if (ret)
8990 commit_transaction = true;
8991 }
8992 if (!ret && sync_log_dest && !commit_transaction) {
8993 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
8994 &ctx_dest);
8995 if (ret)
8996 commit_transaction = true;
8997 }
8998 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00008999 /*
9000 * We may have set commit_transaction when logging the new name
9001 * in the destination root, in which case we left the source
9002 * root context in the list of log contextes. So make sure we
9003 * remove it to avoid invalid memory accesses, since the context
9004 * was allocated in our stack frame.
9005 */
9006 if (sync_log_root) {
9007 mutex_lock(&root->log_mutex);
9008 list_del_init(&ctx_root.list);
9009 mutex_unlock(&root->log_mutex);
9010 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009011 ret = btrfs_commit_transaction(trans);
9012 } else {
9013 int ret2;
9014
9015 ret2 = btrfs_end_transaction(trans);
9016 ret = ret ? ret : ret2;
9017 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009018out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009019 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9020 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009021 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009022
Filipe Mananae6c61712019-11-08 16:11:56 +00009023 ASSERT(list_empty(&ctx_root.list));
9024 ASSERT(list_empty(&ctx_dest.list));
9025
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009026 return ret;
9027}
9028
9029static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9030 struct btrfs_root *root,
9031 struct inode *dir,
9032 struct dentry *dentry)
9033{
9034 int ret;
9035 struct inode *inode;
9036 u64 objectid;
9037 u64 index;
9038
9039 ret = btrfs_find_free_ino(root, &objectid);
9040 if (ret)
9041 return ret;
9042
9043 inode = btrfs_new_inode(trans, root, dir,
9044 dentry->d_name.name,
9045 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009046 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009047 objectid,
9048 S_IFCHR | WHITEOUT_MODE,
9049 &index);
9050
9051 if (IS_ERR(inode)) {
9052 ret = PTR_ERR(inode);
9053 return ret;
9054 }
9055
9056 inode->i_op = &btrfs_special_inode_operations;
9057 init_special_inode(inode, inode->i_mode,
9058 WHITEOUT_DEV);
9059
9060 ret = btrfs_init_inode_security(trans, inode, dir,
9061 &dentry->d_name);
9062 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009063 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009064
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009065 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9066 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009067 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009068 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009069
9070 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009071out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009072 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009073 if (ret)
9074 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009075 iput(inode);
9076
Filipe Mananac9901612016-05-05 01:41:57 +01009077 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009078}
9079
Chris Mason39279cc2007-06-12 06:35:45 -04009080static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009081 struct inode *new_dir, struct dentry *new_dentry,
9082 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009083{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009084 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009085 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009086 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009087 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9088 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009089 struct inode *new_inode = d_inode(new_dentry);
9090 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009091 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009092 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009093 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009094 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009095 struct btrfs_log_ctx ctx;
9096 bool sync_log = false;
9097 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009098
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009099 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009100 return -EPERM;
9101
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009102 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009103 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009104 return -EXDEV;
9105
Li Zefan33345d012011-04-20 10:31:50 +08009106 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009107 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009108 return -ENOTEMPTY;
9109
Chris Mason39279cc2007-06-12 06:35:45 -04009110 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009111 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009112 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009113
9114
9115 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009116 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009117 new_dentry->d_name.name,
9118 new_dentry->d_name.len);
9119
9120 if (ret) {
9121 if (ret == -EEXIST) {
9122 /* we shouldn't get
9123 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309124 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009125 return ret;
9126 }
9127 } else {
9128 /* maybe -EOVERFLOW */
9129 return ret;
9130 }
9131 }
9132 ret = 0;
9133
Chris Mason5a3f23d2009-03-31 13:27:11 -04009134 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009135 * we're using rename to replace one file with another. Start IO on it
9136 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009137 */
Chris Mason8d875f92014-08-12 10:47:42 -07009138 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009139 filemap_flush(old_inode->i_mapping);
9140
Yan, Zheng76dda932009-09-21 16:00:26 -04009141 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009142 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009143 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009144 /*
9145 * We want to reserve the absolute worst case amount of items. So if
9146 * both inodes are subvols and we need to unlink them then that would
9147 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009148 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009149 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9150 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009151 * If our rename has the whiteout flag, we need more 5 units for the
9152 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9153 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009154 */
Filipe Manana5062af32016-05-05 10:26:26 +01009155 trans_num_items = 11;
9156 if (flags & RENAME_WHITEOUT)
9157 trans_num_items += 5;
9158 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009159 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009160 ret = PTR_ERR(trans);
9161 goto out_notrans;
9162 }
Chris Mason5f39d392007-10-15 16:14:19 -04009163
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009164 if (dest != root)
9165 btrfs_record_root_in_trans(trans, dest);
9166
Nikolay Borisov877574e2017-02-20 13:50:33 +02009167 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009168 if (ret)
9169 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009170
Miao Xie67de1172013-12-26 13:07:06 +08009171 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009172 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009173 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009174 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009175 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009176 btrfs_pin_log_trans(root);
9177 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009178 ret = btrfs_insert_inode_ref(trans, dest,
9179 new_dentry->d_name.name,
9180 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009181 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009182 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009183 if (ret)
9184 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009185 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009186
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009187 inode_inc_iversion(old_dir);
9188 inode_inc_iversion(new_dir);
9189 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009190 old_dir->i_ctime = old_dir->i_mtime =
9191 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009192 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009193
Chris Mason12fcfd22009-03-24 10:24:20 -04009194 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009195 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9196 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009197
Li Zefan33345d012011-04-20 10:31:50 +08009198 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009199 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009200 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009201 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9202 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009203 old_dentry->d_name.name,
9204 old_dentry->d_name.len);
9205 if (!ret)
9206 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009207 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009208 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009209 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009210 goto out_fail;
9211 }
Chris Mason39279cc2007-06-12 06:35:45 -04009212
9213 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009214 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009215 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009216 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009217 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009218 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009219 BUG_ON(new_inode->i_nlink == 0);
9220 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009221 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9222 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009223 new_dentry->d_name.name,
9224 new_dentry->d_name.len);
9225 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009226 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009227 ret = btrfs_orphan_add(trans,
9228 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009229 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009230 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009231 goto out_fail;
9232 }
Chris Mason39279cc2007-06-12 06:35:45 -04009233 }
Josef Bacikaec74772008-07-24 12:12:38 -04009234
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009235 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009236 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009237 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009238 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009239 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009240 goto out_fail;
9241 }
Chris Mason39279cc2007-06-12 06:35:45 -04009242
Miao Xie67de1172013-12-26 13:07:06 +08009243 if (old_inode->i_nlink == 1)
9244 BTRFS_I(old_inode)->dir_index = index;
9245
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009246 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009247 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009248
Filipe Mananad4682ba2018-06-11 19:24:28 +01009249 btrfs_init_log_ctx(&ctx, old_inode);
9250 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9251 BTRFS_I(old_dir), parent,
9252 false, &ctx);
9253 if (ret == BTRFS_NEED_LOG_SYNC)
9254 sync_log = true;
9255 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9256 commit_transaction = true;
9257 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009258 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009259 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009260 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009261
9262 if (flags & RENAME_WHITEOUT) {
9263 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9264 old_dentry);
9265
9266 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009267 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009268 goto out_fail;
9269 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009270 }
Chris Mason39279cc2007-06-12 06:35:45 -04009271out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009272 /*
9273 * If we have pinned the log and an error happened, we unpin tasks
9274 * trying to sync the log and force them to fallback to a transaction
9275 * commit if the log currently contains any of the inodes involved in
9276 * this rename operation (to ensure we do not persist a log with an
9277 * inconsistent state for any of these inodes or leading to any
9278 * inconsistencies when replayed). If the transaction was aborted, the
9279 * abortion reason is propagated to userspace when attempting to commit
9280 * the transaction. If the log does not contain any of these inodes, we
9281 * allow the tasks to sync it.
9282 */
9283 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009284 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9285 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9286 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009287 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009288 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009289 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009290
9291 btrfs_end_log_trans(root);
9292 log_pinned = false;
9293 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009294 if (!ret && sync_log) {
9295 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9296 if (ret)
9297 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009298 } else if (sync_log) {
9299 mutex_lock(&root->log_mutex);
9300 list_del(&ctx.list);
9301 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009302 }
9303 if (commit_transaction) {
9304 ret = btrfs_commit_transaction(trans);
9305 } else {
9306 int ret2;
9307
9308 ret2 = btrfs_end_transaction(trans);
9309 ret = ret ? ret : ret2;
9310 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009311out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009312 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009313 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009314
Chris Mason39279cc2007-06-12 06:35:45 -04009315 return ret;
9316}
9317
Miklos Szeredi80ace852014-07-23 15:15:32 +02009318static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9319 struct inode *new_dir, struct dentry *new_dentry,
9320 unsigned int flags)
9321{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009322 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009323 return -EINVAL;
9324
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009325 if (flags & RENAME_EXCHANGE)
9326 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9327 new_dentry);
9328
9329 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009330}
9331
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009332struct btrfs_delalloc_work {
9333 struct inode *inode;
9334 struct completion completion;
9335 struct list_head list;
9336 struct btrfs_work work;
9337};
9338
Miao Xie8ccf6f192012-10-25 09:28:04 +00009339static void btrfs_run_delalloc_work(struct btrfs_work *work)
9340{
9341 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009342 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009343
9344 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9345 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009346 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009347 filemap_flush(inode->i_mapping);
9348 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9349 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009350 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009351
Nikolay Borisov076da912018-04-23 10:54:16 +03009352 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009353 complete(&delalloc_work->completion);
9354}
9355
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009356static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009357{
9358 struct btrfs_delalloc_work *work;
9359
David Sterba100d5702015-12-08 14:39:32 +01009360 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009361 if (!work)
9362 return NULL;
9363
9364 init_completion(&work->completion);
9365 INIT_LIST_HEAD(&work->list);
9366 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009367 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009368
9369 return work;
9370}
9371
Chris Masond352ac62008-09-29 15:18:18 -04009372/*
9373 * some fairly slow code that needs optimization. This walks the list
9374 * of all the inodes with pending delalloc and forces them to disk.
9375 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009376static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009377{
Chris Masonea8c2812008-08-04 23:17:27 -04009378 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009379 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009380 struct btrfs_delalloc_work *work, *next;
9381 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009382 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009383 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009384
Miao Xie8ccf6f192012-10-25 09:28:04 +00009385 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009386 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009387
Miao Xie573bfb72014-03-06 13:55:03 +08009388 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009389 spin_lock(&root->delalloc_lock);
9390 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009391 while (!list_empty(&splice)) {
9392 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009393 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009394
Miao Xieeb73c1b2013-05-15 07:48:22 +00009395 list_move_tail(&binode->delalloc_inodes,
9396 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009397 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009398 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009399 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009400 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009401 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009402 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009403
Ethan Lien3cd24c62018-11-01 14:49:03 +08009404 if (snapshot)
9405 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9406 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009407 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009408 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009409 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009410 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009411 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009412 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009413 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009414 btrfs_queue_work(root->fs_info->flush_workers,
9415 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009416 ret++;
9417 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009418 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009419 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009420 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009421 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009422 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009423
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009424out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009425 list_for_each_entry_safe(work, next, &works, list) {
9426 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009427 wait_for_completion(&work->completion);
9428 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009429 }
9430
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009431 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009432 spin_lock(&root->delalloc_lock);
9433 list_splice_tail(&splice, &root->delalloc_inodes);
9434 spin_unlock(&root->delalloc_lock);
9435 }
Miao Xie573bfb72014-03-06 13:55:03 +08009436 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009437 return ret;
9438}
9439
Ethan Lien3cd24c62018-11-01 14:49:03 +08009440int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009441{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009442 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009443 int ret;
9444
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009445 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009446 return -EROFS;
9447
Ethan Lien3cd24c62018-11-01 14:49:03 +08009448 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009449 if (ret > 0)
9450 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009451 return ret;
9452}
9453
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009454int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009455{
9456 struct btrfs_root *root;
9457 struct list_head splice;
9458 int ret;
9459
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009460 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009461 return -EROFS;
9462
9463 INIT_LIST_HEAD(&splice);
9464
Miao Xie573bfb72014-03-06 13:55:03 +08009465 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009466 spin_lock(&fs_info->delalloc_root_lock);
9467 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009468 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009469 root = list_first_entry(&splice, struct btrfs_root,
9470 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009471 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009472 BUG_ON(!root);
9473 list_move_tail(&root->delalloc_root,
9474 &fs_info->delalloc_roots);
9475 spin_unlock(&fs_info->delalloc_root_lock);
9476
Ethan Lien3cd24c62018-11-01 14:49:03 +08009477 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009478 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009479 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009480 goto out;
9481
Miao Xie6c255e62014-03-06 13:55:01 +08009482 if (nr != -1) {
9483 nr -= ret;
9484 WARN_ON(nr < 0);
9485 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009486 spin_lock(&fs_info->delalloc_root_lock);
9487 }
9488 spin_unlock(&fs_info->delalloc_root_lock);
9489
Miao Xie6c255e62014-03-06 13:55:01 +08009490 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009491out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009492 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009493 spin_lock(&fs_info->delalloc_root_lock);
9494 list_splice_tail(&splice, &fs_info->delalloc_roots);
9495 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009496 }
Miao Xie573bfb72014-03-06 13:55:03 +08009497 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009498 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009499}
9500
Chris Mason39279cc2007-06-12 06:35:45 -04009501static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9502 const char *symname)
9503{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009504 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009505 struct btrfs_trans_handle *trans;
9506 struct btrfs_root *root = BTRFS_I(dir)->root;
9507 struct btrfs_path *path;
9508 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009509 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009510 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009511 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309512 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009513 int name_len;
9514 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009515 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009516 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009517 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009518
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009519 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009520 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009521 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009522
Josef Bacik9ed74f22009-09-11 16:12:44 -04009523 /*
9524 * 2 items for inode item and ref
9525 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009526 * 1 item for updating parent inode item
9527 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009528 * 1 item for xattr if selinux is on
9529 */
Filipe Manana9269d122015-12-31 18:16:29 +00009530 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009531 if (IS_ERR(trans))
9532 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009533
Li Zefan581bb052011-04-20 10:06:11 +08009534 err = btrfs_find_free_ino(root, &objectid);
9535 if (err)
9536 goto out_unlock;
9537
Josef Bacikaec74772008-07-24 12:12:38 -04009538 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009539 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9540 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009541 if (IS_ERR(inode)) {
9542 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009543 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009544 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009545 }
Chris Mason39279cc2007-06-12 06:35:45 -04009546
Casey Schauflerad19db72011-12-15 10:09:07 -05009547 /*
9548 * If the active LSM wants to access the inode during
9549 * d_instantiate it needs these. Smack checks to see
9550 * if the filesystem supports xattrs by looking at the
9551 * ops vector.
9552 */
9553 inode->i_fop = &btrfs_file_operations;
9554 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009555 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009556 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9557
9558 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9559 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009560 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009561
Chris Mason39279cc2007-06-12 06:35:45 -04009562 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009563 if (!path) {
9564 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009565 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009566 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009567 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009568 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009569 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009570 datasize = btrfs_file_extent_calc_inline_size(name_len);
9571 err = btrfs_insert_empty_item(trans, root, path, &key,
9572 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009573 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009574 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009575 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009576 }
Chris Mason5f39d392007-10-15 16:14:19 -04009577 leaf = path->nodes[0];
9578 ei = btrfs_item_ptr(leaf, path->slots[0],
9579 struct btrfs_file_extent_item);
9580 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9581 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009582 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009583 btrfs_set_file_extent_encryption(leaf, ei, 0);
9584 btrfs_set_file_extent_compression(leaf, ei, 0);
9585 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9586 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9587
Chris Mason39279cc2007-06-12 06:35:45 -04009588 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009589 write_extent_buffer(leaf, symname, ptr, name_len);
9590 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009591 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009592
Chris Mason39279cc2007-06-12 06:35:45 -04009593 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009594 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009595 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009596 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009597 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009598 /*
9599 * Last step, add directory indexes for our symlink inode. This is the
9600 * last step to avoid extra cleanup of these indexes if an error happens
9601 * elsewhere above.
9602 */
9603 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009604 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9605 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009606 if (err)
9607 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009608
Al Viro1e2e5472018-05-04 08:23:01 -04009609 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009610
9611out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009612 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009613 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009614 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009615 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009616 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009617 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009618 return err;
9619}
Chris Mason16432982008-04-10 10:23:21 -04009620
Qu Wenruo203f44c52020-06-10 09:04:40 +08009621static int insert_prealloc_file_extent(struct btrfs_trans_handle *trans,
9622 struct inode *inode, struct btrfs_key *ins,
9623 u64 file_offset)
9624{
9625 struct btrfs_file_extent_item stack_fi;
9626 u64 start = ins->objectid;
9627 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009628 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009629
9630 memset(&stack_fi, 0, sizeof(stack_fi));
9631
9632 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9633 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9634 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9635 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9636 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9637 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9638 /* Encryption and other encoding is reserved and all 0 */
9639
Nikolay Borisov72b7d152020-06-03 08:55:18 +03009640 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009641 if (ret < 0)
9642 return ret;
Nikolay Borisovc553f942020-06-03 08:55:19 +03009643 return insert_reserved_file_extent(trans, BTRFS_I(inode), file_offset,
Qu Wenruo9729f102020-06-10 09:04:41 +08009644 &stack_fi, ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009645}
Josef Bacik0af3d002010-06-21 14:48:16 -04009646static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9647 u64 start, u64 num_bytes, u64 min_size,
9648 loff_t actual_len, u64 *alloc_hint,
9649 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009651 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009652 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9653 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009654 struct btrfs_root *root = BTRFS_I(inode)->root;
9655 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009656 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009657 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009658 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009659 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009660 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009661 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009662 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009663 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009664
Josef Bacik0af3d002010-06-21 14:48:16 -04009665 if (trans)
9666 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009667 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009668 if (own_trans) {
9669 trans = btrfs_start_transaction(root, 3);
9670 if (IS_ERR(trans)) {
9671 ret = PTR_ERR(trans);
9672 break;
9673 }
Yan Zhengd899e052008-10-30 14:25:28 -04009674 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009675
Byongho Leeee221842015-12-15 01:42:10 +09009676 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009677 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009678 /*
9679 * If we are severely fragmented we could end up with really
9680 * small allocations, so if the allocator is returning small
9681 * chunks lets make its job easier by only searching for those
9682 * sized chunks.
9683 */
9684 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009685 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9686 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009687 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009688 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009689 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009690 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009691 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009692
9693 /*
9694 * We've reserved this space, and thus converted it from
9695 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9696 * from here on out we will only need to clear our reservation
9697 * for the remaining unreserved area, so advance our
9698 * clear_offset by our extent size.
9699 */
9700 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009701 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009702
Josef Bacik0b670dc2015-09-23 17:11:16 -04009703 last_alloc = ins.offset;
Nikolay Borisovc553f942020-06-03 08:55:19 +03009704 ret = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009705 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009706 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009707 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009708 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009709 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009710 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009711 break;
9712 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009713
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009714 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009715 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009716
Josef Bacik5dc562c2012-08-17 13:14:17 -04009717 em = alloc_extent_map();
9718 if (!em) {
9719 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9720 &BTRFS_I(inode)->runtime_flags);
9721 goto next;
9722 }
9723
9724 em->start = cur_offset;
9725 em->orig_start = cur_offset;
9726 em->len = ins.offset;
9727 em->block_start = ins.objectid;
9728 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009729 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009730 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009731 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9732 em->generation = trans->transid;
9733
9734 while (1) {
9735 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009736 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009737 write_unlock(&em_tree->lock);
9738 if (ret != -EEXIST)
9739 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009740 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009741 cur_offset + ins.offset - 1,
9742 0);
9743 }
9744 free_extent_map(em);
9745next:
Yan Zhengd899e052008-10-30 14:25:28 -04009746 num_bytes -= ins.offset;
9747 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009748 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009749
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009750 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009751 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009752 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009753 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009754 (actual_len > inode->i_size) &&
9755 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009756 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009757 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009758 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009759 i_size = cur_offset;
9760 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009761 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009762 }
9763
Yan Zhengd899e052008-10-30 14:25:28 -04009764 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009765
9766 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009767 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009768 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009769 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009770 break;
9771 }
Yan Zhengd899e052008-10-30 14:25:28 -04009772
Josef Bacik0af3d002010-06-21 14:48:16 -04009773 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009774 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009775 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009776 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009777 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009778 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009779 return ret;
9780}
9781
Josef Bacik0af3d002010-06-21 14:48:16 -04009782int btrfs_prealloc_file_range(struct inode *inode, int mode,
9783 u64 start, u64 num_bytes, u64 min_size,
9784 loff_t actual_len, u64 *alloc_hint)
9785{
9786 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9787 min_size, actual_len, alloc_hint,
9788 NULL);
9789}
9790
9791int btrfs_prealloc_file_range_trans(struct inode *inode,
9792 struct btrfs_trans_handle *trans, int mode,
9793 u64 start, u64 num_bytes, u64 min_size,
9794 loff_t actual_len, u64 *alloc_hint)
9795{
9796 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9797 min_size, actual_len, alloc_hint, trans);
9798}
9799
Chris Masone6dcd2d2008-07-17 12:53:50 -04009800static int btrfs_set_page_dirty(struct page *page)
9801{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009802 return __set_page_dirty_nobuffers(page);
9803}
9804
Al Viro10556cb22011-06-20 19:28:19 -04009805static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009806{
Li Zefanb83cc962010-12-20 16:04:08 +08009807 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009808 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009809
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009810 if (mask & MAY_WRITE &&
9811 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9812 if (btrfs_root_readonly(root))
9813 return -EROFS;
9814 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9815 return -EACCES;
9816 }
Al Viro2830ba72011-06-20 19:16:29 -04009817 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009818}
Chris Mason39279cc2007-06-12 06:35:45 -04009819
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009820static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9821{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009822 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009823 struct btrfs_trans_handle *trans;
9824 struct btrfs_root *root = BTRFS_I(dir)->root;
9825 struct inode *inode = NULL;
9826 u64 objectid;
9827 u64 index;
9828 int ret = 0;
9829
9830 /*
9831 * 5 units required for adding orphan entry
9832 */
9833 trans = btrfs_start_transaction(root, 5);
9834 if (IS_ERR(trans))
9835 return PTR_ERR(trans);
9836
9837 ret = btrfs_find_free_ino(root, &objectid);
9838 if (ret)
9839 goto out;
9840
9841 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009842 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009843 if (IS_ERR(inode)) {
9844 ret = PTR_ERR(inode);
9845 inode = NULL;
9846 goto out;
9847 }
9848
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009849 inode->i_fop = &btrfs_file_operations;
9850 inode->i_op = &btrfs_file_inode_operations;
9851
9852 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009853 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9854
Chris Masonb0d5d102014-09-08 13:08:51 -07009855 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9856 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009857 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009858
9859 ret = btrfs_update_inode(trans, root, inode);
9860 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009861 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009862 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009863 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009864 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009865
Filipe Manana5762b5c2014-08-01 00:10:32 +01009866 /*
9867 * We set number of links to 0 in btrfs_new_inode(), and here we set
9868 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9869 * through:
9870 *
9871 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9872 */
9873 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009874 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009875 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009876 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009877out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009878 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009879 if (ret && inode)
9880 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009881 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009882 return ret;
9883}
9884
David Sterba5cdc84b2018-07-18 20:32:52 +02009885void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009886{
David Sterba5cdc84b2018-07-18 20:32:52 +02009887 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009888 unsigned long index = start >> PAGE_SHIFT;
9889 unsigned long end_index = end >> PAGE_SHIFT;
9890 struct page *page;
9891
9892 while (index <= end_index) {
9893 page = find_get_page(inode->i_mapping, index);
9894 ASSERT(page); /* Pages should be in the extent_io_tree */
9895 set_page_writeback(page);
9896 put_page(page);
9897 index++;
9898 }
9899}
9900
Omar Sandovaled46ff32016-11-03 10:28:14 -07009901#ifdef CONFIG_SWAP
9902/*
9903 * Add an entry indicating a block group or device which is pinned by a
9904 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9905 * negative errno on failure.
9906 */
9907static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9908 bool is_block_group)
9909{
9910 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9911 struct btrfs_swapfile_pin *sp, *entry;
9912 struct rb_node **p;
9913 struct rb_node *parent = NULL;
9914
9915 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9916 if (!sp)
9917 return -ENOMEM;
9918 sp->ptr = ptr;
9919 sp->inode = inode;
9920 sp->is_block_group = is_block_group;
9921
9922 spin_lock(&fs_info->swapfile_pins_lock);
9923 p = &fs_info->swapfile_pins.rb_node;
9924 while (*p) {
9925 parent = *p;
9926 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9927 if (sp->ptr < entry->ptr ||
9928 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9929 p = &(*p)->rb_left;
9930 } else if (sp->ptr > entry->ptr ||
9931 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9932 p = &(*p)->rb_right;
9933 } else {
9934 spin_unlock(&fs_info->swapfile_pins_lock);
9935 kfree(sp);
9936 return 1;
9937 }
9938 }
9939 rb_link_node(&sp->node, parent, p);
9940 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9941 spin_unlock(&fs_info->swapfile_pins_lock);
9942 return 0;
9943}
9944
9945/* Free all of the entries pinned by this swapfile. */
9946static void btrfs_free_swapfile_pins(struct inode *inode)
9947{
9948 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9949 struct btrfs_swapfile_pin *sp;
9950 struct rb_node *node, *next;
9951
9952 spin_lock(&fs_info->swapfile_pins_lock);
9953 node = rb_first(&fs_info->swapfile_pins);
9954 while (node) {
9955 next = rb_next(node);
9956 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9957 if (sp->inode == inode) {
9958 rb_erase(&sp->node, &fs_info->swapfile_pins);
9959 if (sp->is_block_group)
9960 btrfs_put_block_group(sp->ptr);
9961 kfree(sp);
9962 }
9963 node = next;
9964 }
9965 spin_unlock(&fs_info->swapfile_pins_lock);
9966}
9967
9968struct btrfs_swap_info {
9969 u64 start;
9970 u64 block_start;
9971 u64 block_len;
9972 u64 lowest_ppage;
9973 u64 highest_ppage;
9974 unsigned long nr_pages;
9975 int nr_extents;
9976};
9977
9978static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9979 struct btrfs_swap_info *bsi)
9980{
9981 unsigned long nr_pages;
9982 u64 first_ppage, first_ppage_reported, next_ppage;
9983 int ret;
9984
9985 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9986 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9987 PAGE_SIZE) >> PAGE_SHIFT;
9988
9989 if (first_ppage >= next_ppage)
9990 return 0;
9991 nr_pages = next_ppage - first_ppage;
9992
9993 first_ppage_reported = first_ppage;
9994 if (bsi->start == 0)
9995 first_ppage_reported++;
9996 if (bsi->lowest_ppage > first_ppage_reported)
9997 bsi->lowest_ppage = first_ppage_reported;
9998 if (bsi->highest_ppage < (next_ppage - 1))
9999 bsi->highest_ppage = next_ppage - 1;
10000
10001 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10002 if (ret < 0)
10003 return ret;
10004 bsi->nr_extents += ret;
10005 bsi->nr_pages += nr_pages;
10006 return 0;
10007}
10008
10009static void btrfs_swap_deactivate(struct file *file)
10010{
10011 struct inode *inode = file_inode(file);
10012
10013 btrfs_free_swapfile_pins(inode);
10014 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10015}
10016
10017static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10018 sector_t *span)
10019{
10020 struct inode *inode = file_inode(file);
10021 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10022 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10023 struct extent_state *cached_state = NULL;
10024 struct extent_map *em = NULL;
10025 struct btrfs_device *device = NULL;
10026 struct btrfs_swap_info bsi = {
10027 .lowest_ppage = (sector_t)-1ULL,
10028 };
10029 int ret = 0;
10030 u64 isize;
10031 u64 start;
10032
10033 /*
10034 * If the swap file was just created, make sure delalloc is done. If the
10035 * file changes again after this, the user is doing something stupid and
10036 * we don't really care.
10037 */
10038 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10039 if (ret)
10040 return ret;
10041
10042 /*
10043 * The inode is locked, so these flags won't change after we check them.
10044 */
10045 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10046 btrfs_warn(fs_info, "swapfile must not be compressed");
10047 return -EINVAL;
10048 }
10049 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10050 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10051 return -EINVAL;
10052 }
10053 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10054 btrfs_warn(fs_info, "swapfile must not be checksummed");
10055 return -EINVAL;
10056 }
10057
10058 /*
10059 * Balance or device remove/replace/resize can move stuff around from
10060 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10061 * concurrently while we are mapping the swap extents, and
10062 * fs_info->swapfile_pins prevents them from running while the swap file
10063 * is active and moving the extents. Note that this also prevents a
10064 * concurrent device add which isn't actually necessary, but it's not
10065 * really worth the trouble to allow it.
10066 */
10067 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10068 btrfs_warn(fs_info,
10069 "cannot activate swapfile while exclusive operation is running");
10070 return -EBUSY;
10071 }
10072 /*
10073 * Snapshots can create extents which require COW even if NODATACOW is
10074 * set. We use this counter to prevent snapshots. We must increment it
10075 * before walking the extents because we don't want a concurrent
10076 * snapshot to run after we've already checked the extents.
10077 */
10078 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10079
10080 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10081
10082 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10083 start = 0;
10084 while (start < isize) {
10085 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010086 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010087 u64 len = isize - start;
10088
Omar Sandoval39b07b52019-12-02 17:34:23 -080010089 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010090 if (IS_ERR(em)) {
10091 ret = PTR_ERR(em);
10092 goto out;
10093 }
10094
10095 if (em->block_start == EXTENT_MAP_HOLE) {
10096 btrfs_warn(fs_info, "swapfile must not have holes");
10097 ret = -EINVAL;
10098 goto out;
10099 }
10100 if (em->block_start == EXTENT_MAP_INLINE) {
10101 /*
10102 * It's unlikely we'll ever actually find ourselves
10103 * here, as a file small enough to fit inline won't be
10104 * big enough to store more than the swap header, but in
10105 * case something changes in the future, let's catch it
10106 * here rather than later.
10107 */
10108 btrfs_warn(fs_info, "swapfile must not be inline");
10109 ret = -EINVAL;
10110 goto out;
10111 }
10112 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10113 btrfs_warn(fs_info, "swapfile must not be compressed");
10114 ret = -EINVAL;
10115 goto out;
10116 }
10117
10118 logical_block_start = em->block_start + (start - em->start);
10119 len = min(len, em->len - (start - em->start));
10120 free_extent_map(em);
10121 em = NULL;
10122
10123 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10124 if (ret < 0) {
10125 goto out;
10126 } else if (ret) {
10127 ret = 0;
10128 } else {
10129 btrfs_warn(fs_info,
10130 "swapfile must not be copy-on-write");
10131 ret = -EINVAL;
10132 goto out;
10133 }
10134
10135 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10136 if (IS_ERR(em)) {
10137 ret = PTR_ERR(em);
10138 goto out;
10139 }
10140
10141 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10142 btrfs_warn(fs_info,
10143 "swapfile must have single data profile");
10144 ret = -EINVAL;
10145 goto out;
10146 }
10147
10148 if (device == NULL) {
10149 device = em->map_lookup->stripes[0].dev;
10150 ret = btrfs_add_swapfile_pin(inode, device, false);
10151 if (ret == 1)
10152 ret = 0;
10153 else if (ret)
10154 goto out;
10155 } else if (device != em->map_lookup->stripes[0].dev) {
10156 btrfs_warn(fs_info, "swapfile must be on one device");
10157 ret = -EINVAL;
10158 goto out;
10159 }
10160
10161 physical_block_start = (em->map_lookup->stripes[0].physical +
10162 (logical_block_start - em->start));
10163 len = min(len, em->len - (logical_block_start - em->start));
10164 free_extent_map(em);
10165 em = NULL;
10166
10167 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10168 if (!bg) {
10169 btrfs_warn(fs_info,
10170 "could not find block group containing swapfile");
10171 ret = -EINVAL;
10172 goto out;
10173 }
10174
10175 ret = btrfs_add_swapfile_pin(inode, bg, true);
10176 if (ret) {
10177 btrfs_put_block_group(bg);
10178 if (ret == 1)
10179 ret = 0;
10180 else
10181 goto out;
10182 }
10183
10184 if (bsi.block_len &&
10185 bsi.block_start + bsi.block_len == physical_block_start) {
10186 bsi.block_len += len;
10187 } else {
10188 if (bsi.block_len) {
10189 ret = btrfs_add_swap_extent(sis, &bsi);
10190 if (ret)
10191 goto out;
10192 }
10193 bsi.start = start;
10194 bsi.block_start = physical_block_start;
10195 bsi.block_len = len;
10196 }
10197
10198 start += len;
10199 }
10200
10201 if (bsi.block_len)
10202 ret = btrfs_add_swap_extent(sis, &bsi);
10203
10204out:
10205 if (!IS_ERR_OR_NULL(em))
10206 free_extent_map(em);
10207
10208 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10209
10210 if (ret)
10211 btrfs_swap_deactivate(file);
10212
10213 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10214
10215 if (ret)
10216 return ret;
10217
10218 if (device)
10219 sis->bdev = device->bdev;
10220 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10221 sis->max = bsi.nr_pages;
10222 sis->pages = bsi.nr_pages - 1;
10223 sis->highest_bit = bsi.nr_pages - 1;
10224 return bsi.nr_extents;
10225}
10226#else
10227static void btrfs_swap_deactivate(struct file *file)
10228{
10229}
10230
10231static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10232 sector_t *span)
10233{
10234 return -EOPNOTSUPP;
10235}
10236#endif
10237
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010238static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010239 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010240 .lookup = btrfs_lookup,
10241 .create = btrfs_create,
10242 .unlink = btrfs_unlink,
10243 .link = btrfs_link,
10244 .mkdir = btrfs_mkdir,
10245 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010246 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010247 .symlink = btrfs_symlink,
10248 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010249 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010250 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010251 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010252 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010253 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010254 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010255 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010256};
Yan, Zheng76dda932009-09-21 16:00:26 -040010257
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010258static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010259 .llseek = generic_file_llseek,
10260 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010261 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010262 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010263 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010264#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010265 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010266#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010267 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010268 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010269};
10270
David Sterba20e55062015-11-19 11:42:28 +010010271static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010272 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010273 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010274 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10275};
10276
Chris Mason35054392009-01-21 13:11:13 -050010277/*
10278 * btrfs doesn't support the bmap operation because swapfiles
10279 * use bmap to make a mapping of extents in the file. They assume
10280 * these extents won't change over the life of the file and they
10281 * use the bmap result to do IO directly to the drive.
10282 *
10283 * the btrfs bmap call would return logical addresses that aren't
10284 * suitable for IO and they also will change frequently as COW
10285 * operations happen. So, swapfile + btrfs == corruption.
10286 *
10287 * For now we're avoiding this by dropping bmap.
10288 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010289static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010290 .readpage = btrfs_readpage,
10291 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010292 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010293 .readahead = btrfs_readahead,
David Sterba55e20bd2020-06-09 19:56:06 +020010294 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010295 .invalidatepage = btrfs_invalidatepage,
10296 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010297#ifdef CONFIG_MIGRATION
10298 .migratepage = btrfs_migratepage,
10299#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010300 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010301 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010302 .swap_activate = btrfs_swap_activate,
10303 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010304};
10305
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010306static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010307 .getattr = btrfs_getattr,
10308 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010309 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010310 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010311 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010312 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010313 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010314 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010315};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010316static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010317 .getattr = btrfs_getattr,
10318 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010319 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010320 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010321 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010322 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010323 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010324};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010325static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010326 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010327 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010328 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010329 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010330 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010331 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010332};
Yan, Zheng76dda932009-09-21 16:00:26 -040010333
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010334const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010335 .d_delete = btrfs_dentry_delete,
10336};