blob: 982187b0cd595ae54c74b459eab8008df739460b [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;
David Sterba20e55062015-11-19 11:42:28 +010074static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76static struct kmem_cache *btrfs_inode_cachep;
77struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050079struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000080struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
Eric Sandeen3972f262013-01-12 02:57:22 +000082static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000083static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040084static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030085static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050086 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030087 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030088 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030089static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
90 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080091 u64 block_len, u64 orig_block_len,
92 u64 ram_bytes, int compress_type,
93 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040094
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030095static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080096 const u64 offset, const u64 bytes,
97 const bool uptodate);
98
99/*
100 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100101 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800102 *
103 * NOTE: caller must ensure that when an error happens, it can not call
104 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
105 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
106 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200107 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800108 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300109static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200110 struct page *locked_page,
111 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800112{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900113 unsigned long index = offset >> PAGE_SHIFT;
114 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200115 u64 page_start = page_offset(locked_page);
116 u64 page_end = page_start + PAGE_SIZE - 1;
117
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900118 struct page *page;
119
120 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300121 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900122 index++;
123 if (!page)
124 continue;
125 ClearPagePrivate2(page);
126 put_page(page);
127 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200128
129 /*
130 * In case this page belongs to the delalloc range being instantiated
131 * then skip it, since the first page of a range is going to be
132 * properly cleaned up by the caller of run_delalloc_range
133 */
134 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
135 offset += PAGE_SIZE;
136 bytes -= PAGE_SIZE;
137 }
138
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300139 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800140}
141
Eric Sandeen48a3b632013-04-25 20:41:01 +0000142static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400143
Josef Bacik6a3891c2015-03-16 17:38:52 -0400144#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
145void btrfs_test_inode_set_ops(struct inode *inode)
146{
147 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
148}
149#endif
150
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000151static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500152 struct inode *inode, struct inode *dir,
153 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500154{
155 int err;
156
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000157 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500158 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500159 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500160 return err;
161}
162
Chris Masond352ac62008-09-29 15:18:18 -0400163/*
Chris Masonc8b97812008-10-29 14:49:59 -0400164 * this does all the hard work for inserting an inline extent into
165 * the btree. The caller should have done a btrfs_drop_extents so that
166 * no overlapping inline items exist in the btree
167 */
Chris Mason40f76582014-05-21 13:35:51 -0700168static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000169 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400170 struct btrfs_root *root, struct inode *inode,
171 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000172 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct page **compressed_pages)
174{
Chris Masonc8b97812008-10-29 14:49:59 -0400175 struct extent_buffer *leaf;
176 struct page *page = NULL;
177 char *kaddr;
178 unsigned long ptr;
179 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400180 int ret;
181 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400182 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400183
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800184 ASSERT((compressed_size > 0 && compressed_pages) ||
185 (compressed_size == 0 && !compressed_pages));
186
Li Zefanfe3f5662011-03-28 08:30:38 +0000187 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400188 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400189
Chris Masonc8b97812008-10-29 14:49:59 -0400190 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000191
192 if (!extent_inserted) {
193 struct btrfs_key key;
194 size_t datasize;
195
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200196 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000197 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200198 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000199
200 datasize = btrfs_file_extent_calc_inline_size(cur_size);
201 path->leave_spinning = 1;
202 ret = btrfs_insert_empty_item(trans, root, path, &key,
203 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200204 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000205 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400206 }
207 leaf = path->nodes[0];
208 ei = btrfs_item_ptr(leaf, path->slots[0],
209 struct btrfs_file_extent_item);
210 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
211 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
212 btrfs_set_file_extent_encryption(leaf, ei, 0);
213 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
214 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
215 ptr = btrfs_file_extent_inline_start(ei);
216
Li Zefan261507a02010-12-17 14:21:50 +0800217 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400218 struct page *cpage;
219 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500220 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400221 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500222 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300223 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400224
Cong Wang7ac687d2011-11-25 23:14:28 +0800225 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400226 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800227 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400228
229 i++;
230 ptr += cur_size;
231 compressed_size -= cur_size;
232 }
233 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800234 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 } else {
236 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300237 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800239 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100240 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800242 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300243 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 }
245 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000246 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400247
Yan, Zhengc2167752009-11-12 09:34:21 +0000248 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500249 * We align size to sectorsize for inline extents just for simplicity
250 * sake.
251 */
252 size = ALIGN(size, root->fs_info->sectorsize);
253 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
254 if (ret)
255 goto fail;
256
257 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300279static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400280 u64 end, size_t compressed_size,
281 int compress_type,
282 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400283{
Nikolay Borisova0349402020-06-03 08:55:12 +0300284 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300287 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300319 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
Nikolay Borisova0349402020-06-03 08:55:12 +0300328 ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end,
329 NULL, 1, 1, extent_item_size,
330 &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300339 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Nikolay Borisova0349402020-06-03 08:55:12 +0300350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
351 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
Nikolay Borisov97db1202019-03-12 17:20:24 +0200375struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500376 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500377 struct page *locked_page;
378 u64 start;
379 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600380 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500381 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700382 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500383 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200384 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500385};
386
Nikolay Borisov97db1202019-03-12 17:20:24 +0200387struct async_cow {
388 /* Number of chunks in flight; must be first in the structure */
389 atomic_t num_chunks;
390 struct async_chunk chunks[];
391};
392
393static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500394 u64 start, u64 ram_size,
395 u64 compressed_size,
396 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800397 unsigned long nr_pages,
398 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500399{
400 struct async_extent *async_extent;
401
402 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100403 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500404 async_extent->start = start;
405 async_extent->ram_size = ram_size;
406 async_extent->compressed_size = compressed_size;
407 async_extent->pages = pages;
408 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800409 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500410 list_add_tail(&async_extent->list, &cow->extents);
411 return 0;
412}
413
Qu Wenruo42c16da2019-07-01 05:12:46 +0000414/*
415 * Check if the inode has flags compatible with compression
416 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300417static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000418{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300419 if (inode->flags & BTRFS_INODE_NODATACOW ||
420 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000421 return false;
422 return true;
423}
424
425/*
426 * Check if the inode needs to be submitted to compression, based on mount
427 * options, defragmentation, properties or heuristics.
428 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300429static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
430 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800431{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300432 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800433
Nikolay Borisov808a1292020-06-03 08:55:27 +0300434 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000435 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
436 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300437 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000438 return 0;
439 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800440 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400441 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800442 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200443 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300444 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200445 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800446 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300447 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800448 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400449 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300450 inode->flags & BTRFS_INODE_COMPRESS ||
451 inode->prop_compress)
452 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800453 return 0;
454}
455
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200456static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800457 u64 start, u64 end, u64 num_bytes, u64 small_write)
458{
459 /* If this is a small write inside eof, kick off a defrag */
460 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200461 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800462 btrfs_add_inode_defrag(NULL, inode);
463}
464
Chris Masonc8b97812008-10-29 14:49:59 -0400465/*
Chris Mason771ed682008-11-06 22:02:51 -0500466 * we create compressed extents in two phases. The first
467 * phase compresses a range of pages that have already been
468 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400469 *
Chris Mason771ed682008-11-06 22:02:51 -0500470 * This is done inside an ordered work queue, and the compression
471 * is spread across many cpus. The actual IO submission is step
472 * two, and the ordered work queue takes care of making sure that
473 * happens in the same order things were put onto the queue by
474 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400475 *
Chris Mason771ed682008-11-06 22:02:51 -0500476 * If this code finds it can't get good compression, it puts an
477 * entry onto the work queue to write the uncompressed bytes. This
478 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300479 * are written in the same order that the flusher thread sent them
480 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400481 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300482static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400483{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200484 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400485 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400486 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200487 u64 start = async_chunk->start;
488 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400489 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400490 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400491 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400492 struct page **pages = NULL;
493 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400494 unsigned long total_compressed = 0;
495 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400496 int i;
497 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400498 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300499 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400500 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400501
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200502 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
503 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400504
Josef Bacikd98da492019-10-11 09:03:54 -0400505 /*
506 * We need to save i_size before now because it could change in between
507 * us evaluating the size and assigning it. This is because we lock and
508 * unlock the page in truncate and fallocate, and then modify the i_size
509 * later on.
510 *
511 * The barriers are to emulate READ_ONCE, remove that once i_size_read
512 * does that for us.
513 */
514 barrier();
515 i_size = i_size_read(inode);
516 barrier();
517 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400518again:
519 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300520 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100521 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
522 nr_pages = min_t(unsigned long, nr_pages,
523 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400524
Chris Masonf03d9301f2009-02-04 09:31:06 -0500525 /*
526 * we don't want to send crud past the end of i_size through
527 * compression, that's just a waste of CPU time. So, if the
528 * end of the file is before the start of our current
529 * requested range of bytes, we bail out to the uncompressed
530 * cleanup code that can deal with all of this.
531 *
532 * It isn't really the fastest way to fix things, but this is a
533 * very uncommon corner.
534 */
535 if (actual_end <= start)
536 goto cleanup_and_bail_uncompressed;
537
Chris Masonc8b97812008-10-29 14:49:59 -0400538 total_compressed = actual_end - start;
539
Shilong Wang4bcbb332014-10-07 18:44:35 -0400540 /*
541 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400542 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400543 */
544 if (total_compressed <= blocksize &&
545 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
546 goto cleanup_and_bail_uncompressed;
547
David Sterba069eac72017-02-14 19:36:54 +0100548 total_compressed = min_t(unsigned long, total_compressed,
549 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400550 total_in = 0;
551 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400552
Chris Mason771ed682008-11-06 22:02:51 -0500553 /*
554 * we do compression for mount -o compress and when the
555 * inode has not been flagged as nocompress. This flag can
556 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400557 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300558 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400559 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100560 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800561 if (!pages) {
562 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100563 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800564 goto cont;
565 }
Chris Mason179e29e2007-11-01 11:28:41 -0400566
David Sterbaeec63c62017-07-17 19:41:31 +0200567 if (BTRFS_I(inode)->defrag_compress)
568 compress_type = BTRFS_I(inode)->defrag_compress;
569 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200570 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800571
Chris Mason4adaa612013-03-26 13:07:00 -0400572 /*
573 * we need to call clear_page_dirty_for_io on each
574 * page in the range. Otherwise applications with the file
575 * mmap'd can wander in and change the page contents while
576 * we are compressing them.
577 *
578 * If the compression fails for any reason, we set the pages
579 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300580 *
581 * Note that the remaining part is redirtied, the start pointer
582 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400583 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300584 if (!redirty) {
585 extent_range_clear_dirty_for_io(inode, start, end);
586 redirty = 1;
587 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200588
589 /* Compression level is applied here and only here */
590 ret = btrfs_compress_pages(
591 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800592 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100593 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100594 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800595 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100596 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400597
598 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100599 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100600 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400601 char *kaddr;
602
603 /* zero the tail end of the last page, we might be
604 * sending it down to disk
605 */
606 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800607 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400608 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300609 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800610 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400611 }
612 will_compress = 1;
613 }
614 }
Li Zefan560f7d72011-09-08 10:22:01 +0800615cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400616 if (start == 0) {
617 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300618 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400619 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500620 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400621 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300622 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
623 0, BTRFS_COMPRESS_NONE,
624 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400625 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500626 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300627 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000628 total_compressed,
629 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400630 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100631 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400632 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400633 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
634 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100635 unsigned long page_error_op;
636
Filipe Mananae6eb4312014-10-10 10:45:12 +0100637 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400638
Chris Mason771ed682008-11-06 22:02:51 -0500639 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100640 * inline extent creation worked or returned error,
641 * we don't need to create any more async work items.
642 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400643 *
644 * We use DO_ACCOUNTING here because we need the
645 * delalloc_release_metadata to be done _after_ we drop
646 * our outstanding extent for clearing delalloc for this
647 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500648 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300649 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
650 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300651 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800652 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400653 PAGE_CLEAR_DIRTY |
654 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100655 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400656 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300657
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800658 /*
659 * Ensure we only free the compressed pages if we have
660 * them allocated, as we can still reach here with
661 * inode_need_compress() == false.
662 */
663 if (pages) {
664 for (i = 0; i < nr_pages; i++) {
665 WARN_ON(pages[i]->mapping);
666 put_page(pages[i]);
667 }
668 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300669 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300670 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400671 }
672 }
673
674 if (will_compress) {
675 /*
676 * we aren't doing an inline extent round the compressed size
677 * up to a block size boundary so the allocator does sane
678 * things
679 */
Qu Wenruofda28322013-02-26 08:10:22 +0000680 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400681
682 /*
683 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300684 * win, compare the page count read with the blocks on disk,
685 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400686 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300687 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300688 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300689 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700690
691 /*
692 * The async work queues will take care of doing actual
693 * allocation on disk for these compressed pages, and
694 * will submit them to the elevator.
695 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200696 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100697 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700698 compress_type);
699
Timofey Titovets11708622017-10-03 18:06:01 +0300700 if (start + total_in < end) {
701 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700702 pages = NULL;
703 cond_resched();
704 goto again;
705 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300706 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400707 }
708 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700709 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400710 /*
711 * the compression code ran but failed to make things smaller,
712 * free any pages it allocated and our page pointer array
713 */
David Sterba4d3a8002017-02-14 19:04:07 +0100714 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400715 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300716 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400717 }
718 kfree(pages);
719 pages = NULL;
720 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100721 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400722
723 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400724 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200725 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500726 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500727 }
Chris Masonc8b97812008-10-29 14:49:59 -0400728 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500729cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700730 /*
731 * No compression, but we still need to write the pages in the file
732 * we've been given so far. redirty the locked page if it corresponds
733 * to our extent and set things up for the async work queue to run
734 * cow_file_range to do the normal delalloc dance.
735 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700736 if (async_chunk->locked_page &&
737 (page_offset(async_chunk->locked_page) >= start &&
738 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200739 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700740 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700741 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700742
743 if (redirty)
744 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200745 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700746 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300747 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500748
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300749 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500750}
Chris Mason771ed682008-11-06 22:02:51 -0500751
Filipe Manana40ae8372014-10-06 22:14:24 +0100752static void free_async_extent_pages(struct async_extent *async_extent)
753{
754 int i;
755
756 if (!async_extent->pages)
757 return;
758
759 for (i = 0; i < async_extent->nr_pages; i++) {
760 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300761 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100762 }
763 kfree(async_extent->pages);
764 async_extent->nr_pages = 0;
765 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500766}
767
768/*
769 * phase two of compressed writeback. This is the ordered portion
770 * of the code, which only gets called in the order the work was
771 * queued. We walk all the async extents created by compress_file_range
772 * and send them down to the disk.
773 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200774static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500775{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300776 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
777 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500778 struct async_extent *async_extent;
779 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500780 struct btrfs_key ins;
781 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300782 struct btrfs_root *root = inode->root;
783 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500784 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500785
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500786again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200787 while (!list_empty(&async_chunk->extents)) {
788 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500789 struct async_extent, list);
790 list_del(&async_extent->list);
791
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500792retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200793 lock_extent(io_tree, async_extent->start,
794 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500795 /* did the compression code fall back to uncompressed IO? */
796 if (!async_extent->pages) {
797 int page_started = 0;
798 unsigned long nr_written = 0;
799
Chris Mason771ed682008-11-06 22:02:51 -0500800 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300801 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500802 async_extent->start,
803 async_extent->start +
804 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300805 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500806
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100807 /* JDM XXX */
808
Chris Mason771ed682008-11-06 22:02:51 -0500809 /*
810 * if page_started, cow_file_range inserted an
811 * inline extent and took care of all the unlocking
812 * and IO for us. Otherwise, we need to submit
813 * all those pages down to the drive.
814 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500815 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300816 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200817 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500818 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500819 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500820 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700821 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200822 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500823 kfree(async_extent);
824 cond_resched();
825 continue;
826 }
827
Wang Xiaoguang18513092016-07-25 15:51:40 +0800828 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500829 async_extent->compressed_size,
830 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800831 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500832 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100833 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500834
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400835 if (ret == -ENOSPC) {
836 unlock_extent(io_tree, async_extent->start,
837 async_extent->start +
838 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800839
840 /*
841 * we need to redirty the pages if we decide to
842 * fallback to uncompressed IO, otherwise we
843 * will not submit these pages down to lower
844 * layers.
845 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300846 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800847 async_extent->start,
848 async_extent->start +
849 async_extent->ram_size - 1);
850
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100851 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400852 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500853 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500854 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000855 /*
856 * here we're doing allocation and writeback of the
857 * compressed pages
858 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300859 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800860 async_extent->ram_size, /* len */
861 async_extent->start, /* orig_start */
862 ins.objectid, /* block_start */
863 ins.offset, /* block_len */
864 ins.offset, /* orig_block_len */
865 async_extent->ram_size, /* ram_bytes */
866 async_extent->compress_type,
867 BTRFS_ORDERED_COMPRESSED);
868 if (IS_ERR(em))
869 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800871 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300873 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800874 async_extent->start,
875 ins.objectid,
876 async_extent->ram_size,
877 ins.offset,
878 BTRFS_ORDERED_COMPRESSED,
879 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100880 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300881 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100882 async_extent->start +
883 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500884 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100885 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400886 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500887
Chris Mason771ed682008-11-06 22:02:51 -0500888 /*
889 * clear dirty, set writeback and unlock the pages.
890 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300891 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400892 async_extent->start +
893 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400894 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
895 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400896 PAGE_SET_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300897 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500898 async_extent->ram_size,
899 ins.objectid,
900 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600901 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700902 async_chunk->write_flags,
903 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100904 struct page *p = async_extent->pages[0];
905 const u64 start = async_extent->start;
906 const u64 end = start + async_extent->ram_size - 1;
907
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300908 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200909 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200910
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100911 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300912 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100913 PAGE_END_WRITEBACK |
914 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100915 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100916 }
Chris Mason771ed682008-11-06 22:02:51 -0500917 alloc_hint = ins.objectid + ins.offset;
918 kfree(async_extent);
919 cond_resched();
920 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100921 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500922out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400923 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400924 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300926 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500927 async_extent->start +
928 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400929 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100930 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400931 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
932 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100933 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
934 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100935 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100936 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500937 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500938}
939
Nikolay Borisov43c69842020-06-03 08:55:02 +0300940static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400941 u64 num_bytes)
942{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300943 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400944 struct extent_map *em;
945 u64 alloc_hint = 0;
946
947 read_lock(&em_tree->lock);
948 em = search_extent_mapping(em_tree, start, num_bytes);
949 if (em) {
950 /*
951 * if block start isn't an actual block number then find the
952 * first block in this inode and use that as a hint. If that
953 * block is also bogus then just don't worry about it.
954 */
955 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
956 free_extent_map(em);
957 em = search_extent_mapping(em_tree, 0, 0);
958 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
959 alloc_hint = em->block_start;
960 if (em)
961 free_extent_map(em);
962 } else {
963 alloc_hint = em->block_start;
964 free_extent_map(em);
965 }
966 }
967 read_unlock(&em_tree->lock);
968
969 return alloc_hint;
970}
971
Chris Mason771ed682008-11-06 22:02:51 -0500972/*
973 * when extent_io.c finds a delayed allocation range in the file,
974 * the call backs end up in this code. The basic idea is to
975 * allocate extents on disk for the range, and create ordered data structs
976 * in ram to track those extents.
977 *
978 * locked_page is the page that writepage had locked already. We use
979 * it to make sure we don't do extra locks or unlocks.
980 *
981 * *page_started is set to one if we unlock locked_page and do everything
982 * required to start IO on it. It may be clean and already done with
983 * IO when we return.
984 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300985static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -0400986 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300987 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300988 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500989{
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300990 struct btrfs_root *root = inode->root;
991 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500992 u64 alloc_hint = 0;
993 u64 num_bytes;
994 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000995 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +0100996 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400997 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500998 struct btrfs_key ins;
999 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001000 unsigned clear_bits;
1001 unsigned long page_ops;
1002 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001003 int ret = 0;
1004
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001005 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001006 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001007 ret = -EINVAL;
1008 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001009 }
Chris Mason771ed682008-11-06 22:02:51 -05001010
Qu Wenruofda28322013-02-26 08:10:22 +00001011 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001012 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001013 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001014
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001015 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001016
Chris Mason771ed682008-11-06 22:02:51 -05001017 if (start == 0) {
1018 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001019 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001020 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001021 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001022 /*
1023 * We use DO_ACCOUNTING here because we need the
1024 * delalloc_release_metadata to be run _after_ we drop
1025 * our outstanding extent for clearing delalloc for this
1026 * range.
1027 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001028 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001029 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001030 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1031 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001032 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1033 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001034 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001035 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001036 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001037 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001038 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001039 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001040 }
1041 }
Chris Masonc8b97812008-10-29 14:49:59 -04001042
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001043 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1044 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001045
Filipe Manana432cd2a2020-06-08 13:32:55 +01001046 /*
1047 * Relocation relies on the relocated extents to have exactly the same
1048 * size as the original extents. Normally writeback for relocation data
1049 * extents follows a NOCOW path because relocation preallocates the
1050 * extents. However, due to an operation such as scrub turning a block
1051 * group to RO mode, it may fallback to COW mode, so we must make sure
1052 * an extent allocated during COW has exactly the requested size and can
1053 * not be split into smaller extents, otherwise relocation breaks and
1054 * fails during the stage where it updates the bytenr of file extent
1055 * items.
1056 */
1057 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1058 min_alloc_size = num_bytes;
1059 else
1060 min_alloc_size = fs_info->sectorsize;
1061
Anand Jain3752d222018-02-15 12:29:38 +08001062 while (num_bytes > 0) {
1063 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001064 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001065 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001066 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001067 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001068 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001069 cur_alloc_size = ins.offset;
1070 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001071
Chris Mason771ed682008-11-06 22:02:51 -05001072 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001073 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001074 start, /* orig_start */
1075 ins.objectid, /* block_start */
1076 ins.offset, /* block_len */
1077 ins.offset, /* orig_block_len */
1078 ram_size, /* ram_bytes */
1079 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001080 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001081 if (IS_ERR(em)) {
1082 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001083 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001084 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001085 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001086
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001087 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1088 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001089 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001090 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001091
Yan Zheng17d217f2008-12-12 10:03:38 -05001092 if (root->root_key.objectid ==
1093 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001094 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001095 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001096 /*
1097 * Only drop cache here, and process as normal.
1098 *
1099 * We must not allow extent_clear_unlock_delalloc()
1100 * at out_unlock label to free meta of this ordered
1101 * extent, as its meta should be freed by
1102 * btrfs_finish_ordered_io().
1103 *
1104 * So we must continue until @start is increased to
1105 * skip current ordered extent.
1106 */
Josef Bacik00361582013-08-14 14:02:47 -04001107 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001108 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001109 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001110 }
1111
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001112 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001113
Chris Masonc8b97812008-10-29 14:49:59 -04001114 /* we're not doing compressed IO, don't unlock the first
1115 * page (which the caller expects to stay locked), don't
1116 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001117 *
1118 * Do set the Private2 bit so we know this page was properly
1119 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001120 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001121 page_ops = unlock ? PAGE_UNLOCK : 0;
1122 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001123
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001124 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001125 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001126 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001127 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001128 if (num_bytes < cur_alloc_size)
1129 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001130 else
Anand Jain3752d222018-02-15 12:29:38 +08001131 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001132 alloc_hint = ins.objectid + ins.offset;
1133 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001134 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001135
1136 /*
1137 * btrfs_reloc_clone_csums() error, since start is increased
1138 * extent_clear_unlock_delalloc() at out_unlock label won't
1139 * free metadata of current ordered extent, we're OK to exit.
1140 */
1141 if (ret)
1142 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001143 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001144out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001145 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001146
Filipe Mananad9f85962014-08-25 10:43:00 +01001147out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001148 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001149out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001150 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001151 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001152out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001153 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1154 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001155 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1156 PAGE_END_WRITEBACK;
1157 /*
1158 * If we reserved an extent for our delalloc range (or a subrange) and
1159 * failed to create the respective ordered extent, then it means that
1160 * when we reserved the extent we decremented the extent's size from
1161 * the data space_info's bytes_may_use counter and incremented the
1162 * space_info's bytes_reserved counter by the same amount. We must make
1163 * sure extent_clear_unlock_delalloc() does not try to decrement again
1164 * the data space_info's bytes_may_use counter, therefore we do not pass
1165 * it the flag EXTENT_CLEAR_DATA_RESV.
1166 */
1167 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001168 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001169 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001170 locked_page,
1171 clear_bits,
1172 page_ops);
1173 start += cur_alloc_size;
1174 if (start >= end)
1175 goto out;
1176 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001177 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001178 clear_bits | EXTENT_CLEAR_DATA_RESV,
1179 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001180 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001181}
Chris Masonc8b97812008-10-29 14:49:59 -04001182
Chris Mason771ed682008-11-06 22:02:51 -05001183/*
1184 * work queue call back to started compression on a file and pages
1185 */
1186static noinline void async_cow_start(struct btrfs_work *work)
1187{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001188 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001189 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001190
Nikolay Borisovb5326272019-03-12 17:20:25 +02001191 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001192
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001193 compressed_extents = compress_file_range(async_chunk);
1194 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001195 btrfs_add_delayed_iput(async_chunk->inode);
1196 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001197 }
Chris Mason771ed682008-11-06 22:02:51 -05001198}
1199
1200/*
1201 * work queue call back to submit previously compressed pages
1202 */
1203static noinline void async_cow_submit(struct btrfs_work *work)
1204{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001205 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1206 work);
1207 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001208 unsigned long nr_pages;
1209
Nikolay Borisovb5326272019-03-12 17:20:25 +02001210 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001211 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001212
David Sterba093258e2018-02-26 16:15:17 +01001213 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001214 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001215 5 * SZ_1M)
1216 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001217
Nikolay Borisov4546d172019-01-03 10:50:03 +02001218 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001219 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001220 * in which case we don't have anything to submit, yet we need to
1221 * always adjust ->async_delalloc_pages as its paired with the init
1222 * happening in cow_file_range_async
1223 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001224 if (async_chunk->inode)
1225 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001226}
Chris Masonc8b97812008-10-29 14:49:59 -04001227
Chris Mason771ed682008-11-06 22:02:51 -05001228static noinline void async_cow_free(struct btrfs_work *work)
1229{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001230 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001231
Nikolay Borisovb5326272019-03-12 17:20:25 +02001232 async_chunk = container_of(work, struct async_chunk, work);
1233 if (async_chunk->inode)
1234 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001235 if (async_chunk->blkcg_css)
1236 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001237 /*
1238 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001239 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001240 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001241 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001242 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001243}
1244
Nikolay Borisov751b6432020-06-03 08:55:22 +03001245static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001246 struct writeback_control *wbc,
1247 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001248 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001249 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001250{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001251 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001252 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001253 struct async_cow *ctx;
1254 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001255 unsigned long nr_pages;
1256 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001257 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1258 int i;
1259 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001260 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001261 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001262
Nikolay Borisov751b6432020-06-03 08:55:22 +03001263 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001264
Nikolay Borisov751b6432020-06-03 08:55:22 +03001265 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001266 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1267 num_chunks = 1;
1268 should_compress = false;
1269 } else {
1270 should_compress = true;
1271 }
1272
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001273 nofs_flag = memalloc_nofs_save();
1274 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1275 memalloc_nofs_restore(nofs_flag);
1276
Nikolay Borisov97db1202019-03-12 17:20:24 +02001277 if (!ctx) {
1278 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1279 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1280 EXTENT_DO_ACCOUNTING;
1281 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1282 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1283 PAGE_SET_ERROR;
1284
Nikolay Borisov751b6432020-06-03 08:55:22 +03001285 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1286 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001287 return -ENOMEM;
1288 }
1289
1290 async_chunk = ctx->chunks;
1291 atomic_set(&ctx->num_chunks, num_chunks);
1292
1293 for (i = 0; i < num_chunks; i++) {
1294 if (should_compress)
1295 cur_end = min(end, start + SZ_512K - 1);
1296 else
1297 cur_end = end;
1298
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001299 /*
1300 * igrab is called higher up in the call chain, take only the
1301 * lightweight reference for the callback lifetime
1302 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001303 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001304 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001305 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001306 async_chunk[i].start = start;
1307 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001308 async_chunk[i].write_flags = write_flags;
1309 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001310
Chris Mason1d53c9e2019-07-10 12:28:16 -07001311 /*
1312 * The locked_page comes all the way from writepage and its
1313 * the original page we were actually given. As we spread
1314 * this large delalloc region across multiple async_chunk
1315 * structs, only the first struct needs a pointer to locked_page
1316 *
1317 * This way we don't need racey decisions about who is supposed
1318 * to unlock it.
1319 */
1320 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001321 /*
1322 * Depending on the compressibility, the pages might or
1323 * might not go through async. We want all of them to
1324 * be accounted against wbc once. Let's do it here
1325 * before the paths diverge. wbc accounting is used
1326 * only for foreign writeback detection and doesn't
1327 * need full accuracy. Just account the whole thing
1328 * against the first page.
1329 */
1330 wbc_account_cgroup_owner(wbc, locked_page,
1331 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001332 async_chunk[i].locked_page = locked_page;
1333 locked_page = NULL;
1334 } else {
1335 async_chunk[i].locked_page = NULL;
1336 }
1337
Chris Masonec39f762019-07-10 12:28:17 -07001338 if (blkcg_css != blkcg_root_css) {
1339 css_get(blkcg_css);
1340 async_chunk[i].blkcg_css = blkcg_css;
1341 } else {
1342 async_chunk[i].blkcg_css = NULL;
1343 }
1344
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001345 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1346 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001347
Nikolay Borisov97db1202019-03-12 17:20:24 +02001348 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001349 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001350
Nikolay Borisov97db1202019-03-12 17:20:24 +02001351 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001352
Chris Mason771ed682008-11-06 22:02:51 -05001353 *nr_written += nr_pages;
1354 start = cur_end + 1;
1355 }
1356 *page_started = 1;
1357 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001358}
1359
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001360static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001361 u64 bytenr, u64 num_bytes)
1362{
1363 int ret;
1364 struct btrfs_ordered_sum *sums;
1365 LIST_HEAD(list);
1366
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001367 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001368 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001369 if (ret == 0 && list_empty(&list))
1370 return 0;
1371
1372 while (!list_empty(&list)) {
1373 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1374 list_del(&sums->list);
1375 kfree(sums);
1376 }
Liu Bo58113752018-01-31 17:09:13 -07001377 if (ret < 0)
1378 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001379 return 1;
1380}
1381
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001382static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001383 const u64 start, const u64 end,
1384 int *page_started, unsigned long *nr_written)
1385{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001386 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1387 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001388 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001389 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001390 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001391 u64 range_start = start;
1392 u64 count;
1393
1394 /*
1395 * If EXTENT_NORESERVE is set it means that when the buffered write was
1396 * made we had not enough available data space and therefore we did not
1397 * reserve data space for it, since we though we could do NOCOW for the
1398 * respective file range (either there is prealloc extent or the inode
1399 * has the NOCOW bit set).
1400 *
1401 * However when we need to fallback to COW mode (because for example the
1402 * block group for the corresponding extent was turned to RO mode by a
1403 * scrub or relocation) we need to do the following:
1404 *
1405 * 1) We increment the bytes_may_use counter of the data space info.
1406 * If COW succeeds, it allocates a new data extent and after doing
1407 * that it decrements the space info's bytes_may_use counter and
1408 * increments its bytes_reserved counter by the same amount (we do
1409 * this at btrfs_add_reserved_bytes()). So we need to increment the
1410 * bytes_may_use counter to compensate (when space is reserved at
1411 * buffered write time, the bytes_may_use counter is incremented);
1412 *
1413 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1414 * that if the COW path fails for any reason, it decrements (through
1415 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1416 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001417 *
1418 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001419 * space cache inode or an inode of the data relocation tree, we must
1420 * also increment bytes_may_use of the data space_info for the same
1421 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001422 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001423 * group that contains that extent to RO mode and therefore force COW
1424 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001425 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001426 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001427 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001428 if (count > 0 || is_space_ino || is_reloc_ino) {
1429 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001430 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001431 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1432
Filipe Manana6bd335b2020-06-08 13:33:05 +01001433 if (is_space_ino || is_reloc_ino)
1434 bytes = range_bytes;
1435
Filipe Manana467dc472020-05-27 11:16:07 +01001436 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001437 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001438 spin_unlock(&sinfo->lock);
1439
Filipe Manana2166e5e2020-05-27 11:16:19 +01001440 if (count > 0)
1441 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1442 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001443 }
1444
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001445 return cow_file_range(inode, locked_page, start, end, page_started,
1446 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001447}
1448
Chris Masond352ac62008-09-29 15:18:18 -04001449/*
1450 * when nowcow writeback call back. This checks for snapshots or COW copies
1451 * of the extents that exist in the file, and COWs the file as required.
1452 *
1453 * If no cow copies or snapshots exist, we write directly to the existing
1454 * blocks on disk
1455 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001456static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001457 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001458 const u64 start, const u64 end,
1459 int *page_started, int force,
1460 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001461{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001462 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1463 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001464 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001465 u64 cow_start = (u64)-1;
1466 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001467 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001468 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001469 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1470 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001471 bool nocow = false;
1472 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001473
1474 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001475 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001476 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001477 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001478 EXTENT_DO_ACCOUNTING |
1479 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001480 PAGE_CLEAR_DIRTY |
1481 PAGE_SET_WRITEBACK |
1482 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001483 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001484 }
Li Zefan82d59022011-04-20 10:33:24 +08001485
Yan Zheng80ff3852008-10-30 14:20:02 -04001486 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001487 struct btrfs_key found_key;
1488 struct btrfs_file_extent_item *fi;
1489 struct extent_buffer *leaf;
1490 u64 extent_end;
1491 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001492 u64 num_bytes = 0;
1493 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001494 u64 ram_bytes;
1495 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001496
1497 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001498
Liu Boe4c3b2d2017-01-30 12:25:28 -08001499 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001500 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001501 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001502 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001503
1504 /*
1505 * If there is no extent for our range when doing the initial
1506 * search, then go back to the previous slot as it will be the
1507 * one containing the search offset
1508 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001509 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1510 leaf = path->nodes[0];
1511 btrfs_item_key_to_cpu(leaf, &found_key,
1512 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001513 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001514 found_key.type == BTRFS_EXTENT_DATA_KEY)
1515 path->slots[0]--;
1516 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001517 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001518next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001519 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 leaf = path->nodes[0];
1521 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1522 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001523 if (ret < 0) {
1524 if (cow_start != (u64)-1)
1525 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001526 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001527 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001528 if (ret > 0)
1529 break;
1530 leaf = path->nodes[0];
1531 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001532
Yan Zheng80ff3852008-10-30 14:20:02 -04001533 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001534
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001535 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001536 if (found_key.objectid > ino)
1537 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001538 /*
1539 * Keep searching until we find an EXTENT_ITEM or there are no
1540 * more extents for this inode
1541 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001542 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1543 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1544 path->slots[0]++;
1545 goto next_slot;
1546 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001547
1548 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001549 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001550 found_key.offset > end)
1551 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001552
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001553 /*
1554 * If the found extent starts after requested offset, then
1555 * adjust extent_end to be right before this extent begins
1556 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001557 if (found_key.offset > cur_offset) {
1558 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001559 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001560 goto out_check;
1561 }
Chris Masonc31f8832008-01-08 15:46:31 -05001562
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001563 /*
1564 * Found extent which begins before our range and potentially
1565 * intersect it
1566 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001567 fi = btrfs_item_ptr(leaf, path->slots[0],
1568 struct btrfs_file_extent_item);
1569 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001570
Josef Bacikcc95bef2013-04-04 14:31:27 -04001571 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001572 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1573 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001574 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001575 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001576 extent_end = found_key.offset +
1577 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001578 disk_num_bytes =
1579 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001580 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001581 * If the extent we got ends before our current offset,
1582 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001583 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001584 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001585 path->slots[0]++;
1586 goto next_slot;
1587 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001588 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001589 if (disk_bytenr == 0)
1590 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001591 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001592 if (btrfs_file_extent_compression(leaf, fi) ||
1593 btrfs_file_extent_encryption(leaf, fi) ||
1594 btrfs_file_extent_other_encoding(leaf, fi))
1595 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001596 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001597 * If extent is created before the last volume's snapshot
1598 * this implies the extent is shared, hence we can't do
1599 * nocow. This is the same check as in
1600 * btrfs_cross_ref_exist but without calling
1601 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001602 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001603 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001604 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001605 btrfs_root_last_snapshot(&root->root_item))
1606 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001607 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1608 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001609 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001610 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001611 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001612 ret = btrfs_cross_ref_exist(root, ino,
1613 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001614 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001615 if (ret) {
1616 /*
1617 * ret could be -EIO if the above fails to read
1618 * metadata.
1619 */
1620 if (ret < 0) {
1621 if (cow_start != (u64)-1)
1622 cur_offset = cow_start;
1623 goto error;
1624 }
1625
Nikolay Borisov3e024842019-08-21 10:42:03 +03001626 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001627 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001628 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001629 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001630 disk_bytenr += cur_offset - found_key.offset;
1631 num_bytes = min(end + 1, extent_end) - cur_offset;
1632 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001633 * If there are pending snapshots for this root, we
1634 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001635 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001636 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001637 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001638 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001639 * force cow if csum exists in the range.
1640 * this ensure that csum for a given extent are
1641 * either valid or do not exist.
1642 */
Liu Bo58113752018-01-31 17:09:13 -07001643 ret = csum_exist_in_range(fs_info, disk_bytenr,
1644 num_bytes);
1645 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001646 /*
1647 * ret could be -EIO if the above fails to read
1648 * metadata.
1649 */
1650 if (ret < 0) {
1651 if (cow_start != (u64)-1)
1652 cur_offset = cow_start;
1653 goto error;
1654 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001655 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001656 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001657 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001658 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001659 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001660 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001661 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001662 extent_end = found_key.offset + ram_bytes;
1663 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001664 /* Skip extents outside of our requested range */
1665 if (extent_end <= start) {
1666 path->slots[0]++;
1667 goto next_slot;
1668 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001669 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001670 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001671 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001672 }
1673out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001674 /*
1675 * If nocow is false then record the beginning of the range
1676 * that needs to be COWed
1677 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001678 if (!nocow) {
1679 if (cow_start == (u64)-1)
1680 cow_start = cur_offset;
1681 cur_offset = extent_end;
1682 if (cur_offset > end)
1683 break;
1684 path->slots[0]++;
1685 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001686 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001687
David Sterbab3b4aa72011-04-21 01:20:15 +02001688 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001689
1690 /*
1691 * COW range from cow_start to found_key.offset - 1. As the key
1692 * will contain the beginning of the first extent that can be
1693 * NOCOW, following one which needs to be COW'ed
1694 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001695 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001696 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001697 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001698 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001699 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001700 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001701 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001702 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001703
Yan Zhengd899e052008-10-30 14:25:28 -04001704 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001705 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001706 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001707
Nikolay Borisov968322c2020-06-03 08:55:21 +03001708 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001709 orig_start,
1710 disk_bytenr, /* block_start */
1711 num_bytes, /* block_len */
1712 disk_num_bytes, /* orig_block_len */
1713 ram_bytes, BTRFS_COMPRESS_NONE,
1714 BTRFS_ORDERED_PREALLOC);
1715 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001716 ret = PTR_ERR(em);
1717 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001718 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001719 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001720 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001721 disk_bytenr, num_bytes,
1722 num_bytes,
1723 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001724 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001725 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001726 cur_offset + num_bytes - 1,
1727 0);
1728 goto error;
1729 }
Yan Zhengd899e052008-10-30 14:25:28 -04001730 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001731 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001732 disk_bytenr, num_bytes,
1733 num_bytes,
1734 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001735 if (ret)
1736 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001737 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001738
Filipe Mananaf78c4362016-05-09 13:15:41 +01001739 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001740 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001741 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001742
Yan, Zhengefa56462010-05-16 10:49:59 -04001743 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001744 BTRFS_DATA_RELOC_TREE_OBJECTID)
1745 /*
1746 * Error handled later, as we must prevent
1747 * extent_clear_unlock_delalloc() in error handler
1748 * from freeing metadata of created ordered extent.
1749 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001750 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001751 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001752
Nikolay Borisov968322c2020-06-03 08:55:21 +03001753 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001754 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001755 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001756 EXTENT_DELALLOC |
1757 EXTENT_CLEAR_DATA_RESV,
1758 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1759
Yan Zheng80ff3852008-10-30 14:20:02 -04001760 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001761
1762 /*
1763 * btrfs_reloc_clone_csums() error, now we're OK to call error
1764 * handler, as metadata for created ordered extent will only
1765 * be freed by btrfs_finish_ordered_io().
1766 */
1767 if (ret)
1768 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001769 if (cur_offset > end)
1770 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001771 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001772 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001773
Robbie Ko506481b2018-10-30 18:04:04 +08001774 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001775 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001776
Yan Zheng80ff3852008-10-30 14:20:02 -04001777 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001778 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001779 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1780 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001781 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001782 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001783 }
1784
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001785error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001786 if (nocow)
1787 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1788
Josef Bacik17ca04a2012-05-31 15:58:55 -04001789 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001790 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001791 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001792 EXTENT_DELALLOC | EXTENT_DEFRAG |
1793 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1794 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001795 PAGE_SET_WRITEBACK |
1796 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001797 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001798 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001799}
1800
Nikolay Borisov0c494222020-06-03 08:55:28 +03001801static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001802{
1803
Nikolay Borisov0c494222020-06-03 08:55:28 +03001804 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1805 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001806 return 0;
1807
1808 /*
1809 * @defrag_bytes is a hint value, no spinlock held here,
1810 * if is not zero, it means the file is defragging.
1811 * Force cow if given extent needs to be defragged.
1812 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001813 if (inode->defrag_bytes &&
1814 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001815 return 1;
1816
1817 return 0;
1818}
1819
Chris Masond352ac62008-09-29 15:18:18 -04001820/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001821 * Function to process delayed allocation (create CoW) for ranges which are
1822 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001823 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001824int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001825 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1826 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001827{
Chris Masonbe20aa92007-12-17 20:14:01 -05001828 int ret;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001829 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001830
Nikolay Borisov98456b92020-06-03 08:55:29 +03001831 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1832 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001833 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001834 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1835 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001836 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001837 } else if (!inode_can_compress(inode) ||
1838 !inode_need_compress(inode, start, end)) {
1839 ret = cow_file_range(inode, locked_page, start, end,
1840 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001841 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001842 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1843 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001844 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001845 }
Qu Wenruo524272602017-03-08 10:25:52 +08001846 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001847 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001848 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001849 return ret;
1850}
1851
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001852void btrfs_split_delalloc_extent(struct inode *inode,
1853 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001854{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001855 u64 size;
1856
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001857 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001858 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001859 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001860
Josef Bacikdcab6a32015-02-11 15:08:59 -05001861 size = orig->end - orig->start + 1;
1862 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001863 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001864 u64 new_size;
1865
1866 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001867 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001868 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001869 */
1870 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001871 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001872 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001873 num_extents += count_max_extents(new_size);
1874 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001875 return;
1876 }
1877
Josef Bacik9e0baf62011-07-15 15:16:44 +00001878 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001879 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001880 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001881}
1882
1883/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001884 * Handle merged delayed allocation extents so we can keep track of new extents
1885 * that are just merged onto old extents, such as when we are doing sequential
1886 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001887 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001888void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1889 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001890{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001891 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001892 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001893
Josef Bacik9ed74f22009-09-11 16:12:44 -04001894 /* not delalloc, ignore it */
1895 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001896 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001897
Josef Bacik8461a3d2015-03-13 15:12:08 -04001898 if (new->start > other->start)
1899 new_size = new->end - other->start + 1;
1900 else
1901 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001902
1903 /* we're not bigger than the max, unreserve the space and go */
1904 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1905 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001906 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001907 spin_unlock(&BTRFS_I(inode)->lock);
1908 return;
1909 }
1910
1911 /*
Josef Bacikba117212015-03-13 15:01:24 -04001912 * We have to add up either side to figure out how many extents were
1913 * accounted for before we merged into one big extent. If the number of
1914 * extents we accounted for is <= the amount we need for the new range
1915 * then we can return, otherwise drop. Think of it like this
1916 *
1917 * [ 4k][MAX_SIZE]
1918 *
1919 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1920 * need 2 outstanding extents, on one side we have 1 and the other side
1921 * we have 1 so they are == and we can return. But in this case
1922 *
1923 * [MAX_SIZE+4k][MAX_SIZE+4k]
1924 *
1925 * Each range on their own accounts for 2 extents, but merged together
1926 * they are only 3 extents worth of accounting, so we need to drop in
1927 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001928 */
Josef Bacikba117212015-03-13 15:01:24 -04001929 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001930 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001931 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001932 num_extents += count_max_extents(old_size);
1933 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001934 return;
1935
Josef Bacik9e0baf62011-07-15 15:16:44 +00001936 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001937 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001938 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001939}
1940
Miao Xieeb73c1b2013-05-15 07:48:22 +00001941static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1942 struct inode *inode)
1943{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001944 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1945
Miao Xieeb73c1b2013-05-15 07:48:22 +00001946 spin_lock(&root->delalloc_lock);
1947 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1948 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1949 &root->delalloc_inodes);
1950 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1951 &BTRFS_I(inode)->runtime_flags);
1952 root->nr_delalloc_inodes++;
1953 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001954 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001955 BUG_ON(!list_empty(&root->delalloc_root));
1956 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001957 &fs_info->delalloc_roots);
1958 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001959 }
1960 }
1961 spin_unlock(&root->delalloc_lock);
1962}
1963
Nikolay Borisov2b877332018-04-27 12:21:51 +03001964
1965void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1966 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001967{
David Sterba3ffbd682018-06-29 10:56:42 +02001968 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001969
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001970 if (!list_empty(&inode->delalloc_inodes)) {
1971 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001972 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001973 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001974 root->nr_delalloc_inodes--;
1975 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001976 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001977 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001978 BUG_ON(list_empty(&root->delalloc_root));
1979 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001980 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001981 }
1982 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001983}
1984
1985static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1986 struct btrfs_inode *inode)
1987{
1988 spin_lock(&root->delalloc_lock);
1989 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001990 spin_unlock(&root->delalloc_lock);
1991}
1992
Chris Masond352ac62008-09-29 15:18:18 -04001993/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001994 * Properly track delayed allocation bytes in the inode and to maintain the
1995 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001996 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001997void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1998 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001999{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002000 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2001
Wang Shilong47059d92014-07-03 18:22:07 +08002002 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2003 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002004 /*
2005 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002006 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002007 * bit, which is only set or cleared with irqs on
2008 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002009 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002010 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002011 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002012 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002013 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002014
Josef Bacik8b62f872017-10-19 14:15:55 -04002015 spin_lock(&BTRFS_I(inode)->lock);
2016 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2017 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002018
Josef Bacik6a3891c2015-03-16 17:38:52 -04002019 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002020 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002021 return;
2022
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002023 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2024 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002025 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002026 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002027 if (*bits & EXTENT_DEFRAG)
2028 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002029 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002030 &BTRFS_I(inode)->runtime_flags))
2031 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002032 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002033 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002034
2035 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2036 (*bits & EXTENT_DELALLOC_NEW)) {
2037 spin_lock(&BTRFS_I(inode)->lock);
2038 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2039 state->start;
2040 spin_unlock(&BTRFS_I(inode)->lock);
2041 }
Chris Mason291d6732008-01-29 15:55:23 -05002042}
2043
Chris Masond352ac62008-09-29 15:18:18 -04002044/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002045 * Once a range is no longer delalloc this function ensures that proper
2046 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002047 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002048void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2049 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002050{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002051 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2052 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002053 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002054 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002055
Filipe Manana4a4b9642017-07-27 19:52:55 +01002056 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2057 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002058 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002059 spin_unlock(&inode->lock);
2060 }
Wang Shilong47059d92014-07-03 18:22:07 +08002061
Chris Mason75eff682008-12-15 15:54:40 -05002062 /*
2063 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002064 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002065 * bit, which is only set or cleared with irqs on
2066 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002067 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002068 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002069 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002070
Josef Bacik8b62f872017-10-19 14:15:55 -04002071 spin_lock(&inode->lock);
2072 btrfs_mod_outstanding_extents(inode, -num_extents);
2073 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002074
Josef Bacikb6d08f02013-09-27 14:57:43 -04002075 /*
2076 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002077 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002078 * error.
2079 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002080 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002081 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002082 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002083
Josef Bacik6a3891c2015-03-16 17:38:52 -04002084 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002085 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002086 return;
2087
Filipe Mananaa315e682017-03-06 23:04:20 +00002088 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2089 do_list && !(state->state & EXTENT_NORESERVE) &&
2090 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002091 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002092
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002093 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2094 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002095 spin_lock(&inode->lock);
2096 inode->delalloc_bytes -= len;
2097 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002098 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002099 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002100 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002101 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002102 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002103
2104 if ((state->state & EXTENT_DELALLOC_NEW) &&
2105 (*bits & EXTENT_DELALLOC_NEW)) {
2106 spin_lock(&inode->lock);
2107 ASSERT(inode->new_delalloc_bytes >= len);
2108 inode->new_delalloc_bytes -= len;
2109 spin_unlock(&inode->lock);
2110 }
Chris Mason291d6732008-01-29 15:55:23 -05002111}
2112
Chris Masond352ac62008-09-29 15:18:18 -04002113/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002114 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2115 * in a chunk's stripe. This function ensures that bios do not span a
2116 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002117 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002118 * @page - The page we are about to add to the bio
2119 * @size - size we want to add to the bio
2120 * @bio - bio we want to ensure is smaller than a stripe
2121 * @bio_flags - flags of the bio
2122 *
2123 * return 1 if page cannot be added to the bio
2124 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002125 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002126 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002127int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2128 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002129{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002130 struct inode *inode = page->mapping->host;
2131 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002132 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002133 u64 length = 0;
2134 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002135 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002136 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002137
Chris Mason771ed682008-11-06 22:02:51 -05002138 if (bio_flags & EXTENT_BIO_COMPRESSED)
2139 return 0;
2140
Kent Overstreet4f024f32013-10-11 15:44:27 -07002141 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002142 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002143 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2144 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002145 if (ret < 0)
2146 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002147
2148 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002149 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002150 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002151}
2152
Chris Masond352ac62008-09-29 15:18:18 -04002153/*
2154 * in order to insert checksums into the metadata in large chunks,
2155 * we wait until bio submission time. All the pages in the bio are
2156 * checksummed and sums are attached onto the ordered extent record.
2157 *
2158 * At IO completion time the cums attached on the ordered extent record
2159 * are inserted into the btree
2160 */
David Sterbad0ee3932018-03-08 14:35:48 +01002161static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002162 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002163{
Josef Bacikc6100a42017-05-05 11:57:13 -04002164 struct inode *inode = private_data;
Chris Masone0156402008-04-16 11:15:20 -04002165
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002166 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002167}
Chris Masone0156402008-04-16 11:15:20 -04002168
Chris Mason4a69a412008-11-06 22:03:00 -05002169/*
Chris Masoncad321a2008-12-17 14:51:42 -05002170 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002171 * on write, or reading the csums from the tree before a read.
2172 *
2173 * Rules about async/sync submit,
2174 * a) read: sync submit
2175 *
2176 * b) write without checksum: sync submit
2177 *
2178 * c) write with checksum:
2179 * c-1) if bio is issued by fsync: sync submit
2180 * (sync_writers != 0)
2181 *
2182 * c-2) if root is reloc root: sync submit
2183 * (only in case of buffered IO)
2184 *
2185 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002186 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002187static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002188 int mirror_num,
2189 unsigned long bio_flags)
2190
Chris Mason44b8bd72008-04-16 11:14:51 -04002191{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002192 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302194 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002195 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002196 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002197 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002198
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002199 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002200
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002201 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302202 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002203
Mike Christie37226b22016-06-05 14:31:52 -05002204 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002205 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002206 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002207 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002208
Chris Masond20f7042008-12-08 16:58:54 -05002209 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002210 ret = btrfs_submit_compressed_read(inode, bio,
2211 mirror_num,
2212 bio_flags);
2213 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002214 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002215 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002216 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002217 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002218 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002219 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002220 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002221 /* csum items have already been cloned */
2222 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2223 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002224 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002225 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002226 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002227 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002228 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002229 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002230 if (ret)
2231 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002232 }
2233
Chris Mason0b86a832008-03-24 15:01:56 -04002234mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002235 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002236
2237out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002238 if (ret) {
2239 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002240 bio_endio(bio);
2241 }
Stefan Behrens61891922012-11-05 18:51:52 +01002242 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002243}
Chris Mason6885f302008-02-20 16:11:05 -05002244
Chris Masond352ac62008-09-29 15:18:18 -04002245/*
2246 * given a list of ordered sums record them in the inode. This happens
2247 * at IO completion time based on sums calculated at bio submission time.
2248 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002249static int add_pending_csums(struct btrfs_trans_handle *trans,
2250 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002251{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002252 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002253 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002254
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002255 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002256 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002257 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002258 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002259 if (ret)
2260 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002261 }
2262 return 0;
2263}
2264
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002265int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002266 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002267 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002268{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002269 WARN_ON(PAGE_ALIGNED(end));
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002270 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2271 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002272}
2273
Chris Masond352ac62008-09-29 15:18:18 -04002274/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002275struct btrfs_writepage_fixup {
2276 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002277 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002278 struct btrfs_work work;
2279};
2280
Christoph Hellwigb2950862008-12-02 09:54:17 -05002281static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002282{
2283 struct btrfs_writepage_fixup *fixup;
2284 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002285 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002286 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002287 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002288 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002289 u64 page_start;
2290 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002291 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002292 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002293
2294 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2295 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002296 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002297 page_start = page_offset(page);
2298 page_end = page_offset(page) + PAGE_SIZE - 1;
2299
2300 /*
2301 * This is similar to page_mkwrite, we need to reserve the space before
2302 * we take the page lock.
2303 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002304 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2305 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002306again:
Chris Mason247e7432008-07-17 12:53:51 -04002307 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002308
Chris Mason25f3c502020-01-21 11:51:42 -05002309 /*
2310 * Before we queued this fixup, we took a reference on the page.
2311 * page->mapping may go NULL, but it shouldn't be moved to a different
2312 * address space.
2313 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002314 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2315 /*
2316 * Unfortunately this is a little tricky, either
2317 *
2318 * 1) We got here and our page had already been dealt with and
2319 * we reserved our space, thus ret == 0, so we need to just
2320 * drop our space reservation and bail. This can happen the
2321 * first time we come into the fixup worker, or could happen
2322 * while waiting for the ordered extent.
2323 * 2) Our page was already dealt with, but we happened to get an
2324 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2325 * this case we obviously don't have anything to release, but
2326 * because the page was already dealt with we don't want to
2327 * mark the page with an error, so make sure we're resetting
2328 * ret to 0. This is why we have this check _before_ the ret
2329 * check, because we do not want to have a surprise ENOSPC
2330 * when the page was already properly dealt with.
2331 */
2332 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002333 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2334 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002335 page_start, PAGE_SIZE,
2336 true);
2337 }
2338 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002339 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002340 }
Chris Mason25f3c502020-01-21 11:51:42 -05002341
2342 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002343 * We can't mess with the page state unless it is locked, so now that
2344 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002345 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002346 if (ret)
2347 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002348
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002349 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002350
2351 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002352 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002353 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002354
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002355 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002356 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002357 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2358 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002359 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002360 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002361 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002362 goto again;
2363 }
Chris Mason247e7432008-07-17 12:53:51 -04002364
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002365 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002366 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002367 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002368 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002369
Chris Mason25f3c502020-01-21 11:51:42 -05002370 /*
2371 * Everything went as planned, we're now the owner of a dirty page with
2372 * delayed allocation bits set and space reserved for our COW
2373 * destination.
2374 *
2375 * The page was dirty when we started, nothing should have cleaned it.
2376 */
2377 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002378 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002379out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002380 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002381 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002382 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2383 PAGE_SIZE, true);
2384 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002385 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002386out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002387 if (ret) {
2388 /*
2389 * We hit ENOSPC or other errors. Update the mapping and page
2390 * to reflect the errors and clean the page.
2391 */
2392 mapping_set_error(page->mapping, ret);
2393 end_extent_writepage(page, ret, page_start, page_end);
2394 clear_page_dirty_for_io(page);
2395 SetPageError(page);
2396 }
2397 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002398 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002399 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002400 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002401 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002402 /*
2403 * As a precaution, do a delayed iput in case it would be the last iput
2404 * that could need flushing space. Recursing back to fixup worker would
2405 * deadlock.
2406 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002407 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002408}
2409
2410/*
2411 * There are a few paths in the higher layers of the kernel that directly
2412 * set the page dirty bit without asking the filesystem if it is a
2413 * good idea. This causes problems because we want to make sure COW
2414 * properly happens and the data=ordered rules are followed.
2415 *
Chris Masonc8b97812008-10-29 14:49:59 -04002416 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002417 * hasn't been properly setup for IO. We kick off an async process
2418 * to fix it up. The async helper will wait for ordered extents, set
2419 * the delalloc bit and make it safe to write the page.
2420 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002421int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002422{
2423 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002424 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002425 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002426
Chris Mason8b62b722009-09-02 16:53:46 -04002427 /* this page is properly in the ordered list */
2428 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002429 return 0;
2430
Chris Mason25f3c502020-01-21 11:51:42 -05002431 /*
2432 * PageChecked is set below when we create a fixup worker for this page,
2433 * don't try to create another one if we're already PageChecked()
2434 *
2435 * The extent_io writepage code will redirty the page if we send back
2436 * EAGAIN.
2437 */
Chris Mason247e7432008-07-17 12:53:51 -04002438 if (PageChecked(page))
2439 return -EAGAIN;
2440
2441 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2442 if (!fixup)
2443 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002444
Josef Bacikf4b13632020-01-21 14:34:52 -05002445 /*
2446 * We are already holding a reference to this inode from
2447 * write_cache_pages. We need to hold it because the space reservation
2448 * takes place outside of the page lock, and we can't trust
2449 * page->mapping outside of the page lock.
2450 */
2451 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002452 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002453 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002454 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002455 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002456 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002457 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002458
2459 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002460}
2461
Yan Zhengd899e052008-10-30 14:25:28 -04002462static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002463 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002464 struct btrfs_file_extent_item *stack_fi,
2465 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002466{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002467 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002468 struct btrfs_path *path;
2469 struct extent_buffer *leaf;
2470 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002471 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2472 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2473 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2474 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002475 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002476 int ret;
2477
2478 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002479 if (!path)
2480 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002481
Chris Masona1ed8352009-09-11 12:27:37 -04002482 /*
2483 * we may be replacing one extent in the tree with another.
2484 * The new extent is pinned in the extent map, and we don't want
2485 * to drop it from the cache until it is completely in the btree.
2486 *
2487 * So, tell btrfs_drop_extents to leave this extent in the cache.
2488 * the caller is expected to unpin it and allow it to be merged
2489 * with the others.
2490 */
Nikolay Borisovc553f942020-06-03 08:55:19 +03002491 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002492 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002493 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002494 if (ret)
2495 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002496
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002497 if (!extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002498 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002499 ins.offset = file_pos;
2500 ins.type = BTRFS_EXTENT_DATA_KEY;
2501
2502 path->leave_spinning = 1;
2503 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002504 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002505 if (ret)
2506 goto out;
2507 }
Yan Zhengd899e052008-10-30 14:25:28 -04002508 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002509 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2510 write_extent_buffer(leaf, stack_fi,
2511 btrfs_item_ptr_offset(leaf, path->slots[0]),
2512 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002513
Yan Zhengd899e052008-10-30 14:25:28 -04002514 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002515 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002516
Nikolay Borisovc553f942020-06-03 08:55:19 +03002517 inode_add_bytes(&inode->vfs_inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002518
2519 ins.objectid = disk_bytenr;
2520 ins.offset = disk_num_bytes;
2521 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002522
Nikolay Borisovc553f942020-06-03 08:55:19 +03002523 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002524 if (ret)
2525 goto out;
2526
Nikolay Borisovc553f942020-06-03 08:55:19 +03002527 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002528 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002529out:
Yan Zhengd899e052008-10-30 14:25:28 -04002530 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002531
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002532 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002533}
2534
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002535static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002536 u64 start, u64 len)
2537{
David Sterba32da53862019-10-29 19:20:18 +01002538 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002539
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002540 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002541 ASSERT(cache);
2542
2543 spin_lock(&cache->lock);
2544 cache->delalloc_bytes -= len;
2545 spin_unlock(&cache->lock);
2546
2547 btrfs_put_block_group(cache);
2548}
2549
Qu Wenruo203f44c52020-06-10 09:04:40 +08002550static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002551 struct btrfs_ordered_extent *oe)
2552{
2553 struct btrfs_file_extent_item stack_fi;
2554 u64 logical_len;
2555
2556 memset(&stack_fi, 0, sizeof(stack_fi));
2557 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2558 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2559 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2560 oe->disk_num_bytes);
2561 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2562 logical_len = oe->truncated_len;
2563 else
2564 logical_len = oe->num_bytes;
2565 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2566 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2567 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2568 /* Encryption and other encoding is reserved and all 0 */
2569
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002570 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2571 oe->file_offset, &stack_fi,
2572 oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002573}
2574
2575/*
2576 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002577 * an ordered extent if the range of bytes in the file it covers are
2578 * fully written.
2579 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002580static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002581{
Josef Bacik5fd02042012-05-02 14:00:54 -04002582 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002583 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002584 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002585 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002586 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002587 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002588 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002589 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002590 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002591 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002592 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002593 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002594 bool range_locked = false;
2595 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002596 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002597 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002598
Omar Sandovalbffe6332019-12-02 17:34:19 -08002599 start = ordered_extent->file_offset;
2600 end = start + ordered_extent->num_bytes - 1;
2601
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002602 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2603 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2604 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2605 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002606
Nikolay Borisov8d510122019-10-08 20:43:06 +03002607 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002608
Josef Bacik5fd02042012-05-02 14:00:54 -04002609 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2610 ret = -EIO;
2611 goto out;
2612 }
2613
Omar Sandovalbffe6332019-12-02 17:34:19 -08002614 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002615
Josef Bacik77cef2e2013-08-29 13:57:21 -04002616 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2617 truncated = true;
2618 logical_len = ordered_extent->truncated_len;
2619 /* Truncated the entire extent, don't bother adding */
2620 if (!logical_len)
2621 goto out;
2622 }
2623
Yan, Zhengc2167752009-11-12 09:34:21 +00002624 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002625 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002626
Josef Bacikd923afe2020-01-17 09:02:23 -05002627 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002628 if (freespace_inode)
2629 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002630 else
2631 trans = btrfs_join_transaction(root);
2632 if (IS_ERR(trans)) {
2633 ret = PTR_ERR(trans);
2634 trans = NULL;
2635 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002636 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002637 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002638 ret = btrfs_update_inode_fallback(trans, root, inode);
2639 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002640 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002641 goto out;
2642 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002643
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002644 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002645 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002646
Nikolay Borisov8d510122019-10-08 20:43:06 +03002647 if (freespace_inode)
2648 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002649 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002650 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002651 if (IS_ERR(trans)) {
2652 ret = PTR_ERR(trans);
2653 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002654 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002655 }
Chris Masona79b7d42014-05-22 16:18:52 -07002656
Josef Bacik69fe2d72017-10-19 14:15:57 -04002657 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002658
Chris Masonc8b97812008-10-29 14:49:59 -04002659 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002660 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002661 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002662 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002663 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002664 ordered_extent->file_offset,
2665 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002666 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002667 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002668 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002669 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002670 if (!ret) {
2671 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002672 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002673 ordered_extent->disk_bytenr,
2674 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002675 }
Yan Zhengd899e052008-10-30 14:25:28 -04002676 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002677 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002678 ordered_extent->file_offset,
2679 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002680 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002681 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002682 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002683 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002684
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002685 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002686 if (ret) {
2687 btrfs_abort_transaction(trans, ret);
2688 goto out;
2689 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002690
Josef Bacikd923afe2020-01-17 09:02:23 -05002691 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002692 ret = btrfs_update_inode_fallback(trans, root, inode);
2693 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002694 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002695 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002696 }
2697 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002698out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002699 clear_bits = EXTENT_DEFRAG;
2700 if (range_locked)
2701 clear_bits |= EXTENT_LOCKED;
2702 if (clear_new_delalloc_bytes)
2703 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002704 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2705 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002706 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002707
Miao Xiea698d0752012-09-20 01:51:59 -06002708 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002709 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002710
Josef Bacik77cef2e2013-08-29 13:57:21 -04002711 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002712 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002713
2714 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002715 unwritten_start += logical_len;
2716 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002717
2718 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002719 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002720
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002721 /*
2722 * If the ordered extent had an IOERR or something else went
2723 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002724 * back to the allocator. We only free the extent in the
2725 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002726 *
2727 * If we made it past insert_reserved_file_extent before we
2728 * errored out then we don't need to do this as the accounting
2729 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002730 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002731 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002732 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002733 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002734 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2735 /*
2736 * Discard the range before returning it back to the
2737 * free space pool
2738 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002739 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002740 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002741 ordered_extent->disk_bytenr,
2742 ordered_extent->disk_num_bytes,
2743 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002744 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002745 ordered_extent->disk_bytenr,
2746 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002747 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002748 }
2749
Josef Bacik5fd02042012-05-02 14:00:54 -04002750 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002751 * This needs to be done to make sure anybody waiting knows we are done
2752 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002753 */
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03002754 btrfs_remove_ordered_extent(BTRFS_I(inode), ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04002755
Chris Masone6dcd2d2008-07-17 12:53:50 -04002756 /* once for us */
2757 btrfs_put_ordered_extent(ordered_extent);
2758 /* once for the tree */
2759 btrfs_put_ordered_extent(ordered_extent);
2760
Josef Bacik5fd02042012-05-02 14:00:54 -04002761 return ret;
2762}
2763
2764static void finish_ordered_fn(struct btrfs_work *work)
2765{
2766 struct btrfs_ordered_extent *ordered_extent;
2767 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2768 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002769}
2770
Nikolay Borisovc6297322018-11-08 10:18:08 +02002771void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2772 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002773{
Nikolay Borisov3347c482020-08-31 14:42:44 +03002774 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2775 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04002776 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002777 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002778
liubo1abe9b82011-03-24 11:18:59 +00002779 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2780
Chris Mason8b62b722009-09-02 16:53:46 -04002781 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03002782 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2783 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002784 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002785
Nikolay Borisov3347c482020-08-31 14:42:44 +03002786 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002787 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002788 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002789 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002790
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002791 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002792 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002793}
2794
Omar Sandoval47df7762020-04-16 14:46:17 -07002795static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2796 int icsum, struct page *page, int pgoff, u64 start,
2797 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002798{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2800 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002801 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002802 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2803 u8 *csum_expected;
2804 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002805
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002806 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002807
2808 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002809 shash->tfm = fs_info->csum_shash;
2810
Eric Biggersfd080012020-04-30 23:51:59 -07002811 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002812
2813 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002814 goto zeroit;
2815
2816 kunmap_atomic(kaddr);
2817 return 0;
2818zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002819 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2820 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03002821 if (io_bio->device)
2822 btrfs_dev_stat_inc_and_print(io_bio->device,
2823 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08002824 memset(kaddr + pgoff, 1, len);
2825 flush_dcache_page(page);
2826 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002827 return -EIO;
2828}
2829
Chris Masond352ac62008-09-29 15:18:18 -04002830/*
Chris Masond352ac62008-09-29 15:18:18 -04002831 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002832 * if there's a match, we allow the bio to finish. If not, the code in
2833 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002834 */
Nikolay Borisov9a446d62020-09-18 16:34:33 +03002835int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset,
2836 struct page *page, u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002837{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002838 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002839 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002840 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002841 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002842
Chris Masond20f7042008-12-08 16:58:54 -05002843 if (PageChecked(page)) {
2844 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002845 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002846 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002847
2848 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002849 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002850
Yan Zheng17d217f2008-12-12 10:03:38 -05002851 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002852 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002853 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002854 return 0;
2855 }
2856
Miao Xiefacc8a222013-07-25 19:22:34 +08002857 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002858 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2859 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002860}
Chris Masonb888db22007-08-27 16:49:44 -04002861
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002862/*
2863 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2864 *
2865 * @inode: The inode we want to perform iput on
2866 *
2867 * This function uses the generic vfs_inode::i_count to track whether we should
2868 * just decrement it (in case it's > 1) or if this is the last iput then link
2869 * the inode to the delayed iput machinery. Delayed iputs are processed at
2870 * transaction commit time/superblock commit/cleaner kthread.
2871 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002872void btrfs_add_delayed_iput(struct inode *inode)
2873{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002874 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002875 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002876
2877 if (atomic_add_unless(&inode->i_count, -1, 1))
2878 return;
2879
Josef Bacik034f7842018-12-03 11:06:52 -05002880 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002881 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002882 ASSERT(list_empty(&binode->delayed_iput));
2883 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002884 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002885 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2886 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002887}
2888
Josef Bacik63611e72019-06-18 10:59:18 -04002889static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2890 struct btrfs_inode *inode)
2891{
2892 list_del_init(&inode->delayed_iput);
2893 spin_unlock(&fs_info->delayed_iput_lock);
2894 iput(&inode->vfs_inode);
2895 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2896 wake_up(&fs_info->delayed_iputs_wait);
2897 spin_lock(&fs_info->delayed_iput_lock);
2898}
2899
2900static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2901 struct btrfs_inode *inode)
2902{
2903 if (!list_empty(&inode->delayed_iput)) {
2904 spin_lock(&fs_info->delayed_iput_lock);
2905 if (!list_empty(&inode->delayed_iput))
2906 run_delayed_iput_locked(fs_info, inode);
2907 spin_unlock(&fs_info->delayed_iput_lock);
2908 }
2909}
2910
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002911void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002912{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002913
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002914 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002915 while (!list_empty(&fs_info->delayed_iputs)) {
2916 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002917
David Sterba8089fe62015-11-19 14:15:51 +01002918 inode = list_first_entry(&fs_info->delayed_iputs,
2919 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002920 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002921 }
David Sterba8089fe62015-11-19 14:15:51 +01002922 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002923}
2924
Josef Bacik034f7842018-12-03 11:06:52 -05002925/**
2926 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2927 * @fs_info - the fs_info for this fs
2928 * @return - EINTR if we were killed, 0 if nothing's pending
2929 *
2930 * This will wait on any delayed iputs that are currently running with KILLABLE
2931 * set. Once they are all done running we will return, unless we are killed in
2932 * which case we return EINTR. This helps in user operations like fallocate etc
2933 * that might get blocked on the iputs.
2934 */
2935int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2936{
2937 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2938 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2939 if (ret)
2940 return -EINTR;
2941 return 0;
2942}
2943
Yan, Zhengd68fc572010-05-16 10:49:58 -04002944/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002945 * This creates an orphan entry for the given inode in case something goes wrong
2946 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002947 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002948int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002949 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002950{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002951 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002952
Omar Sandoval27919062018-05-11 13:13:37 -07002953 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2954 if (ret && ret != -EEXIST) {
2955 btrfs_abort_transaction(trans, ret);
2956 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002957 }
2958
Yan, Zhengd68fc572010-05-16 10:49:58 -04002959 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002960}
2961
2962/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002963 * We have done the delete so we can go ahead and remove the orphan item for
2964 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002965 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002966static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002967 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002968{
Omar Sandoval27919062018-05-11 13:13:37 -07002969 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002970}
2971
2972/*
2973 * this cleans up any orphans that may be left on the list from the last use
2974 * of this root.
2975 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002976int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002977{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002978 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002979 struct btrfs_path *path;
2980 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002981 struct btrfs_key key, found_key;
2982 struct btrfs_trans_handle *trans;
2983 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002984 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002985 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002986
Yan, Zhengd68fc572010-05-16 10:49:58 -04002987 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002988 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002989
2990 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002991 if (!path) {
2992 ret = -ENOMEM;
2993 goto out;
2994 }
David Sterbae4058b52015-11-27 16:31:35 +01002995 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002996
2997 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002998 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04002999 key.offset = (u64)-1;
3000
Josef Bacik7b128762008-07-24 12:17:14 -04003001 while (1) {
3002 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003003 if (ret < 0)
3004 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003005
3006 /*
3007 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003008 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003009 * find the key and see if we have stuff that matches
3010 */
3011 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003012 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003013 if (path->slots[0] == 0)
3014 break;
3015 path->slots[0]--;
3016 }
3017
3018 /* pull out the item */
3019 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003020 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3021
3022 /* make sure the item matches what we want */
3023 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3024 break;
David Sterba962a2982014-06-04 18:41:45 +02003025 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003026 break;
3027
3028 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003029 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003030
3031 /*
3032 * this is where we are basically btrfs_lookup, without the
3033 * crossing root thing. we store the inode number in the
3034 * offset of the orphan item.
3035 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003036
3037 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003038 btrfs_err(fs_info,
3039 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003040 ret = -EINVAL;
3041 goto out;
3042 }
3043
3044 last_objectid = found_key.offset;
3045
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003046 found_key.objectid = found_key.offset;
3047 found_key.type = BTRFS_INODE_ITEM_KEY;
3048 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003049 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303050 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003051 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003052 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003053
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003054 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003055 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003056 int is_dead_root = 0;
3057
3058 /*
3059 * this is an orphan in the tree root. Currently these
3060 * could come from 2 sources:
3061 * a) a snapshot deletion in progress
3062 * b) a free space cache inode
3063 * We need to distinguish those two, as the snapshot
3064 * orphan must not get deleted.
3065 * find_dead_roots already ran before us, so if this
3066 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003067 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003068 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003069
3070 spin_lock(&fs_info->fs_roots_radix_lock);
3071 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3072 (unsigned long)found_key.objectid);
3073 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3074 is_dead_root = 1;
3075 spin_unlock(&fs_info->fs_roots_radix_lock);
3076
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003077 if (is_dead_root) {
3078 /* prevent this orphan from being found again */
3079 key.offset = found_key.objectid - 1;
3080 continue;
3081 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003082
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003083 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003084
Josef Bacika8c9e572011-09-21 16:55:59 -04003085 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003086 * If we have an inode with links, there are a couple of
3087 * possibilities. Old kernels (before v3.12) used to create an
3088 * orphan item for truncate indicating that there were possibly
3089 * extent items past i_size that needed to be deleted. In v3.12,
3090 * truncate was changed to update i_size in sync with the extent
3091 * items, but the (useless) orphan item was still created. Since
3092 * v4.18, we don't create the orphan item for truncate at all.
3093 *
3094 * So, this item could mean that we need to do a truncate, but
3095 * only if this filesystem was last used on a pre-v3.12 kernel
3096 * and was not cleanly unmounted. The odds of that are quite
3097 * slim, and it's a pain to do the truncate now, so just delete
3098 * the orphan item.
3099 *
3100 * It's also possible that this orphan item was supposed to be
3101 * deleted but wasn't. The inode number may have been reused,
3102 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003103 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003104 if (ret == -ENOENT || inode->i_nlink) {
3105 if (!ret)
3106 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003107 trans = btrfs_start_transaction(root, 1);
3108 if (IS_ERR(trans)) {
3109 ret = PTR_ERR(trans);
3110 goto out;
3111 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003112 btrfs_debug(fs_info, "auto deleting %Lu",
3113 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003114 ret = btrfs_del_orphan_item(trans, root,
3115 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003116 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003117 if (ret)
3118 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003119 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003120 }
Josef Bacik7b128762008-07-24 12:17:14 -04003121
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003122 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003123
3124 /* this will do delete_inode and everything for us */
3125 iput(inode);
3126 }
Miao Xie3254c872011-11-10 20:45:05 -05003127 /* release the path since we're done with it */
3128 btrfs_release_path(path);
3129
Yan, Zhengd68fc572010-05-16 10:49:58 -04003130 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3131
Omar Sandovala575cee2018-05-11 13:13:38 -07003132 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003133 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003134 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003135 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003136 }
Josef Bacik7b128762008-07-24 12:17:14 -04003137
3138 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003139 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003140
3141out:
3142 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003143 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003144 btrfs_free_path(path);
3145 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003146}
3147
Chris Masond352ac62008-09-29 15:18:18 -04003148/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003149 * very simple check to peek ahead in the leaf looking for xattrs. If we
3150 * don't find any xattrs, we know there can't be any acls.
3151 *
3152 * slot is the slot the inode is in, objectid is the objectid of the inode
3153 */
3154static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003155 int slot, u64 objectid,
3156 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003157{
3158 u32 nritems = btrfs_header_nritems(leaf);
3159 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003160 static u64 xattr_access = 0;
3161 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003162 int scanned = 0;
3163
Josef Bacikf23b5a52013-06-19 10:16:26 -04003164 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003165 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3166 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3167 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3168 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003169 }
3170
Chris Mason46a53cc2009-04-27 11:47:50 -04003171 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003172 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003173 while (slot < nritems) {
3174 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3175
3176 /* we found a different objectid, there must not be acls */
3177 if (found_key.objectid != objectid)
3178 return 0;
3179
3180 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003181 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003182 if (*first_xattr_slot == -1)
3183 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003184 if (found_key.offset == xattr_access ||
3185 found_key.offset == xattr_default)
3186 return 1;
3187 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003188
3189 /*
3190 * we found a key greater than an xattr key, there can't
3191 * be any acls later on
3192 */
3193 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3194 return 0;
3195
3196 slot++;
3197 scanned++;
3198
3199 /*
3200 * it goes inode, inode backrefs, xattrs, extents,
3201 * so if there are a ton of hard links to an inode there can
3202 * be a lot of backrefs. Don't waste time searching too hard,
3203 * this is just an optimization
3204 */
3205 if (scanned >= 8)
3206 break;
3207 }
3208 /* we hit the end of the leaf before we found an xattr or
3209 * something larger than an xattr. We have to assume the inode
3210 * has acls
3211 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003212 if (*first_xattr_slot == -1)
3213 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003214 return 1;
3215}
3216
3217/*
Chris Masond352ac62008-09-29 15:18:18 -04003218 * read an inode from the btree into the in-memory inode
3219 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003220static int btrfs_read_locked_inode(struct inode *inode,
3221 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003222{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003223 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003224 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003225 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003226 struct btrfs_inode_item *inode_item;
3227 struct btrfs_root *root = BTRFS_I(inode)->root;
3228 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003229 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003230 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003231 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003232 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003233 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003234 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003235
3236 ret = btrfs_fill_inode(inode, &rdev);
3237 if (!ret)
3238 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003239
Filipe Manana4222ea72018-10-24 10:13:03 +01003240 if (!path) {
3241 path = btrfs_alloc_path();
3242 if (!path)
3243 return -ENOMEM;
3244 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003245
Chris Mason39279cc2007-06-12 06:35:45 -04003246 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003247
Chris Mason39279cc2007-06-12 06:35:45 -04003248 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003249 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003250 if (path != in_path)
3251 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003252 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003253 }
Chris Mason39279cc2007-06-12 06:35:45 -04003254
Chris Mason5f39d392007-10-15 16:14:19 -04003255 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003256
3257 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003258 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003259
Chris Mason5f39d392007-10-15 16:14:19 -04003260 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3261 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003262 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003263 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003264 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3265 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003266 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003267 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3268 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003269
David Sterbaa937b972014-12-12 17:39:12 +01003270 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3271 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003272
David Sterbaa937b972014-12-12 17:39:12 +01003273 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3274 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003275
David Sterbaa937b972014-12-12 17:39:12 +01003276 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3277 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003278
chandan r9cc97d62012-07-04 12:48:07 +05303279 BTRFS_I(inode)->i_otime.tv_sec =
3280 btrfs_timespec_sec(leaf, &inode_item->otime);
3281 BTRFS_I(inode)->i_otime.tv_nsec =
3282 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003283
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003284 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003285 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003286 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3287
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003288 inode_set_iversion_queried(inode,
3289 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003290 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003291 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003292 rdev = btrfs_inode_rdev(leaf, inode_item);
3293
Josef Bacikaec74772008-07-24 12:12:38 -04003294 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003295 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003296
3297cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003298 /*
3299 * If we were modified in the current generation and evicted from memory
3300 * and then re-read we need to do a full sync since we don't have any
3301 * idea about which extents were modified before we were evicted from
3302 * cache.
3303 *
3304 * This is required for both inode re-read from disk and delayed inode
3305 * in delayed_nodes_tree.
3306 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003307 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003308 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3309 &BTRFS_I(inode)->runtime_flags);
3310
Filipe Mananabde6c242015-07-24 00:00:19 +01003311 /*
3312 * We don't persist the id of the transaction where an unlink operation
3313 * against the inode was last made. So here we assume the inode might
3314 * have been evicted, and therefore the exact value of last_unlink_trans
3315 * lost, and set it to last_trans to avoid metadata inconsistencies
3316 * between the inode and its parent if the inode is fsync'ed and the log
3317 * replayed. For example, in the scenario:
3318 *
3319 * touch mydir/foo
3320 * ln mydir/foo mydir/bar
3321 * sync
3322 * unlink mydir/bar
3323 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3324 * xfs_io -c fsync mydir/foo
3325 * <power failure>
3326 * mount fs, triggers fsync log replay
3327 *
3328 * We must make sure that when we fsync our inode foo we also log its
3329 * parent inode, otherwise after log replay the parent still has the
3330 * dentry with the "bar" name but our inode foo has a link count of 1
3331 * and doesn't have an inode ref with the name "bar" anymore.
3332 *
3333 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003334 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003335 * transaction commits on fsync if our inode is a directory, or if our
3336 * inode is not a directory, logging its parent unnecessarily.
3337 */
3338 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3339
Filipe Manana3ebac172020-07-15 12:30:43 +01003340 /*
3341 * Same logic as for last_unlink_trans. We don't persist the generation
3342 * of the last transaction where this inode was used for a reflink
3343 * operation, so after eviction and reloading the inode we must be
3344 * pessimistic and assume the last transaction that modified the inode.
3345 */
3346 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3347
Miao Xie67de1172013-12-26 13:07:06 +08003348 path->slots[0]++;
3349 if (inode->i_nlink != 1 ||
3350 path->slots[0] >= btrfs_header_nritems(leaf))
3351 goto cache_acl;
3352
3353 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003354 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003355 goto cache_acl;
3356
3357 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3358 if (location.type == BTRFS_INODE_REF_KEY) {
3359 struct btrfs_inode_ref *ref;
3360
3361 ref = (struct btrfs_inode_ref *)ptr;
3362 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3363 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3364 struct btrfs_inode_extref *extref;
3365
3366 extref = (struct btrfs_inode_extref *)ptr;
3367 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3368 extref);
3369 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003370cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003371 /*
3372 * try to precache a NULL acl entry for files that don't have
3373 * any xattrs or acls
3374 */
Li Zefan33345d012011-04-20 10:31:50 +08003375 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003376 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003377 if (first_xattr_slot != -1) {
3378 path->slots[0] = first_xattr_slot;
3379 ret = btrfs_load_inode_props(inode, path);
3380 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003381 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003382 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003383 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003384 root->root_key.objectid, ret);
3385 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003386 if (path != in_path)
3387 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003388
Al Viro72c04902009-06-24 16:58:48 -04003389 if (!maybe_acls)
3390 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003391
Chris Mason39279cc2007-06-12 06:35:45 -04003392 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003393 case S_IFREG:
3394 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003395 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003396 inode->i_fop = &btrfs_file_operations;
3397 inode->i_op = &btrfs_file_inode_operations;
3398 break;
3399 case S_IFDIR:
3400 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003401 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003402 break;
3403 case S_IFLNK:
3404 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003405 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003406 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003407 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003408 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003409 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003410 init_special_inode(inode, inode->i_mode, rdev);
3411 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003412 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003413
David Sterba7b6a2212018-03-26 18:40:21 +02003414 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003415 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003416}
3417
Chris Masond352ac62008-09-29 15:18:18 -04003418/*
3419 * given a leaf and an inode, copy the inode fields into the leaf
3420 */
Chris Masone02119d2008-09-05 16:13:11 -04003421static void fill_inode_item(struct btrfs_trans_handle *trans,
3422 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003423 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003424 struct inode *inode)
3425{
Liu Bo51fab692012-12-27 09:01:21 +00003426 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003427
David Sterbac82f8232019-08-09 17:48:21 +02003428 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003429
David Sterbacc4c13d2020-04-29 02:15:56 +02003430 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3431 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3432 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3433 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3434 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003435
David Sterbacc4c13d2020-04-29 02:15:56 +02003436 btrfs_set_token_timespec_sec(&token, &item->atime,
3437 inode->i_atime.tv_sec);
3438 btrfs_set_token_timespec_nsec(&token, &item->atime,
3439 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003440
David Sterbacc4c13d2020-04-29 02:15:56 +02003441 btrfs_set_token_timespec_sec(&token, &item->mtime,
3442 inode->i_mtime.tv_sec);
3443 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3444 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003445
David Sterbacc4c13d2020-04-29 02:15:56 +02003446 btrfs_set_token_timespec_sec(&token, &item->ctime,
3447 inode->i_ctime.tv_sec);
3448 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3449 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003450
David Sterbacc4c13d2020-04-29 02:15:56 +02003451 btrfs_set_token_timespec_sec(&token, &item->otime,
3452 BTRFS_I(inode)->i_otime.tv_sec);
3453 btrfs_set_token_timespec_nsec(&token, &item->otime,
3454 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303455
David Sterbacc4c13d2020-04-29 02:15:56 +02003456 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3457 btrfs_set_token_inode_generation(&token, item,
3458 BTRFS_I(inode)->generation);
3459 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3460 btrfs_set_token_inode_transid(&token, item, trans->transid);
3461 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3462 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3463 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003464}
3465
Chris Masond352ac62008-09-29 15:18:18 -04003466/*
3467 * copy everything in the in-memory inode into the btree.
3468 */
Chris Mason21151332011-11-10 20:39:08 -05003469static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003470 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003471{
3472 struct btrfs_inode_item *inode_item;
3473 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003474 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003475 int ret;
3476
3477 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003478 if (!path)
3479 return -ENOMEM;
3480
Chris Masonb9473432009-03-13 11:00:37 -04003481 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003482 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3483 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003484 if (ret) {
3485 if (ret > 0)
3486 ret = -ENOENT;
3487 goto failed;
3488 }
3489
Chris Mason5f39d392007-10-15 16:14:19 -04003490 leaf = path->nodes[0];
3491 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003492 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003493
Chris Masone02119d2008-09-05 16:13:11 -04003494 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003495 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovd9094412020-06-05 10:41:13 +03003496 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04003497 ret = 0;
3498failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003499 btrfs_free_path(path);
3500 return ret;
3501}
3502
Chris Masond352ac62008-09-29 15:18:18 -04003503/*
Chris Mason21151332011-11-10 20:39:08 -05003504 * copy everything in the in-memory inode into the btree.
3505 */
3506noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3507 struct btrfs_root *root, struct inode *inode)
3508{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003509 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003510 int ret;
3511
3512 /*
3513 * If the inode is a free space inode, we can deadlock during commit
3514 * if we put it into the delayed code.
3515 *
3516 * The data relocation inode should also be directly updated
3517 * without delay
3518 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003519 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003520 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003521 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003522 btrfs_update_root_times(trans, root);
3523
Chris Mason21151332011-11-10 20:39:08 -05003524 ret = btrfs_delayed_update_inode(trans, root, inode);
3525 if (!ret)
Nikolay Borisovd9094412020-06-05 10:41:13 +03003526 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason21151332011-11-10 20:39:08 -05003527 return ret;
3528 }
3529
3530 return btrfs_update_inode_item(trans, root, inode);
3531}
3532
Josef Bacikbe6aef62012-10-22 15:43:12 -04003533noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3534 struct btrfs_root *root,
3535 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003536{
3537 int ret;
3538
3539 ret = btrfs_update_inode(trans, root, inode);
3540 if (ret == -ENOSPC)
3541 return btrfs_update_inode_item(trans, root, inode);
3542 return ret;
3543}
3544
3545/*
Chris Masond352ac62008-09-29 15:18:18 -04003546 * unlink helper that gets used here in inode.c and in the tree logging
3547 * recovery code. It remove a link in a directory with a given name, and
3548 * also drops the back refs in the inode to the directory
3549 */
Al Viro92986792011-03-04 17:14:37 +00003550static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3551 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003552 struct btrfs_inode *dir,
3553 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003554 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003555{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003556 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003557 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003558 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003559 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003560 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003561 u64 ino = btrfs_ino(inode);
3562 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003563
3564 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003565 if (!path) {
3566 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003567 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003568 }
3569
Chris Masonb9473432009-03-13 11:00:37 -04003570 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003571 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003572 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003573 if (IS_ERR_OR_NULL(di)) {
3574 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003575 goto err;
3576 }
Chris Mason39279cc2007-06-12 06:35:45 -04003577 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003578 if (ret)
3579 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003580 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003581
Miao Xie67de1172013-12-26 13:07:06 +08003582 /*
3583 * If we don't have dir index, we have to get it by looking up
3584 * the inode ref, since we get the inode ref, remove it directly,
3585 * it is unnecessary to do delayed deletion.
3586 *
3587 * But if we have dir index, needn't search inode ref to get it.
3588 * Since the inode ref is close to the inode item, it is better
3589 * that we delay to delete it, and just do this deletion when
3590 * we update the inode item.
3591 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003592 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003593 ret = btrfs_delayed_delete_inode_ref(inode);
3594 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003595 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003596 goto skip_backref;
3597 }
3598 }
3599
Li Zefan33345d012011-04-20 10:31:50 +08003600 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3601 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003602 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003603 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003604 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003605 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003606 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003607 goto err;
3608 }
Miao Xie67de1172013-12-26 13:07:06 +08003609skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003610 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003611 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003612 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003613 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003614 }
Chris Mason39279cc2007-06-12 06:35:45 -04003615
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003616 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3617 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003618 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003619 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003620 goto err;
3621 }
Chris Masone02119d2008-09-05 16:13:11 -04003622
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003623 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3624 index);
Chris Mason6418c962010-10-30 07:34:24 -04003625 if (ret == -ENOENT)
3626 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003627 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003628 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003629
3630 /*
3631 * If we have a pending delayed iput we could end up with the final iput
3632 * being run in btrfs-cleaner context. If we have enough of these built
3633 * up we can end up burning a lot of time in btrfs-cleaner without any
3634 * way to throttle the unlinks. Since we're currently holding a ref on
3635 * the inode we can run the delayed iput here without any issues as the
3636 * final iput won't be done until after we drop the ref we're currently
3637 * holding.
3638 */
3639 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003640err:
3641 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003642 if (ret)
3643 goto out;
3644
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003645 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003646 inode_inc_iversion(&inode->vfs_inode);
3647 inode_inc_iversion(&dir->vfs_inode);
3648 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3649 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3650 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003651out:
Chris Mason39279cc2007-06-12 06:35:45 -04003652 return ret;
3653}
3654
Al Viro92986792011-03-04 17:14:37 +00003655int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3656 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003657 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003658 const char *name, int name_len)
3659{
3660 int ret;
3661 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3662 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003663 drop_nlink(&inode->vfs_inode);
3664 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003665 }
3666 return ret;
3667}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003668
3669/*
3670 * helper to start transaction for unlink and rmdir.
3671 *
Josef Bacikd52be812013-05-29 14:54:47 -04003672 * unlink and rmdir are special in btrfs, they do not always free space, so
3673 * if we cannot make our reservations the normal way try and see if there is
3674 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3675 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003676 */
Josef Bacikd52be812013-05-29 14:54:47 -04003677static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003679 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003680
Josef Bacike70bea52011-10-11 14:18:24 -04003681 /*
3682 * 1 for the possible orphan item
3683 * 1 for the dir item
3684 * 1 for the dir index
3685 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003686 * 1 for the inode
3687 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003688 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003689}
3690
Chris Mason39279cc2007-06-12 06:35:45 -04003691static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3692{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003693 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003694 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003695 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003696 int ret;
3697
Josef Bacikd52be812013-05-29 14:54:47 -04003698 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003699 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003700 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003701
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003702 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3703 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003704
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003705 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3706 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3707 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003708 if (ret)
3709 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003710
Yan, Zhenga22285a2010-05-16 10:48:46 -04003711 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003712 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003713 if (ret)
3714 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003715 }
Josef Bacik7b128762008-07-24 12:17:14 -04003716
Tsutomu Itohb5324022011-07-19 07:27:20 +00003717out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003718 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003719 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003720 return ret;
3721}
3722
Misono Tomohirof60a2362018-04-18 11:34:52 +09003723static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003724 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003725{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003726 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003727 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003728 struct btrfs_path *path;
3729 struct extent_buffer *leaf;
3730 struct btrfs_dir_item *di;
3731 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003732 const char *name = dentry->d_name.name;
3733 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003734 u64 index;
3735 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003736 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003737 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003738
Josef Bacik045d3962019-12-18 17:20:27 -05003739 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3740 objectid = inode->root->root_key.objectid;
3741 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3742 objectid = inode->location.objectid;
3743 } else {
3744 WARN_ON(1);
3745 return -EINVAL;
3746 }
3747
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003748 path = btrfs_alloc_path();
3749 if (!path)
3750 return -ENOMEM;
3751
Li Zefan33345d012011-04-20 10:31:50 +08003752 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003753 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003754 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003755 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003756 goto out;
3757 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003758
3759 leaf = path->nodes[0];
3760 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3761 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3762 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003763 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003764 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003765 goto out;
3766 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003767 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003768
Josef Bacikd49d3282019-12-18 17:20:28 -05003769 /*
3770 * This is a placeholder inode for a subvolume we didn't have a
3771 * reference to at the time of the snapshot creation. In the meantime
3772 * we could have renamed the real subvol link into our snapshot, so
3773 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3774 * Instead simply lookup the dir_index_item for this entry so we can
3775 * remove it. Otherwise we know we have a ref to the root and we can
3776 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3777 */
3778 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003779 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003780 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003781 if (IS_ERR_OR_NULL(di)) {
3782 if (!di)
3783 ret = -ENOENT;
3784 else
3785 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003786 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003787 goto out;
3788 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003789
3790 leaf = path->nodes[0];
3791 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003792 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003793 btrfs_release_path(path);
3794 } else {
3795 ret = btrfs_del_root_ref(trans, objectid,
3796 root->root_key.objectid, dir_ino,
3797 &index, name, name_len);
3798 if (ret) {
3799 btrfs_abort_transaction(trans, ret);
3800 goto out;
3801 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003802 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003803
Lu Fengqi9add2942018-08-01 11:32:26 +08003804 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003805 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003806 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003807 goto out;
3808 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003809
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003810 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003811 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003812 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003813 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003814 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003815 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003816out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003817 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003818 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003819}
3820
Misono Tomohiroec42f162018-04-18 11:34:13 +09003821/*
3822 * Helper to check if the subvolume references other subvolumes or if it's
3823 * default.
3824 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003825static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003826{
3827 struct btrfs_fs_info *fs_info = root->fs_info;
3828 struct btrfs_path *path;
3829 struct btrfs_dir_item *di;
3830 struct btrfs_key key;
3831 u64 dir_id;
3832 int ret;
3833
3834 path = btrfs_alloc_path();
3835 if (!path)
3836 return -ENOMEM;
3837
3838 /* Make sure this root isn't set as the default subvol */
3839 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3840 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3841 dir_id, "default", 7, 0);
3842 if (di && !IS_ERR(di)) {
3843 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3844 if (key.objectid == root->root_key.objectid) {
3845 ret = -EPERM;
3846 btrfs_err(fs_info,
3847 "deleting default subvolume %llu is not allowed",
3848 key.objectid);
3849 goto out;
3850 }
3851 btrfs_release_path(path);
3852 }
3853
3854 key.objectid = root->root_key.objectid;
3855 key.type = BTRFS_ROOT_REF_KEY;
3856 key.offset = (u64)-1;
3857
3858 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3859 if (ret < 0)
3860 goto out;
3861 BUG_ON(ret == 0);
3862
3863 ret = 0;
3864 if (path->slots[0] > 0) {
3865 path->slots[0]--;
3866 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3867 if (key.objectid == root->root_key.objectid &&
3868 key.type == BTRFS_ROOT_REF_KEY)
3869 ret = -ENOTEMPTY;
3870 }
3871out:
3872 btrfs_free_path(path);
3873 return ret;
3874}
3875
Nikolay Borisov20a68002018-04-27 14:36:24 +03003876/* Delete all dentries for inodes belonging to the root */
3877static void btrfs_prune_dentries(struct btrfs_root *root)
3878{
3879 struct btrfs_fs_info *fs_info = root->fs_info;
3880 struct rb_node *node;
3881 struct rb_node *prev;
3882 struct btrfs_inode *entry;
3883 struct inode *inode;
3884 u64 objectid = 0;
3885
3886 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3887 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3888
3889 spin_lock(&root->inode_lock);
3890again:
3891 node = root->inode_tree.rb_node;
3892 prev = NULL;
3893 while (node) {
3894 prev = node;
3895 entry = rb_entry(node, struct btrfs_inode, rb_node);
3896
David Sterba37508512018-06-29 10:56:40 +02003897 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003898 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003899 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003900 node = node->rb_right;
3901 else
3902 break;
3903 }
3904 if (!node) {
3905 while (prev) {
3906 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003907 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003908 node = prev;
3909 break;
3910 }
3911 prev = rb_next(prev);
3912 }
3913 }
3914 while (node) {
3915 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003916 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003917 inode = igrab(&entry->vfs_inode);
3918 if (inode) {
3919 spin_unlock(&root->inode_lock);
3920 if (atomic_read(&inode->i_count) > 1)
3921 d_prune_aliases(inode);
3922 /*
3923 * btrfs_drop_inode will have it removed from the inode
3924 * cache when its usage count hits zero.
3925 */
3926 iput(inode);
3927 cond_resched();
3928 spin_lock(&root->inode_lock);
3929 goto again;
3930 }
3931
3932 if (cond_resched_lock(&root->inode_lock))
3933 goto again;
3934
3935 node = rb_next(node);
3936 }
3937 spin_unlock(&root->inode_lock);
3938}
3939
Misono Tomohirof60a2362018-04-18 11:34:52 +09003940int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3941{
3942 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3943 struct btrfs_root *root = BTRFS_I(dir)->root;
3944 struct inode *inode = d_inode(dentry);
3945 struct btrfs_root *dest = BTRFS_I(inode)->root;
3946 struct btrfs_trans_handle *trans;
3947 struct btrfs_block_rsv block_rsv;
3948 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003949 int ret;
3950 int err;
3951
3952 /*
3953 * Don't allow to delete a subvolume with send in progress. This is
3954 * inside the inode lock so the error handling that has to drop the bit
3955 * again is not run concurrently.
3956 */
3957 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003958 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003959 spin_unlock(&dest->root_item_lock);
3960 btrfs_warn(fs_info,
3961 "attempt to delete subvolume %llu during send",
3962 dest->root_key.objectid);
3963 return -EPERM;
3964 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003965 root_flags = btrfs_root_flags(&dest->root_item);
3966 btrfs_set_root_flags(&dest->root_item,
3967 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3968 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003969
3970 down_write(&fs_info->subvol_sem);
3971
3972 err = may_destroy_subvol(dest);
3973 if (err)
3974 goto out_up_write;
3975
3976 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3977 /*
3978 * One for dir inode,
3979 * two for dir entries,
3980 * two for root ref/backref.
3981 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003982 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003983 if (err)
3984 goto out_up_write;
3985
3986 trans = btrfs_start_transaction(root, 0);
3987 if (IS_ERR(trans)) {
3988 err = PTR_ERR(trans);
3989 goto out_release;
3990 }
3991 trans->block_rsv = &block_rsv;
3992 trans->bytes_reserved = block_rsv.size;
3993
3994 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3995
Josef Bacik045d3962019-12-18 17:20:27 -05003996 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003997 if (ret) {
3998 err = ret;
3999 btrfs_abort_transaction(trans, ret);
4000 goto out_end_trans;
4001 }
4002
4003 btrfs_record_root_in_trans(trans, dest);
4004
4005 memset(&dest->root_item.drop_progress, 0,
4006 sizeof(dest->root_item.drop_progress));
4007 dest->root_item.drop_level = 0;
4008 btrfs_set_root_refs(&dest->root_item, 0);
4009
4010 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4011 ret = btrfs_insert_orphan_item(trans,
4012 fs_info->tree_root,
4013 dest->root_key.objectid);
4014 if (ret) {
4015 btrfs_abort_transaction(trans, ret);
4016 err = ret;
4017 goto out_end_trans;
4018 }
4019 }
4020
Lu Fengqid1957792018-05-29 15:01:54 +08004021 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004022 BTRFS_UUID_KEY_SUBVOL,
4023 dest->root_key.objectid);
4024 if (ret && ret != -ENOENT) {
4025 btrfs_abort_transaction(trans, ret);
4026 err = ret;
4027 goto out_end_trans;
4028 }
4029 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004030 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004031 dest->root_item.received_uuid,
4032 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4033 dest->root_key.objectid);
4034 if (ret && ret != -ENOENT) {
4035 btrfs_abort_transaction(trans, ret);
4036 err = ret;
4037 goto out_end_trans;
4038 }
4039 }
4040
Qu Wenruo082b6c92020-06-16 10:17:37 +08004041 free_anon_bdev(dest->anon_dev);
4042 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004043out_end_trans:
4044 trans->block_rsv = NULL;
4045 trans->bytes_reserved = 0;
4046 ret = btrfs_end_transaction(trans);
4047 if (ret && !err)
4048 err = ret;
4049 inode->i_flags |= S_DEAD;
4050out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004051 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004052out_up_write:
4053 up_write(&fs_info->subvol_sem);
4054 if (err) {
4055 spin_lock(&dest->root_item_lock);
4056 root_flags = btrfs_root_flags(&dest->root_item);
4057 btrfs_set_root_flags(&dest->root_item,
4058 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4059 spin_unlock(&dest->root_item_lock);
4060 } else {
4061 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004062 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004063 ASSERT(dest->send_in_progress == 0);
4064
4065 /* the last ref */
4066 if (dest->ino_cache_inode) {
4067 iput(dest->ino_cache_inode);
4068 dest->ino_cache_inode = NULL;
4069 }
4070 }
4071
4072 return err;
4073}
4074
Chris Mason39279cc2007-06-12 06:35:45 -04004075static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4076{
David Howells2b0143b2015-03-17 22:25:59 +00004077 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004078 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004079 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004080 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004081 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004082
David Sterbab3ae2442012-09-13 16:04:34 -06004083 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004084 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004085 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004086 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004087
Josef Bacikd52be812013-05-29 14:54:47 -04004088 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004089 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004090 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004091
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004092 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004093 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004094 goto out;
4095 }
4096
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004097 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004098 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004099 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004100
Filipe Manana44f714d2016-06-06 16:11:13 +01004101 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4102
Chris Mason39279cc2007-06-12 06:35:45 -04004103 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004104 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4105 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4106 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004107 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004108 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004109 /*
4110 * Propagate the last_unlink_trans value of the deleted dir to
4111 * its parent directory. This is to prevent an unrecoverable
4112 * log tree in the case we do something like this:
4113 * 1) create dir foo
4114 * 2) create snapshot under dir foo
4115 * 3) delete the snapshot
4116 * 4) rmdir foo
4117 * 5) mkdir foo
4118 * 6) fsync foo or some file inside foo
4119 */
4120 if (last_unlink_trans >= trans->transid)
4121 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4122 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004123out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004124 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004125 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004126
Chris Mason39279cc2007-06-12 06:35:45 -04004127 return err;
4128}
4129
Josef Bacikddfae632017-10-19 14:16:02 -04004130/*
4131 * Return this if we need to call truncate_block for the last bit of the
4132 * truncate.
4133 */
4134#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004135
Chris Mason323ac952008-10-01 19:05:46 -04004136/*
Chris Mason39279cc2007-06-12 06:35:45 -04004137 * this can truncate away extent items, csum items and directory items.
4138 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004139 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004140 *
4141 * csum items that cross the new i_size are truncated to the new size
4142 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004143 *
4144 * min_type is the minimum key type to truncate down to. If set to 0, this
4145 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004146 */
Yan, Zheng80825102009-11-12 09:35:36 +00004147int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4148 struct btrfs_root *root,
4149 struct inode *inode,
4150 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004151{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004152 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004153 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004154 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004155 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004156 struct btrfs_key key;
4157 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004158 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004159 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004160 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004161 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004162 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004163 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004164 int found_extent;
4165 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004166 int pending_del_nr = 0;
4167 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004168 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004169 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004170 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004171 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004172 bool be_nice = false;
4173 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004174 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4175 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004176
4177 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004178
Chris Mason28ed1342014-12-17 09:41:04 -08004179 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004180 * For non-free space inodes and non-shareable roots, we want to back
4181 * off from time to time. This means all inodes in subvolume roots,
4182 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004183 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004184 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004185 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004186 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004187
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004188 path = btrfs_alloc_path();
4189 if (!path)
4190 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004191 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004192
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004193 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004194 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4195 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004196
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004197 /*
4198 * We want to drop from the next block forward in case this
4199 * new size is not block aligned since we will be keeping the
4200 * last block of the extent just the way it is.
4201 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004202 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004203 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004204 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004205 }
Yan, Zheng80825102009-11-12 09:35:36 +00004206
Miao Xie16cdcec2011-04-22 18:12:22 +08004207 /*
4208 * This function is also used to drop the items in the log tree before
4209 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004210 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004211 * items.
4212 */
4213 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004214 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004215
Li Zefan33345d012011-04-20 10:31:50 +08004216 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004217 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004218 key.type = (u8)-1;
4219
Chris Mason85e21ba2008-01-29 15:11:36 -05004220search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004221 /*
4222 * with a 16K leaf size and 128MB extents, you can actually queue
4223 * up a huge file in a single leaf. Most of the time that
4224 * bytes_deleted is > 0, it will be huge by the time we get here
4225 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004226 if (be_nice && bytes_deleted > SZ_32M &&
4227 btrfs_should_end_transaction(trans)) {
4228 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004229 goto out;
4230 }
Chris Masond3977122009-01-05 21:25:51 -05004231
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004232 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4233 if (ret < 0)
4234 goto out;
4235
Chris Mason85e21ba2008-01-29 15:11:36 -05004236 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004237 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004238 /* there are no items in the tree for us to truncate, we're
4239 * done
4240 */
Yan, Zheng80825102009-11-12 09:35:36 +00004241 if (path->slots[0] == 0)
4242 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004243 path->slots[0]--;
4244 }
4245
Chris Masond3977122009-01-05 21:25:51 -05004246 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004247 u64 clear_start = 0, clear_len = 0;
4248
Chris Mason39279cc2007-06-12 06:35:45 -04004249 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004250 leaf = path->nodes[0];
4251 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004252 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004253
Li Zefan33345d012011-04-20 10:31:50 +08004254 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004255 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004256
Chris Mason85e21ba2008-01-29 15:11:36 -05004257 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004258 break;
4259
Chris Mason5f39d392007-10-15 16:14:19 -04004260 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004261 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004262 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004263 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004264 extent_type = btrfs_file_extent_type(leaf, fi);
4265 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004266 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004267 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004268
4269 trace_btrfs_truncate_show_fi_regular(
4270 BTRFS_I(inode), leaf, fi,
4271 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004272 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004273 item_end += btrfs_file_extent_ram_bytes(leaf,
4274 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004275
4276 trace_btrfs_truncate_show_fi_inline(
4277 BTRFS_I(inode), leaf, fi, path->slots[0],
4278 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004279 }
Yan008630c2007-11-07 13:31:09 -05004280 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004281 }
Yan, Zheng80825102009-11-12 09:35:36 +00004282 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004283 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004284 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004285 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004286 break;
4287 if (found_key.offset >= new_size)
4288 del_item = 1;
4289 else
4290 del_item = 0;
4291 }
Chris Mason39279cc2007-06-12 06:35:45 -04004292 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004293 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004294 if (found_type != BTRFS_EXTENT_DATA_KEY)
4295 goto delete;
4296
4297 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004298 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004299
4300 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004301 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004302 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004303 u64 orig_num_bytes =
4304 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004305 extent_num_bytes = ALIGN(new_size -
4306 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004307 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004308 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004309 btrfs_set_file_extent_num_bytes(leaf, fi,
4310 extent_num_bytes);
4311 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004312 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004313 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004314 &root->state) &&
4315 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004316 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004317 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004318 } else {
Chris Masondb945352007-10-15 16:15:53 -04004319 extent_num_bytes =
4320 btrfs_file_extent_disk_num_bytes(leaf,
4321 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004322 extent_offset = found_key.offset -
4323 btrfs_file_extent_offset(leaf, fi);
4324
Chris Mason39279cc2007-06-12 06:35:45 -04004325 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004326 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004327 if (extent_start != 0) {
4328 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004329 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004330 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004331 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004332 }
4333 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004334 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004335 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004336 /*
4337 * we can't truncate inline items that have had
4338 * special encodings
4339 */
4340 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004341 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004342 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4343 btrfs_file_extent_compression(leaf, fi) == 0) {
4344 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004345
Josef Bacikddfae632017-10-19 14:16:02 -04004346 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4347 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004348 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004349 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004350 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004351 * We have to bail so the last_size is set to
4352 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004353 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004354 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004355 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004356 } else {
4357 /*
4358 * Inline extents are special, we just treat
4359 * them as a full sector worth in the file
4360 * extent tree just for simplicity sake.
4361 */
4362 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004363 }
Josef Bacikddfae632017-10-19 14:16:02 -04004364
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004365 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004366 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004367 }
Chris Mason179e29e2007-11-01 11:28:41 -04004368delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004369 /*
4370 * We use btrfs_truncate_inode_items() to clean up log trees for
4371 * multiple fsyncs, and in this case we don't want to clear the
4372 * file extent range because it's just the log.
4373 */
4374 if (root == BTRFS_I(inode)->root) {
4375 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4376 clear_start, clear_len);
4377 if (ret) {
4378 btrfs_abort_transaction(trans, ret);
4379 break;
4380 }
4381 }
4382
Josef Bacikddfae632017-10-19 14:16:02 -04004383 if (del_item)
4384 last_size = found_key.offset;
4385 else
4386 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004387 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004388 if (!pending_del_nr) {
4389 /* no pending yet, add ourselves */
4390 pending_del_slot = path->slots[0];
4391 pending_del_nr = 1;
4392 } else if (pending_del_nr &&
4393 path->slots[0] + 1 == pending_del_slot) {
4394 /* hop on the pending chunk */
4395 pending_del_nr++;
4396 pending_del_slot = path->slots[0];
4397 } else {
Chris Masond3977122009-01-05 21:25:51 -05004398 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004399 }
Chris Mason39279cc2007-06-12 06:35:45 -04004400 } else {
4401 break;
4402 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004403 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004404
Miao Xie27cdeb72014-04-02 19:51:05 +08004405 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004406 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004407 struct btrfs_ref ref = { 0 };
4408
Chris Mason28ed1342014-12-17 09:41:04 -08004409 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004410
4411 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4412 extent_start, extent_num_bytes, 0);
4413 ref.real_root = root->root_key.objectid;
4414 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4415 ino, extent_offset);
4416 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004417 if (ret) {
4418 btrfs_abort_transaction(trans, ret);
4419 break;
4420 }
Chris Mason28f75a02015-02-04 06:59:29 -08004421 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004422 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004423 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004424 }
Chris Mason39279cc2007-06-12 06:35:45 -04004425 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004426
Yan, Zheng80825102009-11-12 09:35:36 +00004427 if (found_type == BTRFS_INODE_ITEM_KEY)
4428 break;
4429
4430 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004431 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004432 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004433 if (pending_del_nr) {
4434 ret = btrfs_del_items(trans, root, path,
4435 pending_del_slot,
4436 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004437 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004438 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004439 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004440 }
Yan, Zheng80825102009-11-12 09:35:36 +00004441 pending_del_nr = 0;
4442 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004443 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004444
Chris Mason28f75a02015-02-04 06:59:29 -08004445 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004446 * We can generate a lot of delayed refs, so we need to
4447 * throttle every once and a while and make sure we're
4448 * adding enough space to keep up with the work we are
4449 * generating. Since we hold a transaction here we
4450 * can't flush, and we don't want to FLUSH_LIMIT because
4451 * we could have generated too many delayed refs to
4452 * actually allocate, so just bail if we're short and
4453 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004454 */
Josef Bacik28bad212018-12-03 10:20:38 -05004455 if (should_throttle) {
4456 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4457 BTRFS_RESERVE_NO_FLUSH);
4458 if (ret) {
4459 ret = -EAGAIN;
4460 break;
4461 }
Chris Mason28f75a02015-02-04 06:59:29 -08004462 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004463 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004464 } else {
4465 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004466 }
Chris Mason39279cc2007-06-12 06:35:45 -04004467 }
Yan, Zheng80825102009-11-12 09:35:36 +00004468out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004469 if (ret >= 0 && pending_del_nr) {
4470 int err;
4471
4472 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004473 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004474 if (err) {
4475 btrfs_abort_transaction(trans, err);
4476 ret = err;
4477 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004478 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004479 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4480 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004481 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004482 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004483 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004484 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4485 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004486 }
Chris Mason28ed1342014-12-17 09:41:04 -08004487
Chris Mason39279cc2007-06-12 06:35:45 -04004488 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004489 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004490}
4491
Chris Masona52d9a82007-08-27 16:49:44 -04004492/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304493 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004494 * @inode - inode that we're zeroing
4495 * @from - the offset to start zeroing
4496 * @len - the length to zero, 0 to zero the entire range respective to the
4497 * offset
4498 * @front - zero up to the offset instead of from the offset on
4499 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304500 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004501 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004502 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304503int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004504 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004505{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004506 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004507 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004508 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4509 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004510 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004511 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004512 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004513 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004514 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004515 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304516 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004517 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004518 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004519 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004520 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304521 u64 block_start;
4522 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004523
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004524 if (IS_ALIGNED(offset, blocksize) &&
4525 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004526 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304527
Josef Bacik8b62f872017-10-19 14:15:55 -04004528 block_start = round_down(from, blocksize);
4529 block_end = block_start + blocksize - 1;
4530
Nikolay Borisov36ea6f32020-06-03 08:55:41 +03004531 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved,
4532 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004533 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004534 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4535 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004536 /* For nocow case, no need to reserve data space */
4537 only_release_metadata = true;
4538 } else {
4539 goto out;
4540 }
4541 }
4542 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4543 if (ret < 0) {
4544 if (!only_release_metadata)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03004545 btrfs_free_reserved_data_space(BTRFS_I(inode),
4546 data_reserved, block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004547 goto out;
4548 }
Chris Mason211c17f2008-05-15 09:13:45 -04004549again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004550 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004551 if (!page) {
Nikolay Borisov86d52922020-06-03 08:55:40 +03004552 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004553 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004554 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004555 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004556 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004557 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004558
Chris Masona52d9a82007-08-27 16:49:44 -04004559 if (!PageUptodate(page)) {
4560 ret = btrfs_readpage(NULL, page);
4561 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004562 if (page->mapping != mapping) {
4563 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004564 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004565 goto again;
4566 }
Chris Masona52d9a82007-08-27 16:49:44 -04004567 if (!PageUptodate(page)) {
4568 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004569 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004570 }
4571 }
Chris Mason211c17f2008-05-15 09:13:45 -04004572 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004573
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304574 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004575 set_page_extent_mapped(page);
4576
Nikolay Borisovc3504372020-06-03 08:55:03 +03004577 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004578 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304579 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004580 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004581 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004582 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004583 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004584 btrfs_put_ordered_extent(ordered);
4585 goto again;
4586 }
4587
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304588 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004589 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4590 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004591
Nikolay Borisovc2566f22020-06-03 08:55:35 +03004592 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004593 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004594 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304595 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004596 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004597 goto out_unlock;
4598 }
4599
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304600 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004601 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304602 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004603 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004604 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304605 memset(kaddr + (block_start - page_offset(page)),
4606 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004607 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304608 memset(kaddr + (block_start - page_offset(page)) + offset,
4609 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004610 flush_dcache_page(page);
4611 kunmap(page);
4612 }
Chris Mason247e7432008-07-17 12:53:51 -04004613 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004614 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004615 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004616
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004617 if (only_release_metadata)
4618 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4619 block_end, EXTENT_NORESERVE, NULL, NULL,
4620 GFP_NOFS);
4621
Chris Mason89642222008-07-24 09:41:53 -04004622out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004623 if (ret) {
4624 if (only_release_metadata)
4625 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4626 blocksize, true);
4627 else
Nikolay Borisov86d52922020-06-03 08:55:40 +03004628 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004629 block_start, blocksize, true);
4630 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004631 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004632 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004633 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004634out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004635 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004636 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004637 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004638 return ret;
4639}
4640
Josef Bacik16e75492013-10-22 12:18:51 -04004641static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4642 u64 offset, u64 len)
4643{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004644 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004645 struct btrfs_trans_handle *trans;
4646 int ret;
4647
4648 /*
4649 * Still need to make sure the inode looks like it's been updated so
4650 * that any holes get logged if we fsync.
4651 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004652 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4653 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004654 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4655 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4656 return 0;
4657 }
4658
4659 /*
4660 * 1 - for the one we're dropping
4661 * 1 - for the one we're adding
4662 * 1 - for updating the inode.
4663 */
4664 trans = btrfs_start_transaction(root, 3);
4665 if (IS_ERR(trans))
4666 return PTR_ERR(trans);
4667
4668 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4669 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004670 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004671 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004672 return ret;
4673 }
4674
David Sterbaf85b7372017-01-20 14:54:07 +01004675 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4676 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004677 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004678 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004679 else
4680 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004681 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004682 return ret;
4683}
4684
Josef Bacik695a0d02011-03-04 15:46:53 -05004685/*
4686 * This function puts in dummy file extents for the area we're creating a hole
4687 * for. So if we are truncating this file to a larger size we need to insert
4688 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4689 * the range between oldsize and size
4690 */
Josef Bacika41ad392011-01-31 15:30:16 -05004691int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004692{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004693 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004694 struct btrfs_root *root = BTRFS_I(inode)->root;
4695 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004696 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004697 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004698 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004699 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4700 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004701 u64 last_byte;
4702 u64 cur_offset;
4703 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004704 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004705
Josef Bacika71754f2013-06-17 17:14:39 -04004706 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304707 * If our size started in the middle of a block we need to zero out the
4708 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004709 * expose stale data.
4710 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304711 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004712 if (err)
4713 return err;
4714
Yan Zheng9036c102008-10-30 14:19:41 -04004715 if (size <= hole_start)
4716 return 0;
4717
David Sterbab272ae22020-02-05 19:09:33 +01004718 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004719 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004720 cur_offset = hole_start;
4721 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004722 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004723 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004724 if (IS_ERR(em)) {
4725 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004726 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004727 break;
4728 }
Yan Zheng9036c102008-10-30 14:19:41 -04004729 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004730 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004731 hole_size = last_byte - cur_offset;
4732
Yan, Zheng80825102009-11-12 09:35:36 +00004733 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004734 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004735
Josef Bacik16e75492013-10-22 12:18:51 -04004736 err = maybe_insert_hole(root, inode, cur_offset,
4737 hole_size);
4738 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004739 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004740
4741 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4742 cur_offset, hole_size);
4743 if (err)
4744 break;
4745
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004746 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004747 cur_offset + hole_size - 1, 0);
4748 hole_em = alloc_extent_map();
4749 if (!hole_em) {
4750 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4751 &BTRFS_I(inode)->runtime_flags);
4752 goto next;
4753 }
4754 hole_em->start = cur_offset;
4755 hole_em->len = hole_size;
4756 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004757
Josef Bacik5dc562c2012-08-17 13:14:17 -04004758 hole_em->block_start = EXTENT_MAP_HOLE;
4759 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004760 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004761 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004762 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004763 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004764
4765 while (1) {
4766 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004767 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004768 write_unlock(&em_tree->lock);
4769 if (err != -EEXIST)
4770 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004771 btrfs_drop_extent_cache(BTRFS_I(inode),
4772 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004773 cur_offset +
4774 hole_size - 1, 0);
4775 }
4776 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004777 } else {
4778 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4779 cur_offset, hole_size);
4780 if (err)
4781 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004782 }
Josef Bacik16e75492013-10-22 12:18:51 -04004783next:
Yan Zheng9036c102008-10-30 14:19:41 -04004784 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004785 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004786 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004787 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004788 break;
4789 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004790 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004791 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004792 return err;
4793}
4794
Eric Sandeen3972f262013-01-12 02:57:22 +00004795static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004796{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004797 struct btrfs_root *root = BTRFS_I(inode)->root;
4798 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004799 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004800 loff_t newsize = attr->ia_size;
4801 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004802 int ret;
4803
Eric Sandeen3972f262013-01-12 02:57:22 +00004804 /*
4805 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4806 * special case where we need to update the times despite not having
4807 * these flags set. For all other operations the VFS set these flags
4808 * explicitly if it wants a timestamp update.
4809 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004810 if (newsize != oldsize) {
4811 inode_inc_iversion(inode);
4812 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4813 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004814 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004815 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004816
Josef Bacika41ad392011-01-31 15:30:16 -05004817 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004818 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004819 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004820 * This is to ensure the snapshot captures a fully consistent
4821 * state of this file - if the snapshot captures this expanding
4822 * truncation, it must capture all writes that happened before
4823 * this truncation.
4824 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004825 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004826 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004827 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004828 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004829 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004830 }
Yan, Zheng80825102009-11-12 09:35:36 +00004831
Miao Xief4a2f4c2011-12-14 20:12:01 -05004832 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004833 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004834 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004835 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004836 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004837
4838 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004839 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304840 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004841 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004842 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004843 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004844 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004845
Josef Bacika41ad392011-01-31 15:30:16 -05004846 /*
4847 * We're truncating a file that used to have good data down to
4848 * zero. Make sure it gets into the ordered flush list so that
4849 * any new writes get down to disk quickly.
4850 */
4851 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004852 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4853 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004854
Josef Bacika41ad392011-01-31 15:30:16 -05004855 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004856
David Sterba8e0fa5d2020-06-09 19:21:48 +02004857 /* Disable nonlocked read DIO to avoid the endless truncate */
4858 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004859 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004860 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004861
Filipe Manana213e8c52018-02-06 20:40:31 +00004862 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004863 if (ret && inode->i_nlink) {
4864 int err;
4865
4866 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004867 * Truncate failed, so fix up the in-memory size. We
4868 * adjusted disk_i_size down as we removed extents, so
4869 * wait for disk_i_size to be stable and then update the
4870 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004871 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004872 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004873 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004874 return err;
4875 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004876 }
Yan, Zheng80825102009-11-12 09:35:36 +00004877 }
4878
Josef Bacika41ad392011-01-31 15:30:16 -05004879 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004880}
4881
Chris Mason39279cc2007-06-12 06:35:45 -04004882static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4883{
David Howells2b0143b2015-03-17 22:25:59 +00004884 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004885 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004886 int err;
4887
Li Zefanb83cc962010-12-20 16:04:08 +08004888 if (btrfs_root_readonly(root))
4889 return -EROFS;
4890
Jan Kara31051c82016-05-26 16:55:18 +02004891 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004892 if (err)
4893 return err;
4894
Chris Mason5a3f23d2009-03-31 13:27:11 -04004895 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004896 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004897 if (err)
4898 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004899 }
Yan Zheng9036c102008-10-30 14:19:41 -04004900
Christoph Hellwig10257742010-06-04 11:30:02 +02004901 if (attr->ia_valid) {
4902 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004903 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004904 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004905
Josef Bacik22c44fe2011-11-30 10:45:38 -05004906 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004907 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004908 }
4909
Chris Mason39279cc2007-06-12 06:35:45 -04004910 return err;
4911}
Chris Mason61295eb2008-01-14 16:24:38 -05004912
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004913/*
4914 * While truncating the inode pages during eviction, we get the VFS calling
4915 * btrfs_invalidatepage() against each page of the inode. This is slow because
4916 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4917 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4918 * extent_state structures over and over, wasting lots of time.
4919 *
4920 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4921 * those expensive operations on a per page basis and do only the ordered io
4922 * finishing, while we release here the extent_map and extent_state structures,
4923 * without the excessive merging and splitting.
4924 */
4925static void evict_inode_truncate_pages(struct inode *inode)
4926{
4927 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4928 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4929 struct rb_node *node;
4930
4931 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004932 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004933
4934 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004935 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004936 struct extent_map *em;
4937
Liu Bo07e1ce02018-08-23 03:51:52 +08004938 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004939 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004940 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4941 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004942 remove_extent_mapping(map_tree, em);
4943 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004944 if (need_resched()) {
4945 write_unlock(&map_tree->lock);
4946 cond_resched();
4947 write_lock(&map_tree->lock);
4948 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004949 }
4950 write_unlock(&map_tree->lock);
4951
Filipe Manana6ca07092015-05-26 00:55:42 +01004952 /*
4953 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004954 * We can have ongoing bios started by readahead that have
4955 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004956 * still in progress (unlocked the pages in the bio but did not yet
4957 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004958 * ranges can still be locked and eviction started because before
4959 * submitting those bios, which are executed by a separate task (work
4960 * queue kthread), inode references (inode->i_count) were not taken
4961 * (which would be dropped in the end io callback of each bio).
4962 * Therefore here we effectively end up waiting for those bios and
4963 * anyone else holding locked ranges without having bumped the inode's
4964 * reference count - if we don't do it, when they access the inode's
4965 * io_tree to unlock a range it may be too late, leading to an
4966 * use-after-free issue.
4967 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004968 spin_lock(&io_tree->lock);
4969 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4970 struct extent_state *state;
4971 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004972 u64 start;
4973 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004974 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004975
4976 node = rb_first(&io_tree->state);
4977 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004978 start = state->start;
4979 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004980 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004981 spin_unlock(&io_tree->lock);
4982
David Sterbaff13db42015-12-03 14:30:40 +01004983 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004984
4985 /*
4986 * If still has DELALLOC flag, the extent didn't reach disk,
4987 * and its reserved space won't be freed by delayed_ref.
4988 * So we need to free its reserved space here.
4989 * (Refer to comment in btrfs_invalidatepage, case 2)
4990 *
4991 * Note, end is the bytenr of last byte, so we need + 1 here.
4992 */
Filipe Manana421f0922018-10-12 13:02:48 +01004993 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03004994 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
4995 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004996
Filipe Manana6ca07092015-05-26 00:55:42 +01004997 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004998 EXTENT_LOCKED | EXTENT_DELALLOC |
4999 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5000 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005001
Filipe Manana7064dd52014-08-08 02:47:05 +01005002 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005003 spin_lock(&io_tree->lock);
5004 }
5005 spin_unlock(&io_tree->lock);
5006}
5007
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005008static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005009 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005010{
5011 struct btrfs_fs_info *fs_info = root->fs_info;
5012 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005013 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005014 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005015 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005016
Josef Bacikd3984c92019-08-01 18:19:37 -04005017 /*
5018 * Eviction should be taking place at some place safe because of our
5019 * delayed iputs. However the normal flushing code will run delayed
5020 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5021 *
5022 * We reserve the delayed_refs_extra here again because we can't use
5023 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5024 * above. We reserve our extra bit here because we generate a ton of
5025 * delayed refs activity by truncating.
5026 *
5027 * If we cannot make our reservation we'll attempt to steal from the
5028 * global reserve, because we really want to be able to free up space.
5029 */
5030 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5031 BTRFS_RESERVE_FLUSH_EVICT);
5032 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005033 /*
5034 * Try to steal from the global reserve if there is space for
5035 * it.
5036 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005037 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5038 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5039 btrfs_warn(fs_info,
5040 "could not allocate space for delete; will truncate on mount");
5041 return ERR_PTR(-ENOSPC);
5042 }
5043 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005044 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005045
5046 trans = btrfs_join_transaction(root);
5047 if (IS_ERR(trans))
5048 return trans;
5049
5050 if (delayed_refs_extra) {
5051 trans->block_rsv = &fs_info->trans_block_rsv;
5052 trans->bytes_reserved = delayed_refs_extra;
5053 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5054 delayed_refs_extra, 1);
5055 }
5056 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005057}
5058
Al Virobd555972010-06-07 11:35:40 -04005059void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005060{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005061 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005062 struct btrfs_trans_handle *trans;
5063 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005064 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005065 int ret;
5066
liubo1abe9b82011-03-24 11:18:59 +00005067 trace_btrfs_inode_evict(inode);
5068
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005069 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005070 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005071 return;
5072 }
5073
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005074 evict_inode_truncate_pages(inode);
5075
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005076 if (inode->i_nlink &&
5077 ((btrfs_root_refs(&root->root_item) != 0 &&
5078 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005079 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005080 goto no_delete;
5081
Omar Sandoval27919062018-05-11 13:13:37 -07005082 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005083 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005084
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005085 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005086
Omar Sandoval7b40b692018-05-11 13:13:33 -07005087 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005088 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005089
Yan, Zheng76dda932009-09-21 16:00:26 -04005090 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005091 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5092 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005093 goto no_delete;
5094 }
5095
Nikolay Borisovaa790212017-01-10 20:35:40 +02005096 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005097 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005098 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005099
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005100 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005101 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005102 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005103 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005104 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005105
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005106 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005107
Yan, Zheng80825102009-11-12 09:35:36 +00005108 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005109 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005110 if (IS_ERR(trans))
5111 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005112
5113 trans->block_rsv = rsv;
5114
Yan, Zhengd68fc572010-05-16 10:49:58 -04005115 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005116 trans->block_rsv = &fs_info->trans_block_rsv;
5117 btrfs_end_transaction(trans);
5118 btrfs_btree_balance_dirty(fs_info);
5119 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5120 goto free_rsv;
5121 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005122 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005123 }
5124
Josef Bacik4ef31a42013-08-13 14:10:08 -04005125 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005126 * Errors here aren't a big deal, it just means we leave orphan items in
5127 * the tree. They will be cleaned up on the next mount. If the inode
5128 * number gets reused, cleanup deletes the orphan item without doing
5129 * anything, and unlink reuses the existing orphan item.
5130 *
5131 * If it turns out that we are dropping too many of these, we might want
5132 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005133 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005134 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005135 if (!IS_ERR(trans)) {
5136 trans->block_rsv = rsv;
5137 btrfs_orphan_del(trans, BTRFS_I(inode));
5138 trans->block_rsv = &fs_info->trans_block_rsv;
5139 btrfs_end_transaction(trans);
5140 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005141
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005142 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005143 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005144 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005145
Omar Sandoval27919062018-05-11 13:13:37 -07005146free_rsv:
5147 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005148no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005149 /*
5150 * If we didn't successfully delete, the orphan item will still be in
5151 * the tree and we'll retry on the next mount. Again, we might also want
5152 * to retry these periodically in the future.
5153 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005154 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005155 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005156}
5157
5158/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005159 * Return the key found in the dir entry in the location pointer, fill @type
5160 * with BTRFS_FT_*, and return 0.
5161 *
Su Yue005d6712018-03-05 17:13:37 +08005162 * If no dir entries were found, returns -ENOENT.
5163 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005164 */
5165static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005166 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005167{
5168 const char *name = dentry->d_name.name;
5169 int namelen = dentry->d_name.len;
5170 struct btrfs_dir_item *di;
5171 struct btrfs_path *path;
5172 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005173 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005174
5175 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005176 if (!path)
5177 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005178
David Sterbaf85b7372017-01-20 14:54:07 +01005179 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5180 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005181 if (IS_ERR_OR_NULL(di)) {
5182 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005183 goto out;
5184 }
Chris Masond3977122009-01-05 21:25:51 -05005185
Chris Mason5f39d392007-10-15 16:14:19 -04005186 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005187 if (location->type != BTRFS_INODE_ITEM_KEY &&
5188 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005189 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005190 btrfs_warn(root->fs_info,
5191"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5192 __func__, name, btrfs_ino(BTRFS_I(dir)),
5193 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005194 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005195 if (!ret)
5196 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005197out:
Chris Mason39279cc2007-06-12 06:35:45 -04005198 btrfs_free_path(path);
5199 return ret;
5200}
5201
5202/*
5203 * when we hit a tree root in a directory, the btrfs part of the inode
5204 * needs to be changed to reflect the root directory of the tree root. This
5205 * is kind of like crossing a mount point.
5206 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005207static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005208 struct inode *dir,
5209 struct dentry *dentry,
5210 struct btrfs_key *location,
5211 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005212{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005213 struct btrfs_path *path;
5214 struct btrfs_root *new_root;
5215 struct btrfs_root_ref *ref;
5216 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005217 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005218 int ret;
5219 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005220
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005221 path = btrfs_alloc_path();
5222 if (!path) {
5223 err = -ENOMEM;
5224 goto out;
5225 }
Chris Mason39279cc2007-06-12 06:35:45 -04005226
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005227 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005228 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5229 key.type = BTRFS_ROOT_REF_KEY;
5230 key.offset = location->objectid;
5231
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005232 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005233 if (ret) {
5234 if (ret < 0)
5235 err = ret;
5236 goto out;
5237 }
Chris Mason39279cc2007-06-12 06:35:45 -04005238
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005239 leaf = path->nodes[0];
5240 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005241 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005242 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5243 goto out;
5244
5245 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5246 (unsigned long)(ref + 1),
5247 dentry->d_name.len);
5248 if (ret)
5249 goto out;
5250
David Sterbab3b4aa72011-04-21 01:20:15 +02005251 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005252
David Sterba56e93572020-05-15 19:35:55 +02005253 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005254 if (IS_ERR(new_root)) {
5255 err = PTR_ERR(new_root);
5256 goto out;
5257 }
5258
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005259 *sub_root = new_root;
5260 location->objectid = btrfs_root_dirid(&new_root->root_item);
5261 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005262 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005263 err = 0;
5264out:
5265 btrfs_free_path(path);
5266 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005267}
5268
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005269static void inode_tree_add(struct inode *inode)
5270{
5271 struct btrfs_root *root = BTRFS_I(inode)->root;
5272 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005273 struct rb_node **p;
5274 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005275 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005276 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005277
Al Viro1d3382cb2010-10-23 15:19:20 -04005278 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005279 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005280 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005281 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005282 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005283 while (*p) {
5284 parent = *p;
5285 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5286
David Sterba37508512018-06-29 10:56:40 +02005287 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005288 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005289 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005290 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005291 else {
5292 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005293 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005294 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005295 RB_CLEAR_NODE(parent);
5296 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005297 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005298 }
5299 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005300 rb_link_node(new, parent, p);
5301 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005302 spin_unlock(&root->inode_lock);
5303}
5304
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005305static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005306{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005307 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005308 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005309
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005310 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005311 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5312 rb_erase(&inode->rb_node, &root->inode_tree);
5313 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005314 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005315 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005316 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005317
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005318 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005319 spin_lock(&root->inode_lock);
5320 empty = RB_EMPTY_ROOT(&root->inode_tree);
5321 spin_unlock(&root->inode_lock);
5322 if (empty)
5323 btrfs_add_dead_root(root);
5324 }
5325}
5326
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005327
Chris Masone02119d2008-09-05 16:13:11 -04005328static int btrfs_init_locked_inode(struct inode *inode, void *p)
5329{
5330 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005331
5332 inode->i_ino = args->ino;
5333 BTRFS_I(inode)->location.objectid = args->ino;
5334 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5335 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005336 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5337 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005338 return 0;
5339}
5340
5341static int btrfs_find_actor(struct inode *inode, void *opaque)
5342{
5343 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005344
5345 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005346 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005347}
5348
David Sterba0202e832020-05-15 19:35:59 +02005349static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005350 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005351{
5352 struct inode *inode;
5353 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005354 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005355
David Sterba0202e832020-05-15 19:35:59 +02005356 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005357 args.root = root;
5358
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005359 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005360 btrfs_init_locked_inode,
5361 (void *)&args);
5362 return inode;
5363}
5364
David Sterba4c66e0d2019-10-03 19:09:35 +02005365/*
David Sterba0202e832020-05-15 19:35:59 +02005366 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005367 * Path can be preallocated to prevent recursing back to iget through
5368 * allocator. NULL is also valid but may require an additional allocation
5369 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305370 */
David Sterba0202e832020-05-15 19:35:59 +02005371struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005372 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305373{
5374 struct inode *inode;
5375
David Sterba0202e832020-05-15 19:35:59 +02005376 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305377 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005378 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305379
5380 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005381 int ret;
5382
Filipe Manana4222ea72018-10-24 10:13:03 +01005383 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005384 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005385 inode_tree_add(inode);
5386 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005387 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005388 iget_failed(inode);
5389 /*
5390 * ret > 0 can come from btrfs_search_slot called by
5391 * btrfs_read_locked_inode, this means the inode item
5392 * was not found.
5393 */
5394 if (ret > 0)
5395 ret = -ENOENT;
5396 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005397 }
5398 }
5399
Balaji Rao1a54ef82008-07-21 02:01:04 +05305400 return inode;
5401}
5402
David Sterba0202e832020-05-15 19:35:59 +02005403struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005404{
David Sterba0202e832020-05-15 19:35:59 +02005405 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005406}
5407
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005408static struct inode *new_simple_dir(struct super_block *s,
5409 struct btrfs_key *key,
5410 struct btrfs_root *root)
5411{
5412 struct inode *inode = new_inode(s);
5413
5414 if (!inode)
5415 return ERR_PTR(-ENOMEM);
5416
Josef Bacik5c8fd992020-02-14 16:11:43 -05005417 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005418 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005419 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005420
5421 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005422 /*
5423 * We only need lookup, the rest is read-only and there's no inode
5424 * associated with the dentry
5425 */
5426 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005427 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005428 inode->i_fop = &simple_dir_operations;
5429 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005430 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305431 inode->i_atime = inode->i_mtime;
5432 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005433 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005434
5435 return inode;
5436}
5437
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005438static inline u8 btrfs_inode_type(struct inode *inode)
5439{
5440 /*
5441 * Compile-time asserts that generic FT_* types still match
5442 * BTRFS_FT_* types
5443 */
5444 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5445 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5446 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5447 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5448 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5449 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5450 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5451 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5452
5453 return fs_umode_to_ftype(inode->i_mode);
5454}
5455
Chris Mason3de45862008-11-17 21:02:50 -05005456struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005457{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005458 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005459 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005460 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005461 struct btrfs_root *sub_root = root;
5462 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005463 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005464 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005465
5466 if (dentry->d_name.len > BTRFS_NAME_LEN)
5467 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005468
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005469 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005470 if (ret < 0)
5471 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005472
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005473 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005474 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005475 if (IS_ERR(inode))
5476 return inode;
5477
5478 /* Do extra check against inode mode with di_type */
5479 if (btrfs_inode_type(inode) != di_type) {
5480 btrfs_crit(fs_info,
5481"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5482 inode->i_mode, btrfs_inode_type(inode),
5483 di_type);
5484 iput(inode);
5485 return ERR_PTR(-EUCLEAN);
5486 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005487 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005488 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005489
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005490 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005491 &location, &sub_root);
5492 if (ret < 0) {
5493 if (ret != -ENOENT)
5494 inode = ERR_PTR(ret);
5495 else
5496 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5497 } else {
David Sterba0202e832020-05-15 19:35:59 +02005498 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005499 }
Josef Bacik87270022020-01-24 09:32:31 -05005500 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005501 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005502
Julia Lawall34d19ba2011-01-24 19:55:19 +00005503 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005504 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005505 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005506 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005507 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005508 if (ret) {
5509 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005510 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005511 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005512 }
5513
Chris Mason3de45862008-11-17 21:02:50 -05005514 return inode;
5515}
5516
Nick Pigginfe15ce42011-01-07 17:49:23 +11005517static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005518{
5519 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005520 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005521
Li Zefan848cce02012-02-21 17:04:28 +08005522 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005523 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005524
Li Zefan848cce02012-02-21 17:04:28 +08005525 if (inode) {
5526 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005527 if (btrfs_root_refs(&root->root_item) == 0)
5528 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005529
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005530 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005531 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005532 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005533 return 0;
5534}
5535
Chris Mason3de45862008-11-17 21:02:50 -05005536static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005537 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005538{
Al Viro3837d202018-10-10 16:38:27 -04005539 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005540
Al Viro3837d202018-10-10 16:38:27 -04005541 if (inode == ERR_PTR(-ENOENT))
5542 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005543 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005544}
5545
Josef Bacik23b5ec72017-07-24 15:14:25 -04005546/*
5547 * All this infrastructure exists because dir_emit can fault, and we are holding
5548 * the tree lock when doing readdir. For now just allocate a buffer and copy
5549 * our information into that, and then dir_emit from the buffer. This is
5550 * similar to what NFS does, only we don't keep the buffer around in pagecache
5551 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5552 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5553 * tree lock.
5554 */
5555static int btrfs_opendir(struct inode *inode, struct file *file)
5556{
5557 struct btrfs_file_private *private;
5558
5559 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5560 if (!private)
5561 return -ENOMEM;
5562 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5563 if (!private->filldir_buf) {
5564 kfree(private);
5565 return -ENOMEM;
5566 }
5567 file->private_data = private;
5568 return 0;
5569}
5570
5571struct dir_entry {
5572 u64 ino;
5573 u64 offset;
5574 unsigned type;
5575 int name_len;
5576};
5577
5578static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5579{
5580 while (entries--) {
5581 struct dir_entry *entry = addr;
5582 char *name = (char *)(entry + 1);
5583
David Sterba92d32172018-04-16 21:10:14 +02005584 ctx->pos = get_unaligned(&entry->offset);
5585 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5586 get_unaligned(&entry->ino),
5587 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005588 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005589 addr += sizeof(struct dir_entry) +
5590 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005591 ctx->pos++;
5592 }
5593 return 0;
5594}
5595
Al Viro9cdda8d2013-05-22 16:48:09 -04005596static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005597{
Al Viro9cdda8d2013-05-22 16:48:09 -04005598 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005599 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005600 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005601 struct btrfs_dir_item *di;
5602 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005603 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005604 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005605 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005606 struct list_head ins_list;
5607 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005608 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005609 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005610 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005611 char *name_ptr;
5612 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005613 int entries = 0;
5614 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005615 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005616 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005617
Al Viro9cdda8d2013-05-22 16:48:09 -04005618 if (!dir_emit_dots(file, ctx))
5619 return 0;
5620
David Woodhouse49593bf2008-08-17 17:08:36 +01005621 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005622 if (!path)
5623 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005624
Josef Bacik23b5ec72017-07-24 15:14:25 -04005625 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005626 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005627
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005628 INIT_LIST_HEAD(&ins_list);
5629 INIT_LIST_HEAD(&del_list);
5630 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005631
Josef Bacik23b5ec72017-07-24 15:14:25 -04005632again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005633 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005634 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005635 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005636
Chris Mason39279cc2007-06-12 06:35:45 -04005637 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5638 if (ret < 0)
5639 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005640
5641 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005642 struct dir_entry *entry;
5643
Chris Mason5f39d392007-10-15 16:14:19 -04005644 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005645 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005646 if (slot >= btrfs_header_nritems(leaf)) {
5647 ret = btrfs_next_leaf(root, path);
5648 if (ret < 0)
5649 goto err;
5650 else if (ret > 0)
5651 break;
5652 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005653 }
Chris Mason3de45862008-11-17 21:02:50 -05005654
Chris Mason5f39d392007-10-15 16:14:19 -04005655 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5656
5657 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005658 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005659 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005660 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005661 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005662 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005663 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005664 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005665 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005666 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005667 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5668 PAGE_SIZE) {
5669 btrfs_release_path(path);
5670 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5671 if (ret)
5672 goto nopos;
5673 addr = private->filldir_buf;
5674 entries = 0;
5675 total_len = 0;
5676 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005677 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005678
5679 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005680 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005681 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005682 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5683 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005684 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005685 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005686 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005687 put_unaligned(location.objectid, &entry->ino);
5688 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005689 entries++;
5690 addr += sizeof(struct dir_entry) + name_len;
5691 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005692next:
5693 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005694 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005695 btrfs_release_path(path);
5696
5697 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5698 if (ret)
5699 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005700
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005701 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005702 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005703 goto nopos;
5704
Zach Browndb62efb2013-07-11 16:19:42 -07005705 /*
5706 * Stop new entries from being returned after we return the last
5707 * entry.
5708 *
5709 * New directory entries are assigned a strictly increasing
5710 * offset. This means that new entries created during readdir
5711 * are *guaranteed* to be seen in the future by that readdir.
5712 * This has broken buggy programs which operate on names as
5713 * they're returned by readdir. Until we re-use freed offsets
5714 * we have this hack to stop new entries from being returned
5715 * under the assumption that they'll never reach this huge
5716 * offset.
5717 *
5718 * This is being careful not to overflow 32bit loff_t unless the
5719 * last entry requires it because doing so has broken 32bit apps
5720 * in the past.
5721 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005722 if (ctx->pos >= INT_MAX)
5723 ctx->pos = LLONG_MAX;
5724 else
5725 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005726nopos:
5727 ret = 0;
5728err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005729 if (put)
5730 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005731 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005732 return ret;
5733}
5734
Chris Mason39279cc2007-06-12 06:35:45 -04005735/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005736 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005737 * inode changes. But, it is most likely to find the inode in cache.
5738 * FIXME, needs more benchmarking...there are no reasons other than performance
5739 * to keep or drop this code.
5740 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005741static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005742{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005743 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005744 struct btrfs_root *root = BTRFS_I(inode)->root;
5745 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005746 int ret;
5747
Josef Bacik72ac3c02012-05-23 14:13:11 -04005748 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005749 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005750
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005751 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005752 if (IS_ERR(trans))
5753 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005754
5755 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005756 if (ret && ret == -ENOSPC) {
5757 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005758 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005759 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005760 if (IS_ERR(trans))
5761 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005762
Chris Mason94b60442010-05-26 11:02:00 -04005763 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005764 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005765 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005766 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005767 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005768
5769 return ret;
5770}
5771
5772/*
5773 * This is a copy of file_update_time. We need this so we can return error on
5774 * ENOSPC for updating the inode in the case of file write and mmap writes.
5775 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005776static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005777 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005778{
Alexander Block2bc5565282012-06-15 09:49:33 +02005779 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005780 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005781
5782 if (btrfs_root_readonly(root))
5783 return -EROFS;
5784
Josef Bacike41f9412012-03-26 09:46:47 -04005785 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005786 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005787 if (flags & S_CTIME)
5788 inode->i_ctime = *now;
5789 if (flags & S_MTIME)
5790 inode->i_mtime = *now;
5791 if (flags & S_ATIME)
5792 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005793 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005794}
5795
Chris Masond352ac62008-09-29 15:18:18 -04005796/*
5797 * find the highest existing sequence number in a directory
5798 * and then set the in-memory index_cnt variable to reflect
5799 * free sequence numbers
5800 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005801static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005802{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005803 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005804 struct btrfs_key key, found_key;
5805 struct btrfs_path *path;
5806 struct extent_buffer *leaf;
5807 int ret;
5808
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005809 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005810 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005811 key.offset = (u64)-1;
5812
5813 path = btrfs_alloc_path();
5814 if (!path)
5815 return -ENOMEM;
5816
5817 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5818 if (ret < 0)
5819 goto out;
5820 /* FIXME: we should be able to handle this */
5821 if (ret == 0)
5822 goto out;
5823 ret = 0;
5824
5825 /*
5826 * MAGIC NUMBER EXPLANATION:
5827 * since we search a directory based on f_pos we have to start at 2
5828 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5829 * else has to start at 2
5830 */
5831 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005832 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005833 goto out;
5834 }
5835
5836 path->slots[0]--;
5837
5838 leaf = path->nodes[0];
5839 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5840
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005841 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005842 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005843 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005844 goto out;
5845 }
5846
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005847 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005848out:
5849 btrfs_free_path(path);
5850 return ret;
5851}
5852
Chris Masond352ac62008-09-29 15:18:18 -04005853/*
5854 * helper to find a free sequence number in a given directory. This current
5855 * code is very simple, later versions will do smarter things in the btree
5856 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005857int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005858{
5859 int ret = 0;
5860
Nikolay Borisov877574e2017-02-20 13:50:33 +02005861 if (dir->index_cnt == (u64)-1) {
5862 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005863 if (ret) {
5864 ret = btrfs_set_inode_index_count(dir);
5865 if (ret)
5866 return ret;
5867 }
Josef Bacikaec74772008-07-24 12:12:38 -04005868 }
5869
Nikolay Borisov877574e2017-02-20 13:50:33 +02005870 *index = dir->index_cnt;
5871 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005872
5873 return ret;
5874}
5875
Chris Masonb0d5d102014-09-08 13:08:51 -07005876static int btrfs_insert_inode_locked(struct inode *inode)
5877{
5878 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005879
5880 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005881 args.root = BTRFS_I(inode)->root;
5882
5883 return insert_inode_locked4(inode,
5884 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5885 btrfs_find_actor, &args);
5886}
5887
Anand Jain19aee8d2017-07-18 17:37:05 +08005888/*
5889 * Inherit flags from the parent inode.
5890 *
5891 * Currently only the compression flags and the cow flags are inherited.
5892 */
5893static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5894{
5895 unsigned int flags;
5896
5897 if (!dir)
5898 return;
5899
5900 flags = BTRFS_I(dir)->flags;
5901
5902 if (flags & BTRFS_INODE_NOCOMPRESS) {
5903 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5904 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5905 } else if (flags & BTRFS_INODE_COMPRESS) {
5906 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5907 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5908 }
5909
5910 if (flags & BTRFS_INODE_NODATACOW) {
5911 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5912 if (S_ISREG(inode->i_mode))
5913 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5914 }
5915
David Sterba7b6a2212018-03-26 18:40:21 +02005916 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005917}
5918
Chris Mason39279cc2007-06-12 06:35:45 -04005919static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5920 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005921 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005922 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005923 u64 ref_objectid, u64 objectid,
5924 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005925{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005926 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005927 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005928 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005929 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005930 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005931 struct btrfs_inode_ref *ref;
5932 struct btrfs_key key[2];
5933 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005934 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005935 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005936 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005937 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005938
Chris Mason5f39d392007-10-15 16:14:19 -04005939 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005940 if (!path)
5941 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005942
Josef Bacik11a19a92019-09-09 10:12:04 -04005943 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005944 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005945 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005946 if (!inode) {
5947 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005948 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005949 }
Chris Mason39279cc2007-06-12 06:35:45 -04005950
Li Zefan581bb052011-04-20 10:06:11 +08005951 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005952 * O_TMPFILE, set link count to 0, so that after this point,
5953 * we fill in an inode item with the correct link count.
5954 */
5955 if (!name)
5956 set_nlink(inode, 0);
5957
5958 /*
Li Zefan581bb052011-04-20 10:06:11 +08005959 * we have to initialize this early, so we can reclaim the inode
5960 * number if we fail afterwards in this function.
5961 */
5962 inode->i_ino = objectid;
5963
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005964 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005965 trace_btrfs_inode_request(dir);
5966
Nikolay Borisov877574e2017-02-20 13:50:33 +02005967 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005968 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005969 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005970 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005971 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005972 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005973 } else if (dir) {
5974 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005975 }
5976 /*
5977 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005978 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005979 * number
5980 */
5981 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005982 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005983 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005984 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005985 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005986
Josef Bacik5dc562c2012-08-17 13:14:17 -04005987 /*
5988 * We could have gotten an inode number from somebody who was fsynced
5989 * and then removed in this same transaction, so let's just set full
5990 * sync since it will be a full sync anyway and this will blow away the
5991 * old info in the log.
5992 */
5993 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5994
Chris Mason9c583092008-01-29 15:15:18 -05005995 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005996 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005997 key[0].offset = 0;
5998
Chris Mason9c583092008-01-29 15:15:18 -05005999 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006000
6001 if (name) {
6002 /*
6003 * Start new inodes with an inode_ref. This is slightly more
6004 * efficient for small numbers of hard links since they will
6005 * be packed into one item. Extended refs will kick in if we
6006 * add more hard links than can fit in the ref item.
6007 */
6008 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006009 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006010 key[1].offset = ref_objectid;
6011
6012 sizes[1] = name_len + sizeof(*ref);
6013 }
Chris Mason9c583092008-01-29 15:15:18 -05006014
Chris Masonb0d5d102014-09-08 13:08:51 -07006015 location = &BTRFS_I(inode)->location;
6016 location->objectid = objectid;
6017 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006018 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006019
6020 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006021 if (ret < 0) {
6022 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006023 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006024 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006025
Chris Masonb9473432009-03-13 11:00:37 -04006026 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006027 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006028 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006029 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006030
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006031 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006032 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306033
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006034 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306035 inode->i_atime = inode->i_mtime;
6036 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006037 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306038
Chris Mason5f39d392007-10-15 16:14:19 -04006039 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6040 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006041 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006042 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006043 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006044
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006045 if (name) {
6046 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6047 struct btrfs_inode_ref);
6048 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6049 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6050 ptr = (unsigned long)(ref + 1);
6051 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6052 }
Chris Mason9c583092008-01-29 15:15:18 -05006053
Chris Mason5f39d392007-10-15 16:14:19 -04006054 btrfs_mark_buffer_dirty(path->nodes[0]);
6055 btrfs_free_path(path);
6056
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006057 btrfs_inherit_iflags(inode, dir);
6058
Al Viro569254b2011-07-24 17:08:40 -04006059 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006060 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006061 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006062 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006063 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6064 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006065 }
6066
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006067 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006068
6069 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006070 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006071
Alexander Block8ea05e32012-07-25 17:35:53 +02006072 btrfs_update_root_times(trans, root);
6073
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006074 ret = btrfs_inode_inherit_props(trans, inode, dir);
6075 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006076 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006077 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006078 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006079
Chris Mason39279cc2007-06-12 06:35:45 -04006080 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006081
6082fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006083 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006084fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006085 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006086 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006087 btrfs_free_path(path);
6088 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006089}
6090
Chris Masond352ac62008-09-29 15:18:18 -04006091/*
6092 * utility function to add 'inode' into 'parent_inode' with
6093 * a give name and a given sequence number.
6094 * if 'add_backref' is true, also insert a backref from the
6095 * inode to the parent directory.
6096 */
Chris Masone02119d2008-09-05 16:13:11 -04006097int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006098 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006099 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006100{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006101 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006102 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006103 struct btrfs_root *root = parent_inode->root;
6104 u64 ino = btrfs_ino(inode);
6105 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006106
Li Zefan33345d012011-04-20 10:31:50 +08006107 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006108 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006109 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006110 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006111 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006112 key.offset = 0;
6113 }
Chris Mason39279cc2007-06-12 06:35:45 -04006114
Li Zefan33345d012011-04-20 10:31:50 +08006115 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006116 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006117 root->root_key.objectid, parent_ino,
6118 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006119 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006120 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6121 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006122 }
6123
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006124 /* Nothing to clean up yet */
6125 if (ret)
6126 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006127
Lu Fengqi684572d2018-08-04 21:10:57 +08006128 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006129 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006130 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006131 goto fail_dir_item;
6132 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006133 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006134 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006135 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006136
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006137 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006138 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006139 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006140 /*
6141 * If we are replaying a log tree, we do not want to update the mtime
6142 * and ctime of the parent directory with the current time, since the
6143 * log replay procedure is responsible for setting them to their correct
6144 * values (the ones it had when the fsync was done).
6145 */
6146 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6147 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6148
6149 parent_inode->vfs_inode.i_mtime = now;
6150 parent_inode->vfs_inode.i_ctime = now;
6151 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006152 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006153 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006154 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006155 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006156
6157fail_dir_item:
6158 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6159 u64 local_index;
6160 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006161 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006162 root->root_key.objectid, parent_ino,
6163 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006164 if (err)
6165 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006166 } else if (add_backref) {
6167 u64 local_index;
6168 int err;
6169
6170 err = btrfs_del_inode_ref(trans, root, name, name_len,
6171 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006172 if (err)
6173 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006174 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006175
6176 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006177 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006178}
6179
6180static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006181 struct btrfs_inode *dir, struct dentry *dentry,
6182 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006183{
Josef Bacika1b075d2010-11-19 20:36:11 +00006184 int err = btrfs_add_link(trans, dir, inode,
6185 dentry->d_name.name, dentry->d_name.len,
6186 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006187 if (err > 0)
6188 err = -EEXIST;
6189 return err;
6190}
6191
Josef Bacik618e21d2007-07-11 10:18:17 -04006192static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006193 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006194{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006195 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006196 struct btrfs_trans_handle *trans;
6197 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006198 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006199 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006200 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006201 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006202
Josef Bacik9ed74f22009-09-11 16:12:44 -04006203 /*
6204 * 2 for inode item and ref
6205 * 2 for dir items
6206 * 1 for xattr if selinux is on
6207 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006208 trans = btrfs_start_transaction(root, 5);
6209 if (IS_ERR(trans))
6210 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006211
Li Zefan581bb052011-04-20 10:06:11 +08006212 err = btrfs_find_free_ino(root, &objectid);
6213 if (err)
6214 goto out_unlock;
6215
Josef Bacikaec74772008-07-24 12:12:38 -04006216 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006217 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6218 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006219 if (IS_ERR(inode)) {
6220 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006221 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006222 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006223 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006224
Casey Schauflerad19db72011-12-15 10:09:07 -05006225 /*
6226 * If the active LSM wants to access the inode during
6227 * d_instantiate it needs these. Smack checks to see
6228 * if the filesystem supports xattrs by looking at the
6229 * ops vector.
6230 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006231 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006232 init_special_inode(inode, inode->i_mode, rdev);
6233
6234 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006235 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006236 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006237
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006238 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6239 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006240 if (err)
6241 goto out_unlock;
6242
6243 btrfs_update_inode(trans, root, inode);
6244 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006245
Josef Bacik618e21d2007-07-11 10:18:17 -04006246out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006247 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006248 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006249 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006250 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006251 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006252 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006253 return err;
6254}
6255
Chris Mason39279cc2007-06-12 06:35:45 -04006256static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006257 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006258{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006259 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -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;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006263 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006264 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006265 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -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);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006275
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;
Chris Mason39279cc2007-06-12 06:35:45 -04006286 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006287 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006288 /*
6289 * If the active LSM wants to access the inode during
6290 * d_instantiate it needs these. Smack checks to see
6291 * if the filesystem supports xattrs by looking at the
6292 * ops vector.
6293 */
6294 inode->i_fop = &btrfs_file_operations;
6295 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006296 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006297
6298 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6299 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006300 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006301
6302 err = btrfs_update_inode(trans, root, inode);
6303 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006304 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006305
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006306 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6307 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006308 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006309 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006310
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006311 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006312 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006313
Chris Mason39279cc2007-06-12 06:35:45 -04006314out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006315 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006316 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006317 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006318 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006319 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006320 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006321 return err;
6322}
6323
6324static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6325 struct dentry *dentry)
6326{
Filipe Manana271dba452016-01-05 16:24:05 +00006327 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006328 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006329 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006330 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006331 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006332 int err;
6333 int drop_inode = 0;
6334
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006335 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006336 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006337 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006338
Mark Fashehf1863732012-08-08 11:32:27 -07006339 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006340 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006341
Nikolay Borisov877574e2017-02-20 13:50:33 +02006342 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006343 if (err)
6344 goto fail;
6345
Yan, Zhenga22285a2010-05-16 10:48:46 -04006346 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006347 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006348 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006349 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006350 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006351 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006352 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006353 if (IS_ERR(trans)) {
6354 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006355 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006356 goto fail;
6357 }
Chris Mason5f39d392007-10-15 16:14:19 -04006358
Miao Xie67de1172013-12-26 13:07:06 +08006359 /* There are several dir indexes for this inode, clear the cache. */
6360 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006361 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006362 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006363 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006364 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006365 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006366
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006367 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6368 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006369
Yan, Zhenga5719522009-09-24 09:17:31 -04006370 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006371 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006372 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006373 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006374
Yan, Zhenga5719522009-09-24 09:17:31 -04006375 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006376 if (err)
6377 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006378 if (inode->i_nlink == 1) {
6379 /*
6380 * If new hard link count is 1, it's a file created
6381 * with open(2) O_TMPFILE flag.
6382 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006383 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006384 if (err)
6385 goto fail;
6386 }
Al Viro08c422c2011-12-23 07:58:13 -05006387 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006388 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006389 }
Chris Mason39279cc2007-06-12 06:35:45 -04006390
Chris Mason1832a6d2007-12-21 16:27:21 -05006391fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006392 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006393 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006394 if (drop_inode) {
6395 inode_dec_link_count(inode);
6396 iput(inode);
6397 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006398 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006399 return err;
6400}
6401
Al Viro18bb1db2011-07-26 01:41:39 -04006402static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006403{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006404 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006405 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006406 struct btrfs_trans_handle *trans;
6407 struct btrfs_root *root = BTRFS_I(dir)->root;
6408 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006409 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006410 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006411
Josef Bacik9ed74f22009-09-11 16:12:44 -04006412 /*
6413 * 2 items for inode and ref
6414 * 2 items for dir items
6415 * 1 for xattr if selinux is on
6416 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006417 trans = btrfs_start_transaction(root, 5);
6418 if (IS_ERR(trans))
6419 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006420
Li Zefan581bb052011-04-20 10:06:11 +08006421 err = btrfs_find_free_ino(root, &objectid);
6422 if (err)
6423 goto out_fail;
6424
Josef Bacikaec74772008-07-24 12:12:38 -04006425 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006426 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6427 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006428 if (IS_ERR(inode)) {
6429 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006430 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006431 goto out_fail;
6432 }
Chris Mason5f39d392007-10-15 16:14:19 -04006433
Chris Masonb0d5d102014-09-08 13:08:51 -07006434 /* these must be set before we unlock the inode */
6435 inode->i_op = &btrfs_dir_inode_operations;
6436 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006437
Eric Paris2a7dba32011-02-01 11:05:39 -05006438 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006439 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006440 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006441
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006442 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006443 err = btrfs_update_inode(trans, root, inode);
6444 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006445 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006446
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006447 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6448 dentry->d_name.name,
6449 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006450 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006451 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006452
Al Viro1e2e5472018-05-04 08:23:01 -04006453 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006454
6455out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006456 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006457 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006458 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006459 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006460 }
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
Chris Masonc8b97812008-10-29 14:49:59 -04006465static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006466 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006467 size_t pg_offset, u64 extent_offset,
6468 struct btrfs_file_extent_item *item)
6469{
6470 int ret;
6471 struct extent_buffer *leaf = path->nodes[0];
6472 char *tmp;
6473 size_t max_size;
6474 unsigned long inline_size;
6475 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006476 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006477
6478 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006479 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006480 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6481 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006482 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006483 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006484 if (!tmp)
6485 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006486 ptr = btrfs_file_extent_inline_start(item);
6487
6488 read_extent_buffer(leaf, tmp, ptr, inline_size);
6489
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006490 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006491 ret = btrfs_decompress(compress_type, tmp, page,
6492 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006493
6494 /*
6495 * decompression code contains a memset to fill in any space between the end
6496 * of the uncompressed data and the end of max_size in case the decompressed
6497 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6498 * the end of an inline extent and the beginning of the next block, so we
6499 * cover that region here.
6500 */
6501
6502 if (max_size + pg_offset < PAGE_SIZE) {
6503 char *map = kmap(page);
6504 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6505 kunmap(page);
6506 }
Chris Masonc8b97812008-10-29 14:49:59 -04006507 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006508 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006509}
6510
Omar Sandoval39b07b52019-12-02 17:34:23 -08006511/**
6512 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6513 * @inode: file to search in
6514 * @page: page to read extent data into if the extent is inline
6515 * @pg_offset: offset into @page to copy to
6516 * @start: file offset
6517 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006518 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006519 * This returns the first &struct extent_map which overlaps with the given
6520 * range, reading it from the B-tree and caching it if necessary. Note that
6521 * there may be more extents which overlap the given range after the returned
6522 * extent_map.
6523 *
6524 * If @page is not NULL and the extent is inline, this also reads the extent
6525 * data directly into the page and marks the extent up to date in the io_tree.
6526 *
6527 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006528 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006529struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006530 struct page *page, size_t pg_offset,
6531 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006532{
David Sterba3ffbd682018-06-29 10:56:42 +02006533 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006534 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006535 u64 extent_start = 0;
6536 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006537 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006538 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006539 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006540 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006541 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006542 struct extent_buffer *leaf;
6543 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006544 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006545 struct extent_map_tree *em_tree = &inode->extent_tree;
6546 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006547
Chris Mason890871b2009-09-02 16:24:52 -04006548 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006549 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006550 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006551
Chris Masona52d9a82007-08-27 16:49:44 -04006552 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006553 if (em->start > start || em->start + em->len <= start)
6554 free_extent_map(em);
6555 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006556 free_extent_map(em);
6557 else
6558 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006559 }
David Sterba172ddd62011-04-21 00:48:27 +02006560 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006561 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006562 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006563 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006564 }
Chris Masond1310b22008-01-24 16:13:08 -05006565 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006566 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006567 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006568 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006569
Liu Bobee6ec82018-08-17 05:05:28 +08006570 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006571 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006572 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006573 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006574 }
6575
Liu Bobee6ec82018-08-17 05:05:28 +08006576 /* Chances are we'll be called again, so go ahead and do readahead */
6577 path->reada = READA_FORWARD;
6578
Liu Boe49aabd2018-08-25 13:47:09 +08006579 /*
6580 * Unless we're going to uncompress the inline extent, no sleep would
6581 * happen.
6582 */
6583 path->leave_spinning = 1;
6584
Josef Bacik51899412020-08-20 11:46:01 -04006585 path->recurse = btrfs_is_free_space_inode(inode);
6586
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006587 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006588 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006589 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006590 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006591 if (path->slots[0] == 0)
6592 goto not_found;
6593 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006594 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006595 }
6596
Chris Mason5f39d392007-10-15 16:14:19 -04006597 leaf = path->nodes[0];
6598 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006599 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006600 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006601 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006602 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006603 /*
6604 * If we backup past the first extent we want to move forward
6605 * and see if there is an extent in front of us, otherwise we'll
6606 * say there is a hole for our whole search range which can
6607 * cause problems.
6608 */
6609 extent_end = start;
6610 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006611 }
6612
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006613 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006614 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006615 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006616 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6617 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006618 /* Only regular file could have regular/prealloc extent */
6619 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006620 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006621 btrfs_crit(fs_info,
6622 "regular/prealloc extent found for non-regular inode %llu",
6623 btrfs_ino(inode));
6624 goto out;
6625 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006626 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6627 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006628 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006629 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6630 path->slots[0],
6631 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006632 }
Josef Bacik25a50342013-10-14 12:08:38 -04006633next:
Yan Zheng9036c102008-10-30 14:19:41 -04006634 if (start >= extent_end) {
6635 path->slots[0]++;
6636 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6637 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006638 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006639 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006640 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006641 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006642
Yan Zheng9036c102008-10-30 14:19:41 -04006643 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006644 }
Yan Zheng9036c102008-10-30 14:19:41 -04006645 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6646 if (found_key.objectid != objectid ||
6647 found_key.type != BTRFS_EXTENT_DATA_KEY)
6648 goto not_found;
6649 if (start + len <= found_key.offset)
6650 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006651 if (start > found_key.offset)
6652 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006653
6654 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006655 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006656 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006657 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006658 em->block_start = EXTENT_MAP_HOLE;
6659 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006660 }
6661
Omar Sandoval39b07b52019-12-02 17:34:23 -08006662 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006663
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006664 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6665 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006666 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006667 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006668 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006669 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006670 size_t size;
6671 size_t extent_offset;
6672 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006673
Omar Sandoval39b07b52019-12-02 17:34:23 -08006674 if (!page)
Yan689f9342007-10-29 11:41:07 -04006675 goto out;
Yan689f9342007-10-29 11:41:07 -04006676
Qu Wenruoe41ca582018-06-06 15:41:49 +08006677 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006678 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006679 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6680 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006681 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006682 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006683 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006684 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006685 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006686
6687 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006688 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006689 if (btrfs_file_extent_compression(leaf, item) !=
6690 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006691 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006692 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006693 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04006694 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04006695 } else {
6696 map = kmap(page);
6697 read_extent_buffer(leaf, map + pg_offset, ptr,
6698 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006699 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006700 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006701 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006702 copy_size);
6703 }
Chris Masonc8b97812008-10-29 14:49:59 -04006704 kunmap(page);
6705 }
Chris Mason179e29e2007-11-01 11:28:41 -04006706 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006707 }
Chris Masond1310b22008-01-24 16:13:08 -05006708 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006709 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006710 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006711 }
6712not_found:
6713 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006714 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006715 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006716 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006717insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006718 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02006719 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006720 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006721 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006722 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6723 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006724 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04006725 goto out;
6726 }
Chris Masond1310b22008-01-24 16:13:08 -05006727
Chris Mason890871b2009-09-02 16:24:52 -04006728 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006729 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006730 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006731out:
Liu Boc6414282018-08-23 07:36:17 +08006732 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006733
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006734 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006735
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006736 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04006737 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006738 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04006739 }
6740 return em;
6741}
6742
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006743struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006744 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006745{
6746 struct extent_map *em;
6747 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006748 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006749 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006750 u64 delalloc_len;
6751 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006752 int err = 0;
6753
Omar Sandoval39b07b52019-12-02 17:34:23 -08006754 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006755 if (IS_ERR(em))
6756 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006757 /*
6758 * If our em maps to:
6759 * - a hole or
6760 * - a pre-alloc extent,
6761 * there might actually be delalloc bytes behind it.
6762 */
6763 if (em->block_start != EXTENT_MAP_HOLE &&
6764 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6765 return em;
6766 else
6767 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006768
6769 /* check to see if we've wrapped (len == -1 or similar) */
6770 end = start + len;
6771 if (end < start)
6772 end = (u64)-1;
6773 else
6774 end -= 1;
6775
6776 em = NULL;
6777
6778 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006779 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006780 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006781 delalloc_end = delalloc_start + delalloc_len;
6782 if (delalloc_end < delalloc_start)
6783 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006784
6785 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006786 * We didn't find anything useful, return the original results from
6787 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006788 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006789 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006790 em = hole_em;
6791 hole_em = NULL;
6792 goto out;
6793 }
6794
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006795 /*
6796 * Adjust the delalloc_start to make sure it doesn't go backwards from
6797 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006798 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006799 delalloc_start = max(start, delalloc_start);
6800 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006801
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006802 if (delalloc_len > 0) {
6803 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006804 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006805 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006806
David Sterba172ddd62011-04-21 00:48:27 +02006807 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006808 if (!em) {
6809 err = -ENOMEM;
6810 goto out;
6811 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006812
6813 ASSERT(hole_em);
6814 /*
6815 * When btrfs_get_extent can't find anything it returns one
6816 * huge hole
6817 *
6818 * Make sure what it found really fits our range, and adjust to
6819 * make sure it is based on the start from the caller
6820 */
6821 if (hole_end <= start || hole_em->start > end) {
6822 free_extent_map(hole_em);
6823 hole_em = NULL;
6824 } else {
6825 hole_start = max(hole_em->start, start);
6826 hole_len = hole_end - hole_start;
6827 }
6828
6829 if (hole_em && delalloc_start > hole_start) {
6830 /*
6831 * Our hole starts before our delalloc, so we have to
6832 * return just the parts of the hole that go until the
6833 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006834 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006835 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006836 em->start = hole_start;
6837 em->orig_start = hole_start;
6838 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006839 * Don't adjust block start at all, it is fixed at
6840 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006841 */
6842 em->block_start = hole_em->block_start;
6843 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006844 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6845 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006846 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006847 /*
6848 * Hole is out of passed range or it starts after
6849 * delalloc range
6850 */
6851 em->start = delalloc_start;
6852 em->len = delalloc_len;
6853 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006854 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006855 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006856 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006857 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006858 return hole_em;
6859 }
6860out:
6861
6862 free_extent_map(hole_em);
6863 if (err) {
6864 free_extent_map(em);
6865 return ERR_PTR(err);
6866 }
6867 return em;
6868}
6869
Nikolay Borisov64f54182020-06-03 08:55:31 +03006870static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006871 const u64 start,
6872 const u64 len,
6873 const u64 orig_start,
6874 const u64 block_start,
6875 const u64 block_len,
6876 const u64 orig_block_len,
6877 const u64 ram_bytes,
6878 const int type)
6879{
6880 struct extent_map *em = NULL;
6881 int ret;
6882
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006883 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03006884 em = create_io_em(inode, start, len, orig_start, block_start,
6885 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08006886 BTRFS_COMPRESS_NONE, /* compress_type */
6887 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006888 if (IS_ERR(em))
6889 goto out;
6890 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03006891 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
6892 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006893 if (ret) {
6894 if (em) {
6895 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03006896 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006897 }
6898 em = ERR_PTR(ret);
6899 }
6900 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006901
6902 return em;
6903}
6904
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006905static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006906 u64 start, u64 len)
6907{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006908 struct btrfs_root *root = inode->root;
6909 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04006910 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006911 struct btrfs_key ins;
6912 u64 alloc_hint;
6913 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006914
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006915 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006916 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006917 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006918 if (ret)
6919 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006920
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006921 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006922 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006923 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006924 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006925 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006926 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
6927 1);
Josef Bacik00361582013-08-14 14:02:47 -04006928
Josef Bacik4b46fce2010-05-23 11:00:55 -04006929 return em;
6930}
6931
Chris Mason46bfbb52010-05-26 11:04:10 -04006932/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006933 * Check if we can do nocow write into the range [@offset, @offset + @len)
6934 *
6935 * @offset: File offset
6936 * @len: The length to write, will be updated to the nocow writeable
6937 * range
6938 * @orig_start: (optional) Return the original file offset of the file extent
6939 * @orig_len: (optional) Return the original on-disk length of the file extent
6940 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07006941 * @strict: if true, omit optimizations that might force us into unnecessary
6942 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006943 *
6944 * This function will flush ordered extents in the range to ensure proper
6945 * nocow checks for (nowait == false) case.
6946 *
6947 * Return:
6948 * >0 and update @len if we can do nocow write
6949 * 0 if we can't do nocow write
6950 * <0 if error happened
6951 *
6952 * NOTE: This only checks the file extents, caller is responsible to wait for
6953 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04006954 */
Josef Bacik00361582013-08-14 14:02:47 -04006955noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006956 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07006957 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04006958{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006959 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006960 struct btrfs_path *path;
6961 int ret;
6962 struct extent_buffer *leaf;
6963 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006964 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006965 struct btrfs_file_extent_item *fi;
6966 struct btrfs_key key;
6967 u64 disk_bytenr;
6968 u64 backref_offset;
6969 u64 extent_end;
6970 u64 num_bytes;
6971 int slot;
6972 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006973 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006974
Chris Mason46bfbb52010-05-26 11:04:10 -04006975 path = btrfs_alloc_path();
6976 if (!path)
6977 return -ENOMEM;
6978
David Sterbaf85b7372017-01-20 14:54:07 +01006979 ret = btrfs_lookup_file_extent(NULL, root, path,
6980 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006981 if (ret < 0)
6982 goto out;
6983
6984 slot = path->slots[0];
6985 if (ret == 1) {
6986 if (slot == 0) {
6987 /* can't find the item, must cow */
6988 ret = 0;
6989 goto out;
6990 }
6991 slot--;
6992 }
6993 ret = 0;
6994 leaf = path->nodes[0];
6995 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006996 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006997 key.type != BTRFS_EXTENT_DATA_KEY) {
6998 /* not our file or wrong item type, must cow */
6999 goto out;
7000 }
7001
7002 if (key.offset > offset) {
7003 /* Wrong offset, must cow */
7004 goto out;
7005 }
7006
7007 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7008 found_type = btrfs_file_extent_type(leaf, fi);
7009 if (found_type != BTRFS_FILE_EXTENT_REG &&
7010 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7011 /* not a regular extent, must cow */
7012 goto out;
7013 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007014
7015 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7016 goto out;
7017
Miao Xiee77751a2013-12-27 21:11:50 +08007018 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7019 if (extent_end <= offset)
7020 goto out;
7021
Chris Mason46bfbb52010-05-26 11:04:10 -04007022 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007023 if (disk_bytenr == 0)
7024 goto out;
7025
7026 if (btrfs_file_extent_compression(leaf, fi) ||
7027 btrfs_file_extent_encryption(leaf, fi) ||
7028 btrfs_file_extent_other_encoding(leaf, fi))
7029 goto out;
7030
Ethan Lien78d42952018-05-17 14:58:29 +08007031 /*
7032 * Do the same check as in btrfs_cross_ref_exist but without the
7033 * unnecessary search.
7034 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007035 if (!strict &&
7036 (btrfs_file_extent_generation(leaf, fi) <=
7037 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007038 goto out;
7039
Chris Mason46bfbb52010-05-26 11:04:10 -04007040 backref_offset = btrfs_file_extent_offset(leaf, fi);
7041
Josef Bacik7ee9e442013-06-21 16:37:03 -04007042 if (orig_start) {
7043 *orig_start = key.offset - backref_offset;
7044 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7045 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7046 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007047
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007048 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007049 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007050
7051 num_bytes = min(offset + *len, extent_end) - offset;
7052 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7053 u64 range_end;
7054
Jeff Mahoneyda170662016-06-15 09:22:56 -04007055 range_end = round_up(offset + num_bytes,
7056 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007057 ret = test_range_bit(io_tree, offset, range_end,
7058 EXTENT_DELALLOC, 0, NULL);
7059 if (ret) {
7060 ret = -EAGAIN;
7061 goto out;
7062 }
7063 }
7064
Josef Bacik1bda19e2013-10-18 12:10:36 -04007065 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007066
7067 /*
7068 * look for other files referencing this extent, if we
7069 * find any we must cow
7070 */
Josef Bacik00361582013-08-14 14:02:47 -04007071
Liu Boe4c3b2d2017-01-30 12:25:28 -08007072 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007073 key.offset - backref_offset, disk_bytenr,
7074 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007075 if (ret) {
7076 ret = 0;
7077 goto out;
7078 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007079
7080 /*
7081 * adjust disk_bytenr and num_bytes to cover just the bytes
7082 * in this extent we are about to write. If there
7083 * are any csums in that range we have to cow in order
7084 * to keep the csums correct
7085 */
7086 disk_bytenr += backref_offset;
7087 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007088 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7089 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007090 /*
7091 * all of the above have passed, it is safe to overwrite this extent
7092 * without cow
7093 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007094 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007095 ret = 1;
7096out:
7097 btrfs_free_path(path);
7098 return ret;
7099}
7100
Josef Bacikeb838e72012-07-31 16:28:48 -04007101static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007102 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007103{
7104 struct btrfs_ordered_extent *ordered;
7105 int ret = 0;
7106
7107 while (1) {
7108 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007109 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007110 /*
7111 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007112 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007113 * extents in this range.
7114 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007115 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007116 lockend - lockstart + 1);
7117
7118 /*
7119 * We need to make sure there are no buffered pages in this
7120 * range either, we could have raced between the invalidate in
7121 * generic_file_direct_write and locking the extent. The
7122 * invalidate needs to happen so that reads after a write do not
7123 * get stale data.
7124 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007125 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007126 (!writing || !filemap_range_has_page(inode->i_mapping,
7127 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007128 break;
7129
7130 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007131 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007132
7133 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007134 /*
7135 * If we are doing a DIO read and the ordered extent we
7136 * found is for a buffered write, we can not wait for it
7137 * to complete and retry, because if we do so we can
7138 * deadlock with concurrent buffered writes on page
7139 * locks. This happens only if our DIO read covers more
7140 * than one extent map, if at this point has already
7141 * created an ordered extent for a previous extent map
7142 * and locked its range in the inode's io tree, and a
7143 * concurrent write against that previous extent map's
7144 * range and this range started (we unlock the ranges
7145 * in the io tree only when the bios complete and
7146 * buffered writes always lock pages before attempting
7147 * to lock range in the io tree).
7148 */
7149 if (writing ||
7150 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007151 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007152 else
7153 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007154 btrfs_put_ordered_extent(ordered);
7155 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007156 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007157 * We could trigger writeback for this range (and wait
7158 * for it to complete) and then invalidate the pages for
7159 * this range (through invalidate_inode_pages2_range()),
7160 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007161 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007162 * triggered a readahead) on a page lock due to an
7163 * ordered dio extent we created before but did not have
7164 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007165 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007166 * ordered extent to complete while holding a lock on
7167 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007168 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007169 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007170 }
7171
Filipe Mananaade77022016-02-18 14:28:55 +00007172 if (ret)
7173 break;
7174
Josef Bacikeb838e72012-07-31 16:28:48 -04007175 cond_resched();
7176 }
7177
7178 return ret;
7179}
7180
Liu Bo6f9994d2017-01-31 07:50:22 -08007181/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007182static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7183 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007184 u64 block_len, u64 orig_block_len,
7185 u64 ram_bytes, int compress_type,
7186 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007187{
7188 struct extent_map_tree *em_tree;
7189 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007190 int ret;
7191
Liu Bo6f9994d2017-01-31 07:50:22 -08007192 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7193 type == BTRFS_ORDERED_COMPRESSED ||
7194 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007195 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007196
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007197 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007198 em = alloc_extent_map();
7199 if (!em)
7200 return ERR_PTR(-ENOMEM);
7201
7202 em->start = start;
7203 em->orig_start = orig_start;
7204 em->len = len;
7205 em->block_len = block_len;
7206 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007207 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007208 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007209 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007210 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007211 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007212 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007213 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007214 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7215 em->compress_type = compress_type;
7216 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007217
7218 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007219 btrfs_drop_extent_cache(inode, em->start,
7220 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007221 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007222 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007223 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007224 /*
7225 * The caller has taken lock_extent(), who could race with us
7226 * to add em?
7227 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007228 } while (ret == -EEXIST);
7229
7230 if (ret) {
7231 free_extent_map(em);
7232 return ERR_PTR(ret);
7233 }
7234
Liu Bo6f9994d2017-01-31 07:50:22 -08007235 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007236 return em;
7237}
7238
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007239
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007240static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007241 struct inode *inode,
7242 struct btrfs_dio_data *dio_data,
7243 u64 start, u64 len)
7244{
7245 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7246 struct extent_map *em = *map;
7247 int ret = 0;
7248
7249 /*
7250 * We don't allocate a new extent in the following cases
7251 *
7252 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7253 * existing extent.
7254 * 2) The extent is marked as PREALLOC. We're good to go here and can
7255 * just use the extent.
7256 *
7257 */
7258 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7259 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7260 em->block_start != EXTENT_MAP_HOLE)) {
7261 int type;
7262 u64 block_start, orig_start, orig_block_len, ram_bytes;
7263
7264 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7265 type = BTRFS_ORDERED_PREALLOC;
7266 else
7267 type = BTRFS_ORDERED_NOCOW;
7268 len = min(len, em->len - (start - em->start));
7269 block_start = em->block_start + (start - em->start);
7270
7271 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007272 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007273 btrfs_inc_nocow_writers(fs_info, block_start)) {
7274 struct extent_map *em2;
7275
Nikolay Borisov64f54182020-06-03 08:55:31 +03007276 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007277 orig_start, block_start,
7278 len, orig_block_len,
7279 ram_bytes, type);
7280 btrfs_dec_nocow_writers(fs_info, block_start);
7281 if (type == BTRFS_ORDERED_PREALLOC) {
7282 free_extent_map(em);
7283 *map = em = em2;
7284 }
7285
7286 if (em2 && IS_ERR(em2)) {
7287 ret = PTR_ERR(em2);
7288 goto out;
7289 }
7290 /*
7291 * For inode marked NODATACOW or extent marked PREALLOC,
7292 * use the existing or preallocated extent, so does not
7293 * need to adjust btrfs_space_info's bytes_may_use.
7294 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007295 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007296 goto skip_cow;
7297 }
7298 }
7299
7300 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007301 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007302 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007303 if (IS_ERR(em)) {
7304 ret = PTR_ERR(em);
7305 goto out;
7306 }
7307
7308 len = min(len, em->len - (start - em->start));
7309
7310skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007311 /*
7312 * Need to update the i_size under the extent lock so buffered
7313 * readers will get the updated i_size when we unlock.
7314 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007315 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007316 i_size_write(inode, start + len);
7317
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007318 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007319out:
7320 return ret;
7321}
7322
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007323static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7324 loff_t length, unsigned int flags, struct iomap *iomap,
7325 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007326{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007327 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007328 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007329 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307330 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007331 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007332 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007333 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007334 u64 len = length;
7335 bool unlock_extents = false;
Josef Bacik0eb79292020-09-03 11:16:51 -04007336 bool sync = (current->journal_info == BTRFS_DIO_SYNC_STUB);
7337
7338 /*
7339 * We used current->journal_info here to see if we were sync, but
7340 * there's a lot of tests in the enospc machinery to not do flushing if
7341 * we have a journal_info set, so we need to clear this out and re-set
7342 * it in iomap_end.
7343 */
7344 ASSERT(current->journal_info == NULL ||
7345 current->journal_info == BTRFS_DIO_SYNC_STUB);
7346 current->journal_info = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007347
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007348 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007349 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007350
Josef Bacikc3298612012-08-03 16:49:19 -04007351 lockstart = start;
7352 lockend = start + len - 1;
7353
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007354 /*
7355 * The generic stuff only does filemap_write_and_wait_range, which
7356 * isn't enough if we've written compressed pages to this area, so we
7357 * need to flush the dirty pages again to make absolutely sure that any
7358 * outstanding dirty pages are on disk.
7359 */
7360 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7361 &BTRFS_I(inode)->runtime_flags)) {
7362 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7363 start + length - 1);
7364 if (ret)
7365 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007366 }
7367
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007368 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7369 if (!dio_data)
7370 return -ENOMEM;
7371
Josef Bacik0eb79292020-09-03 11:16:51 -04007372 dio_data->sync = sync;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007373 dio_data->length = length;
7374 if (write) {
7375 dio_data->reserve = round_up(length, fs_info->sectorsize);
7376 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7377 &dio_data->data_reserved,
7378 start, dio_data->reserve);
7379 if (ret) {
7380 extent_changeset_free(dio_data->data_reserved);
7381 kfree(dio_data);
7382 return ret;
7383 }
7384 }
7385 iomap->private = dio_data;
7386
7387
Josef Bacikeb838e72012-07-31 16:28:48 -04007388 /*
7389 * If this errors out it's because we couldn't invalidate pagecache for
7390 * this range and we need to fallback to buffered.
7391 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007392 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007393 ret = -ENOTBLK;
7394 goto err;
7395 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007396
Omar Sandoval39b07b52019-12-02 17:34:23 -08007397 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007398 if (IS_ERR(em)) {
7399 ret = PTR_ERR(em);
7400 goto unlock_err;
7401 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007402
7403 /*
7404 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7405 * io. INLINE is special, and we could probably kludge it in here, but
7406 * it's still buffered so for safety lets just fall back to the generic
7407 * buffered path.
7408 *
7409 * For COMPRESSED we _have_ to read the entire extent in so we can
7410 * decompress it, so there will be buffering required no matter what we
7411 * do, so go ahead and fallback to buffered.
7412 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007413 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007414 * to buffered IO. Don't blame me, this is the price we pay for using
7415 * the generic code.
7416 */
7417 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7418 em->block_start == EXTENT_MAP_INLINE) {
7419 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007420 ret = -ENOTBLK;
7421 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007422 }
7423
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007424 len = min(len, em->len - (start - em->start));
7425 if (write) {
7426 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7427 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007428 if (ret < 0)
7429 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007430 unlock_extents = true;
7431 /* Recalc len in case the new em is smaller than requested */
7432 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007433 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007434 /*
7435 * We need to unlock only the end area that we aren't using.
7436 * The rest is going to be unlocked by the endio routine.
7437 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007438 lockstart = start + len;
7439 if (lockstart < lockend)
7440 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007441 }
7442
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007443 if (unlock_extents)
7444 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7445 lockstart, lockend, &cached_state);
7446 else
7447 free_extent_state(cached_state);
7448
7449 /*
7450 * Translate extent map information to iomap.
7451 * We trim the extents (and move the addr) even though iomap code does
7452 * that, since we have locked only the parts we are performing I/O in.
7453 */
7454 if ((em->block_start == EXTENT_MAP_HOLE) ||
7455 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7456 iomap->addr = IOMAP_NULL_ADDR;
7457 iomap->type = IOMAP_HOLE;
7458 } else {
7459 iomap->addr = em->block_start + (start - em->start);
7460 iomap->type = IOMAP_MAPPED;
7461 }
7462 iomap->offset = start;
7463 iomap->bdev = fs_info->fs_devices->latest_bdev;
7464 iomap->length = len;
7465
Josef Bacik4b46fce2010-05-23 11:00:55 -04007466 free_extent_map(em);
7467
7468 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007469
7470unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007471 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7472 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007473err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007474 if (dio_data) {
7475 btrfs_delalloc_release_space(BTRFS_I(inode),
7476 dio_data->data_reserved, start,
7477 dio_data->reserve, true);
7478 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7479 extent_changeset_free(dio_data->data_reserved);
7480 kfree(dio_data);
7481 }
7482 return ret;
7483}
7484
7485static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7486 ssize_t written, unsigned int flags, struct iomap *iomap)
7487{
7488 int ret = 0;
7489 struct btrfs_dio_data *dio_data = iomap->private;
7490 size_t submitted = dio_data->submitted;
7491 const bool write = !!(flags & IOMAP_WRITE);
7492
7493 if (!write && (iomap->type == IOMAP_HOLE)) {
7494 /* If reading from a hole, unlock and return */
7495 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7496 goto out;
7497 }
7498
7499 if (submitted < length) {
7500 pos += submitted;
7501 length -= submitted;
7502 if (write)
7503 __endio_write_update_ordered(BTRFS_I(inode), pos,
7504 length, false);
7505 else
7506 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7507 pos + length - 1);
7508 ret = -ENOTBLK;
7509 }
7510
7511 if (write) {
7512 if (dio_data->reserve)
7513 btrfs_delalloc_release_space(BTRFS_I(inode),
7514 dio_data->data_reserved, pos,
7515 dio_data->reserve, true);
7516 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7517 extent_changeset_free(dio_data->data_reserved);
7518 }
7519out:
Josef Bacik0eb79292020-09-03 11:16:51 -04007520 /*
7521 * We're all done, we can re-set the current->journal_info now safely
7522 * for our endio.
7523 */
7524 if (dio_data->sync) {
7525 ASSERT(current->journal_info == NULL);
7526 current->journal_info = BTRFS_DIO_SYNC_STUB;
7527 }
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007528 kfree(dio_data);
7529 iomap->private = NULL;
7530
Josef Bacikeb838e72012-07-31 16:28:48 -04007531 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007532}
7533
Omar Sandoval769b4f22020-04-16 14:46:22 -07007534static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7535{
7536 /*
7537 * This implies a barrier so that stores to dio_bio->bi_status before
7538 * this and loads of dio_bio->bi_status after this are fully ordered.
7539 */
7540 if (!refcount_dec_and_test(&dip->refs))
7541 return;
7542
7543 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007544 __endio_write_update_ordered(BTRFS_I(dip->inode),
7545 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007546 dip->bytes,
7547 !dip->dio_bio->bi_status);
7548 } else {
7549 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7550 dip->logical_offset,
7551 dip->logical_offset + dip->bytes - 1);
7552 }
7553
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007554 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007555 kfree(dip);
7556}
7557
Omar Sandoval77d5d682020-04-16 14:46:25 -07007558static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7559 int mirror_num,
7560 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007561{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007562 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007563 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007564 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007565
Mike Christie37226b22016-06-05 14:31:52 -05007566 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007567
Omar Sandoval5c047a62020-04-16 14:46:24 -07007568 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007569 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007570 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007571
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007572 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007573 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007574 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007575 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007576 return ret;
7577}
7578
Omar Sandoval769b4f22020-04-16 14:46:22 -07007579static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7580 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007581 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007582{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007583 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7584 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007585 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007586 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7587 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007588 struct bio_vec bvec;
7589 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007590 u64 start = io_bio->logical;
7591 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007592 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007593
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007594 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7595 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307596
Liu Bo17347ce2017-05-15 15:33:27 -07007597 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7598 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007599 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007600 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007601 if (uptodate &&
7602 (!csum || !check_data_csum(inode, io_bio, icsum,
7603 bvec.bv_page, pgoff,
7604 start, sectorsize))) {
7605 clean_io_failure(fs_info, failure_tree, io_tree,
7606 start, bvec.bv_page,
7607 btrfs_ino(BTRFS_I(inode)),
7608 pgoff);
7609 } else {
7610 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007611
Omar Sandoval77d5d682020-04-16 14:46:25 -07007612 status = btrfs_submit_read_repair(inode,
7613 &io_bio->bio,
7614 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007615 bvec.bv_page, pgoff,
7616 start,
7617 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007618 io_bio->mirror_num,
7619 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007620 if (status)
7621 err = status;
7622 }
7623 start += sectorsize;
7624 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307625 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307626 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007627 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007628 return err;
7629}
7630
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007631static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007632 const u64 offset, const u64 bytes,
7633 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007634{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007635 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007636 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007637 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007638 u64 ordered_offset = offset;
7639 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007640 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007641
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007642 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007643 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007644 else
Qu Wenruo524272602017-03-08 10:25:52 +08007645 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007646
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007647 while (ordered_offset < offset + bytes) {
7648 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007649 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007650 &ordered_offset,
7651 ordered_bytes,
7652 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007653 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7654 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007655 btrfs_queue_work(wq, &ordered->work);
7656 }
7657 /*
7658 * If btrfs_dec_test_ordered_pending does not find any ordered
7659 * extent in the range, we can exit.
7660 */
7661 if (ordered_offset == last_offset)
7662 return;
7663 /*
7664 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007665 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007666 */
7667 if (ordered_offset < offset + bytes) {
7668 ordered_bytes = offset + bytes - ordered_offset;
7669 ordered = NULL;
7670 }
Chris Mason163cf092010-11-28 19:56:33 -05007671 }
Filipe Manana14543772015-11-24 16:23:54 +00007672}
7673
David Sterbad0ee3932018-03-08 14:35:48 +01007674static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007675 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007676{
Josef Bacikc6100a42017-05-05 11:57:13 -04007677 struct inode *inode = private_data;
Johannes Thumshirnc965d642020-07-28 20:25:41 +09007678
7679 return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007680}
7681
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007682static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007683{
7684 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007685 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007686
Miao Xie8b110e32014-09-12 18:44:03 +08007687 if (err)
7688 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007689 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007690 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7691 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007692 (unsigned long long)bio->bi_iter.bi_sector,
7693 bio->bi_iter.bi_size, err);
7694
Omar Sandoval769b4f22020-04-16 14:46:22 -07007695 if (bio_op(bio) == REQ_OP_READ) {
7696 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007697 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007698 }
7699
Omar Sandoval769b4f22020-04-16 14:46:22 -07007700 if (err)
7701 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007702
Miao Xiee65e1532010-11-22 03:04:43 +00007703 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007704 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007705}
7706
David Sterbad0ee3932018-03-08 14:35:48 +01007707static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7708 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007710 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007711 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007712 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007713 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007714
Liu Bo4c274bc2017-11-01 17:19:27 -06007715 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007716 if (async_submit)
7717 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7718
Josef Bacik5fd02042012-05-02 14:00:54 -04007719 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007720 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007721 if (ret)
7722 goto err;
7723 }
Miao Xiee65e1532010-11-22 03:04:43 +00007724
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007725 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007726 goto map;
7727
7728 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007729 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7730 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007731 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007732 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007733 } else if (write) {
7734 /*
7735 * If we aren't doing async submit, calculate the csum of the
7736 * bio now.
7737 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007738 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007739 if (ret)
7740 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007741 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007742 u64 csum_offset;
7743
7744 csum_offset = file_offset - dip->logical_offset;
7745 csum_offset >>= inode->i_sb->s_blocksize_bits;
7746 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7747 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007748 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007749map:
Chris Mason08635ba2019-07-10 12:28:14 -07007750 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007751err:
Miao Xiee65e1532010-11-22 03:04:43 +00007752 return ret;
7753}
7754
Omar Sandovalc36cac22020-04-16 14:46:13 -07007755/*
7756 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7757 * or ordered extents whether or not we submit any bios.
7758 */
7759static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7760 struct inode *inode,
7761 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007762{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007763 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007764 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7765 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007766 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007767
Omar Sandoval85879572020-04-16 14:46:21 -07007768 dip_size = sizeof(*dip);
7769 if (!write && csum) {
7770 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7771 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7772 size_t nblocks;
7773
7774 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7775 dip_size += csum_size * nblocks;
7776 }
7777
7778 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007779 if (!dip)
7780 return NULL;
7781
Omar Sandovalc36cac22020-04-16 14:46:13 -07007782 dip->inode = inode;
7783 dip->logical_offset = file_offset;
7784 dip->bytes = dio_bio->bi_iter.bi_size;
7785 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007786 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007787 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007788 return dip;
7789}
7790
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007791static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
7792 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007793{
7794 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007795 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007796 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007797 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7798 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007799 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007800 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007801 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007802 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007803 u64 submit_len;
7804 int clone_offset = 0;
7805 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307806 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007807 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007808 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007809 struct btrfs_dio_data *dio_data = iomap->private;
Miao Xiee65e1532010-11-22 03:04:43 +00007810
Omar Sandovalc36cac22020-04-16 14:46:13 -07007811 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7812 if (!dip) {
7813 if (!write) {
7814 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7815 file_offset + dio_bio->bi_iter.bi_size - 1);
7816 }
7817 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007818 bio_endio(dio_bio);
7819 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04007820 }
7821
Omar Sandoval85879572020-04-16 14:46:21 -07007822 if (!write && csum) {
7823 /*
7824 * Load the csums up front to reduce csum tree searches and
7825 * contention when submitting bios.
7826 */
7827 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7828 dip->csums);
7829 if (status != BLK_STS_OK)
7830 goto out_err;
7831 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007832
Omar Sandoval769b4f22020-04-16 14:46:22 -07007833 start_sector = dio_bio->bi_iter.bi_sector;
7834 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007835
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007836 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007837 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7838 start_sector << 9, submit_len,
7839 &geom);
7840 if (ret) {
7841 status = errno_to_blk_status(ret);
7842 goto out_err;
7843 }
7844 ASSERT(geom.len <= INT_MAX);
7845
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007846 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007847
Liu Bo725130b2017-05-16 09:51:39 -07007848 /*
7849 * This will never fail as it's passing GPF_NOFS and
7850 * the allocation is backed by btrfs_bioset.
7851 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007852 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007853 bio->bi_private = dip;
7854 bio->bi_end_io = btrfs_end_dio_bio;
7855 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007856
Liu Bo725130b2017-05-16 09:51:39 -07007857 ASSERT(submit_len >= clone_len);
7858 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007859
Liu Bo725130b2017-05-16 09:51:39 -07007860 /*
7861 * Increase the count before we submit the bio so we know
7862 * the end IO handler won't happen before we increase the
7863 * count. Otherwise, the dip might get freed before we're
7864 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007865 *
7866 * We transfer the initial reference to the last bio, so we
7867 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007868 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007869 if (submit_len > 0) {
7870 refcount_inc(&dip->refs);
7871 /*
7872 * If we are submitting more than one bio, submit them
7873 * all asynchronously. The exception is RAID 5 or 6, as
7874 * asynchronous checksums make it difficult to collect
7875 * full stripe writes.
7876 */
7877 if (!raid56)
7878 async_submit = 1;
7879 }
Miao Xiee65e1532010-11-22 03:04:43 +00007880
David Sterbad0ee3932018-03-08 14:35:48 +01007881 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007882 async_submit);
7883 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007884 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007885 if (submit_len > 0)
7886 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007887 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007888 }
Liu Bo725130b2017-05-16 09:51:39 -07007889
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007890 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07007891 clone_offset += clone_len;
7892 start_sector += clone_len >> 9;
7893 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007894 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007895 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00007896
Miao Xiee65e1532010-11-22 03:04:43 +00007897out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007898 dip->dio_bio->bi_status = status;
7899 btrfs_dio_private_put(dip);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007900 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00007901}
7902
David Sterbaf4c48b42020-06-09 19:19:27 +02007903static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7904 const struct iov_iter *iter, loff_t offset)
7905{
7906 int seg;
7907 int i;
7908 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7909 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007910
David Sterbaf4c48b42020-06-09 19:19:27 +02007911 if (offset & blocksize_mask)
7912 goto out;
7913
7914 if (iov_iter_alignment(iter) & blocksize_mask)
7915 goto out;
7916
7917 /* If this is a write we don't need to check anymore */
7918 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7919 return 0;
7920 /*
7921 * Check to make sure we don't have duplicate iov_base's in this
7922 * iovec, if so return EINVAL, otherwise we'll get csum errors
7923 * when reading back.
7924 */
7925 for (seg = 0; seg < iter->nr_segs; seg++) {
7926 for (i = seg + 1; i < iter->nr_segs; i++) {
7927 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7928 goto out;
7929 }
7930 }
7931 retval = 0;
7932out:
7933 return retval;
7934}
7935
Josef Bacik0eb79292020-09-03 11:16:51 -04007936static inline int btrfs_maybe_fsync_end_io(struct kiocb *iocb, ssize_t size,
7937 int error, unsigned flags)
7938{
7939 /*
7940 * Now if we're still in the context of our submitter we know we can't
7941 * safely run generic_write_sync(), so clear our flag here so that the
7942 * caller knows to follow up with a sync.
7943 */
7944 if (current->journal_info == BTRFS_DIO_SYNC_STUB) {
7945 current->journal_info = NULL;
7946 return error;
7947 }
7948
7949 if (error)
7950 return error;
7951
7952 if (size) {
7953 iocb->ki_flags |= IOCB_DSYNC;
7954 return generic_write_sync(iocb, size);
7955 }
7956
7957 return 0;
7958}
7959
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007960static const struct iomap_ops btrfs_dio_iomap_ops = {
7961 .iomap_begin = btrfs_dio_iomap_begin,
7962 .iomap_end = btrfs_dio_iomap_end,
7963};
7964
7965static const struct iomap_dio_ops btrfs_dio_ops = {
7966 .submit_io = btrfs_submit_direct,
7967};
7968
Josef Bacik0eb79292020-09-03 11:16:51 -04007969static const struct iomap_dio_ops btrfs_sync_dops = {
7970 .submit_io = btrfs_submit_direct,
7971 .end_io = btrfs_maybe_fsync_end_io,
7972};
7973
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007974ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007975{
7976 struct file *file = iocb->ki_filp;
7977 struct inode *inode = file->f_mapping->host;
7978 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7979 struct extent_changeset *data_reserved = NULL;
7980 loff_t offset = iocb->ki_pos;
7981 size_t count = 0;
7982 bool relock = false;
7983 ssize_t ret;
7984
7985 if (check_direct_IO(fs_info, iter, offset))
7986 return 0;
7987
7988 count = iov_iter_count(iter);
7989 if (iov_iter_rw(iter) == WRITE) {
7990 /*
7991 * If the write DIO is beyond the EOF, we need update
7992 * the isize, but it is protected by i_mutex. So we can
7993 * not unlock the i_mutex at this case.
7994 */
7995 if (offset + count <= inode->i_size) {
7996 inode_unlock(inode);
7997 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007998 }
7999 down_read(&BTRFS_I(inode)->dio_sem);
8000 }
8001
Josef Bacik0eb79292020-09-03 11:16:51 -04008002 /*
8003 * We have are actually a sync iocb, so we need our fancy endio to know
8004 * if we need to sync.
8005 */
8006 if (current->journal_info)
8007 ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops,
8008 &btrfs_sync_dops, is_sync_kiocb(iocb));
8009 else
8010 ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops,
8011 &btrfs_dio_ops, is_sync_kiocb(iocb));
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008012
8013 if (ret == -ENOTBLK)
8014 ret = 0;
8015
8016 if (iov_iter_rw(iter) == WRITE)
David Sterbaf4c48b42020-06-09 19:19:27 +02008017 up_read(&BTRFS_I(inode)->dio_sem);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008018
David Sterbaf4c48b42020-06-09 19:19:27 +02008019 if (relock)
8020 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02008021
David Sterbaf4c48b42020-06-09 19:19:27 +02008022 extent_changeset_free(data_reserved);
8023 return ret;
8024}
Chris Mason16432982008-04-10 10:23:21 -04008025
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008026static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008027 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008028{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008029 int ret;
8030
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008031 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008032 if (ret)
8033 return ret;
8034
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008035 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008036}
8037
Chris Mason9ebefb182007-06-15 13:50:00 -04008038int btrfs_readpage(struct file *file, struct page *page)
8039{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008040 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8041 u64 start = page_offset(page);
8042 u64 end = start + PAGE_SIZE - 1;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008043 unsigned long bio_flags = 0;
Nikolay Borisov0f208812020-09-14 14:39:16 +03008044 struct bio *bio = NULL;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008045 int ret;
8046
Nikolay Borisov0f208812020-09-14 14:39:16 +03008047 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8048
8049 ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008050 if (bio)
8051 ret = submit_one_bio(bio, 0, bio_flags);
8052 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008053}
Chris Mason1832a6d2007-12-21 16:27:21 -05008054
Chris Mason39279cc2007-06-12 06:35:45 -04008055static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8056{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008057 struct inode *inode = page->mapping->host;
8058 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008059
8060 if (current->flags & PF_MEMALLOC) {
8061 redirty_page_for_writepage(wbc, page);
8062 unlock_page(page);
8063 return 0;
8064 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008065
8066 /*
8067 * If we are under memory pressure we will call this directly from the
8068 * VM, we need to make sure we have the inode referenced for the ordered
8069 * extent. If not just return like we didn't do anything.
8070 */
8071 if (!igrab(inode)) {
8072 redirty_page_for_writepage(wbc, page);
8073 return AOP_WRITEPAGE_ACTIVATE;
8074 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008075 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008076 btrfs_add_delayed_iput(inode);
8077 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008078}
Chris Mason39279cc2007-06-12 06:35:45 -04008079
Eric Sandeen48a3b632013-04-25 20:41:01 +00008080static int btrfs_writepages(struct address_space *mapping,
8081 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008082{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008083 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008084}
8085
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008086static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008087{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008088 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008089}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008090
Chris Masone6dcd2d2008-07-17 12:53:50 -04008091static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008092{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008093 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008094 if (ret == 1)
8095 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008096 return ret;
8097}
Chris Mason39279cc2007-06-12 06:35:45 -04008098
Chris Masone6dcd2d2008-07-17 12:53:50 -04008099static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8100{
Chris Mason98509cf2008-09-11 15:51:43 -04008101 if (PageWriteback(page) || PageDirty(page))
8102 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008103 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008104}
8105
Roman Gushchinf8e66082020-03-04 16:57:35 -08008106#ifdef CONFIG_MIGRATION
8107static int btrfs_migratepage(struct address_space *mapping,
8108 struct page *newpage, struct page *page,
8109 enum migrate_mode mode)
8110{
8111 int ret;
8112
8113 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8114 if (ret != MIGRATEPAGE_SUCCESS)
8115 return ret;
8116
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008117 if (page_has_private(page))
8118 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008119
8120 if (PagePrivate2(page)) {
8121 ClearPagePrivate2(page);
8122 SetPagePrivate2(newpage);
8123 }
8124
8125 if (mode != MIGRATE_SYNC_NO_COPY)
8126 migrate_page_copy(newpage, page);
8127 else
8128 migrate_page_states(newpage, page);
8129 return MIGRATEPAGE_SUCCESS;
8130}
8131#endif
8132
Lukas Czernerd47992f2013-05-21 23:17:23 -04008133static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8134 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008135{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008136 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8137 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008138 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008139 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008140 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008141 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308142 u64 start;
8143 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008144 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008145
Chris Mason8b62b722009-09-02 16:53:46 -04008146 /*
8147 * we have the page locked, so new writeback can't start,
8148 * and the dirty bit won't be cleared while we are here.
8149 *
8150 * Wait for IO on this page so that we can safely clear
8151 * the PagePrivate2 bit and do ordered accounting
8152 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008153 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008154
Chris Masone6dcd2d2008-07-17 12:53:50 -04008155 if (offset) {
8156 btrfs_releasepage(page, GFP_NOFS);
8157 return;
8158 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008159
8160 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008161 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308162again:
8163 start = page_start;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008164 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008165 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008166 end = min(page_end,
8167 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008168 /*
8169 * IO on this page will never be started, so we need
8170 * to account for any ordered extents now
8171 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008172 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308173 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008174 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008175 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008176 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008177 /*
8178 * whoever cleared the private bit is responsible
8179 * for the finish_ordered_io
8180 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008181 if (TestClearPagePrivate2(page)) {
8182 struct btrfs_ordered_inode_tree *tree;
8183 u64 new_len;
8184
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008185 tree = &inode->ordered_tree;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008186
8187 spin_lock_irq(&tree->lock);
8188 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308189 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008190 if (new_len < ordered->truncated_len)
8191 ordered->truncated_len = new_len;
8192 spin_unlock_irq(&tree->lock);
8193
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008194 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8195 start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308196 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008197 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008198 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008199 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008200 if (!inode_evicting) {
8201 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308202 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008203 &cached_state);
8204 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308205
8206 start = end + 1;
8207 if (start < page_end)
8208 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008209 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008210
Qu Wenruob9d0b382015-09-29 10:35:16 +08008211 /*
8212 * Qgroup reserved space handler
8213 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008214 * 1) Already written to disk or ordered extent already submitted
8215 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8216 * Qgroup will be handled by its qgroup_record then.
8217 * btrfs_qgroup_free_data() call will do nothing here.
8218 *
8219 * 2) Not written to disk yet
8220 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8221 * bit of its io_tree, and free the qgroup reserved data space.
8222 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008223 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008224 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008225 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008226 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008227 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8228 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008229 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008230
8231 __btrfs_releasepage(page, GFP_NOFS);
8232 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008233
Chris Mason4a096752008-07-21 10:29:44 -04008234 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008235 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008236}
8237
Chris Mason9ebefb182007-06-15 13:50:00 -04008238/*
8239 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8240 * called from a page fault handler when a page is first dirtied. Hence we must
8241 * be careful to check for EOF conditions here. We set the page up correctly
8242 * for a written page which means we get ENOSPC checking when writing into
8243 * holes and correct delalloc and unwritten extent mapping on filesystems that
8244 * support these features.
8245 *
8246 * We are not allowed to take the i_mutex here so we have to play games to
8247 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008248 * truncate_setsize() writes the inode size before removing pages, once we have
8249 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008250 * beyond EOF, then the page is guaranteed safe against truncation until we
8251 * unlock the page.
8252 */
Souptick Joardera528a242018-06-06 19:54:44 +05308253vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008254{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008255 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008256 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008257 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008258 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8259 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008260 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008261 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008262 char *kaddr;
8263 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008264 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308265 vm_fault_t ret;
8266 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008267 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308268 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008269 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008270 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308271 u64 end;
8272
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008273 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008274
Jan Karab2b5ef52012-06-12 16:20:45 +02008275 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008276 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008277 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308278 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008279
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308280 /*
8281 * Reserving delalloc space after obtaining the page lock can lead to
8282 * deadlock. For example, if a dirty page is locked by this function
8283 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8284 * dirty page write out, then the btrfs_writepage() function could
8285 * end up waiting indefinitely to get a lock on the page currently
8286 * being processed by btrfs_page_mkwrite() function.
8287 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008288 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8289 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308290 if (!ret2) {
8291 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008292 reserved = 1;
8293 }
Souptick Joardera528a242018-06-06 19:54:44 +05308294 if (ret2) {
8295 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008296 if (reserved)
8297 goto out;
8298 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008299 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008300
Nick Piggin56a76f82009-03-31 15:23:23 -07008301 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008302again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008303 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008304 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008305
Chris Mason9ebefb182007-06-15 13:50:00 -04008306 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008307 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008308 /* page got truncated out from underneath us */
8309 goto out_unlock;
8310 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008311 wait_on_page_writeback(page);
8312
David Sterbaff13db42015-12-03 14:30:40 +01008313 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008314 set_page_extent_mapped(page);
8315
Chris Masoneb84ae02008-07-17 13:53:27 -04008316 /*
8317 * we can't set the delalloc bits if there are pending ordered
8318 * extents. Drop our locks and wait for them to finish
8319 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008320 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8321 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008322 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008323 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008324 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008325 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008326 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008327 btrfs_put_ordered_extent(ordered);
8328 goto again;
8329 }
8330
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008331 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008332 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008333 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008334 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308335 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008336 btrfs_delalloc_release_space(BTRFS_I(inode),
8337 data_reserved, page_start,
8338 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308339 }
8340 }
8341
Josef Bacikfbf19082009-10-01 17:10:23 -04008342 /*
Liu Bo54160342016-12-13 12:15:19 -08008343 * page_mkwrite gets called when the page is firstly dirtied after it's
8344 * faulted in, but write(2) could also dirty a page and set delalloc
8345 * bits, thus in this case for space account reason, we still need to
8346 * clear any delalloc bits within this page range since we have to
8347 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008348 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308349 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008350 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8351 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008352
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008353 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008354 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308355 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008356 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008357 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008358 ret = VM_FAULT_SIGBUS;
8359 goto out_unlock;
8360 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008361
8362 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008363 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008364 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008365 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008366 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008367
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008368 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008369 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008370 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008371 flush_dcache_page(page);
8372 kunmap(page);
8373 }
Chris Mason247e7432008-07-17 12:53:51 -04008374 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008375 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008376 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008377
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008378 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008379 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008380 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008381
David Sterbae43bbe52017-12-12 21:43:52 +01008382 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008383
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008384 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8385 sb_end_pagefault(inode->i_sb);
8386 extent_changeset_free(data_reserved);
8387 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008388
8389out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008390 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008391out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008392 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008393 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008394 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008395out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008396 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008397 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008398 return ret;
8399}
8400
Filipe Manana213e8c52018-02-06 20:40:31 +00008401static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008402{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008403 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008404 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008405 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008406 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008407 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008408 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008409 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008410
Filipe Manana213e8c52018-02-06 20:40:31 +00008411 if (!skip_writeback) {
8412 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8413 (u64)-1);
8414 if (ret)
8415 return ret;
8416 }
Chris Mason39279cc2007-06-12 06:35:45 -04008417
Josef Bacikfcb80c22011-05-03 10:40:22 -04008418 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008419 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8420 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008421 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008422 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008423 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008424 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008425 * be free'd up by the truncate operation, but also have some slack
8426 * space reserved in case it uses space during the truncate (thank you
8427 * very much snapshotting).
8428 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008429 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008430 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008431 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008432 * doesn't end up using space reserved for updating the inode. We also
8433 * need to be able to stop the transaction and start a new one, which
8434 * means we need to be able to update the inode several times, and we
8435 * have no idea of knowing how many times that will be, so we can't just
8436 * reserve 1 item for the entirety of the operation, so that has to be
8437 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008438 *
8439 * So that leaves us with
8440 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008441 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008442 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008443 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008444 * updating the inode.
8445 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008446 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008447 if (!rsv)
8448 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008449 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008450 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008451
Josef Bacik907cbce2011-08-08 13:46:15 -04008452 /*
Josef Bacik07127182011-08-19 10:29:59 -04008453 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008454 * 1 for updating the inode.
8455 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008456 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008457 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008458 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008459 goto out;
8460 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008461
Josef Bacik907cbce2011-08-08 13:46:15 -04008462 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008463 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008464 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008465 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008466
Chris Mason5a3f23d2009-03-31 13:27:11 -04008467 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008468 * So if we truncate and then write and fsync we normally would just
8469 * write the extents that changed, which is a problem if we need to
8470 * first truncate that entire inode. So set this flag so we write out
8471 * all of the extents in the inode to the sync log so we're completely
8472 * safe.
8473 */
8474 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008475 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008476
Yan, Zheng80825102009-11-12 09:35:36 +00008477 while (1) {
8478 ret = btrfs_truncate_inode_items(trans, root, inode,
8479 inode->i_size,
8480 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008481 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008482 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008483 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008484
Yan, Zheng80825102009-11-12 09:35:36 +00008485 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008486 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008487 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008488
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008489 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008490 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008491
8492 trans = btrfs_start_transaction(root, 2);
8493 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008494 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008495 trans = NULL;
8496 break;
8497 }
8498
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008499 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008500 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008501 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008502 BUG_ON(ret); /* shouldn't happen */
8503 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008504 }
8505
Josef Bacikddfae632017-10-19 14:16:02 -04008506 /*
8507 * We can't call btrfs_truncate_block inside a trans handle as we could
8508 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8509 * we've truncated everything except the last little bit, and can do
8510 * btrfs_truncate_block and then update the disk_i_size.
8511 */
8512 if (ret == NEED_TRUNCATE_BLOCK) {
8513 btrfs_end_transaction(trans);
8514 btrfs_btree_balance_dirty(fs_info);
8515
8516 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8517 if (ret)
8518 goto out;
8519 trans = btrfs_start_transaction(root, 1);
8520 if (IS_ERR(trans)) {
8521 ret = PTR_ERR(trans);
8522 goto out;
8523 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008524 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008525 }
8526
Chris Mason917c16b2011-11-08 14:49:59 -05008527 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008528 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008529
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008530 trans->block_rsv = &fs_info->trans_block_rsv;
8531 ret2 = btrfs_update_inode(trans, root, inode);
8532 if (ret2 && !ret)
8533 ret = ret2;
8534
8535 ret2 = btrfs_end_transaction(trans);
8536 if (ret2 && !ret)
8537 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008538 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008539 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008540out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008541 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008542
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008543 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008544}
8545
Sven Wegener3b963622008-06-09 21:57:42 -04008546/*
Chris Masond352ac62008-09-29 15:18:18 -04008547 * create a new subvolume directory/inode (helper for the ioctl).
8548 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008549int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008550 struct btrfs_root *new_root,
8551 struct btrfs_root *parent_root,
8552 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008553{
Chris Mason39279cc2007-06-12 06:35:45 -04008554 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008555 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008556 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008557
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008558 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8559 new_dirid, new_dirid,
8560 S_IFDIR | (~current_umask() & S_IRWXUGO),
8561 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008562 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008563 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008564 inode->i_op = &btrfs_dir_inode_operations;
8565 inode->i_fop = &btrfs_dir_file_operations;
8566
Miklos Szeredibfe86842011-10-28 14:13:29 +02008567 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008568 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008569 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008570
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008571 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8572 if (err)
8573 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008574 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008575 new_root->root_key.objectid, err);
8576
Yan, Zheng76dda932009-09-21 16:00:26 -04008577 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008578
Yan, Zheng76dda932009-09-21 16:00:26 -04008579 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008580 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008581}
8582
Chris Mason39279cc2007-06-12 06:35:45 -04008583struct inode *btrfs_alloc_inode(struct super_block *sb)
8584{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008585 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008586 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008587 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008588
David Sterba712e36c2017-10-31 17:08:27 +01008589 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008590 if (!ei)
8591 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008592
8593 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008594 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008595 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008596 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008597 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008598 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008599 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008600 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008601 ei->disk_i_size = 0;
8602 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008603 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008604 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008605 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008606 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008607 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008608 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008609
Josef Bacik9e0baf62011-07-15 15:16:44 +00008610 spin_lock_init(&ei->lock);
8611 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008612 if (sb->s_magic != BTRFS_TEST_MAGIC)
8613 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8614 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008615 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008616 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008617 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008618
Miao Xie16cdcec2011-04-22 18:12:22 +08008619 ei->delayed_node = NULL;
8620
chandan r9cc97d62012-07-04 12:48:07 +05308621 ei->i_otime.tv_sec = 0;
8622 ei->i_otime.tv_nsec = 0;
8623
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008624 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008625 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008626 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8627 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8628 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008629 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8630 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008631 ei->io_tree.track_uptodate = true;
8632 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008633 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008634 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008635 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008636 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008637 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008638 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008639 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008640
8641 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008642}
8643
Josef Bacikaaedb552013-10-11 14:44:09 -04008644#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8645void btrfs_test_destroy_inode(struct inode *inode)
8646{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008647 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008648 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8649}
8650#endif
8651
Al Viro26602ca2019-04-10 15:14:41 -04008652void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008653{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008654 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8655}
8656
Nikolay Borisov633cc812020-09-18 12:15:49 +03008657void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008658{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008659 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008660 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8661 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008662
Nikolay Borisov633cc812020-09-18 12:15:49 +03008663 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8664 WARN_ON(vfs_inode->i_data.nrpages);
8665 WARN_ON(inode->block_rsv.reserved);
8666 WARN_ON(inode->block_rsv.size);
8667 WARN_ON(inode->outstanding_extents);
8668 WARN_ON(inode->delalloc_bytes);
8669 WARN_ON(inode->new_delalloc_bytes);
8670 WARN_ON(inode->csum_bytes);
8671 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008672
Chris Mason5a3f23d2009-03-31 13:27:11 -04008673 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008674 * This can happen where we create an inode, but somebody else also
8675 * created the same inode and we need to destroy the one we already
8676 * created.
8677 */
8678 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008679 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008680
Chris Masond3977122009-01-05 21:25:51 -05008681 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008682 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008683 if (!ordered)
8684 break;
8685 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008686 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008687 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008688 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008689 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008690 btrfs_put_ordered_extent(ordered);
8691 btrfs_put_ordered_extent(ordered);
8692 }
8693 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008694 btrfs_qgroup_check_reserved_leak(inode);
8695 inode_tree_del(inode);
8696 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8697 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8698 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008699}
8700
Al Viro45321ac2010-06-07 13:43:19 -04008701int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008702{
8703 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008704
Naohiro Aota6379ef92013-06-06 09:56:34 +00008705 if (root == NULL)
8706 return 1;
8707
Liu Bofa6ac872013-02-20 14:10:23 +00008708 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008709 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008710 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008711 else
Al Viro45321ac2010-06-07 13:43:19 -04008712 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008713}
8714
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008715static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008716{
8717 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8718
8719 inode_init_once(&ei->vfs_inode);
8720}
8721
David Sterbae67c7182018-02-19 17:24:18 +01008722void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008723{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008724 /*
8725 * Make sure all delayed rcu free inodes are flushed before we
8726 * destroy cache.
8727 */
8728 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008729 kmem_cache_destroy(btrfs_inode_cachep);
8730 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008731 kmem_cache_destroy(btrfs_path_cachep);
8732 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008733 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008734}
8735
Liu Bof5c29bd2017-11-02 17:21:50 -06008736int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008737{
David Sterba837e1972012-09-07 03:00:48 -06008738 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008739 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008740 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8741 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008742 if (!btrfs_inode_cachep)
8743 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008744
David Sterba837e1972012-09-07 03:00:48 -06008745 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008746 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008747 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008748 if (!btrfs_trans_handle_cachep)
8749 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008750
David Sterba837e1972012-09-07 03:00:48 -06008751 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008752 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008753 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008754 if (!btrfs_path_cachep)
8755 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008756
David Sterba837e1972012-09-07 03:00:48 -06008757 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008758 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008759 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008760 if (!btrfs_free_space_cachep)
8761 goto fail;
8762
Christophe Leroy3acd4852019-08-21 15:05:55 +00008763 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8764 PAGE_SIZE, PAGE_SIZE,
8765 SLAB_RED_ZONE, NULL);
8766 if (!btrfs_free_space_bitmap_cachep)
8767 goto fail;
8768
Chris Mason39279cc2007-06-12 06:35:45 -04008769 return 0;
8770fail:
8771 btrfs_destroy_cachep();
8772 return -ENOMEM;
8773}
8774
David Howellsa528d352017-01-31 16:46:22 +00008775static int btrfs_getattr(const struct path *path, struct kstat *stat,
8776 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008777{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008778 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008779 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008780 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008781 u32 bi_flags = BTRFS_I(inode)->flags;
8782
8783 stat->result_mask |= STATX_BTIME;
8784 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8785 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8786 if (bi_flags & BTRFS_INODE_APPEND)
8787 stat->attributes |= STATX_ATTR_APPEND;
8788 if (bi_flags & BTRFS_INODE_COMPRESS)
8789 stat->attributes |= STATX_ATTR_COMPRESSED;
8790 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8791 stat->attributes |= STATX_ATTR_IMMUTABLE;
8792 if (bi_flags & BTRFS_INODE_NODUMP)
8793 stat->attributes |= STATX_ATTR_NODUMP;
8794
8795 stat->attributes_mask |= (STATX_ATTR_APPEND |
8796 STATX_ATTR_COMPRESSED |
8797 STATX_ATTR_IMMUTABLE |
8798 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008799
Chris Mason39279cc2007-06-12 06:35:45 -04008800 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008801 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008802
8803 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008804 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008805 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008806 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008807 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008808 return 0;
8809}
8810
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008811static int btrfs_rename_exchange(struct inode *old_dir,
8812 struct dentry *old_dentry,
8813 struct inode *new_dir,
8814 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008815{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008816 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008817 struct btrfs_trans_handle *trans;
8818 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008819 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008820 struct inode *new_inode = new_dentry->d_inode;
8821 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008822 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008823 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8824 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008825 u64 old_idx = 0;
8826 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008827 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01008828 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008829 bool root_log_pinned = false;
8830 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008831
8832 /* we only allow rename subvolume link between subvolumes */
8833 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8834 return -EXDEV;
8835
8836 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008837 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8838 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008839 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008840
8841 /*
8842 * We want to reserve the absolute worst case amount of items. So if
8843 * both inodes are subvols and we need to unlink them then that would
8844 * require 4 item modifications, but if they are both normal inodes it
8845 * would require 5 item modifications, so we'll assume their normal
8846 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8847 * should cover the worst case number of items we'll modify.
8848 */
8849 trans = btrfs_start_transaction(root, 12);
8850 if (IS_ERR(trans)) {
8851 ret = PTR_ERR(trans);
8852 goto out_notrans;
8853 }
8854
Josef Bacik3e174092019-11-15 15:43:06 -05008855 if (dest != root)
8856 btrfs_record_root_in_trans(trans, dest);
8857
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008858 /*
8859 * We need to find a free sequence number both in the source and
8860 * in the destination directory for the exchange.
8861 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008862 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008863 if (ret)
8864 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008865 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008866 if (ret)
8867 goto out_fail;
8868
8869 BTRFS_I(old_inode)->dir_index = 0ULL;
8870 BTRFS_I(new_inode)->dir_index = 0ULL;
8871
8872 /* Reference for the source. */
8873 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8874 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008875 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008876 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008877 btrfs_pin_log_trans(root);
8878 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008879 ret = btrfs_insert_inode_ref(trans, dest,
8880 new_dentry->d_name.name,
8881 new_dentry->d_name.len,
8882 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008883 btrfs_ino(BTRFS_I(new_dir)),
8884 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008885 if (ret)
8886 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008887 }
8888
8889 /* And now for the dest. */
8890 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8891 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008892 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008893 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008894 btrfs_pin_log_trans(dest);
8895 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008896 ret = btrfs_insert_inode_ref(trans, root,
8897 old_dentry->d_name.name,
8898 old_dentry->d_name.len,
8899 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008900 btrfs_ino(BTRFS_I(old_dir)),
8901 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008902 if (ret)
8903 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008904 }
8905
8906 /* Update inode version and ctime/mtime. */
8907 inode_inc_iversion(old_dir);
8908 inode_inc_iversion(new_dir);
8909 inode_inc_iversion(old_inode);
8910 inode_inc_iversion(new_inode);
8911 old_dir->i_ctime = old_dir->i_mtime = ctime;
8912 new_dir->i_ctime = new_dir->i_mtime = ctime;
8913 old_inode->i_ctime = ctime;
8914 new_inode->i_ctime = ctime;
8915
8916 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008917 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8918 BTRFS_I(old_inode), 1);
8919 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8920 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008921 }
8922
8923 /* src is a subvolume */
8924 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008925 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008926 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008927 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8928 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008929 old_dentry->d_name.name,
8930 old_dentry->d_name.len);
8931 if (!ret)
8932 ret = btrfs_update_inode(trans, root, old_inode);
8933 }
8934 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008935 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008936 goto out_fail;
8937 }
8938
8939 /* dest is a subvolume */
8940 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008941 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008942 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008943 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8944 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008945 new_dentry->d_name.name,
8946 new_dentry->d_name.len);
8947 if (!ret)
8948 ret = btrfs_update_inode(trans, dest, new_inode);
8949 }
8950 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008951 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008952 goto out_fail;
8953 }
8954
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008955 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008956 new_dentry->d_name.name,
8957 new_dentry->d_name.len, 0, old_idx);
8958 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008959 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008960 goto out_fail;
8961 }
8962
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008963 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008964 old_dentry->d_name.name,
8965 old_dentry->d_name.len, 0, new_idx);
8966 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008967 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008968 goto out_fail;
8969 }
8970
8971 if (old_inode->i_nlink == 1)
8972 BTRFS_I(old_inode)->dir_index = old_idx;
8973 if (new_inode->i_nlink == 1)
8974 BTRFS_I(new_inode)->dir_index = new_idx;
8975
Filipe Manana86e8aa02016-05-05 02:02:27 +01008976 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01008977 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
8978 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008979 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008980 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008981 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008982 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01008983 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
8984 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008985 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008986 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008987 }
8988out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008989 /*
8990 * If we have pinned a log and an error happened, we unpin tasks
8991 * trying to sync the log and force them to fallback to a transaction
8992 * commit if the log currently contains any of the inodes involved in
8993 * this rename operation (to ensure we do not persist a log with an
8994 * inconsistent state for any of these inodes or leading to any
8995 * inconsistencies when replayed). If the transaction was aborted, the
8996 * abortion reason is propagated to userspace when attempting to commit
8997 * the transaction. If the log does not contain any of these inodes, we
8998 * allow the tasks to sync it.
8999 */
9000 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009001 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9002 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9003 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009004 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009005 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009006 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009007
9008 if (root_log_pinned) {
9009 btrfs_end_log_trans(root);
9010 root_log_pinned = false;
9011 }
9012 if (dest_log_pinned) {
9013 btrfs_end_log_trans(dest);
9014 dest_log_pinned = false;
9015 }
9016 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009017 ret2 = btrfs_end_transaction(trans);
9018 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009019out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009020 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9021 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009022 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009023
9024 return ret;
9025}
9026
9027static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9028 struct btrfs_root *root,
9029 struct inode *dir,
9030 struct dentry *dentry)
9031{
9032 int ret;
9033 struct inode *inode;
9034 u64 objectid;
9035 u64 index;
9036
9037 ret = btrfs_find_free_ino(root, &objectid);
9038 if (ret)
9039 return ret;
9040
9041 inode = btrfs_new_inode(trans, root, dir,
9042 dentry->d_name.name,
9043 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009044 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009045 objectid,
9046 S_IFCHR | WHITEOUT_MODE,
9047 &index);
9048
9049 if (IS_ERR(inode)) {
9050 ret = PTR_ERR(inode);
9051 return ret;
9052 }
9053
9054 inode->i_op = &btrfs_special_inode_operations;
9055 init_special_inode(inode, inode->i_mode,
9056 WHITEOUT_DEV);
9057
9058 ret = btrfs_init_inode_security(trans, inode, dir,
9059 &dentry->d_name);
9060 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009061 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009062
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009063 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9064 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009065 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009066 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009067
9068 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009069out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009070 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009071 if (ret)
9072 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009073 iput(inode);
9074
Filipe Mananac9901612016-05-05 01:41:57 +01009075 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009076}
9077
Chris Mason39279cc2007-06-12 06:35:45 -04009078static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009079 struct inode *new_dir, struct dentry *new_dentry,
9080 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009081{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009082 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009083 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009084 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009085 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9086 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009087 struct inode *new_inode = d_inode(new_dentry);
9088 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009089 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009090 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009091 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009092 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009093 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009094
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009095 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009096 return -EPERM;
9097
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009098 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009099 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009100 return -EXDEV;
9101
Li Zefan33345d012011-04-20 10:31:50 +08009102 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009103 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009104 return -ENOTEMPTY;
9105
Chris Mason39279cc2007-06-12 06:35:45 -04009106 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009107 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009108 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009109
9110
9111 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009112 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009113 new_dentry->d_name.name,
9114 new_dentry->d_name.len);
9115
9116 if (ret) {
9117 if (ret == -EEXIST) {
9118 /* we shouldn't get
9119 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309120 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009121 return ret;
9122 }
9123 } else {
9124 /* maybe -EOVERFLOW */
9125 return ret;
9126 }
9127 }
9128 ret = 0;
9129
Chris Mason5a3f23d2009-03-31 13:27:11 -04009130 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009131 * we're using rename to replace one file with another. Start IO on it
9132 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009133 */
Chris Mason8d875f92014-08-12 10:47:42 -07009134 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009135 filemap_flush(old_inode->i_mapping);
9136
Yan, Zheng76dda932009-09-21 16:00:26 -04009137 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009138 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009139 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009140 /*
9141 * We want to reserve the absolute worst case amount of items. So if
9142 * both inodes are subvols and we need to unlink them then that would
9143 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009144 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009145 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9146 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009147 * If our rename has the whiteout flag, we need more 5 units for the
9148 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9149 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009150 */
Filipe Manana5062af32016-05-05 10:26:26 +01009151 trans_num_items = 11;
9152 if (flags & RENAME_WHITEOUT)
9153 trans_num_items += 5;
9154 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009155 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009156 ret = PTR_ERR(trans);
9157 goto out_notrans;
9158 }
Chris Mason5f39d392007-10-15 16:14:19 -04009159
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009160 if (dest != root)
9161 btrfs_record_root_in_trans(trans, dest);
9162
Nikolay Borisov877574e2017-02-20 13:50:33 +02009163 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009164 if (ret)
9165 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009166
Miao Xie67de1172013-12-26 13:07:06 +08009167 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009168 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009169 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009170 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009171 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009172 btrfs_pin_log_trans(root);
9173 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009174 ret = btrfs_insert_inode_ref(trans, dest,
9175 new_dentry->d_name.name,
9176 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009177 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009178 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009179 if (ret)
9180 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009181 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009182
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009183 inode_inc_iversion(old_dir);
9184 inode_inc_iversion(new_dir);
9185 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009186 old_dir->i_ctime = old_dir->i_mtime =
9187 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009188 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009189
Chris Mason12fcfd22009-03-24 10:24:20 -04009190 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009191 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9192 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009193
Li Zefan33345d012011-04-20 10:31:50 +08009194 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009195 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009196 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009197 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9198 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009199 old_dentry->d_name.name,
9200 old_dentry->d_name.len);
9201 if (!ret)
9202 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009203 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009204 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009205 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009206 goto out_fail;
9207 }
Chris Mason39279cc2007-06-12 06:35:45 -04009208
9209 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009210 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009211 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009212 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009213 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009214 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009215 BUG_ON(new_inode->i_nlink == 0);
9216 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009217 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9218 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009219 new_dentry->d_name.name,
9220 new_dentry->d_name.len);
9221 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009222 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009223 ret = btrfs_orphan_add(trans,
9224 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009225 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009226 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009227 goto out_fail;
9228 }
Chris Mason39279cc2007-06-12 06:35:45 -04009229 }
Josef Bacikaec74772008-07-24 12:12:38 -04009230
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009231 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009232 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009233 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009234 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009235 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009236 goto out_fail;
9237 }
Chris Mason39279cc2007-06-12 06:35:45 -04009238
Miao Xie67de1172013-12-26 13:07:06 +08009239 if (old_inode->i_nlink == 1)
9240 BTRFS_I(old_inode)->dir_index = index;
9241
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009242 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009243 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9244 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009245 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009246 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009247 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009248
9249 if (flags & RENAME_WHITEOUT) {
9250 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9251 old_dentry);
9252
9253 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009254 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009255 goto out_fail;
9256 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009257 }
Chris Mason39279cc2007-06-12 06:35:45 -04009258out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009259 /*
9260 * If we have pinned the log and an error happened, we unpin tasks
9261 * trying to sync the log and force them to fallback to a transaction
9262 * commit if the log currently contains any of the inodes involved in
9263 * this rename operation (to ensure we do not persist a log with an
9264 * inconsistent state for any of these inodes or leading to any
9265 * inconsistencies when replayed). If the transaction was aborted, the
9266 * abortion reason is propagated to userspace when attempting to commit
9267 * the transaction. If the log does not contain any of these inodes, we
9268 * allow the tasks to sync it.
9269 */
9270 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009271 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9272 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9273 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009274 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009275 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009276 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009277
9278 btrfs_end_log_trans(root);
9279 log_pinned = false;
9280 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009281 ret2 = btrfs_end_transaction(trans);
9282 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009283out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009284 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009285 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009286
Chris Mason39279cc2007-06-12 06:35:45 -04009287 return ret;
9288}
9289
Miklos Szeredi80ace852014-07-23 15:15:32 +02009290static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9291 struct inode *new_dir, struct dentry *new_dentry,
9292 unsigned int flags)
9293{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009294 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009295 return -EINVAL;
9296
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009297 if (flags & RENAME_EXCHANGE)
9298 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9299 new_dentry);
9300
9301 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009302}
9303
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009304struct btrfs_delalloc_work {
9305 struct inode *inode;
9306 struct completion completion;
9307 struct list_head list;
9308 struct btrfs_work work;
9309};
9310
Miao Xie8ccf6f192012-10-25 09:28:04 +00009311static void btrfs_run_delalloc_work(struct btrfs_work *work)
9312{
9313 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009314 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009315
9316 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9317 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009318 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009319 filemap_flush(inode->i_mapping);
9320 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9321 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009322 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009323
Nikolay Borisov076da912018-04-23 10:54:16 +03009324 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009325 complete(&delalloc_work->completion);
9326}
9327
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009328static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009329{
9330 struct btrfs_delalloc_work *work;
9331
David Sterba100d5702015-12-08 14:39:32 +01009332 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009333 if (!work)
9334 return NULL;
9335
9336 init_completion(&work->completion);
9337 INIT_LIST_HEAD(&work->list);
9338 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009339 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009340
9341 return work;
9342}
9343
Chris Masond352ac62008-09-29 15:18:18 -04009344/*
9345 * some fairly slow code that needs optimization. This walks the list
9346 * of all the inodes with pending delalloc and forces them to disk.
9347 */
Josef Bacikb4912132020-07-21 10:22:12 -04009348static int start_delalloc_inodes(struct btrfs_root *root, u64 *nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009349{
Chris Masonea8c2812008-08-04 23:17:27 -04009350 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009351 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009352 struct btrfs_delalloc_work *work, *next;
9353 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009354 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009355 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009356
Miao Xie8ccf6f192012-10-25 09:28:04 +00009357 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009358 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009359
Miao Xie573bfb72014-03-06 13:55:03 +08009360 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009361 spin_lock(&root->delalloc_lock);
9362 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009363 while (!list_empty(&splice)) {
9364 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009365 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009366
Miao Xieeb73c1b2013-05-15 07:48:22 +00009367 list_move_tail(&binode->delalloc_inodes,
9368 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009369 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009370 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009371 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009372 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009373 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009374 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009375
Ethan Lien3cd24c62018-11-01 14:49:03 +08009376 if (snapshot)
9377 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9378 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009379 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009380 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009381 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009382 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009383 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009384 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009385 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009386 btrfs_queue_work(root->fs_info->flush_workers,
9387 &work->work);
Josef Bacikb4912132020-07-21 10:22:12 -04009388 if (*nr != U64_MAX) {
9389 (*nr)--;
9390 if (*nr == 0)
9391 goto out;
9392 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009393 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009394 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009395 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009396 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009397
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009398out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009399 list_for_each_entry_safe(work, next, &works, list) {
9400 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009401 wait_for_completion(&work->completion);
9402 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009403 }
9404
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009405 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009406 spin_lock(&root->delalloc_lock);
9407 list_splice_tail(&splice, &root->delalloc_inodes);
9408 spin_unlock(&root->delalloc_lock);
9409 }
Miao Xie573bfb72014-03-06 13:55:03 +08009410 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009411 return ret;
9412}
9413
Ethan Lien3cd24c62018-11-01 14:49:03 +08009414int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009415{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009416 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb4912132020-07-21 10:22:12 -04009417 u64 nr = U64_MAX;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009418
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009419 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009420 return -EROFS;
9421
Josef Bacikb4912132020-07-21 10:22:12 -04009422 return start_delalloc_inodes(root, &nr, true);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009423}
9424
Josef Bacikb4912132020-07-21 10:22:12 -04009425int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009426{
9427 struct btrfs_root *root;
9428 struct list_head splice;
9429 int ret;
9430
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009431 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009432 return -EROFS;
9433
9434 INIT_LIST_HEAD(&splice);
9435
Miao Xie573bfb72014-03-06 13:55:03 +08009436 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009437 spin_lock(&fs_info->delalloc_root_lock);
9438 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009439 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009440 root = list_first_entry(&splice, struct btrfs_root,
9441 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009442 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009443 BUG_ON(!root);
9444 list_move_tail(&root->delalloc_root,
9445 &fs_info->delalloc_roots);
9446 spin_unlock(&fs_info->delalloc_root_lock);
9447
Josef Bacikb4912132020-07-21 10:22:12 -04009448 ret = start_delalloc_inodes(root, &nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009449 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009450 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009451 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009452 spin_lock(&fs_info->delalloc_root_lock);
9453 }
9454 spin_unlock(&fs_info->delalloc_root_lock);
9455
Miao Xie6c255e62014-03-06 13:55:01 +08009456 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009457out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009458 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009459 spin_lock(&fs_info->delalloc_root_lock);
9460 list_splice_tail(&splice, &fs_info->delalloc_roots);
9461 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009462 }
Miao Xie573bfb72014-03-06 13:55:03 +08009463 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009464 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009465}
9466
Chris Mason39279cc2007-06-12 06:35:45 -04009467static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9468 const char *symname)
9469{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009470 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009471 struct btrfs_trans_handle *trans;
9472 struct btrfs_root *root = BTRFS_I(dir)->root;
9473 struct btrfs_path *path;
9474 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009475 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009476 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009477 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309478 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009479 int name_len;
9480 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009481 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009482 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009483 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009484
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009485 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009486 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009487 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009488
Josef Bacik9ed74f22009-09-11 16:12:44 -04009489 /*
9490 * 2 items for inode item and ref
9491 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009492 * 1 item for updating parent inode item
9493 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009494 * 1 item for xattr if selinux is on
9495 */
Filipe Manana9269d122015-12-31 18:16:29 +00009496 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009497 if (IS_ERR(trans))
9498 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009499
Li Zefan581bb052011-04-20 10:06:11 +08009500 err = btrfs_find_free_ino(root, &objectid);
9501 if (err)
9502 goto out_unlock;
9503
Josef Bacikaec74772008-07-24 12:12:38 -04009504 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009505 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9506 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009507 if (IS_ERR(inode)) {
9508 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009509 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009510 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009511 }
Chris Mason39279cc2007-06-12 06:35:45 -04009512
Casey Schauflerad19db72011-12-15 10:09:07 -05009513 /*
9514 * If the active LSM wants to access the inode during
9515 * d_instantiate it needs these. Smack checks to see
9516 * if the filesystem supports xattrs by looking at the
9517 * ops vector.
9518 */
9519 inode->i_fop = &btrfs_file_operations;
9520 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009521 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009522 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9523
9524 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9525 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009526 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009527
Chris Mason39279cc2007-06-12 06:35:45 -04009528 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009529 if (!path) {
9530 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009531 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009532 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009533 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009534 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009535 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009536 datasize = btrfs_file_extent_calc_inline_size(name_len);
9537 err = btrfs_insert_empty_item(trans, root, path, &key,
9538 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009539 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009540 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009541 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009542 }
Chris Mason5f39d392007-10-15 16:14:19 -04009543 leaf = path->nodes[0];
9544 ei = btrfs_item_ptr(leaf, path->slots[0],
9545 struct btrfs_file_extent_item);
9546 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9547 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009548 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009549 btrfs_set_file_extent_encryption(leaf, ei, 0);
9550 btrfs_set_file_extent_compression(leaf, ei, 0);
9551 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9552 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9553
Chris Mason39279cc2007-06-12 06:35:45 -04009554 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009555 write_extent_buffer(leaf, symname, ptr, name_len);
9556 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009557 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009558
Chris Mason39279cc2007-06-12 06:35:45 -04009559 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009560 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009561 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009562 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009563 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009564 /*
9565 * Last step, add directory indexes for our symlink inode. This is the
9566 * last step to avoid extra cleanup of these indexes if an error happens
9567 * elsewhere above.
9568 */
9569 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009570 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9571 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009572 if (err)
9573 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009574
Al Viro1e2e5472018-05-04 08:23:01 -04009575 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009576
9577out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009578 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009579 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009580 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009581 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009582 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009583 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009584 return err;
9585}
Chris Mason16432982008-04-10 10:23:21 -04009586
Filipe Manana8fccebf2020-09-08 11:27:20 +01009587static struct btrfs_trans_handle *insert_prealloc_file_extent(
9588 struct btrfs_trans_handle *trans_in,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009589 struct inode *inode, struct btrfs_key *ins,
9590 u64 file_offset)
9591{
9592 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009593 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009594 struct btrfs_trans_handle *trans = trans_in;
9595 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009596 u64 start = ins->objectid;
9597 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009598 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009599
9600 memset(&stack_fi, 0, sizeof(stack_fi));
9601
9602 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9603 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9604 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9605 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9606 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9607 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9608 /* Encryption and other encoding is reserved and all 0 */
9609
Nikolay Borisov72b7d152020-06-03 08:55:18 +03009610 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009611 if (ret < 0)
Filipe Manana8fccebf2020-09-08 11:27:20 +01009612 return ERR_PTR(ret);
9613
9614 if (trans) {
9615 ret = insert_reserved_file_extent(trans, BTRFS_I(inode),
9616 file_offset, &stack_fi, ret);
9617 if (ret)
9618 return ERR_PTR(ret);
9619 return trans;
9620 }
9621
9622 extent_info.disk_offset = start;
9623 extent_info.disk_len = len;
9624 extent_info.data_offset = 0;
9625 extent_info.data_len = len;
9626 extent_info.file_offset = file_offset;
9627 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009628 extent_info.is_new_extent = true;
9629 extent_info.qgroup_reserved = ret;
9630 extent_info.insertions = 0;
9631
9632 path = btrfs_alloc_path();
9633 if (!path)
9634 return ERR_PTR(-ENOMEM);
9635
Filipe Manana306bfec2020-09-08 11:27:23 +01009636 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009637 file_offset + len - 1, &extent_info,
9638 &trans);
9639 btrfs_free_path(path);
9640 if (ret)
9641 return ERR_PTR(ret);
9642
9643 return trans;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009644}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009645
Josef Bacik0af3d002010-06-21 14:48:16 -04009646static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9647 u64 start, u64 num_bytes, u64 min_size,
9648 loff_t actual_len, u64 *alloc_hint,
9649 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009650{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009651 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009652 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9653 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009654 struct btrfs_root *root = BTRFS_I(inode)->root;
9655 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009656 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009657 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009658 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009659 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009660 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009661 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009662 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009663 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009664
Josef Bacik0af3d002010-06-21 14:48:16 -04009665 if (trans)
9666 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009667 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009668 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009669 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009670 /*
9671 * If we are severely fragmented we could end up with really
9672 * small allocations, so if the allocator is returning small
9673 * chunks lets make its job easier by only searching for those
9674 * sized chunks.
9675 */
9676 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009677 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9678 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009679 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -04009680 break;
Josef Bacikb778cf92020-02-13 10:47:31 -05009681
9682 /*
9683 * We've reserved this space, and thus converted it from
9684 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9685 * from here on out we will only need to clear our reservation
9686 * for the remaining unreserved area, so advance our
9687 * clear_offset by our extent size.
9688 */
9689 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009690 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009691
Josef Bacik0b670dc2015-09-23 17:11:16 -04009692 last_alloc = ins.offset;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009693 trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
9694 if (IS_ERR(trans)) {
9695 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009696 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009697 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009698 break;
9699 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009700
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009701 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009702 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009703
Josef Bacik5dc562c2012-08-17 13:14:17 -04009704 em = alloc_extent_map();
9705 if (!em) {
9706 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9707 &BTRFS_I(inode)->runtime_flags);
9708 goto next;
9709 }
9710
9711 em->start = cur_offset;
9712 em->orig_start = cur_offset;
9713 em->len = ins.offset;
9714 em->block_start = ins.objectid;
9715 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009716 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009717 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009718 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9719 em->generation = trans->transid;
9720
9721 while (1) {
9722 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009723 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009724 write_unlock(&em_tree->lock);
9725 if (ret != -EEXIST)
9726 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009727 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009728 cur_offset + ins.offset - 1,
9729 0);
9730 }
9731 free_extent_map(em);
9732next:
Yan Zhengd899e052008-10-30 14:25:28 -04009733 num_bytes -= ins.offset;
9734 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009735 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009736
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009737 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009738 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009739 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009740 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009741 (actual_len > inode->i_size) &&
9742 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009743 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009744 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009745 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009746 i_size = cur_offset;
9747 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009748 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009749 }
9750
Yan Zhengd899e052008-10-30 14:25:28 -04009751 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009752
9753 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009754 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009755 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009756 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009757 break;
9758 }
Yan Zhengd899e052008-10-30 14:25:28 -04009759
Filipe Manana8fccebf2020-09-08 11:27:20 +01009760 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009761 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009762 trans = NULL;
9763 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009764 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009765 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009766 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009767 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009768 return ret;
9769}
9770
Josef Bacik0af3d002010-06-21 14:48:16 -04009771int btrfs_prealloc_file_range(struct inode *inode, int mode,
9772 u64 start, u64 num_bytes, u64 min_size,
9773 loff_t actual_len, u64 *alloc_hint)
9774{
9775 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9776 min_size, actual_len, alloc_hint,
9777 NULL);
9778}
9779
9780int btrfs_prealloc_file_range_trans(struct inode *inode,
9781 struct btrfs_trans_handle *trans, int mode,
9782 u64 start, u64 num_bytes, u64 min_size,
9783 loff_t actual_len, u64 *alloc_hint)
9784{
9785 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9786 min_size, actual_len, alloc_hint, trans);
9787}
9788
Chris Masone6dcd2d2008-07-17 12:53:50 -04009789static int btrfs_set_page_dirty(struct page *page)
9790{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009791 return __set_page_dirty_nobuffers(page);
9792}
9793
Al Viro10556cb22011-06-20 19:28:19 -04009794static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009795{
Li Zefanb83cc962010-12-20 16:04:08 +08009796 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009797 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009798
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009799 if (mask & MAY_WRITE &&
9800 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9801 if (btrfs_root_readonly(root))
9802 return -EROFS;
9803 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9804 return -EACCES;
9805 }
Al Viro2830ba72011-06-20 19:16:29 -04009806 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009807}
Chris Mason39279cc2007-06-12 06:35:45 -04009808
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009809static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9810{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009811 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009812 struct btrfs_trans_handle *trans;
9813 struct btrfs_root *root = BTRFS_I(dir)->root;
9814 struct inode *inode = NULL;
9815 u64 objectid;
9816 u64 index;
9817 int ret = 0;
9818
9819 /*
9820 * 5 units required for adding orphan entry
9821 */
9822 trans = btrfs_start_transaction(root, 5);
9823 if (IS_ERR(trans))
9824 return PTR_ERR(trans);
9825
9826 ret = btrfs_find_free_ino(root, &objectid);
9827 if (ret)
9828 goto out;
9829
9830 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009831 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009832 if (IS_ERR(inode)) {
9833 ret = PTR_ERR(inode);
9834 inode = NULL;
9835 goto out;
9836 }
9837
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009838 inode->i_fop = &btrfs_file_operations;
9839 inode->i_op = &btrfs_file_inode_operations;
9840
9841 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009842 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9843
Chris Masonb0d5d102014-09-08 13:08:51 -07009844 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9845 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009846 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009847
9848 ret = btrfs_update_inode(trans, root, inode);
9849 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009850 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009851 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009852 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009853 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009854
Filipe Manana5762b5c2014-08-01 00:10:32 +01009855 /*
9856 * We set number of links to 0 in btrfs_new_inode(), and here we set
9857 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9858 * through:
9859 *
9860 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9861 */
9862 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009863 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009864 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009865 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009866out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009867 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009868 if (ret && inode)
9869 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009870 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009871 return ret;
9872}
9873
David Sterba5cdc84b2018-07-18 20:32:52 +02009874void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009875{
David Sterba5cdc84b2018-07-18 20:32:52 +02009876 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009877 unsigned long index = start >> PAGE_SHIFT;
9878 unsigned long end_index = end >> PAGE_SHIFT;
9879 struct page *page;
9880
9881 while (index <= end_index) {
9882 page = find_get_page(inode->i_mapping, index);
9883 ASSERT(page); /* Pages should be in the extent_io_tree */
9884 set_page_writeback(page);
9885 put_page(page);
9886 index++;
9887 }
9888}
9889
Omar Sandovaled46ff32016-11-03 10:28:14 -07009890#ifdef CONFIG_SWAP
9891/*
9892 * Add an entry indicating a block group or device which is pinned by a
9893 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9894 * negative errno on failure.
9895 */
9896static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9897 bool is_block_group)
9898{
9899 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9900 struct btrfs_swapfile_pin *sp, *entry;
9901 struct rb_node **p;
9902 struct rb_node *parent = NULL;
9903
9904 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9905 if (!sp)
9906 return -ENOMEM;
9907 sp->ptr = ptr;
9908 sp->inode = inode;
9909 sp->is_block_group = is_block_group;
9910
9911 spin_lock(&fs_info->swapfile_pins_lock);
9912 p = &fs_info->swapfile_pins.rb_node;
9913 while (*p) {
9914 parent = *p;
9915 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9916 if (sp->ptr < entry->ptr ||
9917 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9918 p = &(*p)->rb_left;
9919 } else if (sp->ptr > entry->ptr ||
9920 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9921 p = &(*p)->rb_right;
9922 } else {
9923 spin_unlock(&fs_info->swapfile_pins_lock);
9924 kfree(sp);
9925 return 1;
9926 }
9927 }
9928 rb_link_node(&sp->node, parent, p);
9929 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9930 spin_unlock(&fs_info->swapfile_pins_lock);
9931 return 0;
9932}
9933
9934/* Free all of the entries pinned by this swapfile. */
9935static void btrfs_free_swapfile_pins(struct inode *inode)
9936{
9937 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9938 struct btrfs_swapfile_pin *sp;
9939 struct rb_node *node, *next;
9940
9941 spin_lock(&fs_info->swapfile_pins_lock);
9942 node = rb_first(&fs_info->swapfile_pins);
9943 while (node) {
9944 next = rb_next(node);
9945 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9946 if (sp->inode == inode) {
9947 rb_erase(&sp->node, &fs_info->swapfile_pins);
9948 if (sp->is_block_group)
9949 btrfs_put_block_group(sp->ptr);
9950 kfree(sp);
9951 }
9952 node = next;
9953 }
9954 spin_unlock(&fs_info->swapfile_pins_lock);
9955}
9956
9957struct btrfs_swap_info {
9958 u64 start;
9959 u64 block_start;
9960 u64 block_len;
9961 u64 lowest_ppage;
9962 u64 highest_ppage;
9963 unsigned long nr_pages;
9964 int nr_extents;
9965};
9966
9967static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9968 struct btrfs_swap_info *bsi)
9969{
9970 unsigned long nr_pages;
9971 u64 first_ppage, first_ppage_reported, next_ppage;
9972 int ret;
9973
9974 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9975 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9976 PAGE_SIZE) >> PAGE_SHIFT;
9977
9978 if (first_ppage >= next_ppage)
9979 return 0;
9980 nr_pages = next_ppage - first_ppage;
9981
9982 first_ppage_reported = first_ppage;
9983 if (bsi->start == 0)
9984 first_ppage_reported++;
9985 if (bsi->lowest_ppage > first_ppage_reported)
9986 bsi->lowest_ppage = first_ppage_reported;
9987 if (bsi->highest_ppage < (next_ppage - 1))
9988 bsi->highest_ppage = next_ppage - 1;
9989
9990 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
9991 if (ret < 0)
9992 return ret;
9993 bsi->nr_extents += ret;
9994 bsi->nr_pages += nr_pages;
9995 return 0;
9996}
9997
9998static void btrfs_swap_deactivate(struct file *file)
9999{
10000 struct inode *inode = file_inode(file);
10001
10002 btrfs_free_swapfile_pins(inode);
10003 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10004}
10005
10006static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10007 sector_t *span)
10008{
10009 struct inode *inode = file_inode(file);
10010 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10011 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10012 struct extent_state *cached_state = NULL;
10013 struct extent_map *em = NULL;
10014 struct btrfs_device *device = NULL;
10015 struct btrfs_swap_info bsi = {
10016 .lowest_ppage = (sector_t)-1ULL,
10017 };
10018 int ret = 0;
10019 u64 isize;
10020 u64 start;
10021
10022 /*
10023 * If the swap file was just created, make sure delalloc is done. If the
10024 * file changes again after this, the user is doing something stupid and
10025 * we don't really care.
10026 */
10027 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10028 if (ret)
10029 return ret;
10030
10031 /*
10032 * The inode is locked, so these flags won't change after we check them.
10033 */
10034 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10035 btrfs_warn(fs_info, "swapfile must not be compressed");
10036 return -EINVAL;
10037 }
10038 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10039 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10040 return -EINVAL;
10041 }
10042 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10043 btrfs_warn(fs_info, "swapfile must not be checksummed");
10044 return -EINVAL;
10045 }
10046
10047 /*
10048 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010049 * under us. The exclop protection makes sure they aren't running/won't
10050 * run concurrently while we are mapping the swap extents, and
10051 * fs_info->swapfile_pins prevents them from running while the swap
10052 * file is active and moving the extents. Note that this also prevents
10053 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010054 * really worth the trouble to allow it.
10055 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010056 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010057 btrfs_warn(fs_info,
10058 "cannot activate swapfile while exclusive operation is running");
10059 return -EBUSY;
10060 }
10061 /*
10062 * Snapshots can create extents which require COW even if NODATACOW is
10063 * set. We use this counter to prevent snapshots. We must increment it
10064 * before walking the extents because we don't want a concurrent
10065 * snapshot to run after we've already checked the extents.
10066 */
10067 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10068
10069 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10070
10071 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10072 start = 0;
10073 while (start < isize) {
10074 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010075 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010076 u64 len = isize - start;
10077
Omar Sandoval39b07b52019-12-02 17:34:23 -080010078 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010079 if (IS_ERR(em)) {
10080 ret = PTR_ERR(em);
10081 goto out;
10082 }
10083
10084 if (em->block_start == EXTENT_MAP_HOLE) {
10085 btrfs_warn(fs_info, "swapfile must not have holes");
10086 ret = -EINVAL;
10087 goto out;
10088 }
10089 if (em->block_start == EXTENT_MAP_INLINE) {
10090 /*
10091 * It's unlikely we'll ever actually find ourselves
10092 * here, as a file small enough to fit inline won't be
10093 * big enough to store more than the swap header, but in
10094 * case something changes in the future, let's catch it
10095 * here rather than later.
10096 */
10097 btrfs_warn(fs_info, "swapfile must not be inline");
10098 ret = -EINVAL;
10099 goto out;
10100 }
10101 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10102 btrfs_warn(fs_info, "swapfile must not be compressed");
10103 ret = -EINVAL;
10104 goto out;
10105 }
10106
10107 logical_block_start = em->block_start + (start - em->start);
10108 len = min(len, em->len - (start - em->start));
10109 free_extent_map(em);
10110 em = NULL;
10111
Boris Burkova84d5d42020-08-18 11:00:05 -070010112 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010113 if (ret < 0) {
10114 goto out;
10115 } else if (ret) {
10116 ret = 0;
10117 } else {
10118 btrfs_warn(fs_info,
10119 "swapfile must not be copy-on-write");
10120 ret = -EINVAL;
10121 goto out;
10122 }
10123
10124 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10125 if (IS_ERR(em)) {
10126 ret = PTR_ERR(em);
10127 goto out;
10128 }
10129
10130 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10131 btrfs_warn(fs_info,
10132 "swapfile must have single data profile");
10133 ret = -EINVAL;
10134 goto out;
10135 }
10136
10137 if (device == NULL) {
10138 device = em->map_lookup->stripes[0].dev;
10139 ret = btrfs_add_swapfile_pin(inode, device, false);
10140 if (ret == 1)
10141 ret = 0;
10142 else if (ret)
10143 goto out;
10144 } else if (device != em->map_lookup->stripes[0].dev) {
10145 btrfs_warn(fs_info, "swapfile must be on one device");
10146 ret = -EINVAL;
10147 goto out;
10148 }
10149
10150 physical_block_start = (em->map_lookup->stripes[0].physical +
10151 (logical_block_start - em->start));
10152 len = min(len, em->len - (logical_block_start - em->start));
10153 free_extent_map(em);
10154 em = NULL;
10155
10156 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10157 if (!bg) {
10158 btrfs_warn(fs_info,
10159 "could not find block group containing swapfile");
10160 ret = -EINVAL;
10161 goto out;
10162 }
10163
10164 ret = btrfs_add_swapfile_pin(inode, bg, true);
10165 if (ret) {
10166 btrfs_put_block_group(bg);
10167 if (ret == 1)
10168 ret = 0;
10169 else
10170 goto out;
10171 }
10172
10173 if (bsi.block_len &&
10174 bsi.block_start + bsi.block_len == physical_block_start) {
10175 bsi.block_len += len;
10176 } else {
10177 if (bsi.block_len) {
10178 ret = btrfs_add_swap_extent(sis, &bsi);
10179 if (ret)
10180 goto out;
10181 }
10182 bsi.start = start;
10183 bsi.block_start = physical_block_start;
10184 bsi.block_len = len;
10185 }
10186
10187 start += len;
10188 }
10189
10190 if (bsi.block_len)
10191 ret = btrfs_add_swap_extent(sis, &bsi);
10192
10193out:
10194 if (!IS_ERR_OR_NULL(em))
10195 free_extent_map(em);
10196
10197 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10198
10199 if (ret)
10200 btrfs_swap_deactivate(file);
10201
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010202 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010203
10204 if (ret)
10205 return ret;
10206
10207 if (device)
10208 sis->bdev = device->bdev;
10209 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10210 sis->max = bsi.nr_pages;
10211 sis->pages = bsi.nr_pages - 1;
10212 sis->highest_bit = bsi.nr_pages - 1;
10213 return bsi.nr_extents;
10214}
10215#else
10216static void btrfs_swap_deactivate(struct file *file)
10217{
10218}
10219
10220static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10221 sector_t *span)
10222{
10223 return -EOPNOTSUPP;
10224}
10225#endif
10226
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010227static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010228 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010229 .lookup = btrfs_lookup,
10230 .create = btrfs_create,
10231 .unlink = btrfs_unlink,
10232 .link = btrfs_link,
10233 .mkdir = btrfs_mkdir,
10234 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010235 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010236 .symlink = btrfs_symlink,
10237 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010238 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010239 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010240 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010241 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010242 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010243 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010244 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010245};
Yan, Zheng76dda932009-09-21 16:00:26 -040010246
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010247static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010248 .llseek = generic_file_llseek,
10249 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010250 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010251 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010252 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010253#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010254 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010255#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010256 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010257 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010258};
10259
David Sterba20e55062015-11-19 11:42:28 +010010260static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010261 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010262 .submit_bio_hook = btrfs_submit_bio_hook,
Nikolay Borisov9a446d62020-09-18 16:34:33 +030010263 .readpage_end_io_hook = NULL
Chris Mason07157aa2007-08-30 08:50:51 -040010264};
10265
Chris Mason35054392009-01-21 13:11:13 -050010266/*
10267 * btrfs doesn't support the bmap operation because swapfiles
10268 * use bmap to make a mapping of extents in the file. They assume
10269 * these extents won't change over the life of the file and they
10270 * use the bmap result to do IO directly to the drive.
10271 *
10272 * the btrfs bmap call would return logical addresses that aren't
10273 * suitable for IO and they also will change frequently as COW
10274 * operations happen. So, swapfile + btrfs == corruption.
10275 *
10276 * For now we're avoiding this by dropping bmap.
10277 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010278static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010279 .readpage = btrfs_readpage,
10280 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010281 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010282 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010283 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010284 .invalidatepage = btrfs_invalidatepage,
10285 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010286#ifdef CONFIG_MIGRATION
10287 .migratepage = btrfs_migratepage,
10288#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010289 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010290 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010291 .swap_activate = btrfs_swap_activate,
10292 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010293};
10294
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010295static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010296 .getattr = btrfs_getattr,
10297 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010298 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010299 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010300 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010301 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010302 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010303 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010304};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010305static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010306 .getattr = btrfs_getattr,
10307 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010308 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010309 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010310 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010311 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010312 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010313};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010314static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010315 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010316 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010317 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010318 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010319 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010320 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010321};
Yan, Zheng76dda932009-09-21 16:00:26 -040010322
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010323const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010324 .d_delete = btrfs_dentry_delete,
10325};