blob: 7aeaa9fe18a6be8a511d307ef1058491714524c4 [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
Qu Wenruo524272602017-03-08 10:25:52 +080093static void __endio_write_update_ordered(struct inode *inode,
94 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 */
107static inline void btrfs_cleanup_ordered_extents(struct 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) {
119 page = find_get_page(inode->i_mapping, index);
120 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
137 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 */
415static inline bool inode_can_compress(struct inode *inode)
416{
417 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW ||
418 BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
419 return false;
420 return true;
421}
422
423/*
424 * Check if the inode needs to be submitted to compression, based on mount
425 * options, defragmentation, properties or heuristics.
426 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300427static inline int inode_need_compress(struct inode *inode, u64 start, u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800428{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400429 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Wang Shilongf79707b2014-07-17 11:44:09 +0800430
Qu Wenruo42c16da2019-07-01 05:12:46 +0000431 if (!inode_can_compress(inode)) {
432 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
433 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
434 btrfs_ino(BTRFS_I(inode)));
435 return 0;
436 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800437 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400438 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800439 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200440 /* defrag ioctl */
441 if (BTRFS_I(inode)->defrag_compress)
442 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800443 /* bad compression ratios */
444 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
445 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400446 if (btrfs_test_opt(fs_info, COMPRESS) ||
Wang Shilongf79707b2014-07-17 11:44:09 +0800447 BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
David Sterbab52aa8c2017-07-17 19:17:20 +0200448 BTRFS_I(inode)->prop_compress)
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300449 return btrfs_compress_heuristic(inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800450 return 0;
451}
452
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200453static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800454 u64 start, u64 end, u64 num_bytes, u64 small_write)
455{
456 /* If this is a small write inside eof, kick off a defrag */
457 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200458 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800459 btrfs_add_inode_defrag(NULL, inode);
460}
461
Chris Masonc8b97812008-10-29 14:49:59 -0400462/*
Chris Mason771ed682008-11-06 22:02:51 -0500463 * we create compressed extents in two phases. The first
464 * phase compresses a range of pages that have already been
465 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400466 *
Chris Mason771ed682008-11-06 22:02:51 -0500467 * This is done inside an ordered work queue, and the compression
468 * is spread across many cpus. The actual IO submission is step
469 * two, and the ordered work queue takes care of making sure that
470 * happens in the same order things were put onto the queue by
471 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400472 *
Chris Mason771ed682008-11-06 22:02:51 -0500473 * If this code finds it can't get good compression, it puts an
474 * entry onto the work queue to write the uncompressed bytes. This
475 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300476 * are written in the same order that the flusher thread sent them
477 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400478 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300479static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400480{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200481 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400482 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400483 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200484 u64 start = async_chunk->start;
485 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400486 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400487 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400488 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400489 struct page **pages = NULL;
490 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400491 unsigned long total_compressed = 0;
492 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400493 int i;
494 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400495 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300496 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400497 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400498
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200499 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
500 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400501
Josef Bacikd98da492019-10-11 09:03:54 -0400502 /*
503 * We need to save i_size before now because it could change in between
504 * us evaluating the size and assigning it. This is because we lock and
505 * unlock the page in truncate and fallocate, and then modify the i_size
506 * later on.
507 *
508 * The barriers are to emulate READ_ONCE, remove that once i_size_read
509 * does that for us.
510 */
511 barrier();
512 i_size = i_size_read(inode);
513 barrier();
514 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400515again:
516 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300517 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100518 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
519 nr_pages = min_t(unsigned long, nr_pages,
520 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400521
Chris Masonf03d9301f2009-02-04 09:31:06 -0500522 /*
523 * we don't want to send crud past the end of i_size through
524 * compression, that's just a waste of CPU time. So, if the
525 * end of the file is before the start of our current
526 * requested range of bytes, we bail out to the uncompressed
527 * cleanup code that can deal with all of this.
528 *
529 * It isn't really the fastest way to fix things, but this is a
530 * very uncommon corner.
531 */
532 if (actual_end <= start)
533 goto cleanup_and_bail_uncompressed;
534
Chris Masonc8b97812008-10-29 14:49:59 -0400535 total_compressed = actual_end - start;
536
Shilong Wang4bcbb332014-10-07 18:44:35 -0400537 /*
538 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400539 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400540 */
541 if (total_compressed <= blocksize &&
542 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
543 goto cleanup_and_bail_uncompressed;
544
David Sterba069eac72017-02-14 19:36:54 +0100545 total_compressed = min_t(unsigned long, total_compressed,
546 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400547 total_in = 0;
548 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400549
Chris Mason771ed682008-11-06 22:02:51 -0500550 /*
551 * we do compression for mount -o compress and when the
552 * inode has not been flagged as nocompress. This flag can
553 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400554 */
Timofey Titovetsc2fcdcd2017-07-17 16:52:58 +0300555 if (inode_need_compress(inode, start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400556 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100557 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800558 if (!pages) {
559 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100560 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800561 goto cont;
562 }
Chris Mason179e29e2007-11-01 11:28:41 -0400563
David Sterbaeec63c62017-07-17 19:41:31 +0200564 if (BTRFS_I(inode)->defrag_compress)
565 compress_type = BTRFS_I(inode)->defrag_compress;
566 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200567 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800568
Chris Mason4adaa612013-03-26 13:07:00 -0400569 /*
570 * we need to call clear_page_dirty_for_io on each
571 * page in the range. Otherwise applications with the file
572 * mmap'd can wander in and change the page contents while
573 * we are compressing them.
574 *
575 * If the compression fails for any reason, we set the pages
576 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300577 *
578 * Note that the remaining part is redirtied, the start pointer
579 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400580 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300581 if (!redirty) {
582 extent_range_clear_dirty_for_io(inode, start, end);
583 redirty = 1;
584 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200585
586 /* Compression level is applied here and only here */
587 ret = btrfs_compress_pages(
588 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800589 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100590 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100591 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800592 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100593 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400594
595 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100596 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100597 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400598 char *kaddr;
599
600 /* zero the tail end of the last page, we might be
601 * sending it down to disk
602 */
603 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800604 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400605 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300606 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800607 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400608 }
609 will_compress = 1;
610 }
611 }
Li Zefan560f7d72011-09-08 10:22:01 +0800612cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400613 if (start == 0) {
614 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300615 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400616 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500617 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400618 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300619 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
620 0, BTRFS_COMPRESS_NONE,
621 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400622 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500623 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300624 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000625 total_compressed,
626 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400627 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100628 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400629 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400630 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
631 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100632 unsigned long page_error_op;
633
Filipe Mananae6eb4312014-10-10 10:45:12 +0100634 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400635
Chris Mason771ed682008-11-06 22:02:51 -0500636 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100637 * inline extent creation worked or returned error,
638 * we don't need to create any more async work items.
639 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400640 *
641 * We use DO_ACCOUNTING here because we need the
642 * delalloc_release_metadata to be done _after_ we drop
643 * our outstanding extent for clearing delalloc for this
644 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500645 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300646 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
647 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300648 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800649 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400650 PAGE_CLEAR_DIRTY |
651 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100652 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400653 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300654
655 for (i = 0; i < nr_pages; i++) {
656 WARN_ON(pages[i]->mapping);
657 put_page(pages[i]);
658 }
659 kfree(pages);
660
661 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400662 }
663 }
664
665 if (will_compress) {
666 /*
667 * we aren't doing an inline extent round the compressed size
668 * up to a block size boundary so the allocator does sane
669 * things
670 */
Qu Wenruofda28322013-02-26 08:10:22 +0000671 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400672
673 /*
674 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300675 * win, compare the page count read with the blocks on disk,
676 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400677 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300678 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300679 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300680 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700681
682 /*
683 * The async work queues will take care of doing actual
684 * allocation on disk for these compressed pages, and
685 * will submit them to the elevator.
686 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200687 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100688 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700689 compress_type);
690
Timofey Titovets11708622017-10-03 18:06:01 +0300691 if (start + total_in < end) {
692 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700693 pages = NULL;
694 cond_resched();
695 goto again;
696 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300697 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400698 }
699 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700700 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400701 /*
702 * the compression code ran but failed to make things smaller,
703 * free any pages it allocated and our page pointer array
704 */
David Sterba4d3a8002017-02-14 19:04:07 +0100705 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400706 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300707 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400708 }
709 kfree(pages);
710 pages = NULL;
711 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100712 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400713
714 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400715 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200716 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500717 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500718 }
Chris Masonc8b97812008-10-29 14:49:59 -0400719 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500720cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700721 /*
722 * No compression, but we still need to write the pages in the file
723 * we've been given so far. redirty the locked page if it corresponds
724 * to our extent and set things up for the async work queue to run
725 * cow_file_range to do the normal delalloc dance.
726 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700727 if (async_chunk->locked_page &&
728 (page_offset(async_chunk->locked_page) >= start &&
729 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200730 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700731 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700732 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700733
734 if (redirty)
735 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200736 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700737 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300738 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500739
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300740 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500741}
Chris Mason771ed682008-11-06 22:02:51 -0500742
Filipe Manana40ae8372014-10-06 22:14:24 +0100743static void free_async_extent_pages(struct async_extent *async_extent)
744{
745 int i;
746
747 if (!async_extent->pages)
748 return;
749
750 for (i = 0; i < async_extent->nr_pages; i++) {
751 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300752 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100753 }
754 kfree(async_extent->pages);
755 async_extent->nr_pages = 0;
756 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500757}
758
759/*
760 * phase two of compressed writeback. This is the ordered portion
761 * of the code, which only gets called in the order the work was
762 * queued. We walk all the async extents created by compress_file_range
763 * and send them down to the disk.
764 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200765static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500766{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300767 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
768 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500769 struct async_extent *async_extent;
770 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500771 struct btrfs_key ins;
772 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300773 struct btrfs_root *root = inode->root;
774 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500775 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500776
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500777again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200778 while (!list_empty(&async_chunk->extents)) {
779 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500780 struct async_extent, list);
781 list_del(&async_extent->list);
782
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500783retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200784 lock_extent(io_tree, async_extent->start,
785 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500786 /* did the compression code fall back to uncompressed IO? */
787 if (!async_extent->pages) {
788 int page_started = 0;
789 unsigned long nr_written = 0;
790
Chris Mason771ed682008-11-06 22:02:51 -0500791 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300792 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500793 async_extent->start,
794 async_extent->start +
795 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300796 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500797
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100798 /* JDM XXX */
799
Chris Mason771ed682008-11-06 22:02:51 -0500800 /*
801 * if page_started, cow_file_range inserted an
802 * inline extent and took care of all the unlocking
803 * and IO for us. Otherwise, we need to submit
804 * all those pages down to the drive.
805 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500806 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300807 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200808 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500809 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500810 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500811 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700812 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200813 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500814 kfree(async_extent);
815 cond_resched();
816 continue;
817 }
818
Wang Xiaoguang18513092016-07-25 15:51:40 +0800819 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500820 async_extent->compressed_size,
821 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800822 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500823 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100824 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500825
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400826 if (ret == -ENOSPC) {
827 unlock_extent(io_tree, async_extent->start,
828 async_extent->start +
829 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800830
831 /*
832 * we need to redirty the pages if we decide to
833 * fallback to uncompressed IO, otherwise we
834 * will not submit these pages down to lower
835 * layers.
836 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300837 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800838 async_extent->start,
839 async_extent->start +
840 async_extent->ram_size - 1);
841
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100842 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400843 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500844 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500845 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000846 /*
847 * here we're doing allocation and writeback of the
848 * compressed pages
849 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300850 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800851 async_extent->ram_size, /* len */
852 async_extent->start, /* orig_start */
853 ins.objectid, /* block_start */
854 ins.offset, /* block_len */
855 ins.offset, /* orig_block_len */
856 async_extent->ram_size, /* ram_bytes */
857 async_extent->compress_type,
858 BTRFS_ORDERED_COMPRESSED);
859 if (IS_ERR(em))
860 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500861 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800862 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500863
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300864 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800865 async_extent->start,
866 ins.objectid,
867 async_extent->ram_size,
868 ins.offset,
869 BTRFS_ORDERED_COMPRESSED,
870 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100871 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300872 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100873 async_extent->start +
874 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500875 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100876 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400877 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500878
Chris Mason771ed682008-11-06 22:02:51 -0500879 /*
880 * clear dirty, set writeback and unlock the pages.
881 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300882 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400883 async_extent->start +
884 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400885 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
886 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400887 PAGE_SET_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300888 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500889 async_extent->ram_size,
890 ins.objectid,
891 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600892 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700893 async_chunk->write_flags,
894 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100895 struct page *p = async_extent->pages[0];
896 const u64 start = async_extent->start;
897 const u64 end = start + async_extent->ram_size - 1;
898
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300899 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200900 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200901
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100902 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300903 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100904 PAGE_END_WRITEBACK |
905 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100906 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100907 }
Chris Mason771ed682008-11-06 22:02:51 -0500908 alloc_hint = ins.objectid + ins.offset;
909 kfree(async_extent);
910 cond_resched();
911 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100912 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500913out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400914 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400915 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100916out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300917 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500918 async_extent->start +
919 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400920 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100921 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400922 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
923 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100924 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
925 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100926 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100927 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500928 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500929}
930
Nikolay Borisov43c69842020-06-03 08:55:02 +0300931static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400932 u64 num_bytes)
933{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300934 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400935 struct extent_map *em;
936 u64 alloc_hint = 0;
937
938 read_lock(&em_tree->lock);
939 em = search_extent_mapping(em_tree, start, num_bytes);
940 if (em) {
941 /*
942 * if block start isn't an actual block number then find the
943 * first block in this inode and use that as a hint. If that
944 * block is also bogus then just don't worry about it.
945 */
946 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
947 free_extent_map(em);
948 em = search_extent_mapping(em_tree, 0, 0);
949 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
950 alloc_hint = em->block_start;
951 if (em)
952 free_extent_map(em);
953 } else {
954 alloc_hint = em->block_start;
955 free_extent_map(em);
956 }
957 }
958 read_unlock(&em_tree->lock);
959
960 return alloc_hint;
961}
962
Chris Mason771ed682008-11-06 22:02:51 -0500963/*
964 * when extent_io.c finds a delayed allocation range in the file,
965 * the call backs end up in this code. The basic idea is to
966 * allocate extents on disk for the range, and create ordered data structs
967 * in ram to track those extents.
968 *
969 * locked_page is the page that writepage had locked already. We use
970 * it to make sure we don't do extra locks or unlocks.
971 *
972 * *page_started is set to one if we unlock locked_page and do everything
973 * required to start IO on it. It may be clean and already done with
974 * IO when we return.
975 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300976static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -0400977 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300978 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300979 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500980{
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300981 struct btrfs_root *root = inode->root;
982 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500983 u64 alloc_hint = 0;
984 u64 num_bytes;
985 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000986 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +0100987 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400988 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500989 struct btrfs_key ins;
990 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000991 unsigned clear_bits;
992 unsigned long page_ops;
993 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500994 int ret = 0;
995
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300996 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -0400997 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -0500998 ret = -EINVAL;
999 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001000 }
Chris Mason771ed682008-11-06 22:02:51 -05001001
Qu Wenruofda28322013-02-26 08:10:22 +00001002 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001003 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001004 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001005
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001006 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001007
Chris Mason771ed682008-11-06 22:02:51 -05001008 if (start == 0) {
1009 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001010 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001011 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001012 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001013 /*
1014 * We use DO_ACCOUNTING here because we need the
1015 * delalloc_release_metadata to be run _after_ we drop
1016 * our outstanding extent for clearing delalloc for this
1017 * range.
1018 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001019 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001020 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001021 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1022 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001023 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1024 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001025 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001026 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001027 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001028 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001029 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001030 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001031 }
1032 }
Chris Masonc8b97812008-10-29 14:49:59 -04001033
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001034 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1035 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001036
Filipe Manana432cd2a2020-06-08 13:32:55 +01001037 /*
1038 * Relocation relies on the relocated extents to have exactly the same
1039 * size as the original extents. Normally writeback for relocation data
1040 * extents follows a NOCOW path because relocation preallocates the
1041 * extents. However, due to an operation such as scrub turning a block
1042 * group to RO mode, it may fallback to COW mode, so we must make sure
1043 * an extent allocated during COW has exactly the requested size and can
1044 * not be split into smaller extents, otherwise relocation breaks and
1045 * fails during the stage where it updates the bytenr of file extent
1046 * items.
1047 */
1048 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1049 min_alloc_size = num_bytes;
1050 else
1051 min_alloc_size = fs_info->sectorsize;
1052
Anand Jain3752d222018-02-15 12:29:38 +08001053 while (num_bytes > 0) {
1054 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001055 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001056 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001057 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001058 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001059 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001060 cur_alloc_size = ins.offset;
1061 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001062
Chris Mason771ed682008-11-06 22:02:51 -05001063 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001064 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001065 start, /* orig_start */
1066 ins.objectid, /* block_start */
1067 ins.offset, /* block_len */
1068 ins.offset, /* orig_block_len */
1069 ram_size, /* ram_bytes */
1070 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001071 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001072 if (IS_ERR(em)) {
1073 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001074 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001075 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001076 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001077
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001078 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1079 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001080 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001081 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001082
Yan Zheng17d217f2008-12-12 10:03:38 -05001083 if (root->root_key.objectid ==
1084 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001085 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001086 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001087 /*
1088 * Only drop cache here, and process as normal.
1089 *
1090 * We must not allow extent_clear_unlock_delalloc()
1091 * at out_unlock label to free meta of this ordered
1092 * extent, as its meta should be freed by
1093 * btrfs_finish_ordered_io().
1094 *
1095 * So we must continue until @start is increased to
1096 * skip current ordered extent.
1097 */
Josef Bacik00361582013-08-14 14:02:47 -04001098 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001099 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001100 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001101 }
1102
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001103 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001104
Chris Masonc8b97812008-10-29 14:49:59 -04001105 /* we're not doing compressed IO, don't unlock the first
1106 * page (which the caller expects to stay locked), don't
1107 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001108 *
1109 * Do set the Private2 bit so we know this page was properly
1110 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001111 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001112 page_ops = unlock ? PAGE_UNLOCK : 0;
1113 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001114
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001115 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001116 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001117 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001118 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001119 if (num_bytes < cur_alloc_size)
1120 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001121 else
Anand Jain3752d222018-02-15 12:29:38 +08001122 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001123 alloc_hint = ins.objectid + ins.offset;
1124 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001125 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001126
1127 /*
1128 * btrfs_reloc_clone_csums() error, since start is increased
1129 * extent_clear_unlock_delalloc() at out_unlock label won't
1130 * free metadata of current ordered extent, we're OK to exit.
1131 */
1132 if (ret)
1133 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001134 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001135out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001136 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001137
Filipe Mananad9f85962014-08-25 10:43:00 +01001138out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001139 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001140out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001141 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001142 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001143out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001144 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1145 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001146 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1147 PAGE_END_WRITEBACK;
1148 /*
1149 * If we reserved an extent for our delalloc range (or a subrange) and
1150 * failed to create the respective ordered extent, then it means that
1151 * when we reserved the extent we decremented the extent's size from
1152 * the data space_info's bytes_may_use counter and incremented the
1153 * space_info's bytes_reserved counter by the same amount. We must make
1154 * sure extent_clear_unlock_delalloc() does not try to decrement again
1155 * the data space_info's bytes_may_use counter, therefore we do not pass
1156 * it the flag EXTENT_CLEAR_DATA_RESV.
1157 */
1158 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001159 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001160 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001161 locked_page,
1162 clear_bits,
1163 page_ops);
1164 start += cur_alloc_size;
1165 if (start >= end)
1166 goto out;
1167 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001168 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001169 clear_bits | EXTENT_CLEAR_DATA_RESV,
1170 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001171 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001172}
Chris Masonc8b97812008-10-29 14:49:59 -04001173
Chris Mason771ed682008-11-06 22:02:51 -05001174/*
1175 * work queue call back to started compression on a file and pages
1176 */
1177static noinline void async_cow_start(struct btrfs_work *work)
1178{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001179 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001180 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001181
Nikolay Borisovb5326272019-03-12 17:20:25 +02001182 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001183
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001184 compressed_extents = compress_file_range(async_chunk);
1185 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001186 btrfs_add_delayed_iput(async_chunk->inode);
1187 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001188 }
Chris Mason771ed682008-11-06 22:02:51 -05001189}
1190
1191/*
1192 * work queue call back to submit previously compressed pages
1193 */
1194static noinline void async_cow_submit(struct btrfs_work *work)
1195{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001196 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1197 work);
1198 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001199 unsigned long nr_pages;
1200
Nikolay Borisovb5326272019-03-12 17:20:25 +02001201 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001202 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001203
David Sterba093258e2018-02-26 16:15:17 +01001204 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001205 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001206 5 * SZ_1M)
1207 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001208
Nikolay Borisov4546d172019-01-03 10:50:03 +02001209 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001210 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001211 * in which case we don't have anything to submit, yet we need to
1212 * always adjust ->async_delalloc_pages as its paired with the init
1213 * happening in cow_file_range_async
1214 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001215 if (async_chunk->inode)
1216 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001217}
Chris Masonc8b97812008-10-29 14:49:59 -04001218
Chris Mason771ed682008-11-06 22:02:51 -05001219static noinline void async_cow_free(struct btrfs_work *work)
1220{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001221 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001222
Nikolay Borisovb5326272019-03-12 17:20:25 +02001223 async_chunk = container_of(work, struct async_chunk, work);
1224 if (async_chunk->inode)
1225 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001226 if (async_chunk->blkcg_css)
1227 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001228 /*
1229 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001230 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001231 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001232 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001233 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001234}
1235
Chris Masonec39f762019-07-10 12:28:17 -07001236static int cow_file_range_async(struct inode *inode,
1237 struct writeback_control *wbc,
1238 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001239 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001240 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001241{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001242 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonec39f762019-07-10 12:28:17 -07001243 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001244 struct async_cow *ctx;
1245 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001246 unsigned long nr_pages;
1247 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001248 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1249 int i;
1250 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001251 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001252 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001253
Nikolay Borisov69684c52019-03-12 17:20:28 +02001254 unlock_extent(&BTRFS_I(inode)->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001255
1256 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1257 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1258 num_chunks = 1;
1259 should_compress = false;
1260 } else {
1261 should_compress = true;
1262 }
1263
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001264 nofs_flag = memalloc_nofs_save();
1265 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1266 memalloc_nofs_restore(nofs_flag);
1267
Nikolay Borisov97db1202019-03-12 17:20:24 +02001268 if (!ctx) {
1269 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1270 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1271 EXTENT_DO_ACCOUNTING;
1272 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1273 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1274 PAGE_SET_ERROR;
1275
Nikolay Borisovad7ff172020-06-03 08:55:06 +03001276 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
1277 locked_page, clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001278 return -ENOMEM;
1279 }
1280
1281 async_chunk = ctx->chunks;
1282 atomic_set(&ctx->num_chunks, num_chunks);
1283
1284 for (i = 0; i < num_chunks; i++) {
1285 if (should_compress)
1286 cur_end = min(end, start + SZ_512K - 1);
1287 else
1288 cur_end = end;
1289
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001290 /*
1291 * igrab is called higher up in the call chain, take only the
1292 * lightweight reference for the callback lifetime
1293 */
1294 ihold(inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001295 async_chunk[i].pending = &ctx->num_chunks;
1296 async_chunk[i].inode = inode;
1297 async_chunk[i].start = start;
1298 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001299 async_chunk[i].write_flags = write_flags;
1300 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001301
Chris Mason1d53c9e2019-07-10 12:28:16 -07001302 /*
1303 * The locked_page comes all the way from writepage and its
1304 * the original page we were actually given. As we spread
1305 * this large delalloc region across multiple async_chunk
1306 * structs, only the first struct needs a pointer to locked_page
1307 *
1308 * This way we don't need racey decisions about who is supposed
1309 * to unlock it.
1310 */
1311 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001312 /*
1313 * Depending on the compressibility, the pages might or
1314 * might not go through async. We want all of them to
1315 * be accounted against wbc once. Let's do it here
1316 * before the paths diverge. wbc accounting is used
1317 * only for foreign writeback detection and doesn't
1318 * need full accuracy. Just account the whole thing
1319 * against the first page.
1320 */
1321 wbc_account_cgroup_owner(wbc, locked_page,
1322 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001323 async_chunk[i].locked_page = locked_page;
1324 locked_page = NULL;
1325 } else {
1326 async_chunk[i].locked_page = NULL;
1327 }
1328
Chris Masonec39f762019-07-10 12:28:17 -07001329 if (blkcg_css != blkcg_root_css) {
1330 css_get(blkcg_css);
1331 async_chunk[i].blkcg_css = blkcg_css;
1332 } else {
1333 async_chunk[i].blkcg_css = NULL;
1334 }
1335
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001336 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1337 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001338
Nikolay Borisov97db1202019-03-12 17:20:24 +02001339 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001340 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001341
Nikolay Borisov97db1202019-03-12 17:20:24 +02001342 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001343
Chris Mason771ed682008-11-06 22:02:51 -05001344 *nr_written += nr_pages;
1345 start = cur_end + 1;
1346 }
1347 *page_started = 1;
1348 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001349}
1350
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001351static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001352 u64 bytenr, u64 num_bytes)
1353{
1354 int ret;
1355 struct btrfs_ordered_sum *sums;
1356 LIST_HEAD(list);
1357
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001358 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001359 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001360 if (ret == 0 && list_empty(&list))
1361 return 0;
1362
1363 while (!list_empty(&list)) {
1364 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1365 list_del(&sums->list);
1366 kfree(sums);
1367 }
Liu Bo58113752018-01-31 17:09:13 -07001368 if (ret < 0)
1369 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001370 return 1;
1371}
1372
Filipe Manana467dc472020-05-27 11:16:07 +01001373static int fallback_to_cow(struct inode *inode, struct page *locked_page,
1374 const u64 start, const u64 end,
1375 int *page_started, unsigned long *nr_written)
1376{
Filipe Manana2166e5e2020-05-27 11:16:19 +01001377 const bool is_space_ino = btrfs_is_free_space_inode(BTRFS_I(inode));
Filipe Manana6bd335b2020-06-08 13:33:05 +01001378 const bool is_reloc_ino = (BTRFS_I(inode)->root->root_key.objectid ==
1379 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001380 const u64 range_bytes = end + 1 - start;
Filipe Manana467dc472020-05-27 11:16:07 +01001381 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1382 u64 range_start = start;
1383 u64 count;
1384
1385 /*
1386 * If EXTENT_NORESERVE is set it means that when the buffered write was
1387 * made we had not enough available data space and therefore we did not
1388 * reserve data space for it, since we though we could do NOCOW for the
1389 * respective file range (either there is prealloc extent or the inode
1390 * has the NOCOW bit set).
1391 *
1392 * However when we need to fallback to COW mode (because for example the
1393 * block group for the corresponding extent was turned to RO mode by a
1394 * scrub or relocation) we need to do the following:
1395 *
1396 * 1) We increment the bytes_may_use counter of the data space info.
1397 * If COW succeeds, it allocates a new data extent and after doing
1398 * that it decrements the space info's bytes_may_use counter and
1399 * increments its bytes_reserved counter by the same amount (we do
1400 * this at btrfs_add_reserved_bytes()). So we need to increment the
1401 * bytes_may_use counter to compensate (when space is reserved at
1402 * buffered write time, the bytes_may_use counter is incremented);
1403 *
1404 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1405 * that if the COW path fails for any reason, it decrements (through
1406 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1407 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001408 *
1409 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001410 * space cache inode or an inode of the data relocation tree, we must
1411 * also increment bytes_may_use of the data space_info for the same
1412 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001413 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001414 * group that contains that extent to RO mode and therefore force COW
1415 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001416 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001417 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001418 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001419 if (count > 0 || is_space_ino || is_reloc_ino) {
1420 u64 bytes = count;
Filipe Manana467dc472020-05-27 11:16:07 +01001421 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1422 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1423
Filipe Manana6bd335b2020-06-08 13:33:05 +01001424 if (is_space_ino || is_reloc_ino)
1425 bytes = range_bytes;
1426
Filipe Manana467dc472020-05-27 11:16:07 +01001427 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001428 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001429 spin_unlock(&sinfo->lock);
1430
Filipe Manana2166e5e2020-05-27 11:16:19 +01001431 if (count > 0)
1432 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1433 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001434 }
1435
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001436 return cow_file_range(BTRFS_I(inode), locked_page, start, end,
1437 page_started, nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001438}
1439
Chris Masond352ac62008-09-29 15:18:18 -04001440/*
1441 * when nowcow writeback call back. This checks for snapshots or COW copies
1442 * of the extents that exist in the file, and COWs the file as required.
1443 *
1444 * If no cow copies or snapshots exist, we write directly to the existing
1445 * blocks on disk
1446 */
Chris Mason7f366cf2009-03-12 20:12:45 -04001447static noinline int run_delalloc_nocow(struct inode *inode,
1448 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001449 const u64 start, const u64 end,
1450 int *page_started, int force,
1451 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001452{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001453 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masonbe20aa92007-12-17 20:14:01 -05001454 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001455 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001456 u64 cow_start = (u64)-1;
1457 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001458 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001459 bool check_prev = true;
1460 const bool freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02001461 u64 ino = btrfs_ino(BTRFS_I(inode));
Nikolay Borisov762bf092019-08-22 17:24:20 +03001462 bool nocow = false;
1463 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001464
1465 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001466 if (!path) {
Nikolay Borisovad7ff172020-06-03 08:55:06 +03001467 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
1468 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) {
Filipe Manana467dc472020-05-27 11:16:07 +01001688 ret = fallback_to_cow(inode, locked_page, cow_start,
1689 found_key.offset - 1,
1690 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 Borisov4b67c112020-06-03 08:55:05 +03001700 em = create_io_em(BTRFS_I(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 Borisove7fbf602020-06-03 08:55:13 +03001712 ret = btrfs_add_ordered_extent(BTRFS_I(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) {
1717 btrfs_drop_extent_cache(BTRFS_I(inode),
1718 cur_offset,
1719 cur_offset + num_bytes - 1,
1720 0);
1721 goto error;
1722 }
Yan Zhengd899e052008-10-30 14:25:28 -04001723 } else {
Nikolay Borisove7fbf602020-06-03 08:55:13 +03001724 ret = btrfs_add_ordered_extent(BTRFS_I(inode), cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001725 disk_bytenr, num_bytes,
1726 num_bytes,
1727 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001728 if (ret)
1729 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001730 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001731
Filipe Mananaf78c4362016-05-09 13:15:41 +01001732 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001733 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001734 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001735
Yan, Zhengefa56462010-05-16 10:49:59 -04001736 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001737 BTRFS_DATA_RELOC_TREE_OBJECTID)
1738 /*
1739 * Error handled later, as we must prevent
1740 * extent_clear_unlock_delalloc() in error handler
1741 * from freeing metadata of created ordered extent.
1742 */
Nikolay Borisov7bfa9532020-06-03 08:55:04 +03001743 ret = btrfs_reloc_clone_csums(BTRFS_I(inode), cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001744 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001745
Nikolay Borisovad7ff172020-06-03 08:55:06 +03001746 extent_clear_unlock_delalloc(BTRFS_I(inode), cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001747 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001748 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001749 EXTENT_DELALLOC |
1750 EXTENT_CLEAR_DATA_RESV,
1751 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1752
Yan Zheng80ff3852008-10-30 14:20:02 -04001753 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001754
1755 /*
1756 * btrfs_reloc_clone_csums() error, now we're OK to call error
1757 * handler, as metadata for created ordered extent will only
1758 * be freed by btrfs_finish_ordered_io().
1759 */
1760 if (ret)
1761 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001762 if (cur_offset > end)
1763 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001764 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001765 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001766
Robbie Ko506481b2018-10-30 18:04:04 +08001767 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001768 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001769
Yan Zheng80ff3852008-10-30 14:20:02 -04001770 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001771 cur_offset = end;
Filipe Manana467dc472020-05-27 11:16:07 +01001772 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1773 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001774 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001775 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001776 }
1777
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001778error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001779 if (nocow)
1780 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1781
Josef Bacik17ca04a2012-05-31 15:58:55 -04001782 if (ret && cur_offset < end)
Nikolay Borisovad7ff172020-06-03 08:55:06 +03001783 extent_clear_unlock_delalloc(BTRFS_I(inode), cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001784 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001785 EXTENT_DELALLOC | EXTENT_DEFRAG |
1786 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1787 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001788 PAGE_SET_WRITEBACK |
1789 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001790 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001791 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001792}
1793
Wang Shilong47059d92014-07-03 18:22:07 +08001794static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1795{
1796
1797 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1798 !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1799 return 0;
1800
1801 /*
1802 * @defrag_bytes is a hint value, no spinlock held here,
1803 * if is not zero, it means the file is defragging.
1804 * Force cow if given extent needs to be defragged.
1805 */
1806 if (BTRFS_I(inode)->defrag_bytes &&
1807 test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1808 EXTENT_DEFRAG, 0, NULL))
1809 return 1;
1810
1811 return 0;
1812}
1813
Chris Masond352ac62008-09-29 15:18:18 -04001814/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001815 * Function to process delayed allocation (create CoW) for ranges which are
1816 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001817 */
Nikolay Borisovbc9a8bf2018-12-19 09:50:20 +02001818int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001819 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1820 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001821{
Chris Masonbe20aa92007-12-17 20:14:01 -05001822 int ret;
Wang Shilong47059d92014-07-03 18:22:07 +08001823 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001824
Wang Shilong47059d92014-07-03 18:22:07 +08001825 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
Chris Masonc8b97812008-10-29 14:49:59 -04001826 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001827 page_started, 1, nr_written);
Wang Shilong47059d92014-07-03 18:22:07 +08001828 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
Yan Zhengd899e052008-10-30 14:25:28 -04001829 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001830 page_started, 0, nr_written);
Qu Wenruo42c16da2019-07-01 05:12:46 +00001831 } else if (!inode_can_compress(inode) ||
1832 !inode_need_compress(inode, start, end)) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001833 ret = cow_file_range(BTRFS_I(inode), locked_page, start, end,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001834 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001835 } else {
1836 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1837 &BTRFS_I(inode)->runtime_flags);
Chris Masonec39f762019-07-10 12:28:17 -07001838 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001839 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001840 }
Qu Wenruo524272602017-03-08 10:25:52 +08001841 if (ret)
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001842 btrfs_cleanup_ordered_extents(inode, locked_page, start,
1843 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001844 return ret;
1845}
1846
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001847void btrfs_split_delalloc_extent(struct inode *inode,
1848 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001849{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001850 u64 size;
1851
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001852 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001853 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001854 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001855
Josef Bacikdcab6a32015-02-11 15:08:59 -05001856 size = orig->end - orig->start + 1;
1857 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001858 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001859 u64 new_size;
1860
1861 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001862 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001863 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001864 */
1865 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001866 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001867 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001868 num_extents += count_max_extents(new_size);
1869 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001870 return;
1871 }
1872
Josef Bacik9e0baf62011-07-15 15:16:44 +00001873 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001874 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001875 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001876}
1877
1878/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001879 * Handle merged delayed allocation extents so we can keep track of new extents
1880 * that are just merged onto old extents, such as when we are doing sequential
1881 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001882 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001883void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1884 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001885{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001886 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001887 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001888
Josef Bacik9ed74f22009-09-11 16:12:44 -04001889 /* not delalloc, ignore it */
1890 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001891 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001892
Josef Bacik8461a3d2015-03-13 15:12:08 -04001893 if (new->start > other->start)
1894 new_size = new->end - other->start + 1;
1895 else
1896 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001897
1898 /* we're not bigger than the max, unreserve the space and go */
1899 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1900 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001901 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001902 spin_unlock(&BTRFS_I(inode)->lock);
1903 return;
1904 }
1905
1906 /*
Josef Bacikba117212015-03-13 15:01:24 -04001907 * We have to add up either side to figure out how many extents were
1908 * accounted for before we merged into one big extent. If the number of
1909 * extents we accounted for is <= the amount we need for the new range
1910 * then we can return, otherwise drop. Think of it like this
1911 *
1912 * [ 4k][MAX_SIZE]
1913 *
1914 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1915 * need 2 outstanding extents, on one side we have 1 and the other side
1916 * we have 1 so they are == and we can return. But in this case
1917 *
1918 * [MAX_SIZE+4k][MAX_SIZE+4k]
1919 *
1920 * Each range on their own accounts for 2 extents, but merged together
1921 * they are only 3 extents worth of accounting, so we need to drop in
1922 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001923 */
Josef Bacikba117212015-03-13 15:01:24 -04001924 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001925 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001926 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001927 num_extents += count_max_extents(old_size);
1928 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001929 return;
1930
Josef Bacik9e0baf62011-07-15 15:16:44 +00001931 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001932 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001933 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001934}
1935
Miao Xieeb73c1b2013-05-15 07:48:22 +00001936static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1937 struct inode *inode)
1938{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001939 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1940
Miao Xieeb73c1b2013-05-15 07:48:22 +00001941 spin_lock(&root->delalloc_lock);
1942 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1943 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1944 &root->delalloc_inodes);
1945 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1946 &BTRFS_I(inode)->runtime_flags);
1947 root->nr_delalloc_inodes++;
1948 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001949 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001950 BUG_ON(!list_empty(&root->delalloc_root));
1951 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001952 &fs_info->delalloc_roots);
1953 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001954 }
1955 }
1956 spin_unlock(&root->delalloc_lock);
1957}
1958
Nikolay Borisov2b877332018-04-27 12:21:51 +03001959
1960void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1961 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001962{
David Sterba3ffbd682018-06-29 10:56:42 +02001963 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001964
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001965 if (!list_empty(&inode->delalloc_inodes)) {
1966 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001967 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001968 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001969 root->nr_delalloc_inodes--;
1970 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001971 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001972 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001973 BUG_ON(list_empty(&root->delalloc_root));
1974 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001975 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001976 }
1977 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001978}
1979
1980static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1981 struct btrfs_inode *inode)
1982{
1983 spin_lock(&root->delalloc_lock);
1984 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001985 spin_unlock(&root->delalloc_lock);
1986}
1987
Chris Masond352ac62008-09-29 15:18:18 -04001988/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001989 * Properly track delayed allocation bytes in the inode and to maintain the
1990 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001991 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001992void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1993 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001994{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001995 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1996
Wang Shilong47059d92014-07-03 18:22:07 +08001997 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1998 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001999 /*
2000 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002001 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002002 * bit, which is only set or cleared with irqs on
2003 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002004 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002005 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002006 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002007 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002008 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002009
Josef Bacik8b62f872017-10-19 14:15:55 -04002010 spin_lock(&BTRFS_I(inode)->lock);
2011 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2012 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002013
Josef Bacik6a3891c2015-03-16 17:38:52 -04002014 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002015 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002016 return;
2017
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002018 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2019 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002020 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002021 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002022 if (*bits & EXTENT_DEFRAG)
2023 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002024 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002025 &BTRFS_I(inode)->runtime_flags))
2026 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002027 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002028 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002029
2030 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2031 (*bits & EXTENT_DELALLOC_NEW)) {
2032 spin_lock(&BTRFS_I(inode)->lock);
2033 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2034 state->start;
2035 spin_unlock(&BTRFS_I(inode)->lock);
2036 }
Chris Mason291d6732008-01-29 15:55:23 -05002037}
2038
Chris Masond352ac62008-09-29 15:18:18 -04002039/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002040 * Once a range is no longer delalloc this function ensures that proper
2041 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002042 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002043void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2044 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002045{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002046 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2047 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002048 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002049 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002050
Filipe Manana4a4b9642017-07-27 19:52:55 +01002051 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2052 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002053 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002054 spin_unlock(&inode->lock);
2055 }
Wang Shilong47059d92014-07-03 18:22:07 +08002056
Chris Mason75eff682008-12-15 15:54:40 -05002057 /*
2058 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002059 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002060 * bit, which is only set or cleared with irqs on
2061 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002062 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002063 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002064 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002065
Josef Bacik8b62f872017-10-19 14:15:55 -04002066 spin_lock(&inode->lock);
2067 btrfs_mod_outstanding_extents(inode, -num_extents);
2068 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002069
Josef Bacikb6d08f02013-09-27 14:57:43 -04002070 /*
2071 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002072 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002073 * error.
2074 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002075 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002076 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002077 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002078
Josef Bacik6a3891c2015-03-16 17:38:52 -04002079 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002080 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002081 return;
2082
Filipe Mananaa315e682017-03-06 23:04:20 +00002083 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2084 do_list && !(state->state & EXTENT_NORESERVE) &&
2085 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002086 btrfs_free_reserved_data_space_noquota(
2087 &inode->vfs_inode,
Filipe Manana46d4dac2020-06-09 11:19:33 +01002088 len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002089
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002090 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2091 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002092 spin_lock(&inode->lock);
2093 inode->delalloc_bytes -= len;
2094 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002095 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002096 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002097 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002098 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002099 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002100
2101 if ((state->state & EXTENT_DELALLOC_NEW) &&
2102 (*bits & EXTENT_DELALLOC_NEW)) {
2103 spin_lock(&inode->lock);
2104 ASSERT(inode->new_delalloc_bytes >= len);
2105 inode->new_delalloc_bytes -= len;
2106 spin_unlock(&inode->lock);
2107 }
Chris Mason291d6732008-01-29 15:55:23 -05002108}
2109
Chris Masond352ac62008-09-29 15:18:18 -04002110/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002111 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2112 * in a chunk's stripe. This function ensures that bios do not span a
2113 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002114 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002115 * @page - The page we are about to add to the bio
2116 * @size - size we want to add to the bio
2117 * @bio - bio we want to ensure is smaller than a stripe
2118 * @bio_flags - flags of the bio
2119 *
2120 * return 1 if page cannot be added to the bio
2121 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002122 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002123 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002124int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2125 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002126{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002127 struct inode *inode = page->mapping->host;
2128 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002129 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002130 u64 length = 0;
2131 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002132 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002133 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002134
Chris Mason771ed682008-11-06 22:02:51 -05002135 if (bio_flags & EXTENT_BIO_COMPRESSED)
2136 return 0;
2137
Kent Overstreet4f024f32013-10-11 15:44:27 -07002138 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002139 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002140 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2141 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002142 if (ret < 0)
2143 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002144
2145 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002146 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002147 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002148}
2149
Chris Masond352ac62008-09-29 15:18:18 -04002150/*
2151 * in order to insert checksums into the metadata in large chunks,
2152 * we wait until bio submission time. All the pages in the bio are
2153 * checksummed and sums are attached onto the ordered extent record.
2154 *
2155 * At IO completion time the cums attached on the ordered extent record
2156 * are inserted into the btree
2157 */
David Sterbad0ee3932018-03-08 14:35:48 +01002158static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002159 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002160{
Josef Bacikc6100a42017-05-05 11:57:13 -04002161 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002162 blk_status_t ret = 0;
Chris Masone0156402008-04-16 11:15:20 -04002163
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002164 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002165 BUG_ON(ret); /* -ENOMEM */
Chris Mason4a69a412008-11-06 22:03:00 -05002166 return 0;
2167}
Chris Masone0156402008-04-16 11:15:20 -04002168
Chris Mason4a69a412008-11-06 22:03:00 -05002169/*
Chris Masoncad321a2008-12-17 14:51:42 -05002170 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002171 * on write, or reading the csums from the tree before a read.
2172 *
2173 * Rules about async/sync submit,
2174 * a) read: sync submit
2175 *
2176 * b) write without checksum: sync submit
2177 *
2178 * c) write with checksum:
2179 * c-1) if bio is issued by fsync: sync submit
2180 * (sync_writers != 0)
2181 *
2182 * c-2) if root is reloc root: sync submit
2183 * (only in case of buffered IO)
2184 *
2185 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002186 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002187static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002188 int mirror_num,
2189 unsigned long bio_flags)
2190
Chris Mason44b8bd72008-04-16 11:14:51 -04002191{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002192 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302194 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002195 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002196 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002197 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002198
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002199 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002200
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002201 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302202 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002203
Mike Christie37226b22016-06-05 14:31:52 -05002204 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002205 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002206 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002207 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002208
Chris Masond20f7042008-12-08 16:58:54 -05002209 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002210 ret = btrfs_submit_compressed_read(inode, bio,
2211 mirror_num,
2212 bio_flags);
2213 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002214 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002215 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002216 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002217 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002218 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002219 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002220 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002221 /* csum items have already been cloned */
2222 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2223 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002224 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002225 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002226 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002227 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002228 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002229 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002230 if (ret)
2231 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002232 }
2233
Chris Mason0b86a832008-03-24 15:01:56 -04002234mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002235 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002236
2237out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002238 if (ret) {
2239 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002240 bio_endio(bio);
2241 }
Stefan Behrens61891922012-11-05 18:51:52 +01002242 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002243}
Chris Mason6885f302008-02-20 16:11:05 -05002244
Chris Masond352ac62008-09-29 15:18:18 -04002245/*
2246 * given a list of ordered sums record them in the inode. This happens
2247 * at IO completion time based on sums calculated at bio submission time.
2248 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002249static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002250 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002251{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002252 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002253 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002254
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002255 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002256 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002257 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002258 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002259 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002260 if (ret)
2261 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002262 }
2263 return 0;
2264}
2265
Josef Bacik2ac55d42010-02-03 19:33:23 +00002266int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002267 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002268 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002269{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002270 WARN_ON(PAGE_ALIGNED(end));
Chris Masonea8c2812008-08-04 23:17:27 -04002271 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002272 extra_bits, cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002273}
2274
Chris Masond352ac62008-09-29 15:18:18 -04002275/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002276struct btrfs_writepage_fixup {
2277 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002278 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002279 struct btrfs_work work;
2280};
2281
Christoph Hellwigb2950862008-12-02 09:54:17 -05002282static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002283{
2284 struct btrfs_writepage_fixup *fixup;
2285 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002286 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002287 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002288 struct page *page;
2289 struct inode *inode;
2290 u64 page_start;
2291 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002292 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002293 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002294
2295 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2296 page = fixup->page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002297 inode = fixup->inode;
2298 page_start = page_offset(page);
2299 page_end = page_offset(page) + PAGE_SIZE - 1;
2300
2301 /*
2302 * This is similar to page_mkwrite, we need to reserve the space before
2303 * we take the page lock.
2304 */
2305 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2306 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002307again:
Chris Mason247e7432008-07-17 12:53:51 -04002308 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002309
Chris Mason25f3c502020-01-21 11:51:42 -05002310 /*
2311 * Before we queued this fixup, we took a reference on the page.
2312 * page->mapping may go NULL, but it shouldn't be moved to a different
2313 * address space.
2314 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002315 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2316 /*
2317 * Unfortunately this is a little tricky, either
2318 *
2319 * 1) We got here and our page had already been dealt with and
2320 * we reserved our space, thus ret == 0, so we need to just
2321 * drop our space reservation and bail. This can happen the
2322 * first time we come into the fixup worker, or could happen
2323 * while waiting for the ordered extent.
2324 * 2) Our page was already dealt with, but we happened to get an
2325 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2326 * this case we obviously don't have anything to release, but
2327 * because the page was already dealt with we don't want to
2328 * mark the page with an error, so make sure we're resetting
2329 * ret to 0. This is why we have this check _before_ the ret
2330 * check, because we do not want to have a surprise ENOSPC
2331 * when the page was already properly dealt with.
2332 */
2333 if (!ret) {
2334 btrfs_delalloc_release_extents(BTRFS_I(inode),
2335 PAGE_SIZE);
2336 btrfs_delalloc_release_space(inode, data_reserved,
2337 page_start, PAGE_SIZE,
2338 true);
2339 }
2340 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002341 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002342 }
Chris Mason25f3c502020-01-21 11:51:42 -05002343
2344 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002345 * We can't mess with the page state unless it is locked, so now that
2346 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002347 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002348 if (ret)
2349 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002350
David Sterbaff13db42015-12-03 14:30:40 +01002351 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002352 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002353
2354 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002355 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002356 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002357
Nikolay Borisova776c6f2017-02-20 13:50:49 +02002358 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002359 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002360 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00002361 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
David Sterbae43bbe52017-12-12 21:43:52 +01002362 page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002363 unlock_page(page);
2364 btrfs_start_ordered_extent(inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002365 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002366 goto again;
2367 }
Chris Mason247e7432008-07-17 12:53:51 -04002368
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002369 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002370 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002371 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002372 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002373
Chris Mason25f3c502020-01-21 11:51:42 -05002374 /*
2375 * Everything went as planned, we're now the owner of a dirty page with
2376 * delayed allocation bits set and space reserved for our COW
2377 * destination.
2378 *
2379 * The page was dirty when we started, nothing should have cleaned it.
2380 */
2381 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002382 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002383out_reserved:
Qu Wenruo8702ba92019-10-14 14:34:51 +08002384 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002385 if (free_delalloc_space)
Filipe Manana53687002019-10-09 17:43:59 +01002386 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2387 PAGE_SIZE, true);
Josef Bacik2ac55d42010-02-03 19:33:23 +00002388 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002389 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002390out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002391 if (ret) {
2392 /*
2393 * We hit ENOSPC or other errors. Update the mapping and page
2394 * to reflect the errors and clean the page.
2395 */
2396 mapping_set_error(page->mapping, ret);
2397 end_extent_writepage(page, ret, page_start, page_end);
2398 clear_page_dirty_for_io(page);
2399 SetPageError(page);
2400 }
2401 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002402 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002403 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002404 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002405 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002406 /*
2407 * As a precaution, do a delayed iput in case it would be the last iput
2408 * that could need flushing space. Recursing back to fixup worker would
2409 * deadlock.
2410 */
2411 btrfs_add_delayed_iput(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002412}
2413
2414/*
2415 * There are a few paths in the higher layers of the kernel that directly
2416 * set the page dirty bit without asking the filesystem if it is a
2417 * good idea. This causes problems because we want to make sure COW
2418 * properly happens and the data=ordered rules are followed.
2419 *
Chris Masonc8b97812008-10-29 14:49:59 -04002420 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002421 * hasn't been properly setup for IO. We kick off an async process
2422 * to fix it up. The async helper will wait for ordered extents, set
2423 * the delalloc bit and make it safe to write the page.
2424 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002425int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002426{
2427 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002428 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002429 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002430
Chris Mason8b62b722009-09-02 16:53:46 -04002431 /* this page is properly in the ordered list */
2432 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002433 return 0;
2434
Chris Mason25f3c502020-01-21 11:51:42 -05002435 /*
2436 * PageChecked is set below when we create a fixup worker for this page,
2437 * don't try to create another one if we're already PageChecked()
2438 *
2439 * The extent_io writepage code will redirty the page if we send back
2440 * EAGAIN.
2441 */
Chris Mason247e7432008-07-17 12:53:51 -04002442 if (PageChecked(page))
2443 return -EAGAIN;
2444
2445 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2446 if (!fixup)
2447 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002448
Josef Bacikf4b13632020-01-21 14:34:52 -05002449 /*
2450 * We are already holding a reference to this inode from
2451 * write_cache_pages. We need to hold it because the space reservation
2452 * takes place outside of the page lock, and we can't trust
2453 * page->mapping outside of the page lock.
2454 */
2455 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002456 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002457 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002458 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002459 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002460 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002461 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002462
2463 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002464}
2465
Yan Zhengd899e052008-10-30 14:25:28 -04002466static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2467 struct inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002468 struct btrfs_file_extent_item *stack_fi,
2469 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002470{
2471 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002472 struct btrfs_path *path;
2473 struct extent_buffer *leaf;
2474 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002475 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2476 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2477 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2478 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002479 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002480 int ret;
2481
2482 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002483 if (!path)
2484 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002485
Chris Masona1ed8352009-09-11 12:27:37 -04002486 /*
2487 * we may be replacing one extent in the tree with another.
2488 * The new extent is pinned in the extent map, and we don't want
2489 * to drop it from the cache until it is completely in the btree.
2490 *
2491 * So, tell btrfs_drop_extents to leave this extent in the cache.
2492 * the caller is expected to unpin it and allow it to be merged
2493 * with the others.
2494 */
Nikolay Borisov906c4482020-06-03 08:55:08 +03002495 ret = __btrfs_drop_extents(trans, root, BTRFS_I(inode), path, file_pos,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002496 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002497 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002498 if (ret)
2499 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002500
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002501 if (!extent_inserted) {
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02002502 ins.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002503 ins.offset = file_pos;
2504 ins.type = BTRFS_EXTENT_DATA_KEY;
2505
2506 path->leave_spinning = 1;
2507 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002508 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002509 if (ret)
2510 goto out;
2511 }
Yan Zhengd899e052008-10-30 14:25:28 -04002512 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002513 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2514 write_extent_buffer(leaf, stack_fi,
2515 btrfs_item_ptr_offset(leaf, path->slots[0]),
2516 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002517
Yan Zhengd899e052008-10-30 14:25:28 -04002518 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002519 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002520
2521 inode_add_bytes(inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002522
2523 ins.objectid = disk_bytenr;
2524 ins.offset = disk_num_bytes;
2525 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002526
Josef Bacik9ddc9592020-01-17 09:02:22 -05002527 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), file_pos,
2528 ram_bytes);
2529 if (ret)
2530 goto out;
2531
Josef Bacik84f7d8e2017-09-29 15:43:49 -04002532 ret = btrfs_alloc_reserved_file_extent(trans, root,
2533 btrfs_ino(BTRFS_I(inode)),
Qu Wenruo9729f102020-06-10 09:04:41 +08002534 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002535out:
Yan Zhengd899e052008-10-30 14:25:28 -04002536 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002537
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002538 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002539}
2540
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002541static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002542 u64 start, u64 len)
2543{
David Sterba32da53862019-10-29 19:20:18 +01002544 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002545
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002546 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002547 ASSERT(cache);
2548
2549 spin_lock(&cache->lock);
2550 cache->delalloc_bytes -= len;
2551 spin_unlock(&cache->lock);
2552
2553 btrfs_put_block_group(cache);
2554}
2555
Qu Wenruo203f44c52020-06-10 09:04:40 +08002556static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2557 struct inode *inode,
2558 struct btrfs_ordered_extent *oe)
2559{
2560 struct btrfs_file_extent_item stack_fi;
2561 u64 logical_len;
2562
2563 memset(&stack_fi, 0, sizeof(stack_fi));
2564 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2565 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2566 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2567 oe->disk_num_bytes);
2568 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2569 logical_len = oe->truncated_len;
2570 else
2571 logical_len = oe->num_bytes;
2572 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2573 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2574 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2575 /* Encryption and other encoding is reserved and all 0 */
2576
2577 return insert_reserved_file_extent(trans, inode, oe->file_offset,
Qu Wenruo7dbeaad2020-06-10 09:04:43 +08002578 &stack_fi, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002579}
2580
2581/*
2582 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002583 * an ordered extent if the range of bytes in the file it covers are
2584 * fully written.
2585 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002586static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002587{
Josef Bacik5fd02042012-05-02 14:00:54 -04002588 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002589 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002590 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002591 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002592 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002593 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002594 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002595 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002596 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002597 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002598 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002599 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002600 bool range_locked = false;
2601 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002602 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002603 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002604
Omar Sandovalbffe6332019-12-02 17:34:19 -08002605 start = ordered_extent->file_offset;
2606 end = start + ordered_extent->num_bytes - 1;
2607
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002608 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2609 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2610 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2611 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002612
Nikolay Borisov8d510122019-10-08 20:43:06 +03002613 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002614
Josef Bacik5fd02042012-05-02 14:00:54 -04002615 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2616 ret = -EIO;
2617 goto out;
2618 }
2619
Omar Sandovalbffe6332019-12-02 17:34:19 -08002620 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002621
Josef Bacik77cef2e2013-08-29 13:57:21 -04002622 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2623 truncated = true;
2624 logical_len = ordered_extent->truncated_len;
2625 /* Truncated the entire extent, don't bother adding */
2626 if (!logical_len)
2627 goto out;
2628 }
2629
Yan, Zhengc2167752009-11-12 09:34:21 +00002630 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002631 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002632
Josef Bacikd923afe2020-01-17 09:02:23 -05002633 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002634 if (freespace_inode)
2635 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002636 else
2637 trans = btrfs_join_transaction(root);
2638 if (IS_ERR(trans)) {
2639 ret = PTR_ERR(trans);
2640 trans = NULL;
2641 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002642 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002643 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002644 ret = btrfs_update_inode_fallback(trans, root, inode);
2645 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002646 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002647 goto out;
2648 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002649
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002650 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002651 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002652
Nikolay Borisov8d510122019-10-08 20:43:06 +03002653 if (freespace_inode)
2654 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002655 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002656 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002657 if (IS_ERR(trans)) {
2658 ret = PTR_ERR(trans);
2659 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002660 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002661 }
Chris Masona79b7d42014-05-22 16:18:52 -07002662
Josef Bacik69fe2d72017-10-19 14:15:57 -04002663 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002664
Chris Masonc8b97812008-10-29 14:49:59 -04002665 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002666 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002667 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002668 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002669 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002670 ordered_extent->file_offset,
2671 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002672 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002673 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002674 BUG_ON(root == fs_info->tree_root);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002675 ret = insert_ordered_extent_file_extent(trans, inode,
2676 ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002677 if (!ret) {
2678 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002679 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002680 ordered_extent->disk_bytenr,
2681 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002682 }
Yan Zhengd899e052008-10-30 14:25:28 -04002683 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002684 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002685 ordered_extent->file_offset,
2686 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002687 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002688 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002689 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002690 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002691
Nikolay Borisovac01f262018-01-08 10:59:43 +02002692 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2693 if (ret) {
2694 btrfs_abort_transaction(trans, ret);
2695 goto out;
2696 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002697
Josef Bacikd923afe2020-01-17 09:02:23 -05002698 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002699 ret = btrfs_update_inode_fallback(trans, root, inode);
2700 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002701 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002702 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002703 }
2704 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002705out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002706 clear_bits = EXTENT_DEFRAG;
2707 if (range_locked)
2708 clear_bits |= EXTENT_LOCKED;
2709 if (clear_new_delalloc_bytes)
2710 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002711 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2712 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002713 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002714
Miao Xiea698d0752012-09-20 01:51:59 -06002715 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002716 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002717
Josef Bacik77cef2e2013-08-29 13:57:21 -04002718 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002719 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002720
2721 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002722 unwritten_start += logical_len;
2723 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002724
2725 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002726 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002727
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002728 /*
2729 * If the ordered extent had an IOERR or something else went
2730 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002731 * back to the allocator. We only free the extent in the
2732 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002733 *
2734 * If we made it past insert_reserved_file_extent before we
2735 * errored out then we don't need to do this as the accounting
2736 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002737 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002738 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002739 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002740 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002741 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2742 /*
2743 * Discard the range before returning it back to the
2744 * free space pool
2745 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002746 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002747 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002748 ordered_extent->disk_bytenr,
2749 ordered_extent->disk_num_bytes,
2750 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002751 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002752 ordered_extent->disk_bytenr,
2753 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002754 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002755 }
2756
Josef Bacik5fd02042012-05-02 14:00:54 -04002757 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002758 * This needs to be done to make sure anybody waiting knows we are done
2759 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002760 */
2761 btrfs_remove_ordered_extent(inode, ordered_extent);
2762
Chris Masone6dcd2d2008-07-17 12:53:50 -04002763 /* once for us */
2764 btrfs_put_ordered_extent(ordered_extent);
2765 /* once for the tree */
2766 btrfs_put_ordered_extent(ordered_extent);
2767
Josef Bacik5fd02042012-05-02 14:00:54 -04002768 return ret;
2769}
2770
2771static void finish_ordered_fn(struct btrfs_work *work)
2772{
2773 struct btrfs_ordered_extent *ordered_extent;
2774 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2775 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002776}
2777
Nikolay Borisovc6297322018-11-08 10:18:08 +02002778void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2779 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002780{
Josef Bacik5fd02042012-05-02 14:00:54 -04002781 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002782 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5fd02042012-05-02 14:00:54 -04002783 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002784 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002785
liubo1abe9b82011-03-24 11:18:59 +00002786 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2787
Chris Mason8b62b722009-09-02 16:53:46 -04002788 ClearPagePrivate2(page);
Josef Bacik5fd02042012-05-02 14:00:54 -04002789 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2790 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002791 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002792
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002793 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002794 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002795 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002796 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002797
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002798 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002799 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002800}
2801
Omar Sandoval47df7762020-04-16 14:46:17 -07002802static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2803 int icsum, struct page *page, int pgoff, u64 start,
2804 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002805{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002806 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2807 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002808 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002809 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2810 u8 *csum_expected;
2811 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002812
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002813 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002814
2815 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002816 shash->tfm = fs_info->csum_shash;
2817
Eric Biggersfd080012020-04-30 23:51:59 -07002818 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002819
2820 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002821 goto zeroit;
2822
2823 kunmap_atomic(kaddr);
2824 return 0;
2825zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002826 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2827 io_bio->mirror_num);
Miao Xiedc380ae2014-09-12 18:43:55 +08002828 memset(kaddr + pgoff, 1, len);
2829 flush_dcache_page(page);
2830 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002831 return -EIO;
2832}
2833
Chris Masond352ac62008-09-29 15:18:18 -04002834/*
Chris Masond352ac62008-09-29 15:18:18 -04002835 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002836 * if there's a match, we allow the bio to finish. If not, the code in
2837 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002838 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002839static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2840 u64 phy_offset, struct page *page,
2841 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002842{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002843 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002844 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002845 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002846 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002847
Chris Masond20f7042008-12-08 16:58:54 -05002848 if (PageChecked(page)) {
2849 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002850 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002851 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002852
2853 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002854 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002855
Yan Zheng17d217f2008-12-12 10:03:38 -05002856 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002857 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002858 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002859 return 0;
2860 }
2861
Miao Xiefacc8a222013-07-25 19:22:34 +08002862 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002863 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2864 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002865}
Chris Masonb888db22007-08-27 16:49:44 -04002866
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002867/*
2868 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2869 *
2870 * @inode: The inode we want to perform iput on
2871 *
2872 * This function uses the generic vfs_inode::i_count to track whether we should
2873 * just decrement it (in case it's > 1) or if this is the last iput then link
2874 * the inode to the delayed iput machinery. Delayed iputs are processed at
2875 * transaction commit time/superblock commit/cleaner kthread.
2876 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002877void btrfs_add_delayed_iput(struct inode *inode)
2878{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002879 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002880 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002881
2882 if (atomic_add_unless(&inode->i_count, -1, 1))
2883 return;
2884
Josef Bacik034f7842018-12-03 11:06:52 -05002885 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002886 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002887 ASSERT(list_empty(&binode->delayed_iput));
2888 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002889 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002890 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2891 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002892}
2893
Josef Bacik63611e72019-06-18 10:59:18 -04002894static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2895 struct btrfs_inode *inode)
2896{
2897 list_del_init(&inode->delayed_iput);
2898 spin_unlock(&fs_info->delayed_iput_lock);
2899 iput(&inode->vfs_inode);
2900 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2901 wake_up(&fs_info->delayed_iputs_wait);
2902 spin_lock(&fs_info->delayed_iput_lock);
2903}
2904
2905static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2906 struct btrfs_inode *inode)
2907{
2908 if (!list_empty(&inode->delayed_iput)) {
2909 spin_lock(&fs_info->delayed_iput_lock);
2910 if (!list_empty(&inode->delayed_iput))
2911 run_delayed_iput_locked(fs_info, inode);
2912 spin_unlock(&fs_info->delayed_iput_lock);
2913 }
2914}
2915
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002916void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002917{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002918
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002919 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002920 while (!list_empty(&fs_info->delayed_iputs)) {
2921 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002922
David Sterba8089fe62015-11-19 14:15:51 +01002923 inode = list_first_entry(&fs_info->delayed_iputs,
2924 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002925 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002926 }
David Sterba8089fe62015-11-19 14:15:51 +01002927 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002928}
2929
Josef Bacik034f7842018-12-03 11:06:52 -05002930/**
2931 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2932 * @fs_info - the fs_info for this fs
2933 * @return - EINTR if we were killed, 0 if nothing's pending
2934 *
2935 * This will wait on any delayed iputs that are currently running with KILLABLE
2936 * set. Once they are all done running we will return, unless we are killed in
2937 * which case we return EINTR. This helps in user operations like fallocate etc
2938 * that might get blocked on the iputs.
2939 */
2940int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2941{
2942 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2943 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2944 if (ret)
2945 return -EINTR;
2946 return 0;
2947}
2948
Yan, Zhengd68fc572010-05-16 10:49:58 -04002949/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002950 * This creates an orphan entry for the given inode in case something goes wrong
2951 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002952 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002953int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002954 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002955{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002956 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002957
Omar Sandoval27919062018-05-11 13:13:37 -07002958 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2959 if (ret && ret != -EEXIST) {
2960 btrfs_abort_transaction(trans, ret);
2961 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002962 }
2963
Yan, Zhengd68fc572010-05-16 10:49:58 -04002964 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002965}
2966
2967/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002968 * We have done the delete so we can go ahead and remove the orphan item for
2969 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002970 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002971static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002972 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002973{
Omar Sandoval27919062018-05-11 13:13:37 -07002974 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002975}
2976
2977/*
2978 * this cleans up any orphans that may be left on the list from the last use
2979 * of this root.
2980 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002981int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002982{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002983 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002984 struct btrfs_path *path;
2985 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002986 struct btrfs_key key, found_key;
2987 struct btrfs_trans_handle *trans;
2988 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002989 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002990 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002991
Yan, Zhengd68fc572010-05-16 10:49:58 -04002992 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002993 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002994
2995 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002996 if (!path) {
2997 ret = -ENOMEM;
2998 goto out;
2999 }
David Sterbae4058b52015-11-27 16:31:35 +01003000 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003001
3002 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003003 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003004 key.offset = (u64)-1;
3005
Josef Bacik7b128762008-07-24 12:17:14 -04003006 while (1) {
3007 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003008 if (ret < 0)
3009 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003010
3011 /*
3012 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003013 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003014 * find the key and see if we have stuff that matches
3015 */
3016 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003017 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003018 if (path->slots[0] == 0)
3019 break;
3020 path->slots[0]--;
3021 }
3022
3023 /* pull out the item */
3024 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003025 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3026
3027 /* make sure the item matches what we want */
3028 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3029 break;
David Sterba962a2982014-06-04 18:41:45 +02003030 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003031 break;
3032
3033 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003034 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003035
3036 /*
3037 * this is where we are basically btrfs_lookup, without the
3038 * crossing root thing. we store the inode number in the
3039 * offset of the orphan item.
3040 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003041
3042 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003043 btrfs_err(fs_info,
3044 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003045 ret = -EINVAL;
3046 goto out;
3047 }
3048
3049 last_objectid = found_key.offset;
3050
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003051 found_key.objectid = found_key.offset;
3052 found_key.type = BTRFS_INODE_ITEM_KEY;
3053 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003054 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303055 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003056 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003057 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003058
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003059 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003060 struct btrfs_root *dead_root;
3061 struct btrfs_fs_info *fs_info = root->fs_info;
3062 int is_dead_root = 0;
3063
3064 /*
3065 * this is an orphan in the tree root. Currently these
3066 * could come from 2 sources:
3067 * a) a snapshot deletion in progress
3068 * b) a free space cache inode
3069 * We need to distinguish those two, as the snapshot
3070 * orphan must not get deleted.
3071 * find_dead_roots already ran before us, so if this
3072 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003073 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003074 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003075
3076 spin_lock(&fs_info->fs_roots_radix_lock);
3077 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3078 (unsigned long)found_key.objectid);
3079 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3080 is_dead_root = 1;
3081 spin_unlock(&fs_info->fs_roots_radix_lock);
3082
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003083 if (is_dead_root) {
3084 /* prevent this orphan from being found again */
3085 key.offset = found_key.objectid - 1;
3086 continue;
3087 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003088
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003089 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003090
Josef Bacika8c9e572011-09-21 16:55:59 -04003091 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003092 * If we have an inode with links, there are a couple of
3093 * possibilities. Old kernels (before v3.12) used to create an
3094 * orphan item for truncate indicating that there were possibly
3095 * extent items past i_size that needed to be deleted. In v3.12,
3096 * truncate was changed to update i_size in sync with the extent
3097 * items, but the (useless) orphan item was still created. Since
3098 * v4.18, we don't create the orphan item for truncate at all.
3099 *
3100 * So, this item could mean that we need to do a truncate, but
3101 * only if this filesystem was last used on a pre-v3.12 kernel
3102 * and was not cleanly unmounted. The odds of that are quite
3103 * slim, and it's a pain to do the truncate now, so just delete
3104 * the orphan item.
3105 *
3106 * It's also possible that this orphan item was supposed to be
3107 * deleted but wasn't. The inode number may have been reused,
3108 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003109 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003110 if (ret == -ENOENT || inode->i_nlink) {
3111 if (!ret)
3112 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003113 trans = btrfs_start_transaction(root, 1);
3114 if (IS_ERR(trans)) {
3115 ret = PTR_ERR(trans);
3116 goto out;
3117 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003118 btrfs_debug(fs_info, "auto deleting %Lu",
3119 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003120 ret = btrfs_del_orphan_item(trans, root,
3121 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003122 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003123 if (ret)
3124 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003125 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003126 }
Josef Bacik7b128762008-07-24 12:17:14 -04003127
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003128 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003129
3130 /* this will do delete_inode and everything for us */
3131 iput(inode);
3132 }
Miao Xie3254c872011-11-10 20:45:05 -05003133 /* release the path since we're done with it */
3134 btrfs_release_path(path);
3135
Yan, Zhengd68fc572010-05-16 10:49:58 -04003136 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3137
Omar Sandovala575cee2018-05-11 13:13:38 -07003138 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003139 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003140 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003141 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003142 }
Josef Bacik7b128762008-07-24 12:17:14 -04003143
3144 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003145 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003146
3147out:
3148 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003149 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003150 btrfs_free_path(path);
3151 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003152}
3153
Chris Masond352ac62008-09-29 15:18:18 -04003154/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003155 * very simple check to peek ahead in the leaf looking for xattrs. If we
3156 * don't find any xattrs, we know there can't be any acls.
3157 *
3158 * slot is the slot the inode is in, objectid is the objectid of the inode
3159 */
3160static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003161 int slot, u64 objectid,
3162 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003163{
3164 u32 nritems = btrfs_header_nritems(leaf);
3165 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003166 static u64 xattr_access = 0;
3167 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003168 int scanned = 0;
3169
Josef Bacikf23b5a52013-06-19 10:16:26 -04003170 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003171 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3172 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3173 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3174 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003175 }
3176
Chris Mason46a53cc2009-04-27 11:47:50 -04003177 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003178 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003179 while (slot < nritems) {
3180 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3181
3182 /* we found a different objectid, there must not be acls */
3183 if (found_key.objectid != objectid)
3184 return 0;
3185
3186 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003187 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003188 if (*first_xattr_slot == -1)
3189 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003190 if (found_key.offset == xattr_access ||
3191 found_key.offset == xattr_default)
3192 return 1;
3193 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003194
3195 /*
3196 * we found a key greater than an xattr key, there can't
3197 * be any acls later on
3198 */
3199 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3200 return 0;
3201
3202 slot++;
3203 scanned++;
3204
3205 /*
3206 * it goes inode, inode backrefs, xattrs, extents,
3207 * so if there are a ton of hard links to an inode there can
3208 * be a lot of backrefs. Don't waste time searching too hard,
3209 * this is just an optimization
3210 */
3211 if (scanned >= 8)
3212 break;
3213 }
3214 /* we hit the end of the leaf before we found an xattr or
3215 * something larger than an xattr. We have to assume the inode
3216 * has acls
3217 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003218 if (*first_xattr_slot == -1)
3219 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003220 return 1;
3221}
3222
3223/*
Chris Masond352ac62008-09-29 15:18:18 -04003224 * read an inode from the btree into the in-memory inode
3225 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003226static int btrfs_read_locked_inode(struct inode *inode,
3227 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003228{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003229 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003230 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003231 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003232 struct btrfs_inode_item *inode_item;
3233 struct btrfs_root *root = BTRFS_I(inode)->root;
3234 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003235 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003236 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003237 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003238 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003239 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003240 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003241
3242 ret = btrfs_fill_inode(inode, &rdev);
3243 if (!ret)
3244 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003245
Filipe Manana4222ea72018-10-24 10:13:03 +01003246 if (!path) {
3247 path = btrfs_alloc_path();
3248 if (!path)
3249 return -ENOMEM;
3250 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003251
Chris Mason39279cc2007-06-12 06:35:45 -04003252 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003253
Chris Mason39279cc2007-06-12 06:35:45 -04003254 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003255 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003256 if (path != in_path)
3257 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003258 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003259 }
Chris Mason39279cc2007-06-12 06:35:45 -04003260
Chris Mason5f39d392007-10-15 16:14:19 -04003261 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003262
3263 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003264 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003265
Chris Mason5f39d392007-10-15 16:14:19 -04003266 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3267 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003268 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003269 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003270 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3271 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003272 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003273 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3274 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003275
David Sterbaa937b972014-12-12 17:39:12 +01003276 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3277 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003278
David Sterbaa937b972014-12-12 17:39:12 +01003279 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3280 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003281
David Sterbaa937b972014-12-12 17:39:12 +01003282 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3283 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003284
chandan r9cc97d62012-07-04 12:48:07 +05303285 BTRFS_I(inode)->i_otime.tv_sec =
3286 btrfs_timespec_sec(leaf, &inode_item->otime);
3287 BTRFS_I(inode)->i_otime.tv_nsec =
3288 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003289
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003290 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003291 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003292 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3293
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003294 inode_set_iversion_queried(inode,
3295 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003296 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003297 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003298 rdev = btrfs_inode_rdev(leaf, inode_item);
3299
Josef Bacikaec74772008-07-24 12:12:38 -04003300 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003301 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003302
3303cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003304 /*
3305 * If we were modified in the current generation and evicted from memory
3306 * and then re-read we need to do a full sync since we don't have any
3307 * idea about which extents were modified before we were evicted from
3308 * cache.
3309 *
3310 * This is required for both inode re-read from disk and delayed inode
3311 * in delayed_nodes_tree.
3312 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003313 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003314 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3315 &BTRFS_I(inode)->runtime_flags);
3316
Filipe Mananabde6c242015-07-24 00:00:19 +01003317 /*
3318 * We don't persist the id of the transaction where an unlink operation
3319 * against the inode was last made. So here we assume the inode might
3320 * have been evicted, and therefore the exact value of last_unlink_trans
3321 * lost, and set it to last_trans to avoid metadata inconsistencies
3322 * between the inode and its parent if the inode is fsync'ed and the log
3323 * replayed. For example, in the scenario:
3324 *
3325 * touch mydir/foo
3326 * ln mydir/foo mydir/bar
3327 * sync
3328 * unlink mydir/bar
3329 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3330 * xfs_io -c fsync mydir/foo
3331 * <power failure>
3332 * mount fs, triggers fsync log replay
3333 *
3334 * We must make sure that when we fsync our inode foo we also log its
3335 * parent inode, otherwise after log replay the parent still has the
3336 * dentry with the "bar" name but our inode foo has a link count of 1
3337 * and doesn't have an inode ref with the name "bar" anymore.
3338 *
3339 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003340 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003341 * transaction commits on fsync if our inode is a directory, or if our
3342 * inode is not a directory, logging its parent unnecessarily.
3343 */
3344 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3345
Miao Xie67de1172013-12-26 13:07:06 +08003346 path->slots[0]++;
3347 if (inode->i_nlink != 1 ||
3348 path->slots[0] >= btrfs_header_nritems(leaf))
3349 goto cache_acl;
3350
3351 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003352 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003353 goto cache_acl;
3354
3355 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3356 if (location.type == BTRFS_INODE_REF_KEY) {
3357 struct btrfs_inode_ref *ref;
3358
3359 ref = (struct btrfs_inode_ref *)ptr;
3360 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3361 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3362 struct btrfs_inode_extref *extref;
3363
3364 extref = (struct btrfs_inode_extref *)ptr;
3365 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3366 extref);
3367 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003368cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003369 /*
3370 * try to precache a NULL acl entry for files that don't have
3371 * any xattrs or acls
3372 */
Li Zefan33345d012011-04-20 10:31:50 +08003373 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003374 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003375 if (first_xattr_slot != -1) {
3376 path->slots[0] = first_xattr_slot;
3377 ret = btrfs_load_inode_props(inode, path);
3378 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003379 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003380 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003381 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003382 root->root_key.objectid, ret);
3383 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003384 if (path != in_path)
3385 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003386
Al Viro72c04902009-06-24 16:58:48 -04003387 if (!maybe_acls)
3388 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003389
Chris Mason39279cc2007-06-12 06:35:45 -04003390 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003391 case S_IFREG:
3392 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003393 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003394 inode->i_fop = &btrfs_file_operations;
3395 inode->i_op = &btrfs_file_inode_operations;
3396 break;
3397 case S_IFDIR:
3398 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003399 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003400 break;
3401 case S_IFLNK:
3402 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003403 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003404 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003405 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003406 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003407 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003408 init_special_inode(inode, inode->i_mode, rdev);
3409 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003410 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003411
David Sterba7b6a2212018-03-26 18:40:21 +02003412 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003413 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003414}
3415
Chris Masond352ac62008-09-29 15:18:18 -04003416/*
3417 * given a leaf and an inode, copy the inode fields into the leaf
3418 */
Chris Masone02119d2008-09-05 16:13:11 -04003419static void fill_inode_item(struct btrfs_trans_handle *trans,
3420 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003421 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003422 struct inode *inode)
3423{
Liu Bo51fab692012-12-27 09:01:21 +00003424 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003425
David Sterbac82f8232019-08-09 17:48:21 +02003426 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003427
David Sterbacc4c13d2020-04-29 02:15:56 +02003428 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3429 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3430 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3431 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3432 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003433
David Sterbacc4c13d2020-04-29 02:15:56 +02003434 btrfs_set_token_timespec_sec(&token, &item->atime,
3435 inode->i_atime.tv_sec);
3436 btrfs_set_token_timespec_nsec(&token, &item->atime,
3437 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003438
David Sterbacc4c13d2020-04-29 02:15:56 +02003439 btrfs_set_token_timespec_sec(&token, &item->mtime,
3440 inode->i_mtime.tv_sec);
3441 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3442 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003443
David Sterbacc4c13d2020-04-29 02:15:56 +02003444 btrfs_set_token_timespec_sec(&token, &item->ctime,
3445 inode->i_ctime.tv_sec);
3446 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3447 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003448
David Sterbacc4c13d2020-04-29 02:15:56 +02003449 btrfs_set_token_timespec_sec(&token, &item->otime,
3450 BTRFS_I(inode)->i_otime.tv_sec);
3451 btrfs_set_token_timespec_nsec(&token, &item->otime,
3452 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303453
David Sterbacc4c13d2020-04-29 02:15:56 +02003454 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3455 btrfs_set_token_inode_generation(&token, item,
3456 BTRFS_I(inode)->generation);
3457 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3458 btrfs_set_token_inode_transid(&token, item, trans->transid);
3459 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3460 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3461 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003462}
3463
Chris Masond352ac62008-09-29 15:18:18 -04003464/*
3465 * copy everything in the in-memory inode into the btree.
3466 */
Chris Mason21151332011-11-10 20:39:08 -05003467static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003468 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003469{
3470 struct btrfs_inode_item *inode_item;
3471 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003472 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003473 int ret;
3474
3475 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003476 if (!path)
3477 return -ENOMEM;
3478
Chris Masonb9473432009-03-13 11:00:37 -04003479 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003480 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3481 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003482 if (ret) {
3483 if (ret > 0)
3484 ret = -ENOENT;
3485 goto failed;
3486 }
3487
Chris Mason5f39d392007-10-15 16:14:19 -04003488 leaf = path->nodes[0];
3489 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003490 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003491
Chris Masone02119d2008-09-05 16:13:11 -04003492 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003493 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003494 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003495 ret = 0;
3496failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003497 btrfs_free_path(path);
3498 return ret;
3499}
3500
Chris Masond352ac62008-09-29 15:18:18 -04003501/*
Chris Mason21151332011-11-10 20:39:08 -05003502 * copy everything in the in-memory inode into the btree.
3503 */
3504noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3505 struct btrfs_root *root, struct inode *inode)
3506{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003507 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003508 int ret;
3509
3510 /*
3511 * If the inode is a free space inode, we can deadlock during commit
3512 * if we put it into the delayed code.
3513 *
3514 * The data relocation inode should also be directly updated
3515 * without delay
3516 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003517 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003518 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003519 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003520 btrfs_update_root_times(trans, root);
3521
Chris Mason21151332011-11-10 20:39:08 -05003522 ret = btrfs_delayed_update_inode(trans, root, inode);
3523 if (!ret)
3524 btrfs_set_inode_last_trans(trans, inode);
3525 return ret;
3526 }
3527
3528 return btrfs_update_inode_item(trans, root, inode);
3529}
3530
Josef Bacikbe6aef62012-10-22 15:43:12 -04003531noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3532 struct btrfs_root *root,
3533 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003534{
3535 int ret;
3536
3537 ret = btrfs_update_inode(trans, root, inode);
3538 if (ret == -ENOSPC)
3539 return btrfs_update_inode_item(trans, root, inode);
3540 return ret;
3541}
3542
3543/*
Chris Masond352ac62008-09-29 15:18:18 -04003544 * unlink helper that gets used here in inode.c and in the tree logging
3545 * recovery code. It remove a link in a directory with a given name, and
3546 * also drops the back refs in the inode to the directory
3547 */
Al Viro92986792011-03-04 17:14:37 +00003548static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3549 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003550 struct btrfs_inode *dir,
3551 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003552 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003553{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003554 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003555 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003556 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003557 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003558 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003559 u64 ino = btrfs_ino(inode);
3560 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003561
3562 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003563 if (!path) {
3564 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003565 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003566 }
3567
Chris Masonb9473432009-03-13 11:00:37 -04003568 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003569 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003570 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003571 if (IS_ERR_OR_NULL(di)) {
3572 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003573 goto err;
3574 }
Chris Mason39279cc2007-06-12 06:35:45 -04003575 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003576 if (ret)
3577 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003578 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003579
Miao Xie67de1172013-12-26 13:07:06 +08003580 /*
3581 * If we don't have dir index, we have to get it by looking up
3582 * the inode ref, since we get the inode ref, remove it directly,
3583 * it is unnecessary to do delayed deletion.
3584 *
3585 * But if we have dir index, needn't search inode ref to get it.
3586 * Since the inode ref is close to the inode item, it is better
3587 * that we delay to delete it, and just do this deletion when
3588 * we update the inode item.
3589 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003590 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003591 ret = btrfs_delayed_delete_inode_ref(inode);
3592 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003593 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003594 goto skip_backref;
3595 }
3596 }
3597
Li Zefan33345d012011-04-20 10:31:50 +08003598 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3599 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003600 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003601 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003602 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003603 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003604 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003605 goto err;
3606 }
Miao Xie67de1172013-12-26 13:07:06 +08003607skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003608 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003609 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003610 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003611 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003612 }
Chris Mason39279cc2007-06-12 06:35:45 -04003613
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003614 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3615 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003616 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003617 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003618 goto err;
3619 }
Chris Masone02119d2008-09-05 16:13:11 -04003620
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003621 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3622 index);
Chris Mason6418c962010-10-30 07:34:24 -04003623 if (ret == -ENOENT)
3624 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003625 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003626 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003627
3628 /*
3629 * If we have a pending delayed iput we could end up with the final iput
3630 * being run in btrfs-cleaner context. If we have enough of these built
3631 * up we can end up burning a lot of time in btrfs-cleaner without any
3632 * way to throttle the unlinks. Since we're currently holding a ref on
3633 * the inode we can run the delayed iput here without any issues as the
3634 * final iput won't be done until after we drop the ref we're currently
3635 * holding.
3636 */
3637 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003638err:
3639 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003640 if (ret)
3641 goto out;
3642
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003643 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003644 inode_inc_iversion(&inode->vfs_inode);
3645 inode_inc_iversion(&dir->vfs_inode);
3646 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3647 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3648 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003649out:
Chris Mason39279cc2007-06-12 06:35:45 -04003650 return ret;
3651}
3652
Al Viro92986792011-03-04 17:14:37 +00003653int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3654 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003655 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003656 const char *name, int name_len)
3657{
3658 int ret;
3659 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3660 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003661 drop_nlink(&inode->vfs_inode);
3662 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003663 }
3664 return ret;
3665}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003666
3667/*
3668 * helper to start transaction for unlink and rmdir.
3669 *
Josef Bacikd52be812013-05-29 14:54:47 -04003670 * unlink and rmdir are special in btrfs, they do not always free space, so
3671 * if we cannot make our reservations the normal way try and see if there is
3672 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3673 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003674 */
Josef Bacikd52be812013-05-29 14:54:47 -04003675static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003676{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003677 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678
Josef Bacike70bea52011-10-11 14:18:24 -04003679 /*
3680 * 1 for the possible orphan item
3681 * 1 for the dir item
3682 * 1 for the dir index
3683 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003684 * 1 for the inode
3685 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003686 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003687}
3688
Chris Mason39279cc2007-06-12 06:35:45 -04003689static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3690{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003691 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003692 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003693 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003694 int ret;
3695
Josef Bacikd52be812013-05-29 14:54:47 -04003696 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003697 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003698 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003699
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003700 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3701 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003702
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003703 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3704 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3705 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003706 if (ret)
3707 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003708
Yan, Zhenga22285a2010-05-16 10:48:46 -04003709 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003710 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003711 if (ret)
3712 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003713 }
Josef Bacik7b128762008-07-24 12:17:14 -04003714
Tsutomu Itohb5324022011-07-19 07:27:20 +00003715out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003716 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003717 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003718 return ret;
3719}
3720
Misono Tomohirof60a2362018-04-18 11:34:52 +09003721static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003722 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003723{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003724 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003725 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003726 struct btrfs_path *path;
3727 struct extent_buffer *leaf;
3728 struct btrfs_dir_item *di;
3729 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003730 const char *name = dentry->d_name.name;
3731 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003732 u64 index;
3733 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003734 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003735 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003736
Josef Bacik045d3962019-12-18 17:20:27 -05003737 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3738 objectid = inode->root->root_key.objectid;
3739 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3740 objectid = inode->location.objectid;
3741 } else {
3742 WARN_ON(1);
3743 return -EINVAL;
3744 }
3745
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003746 path = btrfs_alloc_path();
3747 if (!path)
3748 return -ENOMEM;
3749
Li Zefan33345d012011-04-20 10:31:50 +08003750 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003751 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003752 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003753 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754 goto out;
3755 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003756
3757 leaf = path->nodes[0];
3758 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3759 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3760 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003761 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003762 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003763 goto out;
3764 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003765 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003766
Josef Bacikd49d3282019-12-18 17:20:28 -05003767 /*
3768 * This is a placeholder inode for a subvolume we didn't have a
3769 * reference to at the time of the snapshot creation. In the meantime
3770 * we could have renamed the real subvol link into our snapshot, so
3771 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3772 * Instead simply lookup the dir_index_item for this entry so we can
3773 * remove it. Otherwise we know we have a ref to the root and we can
3774 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3775 */
3776 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003777 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003778 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003779 if (IS_ERR_OR_NULL(di)) {
3780 if (!di)
3781 ret = -ENOENT;
3782 else
3783 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003784 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003785 goto out;
3786 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003787
3788 leaf = path->nodes[0];
3789 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003790 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003791 btrfs_release_path(path);
3792 } else {
3793 ret = btrfs_del_root_ref(trans, objectid,
3794 root->root_key.objectid, dir_ino,
3795 &index, name, name_len);
3796 if (ret) {
3797 btrfs_abort_transaction(trans, ret);
3798 goto out;
3799 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003800 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003801
Lu Fengqi9add2942018-08-01 11:32:26 +08003802 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003803 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003804 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 goto out;
3806 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003807
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003808 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003809 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003810 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003811 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003812 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003813 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003814out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003815 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003816 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003817}
3818
Misono Tomohiroec42f162018-04-18 11:34:13 +09003819/*
3820 * Helper to check if the subvolume references other subvolumes or if it's
3821 * default.
3822 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003823static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003824{
3825 struct btrfs_fs_info *fs_info = root->fs_info;
3826 struct btrfs_path *path;
3827 struct btrfs_dir_item *di;
3828 struct btrfs_key key;
3829 u64 dir_id;
3830 int ret;
3831
3832 path = btrfs_alloc_path();
3833 if (!path)
3834 return -ENOMEM;
3835
3836 /* Make sure this root isn't set as the default subvol */
3837 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3838 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3839 dir_id, "default", 7, 0);
3840 if (di && !IS_ERR(di)) {
3841 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3842 if (key.objectid == root->root_key.objectid) {
3843 ret = -EPERM;
3844 btrfs_err(fs_info,
3845 "deleting default subvolume %llu is not allowed",
3846 key.objectid);
3847 goto out;
3848 }
3849 btrfs_release_path(path);
3850 }
3851
3852 key.objectid = root->root_key.objectid;
3853 key.type = BTRFS_ROOT_REF_KEY;
3854 key.offset = (u64)-1;
3855
3856 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3857 if (ret < 0)
3858 goto out;
3859 BUG_ON(ret == 0);
3860
3861 ret = 0;
3862 if (path->slots[0] > 0) {
3863 path->slots[0]--;
3864 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3865 if (key.objectid == root->root_key.objectid &&
3866 key.type == BTRFS_ROOT_REF_KEY)
3867 ret = -ENOTEMPTY;
3868 }
3869out:
3870 btrfs_free_path(path);
3871 return ret;
3872}
3873
Nikolay Borisov20a68002018-04-27 14:36:24 +03003874/* Delete all dentries for inodes belonging to the root */
3875static void btrfs_prune_dentries(struct btrfs_root *root)
3876{
3877 struct btrfs_fs_info *fs_info = root->fs_info;
3878 struct rb_node *node;
3879 struct rb_node *prev;
3880 struct btrfs_inode *entry;
3881 struct inode *inode;
3882 u64 objectid = 0;
3883
3884 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3885 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3886
3887 spin_lock(&root->inode_lock);
3888again:
3889 node = root->inode_tree.rb_node;
3890 prev = NULL;
3891 while (node) {
3892 prev = node;
3893 entry = rb_entry(node, struct btrfs_inode, rb_node);
3894
David Sterba37508512018-06-29 10:56:40 +02003895 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003896 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003897 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003898 node = node->rb_right;
3899 else
3900 break;
3901 }
3902 if (!node) {
3903 while (prev) {
3904 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003905 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003906 node = prev;
3907 break;
3908 }
3909 prev = rb_next(prev);
3910 }
3911 }
3912 while (node) {
3913 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003914 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003915 inode = igrab(&entry->vfs_inode);
3916 if (inode) {
3917 spin_unlock(&root->inode_lock);
3918 if (atomic_read(&inode->i_count) > 1)
3919 d_prune_aliases(inode);
3920 /*
3921 * btrfs_drop_inode will have it removed from the inode
3922 * cache when its usage count hits zero.
3923 */
3924 iput(inode);
3925 cond_resched();
3926 spin_lock(&root->inode_lock);
3927 goto again;
3928 }
3929
3930 if (cond_resched_lock(&root->inode_lock))
3931 goto again;
3932
3933 node = rb_next(node);
3934 }
3935 spin_unlock(&root->inode_lock);
3936}
3937
Misono Tomohirof60a2362018-04-18 11:34:52 +09003938int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3939{
3940 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3941 struct btrfs_root *root = BTRFS_I(dir)->root;
3942 struct inode *inode = d_inode(dentry);
3943 struct btrfs_root *dest = BTRFS_I(inode)->root;
3944 struct btrfs_trans_handle *trans;
3945 struct btrfs_block_rsv block_rsv;
3946 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003947 int ret;
3948 int err;
3949
3950 /*
3951 * Don't allow to delete a subvolume with send in progress. This is
3952 * inside the inode lock so the error handling that has to drop the bit
3953 * again is not run concurrently.
3954 */
3955 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003956 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003957 spin_unlock(&dest->root_item_lock);
3958 btrfs_warn(fs_info,
3959 "attempt to delete subvolume %llu during send",
3960 dest->root_key.objectid);
3961 return -EPERM;
3962 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003963 root_flags = btrfs_root_flags(&dest->root_item);
3964 btrfs_set_root_flags(&dest->root_item,
3965 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3966 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003967
3968 down_write(&fs_info->subvol_sem);
3969
3970 err = may_destroy_subvol(dest);
3971 if (err)
3972 goto out_up_write;
3973
3974 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3975 /*
3976 * One for dir inode,
3977 * two for dir entries,
3978 * two for root ref/backref.
3979 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003980 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003981 if (err)
3982 goto out_up_write;
3983
3984 trans = btrfs_start_transaction(root, 0);
3985 if (IS_ERR(trans)) {
3986 err = PTR_ERR(trans);
3987 goto out_release;
3988 }
3989 trans->block_rsv = &block_rsv;
3990 trans->bytes_reserved = block_rsv.size;
3991
3992 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3993
Josef Bacik045d3962019-12-18 17:20:27 -05003994 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003995 if (ret) {
3996 err = ret;
3997 btrfs_abort_transaction(trans, ret);
3998 goto out_end_trans;
3999 }
4000
4001 btrfs_record_root_in_trans(trans, dest);
4002
4003 memset(&dest->root_item.drop_progress, 0,
4004 sizeof(dest->root_item.drop_progress));
4005 dest->root_item.drop_level = 0;
4006 btrfs_set_root_refs(&dest->root_item, 0);
4007
4008 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4009 ret = btrfs_insert_orphan_item(trans,
4010 fs_info->tree_root,
4011 dest->root_key.objectid);
4012 if (ret) {
4013 btrfs_abort_transaction(trans, ret);
4014 err = ret;
4015 goto out_end_trans;
4016 }
4017 }
4018
Lu Fengqid1957792018-05-29 15:01:54 +08004019 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004020 BTRFS_UUID_KEY_SUBVOL,
4021 dest->root_key.objectid);
4022 if (ret && ret != -ENOENT) {
4023 btrfs_abort_transaction(trans, ret);
4024 err = ret;
4025 goto out_end_trans;
4026 }
4027 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004028 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004029 dest->root_item.received_uuid,
4030 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4031 dest->root_key.objectid);
4032 if (ret && ret != -ENOENT) {
4033 btrfs_abort_transaction(trans, ret);
4034 err = ret;
4035 goto out_end_trans;
4036 }
4037 }
4038
4039out_end_trans:
4040 trans->block_rsv = NULL;
4041 trans->bytes_reserved = 0;
4042 ret = btrfs_end_transaction(trans);
4043 if (ret && !err)
4044 err = ret;
4045 inode->i_flags |= S_DEAD;
4046out_release:
4047 btrfs_subvolume_release_metadata(fs_info, &block_rsv);
4048out_up_write:
4049 up_write(&fs_info->subvol_sem);
4050 if (err) {
4051 spin_lock(&dest->root_item_lock);
4052 root_flags = btrfs_root_flags(&dest->root_item);
4053 btrfs_set_root_flags(&dest->root_item,
4054 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4055 spin_unlock(&dest->root_item_lock);
4056 } else {
4057 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004058 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004059 ASSERT(dest->send_in_progress == 0);
4060
4061 /* the last ref */
4062 if (dest->ino_cache_inode) {
4063 iput(dest->ino_cache_inode);
4064 dest->ino_cache_inode = NULL;
4065 }
4066 }
4067
4068 return err;
4069}
4070
Chris Mason39279cc2007-06-12 06:35:45 -04004071static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4072{
David Howells2b0143b2015-03-17 22:25:59 +00004073 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004074 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004075 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004076 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004077 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004078
David Sterbab3ae2442012-09-13 16:04:34 -06004079 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004080 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004081 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004082 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004083
Josef Bacikd52be812013-05-29 14:54:47 -04004084 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004085 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004086 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004087
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004088 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004089 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004090 goto out;
4091 }
4092
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004093 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004094 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004095 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004096
Filipe Manana44f714d2016-06-06 16:11:13 +01004097 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4098
Chris Mason39279cc2007-06-12 06:35:45 -04004099 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004100 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4101 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4102 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004103 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004104 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004105 /*
4106 * Propagate the last_unlink_trans value of the deleted dir to
4107 * its parent directory. This is to prevent an unrecoverable
4108 * log tree in the case we do something like this:
4109 * 1) create dir foo
4110 * 2) create snapshot under dir foo
4111 * 3) delete the snapshot
4112 * 4) rmdir foo
4113 * 5) mkdir foo
4114 * 6) fsync foo or some file inside foo
4115 */
4116 if (last_unlink_trans >= trans->transid)
4117 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4118 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004119out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004120 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004121 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004122
Chris Mason39279cc2007-06-12 06:35:45 -04004123 return err;
4124}
4125
Josef Bacikddfae632017-10-19 14:16:02 -04004126/*
4127 * Return this if we need to call truncate_block for the last bit of the
4128 * truncate.
4129 */
4130#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004131
Chris Mason323ac952008-10-01 19:05:46 -04004132/*
Chris Mason39279cc2007-06-12 06:35:45 -04004133 * this can truncate away extent items, csum items and directory items.
4134 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004135 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004136 *
4137 * csum items that cross the new i_size are truncated to the new size
4138 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004139 *
4140 * min_type is the minimum key type to truncate down to. If set to 0, this
4141 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004142 */
Yan, Zheng80825102009-11-12 09:35:36 +00004143int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4144 struct btrfs_root *root,
4145 struct inode *inode,
4146 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004147{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004148 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004149 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004150 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004151 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004152 struct btrfs_key key;
4153 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004154 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004155 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004156 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004157 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004158 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004159 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004160 int found_extent;
4161 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004162 int pending_del_nr = 0;
4163 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004164 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004165 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004166 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004167 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004168 bool be_nice = false;
4169 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004170 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4171 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004172
4173 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004174
Chris Mason28ed1342014-12-17 09:41:04 -08004175 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004176 * For non-free space inodes and non-shareable roots, we want to back
4177 * off from time to time. This means all inodes in subvolume roots,
4178 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004179 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004180 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004181 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004182 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004183
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004184 path = btrfs_alloc_path();
4185 if (!path)
4186 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004187 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004188
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004189 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004190 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4191 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004192
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004193 /*
4194 * We want to drop from the next block forward in case this
4195 * new size is not block aligned since we will be keeping the
4196 * last block of the extent just the way it is.
4197 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004198 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004199 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004200 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004201 }
Yan, Zheng80825102009-11-12 09:35:36 +00004202
Miao Xie16cdcec2011-04-22 18:12:22 +08004203 /*
4204 * This function is also used to drop the items in the log tree before
4205 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004206 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004207 * items.
4208 */
4209 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004210 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004211
Li Zefan33345d012011-04-20 10:31:50 +08004212 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004213 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004214 key.type = (u8)-1;
4215
Chris Mason85e21ba2008-01-29 15:11:36 -05004216search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004217 /*
4218 * with a 16K leaf size and 128MB extents, you can actually queue
4219 * up a huge file in a single leaf. Most of the time that
4220 * bytes_deleted is > 0, it will be huge by the time we get here
4221 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004222 if (be_nice && bytes_deleted > SZ_32M &&
4223 btrfs_should_end_transaction(trans)) {
4224 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004225 goto out;
4226 }
Chris Masond3977122009-01-05 21:25:51 -05004227
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004228 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4229 if (ret < 0)
4230 goto out;
4231
Chris Mason85e21ba2008-01-29 15:11:36 -05004232 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004233 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004234 /* there are no items in the tree for us to truncate, we're
4235 * done
4236 */
Yan, Zheng80825102009-11-12 09:35:36 +00004237 if (path->slots[0] == 0)
4238 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004239 path->slots[0]--;
4240 }
4241
Chris Masond3977122009-01-05 21:25:51 -05004242 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004243 u64 clear_start = 0, clear_len = 0;
4244
Chris Mason39279cc2007-06-12 06:35:45 -04004245 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004246 leaf = path->nodes[0];
4247 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004248 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004249
Li Zefan33345d012011-04-20 10:31:50 +08004250 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004251 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004252
Chris Mason85e21ba2008-01-29 15:11:36 -05004253 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004254 break;
4255
Chris Mason5f39d392007-10-15 16:14:19 -04004256 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004257 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004258 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004259 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004260 extent_type = btrfs_file_extent_type(leaf, fi);
4261 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004262 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004263 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004264
4265 trace_btrfs_truncate_show_fi_regular(
4266 BTRFS_I(inode), leaf, fi,
4267 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004268 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004269 item_end += btrfs_file_extent_ram_bytes(leaf,
4270 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004271
4272 trace_btrfs_truncate_show_fi_inline(
4273 BTRFS_I(inode), leaf, fi, path->slots[0],
4274 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004275 }
Yan008630c2007-11-07 13:31:09 -05004276 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004277 }
Yan, Zheng80825102009-11-12 09:35:36 +00004278 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004279 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004280 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004281 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004282 break;
4283 if (found_key.offset >= new_size)
4284 del_item = 1;
4285 else
4286 del_item = 0;
4287 }
Chris Mason39279cc2007-06-12 06:35:45 -04004288 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004289 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004290 if (found_type != BTRFS_EXTENT_DATA_KEY)
4291 goto delete;
4292
4293 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004294 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004295
4296 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004297 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004298 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004299 u64 orig_num_bytes =
4300 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004301 extent_num_bytes = ALIGN(new_size -
4302 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004303 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004304 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004305 btrfs_set_file_extent_num_bytes(leaf, fi,
4306 extent_num_bytes);
4307 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004308 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004309 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004310 &root->state) &&
4311 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004312 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004313 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004314 } else {
Chris Masondb945352007-10-15 16:15:53 -04004315 extent_num_bytes =
4316 btrfs_file_extent_disk_num_bytes(leaf,
4317 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004318 extent_offset = found_key.offset -
4319 btrfs_file_extent_offset(leaf, fi);
4320
Chris Mason39279cc2007-06-12 06:35:45 -04004321 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004322 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004323 if (extent_start != 0) {
4324 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004325 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004326 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004327 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004328 }
4329 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004330 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004331 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004332 /*
4333 * we can't truncate inline items that have had
4334 * special encodings
4335 */
4336 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004337 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004338 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4339 btrfs_file_extent_compression(leaf, fi) == 0) {
4340 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004341
Josef Bacikddfae632017-10-19 14:16:02 -04004342 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4343 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004344 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004345 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004346 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004347 * We have to bail so the last_size is set to
4348 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004349 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004350 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004351 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004352 } else {
4353 /*
4354 * Inline extents are special, we just treat
4355 * them as a full sector worth in the file
4356 * extent tree just for simplicity sake.
4357 */
4358 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004359 }
Josef Bacikddfae632017-10-19 14:16:02 -04004360
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004361 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004362 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004363 }
Chris Mason179e29e2007-11-01 11:28:41 -04004364delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004365 /*
4366 * We use btrfs_truncate_inode_items() to clean up log trees for
4367 * multiple fsyncs, and in this case we don't want to clear the
4368 * file extent range because it's just the log.
4369 */
4370 if (root == BTRFS_I(inode)->root) {
4371 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4372 clear_start, clear_len);
4373 if (ret) {
4374 btrfs_abort_transaction(trans, ret);
4375 break;
4376 }
4377 }
4378
Josef Bacikddfae632017-10-19 14:16:02 -04004379 if (del_item)
4380 last_size = found_key.offset;
4381 else
4382 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004383 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004384 if (!pending_del_nr) {
4385 /* no pending yet, add ourselves */
4386 pending_del_slot = path->slots[0];
4387 pending_del_nr = 1;
4388 } else if (pending_del_nr &&
4389 path->slots[0] + 1 == pending_del_slot) {
4390 /* hop on the pending chunk */
4391 pending_del_nr++;
4392 pending_del_slot = path->slots[0];
4393 } else {
Chris Masond3977122009-01-05 21:25:51 -05004394 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004395 }
Chris Mason39279cc2007-06-12 06:35:45 -04004396 } else {
4397 break;
4398 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004399 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004400
Miao Xie27cdeb72014-04-02 19:51:05 +08004401 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004402 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004403 struct btrfs_ref ref = { 0 };
4404
Chris Mason28ed1342014-12-17 09:41:04 -08004405 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004406
4407 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4408 extent_start, extent_num_bytes, 0);
4409 ref.real_root = root->root_key.objectid;
4410 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4411 ino, extent_offset);
4412 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004413 if (ret) {
4414 btrfs_abort_transaction(trans, ret);
4415 break;
4416 }
Chris Mason28f75a02015-02-04 06:59:29 -08004417 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004418 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004419 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004420 }
Chris Mason39279cc2007-06-12 06:35:45 -04004421 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004422
Yan, Zheng80825102009-11-12 09:35:36 +00004423 if (found_type == BTRFS_INODE_ITEM_KEY)
4424 break;
4425
4426 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004427 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004428 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004429 if (pending_del_nr) {
4430 ret = btrfs_del_items(trans, root, path,
4431 pending_del_slot,
4432 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004433 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004434 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004435 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004436 }
Yan, Zheng80825102009-11-12 09:35:36 +00004437 pending_del_nr = 0;
4438 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004439 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004440
Chris Mason28f75a02015-02-04 06:59:29 -08004441 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004442 * We can generate a lot of delayed refs, so we need to
4443 * throttle every once and a while and make sure we're
4444 * adding enough space to keep up with the work we are
4445 * generating. Since we hold a transaction here we
4446 * can't flush, and we don't want to FLUSH_LIMIT because
4447 * we could have generated too many delayed refs to
4448 * actually allocate, so just bail if we're short and
4449 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004450 */
Josef Bacik28bad212018-12-03 10:20:38 -05004451 if (should_throttle) {
4452 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4453 BTRFS_RESERVE_NO_FLUSH);
4454 if (ret) {
4455 ret = -EAGAIN;
4456 break;
4457 }
Chris Mason28f75a02015-02-04 06:59:29 -08004458 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004459 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004460 } else {
4461 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004462 }
Chris Mason39279cc2007-06-12 06:35:45 -04004463 }
Yan, Zheng80825102009-11-12 09:35:36 +00004464out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004465 if (ret >= 0 && pending_del_nr) {
4466 int err;
4467
4468 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004469 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004470 if (err) {
4471 btrfs_abort_transaction(trans, err);
4472 ret = err;
4473 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004474 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004475 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4476 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004477 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004478 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004479 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004480 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4481 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004482 }
Chris Mason28ed1342014-12-17 09:41:04 -08004483
Chris Mason39279cc2007-06-12 06:35:45 -04004484 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004485 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004486}
4487
Chris Masona52d9a82007-08-27 16:49:44 -04004488/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304489 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004490 * @inode - inode that we're zeroing
4491 * @from - the offset to start zeroing
4492 * @len - the length to zero, 0 to zero the entire range respective to the
4493 * offset
4494 * @front - zero up to the offset instead of from the offset on
4495 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304496 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004497 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004498 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304499int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004500 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004501{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004502 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004503 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004504 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4505 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004506 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004507 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004508 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004509 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004510 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004511 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304512 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004513 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004514 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004515 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004516 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304517 u64 block_start;
4518 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004519
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004520 if (IS_ALIGNED(offset, blocksize) &&
4521 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004522 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304523
Josef Bacik8b62f872017-10-19 14:15:55 -04004524 block_start = round_down(from, blocksize);
4525 block_end = block_start + blocksize - 1;
4526
Chris Masona52d9a82007-08-27 16:49:44 -04004527
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004528 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4529 blocksize);
4530 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004531 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4532 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004533 /* For nocow case, no need to reserve data space */
4534 only_release_metadata = true;
4535 } else {
4536 goto out;
4537 }
4538 }
4539 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4540 if (ret < 0) {
4541 if (!only_release_metadata)
4542 btrfs_free_reserved_data_space(inode, data_reserved,
4543 block_start, blocksize);
4544 goto out;
4545 }
Chris Mason211c17f2008-05-15 09:13:45 -04004546again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004547 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004548 if (!page) {
Qu Wenruobc42bda2017-02-27 15:10:39 +08004549 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004550 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004551 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004552 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004553 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004554 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004555
Chris Masona52d9a82007-08-27 16:49:44 -04004556 if (!PageUptodate(page)) {
4557 ret = btrfs_readpage(NULL, page);
4558 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004559 if (page->mapping != mapping) {
4560 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004561 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004562 goto again;
4563 }
Chris Masona52d9a82007-08-27 16:49:44 -04004564 if (!PageUptodate(page)) {
4565 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004566 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004567 }
4568 }
Chris Mason211c17f2008-05-15 09:13:45 -04004569 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004570
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304571 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004572 set_page_extent_mapped(page);
4573
Nikolay Borisovc3504372020-06-03 08:55:03 +03004574 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004575 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304576 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004577 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004578 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004579 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004580 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004581 btrfs_put_ordered_extent(ordered);
4582 goto again;
4583 }
4584
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304585 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004586 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4587 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004588
Filipe Mananae3b8a482017-11-04 00:16:59 +00004589 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004590 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004591 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304592 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004593 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004594 goto out_unlock;
4595 }
4596
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304597 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004598 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304599 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004600 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004601 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304602 memset(kaddr + (block_start - page_offset(page)),
4603 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004604 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304605 memset(kaddr + (block_start - page_offset(page)) + offset,
4606 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004607 flush_dcache_page(page);
4608 kunmap(page);
4609 }
Chris Mason247e7432008-07-17 12:53:51 -04004610 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004611 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004612 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004613
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004614 if (only_release_metadata)
4615 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4616 block_end, EXTENT_NORESERVE, NULL, NULL,
4617 GFP_NOFS);
4618
Chris Mason89642222008-07-24 09:41:53 -04004619out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004620 if (ret) {
4621 if (only_release_metadata)
4622 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4623 blocksize, true);
4624 else
4625 btrfs_delalloc_release_space(inode, data_reserved,
4626 block_start, blocksize, true);
4627 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004628 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004629 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004630 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004631out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004632 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004633 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004634 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004635 return ret;
4636}
4637
Josef Bacik16e75492013-10-22 12:18:51 -04004638static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4639 u64 offset, u64 len)
4640{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004641 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004642 struct btrfs_trans_handle *trans;
4643 int ret;
4644
4645 /*
4646 * Still need to make sure the inode looks like it's been updated so
4647 * that any holes get logged if we fsync.
4648 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004649 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4650 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004651 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4652 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4653 return 0;
4654 }
4655
4656 /*
4657 * 1 - for the one we're dropping
4658 * 1 - for the one we're adding
4659 * 1 - for updating the inode.
4660 */
4661 trans = btrfs_start_transaction(root, 3);
4662 if (IS_ERR(trans))
4663 return PTR_ERR(trans);
4664
4665 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4666 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004667 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004668 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004669 return ret;
4670 }
4671
David Sterbaf85b7372017-01-20 14:54:07 +01004672 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4673 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004674 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004675 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004676 else
4677 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004678 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004679 return ret;
4680}
4681
Josef Bacik695a0d02011-03-04 15:46:53 -05004682/*
4683 * This function puts in dummy file extents for the area we're creating a hole
4684 * for. So if we are truncating this file to a larger size we need to insert
4685 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4686 * the range between oldsize and size
4687 */
Josef Bacika41ad392011-01-31 15:30:16 -05004688int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004689{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004690 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004691 struct btrfs_root *root = BTRFS_I(inode)->root;
4692 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004693 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004694 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004695 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004696 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4697 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004698 u64 last_byte;
4699 u64 cur_offset;
4700 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004701 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004702
Josef Bacika71754f2013-06-17 17:14:39 -04004703 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304704 * If our size started in the middle of a block we need to zero out the
4705 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004706 * expose stale data.
4707 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304708 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004709 if (err)
4710 return err;
4711
Yan Zheng9036c102008-10-30 14:19:41 -04004712 if (size <= hole_start)
4713 return 0;
4714
David Sterbab272ae22020-02-05 19:09:33 +01004715 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004716 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004717 cur_offset = hole_start;
4718 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004719 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004720 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004721 if (IS_ERR(em)) {
4722 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004723 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004724 break;
4725 }
Yan Zheng9036c102008-10-30 14:19:41 -04004726 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004727 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004728 hole_size = last_byte - cur_offset;
4729
Yan, Zheng80825102009-11-12 09:35:36 +00004730 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004731 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004732
Josef Bacik16e75492013-10-22 12:18:51 -04004733 err = maybe_insert_hole(root, inode, cur_offset,
4734 hole_size);
4735 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004736 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004737
4738 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4739 cur_offset, hole_size);
4740 if (err)
4741 break;
4742
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004743 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004744 cur_offset + hole_size - 1, 0);
4745 hole_em = alloc_extent_map();
4746 if (!hole_em) {
4747 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4748 &BTRFS_I(inode)->runtime_flags);
4749 goto next;
4750 }
4751 hole_em->start = cur_offset;
4752 hole_em->len = hole_size;
4753 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004754
Josef Bacik5dc562c2012-08-17 13:14:17 -04004755 hole_em->block_start = EXTENT_MAP_HOLE;
4756 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004757 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004758 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004759 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004760 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004761
4762 while (1) {
4763 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004764 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004765 write_unlock(&em_tree->lock);
4766 if (err != -EEXIST)
4767 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004768 btrfs_drop_extent_cache(BTRFS_I(inode),
4769 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004770 cur_offset +
4771 hole_size - 1, 0);
4772 }
4773 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004774 } else {
4775 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4776 cur_offset, hole_size);
4777 if (err)
4778 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004779 }
Josef Bacik16e75492013-10-22 12:18:51 -04004780next:
Yan Zheng9036c102008-10-30 14:19:41 -04004781 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004782 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004783 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004784 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004785 break;
4786 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004787 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004788 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004789 return err;
4790}
4791
Eric Sandeen3972f262013-01-12 02:57:22 +00004792static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004793{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004794 struct btrfs_root *root = BTRFS_I(inode)->root;
4795 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004796 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004797 loff_t newsize = attr->ia_size;
4798 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004799 int ret;
4800
Eric Sandeen3972f262013-01-12 02:57:22 +00004801 /*
4802 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4803 * special case where we need to update the times despite not having
4804 * these flags set. For all other operations the VFS set these flags
4805 * explicitly if it wants a timestamp update.
4806 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004807 if (newsize != oldsize) {
4808 inode_inc_iversion(inode);
4809 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4810 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004811 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004812 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004813
Josef Bacika41ad392011-01-31 15:30:16 -05004814 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004815 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004816 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004817 * This is to ensure the snapshot captures a fully consistent
4818 * state of this file - if the snapshot captures this expanding
4819 * truncation, it must capture all writes that happened before
4820 * this truncation.
4821 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004822 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004823 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004824 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004825 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004826 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004827 }
Yan, Zheng80825102009-11-12 09:35:36 +00004828
Miao Xief4a2f4c2011-12-14 20:12:01 -05004829 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004830 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004831 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004832 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004833 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004834
4835 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004836 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304837 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004838 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004839 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004840 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004841 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004842
Josef Bacika41ad392011-01-31 15:30:16 -05004843 /*
4844 * We're truncating a file that used to have good data down to
4845 * zero. Make sure it gets into the ordered flush list so that
4846 * any new writes get down to disk quickly.
4847 */
4848 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004849 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4850 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004851
Josef Bacika41ad392011-01-31 15:30:16 -05004852 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004853
David Sterba8e0fa5d2020-06-09 19:21:48 +02004854 /* Disable nonlocked read DIO to avoid the endless truncate */
4855 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004856 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004857 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004858
Filipe Manana213e8c52018-02-06 20:40:31 +00004859 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004860 if (ret && inode->i_nlink) {
4861 int err;
4862
4863 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004864 * Truncate failed, so fix up the in-memory size. We
4865 * adjusted disk_i_size down as we removed extents, so
4866 * wait for disk_i_size to be stable and then update the
4867 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004868 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004869 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004870 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004871 return err;
4872 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004873 }
Yan, Zheng80825102009-11-12 09:35:36 +00004874 }
4875
Josef Bacika41ad392011-01-31 15:30:16 -05004876 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004877}
4878
Chris Mason39279cc2007-06-12 06:35:45 -04004879static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4880{
David Howells2b0143b2015-03-17 22:25:59 +00004881 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004882 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004883 int err;
4884
Li Zefanb83cc962010-12-20 16:04:08 +08004885 if (btrfs_root_readonly(root))
4886 return -EROFS;
4887
Jan Kara31051c82016-05-26 16:55:18 +02004888 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004889 if (err)
4890 return err;
4891
Chris Mason5a3f23d2009-03-31 13:27:11 -04004892 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004893 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004894 if (err)
4895 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004896 }
Yan Zheng9036c102008-10-30 14:19:41 -04004897
Christoph Hellwig10257742010-06-04 11:30:02 +02004898 if (attr->ia_valid) {
4899 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004900 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004901 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004902
Josef Bacik22c44fe2011-11-30 10:45:38 -05004903 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004904 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004905 }
4906
Chris Mason39279cc2007-06-12 06:35:45 -04004907 return err;
4908}
Chris Mason61295eb2008-01-14 16:24:38 -05004909
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004910/*
4911 * While truncating the inode pages during eviction, we get the VFS calling
4912 * btrfs_invalidatepage() against each page of the inode. This is slow because
4913 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4914 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4915 * extent_state structures over and over, wasting lots of time.
4916 *
4917 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4918 * those expensive operations on a per page basis and do only the ordered io
4919 * finishing, while we release here the extent_map and extent_state structures,
4920 * without the excessive merging and splitting.
4921 */
4922static void evict_inode_truncate_pages(struct inode *inode)
4923{
4924 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4925 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4926 struct rb_node *node;
4927
4928 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004929 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004930
4931 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004932 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004933 struct extent_map *em;
4934
Liu Bo07e1ce02018-08-23 03:51:52 +08004935 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004936 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004937 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4938 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004939 remove_extent_mapping(map_tree, em);
4940 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004941 if (need_resched()) {
4942 write_unlock(&map_tree->lock);
4943 cond_resched();
4944 write_lock(&map_tree->lock);
4945 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004946 }
4947 write_unlock(&map_tree->lock);
4948
Filipe Manana6ca07092015-05-26 00:55:42 +01004949 /*
4950 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004951 * We can have ongoing bios started by readahead that have
4952 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004953 * still in progress (unlocked the pages in the bio but did not yet
4954 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004955 * ranges can still be locked and eviction started because before
4956 * submitting those bios, which are executed by a separate task (work
4957 * queue kthread), inode references (inode->i_count) were not taken
4958 * (which would be dropped in the end io callback of each bio).
4959 * Therefore here we effectively end up waiting for those bios and
4960 * anyone else holding locked ranges without having bumped the inode's
4961 * reference count - if we don't do it, when they access the inode's
4962 * io_tree to unlock a range it may be too late, leading to an
4963 * use-after-free issue.
4964 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004965 spin_lock(&io_tree->lock);
4966 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4967 struct extent_state *state;
4968 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004969 u64 start;
4970 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004971 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004972
4973 node = rb_first(&io_tree->state);
4974 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004975 start = state->start;
4976 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004977 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004978 spin_unlock(&io_tree->lock);
4979
David Sterbaff13db42015-12-03 14:30:40 +01004980 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004981
4982 /*
4983 * If still has DELALLOC flag, the extent didn't reach disk,
4984 * and its reserved space won't be freed by delayed_ref.
4985 * So we need to free its reserved space here.
4986 * (Refer to comment in btrfs_invalidatepage, case 2)
4987 *
4988 * Note, end is the bytenr of last byte, so we need + 1 here.
4989 */
Filipe Manana421f0922018-10-12 13:02:48 +01004990 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03004991 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
4992 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004993
Filipe Manana6ca07092015-05-26 00:55:42 +01004994 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004995 EXTENT_LOCKED | EXTENT_DELALLOC |
4996 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4997 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004998
Filipe Manana7064dd52014-08-08 02:47:05 +01004999 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005000 spin_lock(&io_tree->lock);
5001 }
5002 spin_unlock(&io_tree->lock);
5003}
5004
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005005static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005006 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005007{
5008 struct btrfs_fs_info *fs_info = root->fs_info;
5009 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005010 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005011 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005012 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005013
Josef Bacikd3984c92019-08-01 18:19:37 -04005014 /*
5015 * Eviction should be taking place at some place safe because of our
5016 * delayed iputs. However the normal flushing code will run delayed
5017 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5018 *
5019 * We reserve the delayed_refs_extra here again because we can't use
5020 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5021 * above. We reserve our extra bit here because we generate a ton of
5022 * delayed refs activity by truncating.
5023 *
5024 * If we cannot make our reservation we'll attempt to steal from the
5025 * global reserve, because we really want to be able to free up space.
5026 */
5027 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5028 BTRFS_RESERVE_FLUSH_EVICT);
5029 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005030 /*
5031 * Try to steal from the global reserve if there is space for
5032 * it.
5033 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005034 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5035 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5036 btrfs_warn(fs_info,
5037 "could not allocate space for delete; will truncate on mount");
5038 return ERR_PTR(-ENOSPC);
5039 }
5040 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005041 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005042
5043 trans = btrfs_join_transaction(root);
5044 if (IS_ERR(trans))
5045 return trans;
5046
5047 if (delayed_refs_extra) {
5048 trans->block_rsv = &fs_info->trans_block_rsv;
5049 trans->bytes_reserved = delayed_refs_extra;
5050 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5051 delayed_refs_extra, 1);
5052 }
5053 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005054}
5055
Al Virobd555972010-06-07 11:35:40 -04005056void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005057{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005058 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005059 struct btrfs_trans_handle *trans;
5060 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005061 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005062 int ret;
5063
liubo1abe9b82011-03-24 11:18:59 +00005064 trace_btrfs_inode_evict(inode);
5065
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005066 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005067 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005068 return;
5069 }
5070
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005071 evict_inode_truncate_pages(inode);
5072
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005073 if (inode->i_nlink &&
5074 ((btrfs_root_refs(&root->root_item) != 0 &&
5075 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005076 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005077 goto no_delete;
5078
Omar Sandoval27919062018-05-11 13:13:37 -07005079 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005080 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005081
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005082 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005083
Omar Sandoval7b40b692018-05-11 13:13:33 -07005084 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005085 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005086
Yan, Zheng76dda932009-09-21 16:00:26 -04005087 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005088 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5089 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005090 goto no_delete;
5091 }
5092
Nikolay Borisovaa790212017-01-10 20:35:40 +02005093 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005094 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005095 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005096
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005097 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005098 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005099 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005100 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005101 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005102
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005103 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005104
Yan, Zheng80825102009-11-12 09:35:36 +00005105 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005106 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005107 if (IS_ERR(trans))
5108 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005109
5110 trans->block_rsv = rsv;
5111
Yan, Zhengd68fc572010-05-16 10:49:58 -04005112 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005113 trans->block_rsv = &fs_info->trans_block_rsv;
5114 btrfs_end_transaction(trans);
5115 btrfs_btree_balance_dirty(fs_info);
5116 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5117 goto free_rsv;
5118 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005119 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005120 }
5121
Josef Bacik4ef31a42013-08-13 14:10:08 -04005122 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005123 * Errors here aren't a big deal, it just means we leave orphan items in
5124 * the tree. They will be cleaned up on the next mount. If the inode
5125 * number gets reused, cleanup deletes the orphan item without doing
5126 * anything, and unlink reuses the existing orphan item.
5127 *
5128 * If it turns out that we are dropping too many of these, we might want
5129 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005130 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005131 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005132 if (!IS_ERR(trans)) {
5133 trans->block_rsv = rsv;
5134 btrfs_orphan_del(trans, BTRFS_I(inode));
5135 trans->block_rsv = &fs_info->trans_block_rsv;
5136 btrfs_end_transaction(trans);
5137 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005138
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005139 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005140 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005141 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005142
Omar Sandoval27919062018-05-11 13:13:37 -07005143free_rsv:
5144 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005145no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005146 /*
5147 * If we didn't successfully delete, the orphan item will still be in
5148 * the tree and we'll retry on the next mount. Again, we might also want
5149 * to retry these periodically in the future.
5150 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005151 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005152 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005153}
5154
5155/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005156 * Return the key found in the dir entry in the location pointer, fill @type
5157 * with BTRFS_FT_*, and return 0.
5158 *
Su Yue005d6712018-03-05 17:13:37 +08005159 * If no dir entries were found, returns -ENOENT.
5160 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005161 */
5162static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005163 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005164{
5165 const char *name = dentry->d_name.name;
5166 int namelen = dentry->d_name.len;
5167 struct btrfs_dir_item *di;
5168 struct btrfs_path *path;
5169 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005170 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005171
5172 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005173 if (!path)
5174 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005175
David Sterbaf85b7372017-01-20 14:54:07 +01005176 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5177 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005178 if (IS_ERR_OR_NULL(di)) {
5179 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005180 goto out;
5181 }
Chris Masond3977122009-01-05 21:25:51 -05005182
Chris Mason5f39d392007-10-15 16:14:19 -04005183 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005184 if (location->type != BTRFS_INODE_ITEM_KEY &&
5185 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005186 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005187 btrfs_warn(root->fs_info,
5188"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5189 __func__, name, btrfs_ino(BTRFS_I(dir)),
5190 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005191 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005192 if (!ret)
5193 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005194out:
Chris Mason39279cc2007-06-12 06:35:45 -04005195 btrfs_free_path(path);
5196 return ret;
5197}
5198
5199/*
5200 * when we hit a tree root in a directory, the btrfs part of the inode
5201 * needs to be changed to reflect the root directory of the tree root. This
5202 * is kind of like crossing a mount point.
5203 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005204static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005205 struct inode *dir,
5206 struct dentry *dentry,
5207 struct btrfs_key *location,
5208 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005209{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005210 struct btrfs_path *path;
5211 struct btrfs_root *new_root;
5212 struct btrfs_root_ref *ref;
5213 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005214 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005215 int ret;
5216 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005217
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005218 path = btrfs_alloc_path();
5219 if (!path) {
5220 err = -ENOMEM;
5221 goto out;
5222 }
Chris Mason39279cc2007-06-12 06:35:45 -04005223
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005224 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005225 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5226 key.type = BTRFS_ROOT_REF_KEY;
5227 key.offset = location->objectid;
5228
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005229 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005230 if (ret) {
5231 if (ret < 0)
5232 err = ret;
5233 goto out;
5234 }
Chris Mason39279cc2007-06-12 06:35:45 -04005235
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005236 leaf = path->nodes[0];
5237 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005238 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005239 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5240 goto out;
5241
5242 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5243 (unsigned long)(ref + 1),
5244 dentry->d_name.len);
5245 if (ret)
5246 goto out;
5247
David Sterbab3b4aa72011-04-21 01:20:15 +02005248 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005249
David Sterba56e93572020-05-15 19:35:55 +02005250 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005251 if (IS_ERR(new_root)) {
5252 err = PTR_ERR(new_root);
5253 goto out;
5254 }
5255
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005256 *sub_root = new_root;
5257 location->objectid = btrfs_root_dirid(&new_root->root_item);
5258 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005259 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005260 err = 0;
5261out:
5262 btrfs_free_path(path);
5263 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005264}
5265
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005266static void inode_tree_add(struct inode *inode)
5267{
5268 struct btrfs_root *root = BTRFS_I(inode)->root;
5269 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005270 struct rb_node **p;
5271 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005272 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005273 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005274
Al Viro1d3382cb2010-10-23 15:19:20 -04005275 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005276 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005277 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005278 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005279 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005280 while (*p) {
5281 parent = *p;
5282 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5283
David Sterba37508512018-06-29 10:56:40 +02005284 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005285 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005286 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005287 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005288 else {
5289 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005290 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005291 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005292 RB_CLEAR_NODE(parent);
5293 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005294 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005295 }
5296 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005297 rb_link_node(new, parent, p);
5298 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005299 spin_unlock(&root->inode_lock);
5300}
5301
5302static void inode_tree_del(struct inode *inode)
5303{
5304 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005305 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005306
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005307 spin_lock(&root->inode_lock);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005308 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005309 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005310 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005311 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005312 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005313 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005314
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005315 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005316 spin_lock(&root->inode_lock);
5317 empty = RB_EMPTY_ROOT(&root->inode_tree);
5318 spin_unlock(&root->inode_lock);
5319 if (empty)
5320 btrfs_add_dead_root(root);
5321 }
5322}
5323
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005324
Chris Masone02119d2008-09-05 16:13:11 -04005325static int btrfs_init_locked_inode(struct inode *inode, void *p)
5326{
5327 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005328
5329 inode->i_ino = args->ino;
5330 BTRFS_I(inode)->location.objectid = args->ino;
5331 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5332 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005333 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5334 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005335 return 0;
5336}
5337
5338static int btrfs_find_actor(struct inode *inode, void *opaque)
5339{
5340 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005341
5342 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005343 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005344}
5345
David Sterba0202e832020-05-15 19:35:59 +02005346static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005347 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005348{
5349 struct inode *inode;
5350 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005351 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005352
David Sterba0202e832020-05-15 19:35:59 +02005353 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005354 args.root = root;
5355
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005356 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005357 btrfs_init_locked_inode,
5358 (void *)&args);
5359 return inode;
5360}
5361
David Sterba4c66e0d2019-10-03 19:09:35 +02005362/*
David Sterba0202e832020-05-15 19:35:59 +02005363 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005364 * Path can be preallocated to prevent recursing back to iget through
5365 * allocator. NULL is also valid but may require an additional allocation
5366 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305367 */
David Sterba0202e832020-05-15 19:35:59 +02005368struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005369 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305370{
5371 struct inode *inode;
5372
David Sterba0202e832020-05-15 19:35:59 +02005373 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305374 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005375 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305376
5377 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005378 int ret;
5379
Filipe Manana4222ea72018-10-24 10:13:03 +01005380 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005381 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005382 inode_tree_add(inode);
5383 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005384 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005385 iget_failed(inode);
5386 /*
5387 * ret > 0 can come from btrfs_search_slot called by
5388 * btrfs_read_locked_inode, this means the inode item
5389 * was not found.
5390 */
5391 if (ret > 0)
5392 ret = -ENOENT;
5393 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005394 }
5395 }
5396
Balaji Rao1a54ef82008-07-21 02:01:04 +05305397 return inode;
5398}
5399
David Sterba0202e832020-05-15 19:35:59 +02005400struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005401{
David Sterba0202e832020-05-15 19:35:59 +02005402 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005403}
5404
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005405static struct inode *new_simple_dir(struct super_block *s,
5406 struct btrfs_key *key,
5407 struct btrfs_root *root)
5408{
5409 struct inode *inode = new_inode(s);
5410
5411 if (!inode)
5412 return ERR_PTR(-ENOMEM);
5413
Josef Bacik5c8fd992020-02-14 16:11:43 -05005414 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005415 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005416 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005417
5418 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005419 /*
5420 * We only need lookup, the rest is read-only and there's no inode
5421 * associated with the dentry
5422 */
5423 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005424 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005425 inode->i_fop = &simple_dir_operations;
5426 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005427 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305428 inode->i_atime = inode->i_mtime;
5429 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005430 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005431
5432 return inode;
5433}
5434
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005435static inline u8 btrfs_inode_type(struct inode *inode)
5436{
5437 /*
5438 * Compile-time asserts that generic FT_* types still match
5439 * BTRFS_FT_* types
5440 */
5441 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5442 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5443 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5444 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5445 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5446 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5447 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5448 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5449
5450 return fs_umode_to_ftype(inode->i_mode);
5451}
5452
Chris Mason3de45862008-11-17 21:02:50 -05005453struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005454{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005455 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005456 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005457 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005458 struct btrfs_root *sub_root = root;
5459 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005460 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005461 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005462
5463 if (dentry->d_name.len > BTRFS_NAME_LEN)
5464 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005465
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005466 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005467 if (ret < 0)
5468 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005469
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005470 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005471 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005472 if (IS_ERR(inode))
5473 return inode;
5474
5475 /* Do extra check against inode mode with di_type */
5476 if (btrfs_inode_type(inode) != di_type) {
5477 btrfs_crit(fs_info,
5478"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5479 inode->i_mode, btrfs_inode_type(inode),
5480 di_type);
5481 iput(inode);
5482 return ERR_PTR(-EUCLEAN);
5483 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005484 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005485 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005487 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005488 &location, &sub_root);
5489 if (ret < 0) {
5490 if (ret != -ENOENT)
5491 inode = ERR_PTR(ret);
5492 else
5493 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5494 } else {
David Sterba0202e832020-05-15 19:35:59 +02005495 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005496 }
Josef Bacik87270022020-01-24 09:32:31 -05005497 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005498 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005499
Julia Lawall34d19ba2011-01-24 19:55:19 +00005500 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005501 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005502 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005503 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005504 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005505 if (ret) {
5506 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005507 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005508 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005509 }
5510
Chris Mason3de45862008-11-17 21:02:50 -05005511 return inode;
5512}
5513
Nick Pigginfe15ce42011-01-07 17:49:23 +11005514static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005515{
5516 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005517 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005518
Li Zefan848cce02012-02-21 17:04:28 +08005519 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005520 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005521
Li Zefan848cce02012-02-21 17:04:28 +08005522 if (inode) {
5523 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005524 if (btrfs_root_refs(&root->root_item) == 0)
5525 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005526
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005527 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005528 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005529 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005530 return 0;
5531}
5532
Chris Mason3de45862008-11-17 21:02:50 -05005533static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005534 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005535{
Al Viro3837d202018-10-10 16:38:27 -04005536 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005537
Al Viro3837d202018-10-10 16:38:27 -04005538 if (inode == ERR_PTR(-ENOENT))
5539 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005540 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005541}
5542
Josef Bacik23b5ec72017-07-24 15:14:25 -04005543/*
5544 * All this infrastructure exists because dir_emit can fault, and we are holding
5545 * the tree lock when doing readdir. For now just allocate a buffer and copy
5546 * our information into that, and then dir_emit from the buffer. This is
5547 * similar to what NFS does, only we don't keep the buffer around in pagecache
5548 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5549 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5550 * tree lock.
5551 */
5552static int btrfs_opendir(struct inode *inode, struct file *file)
5553{
5554 struct btrfs_file_private *private;
5555
5556 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5557 if (!private)
5558 return -ENOMEM;
5559 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5560 if (!private->filldir_buf) {
5561 kfree(private);
5562 return -ENOMEM;
5563 }
5564 file->private_data = private;
5565 return 0;
5566}
5567
5568struct dir_entry {
5569 u64 ino;
5570 u64 offset;
5571 unsigned type;
5572 int name_len;
5573};
5574
5575static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5576{
5577 while (entries--) {
5578 struct dir_entry *entry = addr;
5579 char *name = (char *)(entry + 1);
5580
David Sterba92d32172018-04-16 21:10:14 +02005581 ctx->pos = get_unaligned(&entry->offset);
5582 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5583 get_unaligned(&entry->ino),
5584 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005585 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005586 addr += sizeof(struct dir_entry) +
5587 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005588 ctx->pos++;
5589 }
5590 return 0;
5591}
5592
Al Viro9cdda8d2013-05-22 16:48:09 -04005593static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005594{
Al Viro9cdda8d2013-05-22 16:48:09 -04005595 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005596 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005597 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005598 struct btrfs_dir_item *di;
5599 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005600 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005601 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005602 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005603 struct list_head ins_list;
5604 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005605 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005606 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005607 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005608 char *name_ptr;
5609 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005610 int entries = 0;
5611 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005612 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005613 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005614
Al Viro9cdda8d2013-05-22 16:48:09 -04005615 if (!dir_emit_dots(file, ctx))
5616 return 0;
5617
David Woodhouse49593bf2008-08-17 17:08:36 +01005618 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005619 if (!path)
5620 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005621
Josef Bacik23b5ec72017-07-24 15:14:25 -04005622 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005623 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005624
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005625 INIT_LIST_HEAD(&ins_list);
5626 INIT_LIST_HEAD(&del_list);
5627 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005628
Josef Bacik23b5ec72017-07-24 15:14:25 -04005629again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005630 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005631 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005632 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005633
Chris Mason39279cc2007-06-12 06:35:45 -04005634 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5635 if (ret < 0)
5636 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005637
5638 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005639 struct dir_entry *entry;
5640
Chris Mason5f39d392007-10-15 16:14:19 -04005641 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005642 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005643 if (slot >= btrfs_header_nritems(leaf)) {
5644 ret = btrfs_next_leaf(root, path);
5645 if (ret < 0)
5646 goto err;
5647 else if (ret > 0)
5648 break;
5649 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005650 }
Chris Mason3de45862008-11-17 21:02:50 -05005651
Chris Mason5f39d392007-10-15 16:14:19 -04005652 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5653
5654 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005655 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005656 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005657 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005658 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005659 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005660 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005661 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005662 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005663 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005664 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5665 PAGE_SIZE) {
5666 btrfs_release_path(path);
5667 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5668 if (ret)
5669 goto nopos;
5670 addr = private->filldir_buf;
5671 entries = 0;
5672 total_len = 0;
5673 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005674 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005675
5676 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005677 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005678 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005679 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5680 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005681 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005682 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005683 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005684 put_unaligned(location.objectid, &entry->ino);
5685 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005686 entries++;
5687 addr += sizeof(struct dir_entry) + name_len;
5688 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005689next:
5690 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005691 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005692 btrfs_release_path(path);
5693
5694 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5695 if (ret)
5696 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005697
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005698 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005699 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005700 goto nopos;
5701
Zach Browndb62efb2013-07-11 16:19:42 -07005702 /*
5703 * Stop new entries from being returned after we return the last
5704 * entry.
5705 *
5706 * New directory entries are assigned a strictly increasing
5707 * offset. This means that new entries created during readdir
5708 * are *guaranteed* to be seen in the future by that readdir.
5709 * This has broken buggy programs which operate on names as
5710 * they're returned by readdir. Until we re-use freed offsets
5711 * we have this hack to stop new entries from being returned
5712 * under the assumption that they'll never reach this huge
5713 * offset.
5714 *
5715 * This is being careful not to overflow 32bit loff_t unless the
5716 * last entry requires it because doing so has broken 32bit apps
5717 * in the past.
5718 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005719 if (ctx->pos >= INT_MAX)
5720 ctx->pos = LLONG_MAX;
5721 else
5722 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005723nopos:
5724 ret = 0;
5725err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005726 if (put)
5727 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005728 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005729 return ret;
5730}
5731
Chris Mason39279cc2007-06-12 06:35:45 -04005732/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005733 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005734 * inode changes. But, it is most likely to find the inode in cache.
5735 * FIXME, needs more benchmarking...there are no reasons other than performance
5736 * to keep or drop this code.
5737 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005738static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005739{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005740 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005741 struct btrfs_root *root = BTRFS_I(inode)->root;
5742 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005743 int ret;
5744
Josef Bacik72ac3c02012-05-23 14:13:11 -04005745 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005746 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005747
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005748 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005749 if (IS_ERR(trans))
5750 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005751
5752 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005753 if (ret && ret == -ENOSPC) {
5754 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005755 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005756 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005757 if (IS_ERR(trans))
5758 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005759
Chris Mason94b60442010-05-26 11:02:00 -04005760 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005761 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005762 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005763 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005764 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005765
5766 return ret;
5767}
5768
5769/*
5770 * This is a copy of file_update_time. We need this so we can return error on
5771 * ENOSPC for updating the inode in the case of file write and mmap writes.
5772 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005773static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005774 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005775{
Alexander Block2bc5565282012-06-15 09:49:33 +02005776 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005777 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005778
5779 if (btrfs_root_readonly(root))
5780 return -EROFS;
5781
Josef Bacike41f9412012-03-26 09:46:47 -04005782 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005783 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005784 if (flags & S_CTIME)
5785 inode->i_ctime = *now;
5786 if (flags & S_MTIME)
5787 inode->i_mtime = *now;
5788 if (flags & S_ATIME)
5789 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005790 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005791}
5792
Chris Masond352ac62008-09-29 15:18:18 -04005793/*
5794 * find the highest existing sequence number in a directory
5795 * and then set the in-memory index_cnt variable to reflect
5796 * free sequence numbers
5797 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005798static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005799{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005800 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005801 struct btrfs_key key, found_key;
5802 struct btrfs_path *path;
5803 struct extent_buffer *leaf;
5804 int ret;
5805
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005806 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005807 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005808 key.offset = (u64)-1;
5809
5810 path = btrfs_alloc_path();
5811 if (!path)
5812 return -ENOMEM;
5813
5814 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5815 if (ret < 0)
5816 goto out;
5817 /* FIXME: we should be able to handle this */
5818 if (ret == 0)
5819 goto out;
5820 ret = 0;
5821
5822 /*
5823 * MAGIC NUMBER EXPLANATION:
5824 * since we search a directory based on f_pos we have to start at 2
5825 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5826 * else has to start at 2
5827 */
5828 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005829 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005830 goto out;
5831 }
5832
5833 path->slots[0]--;
5834
5835 leaf = path->nodes[0];
5836 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5837
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005838 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005839 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005840 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005841 goto out;
5842 }
5843
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005844 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005845out:
5846 btrfs_free_path(path);
5847 return ret;
5848}
5849
Chris Masond352ac62008-09-29 15:18:18 -04005850/*
5851 * helper to find a free sequence number in a given directory. This current
5852 * code is very simple, later versions will do smarter things in the btree
5853 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005854int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005855{
5856 int ret = 0;
5857
Nikolay Borisov877574e2017-02-20 13:50:33 +02005858 if (dir->index_cnt == (u64)-1) {
5859 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005860 if (ret) {
5861 ret = btrfs_set_inode_index_count(dir);
5862 if (ret)
5863 return ret;
5864 }
Josef Bacikaec74772008-07-24 12:12:38 -04005865 }
5866
Nikolay Borisov877574e2017-02-20 13:50:33 +02005867 *index = dir->index_cnt;
5868 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005869
5870 return ret;
5871}
5872
Chris Masonb0d5d102014-09-08 13:08:51 -07005873static int btrfs_insert_inode_locked(struct inode *inode)
5874{
5875 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005876
5877 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005878 args.root = BTRFS_I(inode)->root;
5879
5880 return insert_inode_locked4(inode,
5881 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5882 btrfs_find_actor, &args);
5883}
5884
Anand Jain19aee8d2017-07-18 17:37:05 +08005885/*
5886 * Inherit flags from the parent inode.
5887 *
5888 * Currently only the compression flags and the cow flags are inherited.
5889 */
5890static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5891{
5892 unsigned int flags;
5893
5894 if (!dir)
5895 return;
5896
5897 flags = BTRFS_I(dir)->flags;
5898
5899 if (flags & BTRFS_INODE_NOCOMPRESS) {
5900 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5901 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5902 } else if (flags & BTRFS_INODE_COMPRESS) {
5903 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5904 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5905 }
5906
5907 if (flags & BTRFS_INODE_NODATACOW) {
5908 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5909 if (S_ISREG(inode->i_mode))
5910 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5911 }
5912
David Sterba7b6a2212018-03-26 18:40:21 +02005913 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005914}
5915
Chris Mason39279cc2007-06-12 06:35:45 -04005916static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5917 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005918 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005919 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005920 u64 ref_objectid, u64 objectid,
5921 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005922{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005923 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005924 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005925 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005926 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005927 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005928 struct btrfs_inode_ref *ref;
5929 struct btrfs_key key[2];
5930 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005931 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005932 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005933 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005934 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005935
Chris Mason5f39d392007-10-15 16:14:19 -04005936 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005937 if (!path)
5938 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005939
Josef Bacik11a19a92019-09-09 10:12:04 -04005940 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005941 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005942 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005943 if (!inode) {
5944 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005945 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005946 }
Chris Mason39279cc2007-06-12 06:35:45 -04005947
Li Zefan581bb052011-04-20 10:06:11 +08005948 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005949 * O_TMPFILE, set link count to 0, so that after this point,
5950 * we fill in an inode item with the correct link count.
5951 */
5952 if (!name)
5953 set_nlink(inode, 0);
5954
5955 /*
Li Zefan581bb052011-04-20 10:06:11 +08005956 * we have to initialize this early, so we can reclaim the inode
5957 * number if we fail afterwards in this function.
5958 */
5959 inode->i_ino = objectid;
5960
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005961 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005962 trace_btrfs_inode_request(dir);
5963
Nikolay Borisov877574e2017-02-20 13:50:33 +02005964 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005965 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005966 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005967 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005968 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005969 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005970 } else if (dir) {
5971 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005972 }
5973 /*
5974 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005975 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005976 * number
5977 */
5978 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005979 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005980 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005981 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005982 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005983
Josef Bacik5dc562c2012-08-17 13:14:17 -04005984 /*
5985 * We could have gotten an inode number from somebody who was fsynced
5986 * and then removed in this same transaction, so let's just set full
5987 * sync since it will be a full sync anyway and this will blow away the
5988 * old info in the log.
5989 */
5990 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5991
Chris Mason9c583092008-01-29 15:15:18 -05005992 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005993 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005994 key[0].offset = 0;
5995
Chris Mason9c583092008-01-29 15:15:18 -05005996 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005997
5998 if (name) {
5999 /*
6000 * Start new inodes with an inode_ref. This is slightly more
6001 * efficient for small numbers of hard links since they will
6002 * be packed into one item. Extended refs will kick in if we
6003 * add more hard links than can fit in the ref item.
6004 */
6005 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006006 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006007 key[1].offset = ref_objectid;
6008
6009 sizes[1] = name_len + sizeof(*ref);
6010 }
Chris Mason9c583092008-01-29 15:15:18 -05006011
Chris Masonb0d5d102014-09-08 13:08:51 -07006012 location = &BTRFS_I(inode)->location;
6013 location->objectid = objectid;
6014 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006015 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006016
6017 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006018 if (ret < 0) {
6019 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006020 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006021 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006022
Chris Masonb9473432009-03-13 11:00:37 -04006023 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006024 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006025 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006026 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006027
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006028 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006029 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306030
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006031 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306032 inode->i_atime = inode->i_mtime;
6033 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006034 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306035
Chris Mason5f39d392007-10-15 16:14:19 -04006036 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6037 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006038 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006039 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006040 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006041
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006042 if (name) {
6043 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6044 struct btrfs_inode_ref);
6045 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6046 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6047 ptr = (unsigned long)(ref + 1);
6048 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6049 }
Chris Mason9c583092008-01-29 15:15:18 -05006050
Chris Mason5f39d392007-10-15 16:14:19 -04006051 btrfs_mark_buffer_dirty(path->nodes[0]);
6052 btrfs_free_path(path);
6053
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006054 btrfs_inherit_iflags(inode, dir);
6055
Al Viro569254b2011-07-24 17:08:40 -04006056 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006057 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006058 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006059 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006060 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6061 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006062 }
6063
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006064 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006065
6066 trace_btrfs_inode_new(inode);
Chris Mason1973f0f2011-06-24 13:13:29 -04006067 btrfs_set_inode_last_trans(trans, inode);
liubo1abe9b82011-03-24 11:18:59 +00006068
Alexander Block8ea05e32012-07-25 17:35:53 +02006069 btrfs_update_root_times(trans, root);
6070
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006071 ret = btrfs_inode_inherit_props(trans, inode, dir);
6072 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006073 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006074 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006075 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006076
Chris Mason39279cc2007-06-12 06:35:45 -04006077 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006078
6079fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006080 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006081fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006082 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006083 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006084 btrfs_free_path(path);
6085 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006086}
6087
Chris Masond352ac62008-09-29 15:18:18 -04006088/*
6089 * utility function to add 'inode' into 'parent_inode' with
6090 * a give name and a given sequence number.
6091 * if 'add_backref' is true, also insert a backref from the
6092 * inode to the parent directory.
6093 */
Chris Masone02119d2008-09-05 16:13:11 -04006094int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006095 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006096 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006097{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006098 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006099 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006100 struct btrfs_root *root = parent_inode->root;
6101 u64 ino = btrfs_ino(inode);
6102 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006103
Li Zefan33345d012011-04-20 10:31:50 +08006104 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006105 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006106 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006107 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006108 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006109 key.offset = 0;
6110 }
Chris Mason39279cc2007-06-12 06:35:45 -04006111
Li Zefan33345d012011-04-20 10:31:50 +08006112 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006113 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006114 root->root_key.objectid, parent_ino,
6115 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006116 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006117 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6118 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006119 }
6120
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006121 /* Nothing to clean up yet */
6122 if (ret)
6123 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006124
Lu Fengqi684572d2018-08-04 21:10:57 +08006125 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006126 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006127 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006128 goto fail_dir_item;
6129 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006130 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006131 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006132 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006133
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006134 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006135 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006136 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006137 /*
6138 * If we are replaying a log tree, we do not want to update the mtime
6139 * and ctime of the parent directory with the current time, since the
6140 * log replay procedure is responsible for setting them to their correct
6141 * values (the ones it had when the fsync was done).
6142 */
6143 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6144 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6145
6146 parent_inode->vfs_inode.i_mtime = now;
6147 parent_inode->vfs_inode.i_ctime = now;
6148 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006149 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006150 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006151 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006152 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006153
6154fail_dir_item:
6155 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6156 u64 local_index;
6157 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006158 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006159 root->root_key.objectid, parent_ino,
6160 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006161 if (err)
6162 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006163 } else if (add_backref) {
6164 u64 local_index;
6165 int err;
6166
6167 err = btrfs_del_inode_ref(trans, root, name, name_len,
6168 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006169 if (err)
6170 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006171 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006172
6173 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006174 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006175}
6176
6177static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006178 struct btrfs_inode *dir, struct dentry *dentry,
6179 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006180{
Josef Bacika1b075d2010-11-19 20:36:11 +00006181 int err = btrfs_add_link(trans, dir, inode,
6182 dentry->d_name.name, dentry->d_name.len,
6183 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006184 if (err > 0)
6185 err = -EEXIST;
6186 return err;
6187}
6188
Josef Bacik618e21d2007-07-11 10:18:17 -04006189static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006190 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006191{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006192 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006193 struct btrfs_trans_handle *trans;
6194 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006195 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006196 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006197 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006198 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006199
Josef Bacik9ed74f22009-09-11 16:12:44 -04006200 /*
6201 * 2 for inode item and ref
6202 * 2 for dir items
6203 * 1 for xattr if selinux is on
6204 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006205 trans = btrfs_start_transaction(root, 5);
6206 if (IS_ERR(trans))
6207 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006208
Li Zefan581bb052011-04-20 10:06:11 +08006209 err = btrfs_find_free_ino(root, &objectid);
6210 if (err)
6211 goto out_unlock;
6212
Josef Bacikaec74772008-07-24 12:12:38 -04006213 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006214 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6215 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006216 if (IS_ERR(inode)) {
6217 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006218 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006219 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006220 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006221
Casey Schauflerad19db72011-12-15 10:09:07 -05006222 /*
6223 * If the active LSM wants to access the inode during
6224 * d_instantiate it needs these. Smack checks to see
6225 * if the filesystem supports xattrs by looking at the
6226 * ops vector.
6227 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006228 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006229 init_special_inode(inode, inode->i_mode, rdev);
6230
6231 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006232 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006233 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006234
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006235 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6236 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006237 if (err)
6238 goto out_unlock;
6239
6240 btrfs_update_inode(trans, root, inode);
6241 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006242
Josef Bacik618e21d2007-07-11 10:18:17 -04006243out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006244 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006245 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006246 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006247 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006248 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006249 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006250 return err;
6251}
6252
Chris Mason39279cc2007-06-12 06:35:45 -04006253static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006254 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006255{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006256 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006257 struct btrfs_trans_handle *trans;
6258 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006259 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006260 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006261 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006262 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006263
Josef Bacik9ed74f22009-09-11 16:12:44 -04006264 /*
6265 * 2 for inode item and ref
6266 * 2 for dir items
6267 * 1 for xattr if selinux is on
6268 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006269 trans = btrfs_start_transaction(root, 5);
6270 if (IS_ERR(trans))
6271 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006272
Li Zefan581bb052011-04-20 10:06:11 +08006273 err = btrfs_find_free_ino(root, &objectid);
6274 if (err)
6275 goto out_unlock;
6276
Josef Bacikaec74772008-07-24 12:12:38 -04006277 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006278 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6279 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006280 if (IS_ERR(inode)) {
6281 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006282 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006283 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006284 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006285 /*
6286 * If the active LSM wants to access the inode during
6287 * d_instantiate it needs these. Smack checks to see
6288 * if the filesystem supports xattrs by looking at the
6289 * ops vector.
6290 */
6291 inode->i_fop = &btrfs_file_operations;
6292 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006293 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006294
6295 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6296 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006297 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006298
6299 err = btrfs_update_inode(trans, root, inode);
6300 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006301 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006302
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006303 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6304 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006305 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006306 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006307
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006308 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006309 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006310
Chris Mason39279cc2007-06-12 06:35:45 -04006311out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006312 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006313 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006314 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006315 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006316 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006317 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006318 return err;
6319}
6320
6321static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6322 struct dentry *dentry)
6323{
Filipe Manana271dba452016-01-05 16:24:05 +00006324 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006325 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006326 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006327 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006328 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006329 int err;
6330 int drop_inode = 0;
6331
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006332 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006333 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006334 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006335
Mark Fashehf1863732012-08-08 11:32:27 -07006336 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006337 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006338
Nikolay Borisov877574e2017-02-20 13:50:33 +02006339 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006340 if (err)
6341 goto fail;
6342
Yan, Zhenga22285a2010-05-16 10:48:46 -04006343 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006344 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006345 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006346 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006347 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006348 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006349 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006350 if (IS_ERR(trans)) {
6351 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006352 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006353 goto fail;
6354 }
Chris Mason5f39d392007-10-15 16:14:19 -04006355
Miao Xie67de1172013-12-26 13:07:06 +08006356 /* There are several dir indexes for this inode, clear the cache. */
6357 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006358 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006359 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006360 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006361 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006362 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006363
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006364 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6365 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006366
Yan, Zhenga5719522009-09-24 09:17:31 -04006367 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006368 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006369 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006370 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006371 int ret;
6372
Yan, Zhenga5719522009-09-24 09:17:31 -04006373 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006374 if (err)
6375 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006376 if (inode->i_nlink == 1) {
6377 /*
6378 * If new hard link count is 1, it's a file created
6379 * with open(2) O_TMPFILE flag.
6380 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006381 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006382 if (err)
6383 goto fail;
6384 }
Al Viro08c422c2011-12-23 07:58:13 -05006385 d_instantiate(dentry, inode);
Filipe Mananad4682ba2018-06-11 19:24:28 +01006386 ret = btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent,
6387 true, NULL);
6388 if (ret == BTRFS_NEED_TRANS_COMMIT) {
6389 err = btrfs_commit_transaction(trans);
6390 trans = NULL;
6391 }
Yan, Zhenga5719522009-09-24 09:17:31 -04006392 }
Chris Mason39279cc2007-06-12 06:35:45 -04006393
Chris Mason1832a6d2007-12-21 16:27:21 -05006394fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006395 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006396 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006397 if (drop_inode) {
6398 inode_dec_link_count(inode);
6399 iput(inode);
6400 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006401 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006402 return err;
6403}
6404
Al Viro18bb1db2011-07-26 01:41:39 -04006405static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006406{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006407 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006408 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006409 struct btrfs_trans_handle *trans;
6410 struct btrfs_root *root = BTRFS_I(dir)->root;
6411 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006412 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006413 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006414
Josef Bacik9ed74f22009-09-11 16:12:44 -04006415 /*
6416 * 2 items for inode and ref
6417 * 2 items for dir items
6418 * 1 for xattr if selinux is on
6419 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006420 trans = btrfs_start_transaction(root, 5);
6421 if (IS_ERR(trans))
6422 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006423
Li Zefan581bb052011-04-20 10:06:11 +08006424 err = btrfs_find_free_ino(root, &objectid);
6425 if (err)
6426 goto out_fail;
6427
Josef Bacikaec74772008-07-24 12:12:38 -04006428 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006429 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6430 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006431 if (IS_ERR(inode)) {
6432 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006433 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006434 goto out_fail;
6435 }
Chris Mason5f39d392007-10-15 16:14:19 -04006436
Chris Masonb0d5d102014-09-08 13:08:51 -07006437 /* these must be set before we unlock the inode */
6438 inode->i_op = &btrfs_dir_inode_operations;
6439 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006440
Eric Paris2a7dba32011-02-01 11:05:39 -05006441 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006442 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006443 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006444
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006445 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006446 err = btrfs_update_inode(trans, root, inode);
6447 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006448 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006449
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006450 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6451 dentry->d_name.name,
6452 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006453 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006454 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006455
Al Viro1e2e5472018-05-04 08:23:01 -04006456 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006457
6458out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006459 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006460 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006461 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006462 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006463 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006464 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006465 return err;
6466}
6467
Chris Masonc8b97812008-10-29 14:49:59 -04006468static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006469 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006470 size_t pg_offset, u64 extent_offset,
6471 struct btrfs_file_extent_item *item)
6472{
6473 int ret;
6474 struct extent_buffer *leaf = path->nodes[0];
6475 char *tmp;
6476 size_t max_size;
6477 unsigned long inline_size;
6478 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006479 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006480
6481 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006482 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006483 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6484 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006485 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006486 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006487 if (!tmp)
6488 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006489 ptr = btrfs_file_extent_inline_start(item);
6490
6491 read_extent_buffer(leaf, tmp, ptr, inline_size);
6492
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006493 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006494 ret = btrfs_decompress(compress_type, tmp, page,
6495 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006496
6497 /*
6498 * decompression code contains a memset to fill in any space between the end
6499 * of the uncompressed data and the end of max_size in case the decompressed
6500 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6501 * the end of an inline extent and the beginning of the next block, so we
6502 * cover that region here.
6503 */
6504
6505 if (max_size + pg_offset < PAGE_SIZE) {
6506 char *map = kmap(page);
6507 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6508 kunmap(page);
6509 }
Chris Masonc8b97812008-10-29 14:49:59 -04006510 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006511 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006512}
6513
Omar Sandoval39b07b52019-12-02 17:34:23 -08006514/**
6515 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6516 * @inode: file to search in
6517 * @page: page to read extent data into if the extent is inline
6518 * @pg_offset: offset into @page to copy to
6519 * @start: file offset
6520 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006521 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006522 * This returns the first &struct extent_map which overlaps with the given
6523 * range, reading it from the B-tree and caching it if necessary. Note that
6524 * there may be more extents which overlap the given range after the returned
6525 * extent_map.
6526 *
6527 * If @page is not NULL and the extent is inline, this also reads the extent
6528 * data directly into the page and marks the extent up to date in the io_tree.
6529 *
6530 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006531 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006532struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006533 struct page *page, size_t pg_offset,
6534 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006535{
David Sterba3ffbd682018-06-29 10:56:42 +02006536 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masona52d9a82007-08-27 16:49:44 -04006537 int ret;
6538 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006539 u64 extent_start = 0;
6540 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006541 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006542 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006543 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006544 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006545 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006546 struct extent_buffer *leaf;
6547 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006548 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006549 struct extent_map_tree *em_tree = &inode->extent_tree;
6550 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006551
Chris Mason890871b2009-09-02 16:24:52 -04006552 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006553 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006554 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006555
Chris Masona52d9a82007-08-27 16:49:44 -04006556 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006557 if (em->start > start || em->start + em->len <= start)
6558 free_extent_map(em);
6559 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006560 free_extent_map(em);
6561 else
6562 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006563 }
David Sterba172ddd62011-04-21 00:48:27 +02006564 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006565 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05006566 err = -ENOMEM;
6567 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006568 }
Chris Masond1310b22008-01-24 16:13:08 -05006569 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006570 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006571 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006572 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006573
Liu Bobee6ec82018-08-17 05:05:28 +08006574 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006575 if (!path) {
Liu Bobee6ec82018-08-17 05:05:28 +08006576 err = -ENOMEM;
6577 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006578 }
6579
Liu Bobee6ec82018-08-17 05:05:28 +08006580 /* Chances are we'll be called again, so go ahead and do readahead */
6581 path->reada = READA_FORWARD;
6582
Liu Boe49aabd2018-08-25 13:47:09 +08006583 /*
6584 * Unless we're going to uncompress the inline extent, no sleep would
6585 * happen.
6586 */
6587 path->leave_spinning = 1;
6588
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006589 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006590 if (ret < 0) {
6591 err = ret;
6592 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006593 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006594 if (path->slots[0] == 0)
6595 goto not_found;
6596 path->slots[0]--;
6597 }
6598
Chris Mason5f39d392007-10-15 16:14:19 -04006599 leaf = path->nodes[0];
6600 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006601 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006602 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006603 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006604 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006605 /*
6606 * If we backup past the first extent we want to move forward
6607 * and see if there is an extent in front of us, otherwise we'll
6608 * say there is a hole for our whole search range which can
6609 * cause problems.
6610 */
6611 extent_end = start;
6612 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006613 }
6614
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006615 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006616 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006617 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006618 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6619 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006620 /* Only regular file could have regular/prealloc extent */
6621 if (!S_ISREG(inode->vfs_inode.i_mode)) {
6622 ret = -EUCLEAN;
6623 btrfs_crit(fs_info,
6624 "regular/prealloc extent found for non-regular inode %llu",
6625 btrfs_ino(inode));
6626 goto out;
6627 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006628 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6629 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006630 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006631 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6632 path->slots[0],
6633 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006634 }
Josef Bacik25a50342013-10-14 12:08:38 -04006635next:
Yan Zheng9036c102008-10-30 14:19:41 -04006636 if (start >= extent_end) {
6637 path->slots[0]++;
6638 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6639 ret = btrfs_next_leaf(root, path);
6640 if (ret < 0) {
6641 err = ret;
6642 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006643 } else if (ret > 0) {
Yan Zheng9036c102008-10-30 14:19:41 -04006644 goto not_found;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006645 }
Yan Zheng9036c102008-10-30 14:19:41 -04006646 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006647 }
Yan Zheng9036c102008-10-30 14:19:41 -04006648 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6649 if (found_key.objectid != objectid ||
6650 found_key.type != BTRFS_EXTENT_DATA_KEY)
6651 goto not_found;
6652 if (start + len <= found_key.offset)
6653 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006654 if (start > found_key.offset)
6655 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006656
6657 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006658 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006659 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006660 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006661 em->block_start = EXTENT_MAP_HOLE;
6662 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006663 }
6664
Omar Sandoval39b07b52019-12-02 17:34:23 -08006665 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006666
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006667 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6668 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006669 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006670 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006671 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006672 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006673 size_t size;
6674 size_t extent_offset;
6675 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006676
Omar Sandoval39b07b52019-12-02 17:34:23 -08006677 if (!page)
Yan689f9342007-10-29 11:41:07 -04006678 goto out;
Yan689f9342007-10-29 11:41:07 -04006679
Qu Wenruoe41ca582018-06-06 15:41:49 +08006680 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006681 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006682 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6683 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006684 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006685 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006686 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006687 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006688 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006689
6690 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006691 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006692 if (btrfs_file_extent_compression(leaf, item) !=
6693 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006694 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006695 extent_offset, item);
Zach Brown166ae5a2014-05-09 17:15:10 -04006696 if (ret) {
6697 err = ret;
6698 goto out;
6699 }
Chris Masonc8b97812008-10-29 14:49:59 -04006700 } else {
6701 map = kmap(page);
6702 read_extent_buffer(leaf, map + pg_offset, ptr,
6703 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006704 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006705 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006706 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006707 copy_size);
6708 }
Chris Masonc8b97812008-10-29 14:49:59 -04006709 kunmap(page);
6710 }
Chris Mason179e29e2007-11-01 11:28:41 -04006711 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006712 }
Chris Masond1310b22008-01-24 16:13:08 -05006713 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006714 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006715 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006716 }
6717not_found:
6718 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006719 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006720 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006721 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006722insert:
David Sterbab3b4aa72011-04-21 01:20:15 +02006723 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006724 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006725 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006726 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6727 em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04006728 err = -EIO;
6729 goto out;
6730 }
Chris Masond1310b22008-01-24 16:13:08 -05006731
6732 err = 0;
Chris Mason890871b2009-09-02 16:24:52 -04006733 write_lock(&em_tree->lock);
David Sterbaf46b24c2018-04-03 21:45:57 +02006734 err = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006735 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006736out:
Liu Boc6414282018-08-23 07:36:17 +08006737 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006738
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006739 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006740
Chris Masona52d9a82007-08-27 16:49:44 -04006741 if (err) {
6742 free_extent_map(em);
Chris Masona52d9a82007-08-27 16:49:44 -04006743 return ERR_PTR(err);
6744 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006745 BUG_ON(!em); /* Error is always set */
Chris Masona52d9a82007-08-27 16:49:44 -04006746 return em;
6747}
6748
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006749struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006750 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006751{
6752 struct extent_map *em;
6753 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006754 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006755 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006756 u64 delalloc_len;
6757 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006758 int err = 0;
6759
Omar Sandoval39b07b52019-12-02 17:34:23 -08006760 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006761 if (IS_ERR(em))
6762 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006763 /*
6764 * If our em maps to:
6765 * - a hole or
6766 * - a pre-alloc extent,
6767 * there might actually be delalloc bytes behind it.
6768 */
6769 if (em->block_start != EXTENT_MAP_HOLE &&
6770 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6771 return em;
6772 else
6773 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006774
6775 /* check to see if we've wrapped (len == -1 or similar) */
6776 end = start + len;
6777 if (end < start)
6778 end = (u64)-1;
6779 else
6780 end -= 1;
6781
6782 em = NULL;
6783
6784 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006785 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006786 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006787 delalloc_end = delalloc_start + delalloc_len;
6788 if (delalloc_end < delalloc_start)
6789 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006790
6791 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006792 * We didn't find anything useful, return the original results from
6793 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006794 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006795 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006796 em = hole_em;
6797 hole_em = NULL;
6798 goto out;
6799 }
6800
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006801 /*
6802 * Adjust the delalloc_start to make sure it doesn't go backwards from
6803 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006804 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006805 delalloc_start = max(start, delalloc_start);
6806 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006807
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006808 if (delalloc_len > 0) {
6809 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006810 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006811 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006812
David Sterba172ddd62011-04-21 00:48:27 +02006813 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006814 if (!em) {
6815 err = -ENOMEM;
6816 goto out;
6817 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006818
6819 ASSERT(hole_em);
6820 /*
6821 * When btrfs_get_extent can't find anything it returns one
6822 * huge hole
6823 *
6824 * Make sure what it found really fits our range, and adjust to
6825 * make sure it is based on the start from the caller
6826 */
6827 if (hole_end <= start || hole_em->start > end) {
6828 free_extent_map(hole_em);
6829 hole_em = NULL;
6830 } else {
6831 hole_start = max(hole_em->start, start);
6832 hole_len = hole_end - hole_start;
6833 }
6834
6835 if (hole_em && delalloc_start > hole_start) {
6836 /*
6837 * Our hole starts before our delalloc, so we have to
6838 * return just the parts of the hole that go until the
6839 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006840 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006841 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006842 em->start = hole_start;
6843 em->orig_start = hole_start;
6844 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006845 * Don't adjust block start at all, it is fixed at
6846 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006847 */
6848 em->block_start = hole_em->block_start;
6849 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006850 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6851 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006852 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006853 /*
6854 * Hole is out of passed range or it starts after
6855 * delalloc range
6856 */
6857 em->start = delalloc_start;
6858 em->len = delalloc_len;
6859 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006860 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006861 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006862 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006863 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006864 return hole_em;
6865 }
6866out:
6867
6868 free_extent_map(hole_em);
6869 if (err) {
6870 free_extent_map(em);
6871 return ERR_PTR(err);
6872 }
6873 return em;
6874}
6875
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006876static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
6877 const u64 start,
6878 const u64 len,
6879 const u64 orig_start,
6880 const u64 block_start,
6881 const u64 block_len,
6882 const u64 orig_block_len,
6883 const u64 ram_bytes,
6884 const int type)
6885{
6886 struct extent_map *em = NULL;
6887 int ret;
6888
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006889 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03006890 em = create_io_em(BTRFS_I(inode), start, len, orig_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08006891 block_start, block_len, orig_block_len,
6892 ram_bytes,
6893 BTRFS_COMPRESS_NONE, /* compress_type */
6894 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006895 if (IS_ERR(em))
6896 goto out;
6897 }
6898 ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
6899 len, block_len, type);
6900 if (ret) {
6901 if (em) {
6902 free_extent_map(em);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02006903 btrfs_drop_extent_cache(BTRFS_I(inode), start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006904 start + len - 1, 0);
6905 }
6906 em = ERR_PTR(ret);
6907 }
6908 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006909
6910 return em;
6911}
6912
Josef Bacik4b46fce2010-05-23 11:00:55 -04006913static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
6914 u64 start, u64 len)
6915{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006916 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006917 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik70c8a912012-10-11 16:54:30 -04006918 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006919 struct btrfs_key ins;
6920 u64 alloc_hint;
6921 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006922
Nikolay Borisov43c69842020-06-03 08:55:02 +03006923 alloc_hint = get_extent_allocation_hint(BTRFS_I(inode), start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006924 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006925 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006926 if (ret)
6927 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006928
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006929 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
6930 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006931 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006932 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006933 if (IS_ERR(em))
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006934 btrfs_free_reserved_extent(fs_info, ins.objectid,
6935 ins.offset, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006936
Josef Bacik4b46fce2010-05-23 11:00:55 -04006937 return em;
6938}
6939
Chris Mason46bfbb52010-05-26 11:04:10 -04006940/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006941 * Check if we can do nocow write into the range [@offset, @offset + @len)
6942 *
6943 * @offset: File offset
6944 * @len: The length to write, will be updated to the nocow writeable
6945 * range
6946 * @orig_start: (optional) Return the original file offset of the file extent
6947 * @orig_len: (optional) Return the original on-disk length of the file extent
6948 * @ram_bytes: (optional) Return the ram_bytes of the file extent
6949 *
6950 * This function will flush ordered extents in the range to ensure proper
6951 * nocow checks for (nowait == false) case.
6952 *
6953 * Return:
6954 * >0 and update @len if we can do nocow write
6955 * 0 if we can't do nocow write
6956 * <0 if error happened
6957 *
6958 * NOTE: This only checks the file extents, caller is responsible to wait for
6959 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04006960 */
Josef Bacik00361582013-08-14 14:02:47 -04006961noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006962 u64 *orig_start, u64 *orig_block_len,
6963 u64 *ram_bytes)
Chris Mason46bfbb52010-05-26 11:04:10 -04006964{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006965 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006966 struct btrfs_path *path;
6967 int ret;
6968 struct extent_buffer *leaf;
6969 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006970 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006971 struct btrfs_file_extent_item *fi;
6972 struct btrfs_key key;
6973 u64 disk_bytenr;
6974 u64 backref_offset;
6975 u64 extent_end;
6976 u64 num_bytes;
6977 int slot;
6978 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006979 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006980
Chris Mason46bfbb52010-05-26 11:04:10 -04006981 path = btrfs_alloc_path();
6982 if (!path)
6983 return -ENOMEM;
6984
David Sterbaf85b7372017-01-20 14:54:07 +01006985 ret = btrfs_lookup_file_extent(NULL, root, path,
6986 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006987 if (ret < 0)
6988 goto out;
6989
6990 slot = path->slots[0];
6991 if (ret == 1) {
6992 if (slot == 0) {
6993 /* can't find the item, must cow */
6994 ret = 0;
6995 goto out;
6996 }
6997 slot--;
6998 }
6999 ret = 0;
7000 leaf = path->nodes[0];
7001 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007002 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007003 key.type != BTRFS_EXTENT_DATA_KEY) {
7004 /* not our file or wrong item type, must cow */
7005 goto out;
7006 }
7007
7008 if (key.offset > offset) {
7009 /* Wrong offset, must cow */
7010 goto out;
7011 }
7012
7013 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7014 found_type = btrfs_file_extent_type(leaf, fi);
7015 if (found_type != BTRFS_FILE_EXTENT_REG &&
7016 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7017 /* not a regular extent, must cow */
7018 goto out;
7019 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007020
7021 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7022 goto out;
7023
Miao Xiee77751a2013-12-27 21:11:50 +08007024 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7025 if (extent_end <= offset)
7026 goto out;
7027
Chris Mason46bfbb52010-05-26 11:04:10 -04007028 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007029 if (disk_bytenr == 0)
7030 goto out;
7031
7032 if (btrfs_file_extent_compression(leaf, fi) ||
7033 btrfs_file_extent_encryption(leaf, fi) ||
7034 btrfs_file_extent_other_encoding(leaf, fi))
7035 goto out;
7036
Ethan Lien78d42952018-05-17 14:58:29 +08007037 /*
7038 * Do the same check as in btrfs_cross_ref_exist but without the
7039 * unnecessary search.
7040 */
7041 if (btrfs_file_extent_generation(leaf, fi) <=
7042 btrfs_root_last_snapshot(&root->root_item))
7043 goto out;
7044
Chris Mason46bfbb52010-05-26 11:04:10 -04007045 backref_offset = btrfs_file_extent_offset(leaf, fi);
7046
Josef Bacik7ee9e442013-06-21 16:37:03 -04007047 if (orig_start) {
7048 *orig_start = key.offset - backref_offset;
7049 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7050 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7051 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007052
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007053 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007054 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007055
7056 num_bytes = min(offset + *len, extent_end) - offset;
7057 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7058 u64 range_end;
7059
Jeff Mahoneyda170662016-06-15 09:22:56 -04007060 range_end = round_up(offset + num_bytes,
7061 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007062 ret = test_range_bit(io_tree, offset, range_end,
7063 EXTENT_DELALLOC, 0, NULL);
7064 if (ret) {
7065 ret = -EAGAIN;
7066 goto out;
7067 }
7068 }
7069
Josef Bacik1bda19e2013-10-18 12:10:36 -04007070 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007071
7072 /*
7073 * look for other files referencing this extent, if we
7074 * find any we must cow
7075 */
Josef Bacik00361582013-08-14 14:02:47 -04007076
Liu Boe4c3b2d2017-01-30 12:25:28 -08007077 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Josef Bacik00361582013-08-14 14:02:47 -04007078 key.offset - backref_offset, disk_bytenr);
Josef Bacik00361582013-08-14 14:02:47 -04007079 if (ret) {
7080 ret = 0;
7081 goto out;
7082 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007083
7084 /*
7085 * adjust disk_bytenr and num_bytes to cover just the bytes
7086 * in this extent we are about to write. If there
7087 * are any csums in that range we have to cow in order
7088 * to keep the csums correct
7089 */
7090 disk_bytenr += backref_offset;
7091 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007092 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7093 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007094 /*
7095 * all of the above have passed, it is safe to overwrite this extent
7096 * without cow
7097 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007098 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007099 ret = 1;
7100out:
7101 btrfs_free_path(path);
7102 return ret;
7103}
7104
Josef Bacikeb838e72012-07-31 16:28:48 -04007105static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
David Sterba55e20bd2020-06-09 19:56:06 +02007106 struct extent_state **cached_state, int writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007107{
7108 struct btrfs_ordered_extent *ordered;
7109 int ret = 0;
7110
7111 while (1) {
7112 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007113 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007114 /*
7115 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007116 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007117 * extents in this range.
7118 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007119 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007120 lockend - lockstart + 1);
7121
7122 /*
7123 * We need to make sure there are no buffered pages in this
7124 * range either, we could have raced between the invalidate in
7125 * generic_file_direct_write and locking the extent. The
7126 * invalidate needs to happen so that reads after a write do not
7127 * get stale data.
7128 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007129 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007130 (!writing || !filemap_range_has_page(inode->i_mapping,
7131 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007132 break;
7133
7134 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007135 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007136
7137 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007138 /*
7139 * If we are doing a DIO read and the ordered extent we
7140 * found is for a buffered write, we can not wait for it
7141 * to complete and retry, because if we do so we can
7142 * deadlock with concurrent buffered writes on page
7143 * locks. This happens only if our DIO read covers more
7144 * than one extent map, if at this point has already
7145 * created an ordered extent for a previous extent map
7146 * and locked its range in the inode's io tree, and a
7147 * concurrent write against that previous extent map's
7148 * range and this range started (we unlock the ranges
7149 * in the io tree only when the bios complete and
7150 * buffered writes always lock pages before attempting
7151 * to lock range in the io tree).
7152 */
7153 if (writing ||
7154 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7155 btrfs_start_ordered_extent(inode, ordered, 1);
7156 else
7157 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007158 btrfs_put_ordered_extent(ordered);
7159 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007160 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007161 * We could trigger writeback for this range (and wait
7162 * for it to complete) and then invalidate the pages for
7163 * this range (through invalidate_inode_pages2_range()),
7164 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007165 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007166 * triggered a readahead) on a page lock due to an
7167 * ordered dio extent we created before but did not have
7168 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007169 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007170 * ordered extent to complete while holding a lock on
7171 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007172 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007173 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007174 }
7175
Filipe Mananaade77022016-02-18 14:28:55 +00007176 if (ret)
7177 break;
7178
Josef Bacikeb838e72012-07-31 16:28:48 -04007179 cond_resched();
7180 }
7181
7182 return ret;
7183}
7184
Liu Bo6f9994d2017-01-31 07:50:22 -08007185/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007186static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7187 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007188 u64 block_len, u64 orig_block_len,
7189 u64 ram_bytes, int compress_type,
7190 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007191{
7192 struct extent_map_tree *em_tree;
7193 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007194 int ret;
7195
Liu Bo6f9994d2017-01-31 07:50:22 -08007196 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7197 type == BTRFS_ORDERED_COMPRESSED ||
7198 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007199 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007200
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007201 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007202 em = alloc_extent_map();
7203 if (!em)
7204 return ERR_PTR(-ENOMEM);
7205
7206 em->start = start;
7207 em->orig_start = orig_start;
7208 em->len = len;
7209 em->block_len = block_len;
7210 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007211 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007212 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007213 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007214 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007215 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007216 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007217 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007218 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7219 em->compress_type = compress_type;
7220 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007221
7222 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007223 btrfs_drop_extent_cache(inode, em->start,
7224 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007225 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007226 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007227 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007228 /*
7229 * The caller has taken lock_extent(), who could race with us
7230 * to add em?
7231 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007232 } while (ret == -EEXIST);
7233
7234 if (ret) {
7235 free_extent_map(em);
7236 return ERR_PTR(ret);
7237 }
7238
Liu Bo6f9994d2017-01-31 07:50:22 -08007239 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007240 return em;
7241}
7242
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007243
David Sterba55e20bd2020-06-09 19:56:06 +02007244static int btrfs_get_blocks_direct_read(struct extent_map *em,
7245 struct buffer_head *bh_result,
7246 struct inode *inode,
7247 u64 start, u64 len)
7248{
7249 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7250
7251 if (em->block_start == EXTENT_MAP_HOLE ||
7252 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7253 return -ENOENT;
7254
7255 len = min(len, em->len - (start - em->start));
7256
7257 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7258 inode->i_blkbits;
7259 bh_result->b_size = len;
7260 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7261 set_buffer_mapped(bh_result);
7262
7263 return 0;
7264}
7265
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007266static int btrfs_get_blocks_direct_write(struct extent_map **map,
David Sterba55e20bd2020-06-09 19:56:06 +02007267 struct buffer_head *bh_result,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007268 struct inode *inode,
7269 struct btrfs_dio_data *dio_data,
7270 u64 start, u64 len)
7271{
7272 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7273 struct extent_map *em = *map;
7274 int ret = 0;
7275
7276 /*
7277 * We don't allocate a new extent in the following cases
7278 *
7279 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7280 * existing extent.
7281 * 2) The extent is marked as PREALLOC. We're good to go here and can
7282 * just use the extent.
7283 *
7284 */
7285 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7286 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7287 em->block_start != EXTENT_MAP_HOLE)) {
7288 int type;
7289 u64 block_start, orig_start, orig_block_len, ram_bytes;
7290
7291 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7292 type = BTRFS_ORDERED_PREALLOC;
7293 else
7294 type = BTRFS_ORDERED_NOCOW;
7295 len = min(len, em->len - (start - em->start));
7296 block_start = em->block_start + (start - em->start);
7297
7298 if (can_nocow_extent(inode, start, &len, &orig_start,
7299 &orig_block_len, &ram_bytes) == 1 &&
7300 btrfs_inc_nocow_writers(fs_info, block_start)) {
7301 struct extent_map *em2;
7302
7303 em2 = btrfs_create_dio_extent(inode, start, len,
7304 orig_start, block_start,
7305 len, orig_block_len,
7306 ram_bytes, type);
7307 btrfs_dec_nocow_writers(fs_info, block_start);
7308 if (type == BTRFS_ORDERED_PREALLOC) {
7309 free_extent_map(em);
7310 *map = em = em2;
7311 }
7312
7313 if (em2 && IS_ERR(em2)) {
7314 ret = PTR_ERR(em2);
7315 goto out;
7316 }
7317 /*
7318 * For inode marked NODATACOW or extent marked PREALLOC,
7319 * use the existing or preallocated extent, so does not
7320 * need to adjust btrfs_space_info's bytes_may_use.
7321 */
Filipe Manana46d4dac2020-06-09 11:19:33 +01007322 btrfs_free_reserved_data_space_noquota(inode, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007323 goto skip_cow;
7324 }
7325 }
7326
7327 /* this will cow the extent */
David Sterba55e20bd2020-06-09 19:56:06 +02007328 len = bh_result->b_size;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007329 free_extent_map(em);
7330 *map = em = btrfs_new_extent_direct(inode, start, len);
7331 if (IS_ERR(em)) {
7332 ret = PTR_ERR(em);
7333 goto out;
7334 }
7335
7336 len = min(len, em->len - (start - em->start));
7337
7338skip_cow:
David Sterba55e20bd2020-06-09 19:56:06 +02007339 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7340 inode->i_blkbits;
7341 bh_result->b_size = len;
7342 bh_result->b_bdev = fs_info->fs_devices->latest_bdev;
7343 set_buffer_mapped(bh_result);
7344
7345 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7346 set_buffer_new(bh_result);
7347
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007348 /*
7349 * Need to update the i_size under the extent lock so buffered
7350 * readers will get the updated i_size when we unlock.
7351 */
David Sterba55e20bd2020-06-09 19:56:06 +02007352 if (!dio_data->overwrite && start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007353 i_size_write(inode, start + len);
7354
David Sterba55e20bd2020-06-09 19:56:06 +02007355 WARN_ON(dio_data->reserve < len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007356 dio_data->reserve -= len;
David Sterba55e20bd2020-06-09 19:56:06 +02007357 dio_data->unsubmitted_oe_range_end = start + len;
7358 current->journal_info = dio_data;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007359out:
7360 return ret;
7361}
7362
David Sterba55e20bd2020-06-09 19:56:06 +02007363static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7364 struct buffer_head *bh_result, int create)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007365{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007366 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007367 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007368 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307369 struct btrfs_dio_data *dio_data = NULL;
David Sterba55e20bd2020-06-09 19:56:06 +02007370 u64 start = iblock << inode->i_blkbits;
Josef Bacikeb838e72012-07-31 16:28:48 -04007371 u64 lockstart, lockend;
David Sterba55e20bd2020-06-09 19:56:06 +02007372 u64 len = bh_result->b_size;
Miao Xie09348562013-02-07 10:12:07 +00007373 int ret = 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007374
David Sterba55e20bd2020-06-09 19:56:06 +02007375 if (!create)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007376 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007377
Josef Bacikc3298612012-08-03 16:49:19 -04007378 lockstart = start;
7379 lockend = start + len - 1;
7380
David Sterba55e20bd2020-06-09 19:56:06 +02007381 if (current->journal_info) {
7382 /*
7383 * Need to pull our outstanding extents and set journal_info to NULL so
7384 * that anything that needs to check if there's a transaction doesn't get
7385 * confused.
7386 */
7387 dio_data = current->journal_info;
7388 current->journal_info = NULL;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007389 }
7390
Josef Bacikeb838e72012-07-31 16:28:48 -04007391 /*
7392 * If this errors out it's because we couldn't invalidate pagecache for
7393 * this range and we need to fallback to buffered.
7394 */
David Sterba55e20bd2020-06-09 19:56:06 +02007395 if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7396 create)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007397 ret = -ENOTBLK;
7398 goto err;
7399 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007400
Omar Sandoval39b07b52019-12-02 17:34:23 -08007401 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007402 if (IS_ERR(em)) {
7403 ret = PTR_ERR(em);
7404 goto unlock_err;
7405 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007406
7407 /*
7408 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7409 * io. INLINE is special, and we could probably kludge it in here, but
7410 * it's still buffered so for safety lets just fall back to the generic
7411 * buffered path.
7412 *
7413 * For COMPRESSED we _have_ to read the entire extent in so we can
7414 * decompress it, so there will be buffering required no matter what we
7415 * do, so go ahead and fallback to buffered.
7416 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007417 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007418 * to buffered IO. Don't blame me, this is the price we pay for using
7419 * the generic code.
7420 */
7421 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7422 em->block_start == EXTENT_MAP_INLINE) {
7423 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007424 ret = -ENOTBLK;
7425 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007426 }
7427
David Sterba55e20bd2020-06-09 19:56:06 +02007428 if (create) {
7429 ret = btrfs_get_blocks_direct_write(&em, bh_result, inode,
7430 dio_data, start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007431 if (ret < 0)
7432 goto unlock_err;
David Sterba55e20bd2020-06-09 19:56:06 +02007433
7434 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
7435 lockend, &cached_state);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007436 } else {
David Sterba55e20bd2020-06-09 19:56:06 +02007437 ret = btrfs_get_blocks_direct_read(em, bh_result, inode,
7438 start, len);
7439 /* Can be negative only if we read from a hole */
7440 if (ret < 0) {
7441 ret = 0;
7442 free_extent_map(em);
7443 goto unlock_err;
7444 }
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007445 /*
7446 * We need to unlock only the end area that we aren't using.
7447 * The rest is going to be unlocked by the endio routine.
7448 */
David Sterba55e20bd2020-06-09 19:56:06 +02007449 lockstart = start + bh_result->b_size;
7450 if (lockstart < lockend) {
7451 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7452 lockstart, lockend, &cached_state);
7453 } else {
7454 free_extent_state(cached_state);
7455 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007456 }
7457
Josef Bacik4b46fce2010-05-23 11:00:55 -04007458 free_extent_map(em);
7459
7460 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007461
7462unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007463 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7464 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007465err:
David Sterba55e20bd2020-06-09 19:56:06 +02007466 if (dio_data)
7467 current->journal_info = dio_data;
Josef Bacikeb838e72012-07-31 16:28:48 -04007468 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007469}
7470
Omar Sandoval769b4f22020-04-16 14:46:22 -07007471static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7472{
7473 /*
7474 * This implies a barrier so that stores to dio_bio->bi_status before
7475 * this and loads of dio_bio->bi_status after this are fully ordered.
7476 */
7477 if (!refcount_dec_and_test(&dip->refs))
7478 return;
7479
7480 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
7481 __endio_write_update_ordered(dip->inode, dip->logical_offset,
7482 dip->bytes,
7483 !dip->dio_bio->bi_status);
7484 } else {
7485 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7486 dip->logical_offset,
7487 dip->logical_offset + dip->bytes - 1);
7488 }
7489
David Sterba55e20bd2020-06-09 19:56:06 +02007490 dio_end_io(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007491 kfree(dip);
7492}
7493
Omar Sandoval77d5d682020-04-16 14:46:25 -07007494static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7495 int mirror_num,
7496 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007497{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007498 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007499 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007500 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007501
Mike Christie37226b22016-06-05 14:31:52 -05007502 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007503
Omar Sandoval5c047a62020-04-16 14:46:24 -07007504 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007505 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007506 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007507
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007508 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007509 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007510 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007511 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007512 return ret;
7513}
7514
Omar Sandoval769b4f22020-04-16 14:46:22 -07007515static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7516 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007517 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007518{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007519 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7520 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007521 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007522 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7523 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007524 struct bio_vec bvec;
7525 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007526 u64 start = io_bio->logical;
7527 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007528 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007529
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007530 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7531 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307532
Liu Bo17347ce2017-05-15 15:33:27 -07007533 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7534 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007535 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007536 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007537 if (uptodate &&
7538 (!csum || !check_data_csum(inode, io_bio, icsum,
7539 bvec.bv_page, pgoff,
7540 start, sectorsize))) {
7541 clean_io_failure(fs_info, failure_tree, io_tree,
7542 start, bvec.bv_page,
7543 btrfs_ino(BTRFS_I(inode)),
7544 pgoff);
7545 } else {
7546 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007547
Omar Sandoval77d5d682020-04-16 14:46:25 -07007548 status = btrfs_submit_read_repair(inode,
7549 &io_bio->bio,
7550 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007551 bvec.bv_page, pgoff,
7552 start,
7553 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007554 io_bio->mirror_num,
7555 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007556 if (status)
7557 err = status;
7558 }
7559 start += sectorsize;
7560 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307561 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307562 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007563 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007564 return err;
7565}
7566
Qu Wenruo524272602017-03-08 10:25:52 +08007567static void __endio_write_update_ordered(struct inode *inode,
7568 const u64 offset, const u64 bytes,
7569 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007570{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007571 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007572 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007573 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007574 u64 ordered_offset = offset;
7575 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007576 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007577
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007578 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Qu Wenruo524272602017-03-08 10:25:52 +08007579 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007580 else
Qu Wenruo524272602017-03-08 10:25:52 +08007581 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007582
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007583 while (ordered_offset < offset + bytes) {
7584 last_offset = ordered_offset;
7585 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
7586 &ordered_offset,
7587 ordered_bytes,
7588 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007589 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7590 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007591 btrfs_queue_work(wq, &ordered->work);
7592 }
7593 /*
7594 * If btrfs_dec_test_ordered_pending does not find any ordered
7595 * extent in the range, we can exit.
7596 */
7597 if (ordered_offset == last_offset)
7598 return;
7599 /*
7600 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007601 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007602 */
7603 if (ordered_offset < offset + bytes) {
7604 ordered_bytes = offset + bytes - ordered_offset;
7605 ordered = NULL;
7606 }
Chris Mason163cf092010-11-28 19:56:33 -05007607 }
Filipe Manana14543772015-11-24 16:23:54 +00007608}
7609
David Sterbad0ee3932018-03-08 14:35:48 +01007610static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007611 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007612{
Josef Bacikc6100a42017-05-05 11:57:13 -04007613 struct inode *inode = private_data;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007614 blk_status_t ret;
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007615 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01007616 BUG_ON(ret); /* -ENOMEM */
Chris Masoneaf25d92010-05-25 09:48:28 -04007617 return 0;
7618}
7619
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007620static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007621{
7622 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007623 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007624
Miao Xie8b110e32014-09-12 18:44:03 +08007625 if (err)
7626 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007627 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007628 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7629 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007630 (unsigned long long)bio->bi_iter.bi_sector,
7631 bio->bi_iter.bi_size, err);
7632
Omar Sandoval769b4f22020-04-16 14:46:22 -07007633 if (bio_op(bio) == REQ_OP_READ) {
7634 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007635 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007636 }
7637
Omar Sandoval769b4f22020-04-16 14:46:22 -07007638 if (err)
7639 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007640
Miao Xiee65e1532010-11-22 03:04:43 +00007641 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007642 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007643}
7644
David Sterbad0ee3932018-03-08 14:35:48 +01007645static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7646 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007647{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007648 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007649 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007650 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007651 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007652
Liu Bo4c274bc2017-11-01 17:19:27 -06007653 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007654 if (async_submit)
7655 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7656
Josef Bacik5fd02042012-05-02 14:00:54 -04007657 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007658 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007659 if (ret)
7660 goto err;
7661 }
Miao Xiee65e1532010-11-22 03:04:43 +00007662
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007663 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007664 goto map;
7665
7666 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007667 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7668 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007669 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007670 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007671 } else if (write) {
7672 /*
7673 * If we aren't doing async submit, calculate the csum of the
7674 * bio now.
7675 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007676 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007677 if (ret)
7678 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007679 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007680 u64 csum_offset;
7681
7682 csum_offset = file_offset - dip->logical_offset;
7683 csum_offset >>= inode->i_sb->s_blocksize_bits;
7684 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7685 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007686 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007687map:
Chris Mason08635ba2019-07-10 12:28:14 -07007688 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007689err:
Miao Xiee65e1532010-11-22 03:04:43 +00007690 return ret;
7691}
7692
Omar Sandovalc36cac22020-04-16 14:46:13 -07007693/*
7694 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7695 * or ordered extents whether or not we submit any bios.
7696 */
7697static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7698 struct inode *inode,
7699 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007700{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007701 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007702 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7703 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007704 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007705
Omar Sandoval85879572020-04-16 14:46:21 -07007706 dip_size = sizeof(*dip);
7707 if (!write && csum) {
7708 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7709 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7710 size_t nblocks;
7711
7712 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7713 dip_size += csum_size * nblocks;
7714 }
7715
7716 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007717 if (!dip)
7718 return NULL;
7719
Omar Sandovalc36cac22020-04-16 14:46:13 -07007720 dip->inode = inode;
7721 dip->logical_offset = file_offset;
7722 dip->bytes = dio_bio->bi_iter.bi_size;
7723 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007724 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007725 refcount_set(&dip->refs, 1);
David Sterba55e20bd2020-06-09 19:56:06 +02007726
7727 if (write) {
7728 struct btrfs_dio_data *dio_data = current->journal_info;
7729
7730 /*
7731 * Setting range start and end to the same value means that
7732 * no cleanup will happen in btrfs_direct_IO
7733 */
7734 dio_data->unsubmitted_oe_range_end = dip->logical_offset +
7735 dip->bytes;
7736 dio_data->unsubmitted_oe_range_start =
7737 dio_data->unsubmitted_oe_range_end;
7738 }
Omar Sandovalc36cac22020-04-16 14:46:13 -07007739 return dip;
7740}
7741
David Sterba55e20bd2020-06-09 19:56:06 +02007742static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
7743 loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007744{
7745 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007746 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007747 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007748 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7749 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007750 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007751 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007752 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007753 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007754 u64 submit_len;
7755 int clone_offset = 0;
7756 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307757 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007758 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007759 struct btrfs_io_geometry geom;
Miao Xiee65e1532010-11-22 03:04:43 +00007760
Omar Sandovalc36cac22020-04-16 14:46:13 -07007761 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7762 if (!dip) {
7763 if (!write) {
7764 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7765 file_offset + dio_bio->bi_iter.bi_size - 1);
7766 }
7767 dio_bio->bi_status = BLK_STS_RESOURCE;
David Sterba55e20bd2020-06-09 19:56:06 +02007768 dio_end_io(dio_bio);
7769 return;
Josef Bacik02f57c72011-04-06 14:25:44 -04007770 }
7771
Omar Sandoval85879572020-04-16 14:46:21 -07007772 if (!write && csum) {
7773 /*
7774 * Load the csums up front to reduce csum tree searches and
7775 * contention when submitting bios.
7776 */
7777 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7778 dip->csums);
7779 if (status != BLK_STS_OK)
7780 goto out_err;
7781 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007782
Omar Sandoval769b4f22020-04-16 14:46:22 -07007783 start_sector = dio_bio->bi_iter.bi_sector;
7784 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007785
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007786 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007787 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7788 start_sector << 9, submit_len,
7789 &geom);
7790 if (ret) {
7791 status = errno_to_blk_status(ret);
7792 goto out_err;
7793 }
7794 ASSERT(geom.len <= INT_MAX);
7795
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007796 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007797
Liu Bo725130b2017-05-16 09:51:39 -07007798 /*
7799 * This will never fail as it's passing GPF_NOFS and
7800 * the allocation is backed by btrfs_bioset.
7801 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007802 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007803 bio->bi_private = dip;
7804 bio->bi_end_io = btrfs_end_dio_bio;
7805 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007806
Liu Bo725130b2017-05-16 09:51:39 -07007807 ASSERT(submit_len >= clone_len);
7808 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007809
Liu Bo725130b2017-05-16 09:51:39 -07007810 /*
7811 * Increase the count before we submit the bio so we know
7812 * the end IO handler won't happen before we increase the
7813 * count. Otherwise, the dip might get freed before we're
7814 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007815 *
7816 * We transfer the initial reference to the last bio, so we
7817 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007818 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007819 if (submit_len > 0) {
7820 refcount_inc(&dip->refs);
7821 /*
7822 * If we are submitting more than one bio, submit them
7823 * all asynchronously. The exception is RAID 5 or 6, as
7824 * asynchronous checksums make it difficult to collect
7825 * full stripe writes.
7826 */
7827 if (!raid56)
7828 async_submit = 1;
7829 }
Miao Xiee65e1532010-11-22 03:04:43 +00007830
David Sterbad0ee3932018-03-08 14:35:48 +01007831 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007832 async_submit);
7833 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007834 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007835 if (submit_len > 0)
7836 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007837 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007838 }
Liu Bo725130b2017-05-16 09:51:39 -07007839
7840 clone_offset += clone_len;
7841 start_sector += clone_len >> 9;
7842 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007843 } while (submit_len > 0);
David Sterba55e20bd2020-06-09 19:56:06 +02007844 return;
Miao Xiee65e1532010-11-22 03:04:43 +00007845
Miao Xiee65e1532010-11-22 03:04:43 +00007846out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007847 dip->dio_bio->bi_status = status;
7848 btrfs_dio_private_put(dip);
Miao Xiee65e1532010-11-22 03:04:43 +00007849}
7850
David Sterbaf4c48b42020-06-09 19:19:27 +02007851static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7852 const struct iov_iter *iter, loff_t offset)
7853{
7854 int seg;
7855 int i;
7856 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7857 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007858
David Sterbaf4c48b42020-06-09 19:19:27 +02007859 if (offset & blocksize_mask)
7860 goto out;
7861
7862 if (iov_iter_alignment(iter) & blocksize_mask)
7863 goto out;
7864
7865 /* If this is a write we don't need to check anymore */
7866 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7867 return 0;
7868 /*
7869 * Check to make sure we don't have duplicate iov_base's in this
7870 * iovec, if so return EINVAL, otherwise we'll get csum errors
7871 * when reading back.
7872 */
7873 for (seg = 0; seg < iter->nr_segs; seg++) {
7874 for (i = seg + 1; i < iter->nr_segs; i++) {
7875 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7876 goto out;
7877 }
7878 }
7879 retval = 0;
7880out:
7881 return retval;
7882}
7883
David Sterba55e20bd2020-06-09 19:56:06 +02007884static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007885{
7886 struct file *file = iocb->ki_filp;
7887 struct inode *inode = file->f_mapping->host;
7888 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba55e20bd2020-06-09 19:56:06 +02007889 struct btrfs_dio_data dio_data = { 0 };
David Sterbaf4c48b42020-06-09 19:19:27 +02007890 struct extent_changeset *data_reserved = NULL;
7891 loff_t offset = iocb->ki_pos;
7892 size_t count = 0;
David Sterba55e20bd2020-06-09 19:56:06 +02007893 int flags = 0;
7894 bool wakeup = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007895 bool relock = false;
7896 ssize_t ret;
7897
7898 if (check_direct_IO(fs_info, iter, offset))
7899 return 0;
7900
David Sterba55e20bd2020-06-09 19:56:06 +02007901 inode_dio_begin(inode);
7902
7903 /*
7904 * The generic stuff only does filemap_write_and_wait_range, which
7905 * isn't enough if we've written compressed pages to this area, so
7906 * we need to flush the dirty pages again to make absolutely sure
7907 * that any outstanding dirty pages are on disk.
7908 */
David Sterbaf4c48b42020-06-09 19:19:27 +02007909 count = iov_iter_count(iter);
David Sterba55e20bd2020-06-09 19:56:06 +02007910 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7911 &BTRFS_I(inode)->runtime_flags))
7912 filemap_fdatawrite_range(inode->i_mapping, offset,
7913 offset + count - 1);
7914
David Sterbaf4c48b42020-06-09 19:19:27 +02007915 if (iov_iter_rw(iter) == WRITE) {
7916 /*
7917 * If the write DIO is beyond the EOF, we need update
7918 * the isize, but it is protected by i_mutex. So we can
7919 * not unlock the i_mutex at this case.
7920 */
7921 if (offset + count <= inode->i_size) {
David Sterba55e20bd2020-06-09 19:56:06 +02007922 dio_data.overwrite = 1;
David Sterbaf4c48b42020-06-09 19:19:27 +02007923 inode_unlock(inode);
7924 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007925 }
David Sterba55e20bd2020-06-09 19:56:06 +02007926 ret = btrfs_delalloc_reserve_space(inode, &data_reserved,
7927 offset, count);
7928 if (ret)
7929 goto out;
7930
7931 /*
7932 * We need to know how many extents we reserved so that we can
7933 * do the accounting properly if we go over the number we
7934 * originally calculated. Abuse current->journal_info for this.
7935 */
7936 dio_data.reserve = round_up(count,
7937 fs_info->sectorsize);
7938 dio_data.unsubmitted_oe_range_start = (u64)offset;
7939 dio_data.unsubmitted_oe_range_end = (u64)offset;
7940 current->journal_info = &dio_data;
David Sterbaf4c48b42020-06-09 19:19:27 +02007941 down_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007942 } else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
7943 &BTRFS_I(inode)->runtime_flags)) {
7944 inode_dio_end(inode);
7945 flags = DIO_LOCKING | DIO_SKIP_HOLES;
7946 wakeup = false;
David Sterbaf4c48b42020-06-09 19:19:27 +02007947 }
7948
David Sterba55e20bd2020-06-09 19:56:06 +02007949 ret = __blockdev_direct_IO(iocb, inode,
7950 fs_info->fs_devices->latest_bdev,
7951 iter, btrfs_get_blocks_direct, NULL,
7952 btrfs_submit_direct, flags);
David Sterbaf4c48b42020-06-09 19:19:27 +02007953 if (iov_iter_rw(iter) == WRITE) {
7954 up_read(&BTRFS_I(inode)->dio_sem);
David Sterba55e20bd2020-06-09 19:56:06 +02007955 current->journal_info = NULL;
7956 if (ret < 0 && ret != -EIOCBQUEUED) {
7957 if (dio_data.reserve)
7958 btrfs_delalloc_release_space(inode, data_reserved,
7959 offset, dio_data.reserve, true);
7960 /*
7961 * On error we might have left some ordered extents
7962 * without submitting corresponding bios for them, so
7963 * cleanup them up to avoid other tasks getting them
7964 * and waiting for them to complete forever.
7965 */
7966 if (dio_data.unsubmitted_oe_range_start <
7967 dio_data.unsubmitted_oe_range_end)
7968 __endio_write_update_ordered(inode,
7969 dio_data.unsubmitted_oe_range_start,
7970 dio_data.unsubmitted_oe_range_end -
7971 dio_data.unsubmitted_oe_range_start,
7972 false);
7973 } else if (ret >= 0 && (size_t)ret < count)
7974 btrfs_delalloc_release_space(inode, data_reserved,
7975 offset, count - (size_t)ret, true);
7976 btrfs_delalloc_release_extents(BTRFS_I(inode), count);
David Sterbaf4c48b42020-06-09 19:19:27 +02007977 }
7978out:
David Sterba55e20bd2020-06-09 19:56:06 +02007979 if (wakeup)
7980 inode_dio_end(inode);
David Sterbaf4c48b42020-06-09 19:19:27 +02007981 if (relock)
7982 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02007983
David Sterbaf4c48b42020-06-09 19:19:27 +02007984 extent_changeset_free(data_reserved);
7985 return ret;
7986}
Chris Mason16432982008-04-10 10:23:21 -04007987
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007988static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02007989 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007990{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007991 int ret;
7992
Christoph Hellwig45dd0522020-05-23 09:30:14 +02007993 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00007994 if (ret)
7995 return ret;
7996
David Sterba2135fb92017-06-23 04:09:57 +02007997 return extent_fiemap(inode, fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05007998}
7999
Chris Mason9ebefb182007-06-15 13:50:00 -04008000int btrfs_readpage(struct file *file, struct page *page)
8001{
David Sterba71ad38b2020-02-05 19:09:35 +01008002 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008003}
Chris Mason1832a6d2007-12-21 16:27:21 -05008004
Chris Mason39279cc2007-06-12 06:35:45 -04008005static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8006{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008007 struct inode *inode = page->mapping->host;
8008 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008009
8010 if (current->flags & PF_MEMALLOC) {
8011 redirty_page_for_writepage(wbc, page);
8012 unlock_page(page);
8013 return 0;
8014 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008015
8016 /*
8017 * If we are under memory pressure we will call this directly from the
8018 * VM, we need to make sure we have the inode referenced for the ordered
8019 * extent. If not just return like we didn't do anything.
8020 */
8021 if (!igrab(inode)) {
8022 redirty_page_for_writepage(wbc, page);
8023 return AOP_WRITEPAGE_ACTIVATE;
8024 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008025 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008026 btrfs_add_delayed_iput(inode);
8027 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008028}
Chris Mason39279cc2007-06-12 06:35:45 -04008029
Eric Sandeen48a3b632013-04-25 20:41:01 +00008030static int btrfs_writepages(struct address_space *mapping,
8031 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008032{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008033 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008034}
8035
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008036static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008037{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008038 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008039}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008040
Chris Masone6dcd2d2008-07-17 12:53:50 -04008041static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008042{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008043 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008044 if (ret == 1)
8045 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008046 return ret;
8047}
Chris Mason39279cc2007-06-12 06:35:45 -04008048
Chris Masone6dcd2d2008-07-17 12:53:50 -04008049static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8050{
Chris Mason98509cf2008-09-11 15:51:43 -04008051 if (PageWriteback(page) || PageDirty(page))
8052 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008053 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008054}
8055
Roman Gushchinf8e66082020-03-04 16:57:35 -08008056#ifdef CONFIG_MIGRATION
8057static int btrfs_migratepage(struct address_space *mapping,
8058 struct page *newpage, struct page *page,
8059 enum migrate_mode mode)
8060{
8061 int ret;
8062
8063 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8064 if (ret != MIGRATEPAGE_SUCCESS)
8065 return ret;
8066
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008067 if (page_has_private(page))
8068 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008069
8070 if (PagePrivate2(page)) {
8071 ClearPagePrivate2(page);
8072 SetPagePrivate2(newpage);
8073 }
8074
8075 if (mode != MIGRATE_SYNC_NO_COPY)
8076 migrate_page_copy(newpage, page);
8077 else
8078 migrate_page_states(newpage, page);
8079 return MIGRATEPAGE_SUCCESS;
8080}
8081#endif
8082
Lukas Czernerd47992f2013-05-21 23:17:23 -04008083static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8084 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008085{
Josef Bacik5fd02042012-05-02 14:00:54 -04008086 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05008087 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008088 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008089 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008090 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008091 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308092 u64 start;
8093 u64 end;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008094 int inode_evicting = inode->i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008095
Chris Mason8b62b722009-09-02 16:53:46 -04008096 /*
8097 * we have the page locked, so new writeback can't start,
8098 * and the dirty bit won't be cleared while we are here.
8099 *
8100 * Wait for IO on this page so that we can safely clear
8101 * the PagePrivate2 bit and do ordered accounting
8102 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008103 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008104
Josef Bacik5fd02042012-05-02 14:00:54 -04008105 tree = &BTRFS_I(inode)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008106 if (offset) {
8107 btrfs_releasepage(page, GFP_NOFS);
8108 return;
8109 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008110
8111 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008112 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308113again:
8114 start = page_start;
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008115 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308116 page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008117 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008118 end = min(page_end,
8119 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008120 /*
8121 * IO on this page will never be started, so we need
8122 * to account for any ordered extents now
8123 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008124 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308125 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008126 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008127 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008128 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008129 /*
8130 * whoever cleared the private bit is responsible
8131 * for the finish_ordered_io
8132 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008133 if (TestClearPagePrivate2(page)) {
8134 struct btrfs_ordered_inode_tree *tree;
8135 u64 new_len;
8136
8137 tree = &BTRFS_I(inode)->ordered_tree;
8138
8139 spin_lock_irq(&tree->lock);
8140 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308141 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008142 if (new_len < ordered->truncated_len)
8143 ordered->truncated_len = new_len;
8144 spin_unlock_irq(&tree->lock);
8145
8146 if (btrfs_dec_test_ordered_pending(inode, &ordered,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308147 start,
8148 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008149 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008150 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008151 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008152 if (!inode_evicting) {
8153 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308154 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008155 &cached_state);
8156 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308157
8158 start = end + 1;
8159 if (start < page_end)
8160 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008161 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008162
Qu Wenruob9d0b382015-09-29 10:35:16 +08008163 /*
8164 * Qgroup reserved space handler
8165 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008166 * 1) Already written to disk or ordered extent already submitted
8167 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8168 * Qgroup will be handled by its qgroup_record then.
8169 * btrfs_qgroup_free_data() call will do nothing here.
8170 *
8171 * 2) Not written to disk yet
8172 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8173 * bit of its io_tree, and free the qgroup reserved data space.
8174 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008175 */
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03008176 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008177 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008178 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008179 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8180 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008181 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008182
8183 __btrfs_releasepage(page, GFP_NOFS);
8184 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008185
Chris Mason4a096752008-07-21 10:29:44 -04008186 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008187 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008188}
8189
Chris Mason9ebefb182007-06-15 13:50:00 -04008190/*
8191 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8192 * called from a page fault handler when a page is first dirtied. Hence we must
8193 * be careful to check for EOF conditions here. We set the page up correctly
8194 * for a written page which means we get ENOSPC checking when writing into
8195 * holes and correct delalloc and unwritten extent mapping on filesystems that
8196 * support these features.
8197 *
8198 * We are not allowed to take the i_mutex here so we have to play games to
8199 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008200 * truncate_setsize() writes the inode size before removing pages, once we have
8201 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008202 * beyond EOF, then the page is guaranteed safe against truncation until we
8203 * unlock the page.
8204 */
Souptick Joardera528a242018-06-06 19:54:44 +05308205vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008206{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008207 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008208 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008209 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008210 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8211 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008212 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008213 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008214 char *kaddr;
8215 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008216 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308217 vm_fault_t ret;
8218 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008219 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308220 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008221 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008222 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308223 u64 end;
8224
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008225 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008226
Jan Karab2b5ef52012-06-12 16:20:45 +02008227 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008228 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008229 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308230 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008231
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308232 /*
8233 * Reserving delalloc space after obtaining the page lock can lead to
8234 * deadlock. For example, if a dirty page is locked by this function
8235 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8236 * dirty page write out, then the btrfs_writepage() function could
8237 * end up waiting indefinitely to get a lock on the page currently
8238 * being processed by btrfs_page_mkwrite() function.
8239 */
Souptick Joardera528a242018-06-06 19:54:44 +05308240 ret2 = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308241 reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308242 if (!ret2) {
8243 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008244 reserved = 1;
8245 }
Souptick Joardera528a242018-06-06 19:54:44 +05308246 if (ret2) {
8247 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008248 if (reserved)
8249 goto out;
8250 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008251 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008252
Nick Piggin56a76f82009-03-31 15:23:23 -07008253 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008254again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008255 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008256 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008257
Chris Mason9ebefb182007-06-15 13:50:00 -04008258 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008259 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008260 /* page got truncated out from underneath us */
8261 goto out_unlock;
8262 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008263 wait_on_page_writeback(page);
8264
David Sterbaff13db42015-12-03 14:30:40 +01008265 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008266 set_page_extent_mapped(page);
8267
Chris Masoneb84ae02008-07-17 13:53:27 -04008268 /*
8269 * we can't set the delalloc bits if there are pending ordered
8270 * extents. Drop our locks and wait for them to finish
8271 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008272 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8273 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008274 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008275 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008276 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008277 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008278 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008279 btrfs_put_ordered_extent(ordered);
8280 goto again;
8281 }
8282
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008283 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008284 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008285 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008286 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308287 end = page_start + reserved_space - 1;
Qu Wenruobc42bda2017-02-27 15:10:39 +08008288 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08008289 page_start, PAGE_SIZE - reserved_space,
8290 true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308291 }
8292 }
8293
Josef Bacikfbf19082009-10-01 17:10:23 -04008294 /*
Liu Bo54160342016-12-13 12:15:19 -08008295 * page_mkwrite gets called when the page is firstly dirtied after it's
8296 * faulted in, but write(2) could also dirty a page and set delalloc
8297 * bits, thus in this case for space account reason, we still need to
8298 * clear any delalloc bits within this page range since we have to
8299 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008300 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308301 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008302 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8303 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008304
Souptick Joardera528a242018-06-06 19:54:44 +05308305 ret2 = btrfs_set_extent_delalloc(inode, page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008306 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308307 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008308 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008309 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008310 ret = VM_FAULT_SIGBUS;
8311 goto out_unlock;
8312 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008313
8314 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008315 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008316 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008317 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008318 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008319
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008320 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008321 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008322 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008323 flush_dcache_page(page);
8324 kunmap(page);
8325 }
Chris Mason247e7432008-07-17 12:53:51 -04008326 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008327 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008328 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008329
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008330 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008331 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008332 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008333
David Sterbae43bbe52017-12-12 21:43:52 +01008334 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008335
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008336 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8337 sb_end_pagefault(inode->i_sb);
8338 extent_changeset_free(data_reserved);
8339 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008340
8341out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008342 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008343out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008344 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Qu Wenruobc42bda2017-02-27 15:10:39 +08008345 btrfs_delalloc_release_space(inode, data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008346 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008347out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008348 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008349 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008350 return ret;
8351}
8352
Filipe Manana213e8c52018-02-06 20:40:31 +00008353static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008354{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008355 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008356 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008357 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008358 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008359 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008360 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008361 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008362
Filipe Manana213e8c52018-02-06 20:40:31 +00008363 if (!skip_writeback) {
8364 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8365 (u64)-1);
8366 if (ret)
8367 return ret;
8368 }
Chris Mason39279cc2007-06-12 06:35:45 -04008369
Josef Bacikfcb80c22011-05-03 10:40:22 -04008370 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008371 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8372 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008373 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008374 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008375 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008376 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008377 * be free'd up by the truncate operation, but also have some slack
8378 * space reserved in case it uses space during the truncate (thank you
8379 * very much snapshotting).
8380 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008381 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008382 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008383 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008384 * doesn't end up using space reserved for updating the inode. We also
8385 * need to be able to stop the transaction and start a new one, which
8386 * means we need to be able to update the inode several times, and we
8387 * have no idea of knowing how many times that will be, so we can't just
8388 * reserve 1 item for the entirety of the operation, so that has to be
8389 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008390 *
8391 * So that leaves us with
8392 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008393 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008394 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008395 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008396 * updating the inode.
8397 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008398 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008399 if (!rsv)
8400 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008401 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008402 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008403
Josef Bacik907cbce2011-08-08 13:46:15 -04008404 /*
Josef Bacik07127182011-08-19 10:29:59 -04008405 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008406 * 1 for updating the inode.
8407 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008408 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008409 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008410 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008411 goto out;
8412 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008413
Josef Bacik907cbce2011-08-08 13:46:15 -04008414 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008415 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008416 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008417 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008418
Chris Mason5a3f23d2009-03-31 13:27:11 -04008419 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008420 * So if we truncate and then write and fsync we normally would just
8421 * write the extents that changed, which is a problem if we need to
8422 * first truncate that entire inode. So set this flag so we write out
8423 * all of the extents in the inode to the sync log so we're completely
8424 * safe.
8425 */
8426 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008427 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008428
Yan, Zheng80825102009-11-12 09:35:36 +00008429 while (1) {
8430 ret = btrfs_truncate_inode_items(trans, root, inode,
8431 inode->i_size,
8432 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008433 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008434 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008435 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008436
Yan, Zheng80825102009-11-12 09:35:36 +00008437 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008438 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008439 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008440
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008441 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008442 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008443
8444 trans = btrfs_start_transaction(root, 2);
8445 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008446 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008447 trans = NULL;
8448 break;
8449 }
8450
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008451 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008452 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008453 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008454 BUG_ON(ret); /* shouldn't happen */
8455 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008456 }
8457
Josef Bacikddfae632017-10-19 14:16:02 -04008458 /*
8459 * We can't call btrfs_truncate_block inside a trans handle as we could
8460 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8461 * we've truncated everything except the last little bit, and can do
8462 * btrfs_truncate_block and then update the disk_i_size.
8463 */
8464 if (ret == NEED_TRUNCATE_BLOCK) {
8465 btrfs_end_transaction(trans);
8466 btrfs_btree_balance_dirty(fs_info);
8467
8468 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8469 if (ret)
8470 goto out;
8471 trans = btrfs_start_transaction(root, 1);
8472 if (IS_ERR(trans)) {
8473 ret = PTR_ERR(trans);
8474 goto out;
8475 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008476 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008477 }
8478
Chris Mason917c16b2011-11-08 14:49:59 -05008479 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008480 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008481
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008482 trans->block_rsv = &fs_info->trans_block_rsv;
8483 ret2 = btrfs_update_inode(trans, root, inode);
8484 if (ret2 && !ret)
8485 ret = ret2;
8486
8487 ret2 = btrfs_end_transaction(trans);
8488 if (ret2 && !ret)
8489 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008490 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008491 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008492out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008493 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008494
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008495 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008496}
8497
Sven Wegener3b963622008-06-09 21:57:42 -04008498/*
Chris Masond352ac62008-09-29 15:18:18 -04008499 * create a new subvolume directory/inode (helper for the ioctl).
8500 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008501int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008502 struct btrfs_root *new_root,
8503 struct btrfs_root *parent_root,
8504 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008505{
Chris Mason39279cc2007-06-12 06:35:45 -04008506 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008507 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008508 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008509
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008510 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8511 new_dirid, new_dirid,
8512 S_IFDIR | (~current_umask() & S_IRWXUGO),
8513 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008514 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008515 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008516 inode->i_op = &btrfs_dir_inode_operations;
8517 inode->i_fop = &btrfs_dir_file_operations;
8518
Miklos Szeredibfe86842011-10-28 14:13:29 +02008519 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008520 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008521 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008522
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008523 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8524 if (err)
8525 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008526 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008527 new_root->root_key.objectid, err);
8528
Yan, Zheng76dda932009-09-21 16:00:26 -04008529 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008530
Yan, Zheng76dda932009-09-21 16:00:26 -04008531 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008532 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008533}
8534
Chris Mason39279cc2007-06-12 06:35:45 -04008535struct inode *btrfs_alloc_inode(struct super_block *sb)
8536{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008537 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008538 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008539 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008540
David Sterba712e36c2017-10-31 17:08:27 +01008541 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008542 if (!ei)
8543 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008544
8545 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008546 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008547 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008548 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008549 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008550 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008551 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008552 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008553 ei->disk_i_size = 0;
8554 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008555 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008556 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008557 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008558 ei->last_unlink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008559 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008560
Josef Bacik9e0baf62011-07-15 15:16:44 +00008561 spin_lock_init(&ei->lock);
8562 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008563 if (sb->s_magic != BTRFS_TEST_MAGIC)
8564 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8565 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008566 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008567 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008568 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008569
Miao Xie16cdcec2011-04-22 18:12:22 +08008570 ei->delayed_node = NULL;
8571
chandan r9cc97d62012-07-04 12:48:07 +05308572 ei->i_otime.tv_sec = 0;
8573 ei->i_otime.tv_nsec = 0;
8574
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008575 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008576 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008577 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8578 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8579 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008580 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8581 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008582 ei->io_tree.track_uptodate = true;
8583 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008584 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008585 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008586 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008587 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008588 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008589 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008590 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008591
8592 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008593}
8594
Josef Bacikaaedb552013-10-11 14:44:09 -04008595#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8596void btrfs_test_destroy_inode(struct inode *inode)
8597{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008598 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008599 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8600}
8601#endif
8602
Al Viro26602ca2019-04-10 15:14:41 -04008603void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008604{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008605 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8606}
8607
Chris Mason39279cc2007-06-12 06:35:45 -04008608void btrfs_destroy_inode(struct inode *inode)
8609{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008610 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008611 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008612 struct btrfs_root *root = BTRFS_I(inode)->root;
8613
Al Virob3d9b7a2012-06-09 13:51:19 -04008614 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008615 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008616 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8617 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008618 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008619 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008620 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008621 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008622 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008623
Chris Mason5a3f23d2009-03-31 13:27:11 -04008624 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008625 * This can happen where we create an inode, but somebody else also
8626 * created the same inode and we need to destroy the one we already
8627 * created.
8628 */
8629 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008630 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008631
Chris Masond3977122009-01-05 21:25:51 -05008632 while (1) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008633 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
8634 if (!ordered)
8635 break;
8636 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008637 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008638 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008639 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008640 btrfs_remove_ordered_extent(inode, ordered);
8641 btrfs_put_ordered_extent(ordered);
8642 btrfs_put_ordered_extent(ordered);
8643 }
8644 }
Qu Wenruo56fa9d02015-10-13 09:53:10 +08008645 btrfs_qgroup_check_reserved_leak(inode);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04008646 inode_tree_del(inode);
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008647 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008648 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008649 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008650}
8651
Al Viro45321ac2010-06-07 13:43:19 -04008652int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008653{
8654 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008655
Naohiro Aota6379ef92013-06-06 09:56:34 +00008656 if (root == NULL)
8657 return 1;
8658
Liu Bofa6ac872013-02-20 14:10:23 +00008659 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008660 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008661 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008662 else
Al Viro45321ac2010-06-07 13:43:19 -04008663 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008664}
8665
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008666static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008667{
8668 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8669
8670 inode_init_once(&ei->vfs_inode);
8671}
8672
David Sterbae67c7182018-02-19 17:24:18 +01008673void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008674{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008675 /*
8676 * Make sure all delayed rcu free inodes are flushed before we
8677 * destroy cache.
8678 */
8679 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008680 kmem_cache_destroy(btrfs_inode_cachep);
8681 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008682 kmem_cache_destroy(btrfs_path_cachep);
8683 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008684 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008685}
8686
Liu Bof5c29bd2017-11-02 17:21:50 -06008687int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008688{
David Sterba837e1972012-09-07 03:00:48 -06008689 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008690 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008691 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8692 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008693 if (!btrfs_inode_cachep)
8694 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008695
David Sterba837e1972012-09-07 03:00:48 -06008696 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008697 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008698 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008699 if (!btrfs_trans_handle_cachep)
8700 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008701
David Sterba837e1972012-09-07 03:00:48 -06008702 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008703 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008704 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008705 if (!btrfs_path_cachep)
8706 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008707
David Sterba837e1972012-09-07 03:00:48 -06008708 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008709 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008710 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008711 if (!btrfs_free_space_cachep)
8712 goto fail;
8713
Christophe Leroy3acd4852019-08-21 15:05:55 +00008714 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8715 PAGE_SIZE, PAGE_SIZE,
8716 SLAB_RED_ZONE, NULL);
8717 if (!btrfs_free_space_bitmap_cachep)
8718 goto fail;
8719
Chris Mason39279cc2007-06-12 06:35:45 -04008720 return 0;
8721fail:
8722 btrfs_destroy_cachep();
8723 return -ENOMEM;
8724}
8725
David Howellsa528d352017-01-31 16:46:22 +00008726static int btrfs_getattr(const struct path *path, struct kstat *stat,
8727 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008728{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008729 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008730 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008731 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008732 u32 bi_flags = BTRFS_I(inode)->flags;
8733
8734 stat->result_mask |= STATX_BTIME;
8735 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8736 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8737 if (bi_flags & BTRFS_INODE_APPEND)
8738 stat->attributes |= STATX_ATTR_APPEND;
8739 if (bi_flags & BTRFS_INODE_COMPRESS)
8740 stat->attributes |= STATX_ATTR_COMPRESSED;
8741 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8742 stat->attributes |= STATX_ATTR_IMMUTABLE;
8743 if (bi_flags & BTRFS_INODE_NODUMP)
8744 stat->attributes |= STATX_ATTR_NODUMP;
8745
8746 stat->attributes_mask |= (STATX_ATTR_APPEND |
8747 STATX_ATTR_COMPRESSED |
8748 STATX_ATTR_IMMUTABLE |
8749 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008750
Chris Mason39279cc2007-06-12 06:35:45 -04008751 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008752 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008753
8754 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008755 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008756 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008757 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008758 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008759 return 0;
8760}
8761
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008762static int btrfs_rename_exchange(struct inode *old_dir,
8763 struct dentry *old_dentry,
8764 struct inode *new_dir,
8765 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008766{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008767 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008768 struct btrfs_trans_handle *trans;
8769 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008770 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008771 struct inode *new_inode = new_dentry->d_inode;
8772 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008773 struct timespec64 ctime = current_time(old_inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008774 struct dentry *parent;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008775 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8776 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008777 u64 old_idx = 0;
8778 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008779 int ret;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008780 bool root_log_pinned = false;
8781 bool dest_log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008782 struct btrfs_log_ctx ctx_root;
8783 struct btrfs_log_ctx ctx_dest;
8784 bool sync_log_root = false;
8785 bool sync_log_dest = false;
8786 bool commit_transaction = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008787
8788 /* we only allow rename subvolume link between subvolumes */
8789 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8790 return -EXDEV;
8791
Filipe Mananad4682ba2018-06-11 19:24:28 +01008792 btrfs_init_log_ctx(&ctx_root, old_inode);
8793 btrfs_init_log_ctx(&ctx_dest, new_inode);
8794
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008795 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008796 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8797 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008798 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008799
8800 /*
8801 * We want to reserve the absolute worst case amount of items. So if
8802 * both inodes are subvols and we need to unlink them then that would
8803 * require 4 item modifications, but if they are both normal inodes it
8804 * would require 5 item modifications, so we'll assume their normal
8805 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8806 * should cover the worst case number of items we'll modify.
8807 */
8808 trans = btrfs_start_transaction(root, 12);
8809 if (IS_ERR(trans)) {
8810 ret = PTR_ERR(trans);
8811 goto out_notrans;
8812 }
8813
Josef Bacik3e174092019-11-15 15:43:06 -05008814 if (dest != root)
8815 btrfs_record_root_in_trans(trans, dest);
8816
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008817 /*
8818 * We need to find a free sequence number both in the source and
8819 * in the destination directory for the exchange.
8820 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008821 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008822 if (ret)
8823 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008824 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008825 if (ret)
8826 goto out_fail;
8827
8828 BTRFS_I(old_inode)->dir_index = 0ULL;
8829 BTRFS_I(new_inode)->dir_index = 0ULL;
8830
8831 /* Reference for the source. */
8832 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8833 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008834 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008835 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008836 btrfs_pin_log_trans(root);
8837 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008838 ret = btrfs_insert_inode_ref(trans, dest,
8839 new_dentry->d_name.name,
8840 new_dentry->d_name.len,
8841 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008842 btrfs_ino(BTRFS_I(new_dir)),
8843 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008844 if (ret)
8845 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008846 }
8847
8848 /* And now for the dest. */
8849 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8850 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008851 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008852 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008853 btrfs_pin_log_trans(dest);
8854 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008855 ret = btrfs_insert_inode_ref(trans, root,
8856 old_dentry->d_name.name,
8857 old_dentry->d_name.len,
8858 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008859 btrfs_ino(BTRFS_I(old_dir)),
8860 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008861 if (ret)
8862 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008863 }
8864
8865 /* Update inode version and ctime/mtime. */
8866 inode_inc_iversion(old_dir);
8867 inode_inc_iversion(new_dir);
8868 inode_inc_iversion(old_inode);
8869 inode_inc_iversion(new_inode);
8870 old_dir->i_ctime = old_dir->i_mtime = ctime;
8871 new_dir->i_ctime = new_dir->i_mtime = ctime;
8872 old_inode->i_ctime = ctime;
8873 new_inode->i_ctime = ctime;
8874
8875 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008876 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8877 BTRFS_I(old_inode), 1);
8878 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8879 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008880 }
8881
8882 /* src is a subvolume */
8883 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008884 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008885 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008886 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8887 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008888 old_dentry->d_name.name,
8889 old_dentry->d_name.len);
8890 if (!ret)
8891 ret = btrfs_update_inode(trans, root, old_inode);
8892 }
8893 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008894 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008895 goto out_fail;
8896 }
8897
8898 /* dest is a subvolume */
8899 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008900 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008901 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008902 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8903 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008904 new_dentry->d_name.name,
8905 new_dentry->d_name.len);
8906 if (!ret)
8907 ret = btrfs_update_inode(trans, dest, new_inode);
8908 }
8909 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008910 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008911 goto out_fail;
8912 }
8913
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008914 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008915 new_dentry->d_name.name,
8916 new_dentry->d_name.len, 0, old_idx);
8917 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008918 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008919 goto out_fail;
8920 }
8921
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008922 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008923 old_dentry->d_name.name,
8924 old_dentry->d_name.len, 0, new_idx);
8925 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008926 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008927 goto out_fail;
8928 }
8929
8930 if (old_inode->i_nlink == 1)
8931 BTRFS_I(old_inode)->dir_index = old_idx;
8932 if (new_inode->i_nlink == 1)
8933 BTRFS_I(new_inode)->dir_index = new_idx;
8934
Filipe Manana86e8aa02016-05-05 02:02:27 +01008935 if (root_log_pinned) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008936 parent = new_dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01008937 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
8938 BTRFS_I(old_dir), parent,
8939 false, &ctx_root);
8940 if (ret == BTRFS_NEED_LOG_SYNC)
8941 sync_log_root = true;
8942 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8943 commit_transaction = true;
8944 ret = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008945 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008946 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008947 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008948 if (dest_log_pinned) {
Filipe Mananad4682ba2018-06-11 19:24:28 +01008949 if (!commit_transaction) {
8950 parent = old_dentry->d_parent;
8951 ret = btrfs_log_new_name(trans, BTRFS_I(new_inode),
8952 BTRFS_I(new_dir), parent,
8953 false, &ctx_dest);
8954 if (ret == BTRFS_NEED_LOG_SYNC)
8955 sync_log_dest = true;
8956 else if (ret == BTRFS_NEED_TRANS_COMMIT)
8957 commit_transaction = true;
8958 ret = 0;
8959 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008960 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008961 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008962 }
8963out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008964 /*
8965 * If we have pinned a log and an error happened, we unpin tasks
8966 * trying to sync the log and force them to fallback to a transaction
8967 * commit if the log currently contains any of the inodes involved in
8968 * this rename operation (to ensure we do not persist a log with an
8969 * inconsistent state for any of these inodes or leading to any
8970 * inconsistencies when replayed). If the transaction was aborted, the
8971 * abortion reason is propagated to userspace when attempting to commit
8972 * the transaction. If the log does not contain any of these inodes, we
8973 * allow the tasks to sync it.
8974 */
8975 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008976 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8977 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8978 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008979 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008980 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008981 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008982
8983 if (root_log_pinned) {
8984 btrfs_end_log_trans(root);
8985 root_log_pinned = false;
8986 }
8987 if (dest_log_pinned) {
8988 btrfs_end_log_trans(dest);
8989 dest_log_pinned = false;
8990 }
8991 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01008992 if (!ret && sync_log_root && !commit_transaction) {
8993 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root,
8994 &ctx_root);
8995 if (ret)
8996 commit_transaction = true;
8997 }
8998 if (!ret && sync_log_dest && !commit_transaction) {
8999 ret = btrfs_sync_log(trans, BTRFS_I(new_inode)->root,
9000 &ctx_dest);
9001 if (ret)
9002 commit_transaction = true;
9003 }
9004 if (commit_transaction) {
Filipe Mananae6c61712019-11-08 16:11:56 +00009005 /*
9006 * We may have set commit_transaction when logging the new name
9007 * in the destination root, in which case we left the source
9008 * root context in the list of log contextes. So make sure we
9009 * remove it to avoid invalid memory accesses, since the context
9010 * was allocated in our stack frame.
9011 */
9012 if (sync_log_root) {
9013 mutex_lock(&root->log_mutex);
9014 list_del_init(&ctx_root.list);
9015 mutex_unlock(&root->log_mutex);
9016 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009017 ret = btrfs_commit_transaction(trans);
9018 } else {
9019 int ret2;
9020
9021 ret2 = btrfs_end_transaction(trans);
9022 ret = ret ? ret : ret2;
9023 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009024out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009025 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9026 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009027 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009028
Filipe Mananae6c61712019-11-08 16:11:56 +00009029 ASSERT(list_empty(&ctx_root.list));
9030 ASSERT(list_empty(&ctx_dest.list));
9031
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009032 return ret;
9033}
9034
9035static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9036 struct btrfs_root *root,
9037 struct inode *dir,
9038 struct dentry *dentry)
9039{
9040 int ret;
9041 struct inode *inode;
9042 u64 objectid;
9043 u64 index;
9044
9045 ret = btrfs_find_free_ino(root, &objectid);
9046 if (ret)
9047 return ret;
9048
9049 inode = btrfs_new_inode(trans, root, dir,
9050 dentry->d_name.name,
9051 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009052 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009053 objectid,
9054 S_IFCHR | WHITEOUT_MODE,
9055 &index);
9056
9057 if (IS_ERR(inode)) {
9058 ret = PTR_ERR(inode);
9059 return ret;
9060 }
9061
9062 inode->i_op = &btrfs_special_inode_operations;
9063 init_special_inode(inode, inode->i_mode,
9064 WHITEOUT_DEV);
9065
9066 ret = btrfs_init_inode_security(trans, inode, dir,
9067 &dentry->d_name);
9068 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009069 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009070
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009071 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9072 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009073 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009074 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009075
9076 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009077out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009078 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009079 if (ret)
9080 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009081 iput(inode);
9082
Filipe Mananac9901612016-05-05 01:41:57 +01009083 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009084}
9085
Chris Mason39279cc2007-06-12 06:35:45 -04009086static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009087 struct inode *new_dir, struct dentry *new_dentry,
9088 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009089{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009090 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009091 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009092 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009093 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9094 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009095 struct inode *new_inode = d_inode(new_dentry);
9096 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009097 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009098 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009099 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009100 bool log_pinned = false;
Filipe Mananad4682ba2018-06-11 19:24:28 +01009101 struct btrfs_log_ctx ctx;
9102 bool sync_log = false;
9103 bool commit_transaction = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009104
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009105 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009106 return -EPERM;
9107
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009108 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009109 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009110 return -EXDEV;
9111
Li Zefan33345d012011-04-20 10:31:50 +08009112 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009113 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009114 return -ENOTEMPTY;
9115
Chris Mason39279cc2007-06-12 06:35:45 -04009116 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009117 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009118 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009119
9120
9121 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009122 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009123 new_dentry->d_name.name,
9124 new_dentry->d_name.len);
9125
9126 if (ret) {
9127 if (ret == -EEXIST) {
9128 /* we shouldn't get
9129 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309130 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009131 return ret;
9132 }
9133 } else {
9134 /* maybe -EOVERFLOW */
9135 return ret;
9136 }
9137 }
9138 ret = 0;
9139
Chris Mason5a3f23d2009-03-31 13:27:11 -04009140 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009141 * we're using rename to replace one file with another. Start IO on it
9142 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009143 */
Chris Mason8d875f92014-08-12 10:47:42 -07009144 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009145 filemap_flush(old_inode->i_mapping);
9146
Yan, Zheng76dda932009-09-21 16:00:26 -04009147 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009148 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009149 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009150 /*
9151 * We want to reserve the absolute worst case amount of items. So if
9152 * both inodes are subvols and we need to unlink them then that would
9153 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009154 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009155 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9156 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009157 * If our rename has the whiteout flag, we need more 5 units for the
9158 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9159 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009160 */
Filipe Manana5062af32016-05-05 10:26:26 +01009161 trans_num_items = 11;
9162 if (flags & RENAME_WHITEOUT)
9163 trans_num_items += 5;
9164 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009165 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009166 ret = PTR_ERR(trans);
9167 goto out_notrans;
9168 }
Chris Mason5f39d392007-10-15 16:14:19 -04009169
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009170 if (dest != root)
9171 btrfs_record_root_in_trans(trans, dest);
9172
Nikolay Borisov877574e2017-02-20 13:50:33 +02009173 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009174 if (ret)
9175 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009176
Miao Xie67de1172013-12-26 13:07:06 +08009177 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009178 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009179 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009180 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009181 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009182 btrfs_pin_log_trans(root);
9183 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009184 ret = btrfs_insert_inode_ref(trans, dest,
9185 new_dentry->d_name.name,
9186 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009187 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009188 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009189 if (ret)
9190 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009191 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009192
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009193 inode_inc_iversion(old_dir);
9194 inode_inc_iversion(new_dir);
9195 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009196 old_dir->i_ctime = old_dir->i_mtime =
9197 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009198 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009199
Chris Mason12fcfd22009-03-24 10:24:20 -04009200 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009201 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9202 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009203
Li Zefan33345d012011-04-20 10:31:50 +08009204 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009205 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009206 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009207 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9208 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009209 old_dentry->d_name.name,
9210 old_dentry->d_name.len);
9211 if (!ret)
9212 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009213 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009214 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009215 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009216 goto out_fail;
9217 }
Chris Mason39279cc2007-06-12 06:35:45 -04009218
9219 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009220 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009221 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009222 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009223 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009224 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009225 BUG_ON(new_inode->i_nlink == 0);
9226 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009227 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9228 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009229 new_dentry->d_name.name,
9230 new_dentry->d_name.len);
9231 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009232 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009233 ret = btrfs_orphan_add(trans,
9234 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009235 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009236 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009237 goto out_fail;
9238 }
Chris Mason39279cc2007-06-12 06:35:45 -04009239 }
Josef Bacikaec74772008-07-24 12:12:38 -04009240
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009241 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009242 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009243 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009244 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009245 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009246 goto out_fail;
9247 }
Chris Mason39279cc2007-06-12 06:35:45 -04009248
Miao Xie67de1172013-12-26 13:07:06 +08009249 if (old_inode->i_nlink == 1)
9250 BTRFS_I(old_inode)->dir_index = index;
9251
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009252 if (log_pinned) {
Al Viro10d9f302011-07-16 23:09:10 -04009253 struct dentry *parent = new_dentry->d_parent;
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009254
Filipe Mananad4682ba2018-06-11 19:24:28 +01009255 btrfs_init_log_ctx(&ctx, old_inode);
9256 ret = btrfs_log_new_name(trans, BTRFS_I(old_inode),
9257 BTRFS_I(old_dir), parent,
9258 false, &ctx);
9259 if (ret == BTRFS_NEED_LOG_SYNC)
9260 sync_log = true;
9261 else if (ret == BTRFS_NEED_TRANS_COMMIT)
9262 commit_transaction = true;
9263 ret = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009264 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009265 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009266 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009267
9268 if (flags & RENAME_WHITEOUT) {
9269 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9270 old_dentry);
9271
9272 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009273 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009274 goto out_fail;
9275 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009276 }
Chris Mason39279cc2007-06-12 06:35:45 -04009277out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009278 /*
9279 * If we have pinned the log and an error happened, we unpin tasks
9280 * trying to sync the log and force them to fallback to a transaction
9281 * commit if the log currently contains any of the inodes involved in
9282 * this rename operation (to ensure we do not persist a log with an
9283 * inconsistent state for any of these inodes or leading to any
9284 * inconsistencies when replayed). If the transaction was aborted, the
9285 * abortion reason is propagated to userspace when attempting to commit
9286 * the transaction. If the log does not contain any of these inodes, we
9287 * allow the tasks to sync it.
9288 */
9289 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009290 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9291 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9292 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009293 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009294 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009295 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009296
9297 btrfs_end_log_trans(root);
9298 log_pinned = false;
9299 }
Filipe Mananad4682ba2018-06-11 19:24:28 +01009300 if (!ret && sync_log) {
9301 ret = btrfs_sync_log(trans, BTRFS_I(old_inode)->root, &ctx);
9302 if (ret)
9303 commit_transaction = true;
Filipe Manana236ebc22020-03-10 12:13:53 +00009304 } else if (sync_log) {
9305 mutex_lock(&root->log_mutex);
9306 list_del(&ctx.list);
9307 mutex_unlock(&root->log_mutex);
Filipe Mananad4682ba2018-06-11 19:24:28 +01009308 }
9309 if (commit_transaction) {
9310 ret = btrfs_commit_transaction(trans);
9311 } else {
9312 int ret2;
9313
9314 ret2 = btrfs_end_transaction(trans);
9315 ret = ret ? ret : ret2;
9316 }
Johann Lombardib44c59a2011-03-31 13:23:47 +00009317out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009318 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009319 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009320
Chris Mason39279cc2007-06-12 06:35:45 -04009321 return ret;
9322}
9323
Miklos Szeredi80ace852014-07-23 15:15:32 +02009324static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9325 struct inode *new_dir, struct dentry *new_dentry,
9326 unsigned int flags)
9327{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009328 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009329 return -EINVAL;
9330
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009331 if (flags & RENAME_EXCHANGE)
9332 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9333 new_dentry);
9334
9335 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009336}
9337
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009338struct btrfs_delalloc_work {
9339 struct inode *inode;
9340 struct completion completion;
9341 struct list_head list;
9342 struct btrfs_work work;
9343};
9344
Miao Xie8ccf6f192012-10-25 09:28:04 +00009345static void btrfs_run_delalloc_work(struct btrfs_work *work)
9346{
9347 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009348 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009349
9350 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9351 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009352 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009353 filemap_flush(inode->i_mapping);
9354 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9355 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009356 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009357
Nikolay Borisov076da912018-04-23 10:54:16 +03009358 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009359 complete(&delalloc_work->completion);
9360}
9361
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009362static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009363{
9364 struct btrfs_delalloc_work *work;
9365
David Sterba100d5702015-12-08 14:39:32 +01009366 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009367 if (!work)
9368 return NULL;
9369
9370 init_completion(&work->completion);
9371 INIT_LIST_HEAD(&work->list);
9372 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009373 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009374
9375 return work;
9376}
9377
Chris Masond352ac62008-09-29 15:18:18 -04009378/*
9379 * some fairly slow code that needs optimization. This walks the list
9380 * of all the inodes with pending delalloc and forces them to disk.
9381 */
Ethan Lien3cd24c62018-11-01 14:49:03 +08009382static int start_delalloc_inodes(struct btrfs_root *root, int nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009383{
Chris Masonea8c2812008-08-04 23:17:27 -04009384 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009385 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009386 struct btrfs_delalloc_work *work, *next;
9387 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009388 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009389 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009390
Miao Xie8ccf6f192012-10-25 09:28:04 +00009391 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009392 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009393
Miao Xie573bfb72014-03-06 13:55:03 +08009394 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009395 spin_lock(&root->delalloc_lock);
9396 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009397 while (!list_empty(&splice)) {
9398 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009399 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009400
Miao Xieeb73c1b2013-05-15 07:48:22 +00009401 list_move_tail(&binode->delalloc_inodes,
9402 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009403 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009404 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009405 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009406 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009407 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009408 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009409
Ethan Lien3cd24c62018-11-01 14:49:03 +08009410 if (snapshot)
9411 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9412 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009413 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009414 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009415 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009416 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009417 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009418 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009419 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009420 btrfs_queue_work(root->fs_info->flush_workers,
9421 &work->work);
Miao Xie6c255e62014-03-06 13:55:01 +08009422 ret++;
9423 if (nr != -1 && ret >= nr)
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009424 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009425 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009426 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009427 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009428 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009429
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009430out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009431 list_for_each_entry_safe(work, next, &works, list) {
9432 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009433 wait_for_completion(&work->completion);
9434 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009435 }
9436
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009437 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009438 spin_lock(&root->delalloc_lock);
9439 list_splice_tail(&splice, &root->delalloc_inodes);
9440 spin_unlock(&root->delalloc_lock);
9441 }
Miao Xie573bfb72014-03-06 13:55:03 +08009442 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009443 return ret;
9444}
9445
Ethan Lien3cd24c62018-11-01 14:49:03 +08009446int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009447{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009448 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009449 int ret;
9450
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009451 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009452 return -EROFS;
9453
Ethan Lien3cd24c62018-11-01 14:49:03 +08009454 ret = start_delalloc_inodes(root, -1, true);
Miao Xie6c255e62014-03-06 13:55:01 +08009455 if (ret > 0)
9456 ret = 0;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009457 return ret;
9458}
9459
Nikolay Borisov82b3e532018-04-23 10:54:13 +03009460int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009461{
9462 struct btrfs_root *root;
9463 struct list_head splice;
9464 int ret;
9465
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009466 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009467 return -EROFS;
9468
9469 INIT_LIST_HEAD(&splice);
9470
Miao Xie573bfb72014-03-06 13:55:03 +08009471 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009472 spin_lock(&fs_info->delalloc_root_lock);
9473 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009474 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009475 root = list_first_entry(&splice, struct btrfs_root,
9476 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009477 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009478 BUG_ON(!root);
9479 list_move_tail(&root->delalloc_root,
9480 &fs_info->delalloc_roots);
9481 spin_unlock(&fs_info->delalloc_root_lock);
9482
Ethan Lien3cd24c62018-11-01 14:49:03 +08009483 ret = start_delalloc_inodes(root, nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009484 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009485 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009486 goto out;
9487
Miao Xie6c255e62014-03-06 13:55:01 +08009488 if (nr != -1) {
9489 nr -= ret;
9490 WARN_ON(nr < 0);
9491 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009492 spin_lock(&fs_info->delalloc_root_lock);
9493 }
9494 spin_unlock(&fs_info->delalloc_root_lock);
9495
Miao Xie6c255e62014-03-06 13:55:01 +08009496 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009497out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009498 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009499 spin_lock(&fs_info->delalloc_root_lock);
9500 list_splice_tail(&splice, &fs_info->delalloc_roots);
9501 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009502 }
Miao Xie573bfb72014-03-06 13:55:03 +08009503 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009504 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009505}
9506
Chris Mason39279cc2007-06-12 06:35:45 -04009507static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9508 const char *symname)
9509{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009510 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009511 struct btrfs_trans_handle *trans;
9512 struct btrfs_root *root = BTRFS_I(dir)->root;
9513 struct btrfs_path *path;
9514 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009515 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009516 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009517 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309518 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009519 int name_len;
9520 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009521 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009522 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009523 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009524
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009525 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009526 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009527 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009528
Josef Bacik9ed74f22009-09-11 16:12:44 -04009529 /*
9530 * 2 items for inode item and ref
9531 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009532 * 1 item for updating parent inode item
9533 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009534 * 1 item for xattr if selinux is on
9535 */
Filipe Manana9269d122015-12-31 18:16:29 +00009536 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009537 if (IS_ERR(trans))
9538 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009539
Li Zefan581bb052011-04-20 10:06:11 +08009540 err = btrfs_find_free_ino(root, &objectid);
9541 if (err)
9542 goto out_unlock;
9543
Josef Bacikaec74772008-07-24 12:12:38 -04009544 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009545 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9546 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009547 if (IS_ERR(inode)) {
9548 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009549 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009550 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009551 }
Chris Mason39279cc2007-06-12 06:35:45 -04009552
Casey Schauflerad19db72011-12-15 10:09:07 -05009553 /*
9554 * If the active LSM wants to access the inode during
9555 * d_instantiate it needs these. Smack checks to see
9556 * if the filesystem supports xattrs by looking at the
9557 * ops vector.
9558 */
9559 inode->i_fop = &btrfs_file_operations;
9560 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009561 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009562 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9563
9564 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9565 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009566 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009567
Chris Mason39279cc2007-06-12 06:35:45 -04009568 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009569 if (!path) {
9570 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009571 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009572 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009573 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009574 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009575 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009576 datasize = btrfs_file_extent_calc_inline_size(name_len);
9577 err = btrfs_insert_empty_item(trans, root, path, &key,
9578 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009579 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009580 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009581 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009582 }
Chris Mason5f39d392007-10-15 16:14:19 -04009583 leaf = path->nodes[0];
9584 ei = btrfs_item_ptr(leaf, path->slots[0],
9585 struct btrfs_file_extent_item);
9586 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9587 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009588 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009589 btrfs_set_file_extent_encryption(leaf, ei, 0);
9590 btrfs_set_file_extent_compression(leaf, ei, 0);
9591 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9592 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9593
Chris Mason39279cc2007-06-12 06:35:45 -04009594 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009595 write_extent_buffer(leaf, symname, ptr, name_len);
9596 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009597 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009598
Chris Mason39279cc2007-06-12 06:35:45 -04009599 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009600 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009601 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009602 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009603 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009604 /*
9605 * Last step, add directory indexes for our symlink inode. This is the
9606 * last step to avoid extra cleanup of these indexes if an error happens
9607 * elsewhere above.
9608 */
9609 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009610 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9611 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009612 if (err)
9613 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009614
Al Viro1e2e5472018-05-04 08:23:01 -04009615 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009616
9617out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009618 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009619 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009620 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009621 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009622 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009623 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009624 return err;
9625}
Chris Mason16432982008-04-10 10:23:21 -04009626
Qu Wenruo203f44c52020-06-10 09:04:40 +08009627static int insert_prealloc_file_extent(struct btrfs_trans_handle *trans,
9628 struct inode *inode, struct btrfs_key *ins,
9629 u64 file_offset)
9630{
9631 struct btrfs_file_extent_item stack_fi;
9632 u64 start = ins->objectid;
9633 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009634 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009635
9636 memset(&stack_fi, 0, sizeof(stack_fi));
9637
9638 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9639 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9640 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9641 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9642 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9643 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9644 /* Encryption and other encoding is reserved and all 0 */
9645
Qu Wenruo9729f102020-06-10 09:04:41 +08009646 ret = btrfs_qgroup_release_data(inode, file_offset, len);
9647 if (ret < 0)
9648 return ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009649 return insert_reserved_file_extent(trans, inode, file_offset,
Qu Wenruo9729f102020-06-10 09:04:41 +08009650 &stack_fi, ret);
Qu Wenruo203f44c52020-06-10 09:04:40 +08009651}
Josef Bacik0af3d002010-06-21 14:48:16 -04009652static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9653 u64 start, u64 num_bytes, u64 min_size,
9654 loff_t actual_len, u64 *alloc_hint,
9655 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009656{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009657 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009658 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9659 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009660 struct btrfs_root *root = BTRFS_I(inode)->root;
9661 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009662 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009663 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009664 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009665 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009666 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009667 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009668 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009669 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009670
Josef Bacik0af3d002010-06-21 14:48:16 -04009671 if (trans)
9672 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009673 while (num_bytes > 0) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009674 if (own_trans) {
9675 trans = btrfs_start_transaction(root, 3);
9676 if (IS_ERR(trans)) {
9677 ret = PTR_ERR(trans);
9678 break;
9679 }
Yan Zhengd899e052008-10-30 14:25:28 -04009680 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009681
Byongho Leeee221842015-12-15 01:42:10 +09009682 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009683 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009684 /*
9685 * If we are severely fragmented we could end up with really
9686 * small allocations, so if the allocator is returning small
9687 * chunks lets make its job easier by only searching for those
9688 * sized chunks.
9689 */
9690 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009691 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9692 min_size, 0, *alloc_hint, &ins, 1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009693 if (ret) {
Josef Bacik0af3d002010-06-21 14:48:16 -04009694 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009695 btrfs_end_transaction(trans);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009696 break;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009697 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009698
9699 /*
9700 * We've reserved this space, and thus converted it from
9701 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9702 * from here on out we will only need to clear our reservation
9703 * for the remaining unreserved area, so advance our
9704 * clear_offset by our extent size.
9705 */
9706 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009707 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009708
Josef Bacik0b670dc2015-09-23 17:11:16 -04009709 last_alloc = ins.offset;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009710 ret = insert_prealloc_file_extent(trans, inode, &ins,
9711 cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009712 if (ret) {
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009713 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009714 ins.offset, 0);
Jeff Mahoney66642832016-06-10 18:19:25 -04009715 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009716 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009717 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009718 break;
9719 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009720
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009721 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009722 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009723
Josef Bacik5dc562c2012-08-17 13:14:17 -04009724 em = alloc_extent_map();
9725 if (!em) {
9726 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9727 &BTRFS_I(inode)->runtime_flags);
9728 goto next;
9729 }
9730
9731 em->start = cur_offset;
9732 em->orig_start = cur_offset;
9733 em->len = ins.offset;
9734 em->block_start = ins.objectid;
9735 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009736 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009737 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009738 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9739 em->generation = trans->transid;
9740
9741 while (1) {
9742 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009743 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009744 write_unlock(&em_tree->lock);
9745 if (ret != -EEXIST)
9746 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009747 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009748 cur_offset + ins.offset - 1,
9749 0);
9750 }
9751 free_extent_map(em);
9752next:
Yan Zhengd899e052008-10-30 14:25:28 -04009753 num_bytes -= ins.offset;
9754 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009755 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009756
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009757 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009758 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009759 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009760 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009761 (actual_len > inode->i_size) &&
9762 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009763 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009764 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009765 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009766 i_size = cur_offset;
9767 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009768 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009769 }
9770
Yan Zhengd899e052008-10-30 14:25:28 -04009771 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009772
9773 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009774 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009775 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009776 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009777 break;
9778 }
Yan Zhengd899e052008-10-30 14:25:28 -04009779
Josef Bacik0af3d002010-06-21 14:48:16 -04009780 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009781 btrfs_end_transaction(trans);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009782 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009783 if (clear_offset < end)
9784 btrfs_free_reserved_data_space(inode, NULL, clear_offset,
9785 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009786 return ret;
9787}
9788
Josef Bacik0af3d002010-06-21 14:48:16 -04009789int btrfs_prealloc_file_range(struct inode *inode, int mode,
9790 u64 start, u64 num_bytes, u64 min_size,
9791 loff_t actual_len, u64 *alloc_hint)
9792{
9793 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9794 min_size, actual_len, alloc_hint,
9795 NULL);
9796}
9797
9798int btrfs_prealloc_file_range_trans(struct inode *inode,
9799 struct btrfs_trans_handle *trans, int mode,
9800 u64 start, u64 num_bytes, u64 min_size,
9801 loff_t actual_len, u64 *alloc_hint)
9802{
9803 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9804 min_size, actual_len, alloc_hint, trans);
9805}
9806
Chris Masone6dcd2d2008-07-17 12:53:50 -04009807static int btrfs_set_page_dirty(struct page *page)
9808{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009809 return __set_page_dirty_nobuffers(page);
9810}
9811
Al Viro10556cb22011-06-20 19:28:19 -04009812static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009813{
Li Zefanb83cc962010-12-20 16:04:08 +08009814 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009815 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009816
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009817 if (mask & MAY_WRITE &&
9818 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9819 if (btrfs_root_readonly(root))
9820 return -EROFS;
9821 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9822 return -EACCES;
9823 }
Al Viro2830ba72011-06-20 19:16:29 -04009824 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009825}
Chris Mason39279cc2007-06-12 06:35:45 -04009826
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009827static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9828{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009829 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009830 struct btrfs_trans_handle *trans;
9831 struct btrfs_root *root = BTRFS_I(dir)->root;
9832 struct inode *inode = NULL;
9833 u64 objectid;
9834 u64 index;
9835 int ret = 0;
9836
9837 /*
9838 * 5 units required for adding orphan entry
9839 */
9840 trans = btrfs_start_transaction(root, 5);
9841 if (IS_ERR(trans))
9842 return PTR_ERR(trans);
9843
9844 ret = btrfs_find_free_ino(root, &objectid);
9845 if (ret)
9846 goto out;
9847
9848 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009849 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009850 if (IS_ERR(inode)) {
9851 ret = PTR_ERR(inode);
9852 inode = NULL;
9853 goto out;
9854 }
9855
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009856 inode->i_fop = &btrfs_file_operations;
9857 inode->i_op = &btrfs_file_inode_operations;
9858
9859 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009860 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9861
Chris Masonb0d5d102014-09-08 13:08:51 -07009862 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9863 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009864 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009865
9866 ret = btrfs_update_inode(trans, root, inode);
9867 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009868 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009869 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009870 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009871 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009872
Filipe Manana5762b5c2014-08-01 00:10:32 +01009873 /*
9874 * We set number of links to 0 in btrfs_new_inode(), and here we set
9875 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9876 * through:
9877 *
9878 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9879 */
9880 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009881 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009882 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009883 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009884out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009885 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009886 if (ret && inode)
9887 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009888 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009889 return ret;
9890}
9891
David Sterba5cdc84b2018-07-18 20:32:52 +02009892void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009893{
David Sterba5cdc84b2018-07-18 20:32:52 +02009894 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009895 unsigned long index = start >> PAGE_SHIFT;
9896 unsigned long end_index = end >> PAGE_SHIFT;
9897 struct page *page;
9898
9899 while (index <= end_index) {
9900 page = find_get_page(inode->i_mapping, index);
9901 ASSERT(page); /* Pages should be in the extent_io_tree */
9902 set_page_writeback(page);
9903 put_page(page);
9904 index++;
9905 }
9906}
9907
Omar Sandovaled46ff32016-11-03 10:28:14 -07009908#ifdef CONFIG_SWAP
9909/*
9910 * Add an entry indicating a block group or device which is pinned by a
9911 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9912 * negative errno on failure.
9913 */
9914static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9915 bool is_block_group)
9916{
9917 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9918 struct btrfs_swapfile_pin *sp, *entry;
9919 struct rb_node **p;
9920 struct rb_node *parent = NULL;
9921
9922 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9923 if (!sp)
9924 return -ENOMEM;
9925 sp->ptr = ptr;
9926 sp->inode = inode;
9927 sp->is_block_group = is_block_group;
9928
9929 spin_lock(&fs_info->swapfile_pins_lock);
9930 p = &fs_info->swapfile_pins.rb_node;
9931 while (*p) {
9932 parent = *p;
9933 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9934 if (sp->ptr < entry->ptr ||
9935 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9936 p = &(*p)->rb_left;
9937 } else if (sp->ptr > entry->ptr ||
9938 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9939 p = &(*p)->rb_right;
9940 } else {
9941 spin_unlock(&fs_info->swapfile_pins_lock);
9942 kfree(sp);
9943 return 1;
9944 }
9945 }
9946 rb_link_node(&sp->node, parent, p);
9947 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9948 spin_unlock(&fs_info->swapfile_pins_lock);
9949 return 0;
9950}
9951
9952/* Free all of the entries pinned by this swapfile. */
9953static void btrfs_free_swapfile_pins(struct inode *inode)
9954{
9955 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9956 struct btrfs_swapfile_pin *sp;
9957 struct rb_node *node, *next;
9958
9959 spin_lock(&fs_info->swapfile_pins_lock);
9960 node = rb_first(&fs_info->swapfile_pins);
9961 while (node) {
9962 next = rb_next(node);
9963 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9964 if (sp->inode == inode) {
9965 rb_erase(&sp->node, &fs_info->swapfile_pins);
9966 if (sp->is_block_group)
9967 btrfs_put_block_group(sp->ptr);
9968 kfree(sp);
9969 }
9970 node = next;
9971 }
9972 spin_unlock(&fs_info->swapfile_pins_lock);
9973}
9974
9975struct btrfs_swap_info {
9976 u64 start;
9977 u64 block_start;
9978 u64 block_len;
9979 u64 lowest_ppage;
9980 u64 highest_ppage;
9981 unsigned long nr_pages;
9982 int nr_extents;
9983};
9984
9985static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9986 struct btrfs_swap_info *bsi)
9987{
9988 unsigned long nr_pages;
9989 u64 first_ppage, first_ppage_reported, next_ppage;
9990 int ret;
9991
9992 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9993 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9994 PAGE_SIZE) >> PAGE_SHIFT;
9995
9996 if (first_ppage >= next_ppage)
9997 return 0;
9998 nr_pages = next_ppage - first_ppage;
9999
10000 first_ppage_reported = first_ppage;
10001 if (bsi->start == 0)
10002 first_ppage_reported++;
10003 if (bsi->lowest_ppage > first_ppage_reported)
10004 bsi->lowest_ppage = first_ppage_reported;
10005 if (bsi->highest_ppage < (next_ppage - 1))
10006 bsi->highest_ppage = next_ppage - 1;
10007
10008 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10009 if (ret < 0)
10010 return ret;
10011 bsi->nr_extents += ret;
10012 bsi->nr_pages += nr_pages;
10013 return 0;
10014}
10015
10016static void btrfs_swap_deactivate(struct file *file)
10017{
10018 struct inode *inode = file_inode(file);
10019
10020 btrfs_free_swapfile_pins(inode);
10021 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10022}
10023
10024static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10025 sector_t *span)
10026{
10027 struct inode *inode = file_inode(file);
10028 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10029 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10030 struct extent_state *cached_state = NULL;
10031 struct extent_map *em = NULL;
10032 struct btrfs_device *device = NULL;
10033 struct btrfs_swap_info bsi = {
10034 .lowest_ppage = (sector_t)-1ULL,
10035 };
10036 int ret = 0;
10037 u64 isize;
10038 u64 start;
10039
10040 /*
10041 * If the swap file was just created, make sure delalloc is done. If the
10042 * file changes again after this, the user is doing something stupid and
10043 * we don't really care.
10044 */
10045 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10046 if (ret)
10047 return ret;
10048
10049 /*
10050 * The inode is locked, so these flags won't change after we check them.
10051 */
10052 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10053 btrfs_warn(fs_info, "swapfile must not be compressed");
10054 return -EINVAL;
10055 }
10056 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10057 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10058 return -EINVAL;
10059 }
10060 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10061 btrfs_warn(fs_info, "swapfile must not be checksummed");
10062 return -EINVAL;
10063 }
10064
10065 /*
10066 * Balance or device remove/replace/resize can move stuff around from
10067 * under us. The EXCL_OP flag makes sure they aren't running/won't run
10068 * concurrently while we are mapping the swap extents, and
10069 * fs_info->swapfile_pins prevents them from running while the swap file
10070 * is active and moving the extents. Note that this also prevents a
10071 * concurrent device add which isn't actually necessary, but it's not
10072 * really worth the trouble to allow it.
10073 */
10074 if (test_and_set_bit(BTRFS_FS_EXCL_OP, &fs_info->flags)) {
10075 btrfs_warn(fs_info,
10076 "cannot activate swapfile while exclusive operation is running");
10077 return -EBUSY;
10078 }
10079 /*
10080 * Snapshots can create extents which require COW even if NODATACOW is
10081 * set. We use this counter to prevent snapshots. We must increment it
10082 * before walking the extents because we don't want a concurrent
10083 * snapshot to run after we've already checked the extents.
10084 */
10085 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10086
10087 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10088
10089 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10090 start = 0;
10091 while (start < isize) {
10092 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010093 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010094 u64 len = isize - start;
10095
Omar Sandoval39b07b52019-12-02 17:34:23 -080010096 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010097 if (IS_ERR(em)) {
10098 ret = PTR_ERR(em);
10099 goto out;
10100 }
10101
10102 if (em->block_start == EXTENT_MAP_HOLE) {
10103 btrfs_warn(fs_info, "swapfile must not have holes");
10104 ret = -EINVAL;
10105 goto out;
10106 }
10107 if (em->block_start == EXTENT_MAP_INLINE) {
10108 /*
10109 * It's unlikely we'll ever actually find ourselves
10110 * here, as a file small enough to fit inline won't be
10111 * big enough to store more than the swap header, but in
10112 * case something changes in the future, let's catch it
10113 * here rather than later.
10114 */
10115 btrfs_warn(fs_info, "swapfile must not be inline");
10116 ret = -EINVAL;
10117 goto out;
10118 }
10119 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10120 btrfs_warn(fs_info, "swapfile must not be compressed");
10121 ret = -EINVAL;
10122 goto out;
10123 }
10124
10125 logical_block_start = em->block_start + (start - em->start);
10126 len = min(len, em->len - (start - em->start));
10127 free_extent_map(em);
10128 em = NULL;
10129
10130 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL);
10131 if (ret < 0) {
10132 goto out;
10133 } else if (ret) {
10134 ret = 0;
10135 } else {
10136 btrfs_warn(fs_info,
10137 "swapfile must not be copy-on-write");
10138 ret = -EINVAL;
10139 goto out;
10140 }
10141
10142 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10143 if (IS_ERR(em)) {
10144 ret = PTR_ERR(em);
10145 goto out;
10146 }
10147
10148 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10149 btrfs_warn(fs_info,
10150 "swapfile must have single data profile");
10151 ret = -EINVAL;
10152 goto out;
10153 }
10154
10155 if (device == NULL) {
10156 device = em->map_lookup->stripes[0].dev;
10157 ret = btrfs_add_swapfile_pin(inode, device, false);
10158 if (ret == 1)
10159 ret = 0;
10160 else if (ret)
10161 goto out;
10162 } else if (device != em->map_lookup->stripes[0].dev) {
10163 btrfs_warn(fs_info, "swapfile must be on one device");
10164 ret = -EINVAL;
10165 goto out;
10166 }
10167
10168 physical_block_start = (em->map_lookup->stripes[0].physical +
10169 (logical_block_start - em->start));
10170 len = min(len, em->len - (logical_block_start - em->start));
10171 free_extent_map(em);
10172 em = NULL;
10173
10174 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10175 if (!bg) {
10176 btrfs_warn(fs_info,
10177 "could not find block group containing swapfile");
10178 ret = -EINVAL;
10179 goto out;
10180 }
10181
10182 ret = btrfs_add_swapfile_pin(inode, bg, true);
10183 if (ret) {
10184 btrfs_put_block_group(bg);
10185 if (ret == 1)
10186 ret = 0;
10187 else
10188 goto out;
10189 }
10190
10191 if (bsi.block_len &&
10192 bsi.block_start + bsi.block_len == physical_block_start) {
10193 bsi.block_len += len;
10194 } else {
10195 if (bsi.block_len) {
10196 ret = btrfs_add_swap_extent(sis, &bsi);
10197 if (ret)
10198 goto out;
10199 }
10200 bsi.start = start;
10201 bsi.block_start = physical_block_start;
10202 bsi.block_len = len;
10203 }
10204
10205 start += len;
10206 }
10207
10208 if (bsi.block_len)
10209 ret = btrfs_add_swap_extent(sis, &bsi);
10210
10211out:
10212 if (!IS_ERR_OR_NULL(em))
10213 free_extent_map(em);
10214
10215 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10216
10217 if (ret)
10218 btrfs_swap_deactivate(file);
10219
10220 clear_bit(BTRFS_FS_EXCL_OP, &fs_info->flags);
10221
10222 if (ret)
10223 return ret;
10224
10225 if (device)
10226 sis->bdev = device->bdev;
10227 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10228 sis->max = bsi.nr_pages;
10229 sis->pages = bsi.nr_pages - 1;
10230 sis->highest_bit = bsi.nr_pages - 1;
10231 return bsi.nr_extents;
10232}
10233#else
10234static void btrfs_swap_deactivate(struct file *file)
10235{
10236}
10237
10238static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10239 sector_t *span)
10240{
10241 return -EOPNOTSUPP;
10242}
10243#endif
10244
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010245static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010246 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010247 .lookup = btrfs_lookup,
10248 .create = btrfs_create,
10249 .unlink = btrfs_unlink,
10250 .link = btrfs_link,
10251 .mkdir = btrfs_mkdir,
10252 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010253 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010254 .symlink = btrfs_symlink,
10255 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010256 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010257 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010258 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010259 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010260 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010261 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010262 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010263};
Yan, Zheng76dda932009-09-21 16:00:26 -040010264
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010265static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010266 .llseek = generic_file_llseek,
10267 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010268 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010269 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010270 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010271#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010272 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010273#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010274 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010275 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010276};
10277
David Sterba20e55062015-11-19 11:42:28 +010010278static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010279 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010280 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010281 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10282};
10283
Chris Mason35054392009-01-21 13:11:13 -050010284/*
10285 * btrfs doesn't support the bmap operation because swapfiles
10286 * use bmap to make a mapping of extents in the file. They assume
10287 * these extents won't change over the life of the file and they
10288 * use the bmap result to do IO directly to the drive.
10289 *
10290 * the btrfs bmap call would return logical addresses that aren't
10291 * suitable for IO and they also will change frequently as COW
10292 * operations happen. So, swapfile + btrfs == corruption.
10293 *
10294 * For now we're avoiding this by dropping bmap.
10295 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010296static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010297 .readpage = btrfs_readpage,
10298 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010299 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010300 .readahead = btrfs_readahead,
David Sterba55e20bd2020-06-09 19:56:06 +020010301 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010302 .invalidatepage = btrfs_invalidatepage,
10303 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010304#ifdef CONFIG_MIGRATION
10305 .migratepage = btrfs_migratepage,
10306#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010307 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010308 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010309 .swap_activate = btrfs_swap_activate,
10310 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010311};
10312
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010313static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010314 .getattr = btrfs_getattr,
10315 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010316 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010317 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010318 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010319 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010320 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010321 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010322};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010323static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010324 .getattr = btrfs_getattr,
10325 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010326 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010327 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010328 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010329 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010330 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010331};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010332static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010333 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010334 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010335 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010336 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010337 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010338 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010339};
Yan, Zheng76dda932009-09-21 16:00:26 -040010340
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010341const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010342 .d_delete = btrfs_dentry_delete,
10343};