blob: 4a5248097d7aae5a5c1a87cc506fd3baa68ffec1 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Herbert Xu79990962020-06-12 16:57:37 +10006#include <crypto/hash.h>
Chris Mason8f18cf12008-04-25 16:53:30 -04007#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05008#include <linux/bio.h>
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>
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050033#include <linux/iomap.h>
David Sterba92d32172018-04-16 21:10:14 +020034#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020035#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040041#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040042#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040043#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020044#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040045#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050046#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050047#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080048#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000049#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080050#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040051#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040052#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010053#include "space-info.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020056 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040057 struct btrfs_root *root;
58};
59
Filipe Mananaf28a4922015-12-08 19:23:20 +000060struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000061 u64 reserve;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050062 loff_t length;
63 ssize_t submitted;
64 struct extent_changeset *data_reserved;
Josef Bacik0eb79292020-09-03 11:16:51 -040065 bool sync;
Filipe Mananaf28a4922015-12-08 19:23:20 +000066};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070070static const struct inode_operations btrfs_special_inode_operations;
71static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070072static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070073static const struct file_operations btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040074
75static struct kmem_cache *btrfs_inode_cachep;
76struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040077struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050078struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000079struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040080
Eric Sandeen3972f262013-01-12 02:57:22 +000081static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000082static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040083static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030084static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050085 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030086 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030087 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030088static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
89 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080090 u64 block_len, u64 orig_block_len,
91 u64 ram_bytes, int compress_type,
92 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040093
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030094static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080095 const u64 offset, const u64 bytes,
96 const bool uptodate);
97
98/*
99 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100100 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800101 *
102 * NOTE: caller must ensure that when an error happens, it can not call
103 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
104 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
105 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200106 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800107 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300108static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200109 struct page *locked_page,
110 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800111{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900112 unsigned long index = offset >> PAGE_SHIFT;
113 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200114 u64 page_start = page_offset(locked_page);
115 u64 page_end = page_start + PAGE_SIZE - 1;
116
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900117 struct page *page;
118
119 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300120 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900121 index++;
122 if (!page)
123 continue;
124 ClearPagePrivate2(page);
125 put_page(page);
126 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200127
128 /*
129 * In case this page belongs to the delalloc range being instantiated
130 * then skip it, since the first page of a range is going to be
131 * properly cleaned up by the caller of run_delalloc_range
132 */
133 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
134 offset += PAGE_SIZE;
135 bytes -= PAGE_SIZE;
136 }
137
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300138 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800139}
140
Eric Sandeen48a3b632013-04-25 20:41:01 +0000141static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400142
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000143static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500144 struct inode *inode, struct inode *dir,
145 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500146{
147 int err;
148
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000149 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500150 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500151 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500152 return err;
153}
154
Chris Masond352ac62008-09-29 15:18:18 -0400155/*
Chris Masonc8b97812008-10-29 14:49:59 -0400156 * this does all the hard work for inserting an inline extent into
157 * the btree. The caller should have done a btrfs_drop_extents so that
158 * no overlapping inline items exist in the btree
159 */
Chris Mason40f76582014-05-21 13:35:51 -0700160static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000161 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400162 struct btrfs_root *root, struct inode *inode,
163 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000164 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400165 struct page **compressed_pages)
166{
Chris Masonc8b97812008-10-29 14:49:59 -0400167 struct extent_buffer *leaf;
168 struct page *page = NULL;
169 char *kaddr;
170 unsigned long ptr;
171 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400172 int ret;
173 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400174 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400175
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800176 ASSERT((compressed_size > 0 && compressed_pages) ||
177 (compressed_size == 0 && !compressed_pages));
178
Li Zefanfe3f5662011-03-28 08:30:38 +0000179 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400180 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400181
Chris Masonc8b97812008-10-29 14:49:59 -0400182 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000183
184 if (!extent_inserted) {
185 struct btrfs_key key;
186 size_t datasize;
187
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200188 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000189 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200190 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000191
192 datasize = btrfs_file_extent_calc_inline_size(cur_size);
193 path->leave_spinning = 1;
194 ret = btrfs_insert_empty_item(trans, root, path, &key,
195 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200196 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000197 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400198 }
199 leaf = path->nodes[0];
200 ei = btrfs_item_ptr(leaf, path->slots[0],
201 struct btrfs_file_extent_item);
202 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
203 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
204 btrfs_set_file_extent_encryption(leaf, ei, 0);
205 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
206 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
207 ptr = btrfs_file_extent_inline_start(ei);
208
Li Zefan261507a02010-12-17 14:21:50 +0800209 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400210 struct page *cpage;
211 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500212 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400213 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500214 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300215 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400216
Cong Wang7ac687d2011-11-25 23:14:28 +0800217 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400218 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800219 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400220
221 i++;
222 ptr += cur_size;
223 compressed_size -= cur_size;
224 }
225 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800226 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400227 } else {
228 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300229 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400230 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800231 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100232 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400233 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800234 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300235 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400236 }
237 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000238 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400239
Yan, Zhengc2167752009-11-12 09:34:21 +0000240 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500241 * We align size to sectorsize for inline extents just for simplicity
242 * sake.
243 */
244 size = ALIGN(size, root->fs_info->sectorsize);
245 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
246 if (ret)
247 goto fail;
248
249 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000250 * we're an inline extent, so nobody can
251 * extend the file past i_size without locking
252 * a page we already have locked.
253 *
254 * We must do any isize and inode updates
255 * before we unlock the pages. Otherwise we
256 * could end up racing with unlink.
257 */
Chris Masonc8b97812008-10-29 14:49:59 -0400258 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100259 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000260
Chris Masonc8b97812008-10-29 14:49:59 -0400261fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200262 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400263}
264
265
266/*
267 * conditionally insert an inline extent into the file. This
268 * does the checks required to make sure the data is small enough
269 * to fit as an inline extent.
270 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300271static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400272 u64 end, size_t compressed_size,
273 int compress_type,
274 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400275{
Nikolay Borisova0349402020-06-03 08:55:12 +0300276 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400277 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400278 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300279 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400280 u64 actual_end = min(end + 1, isize);
281 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400282 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400283 u64 data_len = inline_len;
284 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000285 struct btrfs_path *path;
286 int extent_inserted = 0;
287 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400288
289 if (compressed_size)
290 data_len = compressed_size;
291
292 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400293 actual_end > fs_info->sectorsize ||
294 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400295 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400296 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400297 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400298 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400299 return 1;
300 }
301
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000302 path = btrfs_alloc_path();
303 if (!path)
304 return -ENOMEM;
305
Josef Bacik00361582013-08-14 14:02:47 -0400306 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000307 if (IS_ERR(trans)) {
308 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400309 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300311 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400312
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000313 if (compressed_size && compressed_pages)
314 extent_item_size = btrfs_file_extent_calc_inline_size(
315 compressed_size);
316 else
317 extent_item_size = btrfs_file_extent_calc_inline_size(
318 inline_len);
319
Nikolay Borisova0349402020-06-03 08:55:12 +0300320 ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end,
321 NULL, 1, 1, extent_item_size,
322 &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400323 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400324 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400325 goto out;
326 }
Chris Masonc8b97812008-10-29 14:49:59 -0400327
328 if (isize > actual_end)
329 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000330 ret = insert_inline_extent(trans, path, extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300331 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400332 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000333 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400334 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400335 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400336 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400337 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400338 ret = 1;
339 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100340 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400341
Nikolay Borisova0349402020-06-03 08:55:12 +0300342 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
343 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400344out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800345 /*
346 * Don't forget to free the reserved space, as for inlined extent
347 * it won't count as data extent, free them directly here.
348 * And at reserve time, it's always aligned to page size, so
349 * just free one page here.
350 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300351 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000352 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400353 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400354 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400355}
356
Chris Mason771ed682008-11-06 22:02:51 -0500357struct async_extent {
358 u64 start;
359 u64 ram_size;
360 u64 compressed_size;
361 struct page **pages;
362 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800363 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500364 struct list_head list;
365};
366
Nikolay Borisov97db1202019-03-12 17:20:24 +0200367struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500368 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500369 struct page *locked_page;
370 u64 start;
371 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600372 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500373 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700374 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500375 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200376 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500377};
378
Nikolay Borisov97db1202019-03-12 17:20:24 +0200379struct async_cow {
380 /* Number of chunks in flight; must be first in the structure */
381 atomic_t num_chunks;
382 struct async_chunk chunks[];
383};
384
385static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500386 u64 start, u64 ram_size,
387 u64 compressed_size,
388 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800389 unsigned long nr_pages,
390 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500391{
392 struct async_extent *async_extent;
393
394 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100395 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500396 async_extent->start = start;
397 async_extent->ram_size = ram_size;
398 async_extent->compressed_size = compressed_size;
399 async_extent->pages = pages;
400 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800401 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500402 list_add_tail(&async_extent->list, &cow->extents);
403 return 0;
404}
405
Qu Wenruo42c16da2019-07-01 05:12:46 +0000406/*
407 * Check if the inode has flags compatible with compression
408 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300409static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000410{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300411 if (inode->flags & BTRFS_INODE_NODATACOW ||
412 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000413 return false;
414 return true;
415}
416
417/*
418 * Check if the inode needs to be submitted to compression, based on mount
419 * options, defragmentation, properties or heuristics.
420 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300421static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
422 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800423{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300424 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800425
Nikolay Borisov808a1292020-06-03 08:55:27 +0300426 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000427 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
428 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300429 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000430 return 0;
431 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800432 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400433 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800434 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200435 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300436 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200437 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800438 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300439 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800440 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400441 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300442 inode->flags & BTRFS_INODE_COMPRESS ||
443 inode->prop_compress)
444 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800445 return 0;
446}
447
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200448static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800449 u64 start, u64 end, u64 num_bytes, u64 small_write)
450{
451 /* If this is a small write inside eof, kick off a defrag */
452 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200453 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800454 btrfs_add_inode_defrag(NULL, inode);
455}
456
Chris Masonc8b97812008-10-29 14:49:59 -0400457/*
Chris Mason771ed682008-11-06 22:02:51 -0500458 * we create compressed extents in two phases. The first
459 * phase compresses a range of pages that have already been
460 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400461 *
Chris Mason771ed682008-11-06 22:02:51 -0500462 * This is done inside an ordered work queue, and the compression
463 * is spread across many cpus. The actual IO submission is step
464 * two, and the ordered work queue takes care of making sure that
465 * happens in the same order things were put onto the queue by
466 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400467 *
Chris Mason771ed682008-11-06 22:02:51 -0500468 * If this code finds it can't get good compression, it puts an
469 * entry onto the work queue to write the uncompressed bytes. This
470 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300471 * are written in the same order that the flusher thread sent them
472 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400473 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300474static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db2b2007-08-27 16:49:44 -0400475{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200476 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400477 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400478 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200479 u64 start = async_chunk->start;
480 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400481 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400482 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400483 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400484 struct page **pages = NULL;
485 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400486 unsigned long total_compressed = 0;
487 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400488 int i;
489 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400490 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300491 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400492 int redirty = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -0400493
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200494 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
495 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400496
Josef Bacikd98da492019-10-11 09:03:54 -0400497 /*
498 * We need to save i_size before now because it could change in between
499 * us evaluating the size and assigning it. This is because we lock and
500 * unlock the page in truncate and fallocate, and then modify the i_size
501 * later on.
502 *
503 * The barriers are to emulate READ_ONCE, remove that once i_size_read
504 * does that for us.
505 */
506 barrier();
507 i_size = i_size_read(inode);
508 barrier();
509 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400510again:
511 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300512 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100513 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
514 nr_pages = min_t(unsigned long, nr_pages,
515 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400516
Chris Masonf03d9301f2009-02-04 09:31:06 -0500517 /*
518 * we don't want to send crud past the end of i_size through
519 * compression, that's just a waste of CPU time. So, if the
520 * end of the file is before the start of our current
521 * requested range of bytes, we bail out to the uncompressed
522 * cleanup code that can deal with all of this.
523 *
524 * It isn't really the fastest way to fix things, but this is a
525 * very uncommon corner.
526 */
527 if (actual_end <= start)
528 goto cleanup_and_bail_uncompressed;
529
Chris Masonc8b97812008-10-29 14:49:59 -0400530 total_compressed = actual_end - start;
531
Shilong Wang4bcbb332014-10-07 18:44:35 -0400532 /*
533 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400534 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400535 */
536 if (total_compressed <= blocksize &&
537 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
538 goto cleanup_and_bail_uncompressed;
539
David Sterba069eac72017-02-14 19:36:54 +0100540 total_compressed = min_t(unsigned long, total_compressed,
541 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400542 total_in = 0;
543 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400544
Chris Mason771ed682008-11-06 22:02:51 -0500545 /*
546 * we do compression for mount -o compress and when the
547 * inode has not been flagged as nocompress. This flag can
548 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400549 */
Qu Wenruo31342922021-08-25 13:41:42 +0800550 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400551 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100552 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800553 if (!pages) {
554 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100555 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800556 goto cont;
557 }
Chris Mason179e29e2007-11-01 11:28:41 -0400558
David Sterbaeec63c62017-07-17 19:41:31 +0200559 if (BTRFS_I(inode)->defrag_compress)
560 compress_type = BTRFS_I(inode)->defrag_compress;
561 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200562 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800563
Chris Mason4adaa612013-03-26 13:07:00 -0400564 /*
565 * we need to call clear_page_dirty_for_io on each
566 * page in the range. Otherwise applications with the file
567 * mmap'd can wander in and change the page contents while
568 * we are compressing them.
569 *
570 * If the compression fails for any reason, we set the pages
571 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300572 *
573 * Note that the remaining part is redirtied, the start pointer
574 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400575 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300576 if (!redirty) {
577 extent_range_clear_dirty_for_io(inode, start, end);
578 redirty = 1;
579 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200580
581 /* Compression level is applied here and only here */
582 ret = btrfs_compress_pages(
583 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800584 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100585 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100586 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800587 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100588 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400589
590 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100591 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100592 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400593 char *kaddr;
594
595 /* zero the tail end of the last page, we might be
596 * sending it down to disk
597 */
598 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800599 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400600 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300601 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800602 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400603 }
604 will_compress = 1;
605 }
606 }
Li Zefan560f7d72011-09-08 10:22:01 +0800607cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400608 if (start == 0) {
609 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300610 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400611 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500612 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400613 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300614 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
615 0, BTRFS_COMPRESS_NONE,
616 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400617 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500618 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300619 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000620 total_compressed,
621 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400622 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100623 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400624 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400625 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
626 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100627 unsigned long page_error_op;
628
Filipe Mananae6eb4312014-10-10 10:45:12 +0100629 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400630
Chris Mason771ed682008-11-06 22:02:51 -0500631 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100632 * inline extent creation worked or returned error,
633 * we don't need to create any more async work items.
634 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400635 *
636 * We use DO_ACCOUNTING here because we need the
637 * delalloc_release_metadata to be done _after_ we drop
638 * our outstanding extent for clearing delalloc for this
639 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500640 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300641 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
642 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300643 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800644 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400645 PAGE_CLEAR_DIRTY |
646 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100647 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400648 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300649
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800650 /*
651 * Ensure we only free the compressed pages if we have
652 * them allocated, as we can still reach here with
653 * inode_need_compress() == false.
654 */
655 if (pages) {
656 for (i = 0; i < nr_pages; i++) {
657 WARN_ON(pages[i]->mapping);
658 put_page(pages[i]);
659 }
660 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300661 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300662 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400663 }
664 }
665
666 if (will_compress) {
667 /*
668 * we aren't doing an inline extent round the compressed size
669 * up to a block size boundary so the allocator does sane
670 * things
671 */
Qu Wenruofda28322013-02-26 08:10:22 +0000672 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400673
674 /*
675 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300676 * win, compare the page count read with the blocks on disk,
677 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400678 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300679 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300680 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300681 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700682
683 /*
684 * The async work queues will take care of doing actual
685 * allocation on disk for these compressed pages, and
686 * will submit them to the elevator.
687 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200688 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100689 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700690 compress_type);
691
Timofey Titovets11708622017-10-03 18:06:01 +0300692 if (start + total_in < end) {
693 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700694 pages = NULL;
695 cond_resched();
696 goto again;
697 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300698 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400699 }
700 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700701 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400702 /*
703 * the compression code ran but failed to make things smaller,
704 * free any pages it allocated and our page pointer array
705 */
David Sterba4d3a8002017-02-14 19:04:07 +0100706 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400707 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300708 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400709 }
710 kfree(pages);
711 pages = NULL;
712 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100713 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400714
715 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400716 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200717 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500718 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500719 }
Chris Masonc8b97812008-10-29 14:49:59 -0400720 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500721cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700722 /*
723 * No compression, but we still need to write the pages in the file
724 * we've been given so far. redirty the locked page if it corresponds
725 * to our extent and set things up for the async work queue to run
726 * cow_file_range to do the normal delalloc dance.
727 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700728 if (async_chunk->locked_page &&
729 (page_offset(async_chunk->locked_page) >= start &&
730 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200731 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700732 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700733 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700734
735 if (redirty)
736 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200737 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700738 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300739 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500740
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300741 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500742}
Chris Mason771ed682008-11-06 22:02:51 -0500743
Filipe Manana40ae8372014-10-06 22:14:24 +0100744static void free_async_extent_pages(struct async_extent *async_extent)
745{
746 int i;
747
748 if (!async_extent->pages)
749 return;
750
751 for (i = 0; i < async_extent->nr_pages; i++) {
752 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300753 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100754 }
755 kfree(async_extent->pages);
756 async_extent->nr_pages = 0;
757 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500758}
759
760/*
761 * phase two of compressed writeback. This is the ordered portion
762 * of the code, which only gets called in the order the work was
763 * queued. We walk all the async extents created by compress_file_range
764 * and send them down to the disk.
765 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200766static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500767{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300768 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
769 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500770 struct async_extent *async_extent;
771 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500772 struct btrfs_key ins;
773 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300774 struct btrfs_root *root = inode->root;
775 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500776 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500777
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500778again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200779 while (!list_empty(&async_chunk->extents)) {
780 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500781 struct async_extent, list);
782 list_del(&async_extent->list);
783
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500784retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200785 lock_extent(io_tree, async_extent->start,
786 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500787 /* did the compression code fall back to uncompressed IO? */
788 if (!async_extent->pages) {
789 int page_started = 0;
790 unsigned long nr_written = 0;
791
Chris Mason771ed682008-11-06 22:02:51 -0500792 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300793 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500794 async_extent->start,
795 async_extent->start +
796 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300797 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500798
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100799 /* JDM XXX */
800
Chris Mason771ed682008-11-06 22:02:51 -0500801 /*
802 * if page_started, cow_file_range inserted an
803 * inline extent and took care of all the unlocking
804 * and IO for us. Otherwise, we need to submit
805 * all those pages down to the drive.
806 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500807 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300808 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200809 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500810 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500811 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500812 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700813 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200814 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500815 kfree(async_extent);
816 cond_resched();
817 continue;
818 }
819
Wang Xiaoguang18513092016-07-25 15:51:40 +0800820 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500821 async_extent->compressed_size,
822 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800823 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500824 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100825 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500826
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400827 if (ret == -ENOSPC) {
828 unlock_extent(io_tree, async_extent->start,
829 async_extent->start +
830 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800831
832 /*
833 * we need to redirty the pages if we decide to
834 * fallback to uncompressed IO, otherwise we
835 * will not submit these pages down to lower
836 * layers.
837 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300838 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800839 async_extent->start,
840 async_extent->start +
841 async_extent->ram_size - 1);
842
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100843 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400844 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500845 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500846 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000847 /*
848 * here we're doing allocation and writeback of the
849 * compressed pages
850 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300851 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800852 async_extent->ram_size, /* len */
853 async_extent->start, /* orig_start */
854 ins.objectid, /* block_start */
855 ins.offset, /* block_len */
856 ins.offset, /* orig_block_len */
857 async_extent->ram_size, /* ram_bytes */
858 async_extent->compress_type,
859 BTRFS_ORDERED_COMPRESSED);
860 if (IS_ERR(em))
861 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500862 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800863 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500864
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300865 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800866 async_extent->start,
867 ins.objectid,
868 async_extent->ram_size,
869 ins.offset,
870 BTRFS_ORDERED_COMPRESSED,
871 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100872 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300873 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100874 async_extent->start +
875 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500876 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100877 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400878 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500879
Chris Mason771ed682008-11-06 22:02:51 -0500880 /*
881 * clear dirty, set writeback and unlock the pages.
882 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300883 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400884 async_extent->start +
885 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400886 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
887 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400888 PAGE_SET_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300889 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500890 async_extent->ram_size,
891 ins.objectid,
892 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600893 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700894 async_chunk->write_flags,
895 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100896 struct page *p = async_extent->pages[0];
897 const u64 start = async_extent->start;
898 const u64 end = start + async_extent->ram_size - 1;
899
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300900 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200901 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200902
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100903 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300904 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100905 PAGE_END_WRITEBACK |
906 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100907 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100908 }
Chris Mason771ed682008-11-06 22:02:51 -0500909 alloc_hint = ins.objectid + ins.offset;
910 kfree(async_extent);
911 cond_resched();
912 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100913 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500914out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400915 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400916 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100917out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300918 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500919 async_extent->start +
920 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400921 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100922 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400923 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
924 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100925 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
926 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100927 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100928 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500929 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500930}
931
Nikolay Borisov43c69842020-06-03 08:55:02 +0300932static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400933 u64 num_bytes)
934{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300935 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400936 struct extent_map *em;
937 u64 alloc_hint = 0;
938
939 read_lock(&em_tree->lock);
940 em = search_extent_mapping(em_tree, start, num_bytes);
941 if (em) {
942 /*
943 * if block start isn't an actual block number then find the
944 * first block in this inode and use that as a hint. If that
945 * block is also bogus then just don't worry about it.
946 */
947 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
948 free_extent_map(em);
949 em = search_extent_mapping(em_tree, 0, 0);
950 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
951 alloc_hint = em->block_start;
952 if (em)
953 free_extent_map(em);
954 } else {
955 alloc_hint = em->block_start;
956 free_extent_map(em);
957 }
958 }
959 read_unlock(&em_tree->lock);
960
961 return alloc_hint;
962}
963
Chris Mason771ed682008-11-06 22:02:51 -0500964/*
965 * when extent_io.c finds a delayed allocation range in the file,
966 * the call backs end up in this code. The basic idea is to
967 * allocate extents on disk for the range, and create ordered data structs
968 * in ram to track those extents.
969 *
970 * locked_page is the page that writepage had locked already. We use
971 * it to make sure we don't do extra locks or unlocks.
972 *
973 * *page_started is set to one if we unlock locked_page and do everything
974 * required to start IO on it. It may be clean and already done with
975 * IO when we return.
976 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300977static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -0400978 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300979 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300980 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500981{
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300982 struct btrfs_root *root = inode->root;
983 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500984 u64 alloc_hint = 0;
985 u64 num_bytes;
986 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000987 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +0100988 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400989 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500990 struct btrfs_key ins;
991 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +0000992 unsigned clear_bits;
993 unsigned long page_ops;
994 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -0500995 int ret = 0;
996
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300997 if (btrfs_is_free_space_inode(inode)) {
Josef 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 Masonb888db2b2007-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
Nikolay Borisov4546d172019-01-03 10:50:03 +02001204 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001205 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001206 * in which case we don't have anything to submit, yet we need to
1207 * always adjust ->async_delalloc_pages as its paired with the init
1208 * happening in cow_file_range_async
1209 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001210 if (async_chunk->inode)
1211 submit_compressed_extents(async_chunk);
Josef Bacik0901af52021-07-14 14:47:17 -04001212
1213 /* atomic_sub_return implies a barrier */
1214 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1215 5 * SZ_1M)
1216 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001217}
Chris Masonc8b97812008-10-29 14:49:59 -04001218
Chris Mason771ed682008-11-06 22:02:51 -05001219static noinline void async_cow_free(struct btrfs_work *work)
1220{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001221 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001222
Nikolay Borisovb5326272019-03-12 17:20:25 +02001223 async_chunk = container_of(work, struct async_chunk, work);
1224 if (async_chunk->inode)
1225 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001226 if (async_chunk->blkcg_css)
1227 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001228 /*
1229 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001230 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001231 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001232 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001233 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001234}
1235
Nikolay Borisov751b6432020-06-03 08:55:22 +03001236static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001237 struct writeback_control *wbc,
1238 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001239 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001240 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001241{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001242 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001243 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001244 struct async_cow *ctx;
1245 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001246 unsigned long nr_pages;
1247 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001248 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1249 int i;
1250 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001251 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001252 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001253
Nikolay Borisov751b6432020-06-03 08:55:22 +03001254 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001255
Nikolay Borisov751b6432020-06-03 08:55:22 +03001256 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001257 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1258 num_chunks = 1;
1259 should_compress = false;
1260 } else {
1261 should_compress = true;
1262 }
1263
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001264 nofs_flag = memalloc_nofs_save();
1265 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1266 memalloc_nofs_restore(nofs_flag);
1267
Nikolay Borisov97db1202019-03-12 17:20:24 +02001268 if (!ctx) {
1269 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1270 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1271 EXTENT_DO_ACCOUNTING;
1272 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1273 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1274 PAGE_SET_ERROR;
1275
Nikolay Borisov751b6432020-06-03 08:55:22 +03001276 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1277 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001278 return -ENOMEM;
1279 }
1280
1281 async_chunk = ctx->chunks;
1282 atomic_set(&ctx->num_chunks, num_chunks);
1283
1284 for (i = 0; i < num_chunks; i++) {
1285 if (should_compress)
1286 cur_end = min(end, start + SZ_512K - 1);
1287 else
1288 cur_end = end;
1289
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001290 /*
1291 * igrab is called higher up in the call chain, take only the
1292 * lightweight reference for the callback lifetime
1293 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001294 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001295 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001296 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001297 async_chunk[i].start = start;
1298 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001299 async_chunk[i].write_flags = write_flags;
1300 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001301
Chris Mason1d53c9e2019-07-10 12:28:16 -07001302 /*
1303 * The locked_page comes all the way from writepage and its
1304 * the original page we were actually given. As we spread
1305 * this large delalloc region across multiple async_chunk
1306 * structs, only the first struct needs a pointer to locked_page
1307 *
1308 * This way we don't need racey decisions about who is supposed
1309 * to unlock it.
1310 */
1311 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001312 /*
1313 * Depending on the compressibility, the pages might or
1314 * might not go through async. We want all of them to
1315 * be accounted against wbc once. Let's do it here
1316 * before the paths diverge. wbc accounting is used
1317 * only for foreign writeback detection and doesn't
1318 * need full accuracy. Just account the whole thing
1319 * against the first page.
1320 */
1321 wbc_account_cgroup_owner(wbc, locked_page,
1322 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001323 async_chunk[i].locked_page = locked_page;
1324 locked_page = NULL;
1325 } else {
1326 async_chunk[i].locked_page = NULL;
1327 }
1328
Chris Masonec39f762019-07-10 12:28:17 -07001329 if (blkcg_css != blkcg_root_css) {
1330 css_get(blkcg_css);
1331 async_chunk[i].blkcg_css = blkcg_css;
1332 } else {
1333 async_chunk[i].blkcg_css = NULL;
1334 }
1335
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001336 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1337 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001338
Nikolay Borisov97db1202019-03-12 17:20:24 +02001339 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001340 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001341
Nikolay Borisov97db1202019-03-12 17:20:24 +02001342 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001343
Chris Mason771ed682008-11-06 22:02:51 -05001344 *nr_written += nr_pages;
1345 start = cur_end + 1;
1346 }
1347 *page_started = 1;
1348 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001349}
1350
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001351static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001352 u64 bytenr, u64 num_bytes)
1353{
1354 int ret;
1355 struct btrfs_ordered_sum *sums;
1356 LIST_HEAD(list);
1357
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001358 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001359 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001360 if (ret == 0 && list_empty(&list))
1361 return 0;
1362
1363 while (!list_empty(&list)) {
1364 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1365 list_del(&sums->list);
1366 kfree(sums);
1367 }
Liu Bo58113752018-01-31 17:09:13 -07001368 if (ret < 0)
1369 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001370 return 1;
1371}
1372
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001373static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001374 const u64 start, const u64 end,
1375 int *page_started, unsigned long *nr_written)
1376{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001377 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1378 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001379 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001380 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001381 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001382 u64 range_start = start;
1383 u64 count;
1384
1385 /*
1386 * If EXTENT_NORESERVE is set it means that when the buffered write was
1387 * made we had not enough available data space and therefore we did not
1388 * reserve data space for it, since we though we could do NOCOW for the
1389 * respective file range (either there is prealloc extent or the inode
1390 * has the NOCOW bit set).
1391 *
1392 * However when we need to fallback to COW mode (because for example the
1393 * block group for the corresponding extent was turned to RO mode by a
1394 * scrub or relocation) we need to do the following:
1395 *
1396 * 1) We increment the bytes_may_use counter of the data space info.
1397 * If COW succeeds, it allocates a new data extent and after doing
1398 * that it decrements the space info's bytes_may_use counter and
1399 * increments its bytes_reserved counter by the same amount (we do
1400 * this at btrfs_add_reserved_bytes()). So we need to increment the
1401 * bytes_may_use counter to compensate (when space is reserved at
1402 * buffered write time, the bytes_may_use counter is incremented);
1403 *
1404 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1405 * that if the COW path fails for any reason, it decrements (through
1406 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1407 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001408 *
1409 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001410 * space cache inode or an inode of the data relocation tree, we must
1411 * also increment bytes_may_use of the data space_info for the same
1412 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001413 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001414 * group that contains that extent to RO mode and therefore force COW
1415 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001416 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001417 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001418 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001419 if (count > 0 || is_space_ino || is_reloc_ino) {
1420 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001421 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001422 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1423
Filipe Manana6bd335b2020-06-08 13:33:05 +01001424 if (is_space_ino || is_reloc_ino)
1425 bytes = range_bytes;
1426
Filipe Manana467dc472020-05-27 11:16:07 +01001427 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001428 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001429 spin_unlock(&sinfo->lock);
1430
Filipe Manana2166e5e2020-05-27 11:16:19 +01001431 if (count > 0)
1432 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1433 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001434 }
1435
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001436 return cow_file_range(inode, locked_page, start, end, page_started,
1437 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001438}
1439
Chris Masond352ac62008-09-29 15:18:18 -04001440/*
1441 * when nowcow writeback call back. This checks for snapshots or COW copies
1442 * of the extents that exist in the file, and COWs the file as required.
1443 *
1444 * If no cow copies or snapshots exist, we write directly to the existing
1445 * blocks on disk
1446 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001447static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001448 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001449 const u64 start, const u64 end,
1450 int *page_started, int force,
1451 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001452{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001453 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1454 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001455 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001456 u64 cow_start = (u64)-1;
1457 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001458 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001459 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001460 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1461 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001462 bool nocow = false;
1463 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001464
1465 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001466 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001467 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001468 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001469 EXTENT_DO_ACCOUNTING |
1470 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001471 PAGE_CLEAR_DIRTY |
1472 PAGE_SET_WRITEBACK |
1473 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001474 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001475 }
Li Zefan82d59022011-04-20 10:33:24 +08001476
Yan Zheng80ff3852008-10-30 14:20:02 -04001477 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001478 struct btrfs_key found_key;
1479 struct btrfs_file_extent_item *fi;
1480 struct extent_buffer *leaf;
1481 u64 extent_end;
1482 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001483 u64 num_bytes = 0;
1484 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001485 u64 ram_bytes;
1486 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001487
1488 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001489
Liu Boe4c3b2d2017-01-30 12:25:28 -08001490 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001491 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001492 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001493 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001494
1495 /*
1496 * If there is no extent for our range when doing the initial
1497 * search, then go back to the previous slot as it will be the
1498 * one containing the search offset
1499 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001500 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1501 leaf = path->nodes[0];
1502 btrfs_item_key_to_cpu(leaf, &found_key,
1503 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001504 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001505 found_key.type == BTRFS_EXTENT_DATA_KEY)
1506 path->slots[0]--;
1507 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001508 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001509next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001510 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001511 leaf = path->nodes[0];
1512 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1513 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001514 if (ret < 0) {
1515 if (cow_start != (u64)-1)
1516 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001517 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001518 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001519 if (ret > 0)
1520 break;
1521 leaf = path->nodes[0];
1522 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001523
Yan Zheng80ff3852008-10-30 14:20:02 -04001524 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001525
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001526 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001527 if (found_key.objectid > ino)
1528 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001529 /*
1530 * Keep searching until we find an EXTENT_ITEM or there are no
1531 * more extents for this inode
1532 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001533 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1534 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1535 path->slots[0]++;
1536 goto next_slot;
1537 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001538
1539 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001540 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001541 found_key.offset > end)
1542 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001543
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001544 /*
1545 * If the found extent starts after requested offset, then
1546 * adjust extent_end to be right before this extent begins
1547 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001548 if (found_key.offset > cur_offset) {
1549 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001550 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001551 goto out_check;
1552 }
Chris Masonc31f8832008-01-08 15:46:31 -05001553
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001554 /*
1555 * Found extent which begins before our range and potentially
1556 * intersect it
1557 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001558 fi = btrfs_item_ptr(leaf, path->slots[0],
1559 struct btrfs_file_extent_item);
1560 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001561
Josef Bacikcc95bef2013-04-04 14:31:27 -04001562 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001563 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1564 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001565 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001566 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001567 extent_end = found_key.offset +
1568 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001569 disk_num_bytes =
1570 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001571 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001572 * If the extent we got ends before our current offset,
1573 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001574 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001575 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001576 path->slots[0]++;
1577 goto next_slot;
1578 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001579 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001580 if (disk_bytenr == 0)
1581 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001582 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001583 if (btrfs_file_extent_compression(leaf, fi) ||
1584 btrfs_file_extent_encryption(leaf, fi) ||
1585 btrfs_file_extent_other_encoding(leaf, fi))
1586 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001587 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001588 * If extent is created before the last volume's snapshot
1589 * this implies the extent is shared, hence we can't do
1590 * nocow. This is the same check as in
1591 * btrfs_cross_ref_exist but without calling
1592 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001593 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001594 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001595 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001596 btrfs_root_last_snapshot(&root->root_item))
1597 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001598 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1599 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001600 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001601 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001602 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001603 ret = btrfs_cross_ref_exist(root, ino,
1604 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001605 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001606 if (ret) {
1607 /*
1608 * ret could be -EIO if the above fails to read
1609 * metadata.
1610 */
1611 if (ret < 0) {
1612 if (cow_start != (u64)-1)
1613 cur_offset = cow_start;
1614 goto error;
1615 }
1616
Nikolay Borisov3e024842019-08-21 10:42:03 +03001617 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001618 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001619 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001620 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001621 disk_bytenr += cur_offset - found_key.offset;
1622 num_bytes = min(end + 1, extent_end) - cur_offset;
1623 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001624 * If there are pending snapshots for this root, we
1625 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001626 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001627 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001628 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001629 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001630 * force cow if csum exists in the range.
1631 * this ensure that csum for a given extent are
1632 * either valid or do not exist.
1633 */
Liu Bo58113752018-01-31 17:09:13 -07001634 ret = csum_exist_in_range(fs_info, disk_bytenr,
1635 num_bytes);
1636 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001637 /*
1638 * ret could be -EIO if the above fails to read
1639 * metadata.
1640 */
1641 if (ret < 0) {
1642 if (cow_start != (u64)-1)
1643 cur_offset = cow_start;
1644 goto error;
1645 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001646 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001647 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001648 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001649 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001650 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001651 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001652 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001653 extent_end = found_key.offset + ram_bytes;
1654 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001655 /* Skip extents outside of our requested range */
1656 if (extent_end <= start) {
1657 path->slots[0]++;
1658 goto next_slot;
1659 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001660 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001661 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001662 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001663 }
1664out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001665 /*
1666 * If nocow is false then record the beginning of the range
1667 * that needs to be COWed
1668 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001669 if (!nocow) {
1670 if (cow_start == (u64)-1)
1671 cow_start = cur_offset;
1672 cur_offset = extent_end;
1673 if (cur_offset > end)
1674 break;
1675 path->slots[0]++;
1676 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001677 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001678
David Sterbab3b4aa72011-04-21 01:20:15 +02001679 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001680
1681 /*
1682 * COW range from cow_start to found_key.offset - 1. As the key
1683 * will contain the beginning of the first extent that can be
1684 * NOCOW, following one which needs to be COW'ed
1685 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001686 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001687 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001688 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001689 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001690 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001691 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001692 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001693 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001694
Yan Zhengd899e052008-10-30 14:25:28 -04001695 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001696 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001697 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001698
Nikolay Borisov968322c2020-06-03 08:55:21 +03001699 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001700 orig_start,
1701 disk_bytenr, /* block_start */
1702 num_bytes, /* block_len */
1703 disk_num_bytes, /* orig_block_len */
1704 ram_bytes, BTRFS_COMPRESS_NONE,
1705 BTRFS_ORDERED_PREALLOC);
1706 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001707 ret = PTR_ERR(em);
1708 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001709 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001710 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001711 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001712 disk_bytenr, num_bytes,
1713 num_bytes,
1714 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001715 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001716 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001717 cur_offset + num_bytes - 1,
1718 0);
1719 goto error;
1720 }
Yan Zhengd899e052008-10-30 14:25:28 -04001721 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001722 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001723 disk_bytenr, num_bytes,
1724 num_bytes,
1725 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001726 if (ret)
1727 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001728 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001729
Filipe Mananaf78c4362016-05-09 13:15:41 +01001730 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001731 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001732 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001733
Yan, Zhengefa56462010-05-16 10:49:59 -04001734 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001735 BTRFS_DATA_RELOC_TREE_OBJECTID)
1736 /*
1737 * Error handled later, as we must prevent
1738 * extent_clear_unlock_delalloc() in error handler
1739 * from freeing metadata of created ordered extent.
1740 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001741 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001742 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001743
Nikolay Borisov968322c2020-06-03 08:55:21 +03001744 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001745 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001746 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001747 EXTENT_DELALLOC |
1748 EXTENT_CLEAR_DATA_RESV,
1749 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1750
Yan Zheng80ff3852008-10-30 14:20:02 -04001751 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001752
1753 /*
1754 * btrfs_reloc_clone_csums() error, now we're OK to call error
1755 * handler, as metadata for created ordered extent will only
1756 * be freed by btrfs_finish_ordered_io().
1757 */
1758 if (ret)
1759 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001760 if (cur_offset > end)
1761 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001762 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001763 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001764
Robbie Ko506481b2018-10-30 18:04:04 +08001765 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001766 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001767
Yan Zheng80ff3852008-10-30 14:20:02 -04001768 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001769 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001770 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1771 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001772 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001773 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001774 }
1775
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001776error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001777 if (nocow)
1778 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1779
Josef Bacik17ca04a2012-05-31 15:58:55 -04001780 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001781 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001782 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001783 EXTENT_DELALLOC | EXTENT_DEFRAG |
1784 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1785 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001786 PAGE_SET_WRITEBACK |
1787 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001788 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001789 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001790}
1791
Nikolay Borisov0c494222020-06-03 08:55:28 +03001792static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001793{
1794
Nikolay Borisov0c494222020-06-03 08:55:28 +03001795 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1796 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001797 return 0;
1798
1799 /*
1800 * @defrag_bytes is a hint value, no spinlock held here,
1801 * if is not zero, it means the file is defragging.
1802 * Force cow if given extent needs to be defragged.
1803 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001804 if (inode->defrag_bytes &&
1805 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001806 return 1;
1807
1808 return 0;
1809}
1810
Chris Masond352ac62008-09-29 15:18:18 -04001811/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001812 * Function to process delayed allocation (create CoW) for ranges which are
1813 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001814 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001815int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001816 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1817 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001818{
Chris Masonbe20aa92007-12-17 20:14:01 -05001819 int ret;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001820 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001821
Nikolay Borisov98456b92020-06-03 08:55:29 +03001822 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1823 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001824 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001825 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1826 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001827 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001828 } else if (!inode_can_compress(inode) ||
1829 !inode_need_compress(inode, start, end)) {
1830 ret = cow_file_range(inode, locked_page, start, end,
1831 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001832 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001833 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1834 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001835 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001836 }
Qu Wenruo524272602017-03-08 10:25:52 +08001837 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001838 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001839 end - start + 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04001840 return ret;
1841}
1842
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001843void btrfs_split_delalloc_extent(struct inode *inode,
1844 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001845{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001846 u64 size;
1847
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001848 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001849 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001850 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001851
Josef Bacikdcab6a32015-02-11 15:08:59 -05001852 size = orig->end - orig->start + 1;
1853 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001854 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001855 u64 new_size;
1856
1857 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001858 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001859 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001860 */
1861 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001862 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001863 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001864 num_extents += count_max_extents(new_size);
1865 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001866 return;
1867 }
1868
Josef Bacik9e0baf62011-07-15 15:16:44 +00001869 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001870 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001871 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001872}
1873
1874/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001875 * Handle merged delayed allocation extents so we can keep track of new extents
1876 * that are just merged onto old extents, such as when we are doing sequential
1877 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001878 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001879void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1880 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001881{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001882 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001883 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001884
Josef Bacik9ed74f22009-09-11 16:12:44 -04001885 /* not delalloc, ignore it */
1886 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001887 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001888
Josef Bacik8461a3d2015-03-13 15:12:08 -04001889 if (new->start > other->start)
1890 new_size = new->end - other->start + 1;
1891 else
1892 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001893
1894 /* we're not bigger than the max, unreserve the space and go */
1895 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1896 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001897 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001898 spin_unlock(&BTRFS_I(inode)->lock);
1899 return;
1900 }
1901
1902 /*
Josef Bacikba117212015-03-13 15:01:24 -04001903 * We have to add up either side to figure out how many extents were
1904 * accounted for before we merged into one big extent. If the number of
1905 * extents we accounted for is <= the amount we need for the new range
1906 * then we can return, otherwise drop. Think of it like this
1907 *
1908 * [ 4k][MAX_SIZE]
1909 *
1910 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1911 * need 2 outstanding extents, on one side we have 1 and the other side
1912 * we have 1 so they are == and we can return. But in this case
1913 *
1914 * [MAX_SIZE+4k][MAX_SIZE+4k]
1915 *
1916 * Each range on their own accounts for 2 extents, but merged together
1917 * they are only 3 extents worth of accounting, so we need to drop in
1918 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001919 */
Josef Bacikba117212015-03-13 15:01:24 -04001920 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001921 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001922 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001923 num_extents += count_max_extents(old_size);
1924 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001925 return;
1926
Josef Bacik9e0baf62011-07-15 15:16:44 +00001927 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001928 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001929 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001930}
1931
Miao Xieeb73c1b2013-05-15 07:48:22 +00001932static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1933 struct inode *inode)
1934{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001935 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1936
Miao Xieeb73c1b2013-05-15 07:48:22 +00001937 spin_lock(&root->delalloc_lock);
1938 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1939 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1940 &root->delalloc_inodes);
1941 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1942 &BTRFS_I(inode)->runtime_flags);
1943 root->nr_delalloc_inodes++;
1944 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001945 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001946 BUG_ON(!list_empty(&root->delalloc_root));
1947 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001948 &fs_info->delalloc_roots);
1949 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001950 }
1951 }
1952 spin_unlock(&root->delalloc_lock);
1953}
1954
Nikolay Borisov2b877332018-04-27 12:21:51 +03001955
1956void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1957 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001958{
David Sterba3ffbd682018-06-29 10:56:42 +02001959 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001960
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001961 if (!list_empty(&inode->delalloc_inodes)) {
1962 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001963 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001964 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001965 root->nr_delalloc_inodes--;
1966 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001967 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001968 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001969 BUG_ON(list_empty(&root->delalloc_root));
1970 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001971 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001972 }
1973 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001974}
1975
1976static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1977 struct btrfs_inode *inode)
1978{
1979 spin_lock(&root->delalloc_lock);
1980 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001981 spin_unlock(&root->delalloc_lock);
1982}
1983
Chris Masond352ac62008-09-29 15:18:18 -04001984/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001985 * Properly track delayed allocation bytes in the inode and to maintain the
1986 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001987 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001988void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1989 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001990{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001991 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1992
Wang Shilong47059d92014-07-03 18:22:07 +08001993 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1994 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05001995 /*
1996 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00001997 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05001998 * bit, which is only set or cleared with irqs on
1999 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002000 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002001 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002002 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002003 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002004 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002005
Josef Bacik8b62f872017-10-19 14:15:55 -04002006 spin_lock(&BTRFS_I(inode)->lock);
2007 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2008 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002009
Josef Bacik6a3891c2015-03-16 17:38:52 -04002010 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002011 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002012 return;
2013
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002014 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2015 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002016 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002017 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002018 if (*bits & EXTENT_DEFRAG)
2019 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002020 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002021 &BTRFS_I(inode)->runtime_flags))
2022 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002023 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002024 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002025
2026 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2027 (*bits & EXTENT_DELALLOC_NEW)) {
2028 spin_lock(&BTRFS_I(inode)->lock);
2029 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2030 state->start;
2031 spin_unlock(&BTRFS_I(inode)->lock);
2032 }
Chris Mason291d6732008-01-29 15:55:23 -05002033}
2034
Chris Masond352ac62008-09-29 15:18:18 -04002035/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002036 * Once a range is no longer delalloc this function ensures that proper
2037 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002038 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002039void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2040 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002041{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002042 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2043 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002044 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002045 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002046
Filipe Manana4a4b9642017-07-27 19:52:55 +01002047 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2048 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002049 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002050 spin_unlock(&inode->lock);
2051 }
Wang Shilong47059d92014-07-03 18:22:07 +08002052
Chris Mason75eff682008-12-15 15:54:40 -05002053 /*
2054 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002055 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002056 * bit, which is only set or cleared with irqs on
2057 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002058 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002059 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002060 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002061
Josef Bacik8b62f872017-10-19 14:15:55 -04002062 spin_lock(&inode->lock);
2063 btrfs_mod_outstanding_extents(inode, -num_extents);
2064 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002065
Josef Bacikb6d08f02013-09-27 14:57:43 -04002066 /*
2067 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002068 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002069 * error.
2070 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002071 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002072 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002073 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002074
Josef Bacik6a3891c2015-03-16 17:38:52 -04002075 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002076 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002077 return;
2078
Filipe Mananaa315e682017-03-06 23:04:20 +00002079 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2080 do_list && !(state->state & EXTENT_NORESERVE) &&
2081 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002082 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002083
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002084 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2085 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002086 spin_lock(&inode->lock);
2087 inode->delalloc_bytes -= len;
2088 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002089 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002090 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002091 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002092 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002093 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002094
2095 if ((state->state & EXTENT_DELALLOC_NEW) &&
2096 (*bits & EXTENT_DELALLOC_NEW)) {
2097 spin_lock(&inode->lock);
2098 ASSERT(inode->new_delalloc_bytes >= len);
2099 inode->new_delalloc_bytes -= len;
2100 spin_unlock(&inode->lock);
2101 }
Chris Mason291d6732008-01-29 15:55:23 -05002102}
2103
Chris Masond352ac62008-09-29 15:18:18 -04002104/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002105 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2106 * in a chunk's stripe. This function ensures that bios do not span a
2107 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002108 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002109 * @page - The page we are about to add to the bio
2110 * @size - size we want to add to the bio
2111 * @bio - bio we want to ensure is smaller than a stripe
2112 * @bio_flags - flags of the bio
2113 *
2114 * return 1 if page cannot be added to the bio
2115 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002116 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002117 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002118int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2119 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002120{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002121 struct inode *inode = page->mapping->host;
2122 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002123 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002124 u64 length = 0;
2125 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002126 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002127 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002128
Chris Mason771ed682008-11-06 22:02:51 -05002129 if (bio_flags & EXTENT_BIO_COMPRESSED)
2130 return 0;
2131
Kent Overstreet4f024f32013-10-11 15:44:27 -07002132 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002133 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002134 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2135 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002136 if (ret < 0)
2137 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002138
2139 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002140 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002141 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002142}
2143
Chris Masond352ac62008-09-29 15:18:18 -04002144/*
2145 * in order to insert checksums into the metadata in large chunks,
2146 * we wait until bio submission time. All the pages in the bio are
2147 * checksummed and sums are attached onto the ordered extent record.
2148 *
2149 * At IO completion time the cums attached on the ordered extent record
2150 * are inserted into the btree
2151 */
David Sterbad0ee3932018-03-08 14:35:48 +01002152static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002153 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002154{
Josef Bacikc6100a42017-05-05 11:57:13 -04002155 struct inode *inode = private_data;
Chris Masone0156402008-04-16 11:15:20 -04002156
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002157 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002158}
Chris Masone0156402008-04-16 11:15:20 -04002159
Chris Mason4a69a412008-11-06 22:03:00 -05002160/*
Chris Masoncad321a2008-12-17 14:51:42 -05002161 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002162 * on write, or reading the csums from the tree before a read.
2163 *
2164 * Rules about async/sync submit,
2165 * a) read: sync submit
2166 *
2167 * b) write without checksum: sync submit
2168 *
2169 * c) write with checksum:
2170 * c-1) if bio is issued by fsync: sync submit
2171 * (sync_writers != 0)
2172 *
2173 * c-2) if root is reloc root: sync submit
2174 * (only in case of buffered IO)
2175 *
2176 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002177 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002178blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2179 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002180
Chris Mason44b8bd72008-04-16 11:14:51 -04002181{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002182 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002183 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302184 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002185 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002186 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002187 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002188
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002189 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002190
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002191 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302192 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002193
Mike Christie37226b22016-06-05 14:31:52 -05002194 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002195 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002196 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002197 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002198
Chris Masond20f7042008-12-08 16:58:54 -05002199 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002200 ret = btrfs_submit_compressed_read(inode, bio,
2201 mirror_num,
2202 bio_flags);
2203 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002204 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002205 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002206 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002207 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002208 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002209 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002210 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002211 /* csum items have already been cloned */
2212 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2213 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002214 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002215 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002216 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002217 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002218 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002219 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002220 if (ret)
2221 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002222 }
2223
Chris Mason0b86a832008-03-24 15:01:56 -04002224mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002225 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002226
2227out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002228 if (ret) {
2229 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002230 bio_endio(bio);
2231 }
Stefan Behrens61891922012-11-05 18:51:52 +01002232 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002233}
Chris Mason6885f302008-02-20 16:11:05 -05002234
Chris Masond352ac62008-09-29 15:18:18 -04002235/*
2236 * given a list of ordered sums record them in the inode. This happens
2237 * at IO completion time based on sums calculated at bio submission time.
2238 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002239static int add_pending_csums(struct btrfs_trans_handle *trans,
2240 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002241{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002242 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002243 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002244
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002245 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002246 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002247 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002248 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002249 if (ret)
2250 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002251 }
2252 return 0;
2253}
2254
Filipe Mananac3347302020-11-04 11:07:31 +00002255static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2256 const u64 start,
2257 const u64 len,
2258 struct extent_state **cached_state)
2259{
2260 u64 search_start = start;
2261 const u64 end = start + len - 1;
2262
2263 while (search_start < end) {
2264 const u64 search_len = end - search_start + 1;
2265 struct extent_map *em;
2266 u64 em_len;
2267 int ret = 0;
2268
2269 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2270 if (IS_ERR(em))
2271 return PTR_ERR(em);
2272
2273 if (em->block_start != EXTENT_MAP_HOLE)
2274 goto next;
2275
2276 em_len = em->len;
2277 if (em->start < search_start)
2278 em_len -= search_start - em->start;
2279 if (em_len > search_len)
2280 em_len = search_len;
2281
2282 ret = set_extent_bit(&inode->io_tree, search_start,
2283 search_start + em_len - 1,
2284 EXTENT_DELALLOC_NEW,
2285 NULL, cached_state, GFP_NOFS);
2286next:
2287 search_start = extent_map_end(em);
2288 free_extent_map(em);
2289 if (ret)
2290 return ret;
2291 }
2292 return 0;
2293}
2294
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002295int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002296 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002297 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002298{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002299 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002300
2301 if (start >= i_size_read(&inode->vfs_inode) &&
2302 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2303 /*
2304 * There can't be any extents following eof in this case so just
2305 * set the delalloc new bit for the range directly.
2306 */
2307 extra_bits |= EXTENT_DELALLOC_NEW;
2308 } else {
2309 int ret;
2310
2311 ret = btrfs_find_new_delalloc_bytes(inode, start,
2312 end + 1 - start,
2313 cached_state);
2314 if (ret)
2315 return ret;
2316 }
2317
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002318 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2319 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002320}
2321
Chris Masond352ac62008-09-29 15:18:18 -04002322/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002323struct btrfs_writepage_fixup {
2324 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002325 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002326 struct btrfs_work work;
2327};
2328
Christoph Hellwigb2950862008-12-02 09:54:17 -05002329static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002330{
2331 struct btrfs_writepage_fixup *fixup;
2332 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002333 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002334 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002335 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002336 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002337 u64 page_start;
2338 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002339 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002340 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002341
2342 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2343 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002344 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002345 page_start = page_offset(page);
2346 page_end = page_offset(page) + PAGE_SIZE - 1;
2347
2348 /*
2349 * This is similar to page_mkwrite, we need to reserve the space before
2350 * we take the page lock.
2351 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002352 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2353 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002354again:
Chris Mason247e7432008-07-17 12:53:51 -04002355 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002356
Chris Mason25f3c502020-01-21 11:51:42 -05002357 /*
2358 * Before we queued this fixup, we took a reference on the page.
2359 * page->mapping may go NULL, but it shouldn't be moved to a different
2360 * address space.
2361 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002362 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2363 /*
2364 * Unfortunately this is a little tricky, either
2365 *
2366 * 1) We got here and our page had already been dealt with and
2367 * we reserved our space, thus ret == 0, so we need to just
2368 * drop our space reservation and bail. This can happen the
2369 * first time we come into the fixup worker, or could happen
2370 * while waiting for the ordered extent.
2371 * 2) Our page was already dealt with, but we happened to get an
2372 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2373 * this case we obviously don't have anything to release, but
2374 * because the page was already dealt with we don't want to
2375 * mark the page with an error, so make sure we're resetting
2376 * ret to 0. This is why we have this check _before_ the ret
2377 * check, because we do not want to have a surprise ENOSPC
2378 * when the page was already properly dealt with.
2379 */
2380 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002381 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2382 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002383 page_start, PAGE_SIZE,
2384 true);
2385 }
2386 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002387 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002388 }
Chris Mason25f3c502020-01-21 11:51:42 -05002389
2390 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002391 * We can't mess with the page state unless it is locked, so now that
2392 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002393 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002394 if (ret)
2395 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002396
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002397 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002398
2399 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002400 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002401 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002402
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002403 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002404 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002405 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2406 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002407 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002408 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002409 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002410 goto again;
2411 }
Chris Mason247e7432008-07-17 12:53:51 -04002412
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002413 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002414 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002415 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002416 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002417
Chris Mason25f3c502020-01-21 11:51:42 -05002418 /*
2419 * Everything went as planned, we're now the owner of a dirty page with
2420 * delayed allocation bits set and space reserved for our COW
2421 * destination.
2422 *
2423 * The page was dirty when we started, nothing should have cleaned it.
2424 */
2425 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002426 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002427out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002428 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002429 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002430 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2431 PAGE_SIZE, true);
2432 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002433 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002434out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002435 if (ret) {
2436 /*
2437 * We hit ENOSPC or other errors. Update the mapping and page
2438 * to reflect the errors and clean the page.
2439 */
2440 mapping_set_error(page->mapping, ret);
2441 end_extent_writepage(page, ret, page_start, page_end);
2442 clear_page_dirty_for_io(page);
2443 SetPageError(page);
2444 }
2445 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002446 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002447 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002448 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002449 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002450 /*
2451 * As a precaution, do a delayed iput in case it would be the last iput
2452 * that could need flushing space. Recursing back to fixup worker would
2453 * deadlock.
2454 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002455 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002456}
2457
2458/*
2459 * There are a few paths in the higher layers of the kernel that directly
2460 * set the page dirty bit without asking the filesystem if it is a
2461 * good idea. This causes problems because we want to make sure COW
2462 * properly happens and the data=ordered rules are followed.
2463 *
Chris Masonc8b97812008-10-29 14:49:59 -04002464 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002465 * hasn't been properly setup for IO. We kick off an async process
2466 * to fix it up. The async helper will wait for ordered extents, set
2467 * the delalloc bit and make it safe to write the page.
2468 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002469int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002470{
2471 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002472 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002473 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002474
Chris Mason8b62b722009-09-02 16:53:46 -04002475 /* this page is properly in the ordered list */
2476 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002477 return 0;
2478
Chris Mason25f3c502020-01-21 11:51:42 -05002479 /*
2480 * PageChecked is set below when we create a fixup worker for this page,
2481 * don't try to create another one if we're already PageChecked()
2482 *
2483 * The extent_io writepage code will redirty the page if we send back
2484 * EAGAIN.
2485 */
Chris Mason247e7432008-07-17 12:53:51 -04002486 if (PageChecked(page))
2487 return -EAGAIN;
2488
2489 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2490 if (!fixup)
2491 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002492
Josef Bacikf4b13632020-01-21 14:34:52 -05002493 /*
2494 * We are already holding a reference to this inode from
2495 * write_cache_pages. We need to hold it because the space reservation
2496 * takes place outside of the page lock, and we can't trust
2497 * page->mapping outside of the page lock.
2498 */
2499 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002500 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002501 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002502 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002503 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002504 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002505 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002506
2507 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002508}
2509
Yan Zhengd899e052008-10-30 14:25:28 -04002510static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002511 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002512 struct btrfs_file_extent_item *stack_fi,
2513 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002514{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002515 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002516 struct btrfs_path *path;
2517 struct extent_buffer *leaf;
2518 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002519 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2520 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2521 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2522 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002523 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002524 int ret;
2525
2526 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002527 if (!path)
2528 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002529
Chris Masona1ed8352009-09-11 12:27:37 -04002530 /*
2531 * we may be replacing one extent in the tree with another.
2532 * The new extent is pinned in the extent map, and we don't want
2533 * to drop it from the cache until it is completely in the btree.
2534 *
2535 * So, tell btrfs_drop_extents to leave this extent in the cache.
2536 * the caller is expected to unpin it and allow it to be merged
2537 * with the others.
2538 */
Nikolay Borisovc553f942020-06-03 08:55:19 +03002539 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002540 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002541 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002542 if (ret)
2543 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002544
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002545 if (!extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002546 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002547 ins.offset = file_pos;
2548 ins.type = BTRFS_EXTENT_DATA_KEY;
2549
2550 path->leave_spinning = 1;
2551 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002552 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002553 if (ret)
2554 goto out;
2555 }
Yan Zhengd899e052008-10-30 14:25:28 -04002556 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002557 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2558 write_extent_buffer(leaf, stack_fi,
2559 btrfs_item_ptr_offset(leaf, path->slots[0]),
2560 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002561
Yan Zhengd899e052008-10-30 14:25:28 -04002562 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002563 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002564
Nikolay Borisovc553f942020-06-03 08:55:19 +03002565 inode_add_bytes(&inode->vfs_inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002566
2567 ins.objectid = disk_bytenr;
2568 ins.offset = disk_num_bytes;
2569 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002570
Nikolay Borisovc553f942020-06-03 08:55:19 +03002571 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002572 if (ret)
2573 goto out;
2574
Nikolay Borisovc553f942020-06-03 08:55:19 +03002575 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002576 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002577out:
Yan Zhengd899e052008-10-30 14:25:28 -04002578 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002579
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002580 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002581}
2582
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002583static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002584 u64 start, u64 len)
2585{
David Sterba32da53862019-10-29 19:20:18 +01002586 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002587
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002588 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002589 ASSERT(cache);
2590
2591 spin_lock(&cache->lock);
2592 cache->delalloc_bytes -= len;
2593 spin_unlock(&cache->lock);
2594
2595 btrfs_put_block_group(cache);
2596}
2597
Qu Wenruo203f44c52020-06-10 09:04:40 +08002598static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002599 struct btrfs_ordered_extent *oe)
2600{
2601 struct btrfs_file_extent_item stack_fi;
2602 u64 logical_len;
2603
2604 memset(&stack_fi, 0, sizeof(stack_fi));
2605 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2606 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2607 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2608 oe->disk_num_bytes);
2609 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2610 logical_len = oe->truncated_len;
2611 else
2612 logical_len = oe->num_bytes;
2613 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2614 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2615 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2616 /* Encryption and other encoding is reserved and all 0 */
2617
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002618 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2619 oe->file_offset, &stack_fi,
2620 oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002621}
2622
2623/*
2624 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002625 * an ordered extent if the range of bytes in the file it covers are
2626 * fully written.
2627 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002628static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002629{
Josef Bacik5fd02042012-05-02 14:00:54 -04002630 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002631 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002632 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002633 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002634 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002635 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002636 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002637 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002638 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002639 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002640 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002641 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002642 bool range_locked = false;
2643 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002644 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002645 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002646
Omar Sandovalbffe6332019-12-02 17:34:19 -08002647 start = ordered_extent->file_offset;
2648 end = start + ordered_extent->num_bytes - 1;
2649
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002650 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2651 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2652 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2653 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002654
Nikolay Borisov8d510122019-10-08 20:43:06 +03002655 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002656
Josef Bacik5fd02042012-05-02 14:00:54 -04002657 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2658 ret = -EIO;
2659 goto out;
2660 }
2661
Omar Sandovalbffe6332019-12-02 17:34:19 -08002662 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002663
Josef Bacik77cef2e2013-08-29 13:57:21 -04002664 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2665 truncated = true;
2666 logical_len = ordered_extent->truncated_len;
2667 /* Truncated the entire extent, don't bother adding */
2668 if (!logical_len)
2669 goto out;
2670 }
2671
Yan, Zhengc2167752009-11-12 09:34:21 +00002672 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002673 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002674
Josef Bacikd923afe2020-01-17 09:02:23 -05002675 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002676 if (freespace_inode)
2677 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002678 else
2679 trans = btrfs_join_transaction(root);
2680 if (IS_ERR(trans)) {
2681 ret = PTR_ERR(trans);
2682 trans = NULL;
2683 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002684 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002685 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002686 ret = btrfs_update_inode_fallback(trans, root, inode);
2687 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002688 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002689 goto out;
2690 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002691
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002692 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002693 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002694
Nikolay Borisov8d510122019-10-08 20:43:06 +03002695 if (freespace_inode)
2696 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002697 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002698 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002699 if (IS_ERR(trans)) {
2700 ret = PTR_ERR(trans);
2701 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002702 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002703 }
Chris Masona79b7d42014-05-22 16:18:52 -07002704
Josef Bacik69fe2d72017-10-19 14:15:57 -04002705 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002706
Chris Masonc8b97812008-10-29 14:49:59 -04002707 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002708 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002709 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002710 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002711 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002712 ordered_extent->file_offset,
2713 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002714 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002715 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002716 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002717 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002718 if (!ret) {
2719 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002720 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002721 ordered_extent->disk_bytenr,
2722 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002723 }
Yan Zhengd899e052008-10-30 14:25:28 -04002724 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002725 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002726 ordered_extent->file_offset,
2727 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002728 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002729 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002730 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002731 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002732
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002733 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002734 if (ret) {
2735 btrfs_abort_transaction(trans, ret);
2736 goto out;
2737 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002738
Josef Bacikd923afe2020-01-17 09:02:23 -05002739 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002740 ret = btrfs_update_inode_fallback(trans, root, inode);
2741 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002742 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002743 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002744 }
2745 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002746out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002747 clear_bits = EXTENT_DEFRAG;
2748 if (range_locked)
2749 clear_bits |= EXTENT_LOCKED;
2750 if (clear_new_delalloc_bytes)
2751 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002752 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2753 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002754 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002755
Miao Xiea698d0752012-09-20 01:51:59 -06002756 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002757 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002758
Josef Bacik77cef2e2013-08-29 13:57:21 -04002759 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002760 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002761
Josef Bacikb547a162021-05-19 09:38:27 -04002762 /*
2763 * If we failed to finish this ordered extent for any reason we
2764 * need to make sure BTRFS_ORDERED_IOERR is set on the ordered
2765 * extent, and mark the inode with the error if it wasn't
2766 * already set. Any error during writeback would have already
2767 * set the mapping error, so we need to set it if we're the ones
2768 * marking this ordered extent as failed.
2769 */
2770 if (ret && !test_and_set_bit(BTRFS_ORDERED_IOERR,
2771 &ordered_extent->flags))
2772 mapping_set_error(ordered_extent->inode->i_mapping, -EIO);
2773
Josef Bacik77cef2e2013-08-29 13:57:21 -04002774 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002775 unwritten_start += logical_len;
2776 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002777
2778 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002779 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002780
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002781 /*
2782 * If the ordered extent had an IOERR or something else went
2783 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002784 * back to the allocator. We only free the extent in the
2785 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002786 *
2787 * If we made it past insert_reserved_file_extent before we
2788 * errored out then we don't need to do this as the accounting
2789 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002790 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002791 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002792 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002793 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002794 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2795 /*
2796 * Discard the range before returning it back to the
2797 * free space pool
2798 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002799 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002800 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002801 ordered_extent->disk_bytenr,
2802 ordered_extent->disk_num_bytes,
2803 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002804 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002805 ordered_extent->disk_bytenr,
2806 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002807 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002808 }
2809
Josef Bacik5fd02042012-05-02 14:00:54 -04002810 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002811 * This needs to be done to make sure anybody waiting knows we are done
2812 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002813 */
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03002814 btrfs_remove_ordered_extent(BTRFS_I(inode), ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04002815
Chris Masone6dcd2d2008-07-17 12:53:50 -04002816 /* once for us */
2817 btrfs_put_ordered_extent(ordered_extent);
2818 /* once for the tree */
2819 btrfs_put_ordered_extent(ordered_extent);
2820
Josef Bacik5fd02042012-05-02 14:00:54 -04002821 return ret;
2822}
2823
2824static void finish_ordered_fn(struct btrfs_work *work)
2825{
2826 struct btrfs_ordered_extent *ordered_extent;
2827 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2828 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002829}
2830
Nikolay Borisovc6297322018-11-08 10:18:08 +02002831void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2832 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002833{
Nikolay Borisov3347c482020-08-31 14:42:44 +03002834 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2835 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04002836 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002837 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002838
liubo1abe9b82011-03-24 11:18:59 +00002839 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2840
Chris Mason8b62b722009-09-02 16:53:46 -04002841 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03002842 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2843 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002844 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002845
Nikolay Borisov3347c482020-08-31 14:42:44 +03002846 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002847 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002848 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002849 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002850
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002851 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002852 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002853}
2854
Omar Sandoval47df7762020-04-16 14:46:17 -07002855static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2856 int icsum, struct page *page, int pgoff, u64 start,
2857 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002858{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002859 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2860 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002861 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002862 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2863 u8 *csum_expected;
2864 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002865
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002866 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002867
2868 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002869 shash->tfm = fs_info->csum_shash;
2870
Eric Biggersfd080012020-04-30 23:51:59 -07002871 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002872
2873 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002874 goto zeroit;
2875
2876 kunmap_atomic(kaddr);
2877 return 0;
2878zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002879 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2880 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03002881 if (io_bio->device)
2882 btrfs_dev_stat_inc_and_print(io_bio->device,
2883 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08002884 memset(kaddr + pgoff, 1, len);
2885 flush_dcache_page(page);
2886 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002887 return -EIO;
2888}
2889
Chris Masond352ac62008-09-29 15:18:18 -04002890/*
Chris Masond352ac62008-09-29 15:18:18 -04002891 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002892 * if there's a match, we allow the bio to finish. If not, the code in
2893 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002894 */
Nikolay Borisov9a446d62020-09-18 16:34:33 +03002895int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset,
2896 struct page *page, u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002897{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002898 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002899 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002900 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002901 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002902
Chris Masond20f7042008-12-08 16:58:54 -05002903 if (PageChecked(page)) {
2904 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002905 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002906 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002907
2908 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002909 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002910
Yan Zheng17d217f2008-12-12 10:03:38 -05002911 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002912 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002913 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002914 return 0;
2915 }
2916
Miao Xiefacc8a222013-07-25 19:22:34 +08002917 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002918 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2919 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002920}
Chris Masonb888db2b2007-08-27 16:49:44 -04002921
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002922/*
2923 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2924 *
2925 * @inode: The inode we want to perform iput on
2926 *
2927 * This function uses the generic vfs_inode::i_count to track whether we should
2928 * just decrement it (in case it's > 1) or if this is the last iput then link
2929 * the inode to the delayed iput machinery. Delayed iputs are processed at
2930 * transaction commit time/superblock commit/cleaner kthread.
2931 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002932void btrfs_add_delayed_iput(struct inode *inode)
2933{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002935 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002936
2937 if (atomic_add_unless(&inode->i_count, -1, 1))
2938 return;
2939
Josef Bacik034f7842018-12-03 11:06:52 -05002940 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002941 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002942 ASSERT(list_empty(&binode->delayed_iput));
2943 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002944 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002945 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2946 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002947}
2948
Josef Bacik63611e72019-06-18 10:59:18 -04002949static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2950 struct btrfs_inode *inode)
2951{
2952 list_del_init(&inode->delayed_iput);
2953 spin_unlock(&fs_info->delayed_iput_lock);
2954 iput(&inode->vfs_inode);
2955 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2956 wake_up(&fs_info->delayed_iputs_wait);
2957 spin_lock(&fs_info->delayed_iput_lock);
2958}
2959
2960static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2961 struct btrfs_inode *inode)
2962{
2963 if (!list_empty(&inode->delayed_iput)) {
2964 spin_lock(&fs_info->delayed_iput_lock);
2965 if (!list_empty(&inode->delayed_iput))
2966 run_delayed_iput_locked(fs_info, inode);
2967 spin_unlock(&fs_info->delayed_iput_lock);
2968 }
2969}
2970
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002971void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002972{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002973
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002974 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002975 while (!list_empty(&fs_info->delayed_iputs)) {
2976 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002977
David Sterba8089fe62015-11-19 14:15:51 +01002978 inode = list_first_entry(&fs_info->delayed_iputs,
2979 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002980 run_delayed_iput_locked(fs_info, inode);
Josef Bacik56001dd2021-04-29 10:51:34 -04002981 cond_resched_lock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002982 }
David Sterba8089fe62015-11-19 14:15:51 +01002983 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002984}
2985
Josef Bacik034f7842018-12-03 11:06:52 -05002986/**
2987 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2988 * @fs_info - the fs_info for this fs
2989 * @return - EINTR if we were killed, 0 if nothing's pending
2990 *
2991 * This will wait on any delayed iputs that are currently running with KILLABLE
2992 * set. Once they are all done running we will return, unless we are killed in
2993 * which case we return EINTR. This helps in user operations like fallocate etc
2994 * that might get blocked on the iputs.
2995 */
2996int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2997{
2998 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2999 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3000 if (ret)
3001 return -EINTR;
3002 return 0;
3003}
3004
Yan, Zhengd68fc572010-05-16 10:49:58 -04003005/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003006 * This creates an orphan entry for the given inode in case something goes wrong
3007 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003008 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003009int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003010 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003011{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003012 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003013
Omar Sandoval27919062018-05-11 13:13:37 -07003014 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3015 if (ret && ret != -EEXIST) {
3016 btrfs_abort_transaction(trans, ret);
3017 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003018 }
3019
Yan, Zhengd68fc572010-05-16 10:49:58 -04003020 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003021}
3022
3023/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003024 * We have done the delete so we can go ahead and remove the orphan item for
3025 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003026 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003027static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003028 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003029{
Omar Sandoval27919062018-05-11 13:13:37 -07003030 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003031}
3032
3033/*
3034 * this cleans up any orphans that may be left on the list from the last use
3035 * of this root.
3036 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003037int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003038{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003039 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003040 struct btrfs_path *path;
3041 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003042 struct btrfs_key key, found_key;
3043 struct btrfs_trans_handle *trans;
3044 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003045 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003046 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003047
Yan, Zhengd68fc572010-05-16 10:49:58 -04003048 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003049 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003050
3051 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003052 if (!path) {
3053 ret = -ENOMEM;
3054 goto out;
3055 }
David Sterbae4058b52015-11-27 16:31:35 +01003056 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003057
3058 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003059 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003060 key.offset = (u64)-1;
3061
Josef Bacik7b128762008-07-24 12:17:14 -04003062 while (1) {
3063 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003064 if (ret < 0)
3065 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003066
3067 /*
3068 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003069 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003070 * find the key and see if we have stuff that matches
3071 */
3072 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003073 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003074 if (path->slots[0] == 0)
3075 break;
3076 path->slots[0]--;
3077 }
3078
3079 /* pull out the item */
3080 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003081 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3082
3083 /* make sure the item matches what we want */
3084 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3085 break;
David Sterba962a2982014-06-04 18:41:45 +02003086 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003087 break;
3088
3089 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003090 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003091
3092 /*
3093 * this is where we are basically btrfs_lookup, without the
3094 * crossing root thing. we store the inode number in the
3095 * offset of the orphan item.
3096 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003097
3098 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003099 btrfs_err(fs_info,
3100 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003101 ret = -EINVAL;
3102 goto out;
3103 }
3104
3105 last_objectid = found_key.offset;
3106
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003107 found_key.objectid = found_key.offset;
3108 found_key.type = BTRFS_INODE_ITEM_KEY;
3109 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003110 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303111 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003112 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003113 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003114
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003115 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003116 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003117 int is_dead_root = 0;
3118
3119 /*
3120 * this is an orphan in the tree root. Currently these
3121 * could come from 2 sources:
3122 * a) a snapshot deletion in progress
3123 * b) a free space cache inode
3124 * We need to distinguish those two, as the snapshot
3125 * orphan must not get deleted.
3126 * find_dead_roots already ran before us, so if this
3127 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003128 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003129 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003130
3131 spin_lock(&fs_info->fs_roots_radix_lock);
3132 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3133 (unsigned long)found_key.objectid);
3134 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3135 is_dead_root = 1;
3136 spin_unlock(&fs_info->fs_roots_radix_lock);
3137
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003138 if (is_dead_root) {
3139 /* prevent this orphan from being found again */
3140 key.offset = found_key.objectid - 1;
3141 continue;
3142 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003143
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003144 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003145
Josef Bacika8c9e572011-09-21 16:55:59 -04003146 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003147 * If we have an inode with links, there are a couple of
3148 * possibilities. Old kernels (before v3.12) used to create an
3149 * orphan item for truncate indicating that there were possibly
3150 * extent items past i_size that needed to be deleted. In v3.12,
3151 * truncate was changed to update i_size in sync with the extent
3152 * items, but the (useless) orphan item was still created. Since
3153 * v4.18, we don't create the orphan item for truncate at all.
3154 *
3155 * So, this item could mean that we need to do a truncate, but
3156 * only if this filesystem was last used on a pre-v3.12 kernel
3157 * and was not cleanly unmounted. The odds of that are quite
3158 * slim, and it's a pain to do the truncate now, so just delete
3159 * the orphan item.
3160 *
3161 * It's also possible that this orphan item was supposed to be
3162 * deleted but wasn't. The inode number may have been reused,
3163 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003164 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003165 if (ret == -ENOENT || inode->i_nlink) {
3166 if (!ret)
3167 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003168 trans = btrfs_start_transaction(root, 1);
3169 if (IS_ERR(trans)) {
3170 ret = PTR_ERR(trans);
3171 goto out;
3172 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003173 btrfs_debug(fs_info, "auto deleting %Lu",
3174 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003175 ret = btrfs_del_orphan_item(trans, root,
3176 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003177 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003178 if (ret)
3179 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003180 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003181 }
Josef Bacik7b128762008-07-24 12:17:14 -04003182
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003183 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003184
3185 /* this will do delete_inode and everything for us */
3186 iput(inode);
3187 }
Miao Xie3254c872011-11-10 20:45:05 -05003188 /* release the path since we're done with it */
3189 btrfs_release_path(path);
3190
Yan, Zhengd68fc572010-05-16 10:49:58 -04003191 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3192
Omar Sandovala575cee2018-05-11 13:13:38 -07003193 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003194 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003195 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003196 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003197 }
Josef Bacik7b128762008-07-24 12:17:14 -04003198
3199 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003200 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003201
3202out:
3203 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003204 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003205 btrfs_free_path(path);
3206 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003207}
3208
Chris Masond352ac62008-09-29 15:18:18 -04003209/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003210 * very simple check to peek ahead in the leaf looking for xattrs. If we
3211 * don't find any xattrs, we know there can't be any acls.
3212 *
3213 * slot is the slot the inode is in, objectid is the objectid of the inode
3214 */
3215static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003216 int slot, u64 objectid,
3217 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003218{
3219 u32 nritems = btrfs_header_nritems(leaf);
3220 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003221 static u64 xattr_access = 0;
3222 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003223 int scanned = 0;
3224
Josef Bacikf23b5a52013-06-19 10:16:26 -04003225 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003226 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3227 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3228 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3229 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003230 }
3231
Chris Mason46a53cc2009-04-27 11:47:50 -04003232 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003233 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003234 while (slot < nritems) {
3235 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3236
3237 /* we found a different objectid, there must not be acls */
3238 if (found_key.objectid != objectid)
3239 return 0;
3240
3241 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003242 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003243 if (*first_xattr_slot == -1)
3244 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003245 if (found_key.offset == xattr_access ||
3246 found_key.offset == xattr_default)
3247 return 1;
3248 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003249
3250 /*
3251 * we found a key greater than an xattr key, there can't
3252 * be any acls later on
3253 */
3254 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3255 return 0;
3256
3257 slot++;
3258 scanned++;
3259
3260 /*
3261 * it goes inode, inode backrefs, xattrs, extents,
3262 * so if there are a ton of hard links to an inode there can
3263 * be a lot of backrefs. Don't waste time searching too hard,
3264 * this is just an optimization
3265 */
3266 if (scanned >= 8)
3267 break;
3268 }
3269 /* we hit the end of the leaf before we found an xattr or
3270 * something larger than an xattr. We have to assume the inode
3271 * has acls
3272 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003273 if (*first_xattr_slot == -1)
3274 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003275 return 1;
3276}
3277
3278/*
Chris Masond352ac62008-09-29 15:18:18 -04003279 * read an inode from the btree into the in-memory inode
3280 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003281static int btrfs_read_locked_inode(struct inode *inode,
3282 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003283{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003284 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003285 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003286 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003287 struct btrfs_inode_item *inode_item;
3288 struct btrfs_root *root = BTRFS_I(inode)->root;
3289 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003290 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003291 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003292 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003293 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003294 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003295 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003296
3297 ret = btrfs_fill_inode(inode, &rdev);
3298 if (!ret)
3299 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003300
Filipe Manana4222ea72018-10-24 10:13:03 +01003301 if (!path) {
3302 path = btrfs_alloc_path();
3303 if (!path)
3304 return -ENOMEM;
3305 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003306
Chris Mason39279cc2007-06-12 06:35:45 -04003307 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003308
Chris Mason39279cc2007-06-12 06:35:45 -04003309 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003310 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003311 if (path != in_path)
3312 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003313 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003314 }
Chris Mason39279cc2007-06-12 06:35:45 -04003315
Chris Mason5f39d392007-10-15 16:14:19 -04003316 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003317
3318 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003319 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003320
Chris Mason5f39d392007-10-15 16:14:19 -04003321 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3322 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003323 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003324 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003325 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3326 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003327 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003328 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3329 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003330
David Sterbaa937b972014-12-12 17:39:12 +01003331 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3332 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003333
David Sterbaa937b972014-12-12 17:39:12 +01003334 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3335 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003336
David Sterbaa937b972014-12-12 17:39:12 +01003337 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3338 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003339
chandan r9cc97d62012-07-04 12:48:07 +05303340 BTRFS_I(inode)->i_otime.tv_sec =
3341 btrfs_timespec_sec(leaf, &inode_item->otime);
3342 BTRFS_I(inode)->i_otime.tv_nsec =
3343 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003344
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003345 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003346 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003347 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3348
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003349 inode_set_iversion_queried(inode,
3350 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003351 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003352 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003353 rdev = btrfs_inode_rdev(leaf, inode_item);
3354
Josef Bacikaec74772008-07-24 12:12:38 -04003355 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003356 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003357
3358cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003359 /*
3360 * If we were modified in the current generation and evicted from memory
3361 * and then re-read we need to do a full sync since we don't have any
3362 * idea about which extents were modified before we were evicted from
3363 * cache.
3364 *
3365 * This is required for both inode re-read from disk and delayed inode
3366 * in delayed_nodes_tree.
3367 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003368 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003369 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3370 &BTRFS_I(inode)->runtime_flags);
3371
Filipe Mananabde6c242015-07-24 00:00:19 +01003372 /*
3373 * We don't persist the id of the transaction where an unlink operation
3374 * against the inode was last made. So here we assume the inode might
3375 * have been evicted, and therefore the exact value of last_unlink_trans
3376 * lost, and set it to last_trans to avoid metadata inconsistencies
3377 * between the inode and its parent if the inode is fsync'ed and the log
3378 * replayed. For example, in the scenario:
3379 *
3380 * touch mydir/foo
3381 * ln mydir/foo mydir/bar
3382 * sync
3383 * unlink mydir/bar
3384 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3385 * xfs_io -c fsync mydir/foo
3386 * <power failure>
3387 * mount fs, triggers fsync log replay
3388 *
3389 * We must make sure that when we fsync our inode foo we also log its
3390 * parent inode, otherwise after log replay the parent still has the
3391 * dentry with the "bar" name but our inode foo has a link count of 1
3392 * and doesn't have an inode ref with the name "bar" anymore.
3393 *
3394 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003395 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003396 * transaction commits on fsync if our inode is a directory, or if our
3397 * inode is not a directory, logging its parent unnecessarily.
3398 */
3399 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3400
Filipe Manana3ebac172020-07-15 12:30:43 +01003401 /*
3402 * Same logic as for last_unlink_trans. We don't persist the generation
3403 * of the last transaction where this inode was used for a reflink
3404 * operation, so after eviction and reloading the inode we must be
3405 * pessimistic and assume the last transaction that modified the inode.
3406 */
3407 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3408
Miao Xie67de1172013-12-26 13:07:06 +08003409 path->slots[0]++;
3410 if (inode->i_nlink != 1 ||
3411 path->slots[0] >= btrfs_header_nritems(leaf))
3412 goto cache_acl;
3413
3414 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003415 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003416 goto cache_acl;
3417
3418 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3419 if (location.type == BTRFS_INODE_REF_KEY) {
3420 struct btrfs_inode_ref *ref;
3421
3422 ref = (struct btrfs_inode_ref *)ptr;
3423 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3424 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3425 struct btrfs_inode_extref *extref;
3426
3427 extref = (struct btrfs_inode_extref *)ptr;
3428 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3429 extref);
3430 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003431cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003432 /*
3433 * try to precache a NULL acl entry for files that don't have
3434 * any xattrs or acls
3435 */
Li Zefan33345d012011-04-20 10:31:50 +08003436 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003437 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003438 if (first_xattr_slot != -1) {
3439 path->slots[0] = first_xattr_slot;
3440 ret = btrfs_load_inode_props(inode, path);
3441 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003442 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003443 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003444 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003445 root->root_key.objectid, ret);
3446 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003447 if (path != in_path)
3448 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003449
Al Viro72c04902009-06-24 16:58:48 -04003450 if (!maybe_acls)
3451 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003452
Chris Mason39279cc2007-06-12 06:35:45 -04003453 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003454 case S_IFREG:
3455 inode->i_mapping->a_ops = &btrfs_aops;
3456 inode->i_fop = &btrfs_file_operations;
3457 inode->i_op = &btrfs_file_inode_operations;
3458 break;
3459 case S_IFDIR:
3460 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003461 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003462 break;
3463 case S_IFLNK:
3464 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003465 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003466 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003467 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003468 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003469 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003470 init_special_inode(inode, inode->i_mode, rdev);
3471 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003472 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003473
David Sterba7b6a2212018-03-26 18:40:21 +02003474 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003475 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003476}
3477
Chris Masond352ac62008-09-29 15:18:18 -04003478/*
3479 * given a leaf and an inode, copy the inode fields into the leaf
3480 */
Chris Masone02119d2008-09-05 16:13:11 -04003481static void fill_inode_item(struct btrfs_trans_handle *trans,
3482 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003483 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003484 struct inode *inode)
3485{
Liu Bo51fab692012-12-27 09:01:21 +00003486 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003487
David Sterbac82f8232019-08-09 17:48:21 +02003488 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003489
David Sterbacc4c13d2020-04-29 02:15:56 +02003490 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3491 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3492 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3493 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3494 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003495
David Sterbacc4c13d2020-04-29 02:15:56 +02003496 btrfs_set_token_timespec_sec(&token, &item->atime,
3497 inode->i_atime.tv_sec);
3498 btrfs_set_token_timespec_nsec(&token, &item->atime,
3499 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003500
David Sterbacc4c13d2020-04-29 02:15:56 +02003501 btrfs_set_token_timespec_sec(&token, &item->mtime,
3502 inode->i_mtime.tv_sec);
3503 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3504 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003505
David Sterbacc4c13d2020-04-29 02:15:56 +02003506 btrfs_set_token_timespec_sec(&token, &item->ctime,
3507 inode->i_ctime.tv_sec);
3508 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3509 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003510
David Sterbacc4c13d2020-04-29 02:15:56 +02003511 btrfs_set_token_timespec_sec(&token, &item->otime,
3512 BTRFS_I(inode)->i_otime.tv_sec);
3513 btrfs_set_token_timespec_nsec(&token, &item->otime,
3514 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303515
David Sterbacc4c13d2020-04-29 02:15:56 +02003516 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3517 btrfs_set_token_inode_generation(&token, item,
3518 BTRFS_I(inode)->generation);
3519 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3520 btrfs_set_token_inode_transid(&token, item, trans->transid);
3521 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3522 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3523 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003524}
3525
Chris Masond352ac62008-09-29 15:18:18 -04003526/*
3527 * copy everything in the in-memory inode into the btree.
3528 */
Chris Mason21151332011-11-10 20:39:08 -05003529static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003530 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003531{
3532 struct btrfs_inode_item *inode_item;
3533 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003534 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003535 int ret;
3536
3537 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003538 if (!path)
3539 return -ENOMEM;
3540
Chris Masonb9473432009-03-13 11:00:37 -04003541 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003542 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3543 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003544 if (ret) {
3545 if (ret > 0)
3546 ret = -ENOENT;
3547 goto failed;
3548 }
3549
Chris Mason5f39d392007-10-15 16:14:19 -04003550 leaf = path->nodes[0];
3551 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003552 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003553
Chris Masone02119d2008-09-05 16:13:11 -04003554 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003555 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovd9094412020-06-05 10:41:13 +03003556 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04003557 ret = 0;
3558failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003559 btrfs_free_path(path);
3560 return ret;
3561}
3562
Chris Masond352ac62008-09-29 15:18:18 -04003563/*
Chris Mason21151332011-11-10 20:39:08 -05003564 * copy everything in the in-memory inode into the btree.
3565 */
3566noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3567 struct btrfs_root *root, struct inode *inode)
3568{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003569 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003570 int ret;
3571
3572 /*
3573 * If the inode is a free space inode, we can deadlock during commit
3574 * if we put it into the delayed code.
3575 *
3576 * The data relocation inode should also be directly updated
3577 * without delay
3578 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003579 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003580 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003581 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003582 btrfs_update_root_times(trans, root);
3583
Chris Mason21151332011-11-10 20:39:08 -05003584 ret = btrfs_delayed_update_inode(trans, root, inode);
3585 if (!ret)
Nikolay Borisovd9094412020-06-05 10:41:13 +03003586 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason21151332011-11-10 20:39:08 -05003587 return ret;
3588 }
3589
3590 return btrfs_update_inode_item(trans, root, inode);
3591}
3592
Josef Bacikbe6aef62012-10-22 15:43:12 -04003593noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3594 struct btrfs_root *root,
3595 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003596{
3597 int ret;
3598
3599 ret = btrfs_update_inode(trans, root, inode);
3600 if (ret == -ENOSPC)
3601 return btrfs_update_inode_item(trans, root, inode);
3602 return ret;
3603}
3604
3605/*
Chris Masond352ac62008-09-29 15:18:18 -04003606 * unlink helper that gets used here in inode.c and in the tree logging
3607 * recovery code. It remove a link in a directory with a given name, and
3608 * also drops the back refs in the inode to the directory
3609 */
Al Viro92986792011-03-04 17:14:37 +00003610static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3611 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003612 struct btrfs_inode *dir,
3613 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003614 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003615{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003616 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003617 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003618 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003619 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003620 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003621 u64 ino = btrfs_ino(inode);
3622 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003623
3624 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003625 if (!path) {
3626 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003627 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003628 }
3629
Chris Masonb9473432009-03-13 11:00:37 -04003630 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003631 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003632 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003633 if (IS_ERR_OR_NULL(di)) {
3634 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003635 goto err;
3636 }
Chris Mason39279cc2007-06-12 06:35:45 -04003637 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003638 if (ret)
3639 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003640 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003641
Miao Xie67de1172013-12-26 13:07:06 +08003642 /*
3643 * If we don't have dir index, we have to get it by looking up
3644 * the inode ref, since we get the inode ref, remove it directly,
3645 * it is unnecessary to do delayed deletion.
3646 *
3647 * But if we have dir index, needn't search inode ref to get it.
3648 * Since the inode ref is close to the inode item, it is better
3649 * that we delay to delete it, and just do this deletion when
3650 * we update the inode item.
3651 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003652 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003653 ret = btrfs_delayed_delete_inode_ref(inode);
3654 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003655 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003656 goto skip_backref;
3657 }
3658 }
3659
Li Zefan33345d012011-04-20 10:31:50 +08003660 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3661 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003662 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003663 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003664 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003665 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003666 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003667 goto err;
3668 }
Miao Xie67de1172013-12-26 13:07:06 +08003669skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003670 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003671 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003672 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003673 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003674 }
Chris Mason39279cc2007-06-12 06:35:45 -04003675
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003676 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3677 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003678 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003679 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003680 goto err;
3681 }
Chris Masone02119d2008-09-05 16:13:11 -04003682
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003683 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3684 index);
Chris Mason6418c962010-10-30 07:34:24 -04003685 if (ret == -ENOENT)
3686 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003687 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003688 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003689
3690 /*
3691 * If we have a pending delayed iput we could end up with the final iput
3692 * being run in btrfs-cleaner context. If we have enough of these built
3693 * up we can end up burning a lot of time in btrfs-cleaner without any
3694 * way to throttle the unlinks. Since we're currently holding a ref on
3695 * the inode we can run the delayed iput here without any issues as the
3696 * final iput won't be done until after we drop the ref we're currently
3697 * holding.
3698 */
3699 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003700err:
3701 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003702 if (ret)
3703 goto out;
3704
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003705 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003706 inode_inc_iversion(&inode->vfs_inode);
3707 inode_inc_iversion(&dir->vfs_inode);
3708 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3709 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3710 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003711out:
Chris Mason39279cc2007-06-12 06:35:45 -04003712 return ret;
3713}
3714
Al Viro92986792011-03-04 17:14:37 +00003715int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3716 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003717 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003718 const char *name, int name_len)
3719{
3720 int ret;
3721 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3722 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003723 drop_nlink(&inode->vfs_inode);
3724 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003725 }
3726 return ret;
3727}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003728
3729/*
3730 * helper to start transaction for unlink and rmdir.
3731 *
Josef Bacikd52be812013-05-29 14:54:47 -04003732 * unlink and rmdir are special in btrfs, they do not always free space, so
3733 * if we cannot make our reservations the normal way try and see if there is
3734 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3735 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003736 */
Josef Bacikd52be812013-05-29 14:54:47 -04003737static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003738{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003739 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003740
Josef Bacike70bea52011-10-11 14:18:24 -04003741 /*
3742 * 1 for the possible orphan item
3743 * 1 for the dir item
3744 * 1 for the dir index
3745 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003746 * 1 for the inode
3747 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003748 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003749}
3750
Chris Mason39279cc2007-06-12 06:35:45 -04003751static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3752{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003753 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003755 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003756 int ret;
3757
Josef Bacikd52be812013-05-29 14:54:47 -04003758 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003759 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003760 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003761
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003762 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3763 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003764
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003765 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3766 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3767 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003768 if (ret)
3769 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003770
Yan, Zhenga22285a2010-05-16 10:48:46 -04003771 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003772 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003773 if (ret)
3774 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003775 }
Josef Bacik7b128762008-07-24 12:17:14 -04003776
Tsutomu Itohb5324022011-07-19 07:27:20 +00003777out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003778 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003779 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003780 return ret;
3781}
3782
Misono Tomohirof60a2362018-04-18 11:34:52 +09003783static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003784 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003785{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003786 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003787 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003788 struct btrfs_path *path;
3789 struct extent_buffer *leaf;
3790 struct btrfs_dir_item *di;
3791 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003792 const char *name = dentry->d_name.name;
3793 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003794 u64 index;
3795 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003796 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003797 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003798
Josef Bacik045d3962019-12-18 17:20:27 -05003799 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3800 objectid = inode->root->root_key.objectid;
3801 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3802 objectid = inode->location.objectid;
3803 } else {
3804 WARN_ON(1);
3805 return -EINVAL;
3806 }
3807
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003808 path = btrfs_alloc_path();
3809 if (!path)
3810 return -ENOMEM;
3811
Li Zefan33345d012011-04-20 10:31:50 +08003812 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003813 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003814 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003815 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003816 goto out;
3817 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003818
3819 leaf = path->nodes[0];
3820 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3821 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3822 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003823 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003824 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003825 goto out;
3826 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003827 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003828
Josef Bacikd49d3282019-12-18 17:20:28 -05003829 /*
3830 * This is a placeholder inode for a subvolume we didn't have a
3831 * reference to at the time of the snapshot creation. In the meantime
3832 * we could have renamed the real subvol link into our snapshot, so
3833 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3834 * Instead simply lookup the dir_index_item for this entry so we can
3835 * remove it. Otherwise we know we have a ref to the root and we can
3836 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3837 */
3838 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003839 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003840 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003841 if (IS_ERR_OR_NULL(di)) {
3842 if (!di)
3843 ret = -ENOENT;
3844 else
3845 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003846 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003847 goto out;
3848 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003849
3850 leaf = path->nodes[0];
3851 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003852 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003853 btrfs_release_path(path);
3854 } else {
3855 ret = btrfs_del_root_ref(trans, objectid,
3856 root->root_key.objectid, dir_ino,
3857 &index, name, name_len);
3858 if (ret) {
3859 btrfs_abort_transaction(trans, ret);
3860 goto out;
3861 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003862 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003863
Lu Fengqi9add2942018-08-01 11:32:26 +08003864 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003865 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003866 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003867 goto out;
3868 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003869
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003870 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003871 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003872 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003873 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003874 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003875 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003876out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003877 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003878 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003879}
3880
Misono Tomohiroec42f162018-04-18 11:34:13 +09003881/*
3882 * Helper to check if the subvolume references other subvolumes or if it's
3883 * default.
3884 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003885static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003886{
3887 struct btrfs_fs_info *fs_info = root->fs_info;
3888 struct btrfs_path *path;
3889 struct btrfs_dir_item *di;
3890 struct btrfs_key key;
3891 u64 dir_id;
3892 int ret;
3893
3894 path = btrfs_alloc_path();
3895 if (!path)
3896 return -ENOMEM;
3897
3898 /* Make sure this root isn't set as the default subvol */
3899 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3900 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3901 dir_id, "default", 7, 0);
3902 if (di && !IS_ERR(di)) {
3903 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3904 if (key.objectid == root->root_key.objectid) {
3905 ret = -EPERM;
3906 btrfs_err(fs_info,
3907 "deleting default subvolume %llu is not allowed",
3908 key.objectid);
3909 goto out;
3910 }
3911 btrfs_release_path(path);
3912 }
3913
3914 key.objectid = root->root_key.objectid;
3915 key.type = BTRFS_ROOT_REF_KEY;
3916 key.offset = (u64)-1;
3917
3918 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3919 if (ret < 0)
3920 goto out;
3921 BUG_ON(ret == 0);
3922
3923 ret = 0;
3924 if (path->slots[0] > 0) {
3925 path->slots[0]--;
3926 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3927 if (key.objectid == root->root_key.objectid &&
3928 key.type == BTRFS_ROOT_REF_KEY)
3929 ret = -ENOTEMPTY;
3930 }
3931out:
3932 btrfs_free_path(path);
3933 return ret;
3934}
3935
Nikolay Borisov20a68002018-04-27 14:36:24 +03003936/* Delete all dentries for inodes belonging to the root */
3937static void btrfs_prune_dentries(struct btrfs_root *root)
3938{
3939 struct btrfs_fs_info *fs_info = root->fs_info;
3940 struct rb_node *node;
3941 struct rb_node *prev;
3942 struct btrfs_inode *entry;
3943 struct inode *inode;
3944 u64 objectid = 0;
3945
3946 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3947 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3948
3949 spin_lock(&root->inode_lock);
3950again:
3951 node = root->inode_tree.rb_node;
3952 prev = NULL;
3953 while (node) {
3954 prev = node;
3955 entry = rb_entry(node, struct btrfs_inode, rb_node);
3956
David Sterba37508512018-06-29 10:56:40 +02003957 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003958 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003959 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003960 node = node->rb_right;
3961 else
3962 break;
3963 }
3964 if (!node) {
3965 while (prev) {
3966 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003967 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003968 node = prev;
3969 break;
3970 }
3971 prev = rb_next(prev);
3972 }
3973 }
3974 while (node) {
3975 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003976 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003977 inode = igrab(&entry->vfs_inode);
3978 if (inode) {
3979 spin_unlock(&root->inode_lock);
3980 if (atomic_read(&inode->i_count) > 1)
3981 d_prune_aliases(inode);
3982 /*
3983 * btrfs_drop_inode will have it removed from the inode
3984 * cache when its usage count hits zero.
3985 */
3986 iput(inode);
3987 cond_resched();
3988 spin_lock(&root->inode_lock);
3989 goto again;
3990 }
3991
3992 if (cond_resched_lock(&root->inode_lock))
3993 goto again;
3994
3995 node = rb_next(node);
3996 }
3997 spin_unlock(&root->inode_lock);
3998}
3999
Misono Tomohirof60a2362018-04-18 11:34:52 +09004000int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4001{
4002 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4003 struct btrfs_root *root = BTRFS_I(dir)->root;
4004 struct inode *inode = d_inode(dentry);
4005 struct btrfs_root *dest = BTRFS_I(inode)->root;
4006 struct btrfs_trans_handle *trans;
4007 struct btrfs_block_rsv block_rsv;
4008 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004009 int ret;
4010 int err;
4011
4012 /*
4013 * Don't allow to delete a subvolume with send in progress. This is
4014 * inside the inode lock so the error handling that has to drop the bit
4015 * again is not run concurrently.
4016 */
4017 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004018 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004019 spin_unlock(&dest->root_item_lock);
4020 btrfs_warn(fs_info,
4021 "attempt to delete subvolume %llu during send",
4022 dest->root_key.objectid);
4023 return -EPERM;
4024 }
Kaiwen Hua044bca2022-03-23 15:10:32 +08004025 if (atomic_read(&dest->nr_swapfiles)) {
4026 spin_unlock(&dest->root_item_lock);
4027 btrfs_warn(fs_info,
4028 "attempt to delete subvolume %llu with active swapfile",
4029 root->root_key.objectid);
4030 return -EPERM;
4031 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004032 root_flags = btrfs_root_flags(&dest->root_item);
4033 btrfs_set_root_flags(&dest->root_item,
4034 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4035 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004036
4037 down_write(&fs_info->subvol_sem);
4038
4039 err = may_destroy_subvol(dest);
4040 if (err)
4041 goto out_up_write;
4042
4043 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4044 /*
4045 * One for dir inode,
4046 * two for dir entries,
4047 * two for root ref/backref.
4048 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004049 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004050 if (err)
4051 goto out_up_write;
4052
4053 trans = btrfs_start_transaction(root, 0);
4054 if (IS_ERR(trans)) {
4055 err = PTR_ERR(trans);
4056 goto out_release;
4057 }
4058 trans->block_rsv = &block_rsv;
4059 trans->bytes_reserved = block_rsv.size;
4060
4061 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4062
Josef Bacik045d3962019-12-18 17:20:27 -05004063 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004064 if (ret) {
4065 err = ret;
4066 btrfs_abort_transaction(trans, ret);
4067 goto out_end_trans;
4068 }
4069
4070 btrfs_record_root_in_trans(trans, dest);
4071
4072 memset(&dest->root_item.drop_progress, 0,
4073 sizeof(dest->root_item.drop_progress));
4074 dest->root_item.drop_level = 0;
4075 btrfs_set_root_refs(&dest->root_item, 0);
4076
4077 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4078 ret = btrfs_insert_orphan_item(trans,
4079 fs_info->tree_root,
4080 dest->root_key.objectid);
4081 if (ret) {
4082 btrfs_abort_transaction(trans, ret);
4083 err = ret;
4084 goto out_end_trans;
4085 }
4086 }
4087
Lu Fengqid1957792018-05-29 15:01:54 +08004088 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004089 BTRFS_UUID_KEY_SUBVOL,
4090 dest->root_key.objectid);
4091 if (ret && ret != -ENOENT) {
4092 btrfs_abort_transaction(trans, ret);
4093 err = ret;
4094 goto out_end_trans;
4095 }
4096 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004097 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004098 dest->root_item.received_uuid,
4099 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4100 dest->root_key.objectid);
4101 if (ret && ret != -ENOENT) {
4102 btrfs_abort_transaction(trans, ret);
4103 err = ret;
4104 goto out_end_trans;
4105 }
4106 }
4107
Qu Wenruo082b6c92020-06-16 10:17:37 +08004108 free_anon_bdev(dest->anon_dev);
4109 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004110out_end_trans:
4111 trans->block_rsv = NULL;
4112 trans->bytes_reserved = 0;
4113 ret = btrfs_end_transaction(trans);
4114 if (ret && !err)
4115 err = ret;
4116 inode->i_flags |= S_DEAD;
4117out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004118 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004119out_up_write:
4120 up_write(&fs_info->subvol_sem);
4121 if (err) {
4122 spin_lock(&dest->root_item_lock);
4123 root_flags = btrfs_root_flags(&dest->root_item);
4124 btrfs_set_root_flags(&dest->root_item,
4125 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4126 spin_unlock(&dest->root_item_lock);
4127 } else {
4128 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004129 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004130 ASSERT(dest->send_in_progress == 0);
4131
4132 /* the last ref */
4133 if (dest->ino_cache_inode) {
4134 iput(dest->ino_cache_inode);
4135 dest->ino_cache_inode = NULL;
4136 }
4137 }
4138
4139 return err;
4140}
4141
Chris Mason39279cc2007-06-12 06:35:45 -04004142static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4143{
David Howells2b0143b2015-03-17 22:25:59 +00004144 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004145 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004146 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004147 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004148 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004149
David Sterbab3ae2442012-09-13 16:04:34 -06004150 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004151 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004152 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004153 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004154
Josef Bacikd52be812013-05-29 14:54:47 -04004155 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004156 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004157 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004158
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004159 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004160 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004161 goto out;
4162 }
4163
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004164 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004165 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004166 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004167
Filipe Manana44f714d2016-06-06 16:11:13 +01004168 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4169
Chris Mason39279cc2007-06-12 06:35:45 -04004170 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004171 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4172 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4173 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004174 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004175 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004176 /*
4177 * Propagate the last_unlink_trans value of the deleted dir to
4178 * its parent directory. This is to prevent an unrecoverable
4179 * log tree in the case we do something like this:
4180 * 1) create dir foo
4181 * 2) create snapshot under dir foo
4182 * 3) delete the snapshot
4183 * 4) rmdir foo
4184 * 5) mkdir foo
4185 * 6) fsync foo or some file inside foo
4186 */
4187 if (last_unlink_trans >= trans->transid)
4188 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4189 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004190out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004191 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004192 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004193
Chris Mason39279cc2007-06-12 06:35:45 -04004194 return err;
4195}
4196
Josef Bacikddfae632017-10-19 14:16:02 -04004197/*
4198 * Return this if we need to call truncate_block for the last bit of the
4199 * truncate.
4200 */
4201#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004202
Chris Mason323ac952008-10-01 19:05:46 -04004203/*
Chris Mason39279cc2007-06-12 06:35:45 -04004204 * this can truncate away extent items, csum items and directory items.
4205 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004206 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004207 *
4208 * csum items that cross the new i_size are truncated to the new size
4209 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004210 *
4211 * min_type is the minimum key type to truncate down to. If set to 0, this
4212 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004213 */
Yan, Zheng80825102009-11-12 09:35:36 +00004214int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4215 struct btrfs_root *root,
4216 struct inode *inode,
4217 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004218{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004219 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004220 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004221 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004222 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004223 struct btrfs_key key;
4224 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004225 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004226 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004227 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004228 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004229 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004230 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004231 int found_extent;
4232 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004233 int pending_del_nr = 0;
4234 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004235 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004236 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004237 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004238 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004239 bool be_nice = false;
4240 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004241 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4242 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004243
4244 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004245
Chris Mason28ed1342014-12-17 09:41:04 -08004246 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004247 * For non-free space inodes and non-shareable roots, we want to back
4248 * off from time to time. This means all inodes in subvolume roots,
4249 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004250 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004251 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004252 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004253 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004254
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004255 path = btrfs_alloc_path();
4256 if (!path)
4257 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004258 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004259
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004260 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004261 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4262 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004263
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004264 /*
4265 * We want to drop from the next block forward in case this
4266 * new size is not block aligned since we will be keeping the
4267 * last block of the extent just the way it is.
4268 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004269 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004270 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004271 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004272 }
Yan, Zheng80825102009-11-12 09:35:36 +00004273
Miao Xie16cdcec2011-04-22 18:12:22 +08004274 /*
4275 * This function is also used to drop the items in the log tree before
4276 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004277 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004278 * items.
4279 */
4280 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004281 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004282
Li Zefan33345d012011-04-20 10:31:50 +08004283 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004284 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004285 key.type = (u8)-1;
4286
Chris Mason85e21ba2008-01-29 15:11:36 -05004287search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004288 /*
4289 * with a 16K leaf size and 128MB extents, you can actually queue
4290 * up a huge file in a single leaf. Most of the time that
4291 * bytes_deleted is > 0, it will be huge by the time we get here
4292 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004293 if (be_nice && bytes_deleted > SZ_32M &&
4294 btrfs_should_end_transaction(trans)) {
4295 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004296 goto out;
4297 }
Chris Masond3977122009-01-05 21:25:51 -05004298
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004299 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4300 if (ret < 0)
4301 goto out;
4302
Chris Mason85e21ba2008-01-29 15:11:36 -05004303 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004304 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004305 /* there are no items in the tree for us to truncate, we're
4306 * done
4307 */
Yan, Zheng80825102009-11-12 09:35:36 +00004308 if (path->slots[0] == 0)
4309 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004310 path->slots[0]--;
4311 }
4312
Chris Masond3977122009-01-05 21:25:51 -05004313 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004314 u64 clear_start = 0, clear_len = 0;
4315
Chris Mason39279cc2007-06-12 06:35:45 -04004316 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004317 leaf = path->nodes[0];
4318 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004319 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004320
Li Zefan33345d012011-04-20 10:31:50 +08004321 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004322 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004323
Chris Mason85e21ba2008-01-29 15:11:36 -05004324 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004325 break;
4326
Chris Mason5f39d392007-10-15 16:14:19 -04004327 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004328 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004329 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004330 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004331 extent_type = btrfs_file_extent_type(leaf, fi);
4332 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004333 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004334 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004335
4336 trace_btrfs_truncate_show_fi_regular(
4337 BTRFS_I(inode), leaf, fi,
4338 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004339 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004340 item_end += btrfs_file_extent_ram_bytes(leaf,
4341 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004342
4343 trace_btrfs_truncate_show_fi_inline(
4344 BTRFS_I(inode), leaf, fi, path->slots[0],
4345 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004346 }
Yan008630c2007-11-07 13:31:09 -05004347 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004348 }
Yan, Zheng80825102009-11-12 09:35:36 +00004349 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004350 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004351 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004352 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004353 break;
4354 if (found_key.offset >= new_size)
4355 del_item = 1;
4356 else
4357 del_item = 0;
4358 }
Chris Mason39279cc2007-06-12 06:35:45 -04004359 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004360 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004361 if (found_type != BTRFS_EXTENT_DATA_KEY)
4362 goto delete;
4363
4364 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004365 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004366
4367 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004368 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004369 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004370 u64 orig_num_bytes =
4371 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004372 extent_num_bytes = ALIGN(new_size -
4373 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004374 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004375 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004376 btrfs_set_file_extent_num_bytes(leaf, fi,
4377 extent_num_bytes);
4378 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004379 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004380 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004381 &root->state) &&
4382 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004383 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004384 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004385 } else {
Chris Masondb945352007-10-15 16:15:53 -04004386 extent_num_bytes =
4387 btrfs_file_extent_disk_num_bytes(leaf,
4388 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004389 extent_offset = found_key.offset -
4390 btrfs_file_extent_offset(leaf, fi);
4391
Chris Mason39279cc2007-06-12 06:35:45 -04004392 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004393 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004394 if (extent_start != 0) {
4395 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004396 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004397 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004398 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004399 }
4400 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004401 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004402 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004403 /*
4404 * we can't truncate inline items that have had
4405 * special encodings
4406 */
4407 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004408 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004409 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4410 btrfs_file_extent_compression(leaf, fi) == 0) {
4411 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004412
Josef Bacikddfae632017-10-19 14:16:02 -04004413 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4414 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004415 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004416 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004417 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004418 * We have to bail so the last_size is set to
4419 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004420 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004421 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004422 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004423 } else {
4424 /*
4425 * Inline extents are special, we just treat
4426 * them as a full sector worth in the file
4427 * extent tree just for simplicity sake.
4428 */
4429 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004430 }
Josef Bacikddfae632017-10-19 14:16:02 -04004431
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004432 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004433 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004434 }
Chris Mason179e29e2007-11-01 11:28:41 -04004435delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004436 /*
4437 * We use btrfs_truncate_inode_items() to clean up log trees for
4438 * multiple fsyncs, and in this case we don't want to clear the
4439 * file extent range because it's just the log.
4440 */
4441 if (root == BTRFS_I(inode)->root) {
4442 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4443 clear_start, clear_len);
4444 if (ret) {
4445 btrfs_abort_transaction(trans, ret);
4446 break;
4447 }
4448 }
4449
Josef Bacikddfae632017-10-19 14:16:02 -04004450 if (del_item)
4451 last_size = found_key.offset;
4452 else
4453 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004454 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004455 if (!pending_del_nr) {
4456 /* no pending yet, add ourselves */
4457 pending_del_slot = path->slots[0];
4458 pending_del_nr = 1;
4459 } else if (pending_del_nr &&
4460 path->slots[0] + 1 == pending_del_slot) {
4461 /* hop on the pending chunk */
4462 pending_del_nr++;
4463 pending_del_slot = path->slots[0];
4464 } else {
Chris Masond3977122009-01-05 21:25:51 -05004465 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004466 }
Chris Mason39279cc2007-06-12 06:35:45 -04004467 } else {
4468 break;
4469 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004470 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004471
Miao Xie27cdeb72014-04-02 19:51:05 +08004472 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004473 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004474 struct btrfs_ref ref = { 0 };
4475
Chris Mason28ed1342014-12-17 09:41:04 -08004476 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004477
4478 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4479 extent_start, extent_num_bytes, 0);
4480 ref.real_root = root->root_key.objectid;
4481 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4482 ino, extent_offset);
4483 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004484 if (ret) {
4485 btrfs_abort_transaction(trans, ret);
4486 break;
4487 }
Chris Mason28f75a02015-02-04 06:59:29 -08004488 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004489 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004490 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004491 }
Chris Mason39279cc2007-06-12 06:35:45 -04004492 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004493
Yan, Zheng80825102009-11-12 09:35:36 +00004494 if (found_type == BTRFS_INODE_ITEM_KEY)
4495 break;
4496
4497 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004498 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004499 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004500 if (pending_del_nr) {
4501 ret = btrfs_del_items(trans, root, path,
4502 pending_del_slot,
4503 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004504 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004505 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004506 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004507 }
Yan, Zheng80825102009-11-12 09:35:36 +00004508 pending_del_nr = 0;
4509 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004510 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004511
Chris Mason28f75a02015-02-04 06:59:29 -08004512 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004513 * We can generate a lot of delayed refs, so we need to
4514 * throttle every once and a while and make sure we're
4515 * adding enough space to keep up with the work we are
4516 * generating. Since we hold a transaction here we
4517 * can't flush, and we don't want to FLUSH_LIMIT because
4518 * we could have generated too many delayed refs to
4519 * actually allocate, so just bail if we're short and
4520 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004521 */
Josef Bacik28bad212018-12-03 10:20:38 -05004522 if (should_throttle) {
4523 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4524 BTRFS_RESERVE_NO_FLUSH);
4525 if (ret) {
4526 ret = -EAGAIN;
4527 break;
4528 }
Chris Mason28f75a02015-02-04 06:59:29 -08004529 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004530 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004531 } else {
4532 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004533 }
Chris Mason39279cc2007-06-12 06:35:45 -04004534 }
Yan, Zheng80825102009-11-12 09:35:36 +00004535out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004536 if (ret >= 0 && pending_del_nr) {
4537 int err;
4538
4539 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004540 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004541 if (err) {
4542 btrfs_abort_transaction(trans, err);
4543 ret = err;
4544 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004545 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004546 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4547 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004548 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004549 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004550 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004551 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4552 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004553 }
Chris Mason28ed1342014-12-17 09:41:04 -08004554
Chris Mason39279cc2007-06-12 06:35:45 -04004555 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004556 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004557}
4558
Chris Masona52d9a82007-08-27 16:49:44 -04004559/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304560 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004561 * @inode - inode that we're zeroing
4562 * @from - the offset to start zeroing
4563 * @len - the length to zero, 0 to zero the entire range respective to the
4564 * offset
4565 * @front - zero up to the offset instead of from the offset on
4566 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304567 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004568 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004569 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304570int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004571 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004572{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004573 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004574 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004575 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4576 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004577 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004578 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004579 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004580 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004581 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004582 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304583 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004584 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004585 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004586 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004587 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304588 u64 block_start;
4589 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004590
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004591 if (IS_ALIGNED(offset, blocksize) &&
4592 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004593 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304594
Josef Bacik8b62f872017-10-19 14:15:55 -04004595 block_start = round_down(from, blocksize);
4596 block_end = block_start + blocksize - 1;
4597
Nikolay Borisov36ea6f32020-06-03 08:55:41 +03004598 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved,
4599 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004600 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004601 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4602 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004603 /* For nocow case, no need to reserve data space */
4604 only_release_metadata = true;
4605 } else {
4606 goto out;
4607 }
4608 }
4609 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4610 if (ret < 0) {
4611 if (!only_release_metadata)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03004612 btrfs_free_reserved_data_space(BTRFS_I(inode),
4613 data_reserved, block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004614 goto out;
4615 }
Chris Mason211c17f2008-05-15 09:13:45 -04004616again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004617 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004618 if (!page) {
Nikolay Borisov86d52922020-06-03 08:55:40 +03004619 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004620 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004621 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004622 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004623 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004624 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004625
Chris Masona52d9a82007-08-27 16:49:44 -04004626 if (!PageUptodate(page)) {
4627 ret = btrfs_readpage(NULL, page);
4628 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004629 if (page->mapping != mapping) {
4630 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004631 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004632 goto again;
4633 }
Chris Masona52d9a82007-08-27 16:49:44 -04004634 if (!PageUptodate(page)) {
4635 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004636 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004637 }
4638 }
Chris Mason211c17f2008-05-15 09:13:45 -04004639 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004640
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304641 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004642 set_page_extent_mapped(page);
4643
Nikolay Borisovc3504372020-06-03 08:55:03 +03004644 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004645 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304646 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004647 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004648 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004649 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004650 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004651 btrfs_put_ordered_extent(ordered);
4652 goto again;
4653 }
4654
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304655 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004656 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4657 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004658
Nikolay Borisovc2566f22020-06-03 08:55:35 +03004659 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004660 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004661 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304662 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004663 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004664 goto out_unlock;
4665 }
4666
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304667 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004668 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304669 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004670 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004671 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304672 memset(kaddr + (block_start - page_offset(page)),
4673 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004674 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304675 memset(kaddr + (block_start - page_offset(page)) + offset,
4676 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004677 flush_dcache_page(page);
4678 kunmap(page);
4679 }
Chris Mason247e7432008-07-17 12:53:51 -04004680 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004681 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004682 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004683
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004684 if (only_release_metadata)
4685 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4686 block_end, EXTENT_NORESERVE, NULL, NULL,
4687 GFP_NOFS);
4688
Chris Mason89642222008-07-24 09:41:53 -04004689out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004690 if (ret) {
4691 if (only_release_metadata)
4692 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4693 blocksize, true);
4694 else
Nikolay Borisov86d52922020-06-03 08:55:40 +03004695 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004696 block_start, blocksize, true);
4697 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004698 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004699 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004700 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004701out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004702 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004703 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004704 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004705 return ret;
4706}
4707
Josef Bacik16e75492013-10-22 12:18:51 -04004708static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4709 u64 offset, u64 len)
4710{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004711 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004712 struct btrfs_trans_handle *trans;
4713 int ret;
4714
4715 /*
4716 * Still need to make sure the inode looks like it's been updated so
4717 * that any holes get logged if we fsync.
4718 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004719 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4720 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004721 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4722 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4723 return 0;
4724 }
4725
4726 /*
4727 * 1 - for the one we're dropping
4728 * 1 - for the one we're adding
4729 * 1 - for updating the inode.
4730 */
4731 trans = btrfs_start_transaction(root, 3);
4732 if (IS_ERR(trans))
4733 return PTR_ERR(trans);
4734
4735 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4736 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004737 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004738 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004739 return ret;
4740 }
4741
David Sterbaf85b7372017-01-20 14:54:07 +01004742 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4743 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004744 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004745 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004746 else
4747 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004748 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004749 return ret;
4750}
4751
Josef Bacik695a0d02011-03-04 15:46:53 -05004752/*
4753 * This function puts in dummy file extents for the area we're creating a hole
4754 * for. So if we are truncating this file to a larger size we need to insert
4755 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4756 * the range between oldsize and size
4757 */
Josef Bacika41ad392011-01-31 15:30:16 -05004758int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004759{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004760 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004761 struct btrfs_root *root = BTRFS_I(inode)->root;
4762 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004763 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004764 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004765 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004766 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4767 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004768 u64 last_byte;
4769 u64 cur_offset;
4770 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004771 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004772
Josef Bacika71754f2013-06-17 17:14:39 -04004773 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304774 * If our size started in the middle of a block we need to zero out the
4775 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004776 * expose stale data.
4777 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304778 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004779 if (err)
4780 return err;
4781
Yan Zheng9036c102008-10-30 14:19:41 -04004782 if (size <= hole_start)
4783 return 0;
4784
David Sterbab272ae22020-02-05 19:09:33 +01004785 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004786 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004787 cur_offset = hole_start;
4788 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004789 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004790 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004791 if (IS_ERR(em)) {
4792 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004793 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004794 break;
4795 }
Yan Zheng9036c102008-10-30 14:19:41 -04004796 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004797 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004798 hole_size = last_byte - cur_offset;
4799
Yan, Zheng80825102009-11-12 09:35:36 +00004800 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004801 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004802
Josef Bacik16e75492013-10-22 12:18:51 -04004803 err = maybe_insert_hole(root, inode, cur_offset,
4804 hole_size);
4805 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004806 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004807
4808 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4809 cur_offset, hole_size);
4810 if (err)
4811 break;
4812
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004813 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004814 cur_offset + hole_size - 1, 0);
4815 hole_em = alloc_extent_map();
4816 if (!hole_em) {
4817 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4818 &BTRFS_I(inode)->runtime_flags);
4819 goto next;
4820 }
4821 hole_em->start = cur_offset;
4822 hole_em->len = hole_size;
4823 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004824
Josef Bacik5dc562c2012-08-17 13:14:17 -04004825 hole_em->block_start = EXTENT_MAP_HOLE;
4826 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004827 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004828 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004829 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004830 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004831
4832 while (1) {
4833 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004834 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004835 write_unlock(&em_tree->lock);
4836 if (err != -EEXIST)
4837 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004838 btrfs_drop_extent_cache(BTRFS_I(inode),
4839 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004840 cur_offset +
4841 hole_size - 1, 0);
4842 }
4843 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004844 } else {
4845 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4846 cur_offset, hole_size);
4847 if (err)
4848 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004849 }
Josef Bacik16e75492013-10-22 12:18:51 -04004850next:
Yan Zheng9036c102008-10-30 14:19:41 -04004851 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004852 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004853 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004854 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004855 break;
4856 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004857 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004858 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004859 return err;
4860}
4861
Eric Sandeen3972f262013-01-12 02:57:22 +00004862static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004863{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004864 struct btrfs_root *root = BTRFS_I(inode)->root;
4865 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004866 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004867 loff_t newsize = attr->ia_size;
4868 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004869 int ret;
4870
Eric Sandeen3972f262013-01-12 02:57:22 +00004871 /*
4872 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4873 * special case where we need to update the times despite not having
4874 * these flags set. For all other operations the VFS set these flags
4875 * explicitly if it wants a timestamp update.
4876 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004877 if (newsize != oldsize) {
4878 inode_inc_iversion(inode);
4879 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4880 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004881 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004882 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004883
Josef Bacika41ad392011-01-31 15:30:16 -05004884 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004885 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004886 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004887 * This is to ensure the snapshot captures a fully consistent
4888 * state of this file - if the snapshot captures this expanding
4889 * truncation, it must capture all writes that happened before
4890 * this truncation.
4891 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004892 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004893 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004894 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004895 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004896 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004897 }
Yan, Zheng80825102009-11-12 09:35:36 +00004898
Miao Xief4a2f4c2011-12-14 20:12:01 -05004899 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004900 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004901 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004902 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004903 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004904
4905 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004906 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304907 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004908 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004909 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004910 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004911 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004912
Josef Bacika41ad392011-01-31 15:30:16 -05004913 /*
4914 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03004915 * zero. Make sure any new writes to the file get on disk
4916 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05004917 */
4918 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03004919 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04004920 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004921
Josef Bacika41ad392011-01-31 15:30:16 -05004922 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004923
Miao Xie2e60a512013-02-08 07:01:08 +00004924 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00004925
Filipe Manana213e8c52018-02-06 20:40:31 +00004926 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004927 if (ret && inode->i_nlink) {
4928 int err;
4929
4930 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004931 * Truncate failed, so fix up the in-memory size. We
4932 * adjusted disk_i_size down as we removed extents, so
4933 * wait for disk_i_size to be stable and then update the
4934 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004935 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004936 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004937 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004938 return err;
4939 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004940 }
Yan, Zheng80825102009-11-12 09:35:36 +00004941 }
4942
Josef Bacika41ad392011-01-31 15:30:16 -05004943 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004944}
4945
Chris Mason39279cc2007-06-12 06:35:45 -04004946static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4947{
David Howells2b0143b2015-03-17 22:25:59 +00004948 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004949 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004950 int err;
4951
Li Zefanb83cc962010-12-20 16:04:08 +08004952 if (btrfs_root_readonly(root))
4953 return -EROFS;
4954
Jan Kara31051c82016-05-26 16:55:18 +02004955 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004956 if (err)
4957 return err;
4958
Chris Mason5a3f23d2009-03-31 13:27:11 -04004959 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004960 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004961 if (err)
4962 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004963 }
Yan Zheng9036c102008-10-30 14:19:41 -04004964
Christoph Hellwig10257742010-06-04 11:30:02 +02004965 if (attr->ia_valid) {
4966 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004967 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004968 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004969
Josef Bacik22c44fe2011-11-30 10:45:38 -05004970 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004971 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004972 }
4973
Chris Mason39279cc2007-06-12 06:35:45 -04004974 return err;
4975}
Chris Mason61295eb2008-01-14 16:24:38 -05004976
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004977/*
4978 * While truncating the inode pages during eviction, we get the VFS calling
4979 * btrfs_invalidatepage() against each page of the inode. This is slow because
4980 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4981 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4982 * extent_state structures over and over, wasting lots of time.
4983 *
4984 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4985 * those expensive operations on a per page basis and do only the ordered io
4986 * finishing, while we release here the extent_map and extent_state structures,
4987 * without the excessive merging and splitting.
4988 */
4989static void evict_inode_truncate_pages(struct inode *inode)
4990{
4991 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4992 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4993 struct rb_node *node;
4994
4995 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004996 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004997
4998 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004999 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005000 struct extent_map *em;
5001
Liu Bo07e1ce02018-08-23 03:51:52 +08005002 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005003 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005004 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5005 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005006 remove_extent_mapping(map_tree, em);
5007 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005008 if (need_resched()) {
5009 write_unlock(&map_tree->lock);
5010 cond_resched();
5011 write_lock(&map_tree->lock);
5012 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005013 }
5014 write_unlock(&map_tree->lock);
5015
Filipe Manana6ca07092015-05-26 00:55:42 +01005016 /*
5017 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005018 * We can have ongoing bios started by readahead that have
5019 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005020 * still in progress (unlocked the pages in the bio but did not yet
5021 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005022 * ranges can still be locked and eviction started because before
5023 * submitting those bios, which are executed by a separate task (work
5024 * queue kthread), inode references (inode->i_count) were not taken
5025 * (which would be dropped in the end io callback of each bio).
5026 * Therefore here we effectively end up waiting for those bios and
5027 * anyone else holding locked ranges without having bumped the inode's
5028 * reference count - if we don't do it, when they access the inode's
5029 * io_tree to unlock a range it may be too late, leading to an
5030 * use-after-free issue.
5031 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005032 spin_lock(&io_tree->lock);
5033 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5034 struct extent_state *state;
5035 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005036 u64 start;
5037 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005038 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005039
5040 node = rb_first(&io_tree->state);
5041 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005042 start = state->start;
5043 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005044 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005045 spin_unlock(&io_tree->lock);
5046
David Sterbaff13db42015-12-03 14:30:40 +01005047 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005048
5049 /*
5050 * If still has DELALLOC flag, the extent didn't reach disk,
5051 * and its reserved space won't be freed by delayed_ref.
5052 * So we need to free its reserved space here.
5053 * (Refer to comment in btrfs_invalidatepage, case 2)
5054 *
5055 * Note, end is the bytenr of last byte, so we need + 1 here.
5056 */
Filipe Manana421f0922018-10-12 13:02:48 +01005057 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005058 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5059 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005060
Filipe Manana6ca07092015-05-26 00:55:42 +01005061 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005062 EXTENT_LOCKED | EXTENT_DELALLOC |
5063 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5064 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005065
Filipe Manana7064dd52014-08-08 02:47:05 +01005066 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005067 spin_lock(&io_tree->lock);
5068 }
5069 spin_unlock(&io_tree->lock);
5070}
5071
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005072static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005073 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005074{
5075 struct btrfs_fs_info *fs_info = root->fs_info;
5076 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005077 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005078 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005079 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005080
Josef Bacikd3984c92019-08-01 18:19:37 -04005081 /*
5082 * Eviction should be taking place at some place safe because of our
5083 * delayed iputs. However the normal flushing code will run delayed
5084 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5085 *
5086 * We reserve the delayed_refs_extra here again because we can't use
5087 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5088 * above. We reserve our extra bit here because we generate a ton of
5089 * delayed refs activity by truncating.
5090 *
5091 * If we cannot make our reservation we'll attempt to steal from the
5092 * global reserve, because we really want to be able to free up space.
5093 */
5094 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5095 BTRFS_RESERVE_FLUSH_EVICT);
5096 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005097 /*
5098 * Try to steal from the global reserve if there is space for
5099 * it.
5100 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005101 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5102 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5103 btrfs_warn(fs_info,
5104 "could not allocate space for delete; will truncate on mount");
5105 return ERR_PTR(-ENOSPC);
5106 }
5107 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005108 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005109
5110 trans = btrfs_join_transaction(root);
5111 if (IS_ERR(trans))
5112 return trans;
5113
5114 if (delayed_refs_extra) {
5115 trans->block_rsv = &fs_info->trans_block_rsv;
5116 trans->bytes_reserved = delayed_refs_extra;
5117 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5118 delayed_refs_extra, 1);
5119 }
5120 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005121}
5122
Al Virobd555972010-06-07 11:35:40 -04005123void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005124{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005125 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005126 struct btrfs_trans_handle *trans;
5127 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005128 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005129 int ret;
5130
liubo1abe9b82011-03-24 11:18:59 +00005131 trace_btrfs_inode_evict(inode);
5132
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005133 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005134 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005135 return;
5136 }
5137
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005138 evict_inode_truncate_pages(inode);
5139
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005140 if (inode->i_nlink &&
5141 ((btrfs_root_refs(&root->root_item) != 0 &&
5142 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005143 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005144 goto no_delete;
5145
Omar Sandoval27919062018-05-11 13:13:37 -07005146 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005147 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005148
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005149 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005150
Omar Sandoval7b40b692018-05-11 13:13:33 -07005151 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005152 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005153
Yan, Zheng76dda932009-09-21 16:00:26 -04005154 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005155 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5156 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005157 goto no_delete;
5158 }
5159
Nikolay Borisovaa790212017-01-10 20:35:40 +02005160 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005161 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005162 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005163
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005164 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005165 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005166 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005167 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005168 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005169
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005170 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005171
Yan, Zheng80825102009-11-12 09:35:36 +00005172 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005173 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005174 if (IS_ERR(trans))
5175 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005176
5177 trans->block_rsv = rsv;
5178
Yan, Zhengd68fc572010-05-16 10:49:58 -04005179 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005180 trans->block_rsv = &fs_info->trans_block_rsv;
5181 btrfs_end_transaction(trans);
5182 btrfs_btree_balance_dirty(fs_info);
5183 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5184 goto free_rsv;
5185 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005186 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005187 }
5188
Josef Bacik4ef31a42013-08-13 14:10:08 -04005189 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005190 * Errors here aren't a big deal, it just means we leave orphan items in
5191 * the tree. They will be cleaned up on the next mount. If the inode
5192 * number gets reused, cleanup deletes the orphan item without doing
5193 * anything, and unlink reuses the existing orphan item.
5194 *
5195 * If it turns out that we are dropping too many of these, we might want
5196 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005197 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005198 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005199 if (!IS_ERR(trans)) {
5200 trans->block_rsv = rsv;
5201 btrfs_orphan_del(trans, BTRFS_I(inode));
5202 trans->block_rsv = &fs_info->trans_block_rsv;
5203 btrfs_end_transaction(trans);
5204 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005205
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005206 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005207 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005208 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005209
Omar Sandoval27919062018-05-11 13:13:37 -07005210free_rsv:
5211 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005212no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005213 /*
5214 * If we didn't successfully delete, the orphan item will still be in
5215 * the tree and we'll retry on the next mount. Again, we might also want
5216 * to retry these periodically in the future.
5217 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005218 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005219 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005220}
5221
5222/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005223 * Return the key found in the dir entry in the location pointer, fill @type
5224 * with BTRFS_FT_*, and return 0.
5225 *
Su Yue005d6712018-03-05 17:13:37 +08005226 * If no dir entries were found, returns -ENOENT.
5227 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005228 */
5229static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005230 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005231{
5232 const char *name = dentry->d_name.name;
5233 int namelen = dentry->d_name.len;
5234 struct btrfs_dir_item *di;
5235 struct btrfs_path *path;
5236 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005237 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005238
5239 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005240 if (!path)
5241 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005242
David Sterbaf85b7372017-01-20 14:54:07 +01005243 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5244 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005245 if (IS_ERR_OR_NULL(di)) {
5246 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005247 goto out;
5248 }
Chris Masond3977122009-01-05 21:25:51 -05005249
Chris Mason5f39d392007-10-15 16:14:19 -04005250 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005251 if (location->type != BTRFS_INODE_ITEM_KEY &&
5252 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005253 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005254 btrfs_warn(root->fs_info,
5255"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5256 __func__, name, btrfs_ino(BTRFS_I(dir)),
5257 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005258 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005259 if (!ret)
5260 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005261out:
Chris Mason39279cc2007-06-12 06:35:45 -04005262 btrfs_free_path(path);
5263 return ret;
5264}
5265
5266/*
5267 * when we hit a tree root in a directory, the btrfs part of the inode
5268 * needs to be changed to reflect the root directory of the tree root. This
5269 * is kind of like crossing a mount point.
5270 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005271static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005272 struct inode *dir,
5273 struct dentry *dentry,
5274 struct btrfs_key *location,
5275 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005276{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005277 struct btrfs_path *path;
5278 struct btrfs_root *new_root;
5279 struct btrfs_root_ref *ref;
5280 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005281 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005282 int ret;
5283 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005284
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005285 path = btrfs_alloc_path();
5286 if (!path) {
5287 err = -ENOMEM;
5288 goto out;
5289 }
Chris Mason39279cc2007-06-12 06:35:45 -04005290
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005291 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005292 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5293 key.type = BTRFS_ROOT_REF_KEY;
5294 key.offset = location->objectid;
5295
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005296 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005297 if (ret) {
5298 if (ret < 0)
5299 err = ret;
5300 goto out;
5301 }
Chris Mason39279cc2007-06-12 06:35:45 -04005302
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005303 leaf = path->nodes[0];
5304 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005305 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005306 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5307 goto out;
5308
5309 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5310 (unsigned long)(ref + 1),
5311 dentry->d_name.len);
5312 if (ret)
5313 goto out;
5314
David Sterbab3b4aa72011-04-21 01:20:15 +02005315 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005316
David Sterba56e93572020-05-15 19:35:55 +02005317 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005318 if (IS_ERR(new_root)) {
5319 err = PTR_ERR(new_root);
5320 goto out;
5321 }
5322
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005323 *sub_root = new_root;
5324 location->objectid = btrfs_root_dirid(&new_root->root_item);
5325 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005326 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005327 err = 0;
5328out:
5329 btrfs_free_path(path);
5330 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005331}
5332
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005333static void inode_tree_add(struct inode *inode)
5334{
5335 struct btrfs_root *root = BTRFS_I(inode)->root;
5336 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005337 struct rb_node **p;
5338 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005339 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005340 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005341
Al Viro1d3382cb2010-10-23 15:19:20 -04005342 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005343 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005344 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005345 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005346 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005347 while (*p) {
5348 parent = *p;
5349 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5350
David Sterba37508512018-06-29 10:56:40 +02005351 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005352 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005353 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005354 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005355 else {
5356 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005357 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005358 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005359 RB_CLEAR_NODE(parent);
5360 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005361 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005362 }
5363 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005364 rb_link_node(new, parent, p);
5365 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005366 spin_unlock(&root->inode_lock);
5367}
5368
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005369static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005370{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005371 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005372 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005373
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005374 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005375 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5376 rb_erase(&inode->rb_node, &root->inode_tree);
5377 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005378 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005379 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005380 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005381
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005382 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005383 spin_lock(&root->inode_lock);
5384 empty = RB_EMPTY_ROOT(&root->inode_tree);
5385 spin_unlock(&root->inode_lock);
5386 if (empty)
5387 btrfs_add_dead_root(root);
5388 }
5389}
5390
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005391
Chris Masone02119d2008-09-05 16:13:11 -04005392static int btrfs_init_locked_inode(struct inode *inode, void *p)
5393{
5394 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005395
5396 inode->i_ino = args->ino;
5397 BTRFS_I(inode)->location.objectid = args->ino;
5398 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5399 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005400 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5401 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005402 return 0;
5403}
5404
5405static int btrfs_find_actor(struct inode *inode, void *opaque)
5406{
5407 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005408
5409 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005410 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005411}
5412
David Sterba0202e832020-05-15 19:35:59 +02005413static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005414 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005415{
5416 struct inode *inode;
5417 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005418 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005419
David Sterba0202e832020-05-15 19:35:59 +02005420 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005421 args.root = root;
5422
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005423 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005424 btrfs_init_locked_inode,
5425 (void *)&args);
5426 return inode;
5427}
5428
David Sterba4c66e0d2019-10-03 19:09:35 +02005429/*
David Sterba0202e832020-05-15 19:35:59 +02005430 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005431 * Path can be preallocated to prevent recursing back to iget through
5432 * allocator. NULL is also valid but may require an additional allocation
5433 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305434 */
David Sterba0202e832020-05-15 19:35:59 +02005435struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005436 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305437{
5438 struct inode *inode;
5439
David Sterba0202e832020-05-15 19:35:59 +02005440 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305441 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005442 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305443
5444 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005445 int ret;
5446
Filipe Manana4222ea72018-10-24 10:13:03 +01005447 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005448 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005449 inode_tree_add(inode);
5450 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005451 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005452 iget_failed(inode);
5453 /*
5454 * ret > 0 can come from btrfs_search_slot called by
5455 * btrfs_read_locked_inode, this means the inode item
5456 * was not found.
5457 */
5458 if (ret > 0)
5459 ret = -ENOENT;
5460 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005461 }
5462 }
5463
Balaji Rao1a54ef82008-07-21 02:01:04 +05305464 return inode;
5465}
5466
David Sterba0202e832020-05-15 19:35:59 +02005467struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005468{
David Sterba0202e832020-05-15 19:35:59 +02005469 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005470}
5471
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005472static struct inode *new_simple_dir(struct super_block *s,
5473 struct btrfs_key *key,
5474 struct btrfs_root *root)
5475{
5476 struct inode *inode = new_inode(s);
5477
5478 if (!inode)
5479 return ERR_PTR(-ENOMEM);
5480
Josef Bacik5c8fd992020-02-14 16:11:43 -05005481 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005482 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005483 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005484
5485 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005486 /*
5487 * We only need lookup, the rest is read-only and there's no inode
5488 * associated with the dentry
5489 */
5490 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005491 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005492 inode->i_fop = &simple_dir_operations;
5493 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005494 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305495 inode->i_atime = inode->i_mtime;
5496 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005497 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005498
5499 return inode;
5500}
5501
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005502static inline u8 btrfs_inode_type(struct inode *inode)
5503{
5504 /*
5505 * Compile-time asserts that generic FT_* types still match
5506 * BTRFS_FT_* types
5507 */
5508 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5509 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5510 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5511 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5512 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5513 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5514 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5515 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5516
5517 return fs_umode_to_ftype(inode->i_mode);
5518}
5519
Chris Mason3de45862008-11-17 21:02:50 -05005520struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005521{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005522 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005523 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005524 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005525 struct btrfs_root *sub_root = root;
5526 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005527 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005528 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005529
5530 if (dentry->d_name.len > BTRFS_NAME_LEN)
5531 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005532
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005533 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005534 if (ret < 0)
5535 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005536
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005537 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005538 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005539 if (IS_ERR(inode))
5540 return inode;
5541
5542 /* Do extra check against inode mode with di_type */
5543 if (btrfs_inode_type(inode) != di_type) {
5544 btrfs_crit(fs_info,
5545"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5546 inode->i_mode, btrfs_inode_type(inode),
5547 di_type);
5548 iput(inode);
5549 return ERR_PTR(-EUCLEAN);
5550 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005551 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005552 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005553
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005554 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005555 &location, &sub_root);
5556 if (ret < 0) {
5557 if (ret != -ENOENT)
5558 inode = ERR_PTR(ret);
5559 else
5560 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5561 } else {
David Sterba0202e832020-05-15 19:35:59 +02005562 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005563 }
Josef Bacik87270022020-01-24 09:32:31 -05005564 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005565 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005566
Julia Lawall34d19ba2011-01-24 19:55:19 +00005567 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005568 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005569 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005570 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005571 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005572 if (ret) {
5573 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005574 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005575 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005576 }
5577
Chris Mason3de45862008-11-17 21:02:50 -05005578 return inode;
5579}
5580
Nick Pigginfe15ce42011-01-07 17:49:23 +11005581static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005582{
5583 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005584 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005585
Li Zefan848cce02012-02-21 17:04:28 +08005586 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005587 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005588
Li Zefan848cce02012-02-21 17:04:28 +08005589 if (inode) {
5590 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005591 if (btrfs_root_refs(&root->root_item) == 0)
5592 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005593
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005594 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005595 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005596 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005597 return 0;
5598}
5599
Chris Mason3de45862008-11-17 21:02:50 -05005600static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005601 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005602{
Al Viro3837d202018-10-10 16:38:27 -04005603 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005604
Al Viro3837d202018-10-10 16:38:27 -04005605 if (inode == ERR_PTR(-ENOENT))
5606 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005607 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005608}
5609
Josef Bacik23b5ec72017-07-24 15:14:25 -04005610/*
5611 * All this infrastructure exists because dir_emit can fault, and we are holding
5612 * the tree lock when doing readdir. For now just allocate a buffer and copy
5613 * our information into that, and then dir_emit from the buffer. This is
5614 * similar to what NFS does, only we don't keep the buffer around in pagecache
5615 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5616 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5617 * tree lock.
5618 */
5619static int btrfs_opendir(struct inode *inode, struct file *file)
5620{
5621 struct btrfs_file_private *private;
5622
5623 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5624 if (!private)
5625 return -ENOMEM;
5626 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5627 if (!private->filldir_buf) {
5628 kfree(private);
5629 return -ENOMEM;
5630 }
5631 file->private_data = private;
5632 return 0;
5633}
5634
5635struct dir_entry {
5636 u64 ino;
5637 u64 offset;
5638 unsigned type;
5639 int name_len;
5640};
5641
5642static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5643{
5644 while (entries--) {
5645 struct dir_entry *entry = addr;
5646 char *name = (char *)(entry + 1);
5647
David Sterba92d32172018-04-16 21:10:14 +02005648 ctx->pos = get_unaligned(&entry->offset);
5649 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5650 get_unaligned(&entry->ino),
5651 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005652 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005653 addr += sizeof(struct dir_entry) +
5654 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005655 ctx->pos++;
5656 }
5657 return 0;
5658}
5659
Al Viro9cdda8d2013-05-22 16:48:09 -04005660static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005661{
Al Viro9cdda8d2013-05-22 16:48:09 -04005662 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005663 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005664 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005665 struct btrfs_dir_item *di;
5666 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005667 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005668 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005669 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005670 struct list_head ins_list;
5671 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005672 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005673 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005674 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005675 char *name_ptr;
5676 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005677 int entries = 0;
5678 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005679 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005680 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005681
Al Viro9cdda8d2013-05-22 16:48:09 -04005682 if (!dir_emit_dots(file, ctx))
5683 return 0;
5684
David Woodhouse49593bf2008-08-17 17:08:36 +01005685 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005686 if (!path)
5687 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005688
Josef Bacik23b5ec72017-07-24 15:14:25 -04005689 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005690 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005691
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005692 INIT_LIST_HEAD(&ins_list);
5693 INIT_LIST_HEAD(&del_list);
5694 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005695
Josef Bacik23b5ec72017-07-24 15:14:25 -04005696again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005697 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005698 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005699 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005700
Chris Mason39279cc2007-06-12 06:35:45 -04005701 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5702 if (ret < 0)
5703 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005704
5705 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005706 struct dir_entry *entry;
5707
Chris Mason5f39d392007-10-15 16:14:19 -04005708 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005709 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005710 if (slot >= btrfs_header_nritems(leaf)) {
5711 ret = btrfs_next_leaf(root, path);
5712 if (ret < 0)
5713 goto err;
5714 else if (ret > 0)
5715 break;
5716 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005717 }
Chris Mason3de45862008-11-17 21:02:50 -05005718
Chris Mason5f39d392007-10-15 16:14:19 -04005719 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5720
5721 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005722 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005723 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005724 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005725 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005726 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005727 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005728 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005729 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005730 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005731 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5732 PAGE_SIZE) {
5733 btrfs_release_path(path);
5734 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5735 if (ret)
5736 goto nopos;
5737 addr = private->filldir_buf;
5738 entries = 0;
5739 total_len = 0;
5740 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005741 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005742
5743 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005744 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005745 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005746 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5747 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005748 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005749 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005750 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005751 put_unaligned(location.objectid, &entry->ino);
5752 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005753 entries++;
5754 addr += sizeof(struct dir_entry) + name_len;
5755 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005756next:
5757 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005758 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005759 btrfs_release_path(path);
5760
5761 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5762 if (ret)
5763 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005764
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005765 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005766 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005767 goto nopos;
5768
Zach Browndb62efb2013-07-11 16:19:42 -07005769 /*
5770 * Stop new entries from being returned after we return the last
5771 * entry.
5772 *
5773 * New directory entries are assigned a strictly increasing
5774 * offset. This means that new entries created during readdir
5775 * are *guaranteed* to be seen in the future by that readdir.
5776 * This has broken buggy programs which operate on names as
5777 * they're returned by readdir. Until we re-use freed offsets
5778 * we have this hack to stop new entries from being returned
5779 * under the assumption that they'll never reach this huge
5780 * offset.
5781 *
5782 * This is being careful not to overflow 32bit loff_t unless the
5783 * last entry requires it because doing so has broken 32bit apps
5784 * in the past.
5785 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005786 if (ctx->pos >= INT_MAX)
5787 ctx->pos = LLONG_MAX;
5788 else
5789 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005790nopos:
5791 ret = 0;
5792err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005793 if (put)
5794 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005795 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005796 return ret;
5797}
5798
Chris Mason39279cc2007-06-12 06:35:45 -04005799/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005800 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005801 * inode changes. But, it is most likely to find the inode in cache.
5802 * FIXME, needs more benchmarking...there are no reasons other than performance
5803 * to keep or drop this code.
5804 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005805static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005806{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005807 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005808 struct btrfs_root *root = BTRFS_I(inode)->root;
5809 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005810 int ret;
5811
Josef Bacik72ac3c02012-05-23 14:13:11 -04005812 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005813 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005814
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005815 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005816 if (IS_ERR(trans))
5817 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005818
5819 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovbf6dd432021-02-22 18:40:44 +02005820 if (ret && (ret == -ENOSPC || ret == -EDQUOT)) {
Chris Mason94b60442010-05-26 11:02:00 -04005821 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005822 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005823 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005824 if (IS_ERR(trans))
5825 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005826
Chris Mason94b60442010-05-26 11:02:00 -04005827 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005828 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005829 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005830 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005831 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005832
5833 return ret;
5834}
5835
5836/*
5837 * This is a copy of file_update_time. We need this so we can return error on
5838 * ENOSPC for updating the inode in the case of file write and mmap writes.
5839 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005840static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005841 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005842{
Alexander Block2bc5565282012-06-15 09:49:33 +02005843 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005844 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005845
5846 if (btrfs_root_readonly(root))
5847 return -EROFS;
5848
Josef Bacike41f9412012-03-26 09:46:47 -04005849 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005850 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005851 if (flags & S_CTIME)
5852 inode->i_ctime = *now;
5853 if (flags & S_MTIME)
5854 inode->i_mtime = *now;
5855 if (flags & S_ATIME)
5856 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005857 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005858}
5859
Chris Masond352ac62008-09-29 15:18:18 -04005860/*
5861 * find the highest existing sequence number in a directory
5862 * and then set the in-memory index_cnt variable to reflect
5863 * free sequence numbers
5864 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005865static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005866{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005867 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005868 struct btrfs_key key, found_key;
5869 struct btrfs_path *path;
5870 struct extent_buffer *leaf;
5871 int ret;
5872
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005873 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005874 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005875 key.offset = (u64)-1;
5876
5877 path = btrfs_alloc_path();
5878 if (!path)
5879 return -ENOMEM;
5880
5881 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5882 if (ret < 0)
5883 goto out;
5884 /* FIXME: we should be able to handle this */
5885 if (ret == 0)
5886 goto out;
5887 ret = 0;
5888
5889 /*
5890 * MAGIC NUMBER EXPLANATION:
5891 * since we search a directory based on f_pos we have to start at 2
5892 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5893 * else has to start at 2
5894 */
5895 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005896 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005897 goto out;
5898 }
5899
5900 path->slots[0]--;
5901
5902 leaf = path->nodes[0];
5903 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5904
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005905 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005906 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005907 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005908 goto out;
5909 }
5910
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005911 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005912out:
5913 btrfs_free_path(path);
5914 return ret;
5915}
5916
Chris Masond352ac62008-09-29 15:18:18 -04005917/*
5918 * helper to find a free sequence number in a given directory. This current
5919 * code is very simple, later versions will do smarter things in the btree
5920 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005921int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005922{
5923 int ret = 0;
5924
Nikolay Borisov877574e2017-02-20 13:50:33 +02005925 if (dir->index_cnt == (u64)-1) {
5926 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005927 if (ret) {
5928 ret = btrfs_set_inode_index_count(dir);
5929 if (ret)
5930 return ret;
5931 }
Josef Bacikaec74772008-07-24 12:12:38 -04005932 }
5933
Nikolay Borisov877574e2017-02-20 13:50:33 +02005934 *index = dir->index_cnt;
5935 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005936
5937 return ret;
5938}
5939
Chris Masonb0d5d102014-09-08 13:08:51 -07005940static int btrfs_insert_inode_locked(struct inode *inode)
5941{
5942 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005943
5944 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005945 args.root = BTRFS_I(inode)->root;
5946
5947 return insert_inode_locked4(inode,
5948 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5949 btrfs_find_actor, &args);
5950}
5951
Anand Jain19aee8d2017-07-18 17:37:05 +08005952/*
5953 * Inherit flags from the parent inode.
5954 *
5955 * Currently only the compression flags and the cow flags are inherited.
5956 */
5957static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5958{
5959 unsigned int flags;
5960
5961 if (!dir)
5962 return;
5963
5964 flags = BTRFS_I(dir)->flags;
5965
5966 if (flags & BTRFS_INODE_NOCOMPRESS) {
5967 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5968 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5969 } else if (flags & BTRFS_INODE_COMPRESS) {
5970 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5971 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5972 }
5973
5974 if (flags & BTRFS_INODE_NODATACOW) {
5975 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5976 if (S_ISREG(inode->i_mode))
5977 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5978 }
5979
David Sterba7b6a2212018-03-26 18:40:21 +02005980 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005981}
5982
Chris Mason39279cc2007-06-12 06:35:45 -04005983static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5984 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005985 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005986 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005987 u64 ref_objectid, u64 objectid,
5988 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005989{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005990 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005991 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005992 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005993 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005994 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005995 struct btrfs_inode_ref *ref;
5996 struct btrfs_key key[2];
5997 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005998 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005999 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006000 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006001 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006002
Chris Mason5f39d392007-10-15 16:14:19 -04006003 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006004 if (!path)
6005 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006006
Josef Bacik11a19a92019-09-09 10:12:04 -04006007 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006008 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006009 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006010 if (!inode) {
6011 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006012 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006013 }
Chris Mason39279cc2007-06-12 06:35:45 -04006014
Li Zefan581bb052011-04-20 10:06:11 +08006015 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006016 * O_TMPFILE, set link count to 0, so that after this point,
6017 * we fill in an inode item with the correct link count.
6018 */
6019 if (!name)
6020 set_nlink(inode, 0);
6021
6022 /*
Li Zefan581bb052011-04-20 10:06:11 +08006023 * we have to initialize this early, so we can reclaim the inode
6024 * number if we fail afterwards in this function.
6025 */
6026 inode->i_ino = objectid;
6027
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006028 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006029 trace_btrfs_inode_request(dir);
6030
Nikolay Borisov877574e2017-02-20 13:50:33 +02006031 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006032 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006033 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006034 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006035 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006036 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006037 } else if (dir) {
6038 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006039 }
6040 /*
6041 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006042 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006043 * number
6044 */
6045 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006046 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006047 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006048 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006049 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db2b2007-08-27 16:49:44 -04006050
Josef Bacik5dc562c2012-08-17 13:14:17 -04006051 /*
6052 * We could have gotten an inode number from somebody who was fsynced
6053 * and then removed in this same transaction, so let's just set full
6054 * sync since it will be a full sync anyway and this will blow away the
6055 * old info in the log.
6056 */
6057 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6058
Chris Mason9c583092008-01-29 15:15:18 -05006059 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006060 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006061 key[0].offset = 0;
6062
Chris Mason9c583092008-01-29 15:15:18 -05006063 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006064
6065 if (name) {
6066 /*
6067 * Start new inodes with an inode_ref. This is slightly more
6068 * efficient for small numbers of hard links since they will
6069 * be packed into one item. Extended refs will kick in if we
6070 * add more hard links than can fit in the ref item.
6071 */
6072 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006073 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006074 key[1].offset = ref_objectid;
6075
6076 sizes[1] = name_len + sizeof(*ref);
6077 }
Chris Mason9c583092008-01-29 15:15:18 -05006078
Chris Masonb0d5d102014-09-08 13:08:51 -07006079 location = &BTRFS_I(inode)->location;
6080 location->objectid = objectid;
6081 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006082 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006083
6084 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006085 if (ret < 0) {
6086 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006087 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006088 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006089
Chris Masonb9473432009-03-13 11:00:37 -04006090 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006091 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006092 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006093 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006094
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006095 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006096 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306097
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006098 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306099 inode->i_atime = inode->i_mtime;
6100 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006101 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306102
Chris Mason5f39d392007-10-15 16:14:19 -04006103 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6104 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006105 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006106 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006107 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006108
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006109 if (name) {
6110 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6111 struct btrfs_inode_ref);
6112 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6113 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6114 ptr = (unsigned long)(ref + 1);
6115 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6116 }
Chris Mason9c583092008-01-29 15:15:18 -05006117
Chris Mason5f39d392007-10-15 16:14:19 -04006118 btrfs_mark_buffer_dirty(path->nodes[0]);
6119 btrfs_free_path(path);
6120
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006121 btrfs_inherit_iflags(inode, dir);
6122
Al Viro569254b2011-07-24 17:08:40 -04006123 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006124 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006125 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006126 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006127 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6128 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006129 }
6130
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006131 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006132
6133 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006134 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006135
Alexander Block8ea05e32012-07-25 17:35:53 +02006136 btrfs_update_root_times(trans, root);
6137
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006138 ret = btrfs_inode_inherit_props(trans, inode, dir);
6139 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006140 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006141 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006142 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006143
Chris Mason39279cc2007-06-12 06:35:45 -04006144 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006145
6146fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006147 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006148fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006149 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006150 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006151 btrfs_free_path(path);
6152 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006153}
6154
Chris Masond352ac62008-09-29 15:18:18 -04006155/*
6156 * utility function to add 'inode' into 'parent_inode' with
6157 * a give name and a given sequence number.
6158 * if 'add_backref' is true, also insert a backref from the
6159 * inode to the parent directory.
6160 */
Chris Masone02119d2008-09-05 16:13:11 -04006161int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006162 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006163 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006164{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006165 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006166 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006167 struct btrfs_root *root = parent_inode->root;
6168 u64 ino = btrfs_ino(inode);
6169 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006170
Li Zefan33345d012011-04-20 10:31:50 +08006171 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006172 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006173 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006174 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006175 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006176 key.offset = 0;
6177 }
Chris Mason39279cc2007-06-12 06:35:45 -04006178
Li Zefan33345d012011-04-20 10:31:50 +08006179 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006180 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006181 root->root_key.objectid, parent_ino,
6182 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006183 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006184 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6185 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006186 }
6187
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006188 /* Nothing to clean up yet */
6189 if (ret)
6190 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006191
Lu Fengqi684572d2018-08-04 21:10:57 +08006192 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006193 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006194 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006195 goto fail_dir_item;
6196 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006197 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006198 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006199 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006200
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006201 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006202 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006203 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006204 /*
6205 * If we are replaying a log tree, we do not want to update the mtime
6206 * and ctime of the parent directory with the current time, since the
6207 * log replay procedure is responsible for setting them to their correct
6208 * values (the ones it had when the fsync was done).
6209 */
6210 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6211 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6212
6213 parent_inode->vfs_inode.i_mtime = now;
6214 parent_inode->vfs_inode.i_ctime = now;
6215 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006216 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006217 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006218 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006219 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006220
6221fail_dir_item:
6222 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6223 u64 local_index;
6224 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006225 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006226 root->root_key.objectid, parent_ino,
6227 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006228 if (err)
6229 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006230 } else if (add_backref) {
6231 u64 local_index;
6232 int err;
6233
6234 err = btrfs_del_inode_ref(trans, root, name, name_len,
6235 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006236 if (err)
6237 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006238 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006239
6240 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006241 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006242}
6243
6244static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006245 struct btrfs_inode *dir, struct dentry *dentry,
6246 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006247{
Josef Bacika1b075d2010-11-19 20:36:11 +00006248 int err = btrfs_add_link(trans, dir, inode,
6249 dentry->d_name.name, dentry->d_name.len,
6250 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006251 if (err > 0)
6252 err = -EEXIST;
6253 return err;
6254}
6255
Josef Bacik618e21d2007-07-11 10:18:17 -04006256static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006257 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006258{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006259 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006260 struct btrfs_trans_handle *trans;
6261 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006262 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006263 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006264 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006265 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006266
Josef Bacik9ed74f22009-09-11 16:12:44 -04006267 /*
6268 * 2 for inode item and ref
6269 * 2 for dir items
6270 * 1 for xattr if selinux is on
6271 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006272 trans = btrfs_start_transaction(root, 5);
6273 if (IS_ERR(trans))
6274 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006275
Li Zefan581bb052011-04-20 10:06:11 +08006276 err = btrfs_find_free_ino(root, &objectid);
6277 if (err)
6278 goto out_unlock;
6279
Josef Bacikaec74772008-07-24 12:12:38 -04006280 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006281 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6282 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006283 if (IS_ERR(inode)) {
6284 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006285 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006286 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006287 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006288
Casey Schauflerad19db72011-12-15 10:09:07 -05006289 /*
6290 * If the active LSM wants to access the inode during
6291 * d_instantiate it needs these. Smack checks to see
6292 * if the filesystem supports xattrs by looking at the
6293 * ops vector.
6294 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006295 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006296 init_special_inode(inode, inode->i_mode, rdev);
6297
6298 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006299 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006300 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006301
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006302 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6303 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006304 if (err)
6305 goto out_unlock;
6306
6307 btrfs_update_inode(trans, root, inode);
6308 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006309
Josef Bacik618e21d2007-07-11 10:18:17 -04006310out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006311 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006312 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006313 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006314 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006315 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006316 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006317 return err;
6318}
6319
Chris Mason39279cc2007-06-12 06:35:45 -04006320static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006321 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006322{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006323 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006324 struct btrfs_trans_handle *trans;
6325 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006326 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006327 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006328 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006329 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006330
Josef Bacik9ed74f22009-09-11 16:12:44 -04006331 /*
6332 * 2 for inode item and ref
6333 * 2 for dir items
6334 * 1 for xattr if selinux is on
6335 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006336 trans = btrfs_start_transaction(root, 5);
6337 if (IS_ERR(trans))
6338 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006339
Li Zefan581bb052011-04-20 10:06:11 +08006340 err = btrfs_find_free_ino(root, &objectid);
6341 if (err)
6342 goto out_unlock;
6343
Josef Bacikaec74772008-07-24 12:12:38 -04006344 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006345 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6346 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006347 if (IS_ERR(inode)) {
6348 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006349 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006350 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006351 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006352 /*
6353 * If the active LSM wants to access the inode during
6354 * d_instantiate it needs these. Smack checks to see
6355 * if the filesystem supports xattrs by looking at the
6356 * ops vector.
6357 */
6358 inode->i_fop = &btrfs_file_operations;
6359 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006360 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006361
6362 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6363 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006364 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006365
6366 err = btrfs_update_inode(trans, root, inode);
6367 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006368 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006369
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006370 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6371 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006372 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006373 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006374
Al Viro1e2e5472018-05-04 08:23:01 -04006375 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006376
Chris Mason39279cc2007-06-12 06:35:45 -04006377out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006378 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006379 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006380 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006381 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006382 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006383 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006384 return err;
6385}
6386
6387static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6388 struct dentry *dentry)
6389{
Filipe Manana271dba452016-01-05 16:24:05 +00006390 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006391 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006392 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006393 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006394 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006395 int err;
6396 int drop_inode = 0;
6397
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006398 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006399 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006400 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006401
Mark Fashehf1863732012-08-08 11:32:27 -07006402 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006403 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006404
Nikolay Borisov877574e2017-02-20 13:50:33 +02006405 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006406 if (err)
6407 goto fail;
6408
Yan, Zhenga22285a2010-05-16 10:48:46 -04006409 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006410 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006411 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006412 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006413 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006414 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006415 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006416 if (IS_ERR(trans)) {
6417 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006418 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006419 goto fail;
6420 }
Chris Mason5f39d392007-10-15 16:14:19 -04006421
Miao Xie67de1172013-12-26 13:07:06 +08006422 /* There are several dir indexes for this inode, clear the cache. */
6423 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006424 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006425 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006426 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006427 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006428 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006429
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006430 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6431 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006432
Yan, Zhenga5719522009-09-24 09:17:31 -04006433 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006434 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006435 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006436 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006437
Yan, Zhenga5719522009-09-24 09:17:31 -04006438 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006439 if (err)
6440 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006441 if (inode->i_nlink == 1) {
6442 /*
6443 * If new hard link count is 1, it's a file created
6444 * with open(2) O_TMPFILE flag.
6445 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006446 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006447 if (err)
6448 goto fail;
6449 }
Al Viro08c422c2011-12-23 07:58:13 -05006450 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006451 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006452 }
Chris Mason39279cc2007-06-12 06:35:45 -04006453
Chris Mason1832a6d2007-12-21 16:27:21 -05006454fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006455 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006456 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006457 if (drop_inode) {
6458 inode_dec_link_count(inode);
6459 iput(inode);
6460 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006461 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006462 return err;
6463}
6464
Al Viro18bb1db2011-07-26 01:41:39 -04006465static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006466{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006467 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006468 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006469 struct btrfs_trans_handle *trans;
6470 struct btrfs_root *root = BTRFS_I(dir)->root;
6471 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006472 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006473 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006474
Josef Bacik9ed74f22009-09-11 16:12:44 -04006475 /*
6476 * 2 items for inode and ref
6477 * 2 items for dir items
6478 * 1 for xattr if selinux is on
6479 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006480 trans = btrfs_start_transaction(root, 5);
6481 if (IS_ERR(trans))
6482 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006483
Li Zefan581bb052011-04-20 10:06:11 +08006484 err = btrfs_find_free_ino(root, &objectid);
6485 if (err)
6486 goto out_fail;
6487
Josef Bacikaec74772008-07-24 12:12:38 -04006488 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006489 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6490 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006491 if (IS_ERR(inode)) {
6492 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006493 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006494 goto out_fail;
6495 }
Chris Mason5f39d392007-10-15 16:14:19 -04006496
Chris Masonb0d5d102014-09-08 13:08:51 -07006497 /* these must be set before we unlock the inode */
6498 inode->i_op = &btrfs_dir_inode_operations;
6499 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006500
Eric Paris2a7dba32011-02-01 11:05:39 -05006501 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006502 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006503 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006504
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006505 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006506 err = btrfs_update_inode(trans, root, inode);
6507 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006508 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006509
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006510 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6511 dentry->d_name.name,
6512 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006513 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006514 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006515
Al Viro1e2e5472018-05-04 08:23:01 -04006516 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006517
6518out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006519 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006520 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006521 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006522 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006523 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006524 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006525 return err;
6526}
6527
Chris Masonc8b97812008-10-29 14:49:59 -04006528static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006529 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006530 size_t pg_offset, u64 extent_offset,
6531 struct btrfs_file_extent_item *item)
6532{
6533 int ret;
6534 struct extent_buffer *leaf = path->nodes[0];
6535 char *tmp;
6536 size_t max_size;
6537 unsigned long inline_size;
6538 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006539 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006540
6541 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006542 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006543 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6544 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006545 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006546 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006547 if (!tmp)
6548 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006549 ptr = btrfs_file_extent_inline_start(item);
6550
6551 read_extent_buffer(leaf, tmp, ptr, inline_size);
6552
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006553 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006554 ret = btrfs_decompress(compress_type, tmp, page,
6555 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006556
6557 /*
6558 * decompression code contains a memset to fill in any space between the end
6559 * of the uncompressed data and the end of max_size in case the decompressed
6560 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6561 * the end of an inline extent and the beginning of the next block, so we
6562 * cover that region here.
6563 */
6564
6565 if (max_size + pg_offset < PAGE_SIZE) {
6566 char *map = kmap(page);
6567 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6568 kunmap(page);
6569 }
Chris Masonc8b97812008-10-29 14:49:59 -04006570 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006571 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006572}
6573
Omar Sandoval39b07b52019-12-02 17:34:23 -08006574/**
6575 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6576 * @inode: file to search in
6577 * @page: page to read extent data into if the extent is inline
6578 * @pg_offset: offset into @page to copy to
6579 * @start: file offset
6580 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006581 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006582 * This returns the first &struct extent_map which overlaps with the given
6583 * range, reading it from the B-tree and caching it if necessary. Note that
6584 * there may be more extents which overlap the given range after the returned
6585 * extent_map.
6586 *
6587 * If @page is not NULL and the extent is inline, this also reads the extent
6588 * data directly into the page and marks the extent up to date in the io_tree.
6589 *
6590 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006591 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006592struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006593 struct page *page, size_t pg_offset,
6594 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006595{
David Sterba3ffbd682018-06-29 10:56:42 +02006596 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006597 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006598 u64 extent_start = 0;
6599 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006600 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006601 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006602 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006603 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006604 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006605 struct extent_buffer *leaf;
6606 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006607 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006608 struct extent_map_tree *em_tree = &inode->extent_tree;
6609 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006610
Chris Mason890871b2009-09-02 16:24:52 -04006611 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006612 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006613 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006614
Chris Masona52d9a82007-08-27 16:49:44 -04006615 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006616 if (em->start > start || em->start + em->len <= start)
6617 free_extent_map(em);
6618 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006619 free_extent_map(em);
6620 else
6621 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006622 }
David Sterba172ddd62011-04-21 00:48:27 +02006623 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006624 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006625 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006626 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006627 }
Chris Masond1310b22008-01-24 16:13:08 -05006628 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006629 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006630 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006631 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006632
Liu Bobee6ec82018-08-17 05:05:28 +08006633 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006634 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006635 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006636 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006637 }
6638
Liu Bobee6ec82018-08-17 05:05:28 +08006639 /* Chances are we'll be called again, so go ahead and do readahead */
6640 path->reada = READA_FORWARD;
6641
Liu Boe49aabd2018-08-25 13:47:09 +08006642 /*
6643 * Unless we're going to uncompress the inline extent, no sleep would
6644 * happen.
6645 */
6646 path->leave_spinning = 1;
6647
Josef Bacik51899412020-08-20 11:46:01 -04006648 path->recurse = btrfs_is_free_space_inode(inode);
6649
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006650 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006651 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006652 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006653 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006654 if (path->slots[0] == 0)
6655 goto not_found;
6656 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006657 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006658 }
6659
Chris Mason5f39d392007-10-15 16:14:19 -04006660 leaf = path->nodes[0];
6661 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006662 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006663 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006664 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006665 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006666 /*
6667 * If we backup past the first extent we want to move forward
6668 * and see if there is an extent in front of us, otherwise we'll
6669 * say there is a hole for our whole search range which can
6670 * cause problems.
6671 */
6672 extent_end = start;
6673 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006674 }
6675
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006676 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006677 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006678 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006679 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6680 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006681 /* Only regular file could have regular/prealloc extent */
6682 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006683 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006684 btrfs_crit(fs_info,
6685 "regular/prealloc extent found for non-regular inode %llu",
6686 btrfs_ino(inode));
6687 goto out;
6688 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006689 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6690 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006691 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006692 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6693 path->slots[0],
6694 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006695 }
Josef Bacik25a50342013-10-14 12:08:38 -04006696next:
Yan Zheng9036c102008-10-30 14:19:41 -04006697 if (start >= extent_end) {
6698 path->slots[0]++;
6699 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6700 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006701 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006702 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006703 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006704 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006705
Yan Zheng9036c102008-10-30 14:19:41 -04006706 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006707 }
Yan Zheng9036c102008-10-30 14:19:41 -04006708 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6709 if (found_key.objectid != objectid ||
6710 found_key.type != BTRFS_EXTENT_DATA_KEY)
6711 goto not_found;
6712 if (start + len <= found_key.offset)
6713 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006714 if (start > found_key.offset)
6715 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006716
6717 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006718 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006719 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006720 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006721 em->block_start = EXTENT_MAP_HOLE;
6722 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006723 }
6724
Omar Sandoval39b07b52019-12-02 17:34:23 -08006725 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006726
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006727 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6728 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006729 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006730 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006731 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006732 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006733 size_t size;
6734 size_t extent_offset;
6735 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006736
Omar Sandoval39b07b52019-12-02 17:34:23 -08006737 if (!page)
Yan689f9342007-10-29 11:41:07 -04006738 goto out;
Yan689f9342007-10-29 11:41:07 -04006739
Qu Wenruoe41ca582018-06-06 15:41:49 +08006740 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006741 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006742 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6743 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006744 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006745 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006746 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006747 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006748 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006749
6750 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006751 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006752 if (btrfs_file_extent_compression(leaf, item) !=
6753 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006754 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006755 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006756 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04006757 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04006758 } else {
6759 map = kmap(page);
6760 read_extent_buffer(leaf, map + pg_offset, ptr,
6761 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006762 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006763 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006764 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006765 copy_size);
6766 }
Chris Masonc8b97812008-10-29 14:49:59 -04006767 kunmap(page);
6768 }
Chris Mason179e29e2007-11-01 11:28:41 -04006769 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006770 }
Chris Masond1310b22008-01-24 16:13:08 -05006771 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006772 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006773 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006774 }
6775not_found:
6776 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006777 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006778 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006779 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006780insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006781 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02006782 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006783 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006784 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006785 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6786 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006787 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04006788 goto out;
6789 }
Chris Masond1310b22008-01-24 16:13:08 -05006790
Chris Mason890871b2009-09-02 16:24:52 -04006791 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006792 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006793 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006794out:
Liu Boc6414282018-08-23 07:36:17 +08006795 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006796
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006797 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006798
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006799 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04006800 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006801 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04006802 }
6803 return em;
6804}
6805
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006806struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006807 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006808{
6809 struct extent_map *em;
6810 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006811 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006812 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006813 u64 delalloc_len;
6814 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006815 int err = 0;
6816
Omar Sandoval39b07b52019-12-02 17:34:23 -08006817 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006818 if (IS_ERR(em))
6819 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006820 /*
6821 * If our em maps to:
6822 * - a hole or
6823 * - a pre-alloc extent,
6824 * there might actually be delalloc bytes behind it.
6825 */
6826 if (em->block_start != EXTENT_MAP_HOLE &&
6827 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6828 return em;
6829 else
6830 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006831
6832 /* check to see if we've wrapped (len == -1 or similar) */
6833 end = start + len;
6834 if (end < start)
6835 end = (u64)-1;
6836 else
6837 end -= 1;
6838
6839 em = NULL;
6840
6841 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006842 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006843 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006844 delalloc_end = delalloc_start + delalloc_len;
6845 if (delalloc_end < delalloc_start)
6846 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006847
6848 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006849 * We didn't find anything useful, return the original results from
6850 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006851 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006852 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006853 em = hole_em;
6854 hole_em = NULL;
6855 goto out;
6856 }
6857
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006858 /*
6859 * Adjust the delalloc_start to make sure it doesn't go backwards from
6860 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006861 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006862 delalloc_start = max(start, delalloc_start);
6863 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006864
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006865 if (delalloc_len > 0) {
6866 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006867 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006868 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006869
David Sterba172ddd62011-04-21 00:48:27 +02006870 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006871 if (!em) {
6872 err = -ENOMEM;
6873 goto out;
6874 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006875
6876 ASSERT(hole_em);
6877 /*
6878 * When btrfs_get_extent can't find anything it returns one
6879 * huge hole
6880 *
6881 * Make sure what it found really fits our range, and adjust to
6882 * make sure it is based on the start from the caller
6883 */
6884 if (hole_end <= start || hole_em->start > end) {
6885 free_extent_map(hole_em);
6886 hole_em = NULL;
6887 } else {
6888 hole_start = max(hole_em->start, start);
6889 hole_len = hole_end - hole_start;
6890 }
6891
6892 if (hole_em && delalloc_start > hole_start) {
6893 /*
6894 * Our hole starts before our delalloc, so we have to
6895 * return just the parts of the hole that go until the
6896 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006897 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006898 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006899 em->start = hole_start;
6900 em->orig_start = hole_start;
6901 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006902 * Don't adjust block start at all, it is fixed at
6903 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006904 */
6905 em->block_start = hole_em->block_start;
6906 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006907 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6908 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006909 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006910 /*
6911 * Hole is out of passed range or it starts after
6912 * delalloc range
6913 */
6914 em->start = delalloc_start;
6915 em->len = delalloc_len;
6916 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006917 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006918 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006919 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006920 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006921 return hole_em;
6922 }
6923out:
6924
6925 free_extent_map(hole_em);
6926 if (err) {
6927 free_extent_map(em);
6928 return ERR_PTR(err);
6929 }
6930 return em;
6931}
6932
Nikolay Borisov64f54182020-06-03 08:55:31 +03006933static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006934 const u64 start,
6935 const u64 len,
6936 const u64 orig_start,
6937 const u64 block_start,
6938 const u64 block_len,
6939 const u64 orig_block_len,
6940 const u64 ram_bytes,
6941 const int type)
6942{
6943 struct extent_map *em = NULL;
6944 int ret;
6945
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006946 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03006947 em = create_io_em(inode, start, len, orig_start, block_start,
6948 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08006949 BTRFS_COMPRESS_NONE, /* compress_type */
6950 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006951 if (IS_ERR(em))
6952 goto out;
6953 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03006954 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
6955 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006956 if (ret) {
6957 if (em) {
6958 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03006959 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006960 }
6961 em = ERR_PTR(ret);
6962 }
6963 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006964
6965 return em;
6966}
6967
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006968static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006969 u64 start, u64 len)
6970{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006971 struct btrfs_root *root = inode->root;
6972 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04006973 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006974 struct btrfs_key ins;
6975 u64 alloc_hint;
6976 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006977
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006978 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006979 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006980 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006981 if (ret)
6982 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006983
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006984 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006985 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006986 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006987 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006988 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006989 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
6990 1);
Josef Bacik00361582013-08-14 14:02:47 -04006991
Josef Bacik4b46fce2010-05-23 11:00:55 -04006992 return em;
6993}
6994
Chris Mason46bfbb52010-05-26 11:04:10 -04006995/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006996 * Check if we can do nocow write into the range [@offset, @offset + @len)
6997 *
6998 * @offset: File offset
6999 * @len: The length to write, will be updated to the nocow writeable
7000 * range
7001 * @orig_start: (optional) Return the original file offset of the file extent
7002 * @orig_len: (optional) Return the original on-disk length of the file extent
7003 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007004 * @strict: if true, omit optimizations that might force us into unnecessary
7005 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007006 *
7007 * This function will flush ordered extents in the range to ensure proper
7008 * nocow checks for (nowait == false) case.
7009 *
7010 * Return:
7011 * >0 and update @len if we can do nocow write
7012 * 0 if we can't do nocow write
7013 * <0 if error happened
7014 *
7015 * NOTE: This only checks the file extents, caller is responsible to wait for
7016 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007017 */
Josef Bacik00361582013-08-14 14:02:47 -04007018noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007019 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007020 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007021{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007022 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007023 struct btrfs_path *path;
7024 int ret;
7025 struct extent_buffer *leaf;
7026 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007027 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007028 struct btrfs_file_extent_item *fi;
7029 struct btrfs_key key;
7030 u64 disk_bytenr;
7031 u64 backref_offset;
7032 u64 extent_end;
7033 u64 num_bytes;
7034 int slot;
7035 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007036 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007037
Chris Mason46bfbb52010-05-26 11:04:10 -04007038 path = btrfs_alloc_path();
7039 if (!path)
7040 return -ENOMEM;
7041
David Sterbaf85b7372017-01-20 14:54:07 +01007042 ret = btrfs_lookup_file_extent(NULL, root, path,
7043 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007044 if (ret < 0)
7045 goto out;
7046
7047 slot = path->slots[0];
7048 if (ret == 1) {
7049 if (slot == 0) {
7050 /* can't find the item, must cow */
7051 ret = 0;
7052 goto out;
7053 }
7054 slot--;
7055 }
7056 ret = 0;
7057 leaf = path->nodes[0];
7058 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007059 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007060 key.type != BTRFS_EXTENT_DATA_KEY) {
7061 /* not our file or wrong item type, must cow */
7062 goto out;
7063 }
7064
7065 if (key.offset > offset) {
7066 /* Wrong offset, must cow */
7067 goto out;
7068 }
7069
7070 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7071 found_type = btrfs_file_extent_type(leaf, fi);
7072 if (found_type != BTRFS_FILE_EXTENT_REG &&
7073 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7074 /* not a regular extent, must cow */
7075 goto out;
7076 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007077
7078 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7079 goto out;
7080
Miao Xiee77751a2013-12-27 21:11:50 +08007081 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7082 if (extent_end <= offset)
7083 goto out;
7084
Chris Mason46bfbb52010-05-26 11:04:10 -04007085 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007086 if (disk_bytenr == 0)
7087 goto out;
7088
7089 if (btrfs_file_extent_compression(leaf, fi) ||
7090 btrfs_file_extent_encryption(leaf, fi) ||
7091 btrfs_file_extent_other_encoding(leaf, fi))
7092 goto out;
7093
Ethan Lien78d42952018-05-17 14:58:29 +08007094 /*
7095 * Do the same check as in btrfs_cross_ref_exist but without the
7096 * unnecessary search.
7097 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007098 if (!strict &&
7099 (btrfs_file_extent_generation(leaf, fi) <=
7100 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007101 goto out;
7102
Chris Mason46bfbb52010-05-26 11:04:10 -04007103 backref_offset = btrfs_file_extent_offset(leaf, fi);
7104
Josef Bacik7ee9e442013-06-21 16:37:03 -04007105 if (orig_start) {
7106 *orig_start = key.offset - backref_offset;
7107 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7108 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7109 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007110
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007111 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007112 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007113
7114 num_bytes = min(offset + *len, extent_end) - offset;
7115 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7116 u64 range_end;
7117
Jeff Mahoneyda170662016-06-15 09:22:56 -04007118 range_end = round_up(offset + num_bytes,
7119 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007120 ret = test_range_bit(io_tree, offset, range_end,
7121 EXTENT_DELALLOC, 0, NULL);
7122 if (ret) {
7123 ret = -EAGAIN;
7124 goto out;
7125 }
7126 }
7127
Josef Bacik1bda19e2013-10-18 12:10:36 -04007128 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007129
7130 /*
7131 * look for other files referencing this extent, if we
7132 * find any we must cow
7133 */
Josef Bacik00361582013-08-14 14:02:47 -04007134
Liu Boe4c3b2d2017-01-30 12:25:28 -08007135 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007136 key.offset - backref_offset, disk_bytenr,
7137 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007138 if (ret) {
7139 ret = 0;
7140 goto out;
7141 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007142
7143 /*
7144 * adjust disk_bytenr and num_bytes to cover just the bytes
7145 * in this extent we are about to write. If there
7146 * are any csums in that range we have to cow in order
7147 * to keep the csums correct
7148 */
7149 disk_bytenr += backref_offset;
7150 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007151 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7152 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007153 /*
7154 * all of the above have passed, it is safe to overwrite this extent
7155 * without cow
7156 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007157 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007158 ret = 1;
7159out:
7160 btrfs_free_path(path);
7161 return ret;
7162}
7163
Josef Bacikeb838e72012-07-31 16:28:48 -04007164static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007165 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007166{
7167 struct btrfs_ordered_extent *ordered;
7168 int ret = 0;
7169
7170 while (1) {
7171 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007172 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007173 /*
7174 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007175 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007176 * extents in this range.
7177 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007178 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007179 lockend - lockstart + 1);
7180
7181 /*
7182 * We need to make sure there are no buffered pages in this
7183 * range either, we could have raced between the invalidate in
7184 * generic_file_direct_write and locking the extent. The
7185 * invalidate needs to happen so that reads after a write do not
7186 * get stale data.
7187 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007188 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007189 (!writing || !filemap_range_has_page(inode->i_mapping,
7190 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007191 break;
7192
7193 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007194 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007195
7196 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007197 /*
7198 * If we are doing a DIO read and the ordered extent we
7199 * found is for a buffered write, we can not wait for it
7200 * to complete and retry, because if we do so we can
7201 * deadlock with concurrent buffered writes on page
7202 * locks. This happens only if our DIO read covers more
7203 * than one extent map, if at this point has already
7204 * created an ordered extent for a previous extent map
7205 * and locked its range in the inode's io tree, and a
7206 * concurrent write against that previous extent map's
7207 * range and this range started (we unlock the ranges
7208 * in the io tree only when the bios complete and
7209 * buffered writes always lock pages before attempting
7210 * to lock range in the io tree).
7211 */
7212 if (writing ||
7213 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007214 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007215 else
7216 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007217 btrfs_put_ordered_extent(ordered);
7218 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007219 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007220 * We could trigger writeback for this range (and wait
7221 * for it to complete) and then invalidate the pages for
7222 * this range (through invalidate_inode_pages2_range()),
7223 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007224 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007225 * triggered a readahead) on a page lock due to an
7226 * ordered dio extent we created before but did not have
7227 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007228 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007229 * ordered extent to complete while holding a lock on
7230 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007231 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007232 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007233 }
7234
Filipe Mananaade77022016-02-18 14:28:55 +00007235 if (ret)
7236 break;
7237
Josef Bacikeb838e72012-07-31 16:28:48 -04007238 cond_resched();
7239 }
7240
7241 return ret;
7242}
7243
Liu Bo6f9994d2017-01-31 07:50:22 -08007244/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007245static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7246 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007247 u64 block_len, u64 orig_block_len,
7248 u64 ram_bytes, int compress_type,
7249 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007250{
7251 struct extent_map_tree *em_tree;
7252 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007253 int ret;
7254
Liu Bo6f9994d2017-01-31 07:50:22 -08007255 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7256 type == BTRFS_ORDERED_COMPRESSED ||
7257 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007258 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007259
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007260 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007261 em = alloc_extent_map();
7262 if (!em)
7263 return ERR_PTR(-ENOMEM);
7264
7265 em->start = start;
7266 em->orig_start = orig_start;
7267 em->len = len;
7268 em->block_len = block_len;
7269 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007270 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007271 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007272 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007273 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007274 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007275 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007276 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007277 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7278 em->compress_type = compress_type;
7279 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007280
7281 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007282 btrfs_drop_extent_cache(inode, em->start,
7283 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007284 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007285 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007286 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007287 /*
7288 * The caller has taken lock_extent(), who could race with us
7289 * to add em?
7290 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007291 } while (ret == -EEXIST);
7292
7293 if (ret) {
7294 free_extent_map(em);
7295 return ERR_PTR(ret);
7296 }
7297
Liu Bo6f9994d2017-01-31 07:50:22 -08007298 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007299 return em;
7300}
7301
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007302
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007303static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007304 struct inode *inode,
7305 struct btrfs_dio_data *dio_data,
7306 u64 start, u64 len)
7307{
7308 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7309 struct extent_map *em = *map;
7310 int ret = 0;
7311
7312 /*
7313 * We don't allocate a new extent in the following cases
7314 *
7315 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7316 * existing extent.
7317 * 2) The extent is marked as PREALLOC. We're good to go here and can
7318 * just use the extent.
7319 *
7320 */
7321 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7322 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7323 em->block_start != EXTENT_MAP_HOLE)) {
7324 int type;
7325 u64 block_start, orig_start, orig_block_len, ram_bytes;
7326
7327 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7328 type = BTRFS_ORDERED_PREALLOC;
7329 else
7330 type = BTRFS_ORDERED_NOCOW;
7331 len = min(len, em->len - (start - em->start));
7332 block_start = em->block_start + (start - em->start);
7333
7334 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007335 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007336 btrfs_inc_nocow_writers(fs_info, block_start)) {
7337 struct extent_map *em2;
7338
Nikolay Borisov64f54182020-06-03 08:55:31 +03007339 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007340 orig_start, block_start,
7341 len, orig_block_len,
7342 ram_bytes, type);
7343 btrfs_dec_nocow_writers(fs_info, block_start);
7344 if (type == BTRFS_ORDERED_PREALLOC) {
7345 free_extent_map(em);
7346 *map = em = em2;
7347 }
7348
7349 if (em2 && IS_ERR(em2)) {
7350 ret = PTR_ERR(em2);
7351 goto out;
7352 }
7353 /*
7354 * For inode marked NODATACOW or extent marked PREALLOC,
7355 * use the existing or preallocated extent, so does not
7356 * need to adjust btrfs_space_info's bytes_may_use.
7357 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007358 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007359 goto skip_cow;
7360 }
7361 }
7362
7363 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007364 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007365 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007366 if (IS_ERR(em)) {
7367 ret = PTR_ERR(em);
7368 goto out;
7369 }
7370
7371 len = min(len, em->len - (start - em->start));
7372
7373skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007374 /*
7375 * Need to update the i_size under the extent lock so buffered
7376 * readers will get the updated i_size when we unlock.
7377 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007378 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007379 i_size_write(inode, start + len);
7380
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007381 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007382out:
7383 return ret;
7384}
7385
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007386static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7387 loff_t length, unsigned int flags, struct iomap *iomap,
7388 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007389{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007390 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007391 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007392 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307393 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007394 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007395 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007396 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007397 u64 len = length;
7398 bool unlock_extents = false;
Josef Bacik0eb79292020-09-03 11:16:51 -04007399 bool sync = (current->journal_info == BTRFS_DIO_SYNC_STUB);
7400
7401 /*
7402 * We used current->journal_info here to see if we were sync, but
7403 * there's a lot of tests in the enospc machinery to not do flushing if
7404 * we have a journal_info set, so we need to clear this out and re-set
7405 * it in iomap_end.
7406 */
7407 ASSERT(current->journal_info == NULL ||
7408 current->journal_info == BTRFS_DIO_SYNC_STUB);
7409 current->journal_info = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007410
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007411 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007412 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007413
Josef Bacikc3298612012-08-03 16:49:19 -04007414 lockstart = start;
7415 lockend = start + len - 1;
7416
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007417 /*
7418 * The generic stuff only does filemap_write_and_wait_range, which
7419 * isn't enough if we've written compressed pages to this area, so we
7420 * need to flush the dirty pages again to make absolutely sure that any
7421 * outstanding dirty pages are on disk.
7422 */
7423 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7424 &BTRFS_I(inode)->runtime_flags)) {
7425 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7426 start + length - 1);
7427 if (ret)
7428 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007429 }
7430
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007431 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7432 if (!dio_data)
7433 return -ENOMEM;
7434
Josef Bacik0eb79292020-09-03 11:16:51 -04007435 dio_data->sync = sync;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007436 dio_data->length = length;
7437 if (write) {
7438 dio_data->reserve = round_up(length, fs_info->sectorsize);
7439 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7440 &dio_data->data_reserved,
7441 start, dio_data->reserve);
7442 if (ret) {
7443 extent_changeset_free(dio_data->data_reserved);
7444 kfree(dio_data);
7445 return ret;
7446 }
7447 }
7448 iomap->private = dio_data;
7449
7450
Josef Bacikeb838e72012-07-31 16:28:48 -04007451 /*
7452 * If this errors out it's because we couldn't invalidate pagecache for
7453 * this range and we need to fallback to buffered.
7454 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007455 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007456 ret = -ENOTBLK;
7457 goto err;
7458 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007459
Omar Sandoval39b07b52019-12-02 17:34:23 -08007460 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007461 if (IS_ERR(em)) {
7462 ret = PTR_ERR(em);
7463 goto unlock_err;
7464 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007465
7466 /*
7467 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7468 * io. INLINE is special, and we could probably kludge it in here, but
7469 * it's still buffered so for safety lets just fall back to the generic
7470 * buffered path.
7471 *
7472 * For COMPRESSED we _have_ to read the entire extent in so we can
7473 * decompress it, so there will be buffering required no matter what we
7474 * do, so go ahead and fallback to buffered.
7475 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007476 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007477 * to buffered IO. Don't blame me, this is the price we pay for using
7478 * the generic code.
7479 */
7480 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7481 em->block_start == EXTENT_MAP_INLINE) {
7482 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007483 ret = -ENOTBLK;
7484 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007485 }
7486
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007487 len = min(len, em->len - (start - em->start));
7488 if (write) {
7489 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7490 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007491 if (ret < 0)
7492 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007493 unlock_extents = true;
7494 /* Recalc len in case the new em is smaller than requested */
7495 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007496 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007497 /*
7498 * We need to unlock only the end area that we aren't using.
7499 * The rest is going to be unlocked by the endio routine.
7500 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007501 lockstart = start + len;
7502 if (lockstart < lockend)
7503 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007504 }
7505
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007506 if (unlock_extents)
7507 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7508 lockstart, lockend, &cached_state);
7509 else
7510 free_extent_state(cached_state);
7511
7512 /*
7513 * Translate extent map information to iomap.
7514 * We trim the extents (and move the addr) even though iomap code does
7515 * that, since we have locked only the parts we are performing I/O in.
7516 */
7517 if ((em->block_start == EXTENT_MAP_HOLE) ||
7518 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7519 iomap->addr = IOMAP_NULL_ADDR;
7520 iomap->type = IOMAP_HOLE;
7521 } else {
7522 iomap->addr = em->block_start + (start - em->start);
7523 iomap->type = IOMAP_MAPPED;
7524 }
7525 iomap->offset = start;
7526 iomap->bdev = fs_info->fs_devices->latest_bdev;
7527 iomap->length = len;
7528
Josef Bacik4b46fce2010-05-23 11:00:55 -04007529 free_extent_map(em);
7530
7531 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007532
7533unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007534 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7535 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007536err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007537 if (dio_data) {
7538 btrfs_delalloc_release_space(BTRFS_I(inode),
7539 dio_data->data_reserved, start,
7540 dio_data->reserve, true);
7541 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7542 extent_changeset_free(dio_data->data_reserved);
7543 kfree(dio_data);
7544 }
7545 return ret;
7546}
7547
7548static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7549 ssize_t written, unsigned int flags, struct iomap *iomap)
7550{
7551 int ret = 0;
7552 struct btrfs_dio_data *dio_data = iomap->private;
7553 size_t submitted = dio_data->submitted;
7554 const bool write = !!(flags & IOMAP_WRITE);
7555
7556 if (!write && (iomap->type == IOMAP_HOLE)) {
7557 /* If reading from a hole, unlock and return */
7558 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7559 goto out;
7560 }
7561
7562 if (submitted < length) {
7563 pos += submitted;
7564 length -= submitted;
7565 if (write)
7566 __endio_write_update_ordered(BTRFS_I(inode), pos,
7567 length, false);
7568 else
7569 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7570 pos + length - 1);
7571 ret = -ENOTBLK;
7572 }
7573
7574 if (write) {
7575 if (dio_data->reserve)
7576 btrfs_delalloc_release_space(BTRFS_I(inode),
7577 dio_data->data_reserved, pos,
7578 dio_data->reserve, true);
7579 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7580 extent_changeset_free(dio_data->data_reserved);
7581 }
7582out:
Josef Bacik0eb79292020-09-03 11:16:51 -04007583 /*
7584 * We're all done, we can re-set the current->journal_info now safely
7585 * for our endio.
7586 */
7587 if (dio_data->sync) {
7588 ASSERT(current->journal_info == NULL);
7589 current->journal_info = BTRFS_DIO_SYNC_STUB;
7590 }
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007591 kfree(dio_data);
7592 iomap->private = NULL;
7593
Josef Bacikeb838e72012-07-31 16:28:48 -04007594 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007595}
7596
Omar Sandoval769b4f22020-04-16 14:46:22 -07007597static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7598{
7599 /*
7600 * This implies a barrier so that stores to dio_bio->bi_status before
7601 * this and loads of dio_bio->bi_status after this are fully ordered.
7602 */
7603 if (!refcount_dec_and_test(&dip->refs))
7604 return;
7605
7606 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007607 __endio_write_update_ordered(BTRFS_I(dip->inode),
7608 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007609 dip->bytes,
7610 !dip->dio_bio->bi_status);
7611 } else {
7612 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7613 dip->logical_offset,
7614 dip->logical_offset + dip->bytes - 1);
7615 }
7616
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007617 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007618 kfree(dip);
7619}
7620
Omar Sandoval77d5d682020-04-16 14:46:25 -07007621static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7622 int mirror_num,
7623 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007624{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007625 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007626 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007627 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007628
Mike Christie37226b22016-06-05 14:31:52 -05007629 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007630
Omar Sandoval5c047a62020-04-16 14:46:24 -07007631 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007632 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007633 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007634
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007635 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007636 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007637 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007638 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007639 return ret;
7640}
7641
Omar Sandoval769b4f22020-04-16 14:46:22 -07007642static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7643 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007644 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007645{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007646 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7647 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007648 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007649 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7650 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007651 struct bio_vec bvec;
7652 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007653 u64 start = io_bio->logical;
7654 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007655 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007656
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007657 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7658 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307659
Liu Bo17347ce2017-05-15 15:33:27 -07007660 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7661 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007662 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007663 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007664 if (uptodate &&
7665 (!csum || !check_data_csum(inode, io_bio, icsum,
7666 bvec.bv_page, pgoff,
7667 start, sectorsize))) {
7668 clean_io_failure(fs_info, failure_tree, io_tree,
7669 start, bvec.bv_page,
7670 btrfs_ino(BTRFS_I(inode)),
7671 pgoff);
7672 } else {
7673 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007674
Omar Sandoval77d5d682020-04-16 14:46:25 -07007675 status = btrfs_submit_read_repair(inode,
7676 &io_bio->bio,
7677 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007678 bvec.bv_page, pgoff,
7679 start,
7680 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007681 io_bio->mirror_num,
7682 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007683 if (status)
7684 err = status;
7685 }
7686 start += sectorsize;
7687 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307688 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307689 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007690 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007691 return err;
7692}
7693
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007694static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007695 const u64 offset, const u64 bytes,
7696 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007697{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007698 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007699 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007700 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007701 u64 ordered_offset = offset;
7702 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007703 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007704
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007705 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007706 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007707 else
Qu Wenruo524272602017-03-08 10:25:52 +08007708 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007709
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007710 while (ordered_offset < offset + bytes) {
7711 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007712 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007713 &ordered_offset,
7714 ordered_bytes,
7715 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007716 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7717 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007718 btrfs_queue_work(wq, &ordered->work);
7719 }
7720 /*
7721 * If btrfs_dec_test_ordered_pending does not find any ordered
7722 * extent in the range, we can exit.
7723 */
7724 if (ordered_offset == last_offset)
7725 return;
7726 /*
7727 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007728 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007729 */
7730 if (ordered_offset < offset + bytes) {
7731 ordered_bytes = offset + bytes - ordered_offset;
7732 ordered = NULL;
7733 }
Chris Mason163cf092010-11-28 19:56:33 -05007734 }
Filipe Manana14543772015-11-24 16:23:54 +00007735}
7736
David Sterbad0ee3932018-03-08 14:35:48 +01007737static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007738 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007739{
Josef Bacikc6100a42017-05-05 11:57:13 -04007740 struct inode *inode = private_data;
Johannes Thumshirnc965d642020-07-28 20:25:41 +09007741
7742 return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007743}
7744
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007745static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007746{
7747 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007748 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007749
Miao Xie8b110e32014-09-12 18:44:03 +08007750 if (err)
7751 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007752 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007753 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7754 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007755 (unsigned long long)bio->bi_iter.bi_sector,
7756 bio->bi_iter.bi_size, err);
7757
Omar Sandoval769b4f22020-04-16 14:46:22 -07007758 if (bio_op(bio) == REQ_OP_READ) {
7759 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007760 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007761 }
7762
Omar Sandoval769b4f22020-04-16 14:46:22 -07007763 if (err)
7764 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007765
Miao Xiee65e1532010-11-22 03:04:43 +00007766 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007767 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007768}
7769
David Sterbad0ee3932018-03-08 14:35:48 +01007770static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7771 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007772{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007773 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007774 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007775 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007776 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007777
Liu Bo4c274bc2017-11-01 17:19:27 -06007778 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007779 if (async_submit)
7780 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7781
Josef Bacik5fd02042012-05-02 14:00:54 -04007782 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007783 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007784 if (ret)
7785 goto err;
7786 }
Miao Xiee65e1532010-11-22 03:04:43 +00007787
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007788 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007789 goto map;
7790
7791 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007792 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7793 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007794 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007795 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007796 } else if (write) {
7797 /*
7798 * If we aren't doing async submit, calculate the csum of the
7799 * bio now.
7800 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007801 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007802 if (ret)
7803 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007804 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007805 u64 csum_offset;
7806
7807 csum_offset = file_offset - dip->logical_offset;
7808 csum_offset >>= inode->i_sb->s_blocksize_bits;
7809 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7810 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007811 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007812map:
Chris Mason08635ba2019-07-10 12:28:14 -07007813 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007814err:
Miao Xiee65e1532010-11-22 03:04:43 +00007815 return ret;
7816}
7817
Omar Sandovalc36cac22020-04-16 14:46:13 -07007818/*
7819 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7820 * or ordered extents whether or not we submit any bios.
7821 */
7822static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7823 struct inode *inode,
7824 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007825{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007826 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007827 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7828 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007829 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007830
Omar Sandoval85879572020-04-16 14:46:21 -07007831 dip_size = sizeof(*dip);
7832 if (!write && csum) {
7833 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7834 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7835 size_t nblocks;
7836
7837 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7838 dip_size += csum_size * nblocks;
7839 }
7840
7841 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007842 if (!dip)
7843 return NULL;
7844
Omar Sandovalc36cac22020-04-16 14:46:13 -07007845 dip->inode = inode;
7846 dip->logical_offset = file_offset;
7847 dip->bytes = dio_bio->bi_iter.bi_size;
7848 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007849 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007850 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007851 return dip;
7852}
7853
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007854static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
7855 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007856{
7857 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007858 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007859 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007860 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7861 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007862 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007863 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007864 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007865 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007866 u64 submit_len;
7867 int clone_offset = 0;
7868 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307869 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007870 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007871 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007872 struct btrfs_dio_data *dio_data = iomap->private;
Miao Xiee65e1532010-11-22 03:04:43 +00007873
Omar Sandovalc36cac22020-04-16 14:46:13 -07007874 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7875 if (!dip) {
7876 if (!write) {
7877 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7878 file_offset + dio_bio->bi_iter.bi_size - 1);
7879 }
7880 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007881 bio_endio(dio_bio);
7882 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04007883 }
7884
Omar Sandoval85879572020-04-16 14:46:21 -07007885 if (!write && csum) {
7886 /*
7887 * Load the csums up front to reduce csum tree searches and
7888 * contention when submitting bios.
7889 */
7890 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7891 dip->csums);
7892 if (status != BLK_STS_OK)
7893 goto out_err;
7894 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007895
Omar Sandoval769b4f22020-04-16 14:46:22 -07007896 start_sector = dio_bio->bi_iter.bi_sector;
7897 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007898
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007899 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007900 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7901 start_sector << 9, submit_len,
7902 &geom);
7903 if (ret) {
7904 status = errno_to_blk_status(ret);
7905 goto out_err;
7906 }
7907 ASSERT(geom.len <= INT_MAX);
7908
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007909 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007910
Liu Bo725130b2017-05-16 09:51:39 -07007911 /*
7912 * This will never fail as it's passing GPF_NOFS and
7913 * the allocation is backed by btrfs_bioset.
7914 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007915 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007916 bio->bi_private = dip;
7917 bio->bi_end_io = btrfs_end_dio_bio;
7918 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007919
Liu Bo725130b2017-05-16 09:51:39 -07007920 ASSERT(submit_len >= clone_len);
7921 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007922
Liu Bo725130b2017-05-16 09:51:39 -07007923 /*
7924 * Increase the count before we submit the bio so we know
7925 * the end IO handler won't happen before we increase the
7926 * count. Otherwise, the dip might get freed before we're
7927 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007928 *
7929 * We transfer the initial reference to the last bio, so we
7930 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007931 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007932 if (submit_len > 0) {
7933 refcount_inc(&dip->refs);
7934 /*
7935 * If we are submitting more than one bio, submit them
7936 * all asynchronously. The exception is RAID 5 or 6, as
7937 * asynchronous checksums make it difficult to collect
7938 * full stripe writes.
7939 */
7940 if (!raid56)
7941 async_submit = 1;
7942 }
Miao Xiee65e1532010-11-22 03:04:43 +00007943
David Sterbad0ee3932018-03-08 14:35:48 +01007944 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007945 async_submit);
7946 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007947 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007948 if (submit_len > 0)
7949 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007950 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007951 }
Liu Bo725130b2017-05-16 09:51:39 -07007952
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007953 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07007954 clone_offset += clone_len;
7955 start_sector += clone_len >> 9;
7956 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007957 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007958 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00007959
Miao Xiee65e1532010-11-22 03:04:43 +00007960out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007961 dip->dio_bio->bi_status = status;
7962 btrfs_dio_private_put(dip);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007963 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00007964}
7965
David Sterbaf4c48b42020-06-09 19:19:27 +02007966static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7967 const struct iov_iter *iter, loff_t offset)
7968{
7969 int seg;
7970 int i;
7971 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7972 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007973
David Sterbaf4c48b42020-06-09 19:19:27 +02007974 if (offset & blocksize_mask)
7975 goto out;
7976
7977 if (iov_iter_alignment(iter) & blocksize_mask)
7978 goto out;
7979
7980 /* If this is a write we don't need to check anymore */
7981 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7982 return 0;
7983 /*
7984 * Check to make sure we don't have duplicate iov_base's in this
7985 * iovec, if so return EINVAL, otherwise we'll get csum errors
7986 * when reading back.
7987 */
7988 for (seg = 0; seg < iter->nr_segs; seg++) {
7989 for (i = seg + 1; i < iter->nr_segs; i++) {
7990 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7991 goto out;
7992 }
7993 }
7994 retval = 0;
7995out:
7996 return retval;
7997}
7998
Josef Bacik0eb79292020-09-03 11:16:51 -04007999static inline int btrfs_maybe_fsync_end_io(struct kiocb *iocb, ssize_t size,
8000 int error, unsigned flags)
8001{
8002 /*
8003 * Now if we're still in the context of our submitter we know we can't
8004 * safely run generic_write_sync(), so clear our flag here so that the
8005 * caller knows to follow up with a sync.
8006 */
8007 if (current->journal_info == BTRFS_DIO_SYNC_STUB) {
8008 current->journal_info = NULL;
8009 return error;
8010 }
8011
8012 if (error)
8013 return error;
8014
8015 if (size) {
8016 iocb->ki_flags |= IOCB_DSYNC;
8017 return generic_write_sync(iocb, size);
8018 }
8019
8020 return 0;
8021}
8022
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008023static const struct iomap_ops btrfs_dio_iomap_ops = {
8024 .iomap_begin = btrfs_dio_iomap_begin,
8025 .iomap_end = btrfs_dio_iomap_end,
8026};
8027
8028static const struct iomap_dio_ops btrfs_dio_ops = {
8029 .submit_io = btrfs_submit_direct,
8030};
8031
Josef Bacik0eb79292020-09-03 11:16:51 -04008032static const struct iomap_dio_ops btrfs_sync_dops = {
8033 .submit_io = btrfs_submit_direct,
8034 .end_io = btrfs_maybe_fsync_end_io,
8035};
8036
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008037ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02008038{
8039 struct file *file = iocb->ki_filp;
8040 struct inode *inode = file->f_mapping->host;
8041 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8042 struct extent_changeset *data_reserved = NULL;
8043 loff_t offset = iocb->ki_pos;
8044 size_t count = 0;
8045 bool relock = false;
8046 ssize_t ret;
8047
Filipe Mananaa6703c72021-02-16 14:40:31 +00008048 if (check_direct_IO(fs_info, iter, offset)) {
8049 ASSERT(current->journal_info == NULL ||
8050 current->journal_info == BTRFS_DIO_SYNC_STUB);
8051 current->journal_info = NULL;
David Sterbaf4c48b42020-06-09 19:19:27 +02008052 return 0;
Filipe Mananaa6703c72021-02-16 14:40:31 +00008053 }
David Sterbaf4c48b42020-06-09 19:19:27 +02008054
8055 count = iov_iter_count(iter);
8056 if (iov_iter_rw(iter) == WRITE) {
8057 /*
8058 * If the write DIO is beyond the EOF, we need update
8059 * the isize, but it is protected by i_mutex. So we can
8060 * not unlock the i_mutex at this case.
8061 */
8062 if (offset + count <= inode->i_size) {
8063 inode_unlock(inode);
8064 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02008065 }
8066 down_read(&BTRFS_I(inode)->dio_sem);
8067 }
8068
Josef Bacik0eb79292020-09-03 11:16:51 -04008069 /*
8070 * We have are actually a sync iocb, so we need our fancy endio to know
8071 * if we need to sync.
8072 */
8073 if (current->journal_info)
8074 ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops,
8075 &btrfs_sync_dops, is_sync_kiocb(iocb));
8076 else
8077 ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops,
8078 &btrfs_dio_ops, is_sync_kiocb(iocb));
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008079
8080 if (ret == -ENOTBLK)
8081 ret = 0;
8082
8083 if (iov_iter_rw(iter) == WRITE)
David Sterbaf4c48b42020-06-09 19:19:27 +02008084 up_read(&BTRFS_I(inode)->dio_sem);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008085
David Sterbaf4c48b42020-06-09 19:19:27 +02008086 if (relock)
8087 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02008088
David Sterbaf4c48b42020-06-09 19:19:27 +02008089 extent_changeset_free(data_reserved);
8090 return ret;
8091}
Chris Mason16432982008-04-10 10:23:21 -04008092
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008093static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008094 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008095{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008096 int ret;
8097
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008098 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008099 if (ret)
8100 return ret;
8101
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008102 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008103}
8104
Chris Mason9ebefb182007-06-15 13:50:00 -04008105int btrfs_readpage(struct file *file, struct page *page)
8106{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008107 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8108 u64 start = page_offset(page);
8109 u64 end = start + PAGE_SIZE - 1;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008110 unsigned long bio_flags = 0;
Nikolay Borisov0f208812020-09-14 14:39:16 +03008111 struct bio *bio = NULL;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008112 int ret;
8113
Nikolay Borisov0f208812020-09-14 14:39:16 +03008114 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8115
8116 ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008117 if (bio)
8118 ret = submit_one_bio(bio, 0, bio_flags);
8119 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008120}
Chris Mason1832a6d2007-12-21 16:27:21 -05008121
Chris Mason39279cc2007-06-12 06:35:45 -04008122static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8123{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008124 struct inode *inode = page->mapping->host;
8125 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -04008126
8127 if (current->flags & PF_MEMALLOC) {
8128 redirty_page_for_writepage(wbc, page);
8129 unlock_page(page);
8130 return 0;
8131 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008132
8133 /*
8134 * If we are under memory pressure we will call this directly from the
8135 * VM, we need to make sure we have the inode referenced for the ordered
8136 * extent. If not just return like we didn't do anything.
8137 */
8138 if (!igrab(inode)) {
8139 redirty_page_for_writepage(wbc, page);
8140 return AOP_WRITEPAGE_ACTIVATE;
8141 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008142 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008143 btrfs_add_delayed_iput(inode);
8144 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008145}
Chris Mason39279cc2007-06-12 06:35:45 -04008146
Eric Sandeen48a3b632013-04-25 20:41:01 +00008147static int btrfs_writepages(struct address_space *mapping,
8148 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008149{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008150 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008151}
8152
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008153static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008154{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008155 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008156}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008157
Chris Masone6dcd2d2008-07-17 12:53:50 -04008158static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008159{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008160 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008161 if (ret == 1)
8162 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008163 return ret;
8164}
Chris Mason39279cc2007-06-12 06:35:45 -04008165
Chris Masone6dcd2d2008-07-17 12:53:50 -04008166static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8167{
Chris Mason98509cf2008-09-11 15:51:43 -04008168 if (PageWriteback(page) || PageDirty(page))
8169 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008170 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008171}
8172
Roman Gushchinf8e66082020-03-04 16:57:35 -08008173#ifdef CONFIG_MIGRATION
8174static int btrfs_migratepage(struct address_space *mapping,
8175 struct page *newpage, struct page *page,
8176 enum migrate_mode mode)
8177{
8178 int ret;
8179
8180 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8181 if (ret != MIGRATEPAGE_SUCCESS)
8182 return ret;
8183
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008184 if (page_has_private(page))
8185 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008186
8187 if (PagePrivate2(page)) {
8188 ClearPagePrivate2(page);
8189 SetPagePrivate2(newpage);
8190 }
8191
8192 if (mode != MIGRATE_SYNC_NO_COPY)
8193 migrate_page_copy(newpage, page);
8194 else
8195 migrate_page_states(newpage, page);
8196 return MIGRATEPAGE_SUCCESS;
8197}
8198#endif
8199
Lukas Czernerd47992f2013-05-21 23:17:23 -04008200static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8201 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008202{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008203 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8204 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008205 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008206 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008207 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008208 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308209 u64 start;
8210 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008211 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008212
Chris Mason8b62b722009-09-02 16:53:46 -04008213 /*
8214 * we have the page locked, so new writeback can't start,
8215 * and the dirty bit won't be cleared while we are here.
8216 *
8217 * Wait for IO on this page so that we can safely clear
8218 * the PagePrivate2 bit and do ordered accounting
8219 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008220 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008221
Qu Wenruoad71a9a2021-05-31 16:50:55 +08008222 /*
8223 * For subpage case, we have call sites like
8224 * btrfs_punch_hole_lock_range() which passes range not aligned to
8225 * sectorsize.
8226 * If the range doesn't cover the full page, we don't need to and
8227 * shouldn't clear page extent mapped, as page->private can still
8228 * record subpage dirty bits for other part of the range.
8229 *
8230 * For cases that can invalidate the full even the range doesn't
8231 * cover the full page, like invalidating the last page, we're
8232 * still safe to wait for ordered extent to finish.
8233 */
8234 if (!(offset == 0 && length == PAGE_SIZE)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008235 btrfs_releasepage(page, GFP_NOFS);
8236 return;
8237 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008238
8239 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008240 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo6a402b92021-01-27 14:38:48 +08008241
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308242 start = page_start;
Qu Wenruo6a402b92021-01-27 14:38:48 +08008243again:
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008244 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008245 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008246 end = min(page_end,
8247 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008248 /*
8249 * IO on this page will never be started, so we need
8250 * to account for any ordered extents now
8251 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008252 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308253 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008254 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008255 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008256 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008257 /*
8258 * whoever cleared the private bit is responsible
8259 * for the finish_ordered_io
8260 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008261 if (TestClearPagePrivate2(page)) {
8262 struct btrfs_ordered_inode_tree *tree;
8263 u64 new_len;
8264
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008265 tree = &inode->ordered_tree;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008266
8267 spin_lock_irq(&tree->lock);
8268 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308269 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008270 if (new_len < ordered->truncated_len)
8271 ordered->truncated_len = new_len;
8272 spin_unlock_irq(&tree->lock);
8273
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008274 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8275 start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308276 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008277 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008278 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008279 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008280 if (!inode_evicting) {
8281 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308282 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008283 &cached_state);
8284 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308285
8286 start = end + 1;
8287 if (start < page_end)
8288 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008289 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008290
Qu Wenruob9d0b382015-09-29 10:35:16 +08008291 /*
8292 * Qgroup reserved space handler
8293 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008294 * 1) Already written to disk or ordered extent already submitted
8295 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8296 * Qgroup will be handled by its qgroup_record then.
8297 * btrfs_qgroup_free_data() call will do nothing here.
8298 *
8299 * 2) Not written to disk yet
8300 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8301 * bit of its io_tree, and free the qgroup reserved data space.
8302 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008303 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008304 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008305 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008306 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008307 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8308 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008309 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008310
8311 __btrfs_releasepage(page, GFP_NOFS);
8312 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008313
Chris Mason4a096752008-07-21 10:29:44 -04008314 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008315 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008316}
8317
Chris Mason9ebefb182007-06-15 13:50:00 -04008318/*
8319 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8320 * called from a page fault handler when a page is first dirtied. Hence we must
8321 * be careful to check for EOF conditions here. We set the page up correctly
8322 * for a written page which means we get ENOSPC checking when writing into
8323 * holes and correct delalloc and unwritten extent mapping on filesystems that
8324 * support these features.
8325 *
8326 * We are not allowed to take the i_mutex here so we have to play games to
8327 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008328 * truncate_setsize() writes the inode size before removing pages, once we have
8329 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008330 * beyond EOF, then the page is guaranteed safe against truncation until we
8331 * unlock the page.
8332 */
Souptick Joardera528a242018-06-06 19:54:44 +05308333vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008334{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008335 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008336 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008337 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008338 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8339 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008340 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008341 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008342 char *kaddr;
8343 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008344 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308345 vm_fault_t ret;
8346 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008347 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308348 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008349 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008350 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308351 u64 end;
8352
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008353 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008354
Jan Karab2b5ef52012-06-12 16:20:45 +02008355 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008356 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008357 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308358 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008359
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308360 /*
8361 * Reserving delalloc space after obtaining the page lock can lead to
8362 * deadlock. For example, if a dirty page is locked by this function
8363 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8364 * dirty page write out, then the btrfs_writepage() function could
8365 * end up waiting indefinitely to get a lock on the page currently
8366 * being processed by btrfs_page_mkwrite() function.
8367 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008368 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8369 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308370 if (!ret2) {
8371 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008372 reserved = 1;
8373 }
Souptick Joardera528a242018-06-06 19:54:44 +05308374 if (ret2) {
8375 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008376 if (reserved)
8377 goto out;
8378 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008379 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008380
Nick Piggin56a76f82009-03-31 15:23:23 -07008381 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008382again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008383 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008384 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008385
Chris Mason9ebefb182007-06-15 13:50:00 -04008386 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008387 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008388 /* page got truncated out from underneath us */
8389 goto out_unlock;
8390 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008391 wait_on_page_writeback(page);
8392
David Sterbaff13db42015-12-03 14:30:40 +01008393 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008394 set_page_extent_mapped(page);
8395
Chris Masoneb84ae02008-07-17 13:53:27 -04008396 /*
8397 * we can't set the delalloc bits if there are pending ordered
8398 * extents. Drop our locks and wait for them to finish
8399 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008400 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8401 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008402 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008403 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008404 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008405 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008406 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008407 btrfs_put_ordered_extent(ordered);
8408 goto again;
8409 }
8410
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008411 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008412 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008413 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008414 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308415 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008416 btrfs_delalloc_release_space(BTRFS_I(inode),
8417 data_reserved, page_start,
8418 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308419 }
8420 }
8421
Josef Bacikfbf19082009-10-01 17:10:23 -04008422 /*
Liu Bo54160342016-12-13 12:15:19 -08008423 * page_mkwrite gets called when the page is firstly dirtied after it's
8424 * faulted in, but write(2) could also dirty a page and set delalloc
8425 * bits, thus in this case for space account reason, we still need to
8426 * clear any delalloc bits within this page range since we have to
8427 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008428 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308429 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008430 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8431 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008432
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008433 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008434 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308435 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008436 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008437 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008438 ret = VM_FAULT_SIGBUS;
8439 goto out_unlock;
8440 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008441
8442 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008443 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008444 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008445 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008446 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008447
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008448 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008449 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008450 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008451 flush_dcache_page(page);
8452 kunmap(page);
8453 }
Chris Mason247e7432008-07-17 12:53:51 -04008454 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008455 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008456 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008457
Filipe Mananad845f892021-02-23 12:08:48 +00008458 btrfs_set_inode_last_sub_trans(BTRFS_I(inode));
Chris Mason257c62e2009-10-13 13:21:08 -04008459
David Sterbae43bbe52017-12-12 21:43:52 +01008460 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008461
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008462 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8463 sb_end_pagefault(inode->i_sb);
8464 extent_changeset_free(data_reserved);
8465 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008466
8467out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008468 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008469out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008470 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008471 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008472 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008473out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008474 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008475 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008476 return ret;
8477}
8478
Filipe Manana213e8c52018-02-06 20:40:31 +00008479static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008480{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008481 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008482 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008483 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008484 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008485 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008486 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008487 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008488
Filipe Manana213e8c52018-02-06 20:40:31 +00008489 if (!skip_writeback) {
8490 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8491 (u64)-1);
8492 if (ret)
8493 return ret;
8494 }
Chris Mason39279cc2007-06-12 06:35:45 -04008495
Josef Bacikfcb80c22011-05-03 10:40:22 -04008496 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008497 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8498 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008499 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008500 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008501 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008502 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008503 * be free'd up by the truncate operation, but also have some slack
8504 * space reserved in case it uses space during the truncate (thank you
8505 * very much snapshotting).
8506 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008507 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008508 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008509 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008510 * doesn't end up using space reserved for updating the inode. We also
8511 * need to be able to stop the transaction and start a new one, which
8512 * means we need to be able to update the inode several times, and we
8513 * have no idea of knowing how many times that will be, so we can't just
8514 * reserve 1 item for the entirety of the operation, so that has to be
8515 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008516 *
8517 * So that leaves us with
8518 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008519 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008520 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008521 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008522 * updating the inode.
8523 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008524 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008525 if (!rsv)
8526 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008527 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008528 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008529
Josef Bacik907cbce2011-08-08 13:46:15 -04008530 /*
Josef Bacik07127182011-08-19 10:29:59 -04008531 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008532 * 1 for updating the inode.
8533 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008534 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008535 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008536 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008537 goto out;
8538 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008539
Josef Bacik907cbce2011-08-08 13:46:15 -04008540 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008541 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008542 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008543 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008544
Chris Mason5a3f23d2009-03-31 13:27:11 -04008545 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008546 * So if we truncate and then write and fsync we normally would just
8547 * write the extents that changed, which is a problem if we need to
8548 * first truncate that entire inode. So set this flag so we write out
8549 * all of the extents in the inode to the sync log so we're completely
8550 * safe.
8551 */
8552 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008553 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008554
Yan, Zheng80825102009-11-12 09:35:36 +00008555 while (1) {
8556 ret = btrfs_truncate_inode_items(trans, root, inode,
8557 inode->i_size,
8558 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008559 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008560 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008561 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008562
Yan, Zheng80825102009-11-12 09:35:36 +00008563 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008564 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008565 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008566
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008567 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008568 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008569
8570 trans = btrfs_start_transaction(root, 2);
8571 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008572 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008573 trans = NULL;
8574 break;
8575 }
8576
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008577 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008578 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008579 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008580 BUG_ON(ret); /* shouldn't happen */
8581 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008582 }
8583
Josef Bacikddfae632017-10-19 14:16:02 -04008584 /*
8585 * We can't call btrfs_truncate_block inside a trans handle as we could
8586 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8587 * we've truncated everything except the last little bit, and can do
8588 * btrfs_truncate_block and then update the disk_i_size.
8589 */
8590 if (ret == NEED_TRUNCATE_BLOCK) {
8591 btrfs_end_transaction(trans);
8592 btrfs_btree_balance_dirty(fs_info);
8593
8594 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8595 if (ret)
8596 goto out;
8597 trans = btrfs_start_transaction(root, 1);
8598 if (IS_ERR(trans)) {
8599 ret = PTR_ERR(trans);
8600 goto out;
8601 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008602 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008603 }
8604
Chris Mason917c16b2011-11-08 14:49:59 -05008605 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008606 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008607
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008608 trans->block_rsv = &fs_info->trans_block_rsv;
8609 ret2 = btrfs_update_inode(trans, root, inode);
8610 if (ret2 && !ret)
8611 ret = ret2;
8612
8613 ret2 = btrfs_end_transaction(trans);
8614 if (ret2 && !ret)
8615 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008616 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008617 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008618out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008619 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008620
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008621 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008622}
8623
Sven Wegener3b963622008-06-09 21:57:42 -04008624/*
Chris Masond352ac62008-09-29 15:18:18 -04008625 * create a new subvolume directory/inode (helper for the ioctl).
8626 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008627int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008628 struct btrfs_root *new_root,
8629 struct btrfs_root *parent_root,
8630 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008631{
Chris Mason39279cc2007-06-12 06:35:45 -04008632 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008633 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008634 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008635
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008636 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8637 new_dirid, new_dirid,
8638 S_IFDIR | (~current_umask() & S_IRWXUGO),
8639 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008640 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008641 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008642 inode->i_op = &btrfs_dir_inode_operations;
8643 inode->i_fop = &btrfs_dir_file_operations;
8644
Miklos Szeredibfe86842011-10-28 14:13:29 +02008645 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008646 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008647 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008648
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008649 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8650 if (err)
8651 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008652 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008653 new_root->root_key.objectid, err);
8654
Yan, Zheng76dda932009-09-21 16:00:26 -04008655 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008656
Yan, Zheng76dda932009-09-21 16:00:26 -04008657 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008658 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008659}
8660
Chris Mason39279cc2007-06-12 06:35:45 -04008661struct inode *btrfs_alloc_inode(struct super_block *sb)
8662{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008663 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008664 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008665 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008666
David Sterba712e36c2017-10-31 17:08:27 +01008667 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008668 if (!ei)
8669 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008670
8671 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008672 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008673 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008674 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008675 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008676 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008677 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008678 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008679 ei->disk_i_size = 0;
8680 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008681 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008682 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008683 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008684 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008685 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008686 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008687
Josef Bacik9e0baf62011-07-15 15:16:44 +00008688 spin_lock_init(&ei->lock);
8689 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008690 if (sb->s_magic != BTRFS_TEST_MAGIC)
8691 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8692 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008693 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008694 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008695 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008696
Miao Xie16cdcec2011-04-22 18:12:22 +08008697 ei->delayed_node = NULL;
8698
chandan r9cc97d62012-07-04 12:48:07 +05308699 ei->i_otime.tv_sec = 0;
8700 ei->i_otime.tv_nsec = 0;
8701
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008702 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008703 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008704 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8705 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8706 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008707 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8708 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008709 ei->io_tree.track_uptodate = true;
8710 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008711 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008712 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008713 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008714 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008715 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008716 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008717 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008718
8719 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008720}
8721
Josef Bacikaaedb552013-10-11 14:44:09 -04008722#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8723void btrfs_test_destroy_inode(struct inode *inode)
8724{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008725 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008726 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8727}
8728#endif
8729
Al Viro26602ca2019-04-10 15:14:41 -04008730void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008731{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008732 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8733}
8734
Nikolay Borisov633cc812020-09-18 12:15:49 +03008735void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008736{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008737 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008738 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8739 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008740
Nikolay Borisov633cc812020-09-18 12:15:49 +03008741 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8742 WARN_ON(vfs_inode->i_data.nrpages);
8743 WARN_ON(inode->block_rsv.reserved);
8744 WARN_ON(inode->block_rsv.size);
8745 WARN_ON(inode->outstanding_extents);
8746 WARN_ON(inode->delalloc_bytes);
8747 WARN_ON(inode->new_delalloc_bytes);
8748 WARN_ON(inode->csum_bytes);
8749 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008750
Chris Mason5a3f23d2009-03-31 13:27:11 -04008751 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008752 * This can happen where we create an inode, but somebody else also
8753 * created the same inode and we need to destroy the one we already
8754 * created.
8755 */
8756 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008757 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008758
Chris Masond3977122009-01-05 21:25:51 -05008759 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008760 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008761 if (!ordered)
8762 break;
8763 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008764 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008765 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008766 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008767 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008768 btrfs_put_ordered_extent(ordered);
8769 btrfs_put_ordered_extent(ordered);
8770 }
8771 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008772 btrfs_qgroup_check_reserved_leak(inode);
8773 inode_tree_del(inode);
8774 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8775 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8776 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008777}
8778
Al Viro45321ac2010-06-07 13:43:19 -04008779int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008780{
8781 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008782
Naohiro Aota6379ef92013-06-06 09:56:34 +00008783 if (root == NULL)
8784 return 1;
8785
Liu Bofa6ac872013-02-20 14:10:23 +00008786 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008787 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008788 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008789 else
Al Viro45321ac2010-06-07 13:43:19 -04008790 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008791}
8792
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008793static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008794{
8795 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8796
8797 inode_init_once(&ei->vfs_inode);
8798}
8799
David Sterbae67c7182018-02-19 17:24:18 +01008800void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008801{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008802 /*
8803 * Make sure all delayed rcu free inodes are flushed before we
8804 * destroy cache.
8805 */
8806 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008807 kmem_cache_destroy(btrfs_inode_cachep);
8808 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008809 kmem_cache_destroy(btrfs_path_cachep);
8810 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008811 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008812}
8813
Liu Bof5c29bd2017-11-02 17:21:50 -06008814int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008815{
David Sterba837e1972012-09-07 03:00:48 -06008816 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008817 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008818 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8819 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008820 if (!btrfs_inode_cachep)
8821 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008822
David Sterba837e1972012-09-07 03:00:48 -06008823 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008824 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008825 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008826 if (!btrfs_trans_handle_cachep)
8827 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008828
David Sterba837e1972012-09-07 03:00:48 -06008829 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008830 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008831 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008832 if (!btrfs_path_cachep)
8833 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008834
David Sterba837e1972012-09-07 03:00:48 -06008835 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008836 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008837 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008838 if (!btrfs_free_space_cachep)
8839 goto fail;
8840
Christophe Leroy3acd4852019-08-21 15:05:55 +00008841 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8842 PAGE_SIZE, PAGE_SIZE,
David Sterba2c8d6a92021-03-15 15:18:24 +01008843 SLAB_MEM_SPREAD, NULL);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008844 if (!btrfs_free_space_bitmap_cachep)
8845 goto fail;
8846
Chris Mason39279cc2007-06-12 06:35:45 -04008847 return 0;
8848fail:
8849 btrfs_destroy_cachep();
8850 return -ENOMEM;
8851}
8852
David Howellsa528d352017-01-31 16:46:22 +00008853static int btrfs_getattr(const struct path *path, struct kstat *stat,
8854 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008855{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008856 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008857 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008858 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008859 u32 bi_flags = BTRFS_I(inode)->flags;
8860
8861 stat->result_mask |= STATX_BTIME;
8862 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8863 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8864 if (bi_flags & BTRFS_INODE_APPEND)
8865 stat->attributes |= STATX_ATTR_APPEND;
8866 if (bi_flags & BTRFS_INODE_COMPRESS)
8867 stat->attributes |= STATX_ATTR_COMPRESSED;
8868 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8869 stat->attributes |= STATX_ATTR_IMMUTABLE;
8870 if (bi_flags & BTRFS_INODE_NODUMP)
8871 stat->attributes |= STATX_ATTR_NODUMP;
8872
8873 stat->attributes_mask |= (STATX_ATTR_APPEND |
8874 STATX_ATTR_COMPRESSED |
8875 STATX_ATTR_IMMUTABLE |
8876 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008877
Chris Mason39279cc2007-06-12 06:35:45 -04008878 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008879 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008880
8881 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008882 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008883 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008884 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008885 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008886 return 0;
8887}
8888
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008889static int btrfs_rename_exchange(struct inode *old_dir,
8890 struct dentry *old_dentry,
8891 struct inode *new_dir,
8892 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008893{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008894 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008895 struct btrfs_trans_handle *trans;
8896 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008897 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008898 struct inode *new_inode = new_dentry->d_inode;
8899 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008900 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008901 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8902 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008903 u64 old_idx = 0;
8904 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008905 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01008906 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008907 bool root_log_pinned = false;
8908 bool dest_log_pinned = false;
Josef Bacik0df50d42021-05-19 14:04:21 -04008909 bool need_abort = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008910
NeilBrown67fece62021-08-06 14:26:24 +10008911 /*
8912 * For non-subvolumes allow exchange only within one subvolume, in the
8913 * same inode namespace. Two subvolumes (represented as directory) can
8914 * be exchanged as they're a logical link and have a fixed inode number.
8915 */
8916 if (root != dest &&
8917 (old_ino != BTRFS_FIRST_FREE_OBJECTID ||
8918 new_ino != BTRFS_FIRST_FREE_OBJECTID))
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008919 return -EXDEV;
8920
8921 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008922 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8923 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008924 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008925
8926 /*
8927 * We want to reserve the absolute worst case amount of items. So if
8928 * both inodes are subvols and we need to unlink them then that would
8929 * require 4 item modifications, but if they are both normal inodes it
8930 * would require 5 item modifications, so we'll assume their normal
8931 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8932 * should cover the worst case number of items we'll modify.
8933 */
8934 trans = btrfs_start_transaction(root, 12);
8935 if (IS_ERR(trans)) {
8936 ret = PTR_ERR(trans);
8937 goto out_notrans;
8938 }
8939
Josef Bacik3e174092019-11-15 15:43:06 -05008940 if (dest != root)
8941 btrfs_record_root_in_trans(trans, dest);
8942
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008943 /*
8944 * We need to find a free sequence number both in the source and
8945 * in the destination directory for the exchange.
8946 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008947 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008948 if (ret)
8949 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008950 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008951 if (ret)
8952 goto out_fail;
8953
8954 BTRFS_I(old_inode)->dir_index = 0ULL;
8955 BTRFS_I(new_inode)->dir_index = 0ULL;
8956
8957 /* Reference for the source. */
8958 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8959 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008960 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008961 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008962 btrfs_pin_log_trans(root);
8963 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008964 ret = btrfs_insert_inode_ref(trans, dest,
8965 new_dentry->d_name.name,
8966 new_dentry->d_name.len,
8967 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008968 btrfs_ino(BTRFS_I(new_dir)),
8969 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008970 if (ret)
8971 goto out_fail;
Josef Bacik0df50d42021-05-19 14:04:21 -04008972 need_abort = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008973 }
8974
8975 /* And now for the dest. */
8976 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8977 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008978 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008979 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008980 btrfs_pin_log_trans(dest);
8981 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008982 ret = btrfs_insert_inode_ref(trans, root,
8983 old_dentry->d_name.name,
8984 old_dentry->d_name.len,
8985 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008986 btrfs_ino(BTRFS_I(old_dir)),
8987 new_idx);
Josef Bacik0df50d42021-05-19 14:04:21 -04008988 if (ret) {
8989 if (need_abort)
8990 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008991 goto out_fail;
Josef Bacik0df50d42021-05-19 14:04:21 -04008992 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008993 }
8994
8995 /* Update inode version and ctime/mtime. */
8996 inode_inc_iversion(old_dir);
8997 inode_inc_iversion(new_dir);
8998 inode_inc_iversion(old_inode);
8999 inode_inc_iversion(new_inode);
9000 old_dir->i_ctime = old_dir->i_mtime = ctime;
9001 new_dir->i_ctime = new_dir->i_mtime = ctime;
9002 old_inode->i_ctime = ctime;
9003 new_inode->i_ctime = ctime;
9004
9005 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01009006 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9007 BTRFS_I(old_inode), 1);
9008 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9009 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009010 }
9011
9012 /* src is a subvolume */
9013 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009014 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009015 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009016 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9017 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009018 old_dentry->d_name.name,
9019 old_dentry->d_name.len);
9020 if (!ret)
9021 ret = btrfs_update_inode(trans, root, old_inode);
9022 }
9023 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009024 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009025 goto out_fail;
9026 }
9027
9028 /* dest is a subvolume */
9029 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05009030 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009031 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009032 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9033 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009034 new_dentry->d_name.name,
9035 new_dentry->d_name.len);
9036 if (!ret)
9037 ret = btrfs_update_inode(trans, dest, new_inode);
9038 }
9039 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009040 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009041 goto out_fail;
9042 }
9043
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009044 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009045 new_dentry->d_name.name,
9046 new_dentry->d_name.len, 0, old_idx);
9047 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009048 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009049 goto out_fail;
9050 }
9051
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009052 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009053 old_dentry->d_name.name,
9054 old_dentry->d_name.len, 0, new_idx);
9055 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009056 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009057 goto out_fail;
9058 }
9059
9060 if (old_inode->i_nlink == 1)
9061 BTRFS_I(old_inode)->dir_index = old_idx;
9062 if (new_inode->i_nlink == 1)
9063 BTRFS_I(new_inode)->dir_index = new_idx;
9064
Filipe Manana86e8aa02016-05-05 02:02:27 +01009065 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009066 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9067 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009068 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009069 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009070 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009071 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009072 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9073 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009074 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009075 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009076 }
9077out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009078 /*
9079 * If we have pinned a log and an error happened, we unpin tasks
9080 * trying to sync the log and force them to fallback to a transaction
9081 * commit if the log currently contains any of the inodes involved in
9082 * this rename operation (to ensure we do not persist a log with an
9083 * inconsistent state for any of these inodes or leading to any
9084 * inconsistencies when replayed). If the transaction was aborted, the
9085 * abortion reason is propagated to userspace when attempting to commit
9086 * the transaction. If the log does not contain any of these inodes, we
9087 * allow the tasks to sync it.
9088 */
9089 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009090 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9091 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9092 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009093 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009094 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009095 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009096
9097 if (root_log_pinned) {
9098 btrfs_end_log_trans(root);
9099 root_log_pinned = false;
9100 }
9101 if (dest_log_pinned) {
9102 btrfs_end_log_trans(dest);
9103 dest_log_pinned = false;
9104 }
9105 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009106 ret2 = btrfs_end_transaction(trans);
9107 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009108out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009109 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9110 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009111 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009112
9113 return ret;
9114}
9115
9116static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9117 struct btrfs_root *root,
9118 struct inode *dir,
9119 struct dentry *dentry)
9120{
9121 int ret;
9122 struct inode *inode;
9123 u64 objectid;
9124 u64 index;
9125
9126 ret = btrfs_find_free_ino(root, &objectid);
9127 if (ret)
9128 return ret;
9129
9130 inode = btrfs_new_inode(trans, root, dir,
9131 dentry->d_name.name,
9132 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009133 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009134 objectid,
9135 S_IFCHR | WHITEOUT_MODE,
9136 &index);
9137
9138 if (IS_ERR(inode)) {
9139 ret = PTR_ERR(inode);
9140 return ret;
9141 }
9142
9143 inode->i_op = &btrfs_special_inode_operations;
9144 init_special_inode(inode, inode->i_mode,
9145 WHITEOUT_DEV);
9146
9147 ret = btrfs_init_inode_security(trans, inode, dir,
9148 &dentry->d_name);
9149 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009150 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009151
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009152 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9153 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009154 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009155 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009156
9157 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009158out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009159 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009160 if (ret)
9161 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009162 iput(inode);
9163
Filipe Mananac9901612016-05-05 01:41:57 +01009164 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009165}
9166
Chris Mason39279cc2007-06-12 06:35:45 -04009167static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009168 struct inode *new_dir, struct dentry *new_dentry,
9169 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009170{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009171 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009172 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009173 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009174 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9175 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009176 struct inode *new_inode = d_inode(new_dentry);
9177 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009178 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009179 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009180 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009181 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009182 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009183
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009184 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009185 return -EPERM;
9186
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009187 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009188 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009189 return -EXDEV;
9190
Li Zefan33345d012011-04-20 10:31:50 +08009191 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009192 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009193 return -ENOTEMPTY;
9194
Chris Mason39279cc2007-06-12 06:35:45 -04009195 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009196 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009197 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009198
9199
9200 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009201 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009202 new_dentry->d_name.name,
9203 new_dentry->d_name.len);
9204
9205 if (ret) {
9206 if (ret == -EEXIST) {
9207 /* we shouldn't get
9208 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309209 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009210 return ret;
9211 }
9212 } else {
9213 /* maybe -EOVERFLOW */
9214 return ret;
9215 }
9216 }
9217 ret = 0;
9218
Chris Mason5a3f23d2009-03-31 13:27:11 -04009219 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009220 * we're using rename to replace one file with another. Start IO on it
9221 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009222 */
Chris Mason8d875f92014-08-12 10:47:42 -07009223 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009224 filemap_flush(old_inode->i_mapping);
9225
Yan, Zheng76dda932009-09-21 16:00:26 -04009226 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009227 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009228 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009229 /*
9230 * We want to reserve the absolute worst case amount of items. So if
9231 * both inodes are subvols and we need to unlink them then that would
9232 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009233 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009234 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9235 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009236 * If our rename has the whiteout flag, we need more 5 units for the
9237 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9238 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009239 */
Filipe Manana5062af32016-05-05 10:26:26 +01009240 trans_num_items = 11;
9241 if (flags & RENAME_WHITEOUT)
9242 trans_num_items += 5;
9243 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009244 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009245 ret = PTR_ERR(trans);
9246 goto out_notrans;
9247 }
Chris Mason5f39d392007-10-15 16:14:19 -04009248
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009249 if (dest != root)
9250 btrfs_record_root_in_trans(trans, dest);
9251
Nikolay Borisov877574e2017-02-20 13:50:33 +02009252 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009253 if (ret)
9254 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009255
Miao Xie67de1172013-12-26 13:07:06 +08009256 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009257 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009258 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009259 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009260 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009261 btrfs_pin_log_trans(root);
9262 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009263 ret = btrfs_insert_inode_ref(trans, dest,
9264 new_dentry->d_name.name,
9265 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009266 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009267 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009268 if (ret)
9269 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009270 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009271
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009272 inode_inc_iversion(old_dir);
9273 inode_inc_iversion(new_dir);
9274 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009275 old_dir->i_ctime = old_dir->i_mtime =
9276 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009277 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009278
Chris Mason12fcfd22009-03-24 10:24:20 -04009279 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009280 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9281 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009282
Li Zefan33345d012011-04-20 10:31:50 +08009283 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009284 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009285 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009286 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9287 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009288 old_dentry->d_name.name,
9289 old_dentry->d_name.len);
9290 if (!ret)
9291 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009292 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009293 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009294 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009295 goto out_fail;
9296 }
Chris Mason39279cc2007-06-12 06:35:45 -04009297
9298 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009299 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009300 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009301 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009302 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009303 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009304 BUG_ON(new_inode->i_nlink == 0);
9305 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009306 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9307 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009308 new_dentry->d_name.name,
9309 new_dentry->d_name.len);
9310 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009311 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009312 ret = btrfs_orphan_add(trans,
9313 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009314 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009315 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009316 goto out_fail;
9317 }
Chris Mason39279cc2007-06-12 06:35:45 -04009318 }
Josef Bacikaec74772008-07-24 12:12:38 -04009319
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009320 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009321 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009322 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009323 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009324 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009325 goto out_fail;
9326 }
Chris Mason39279cc2007-06-12 06:35:45 -04009327
Miao Xie67de1172013-12-26 13:07:06 +08009328 if (old_inode->i_nlink == 1)
9329 BTRFS_I(old_inode)->dir_index = index;
9330
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009331 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009332 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9333 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009334 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009335 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009336 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009337
9338 if (flags & RENAME_WHITEOUT) {
9339 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9340 old_dentry);
9341
9342 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009343 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009344 goto out_fail;
9345 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009346 }
Chris Mason39279cc2007-06-12 06:35:45 -04009347out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009348 /*
9349 * If we have pinned the log and an error happened, we unpin tasks
9350 * trying to sync the log and force them to fallback to a transaction
9351 * commit if the log currently contains any of the inodes involved in
9352 * this rename operation (to ensure we do not persist a log with an
9353 * inconsistent state for any of these inodes or leading to any
9354 * inconsistencies when replayed). If the transaction was aborted, the
9355 * abortion reason is propagated to userspace when attempting to commit
9356 * the transaction. If the log does not contain any of these inodes, we
9357 * allow the tasks to sync it.
9358 */
9359 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009360 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9361 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9362 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009363 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009364 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009365 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009366
9367 btrfs_end_log_trans(root);
9368 log_pinned = false;
9369 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009370 ret2 = btrfs_end_transaction(trans);
9371 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009372out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009373 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009374 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009375
Chris Mason39279cc2007-06-12 06:35:45 -04009376 return ret;
9377}
9378
Miklos Szeredi80ace852014-07-23 15:15:32 +02009379static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9380 struct inode *new_dir, struct dentry *new_dentry,
9381 unsigned int flags)
9382{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009383 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009384 return -EINVAL;
9385
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009386 if (flags & RENAME_EXCHANGE)
9387 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9388 new_dentry);
9389
9390 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009391}
9392
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009393struct btrfs_delalloc_work {
9394 struct inode *inode;
9395 struct completion completion;
9396 struct list_head list;
9397 struct btrfs_work work;
9398};
9399
Miao Xie8ccf6f192012-10-25 09:28:04 +00009400static void btrfs_run_delalloc_work(struct btrfs_work *work)
9401{
9402 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009403 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009404
9405 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9406 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009407 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009408 filemap_flush(inode->i_mapping);
9409 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9410 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009411 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009412
Nikolay Borisov076da912018-04-23 10:54:16 +03009413 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009414 complete(&delalloc_work->completion);
9415}
9416
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009417static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009418{
9419 struct btrfs_delalloc_work *work;
9420
David Sterba100d5702015-12-08 14:39:32 +01009421 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009422 if (!work)
9423 return NULL;
9424
9425 init_completion(&work->completion);
9426 INIT_LIST_HEAD(&work->list);
9427 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009428 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009429
9430 return work;
9431}
9432
Chris Masond352ac62008-09-29 15:18:18 -04009433/*
9434 * some fairly slow code that needs optimization. This walks the list
9435 * of all the inodes with pending delalloc and forces them to disk.
9436 */
Josef Bacike3b52522021-01-07 17:08:30 -05009437static int start_delalloc_inodes(struct btrfs_root *root,
9438 struct writeback_control *wbc, bool snapshot,
Filipe Manana17243f72020-12-02 11:55:58 +00009439 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009440{
Chris Masonea8c2812008-08-04 23:17:27 -04009441 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009442 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009443 struct btrfs_delalloc_work *work, *next;
9444 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009445 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009446 int ret = 0;
Josef Bacike3b52522021-01-07 17:08:30 -05009447 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009448
Miao Xie8ccf6f192012-10-25 09:28:04 +00009449 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009450 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009451
Miao Xie573bfb72014-03-06 13:55:03 +08009452 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009453 spin_lock(&root->delalloc_lock);
9454 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009455 while (!list_empty(&splice)) {
9456 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009457 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009458
Miao Xieeb73c1b2013-05-15 07:48:22 +00009459 list_move_tail(&binode->delalloc_inodes,
9460 &root->delalloc_inodes);
Filipe Manana17243f72020-12-02 11:55:58 +00009461
9462 if (in_reclaim_context &&
9463 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9464 continue;
9465
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009466 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009467 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009468 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009469 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009470 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009471 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009472
Ethan Lien3cd24c62018-11-01 14:49:03 +08009473 if (snapshot)
9474 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9475 &binode->runtime_flags);
Josef Bacike3b52522021-01-07 17:08:30 -05009476 if (full_flush) {
9477 work = btrfs_alloc_delalloc_work(inode);
9478 if (!work) {
9479 iput(inode);
9480 ret = -ENOMEM;
9481 goto out;
9482 }
9483 list_add_tail(&work->list, &works);
9484 btrfs_queue_work(root->fs_info->flush_workers,
9485 &work->work);
9486 } else {
9487 ret = sync_inode(inode, wbc);
9488 if (!ret &&
9489 test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9490 &BTRFS_I(inode)->runtime_flags))
9491 ret = sync_inode(inode, wbc);
9492 btrfs_add_delayed_iput(inode);
9493 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009494 goto out;
9495 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009496 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009497 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009498 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009499 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009500
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009501out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009502 list_for_each_entry_safe(work, next, &works, list) {
9503 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009504 wait_for_completion(&work->completion);
9505 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009506 }
9507
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009508 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009509 spin_lock(&root->delalloc_lock);
9510 list_splice_tail(&splice, &root->delalloc_inodes);
9511 spin_unlock(&root->delalloc_lock);
9512 }
Miao Xie573bfb72014-03-06 13:55:03 +08009513 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009514 return ret;
9515}
9516
Ethan Lien3cd24c62018-11-01 14:49:03 +08009517int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009518{
Josef Bacike3b52522021-01-07 17:08:30 -05009519 struct writeback_control wbc = {
9520 .nr_to_write = LONG_MAX,
9521 .sync_mode = WB_SYNC_NONE,
9522 .range_start = 0,
9523 .range_end = LLONG_MAX,
9524 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009525 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009526
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009527 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009528 return -EROFS;
9529
Josef Bacike3b52522021-01-07 17:08:30 -05009530 return start_delalloc_inodes(root, &wbc, true, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009531}
9532
Filipe Manana17243f72020-12-02 11:55:58 +00009533int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr,
9534 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009535{
Josef Bacike3b52522021-01-07 17:08:30 -05009536 struct writeback_control wbc = {
9537 .nr_to_write = (nr == U64_MAX) ? LONG_MAX : (unsigned long)nr,
9538 .sync_mode = WB_SYNC_NONE,
9539 .range_start = 0,
9540 .range_end = LLONG_MAX,
9541 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009542 struct btrfs_root *root;
9543 struct list_head splice;
9544 int ret;
9545
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009546 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009547 return -EROFS;
9548
9549 INIT_LIST_HEAD(&splice);
9550
Miao Xie573bfb72014-03-06 13:55:03 +08009551 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009552 spin_lock(&fs_info->delalloc_root_lock);
9553 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009554 while (!list_empty(&splice) && nr) {
Josef Bacike3b52522021-01-07 17:08:30 -05009555 /*
9556 * Reset nr_to_write here so we know that we're doing a full
9557 * flush.
9558 */
9559 if (nr == U64_MAX)
9560 wbc.nr_to_write = LONG_MAX;
9561
Miao Xieeb73c1b2013-05-15 07:48:22 +00009562 root = list_first_entry(&splice, struct btrfs_root,
9563 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009564 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009565 BUG_ON(!root);
9566 list_move_tail(&root->delalloc_root,
9567 &fs_info->delalloc_roots);
9568 spin_unlock(&fs_info->delalloc_root_lock);
9569
Josef Bacike3b52522021-01-07 17:08:30 -05009570 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -05009571 btrfs_put_root(root);
Josef Bacike3b52522021-01-07 17:08:30 -05009572 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009573 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009574 spin_lock(&fs_info->delalloc_root_lock);
9575 }
9576 spin_unlock(&fs_info->delalloc_root_lock);
9577
Miao Xie6c255e62014-03-06 13:55:01 +08009578 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009579out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009580 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009581 spin_lock(&fs_info->delalloc_root_lock);
9582 list_splice_tail(&splice, &fs_info->delalloc_roots);
9583 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009584 }
Miao Xie573bfb72014-03-06 13:55:03 +08009585 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009586 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009587}
9588
Chris Mason39279cc2007-06-12 06:35:45 -04009589static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9590 const char *symname)
9591{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009592 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009593 struct btrfs_trans_handle *trans;
9594 struct btrfs_root *root = BTRFS_I(dir)->root;
9595 struct btrfs_path *path;
9596 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009597 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009598 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009599 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309600 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009601 int name_len;
9602 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009603 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009604 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009605 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009606
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009607 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009608 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009609 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009610
Josef Bacik9ed74f22009-09-11 16:12:44 -04009611 /*
9612 * 2 items for inode item and ref
9613 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009614 * 1 item for updating parent inode item
9615 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009616 * 1 item for xattr if selinux is on
9617 */
Filipe Manana9269d122015-12-31 18:16:29 +00009618 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009619 if (IS_ERR(trans))
9620 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009621
Li Zefan581bb052011-04-20 10:06:11 +08009622 err = btrfs_find_free_ino(root, &objectid);
9623 if (err)
9624 goto out_unlock;
9625
Josef Bacikaec74772008-07-24 12:12:38 -04009626 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009627 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9628 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009629 if (IS_ERR(inode)) {
9630 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009631 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009632 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009633 }
Chris Mason39279cc2007-06-12 06:35:45 -04009634
Casey Schauflerad19db72011-12-15 10:09:07 -05009635 /*
9636 * If the active LSM wants to access the inode during
9637 * d_instantiate it needs these. Smack checks to see
9638 * if the filesystem supports xattrs by looking at the
9639 * ops vector.
9640 */
9641 inode->i_fop = &btrfs_file_operations;
9642 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009643 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009644
9645 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9646 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009647 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009648
Chris Mason39279cc2007-06-12 06:35:45 -04009649 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009650 if (!path) {
9651 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009652 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009653 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009654 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009655 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009656 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009657 datasize = btrfs_file_extent_calc_inline_size(name_len);
9658 err = btrfs_insert_empty_item(trans, root, path, &key,
9659 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009660 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009661 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009662 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009663 }
Chris Mason5f39d392007-10-15 16:14:19 -04009664 leaf = path->nodes[0];
9665 ei = btrfs_item_ptr(leaf, path->slots[0],
9666 struct btrfs_file_extent_item);
9667 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9668 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009669 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009670 btrfs_set_file_extent_encryption(leaf, ei, 0);
9671 btrfs_set_file_extent_compression(leaf, ei, 0);
9672 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9673 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9674
Chris Mason39279cc2007-06-12 06:35:45 -04009675 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009676 write_extent_buffer(leaf, symname, ptr, name_len);
9677 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009678 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009679
Chris Mason39279cc2007-06-12 06:35:45 -04009680 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009681 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009682 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009683 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009684 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009685 /*
9686 * Last step, add directory indexes for our symlink inode. This is the
9687 * last step to avoid extra cleanup of these indexes if an error happens
9688 * elsewhere above.
9689 */
9690 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009691 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9692 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009693 if (err)
9694 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009695
Al Viro1e2e5472018-05-04 08:23:01 -04009696 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009697
9698out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009699 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009700 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009701 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009702 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009703 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009704 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009705 return err;
9706}
Chris Mason16432982008-04-10 10:23:21 -04009707
Filipe Manana8fccebf2020-09-08 11:27:20 +01009708static struct btrfs_trans_handle *insert_prealloc_file_extent(
9709 struct btrfs_trans_handle *trans_in,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009710 struct inode *inode, struct btrfs_key *ins,
9711 u64 file_offset)
9712{
9713 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009714 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009715 struct btrfs_trans_handle *trans = trans_in;
9716 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009717 u64 start = ins->objectid;
9718 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009719 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009720
9721 memset(&stack_fi, 0, sizeof(stack_fi));
9722
9723 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9724 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9725 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9726 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9727 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9728 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9729 /* Encryption and other encoding is reserved and all 0 */
9730
Nikolay Borisov72b7d152020-06-03 08:55:18 +03009731 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009732 if (ret < 0)
Filipe Manana8fccebf2020-09-08 11:27:20 +01009733 return ERR_PTR(ret);
9734
9735 if (trans) {
9736 ret = insert_reserved_file_extent(trans, BTRFS_I(inode),
9737 file_offset, &stack_fi, ret);
9738 if (ret)
9739 return ERR_PTR(ret);
9740 return trans;
9741 }
9742
9743 extent_info.disk_offset = start;
9744 extent_info.disk_len = len;
9745 extent_info.data_offset = 0;
9746 extent_info.data_len = len;
9747 extent_info.file_offset = file_offset;
9748 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009749 extent_info.is_new_extent = true;
9750 extent_info.qgroup_reserved = ret;
9751 extent_info.insertions = 0;
9752
9753 path = btrfs_alloc_path();
9754 if (!path)
9755 return ERR_PTR(-ENOMEM);
9756
Filipe Manana306bfec2020-09-08 11:27:23 +01009757 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009758 file_offset + len - 1, &extent_info,
9759 &trans);
9760 btrfs_free_path(path);
9761 if (ret)
9762 return ERR_PTR(ret);
9763
9764 return trans;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009765}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009766
Josef Bacik0af3d002010-06-21 14:48:16 -04009767static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9768 u64 start, u64 num_bytes, u64 min_size,
9769 loff_t actual_len, u64 *alloc_hint,
9770 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009771{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009772 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009773 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9774 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009775 struct btrfs_root *root = BTRFS_I(inode)->root;
9776 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009777 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009778 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009779 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009780 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009781 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009782 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009783 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009784 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009785
Josef Bacik0af3d002010-06-21 14:48:16 -04009786 if (trans)
9787 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009788 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009789 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009790 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009791 /*
9792 * If we are severely fragmented we could end up with really
9793 * small allocations, so if the allocator is returning small
9794 * chunks lets make its job easier by only searching for those
9795 * sized chunks.
9796 */
9797 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009798 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9799 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009800 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -04009801 break;
Josef Bacikb778cf92020-02-13 10:47:31 -05009802
9803 /*
9804 * We've reserved this space, and thus converted it from
9805 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9806 * from here on out we will only need to clear our reservation
9807 * for the remaining unreserved area, so advance our
9808 * clear_offset by our extent size.
9809 */
9810 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009811
Josef Bacik0b670dc2015-09-23 17:11:16 -04009812 last_alloc = ins.offset;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009813 trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +01009814 /*
9815 * Now that we inserted the prealloc extent we can finally
9816 * decrement the number of reservations in the block group.
9817 * If we did it before, we could race with relocation and have
9818 * relocation miss the reserved extent, making it fail later.
9819 */
9820 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009821 if (IS_ERR(trans)) {
9822 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009823 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009824 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009825 break;
9826 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009827
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009828 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009829 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009830
Josef Bacik5dc562c2012-08-17 13:14:17 -04009831 em = alloc_extent_map();
9832 if (!em) {
9833 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9834 &BTRFS_I(inode)->runtime_flags);
9835 goto next;
9836 }
9837
9838 em->start = cur_offset;
9839 em->orig_start = cur_offset;
9840 em->len = ins.offset;
9841 em->block_start = ins.objectid;
9842 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009843 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009844 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009845 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9846 em->generation = trans->transid;
9847
9848 while (1) {
9849 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009850 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009851 write_unlock(&em_tree->lock);
9852 if (ret != -EEXIST)
9853 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009854 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009855 cur_offset + ins.offset - 1,
9856 0);
9857 }
9858 free_extent_map(em);
9859next:
Yan Zhengd899e052008-10-30 14:25:28 -04009860 num_bytes -= ins.offset;
9861 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009862 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009863
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009864 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009865 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009866 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009867 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009868 (actual_len > inode->i_size) &&
9869 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009870 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009871 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009872 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009873 i_size = cur_offset;
9874 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009875 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009876 }
9877
Yan Zhengd899e052008-10-30 14:25:28 -04009878 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009879
9880 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009881 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009882 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009883 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009884 break;
9885 }
Yan Zhengd899e052008-10-30 14:25:28 -04009886
Filipe Manana8fccebf2020-09-08 11:27:20 +01009887 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009888 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009889 trans = NULL;
9890 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009891 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009892 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009893 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009894 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009895 return ret;
9896}
9897
Josef Bacik0af3d002010-06-21 14:48:16 -04009898int btrfs_prealloc_file_range(struct inode *inode, int mode,
9899 u64 start, u64 num_bytes, u64 min_size,
9900 loff_t actual_len, u64 *alloc_hint)
9901{
9902 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9903 min_size, actual_len, alloc_hint,
9904 NULL);
9905}
9906
9907int btrfs_prealloc_file_range_trans(struct inode *inode,
9908 struct btrfs_trans_handle *trans, int mode,
9909 u64 start, u64 num_bytes, u64 min_size,
9910 loff_t actual_len, u64 *alloc_hint)
9911{
9912 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9913 min_size, actual_len, alloc_hint, trans);
9914}
9915
Chris Masone6dcd2d2008-07-17 12:53:50 -04009916static int btrfs_set_page_dirty(struct page *page)
9917{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009918 return __set_page_dirty_nobuffers(page);
9919}
9920
Al Viro10556cb22011-06-20 19:28:19 -04009921static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009922{
Li Zefanb83cc962010-12-20 16:04:08 +08009923 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009924 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009925
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009926 if (mask & MAY_WRITE &&
9927 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9928 if (btrfs_root_readonly(root))
9929 return -EROFS;
9930 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9931 return -EACCES;
9932 }
Al Viro2830ba72011-06-20 19:16:29 -04009933 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009934}
Chris Mason39279cc2007-06-12 06:35:45 -04009935
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009936static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9937{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009938 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009939 struct btrfs_trans_handle *trans;
9940 struct btrfs_root *root = BTRFS_I(dir)->root;
9941 struct inode *inode = NULL;
9942 u64 objectid;
9943 u64 index;
9944 int ret = 0;
9945
9946 /*
9947 * 5 units required for adding orphan entry
9948 */
9949 trans = btrfs_start_transaction(root, 5);
9950 if (IS_ERR(trans))
9951 return PTR_ERR(trans);
9952
9953 ret = btrfs_find_free_ino(root, &objectid);
9954 if (ret)
9955 goto out;
9956
9957 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009958 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009959 if (IS_ERR(inode)) {
9960 ret = PTR_ERR(inode);
9961 inode = NULL;
9962 goto out;
9963 }
9964
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009965 inode->i_fop = &btrfs_file_operations;
9966 inode->i_op = &btrfs_file_inode_operations;
9967
9968 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009969
Chris Masonb0d5d102014-09-08 13:08:51 -07009970 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9971 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009972 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009973
9974 ret = btrfs_update_inode(trans, root, inode);
9975 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009976 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009977 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009978 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009979 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009980
Filipe Manana5762b5c2014-08-01 00:10:32 +01009981 /*
9982 * We set number of links to 0 in btrfs_new_inode(), and here we set
9983 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9984 * through:
9985 *
9986 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9987 */
9988 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009989 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009990 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009991 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009992out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009993 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009994 if (ret && inode)
9995 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009996 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009997 return ret;
9998}
9999
David Sterba5cdc84b2018-07-18 20:32:52 +020010000void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -040010001{
David Sterba5cdc84b2018-07-18 20:32:52 +020010002 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -040010003 unsigned long index = start >> PAGE_SHIFT;
10004 unsigned long end_index = end >> PAGE_SHIFT;
10005 struct page *page;
10006
10007 while (index <= end_index) {
10008 page = find_get_page(inode->i_mapping, index);
10009 ASSERT(page); /* Pages should be in the extent_io_tree */
10010 set_page_writeback(page);
10011 put_page(page);
10012 index++;
10013 }
10014}
10015
Omar Sandovaled46ff32016-11-03 10:28:14 -070010016#ifdef CONFIG_SWAP
10017/*
10018 * Add an entry indicating a block group or device which is pinned by a
10019 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
10020 * negative errno on failure.
10021 */
10022static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
10023 bool is_block_group)
10024{
10025 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10026 struct btrfs_swapfile_pin *sp, *entry;
10027 struct rb_node **p;
10028 struct rb_node *parent = NULL;
10029
10030 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10031 if (!sp)
10032 return -ENOMEM;
10033 sp->ptr = ptr;
10034 sp->inode = inode;
10035 sp->is_block_group = is_block_group;
Filipe Manana501fdd12021-02-05 12:55:37 +000010036 sp->bg_extent_count = 1;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010037
10038 spin_lock(&fs_info->swapfile_pins_lock);
10039 p = &fs_info->swapfile_pins.rb_node;
10040 while (*p) {
10041 parent = *p;
10042 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10043 if (sp->ptr < entry->ptr ||
10044 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10045 p = &(*p)->rb_left;
10046 } else if (sp->ptr > entry->ptr ||
10047 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10048 p = &(*p)->rb_right;
10049 } else {
Filipe Manana501fdd12021-02-05 12:55:37 +000010050 if (is_block_group)
10051 entry->bg_extent_count++;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010052 spin_unlock(&fs_info->swapfile_pins_lock);
10053 kfree(sp);
10054 return 1;
10055 }
10056 }
10057 rb_link_node(&sp->node, parent, p);
10058 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10059 spin_unlock(&fs_info->swapfile_pins_lock);
10060 return 0;
10061}
10062
10063/* Free all of the entries pinned by this swapfile. */
10064static void btrfs_free_swapfile_pins(struct inode *inode)
10065{
10066 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10067 struct btrfs_swapfile_pin *sp;
10068 struct rb_node *node, *next;
10069
10070 spin_lock(&fs_info->swapfile_pins_lock);
10071 node = rb_first(&fs_info->swapfile_pins);
10072 while (node) {
10073 next = rb_next(node);
10074 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10075 if (sp->inode == inode) {
10076 rb_erase(&sp->node, &fs_info->swapfile_pins);
Filipe Manana501fdd12021-02-05 12:55:37 +000010077 if (sp->is_block_group) {
10078 btrfs_dec_block_group_swap_extents(sp->ptr,
10079 sp->bg_extent_count);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010080 btrfs_put_block_group(sp->ptr);
Filipe Manana501fdd12021-02-05 12:55:37 +000010081 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010082 kfree(sp);
10083 }
10084 node = next;
10085 }
10086 spin_unlock(&fs_info->swapfile_pins_lock);
10087}
10088
10089struct btrfs_swap_info {
10090 u64 start;
10091 u64 block_start;
10092 u64 block_len;
10093 u64 lowest_ppage;
10094 u64 highest_ppage;
10095 unsigned long nr_pages;
10096 int nr_extents;
10097};
10098
10099static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10100 struct btrfs_swap_info *bsi)
10101{
10102 unsigned long nr_pages;
Filipe Mananaf8c3ec22021-12-16 15:00:32 +000010103 unsigned long max_pages;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010104 u64 first_ppage, first_ppage_reported, next_ppage;
10105 int ret;
10106
Filipe Mananaf8c3ec22021-12-16 15:00:32 +000010107 /*
10108 * Our swapfile may have had its size extended after the swap header was
10109 * written. In that case activating the swapfile should not go beyond
10110 * the max size set in the swap header.
10111 */
10112 if (bsi->nr_pages >= sis->max)
10113 return 0;
10114
10115 max_pages = sis->max - bsi->nr_pages;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010116 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10117 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10118 PAGE_SIZE) >> PAGE_SHIFT;
10119
10120 if (first_ppage >= next_ppage)
10121 return 0;
10122 nr_pages = next_ppage - first_ppage;
Filipe Mananaf8c3ec22021-12-16 15:00:32 +000010123 nr_pages = min(nr_pages, max_pages);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010124
10125 first_ppage_reported = first_ppage;
10126 if (bsi->start == 0)
10127 first_ppage_reported++;
10128 if (bsi->lowest_ppage > first_ppage_reported)
10129 bsi->lowest_ppage = first_ppage_reported;
10130 if (bsi->highest_ppage < (next_ppage - 1))
10131 bsi->highest_ppage = next_ppage - 1;
10132
10133 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10134 if (ret < 0)
10135 return ret;
10136 bsi->nr_extents += ret;
10137 bsi->nr_pages += nr_pages;
10138 return 0;
10139}
10140
10141static void btrfs_swap_deactivate(struct file *file)
10142{
10143 struct inode *inode = file_inode(file);
10144
10145 btrfs_free_swapfile_pins(inode);
10146 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10147}
10148
10149static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10150 sector_t *span)
10151{
10152 struct inode *inode = file_inode(file);
Filipe Manana6fc9e582021-02-05 12:55:38 +000010153 struct btrfs_root *root = BTRFS_I(inode)->root;
10154 struct btrfs_fs_info *fs_info = root->fs_info;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010155 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10156 struct extent_state *cached_state = NULL;
10157 struct extent_map *em = NULL;
10158 struct btrfs_device *device = NULL;
10159 struct btrfs_swap_info bsi = {
10160 .lowest_ppage = (sector_t)-1ULL,
10161 };
10162 int ret = 0;
10163 u64 isize;
10164 u64 start;
10165
10166 /*
10167 * If the swap file was just created, make sure delalloc is done. If the
10168 * file changes again after this, the user is doing something stupid and
10169 * we don't really care.
10170 */
10171 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10172 if (ret)
10173 return ret;
10174
10175 /*
10176 * The inode is locked, so these flags won't change after we check them.
10177 */
10178 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10179 btrfs_warn(fs_info, "swapfile must not be compressed");
10180 return -EINVAL;
10181 }
10182 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10183 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10184 return -EINVAL;
10185 }
10186 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10187 btrfs_warn(fs_info, "swapfile must not be checksummed");
10188 return -EINVAL;
10189 }
10190
10191 /*
10192 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010193 * under us. The exclop protection makes sure they aren't running/won't
10194 * run concurrently while we are mapping the swap extents, and
10195 * fs_info->swapfile_pins prevents them from running while the swap
10196 * file is active and moving the extents. Note that this also prevents
10197 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010198 * really worth the trouble to allow it.
10199 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010200 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010201 btrfs_warn(fs_info,
10202 "cannot activate swapfile while exclusive operation is running");
10203 return -EBUSY;
10204 }
Filipe Manana6fc9e582021-02-05 12:55:38 +000010205
10206 /*
10207 * Prevent snapshot creation while we are activating the swap file.
10208 * We do not want to race with snapshot creation. If snapshot creation
10209 * already started before we bumped nr_swapfiles from 0 to 1 and
10210 * completes before the first write into the swap file after it is
10211 * activated, than that write would fallback to COW.
10212 */
10213 if (!btrfs_drew_try_write_lock(&root->snapshot_lock)) {
10214 btrfs_exclop_finish(fs_info);
10215 btrfs_warn(fs_info,
10216 "cannot activate swapfile because snapshot creation is in progress");
10217 return -EINVAL;
10218 }
Omar Sandovaled46ff32016-11-03 10:28:14 -070010219 /*
10220 * Snapshots can create extents which require COW even if NODATACOW is
10221 * set. We use this counter to prevent snapshots. We must increment it
10222 * before walking the extents because we don't want a concurrent
10223 * snapshot to run after we've already checked the extents.
Kaiwen Hua044bca2022-03-23 15:10:32 +080010224 *
10225 * It is possible that subvolume is marked for deletion but still not
10226 * removed yet. To prevent this race, we check the root status before
10227 * activating the swapfile.
Omar Sandovaled46ff32016-11-03 10:28:14 -070010228 */
Kaiwen Hua044bca2022-03-23 15:10:32 +080010229 spin_lock(&root->root_item_lock);
10230 if (btrfs_root_dead(root)) {
10231 spin_unlock(&root->root_item_lock);
10232
10233 btrfs_exclop_finish(fs_info);
10234 btrfs_warn(fs_info,
10235 "cannot activate swapfile because subvolume %llu is being deleted",
10236 root->root_key.objectid);
10237 return -EPERM;
10238 }
Filipe Manana6fc9e582021-02-05 12:55:38 +000010239 atomic_inc(&root->nr_swapfiles);
Kaiwen Hua044bca2022-03-23 15:10:32 +080010240 spin_unlock(&root->root_item_lock);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010241
10242 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10243
10244 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10245 start = 0;
10246 while (start < isize) {
10247 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010248 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010249 u64 len = isize - start;
10250
Omar Sandoval39b07b52019-12-02 17:34:23 -080010251 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010252 if (IS_ERR(em)) {
10253 ret = PTR_ERR(em);
10254 goto out;
10255 }
10256
10257 if (em->block_start == EXTENT_MAP_HOLE) {
10258 btrfs_warn(fs_info, "swapfile must not have holes");
10259 ret = -EINVAL;
10260 goto out;
10261 }
10262 if (em->block_start == EXTENT_MAP_INLINE) {
10263 /*
10264 * It's unlikely we'll ever actually find ourselves
10265 * here, as a file small enough to fit inline won't be
10266 * big enough to store more than the swap header, but in
10267 * case something changes in the future, let's catch it
10268 * here rather than later.
10269 */
10270 btrfs_warn(fs_info, "swapfile must not be inline");
10271 ret = -EINVAL;
10272 goto out;
10273 }
10274 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10275 btrfs_warn(fs_info, "swapfile must not be compressed");
10276 ret = -EINVAL;
10277 goto out;
10278 }
10279
10280 logical_block_start = em->block_start + (start - em->start);
10281 len = min(len, em->len - (start - em->start));
10282 free_extent_map(em);
10283 em = NULL;
10284
Boris Burkova84d5d42020-08-18 11:00:05 -070010285 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010286 if (ret < 0) {
10287 goto out;
10288 } else if (ret) {
10289 ret = 0;
10290 } else {
10291 btrfs_warn(fs_info,
10292 "swapfile must not be copy-on-write");
10293 ret = -EINVAL;
10294 goto out;
10295 }
10296
10297 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10298 if (IS_ERR(em)) {
10299 ret = PTR_ERR(em);
10300 goto out;
10301 }
10302
10303 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10304 btrfs_warn(fs_info,
10305 "swapfile must have single data profile");
10306 ret = -EINVAL;
10307 goto out;
10308 }
10309
10310 if (device == NULL) {
10311 device = em->map_lookup->stripes[0].dev;
10312 ret = btrfs_add_swapfile_pin(inode, device, false);
10313 if (ret == 1)
10314 ret = 0;
10315 else if (ret)
10316 goto out;
10317 } else if (device != em->map_lookup->stripes[0].dev) {
10318 btrfs_warn(fs_info, "swapfile must be on one device");
10319 ret = -EINVAL;
10320 goto out;
10321 }
10322
10323 physical_block_start = (em->map_lookup->stripes[0].physical +
10324 (logical_block_start - em->start));
10325 len = min(len, em->len - (logical_block_start - em->start));
10326 free_extent_map(em);
10327 em = NULL;
10328
10329 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10330 if (!bg) {
10331 btrfs_warn(fs_info,
10332 "could not find block group containing swapfile");
10333 ret = -EINVAL;
10334 goto out;
10335 }
10336
Filipe Manana501fdd12021-02-05 12:55:37 +000010337 if (!btrfs_inc_block_group_swap_extents(bg)) {
10338 btrfs_warn(fs_info,
10339 "block group for swapfile at %llu is read-only%s",
10340 bg->start,
10341 atomic_read(&fs_info->scrubs_running) ?
10342 " (scrub running)" : "");
10343 btrfs_put_block_group(bg);
10344 ret = -EINVAL;
10345 goto out;
10346 }
10347
Omar Sandovaled46ff32016-11-03 10:28:14 -070010348 ret = btrfs_add_swapfile_pin(inode, bg, true);
10349 if (ret) {
10350 btrfs_put_block_group(bg);
10351 if (ret == 1)
10352 ret = 0;
10353 else
10354 goto out;
10355 }
10356
10357 if (bsi.block_len &&
10358 bsi.block_start + bsi.block_len == physical_block_start) {
10359 bsi.block_len += len;
10360 } else {
10361 if (bsi.block_len) {
10362 ret = btrfs_add_swap_extent(sis, &bsi);
10363 if (ret)
10364 goto out;
10365 }
10366 bsi.start = start;
10367 bsi.block_start = physical_block_start;
10368 bsi.block_len = len;
10369 }
10370
10371 start += len;
10372 }
10373
10374 if (bsi.block_len)
10375 ret = btrfs_add_swap_extent(sis, &bsi);
10376
10377out:
10378 if (!IS_ERR_OR_NULL(em))
10379 free_extent_map(em);
10380
10381 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10382
10383 if (ret)
10384 btrfs_swap_deactivate(file);
10385
Filipe Manana6fc9e582021-02-05 12:55:38 +000010386 btrfs_drew_write_unlock(&root->snapshot_lock);
10387
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010388 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010389
10390 if (ret)
10391 return ret;
10392
10393 if (device)
10394 sis->bdev = device->bdev;
10395 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10396 sis->max = bsi.nr_pages;
10397 sis->pages = bsi.nr_pages - 1;
10398 sis->highest_bit = bsi.nr_pages - 1;
10399 return bsi.nr_extents;
10400}
10401#else
10402static void btrfs_swap_deactivate(struct file *file)
10403{
10404}
10405
10406static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10407 sector_t *span)
10408{
10409 return -EOPNOTSUPP;
10410}
10411#endif
10412
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010413static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010414 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010415 .lookup = btrfs_lookup,
10416 .create = btrfs_create,
10417 .unlink = btrfs_unlink,
10418 .link = btrfs_link,
10419 .mkdir = btrfs_mkdir,
10420 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010421 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010422 .symlink = btrfs_symlink,
10423 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010424 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010425 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010426 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010427 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010428 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010429 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010430 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010431};
Yan, Zheng76dda932009-09-21 16:00:26 -040010432
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010433static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010434 .llseek = generic_file_llseek,
10435 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010436 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010437 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010438 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010439#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010440 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010441#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010442 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010443 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010444};
10445
Chris Mason35054392009-01-21 13:11:13 -050010446/*
10447 * btrfs doesn't support the bmap operation because swapfiles
10448 * use bmap to make a mapping of extents in the file. They assume
10449 * these extents won't change over the life of the file and they
10450 * use the bmap result to do IO directly to the drive.
10451 *
10452 * the btrfs bmap call would return logical addresses that aren't
10453 * suitable for IO and they also will change frequently as COW
10454 * operations happen. So, swapfile + btrfs == corruption.
10455 *
10456 * For now we're avoiding this by dropping bmap.
10457 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010458static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010459 .readpage = btrfs_readpage,
10460 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010461 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010462 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010463 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010464 .invalidatepage = btrfs_invalidatepage,
10465 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010466#ifdef CONFIG_MIGRATION
10467 .migratepage = btrfs_migratepage,
10468#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010469 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010470 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010471 .swap_activate = btrfs_swap_activate,
10472 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010473};
10474
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010475static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010476 .getattr = btrfs_getattr,
10477 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010478 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010479 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010480 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010481 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010482 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010483 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010484};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010485static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010486 .getattr = btrfs_getattr,
10487 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010488 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010489 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010490 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010491 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010492 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010493};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010494static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010495 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010496 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010497 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010498 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010499 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010500 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010501};
Yan, Zheng76dda932009-09-21 16:00:26 -040010502
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010503const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010504 .d_delete = btrfs_dentry_delete,
10505};