blob: dddff27774a1c1165112fe0c6b7be888731a58ca [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 Borisov908930f2020-09-18 16:34:37 +03002187blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2188 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002189
Chris Mason44b8bd72008-04-16 11:14:51 -04002190{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002191 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002192 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302193 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002194 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002195 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002196 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002197
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002198 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002199
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002200 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302201 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002202
Mike Christie37226b22016-06-05 14:31:52 -05002203 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002204 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002205 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002206 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002207
Chris Masond20f7042008-12-08 16:58:54 -05002208 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002209 ret = btrfs_submit_compressed_read(inode, bio,
2210 mirror_num,
2211 bio_flags);
2212 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002213 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002214 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002215 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002216 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002217 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002218 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002219 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002220 /* csum items have already been cloned */
2221 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2222 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002223 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002224 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002225 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002226 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002227 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002228 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002229 if (ret)
2230 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002231 }
2232
Chris Mason0b86a832008-03-24 15:01:56 -04002233mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002234 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002235
2236out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002237 if (ret) {
2238 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002239 bio_endio(bio);
2240 }
Stefan Behrens61891922012-11-05 18:51:52 +01002241 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002242}
Chris Mason6885f302008-02-20 16:11:05 -05002243
Chris Masond352ac62008-09-29 15:18:18 -04002244/*
2245 * given a list of ordered sums record them in the inode. This happens
2246 * at IO completion time based on sums calculated at bio submission time.
2247 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002248static int add_pending_csums(struct btrfs_trans_handle *trans,
2249 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002250{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002251 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002252 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002253
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002254 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002255 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002256 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002257 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002258 if (ret)
2259 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002260 }
2261 return 0;
2262}
2263
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002264int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002265 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002266 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002267{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002268 WARN_ON(PAGE_ALIGNED(end));
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002269 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2270 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002271}
2272
Chris Masond352ac62008-09-29 15:18:18 -04002273/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002274struct btrfs_writepage_fixup {
2275 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002276 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002277 struct btrfs_work work;
2278};
2279
Christoph Hellwigb2950862008-12-02 09:54:17 -05002280static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002281{
2282 struct btrfs_writepage_fixup *fixup;
2283 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002284 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002285 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002286 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002287 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002288 u64 page_start;
2289 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002290 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002291 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002292
2293 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2294 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002295 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002296 page_start = page_offset(page);
2297 page_end = page_offset(page) + PAGE_SIZE - 1;
2298
2299 /*
2300 * This is similar to page_mkwrite, we need to reserve the space before
2301 * we take the page lock.
2302 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002303 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2304 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002305again:
Chris Mason247e7432008-07-17 12:53:51 -04002306 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002307
Chris Mason25f3c502020-01-21 11:51:42 -05002308 /*
2309 * Before we queued this fixup, we took a reference on the page.
2310 * page->mapping may go NULL, but it shouldn't be moved to a different
2311 * address space.
2312 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002313 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2314 /*
2315 * Unfortunately this is a little tricky, either
2316 *
2317 * 1) We got here and our page had already been dealt with and
2318 * we reserved our space, thus ret == 0, so we need to just
2319 * drop our space reservation and bail. This can happen the
2320 * first time we come into the fixup worker, or could happen
2321 * while waiting for the ordered extent.
2322 * 2) Our page was already dealt with, but we happened to get an
2323 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2324 * this case we obviously don't have anything to release, but
2325 * because the page was already dealt with we don't want to
2326 * mark the page with an error, so make sure we're resetting
2327 * ret to 0. This is why we have this check _before_ the ret
2328 * check, because we do not want to have a surprise ENOSPC
2329 * when the page was already properly dealt with.
2330 */
2331 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002332 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2333 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002334 page_start, PAGE_SIZE,
2335 true);
2336 }
2337 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002338 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002339 }
Chris Mason25f3c502020-01-21 11:51:42 -05002340
2341 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002342 * We can't mess with the page state unless it is locked, so now that
2343 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002344 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002345 if (ret)
2346 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002347
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002348 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002349
2350 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002351 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002352 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002353
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002354 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002355 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002356 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2357 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002358 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002359 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002360 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002361 goto again;
2362 }
Chris Mason247e7432008-07-17 12:53:51 -04002363
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002364 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002365 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002366 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002367 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002368
Chris Mason25f3c502020-01-21 11:51:42 -05002369 /*
2370 * Everything went as planned, we're now the owner of a dirty page with
2371 * delayed allocation bits set and space reserved for our COW
2372 * destination.
2373 *
2374 * The page was dirty when we started, nothing should have cleaned it.
2375 */
2376 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002377 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002378out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002379 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002380 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002381 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2382 PAGE_SIZE, true);
2383 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002384 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002385out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002386 if (ret) {
2387 /*
2388 * We hit ENOSPC or other errors. Update the mapping and page
2389 * to reflect the errors and clean the page.
2390 */
2391 mapping_set_error(page->mapping, ret);
2392 end_extent_writepage(page, ret, page_start, page_end);
2393 clear_page_dirty_for_io(page);
2394 SetPageError(page);
2395 }
2396 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002397 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002398 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002399 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002400 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002401 /*
2402 * As a precaution, do a delayed iput in case it would be the last iput
2403 * that could need flushing space. Recursing back to fixup worker would
2404 * deadlock.
2405 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002406 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002407}
2408
2409/*
2410 * There are a few paths in the higher layers of the kernel that directly
2411 * set the page dirty bit without asking the filesystem if it is a
2412 * good idea. This causes problems because we want to make sure COW
2413 * properly happens and the data=ordered rules are followed.
2414 *
Chris Masonc8b97812008-10-29 14:49:59 -04002415 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002416 * hasn't been properly setup for IO. We kick off an async process
2417 * to fix it up. The async helper will wait for ordered extents, set
2418 * the delalloc bit and make it safe to write the page.
2419 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002420int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002421{
2422 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002423 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002424 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002425
Chris Mason8b62b722009-09-02 16:53:46 -04002426 /* this page is properly in the ordered list */
2427 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002428 return 0;
2429
Chris Mason25f3c502020-01-21 11:51:42 -05002430 /*
2431 * PageChecked is set below when we create a fixup worker for this page,
2432 * don't try to create another one if we're already PageChecked()
2433 *
2434 * The extent_io writepage code will redirty the page if we send back
2435 * EAGAIN.
2436 */
Chris Mason247e7432008-07-17 12:53:51 -04002437 if (PageChecked(page))
2438 return -EAGAIN;
2439
2440 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2441 if (!fixup)
2442 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002443
Josef Bacikf4b13632020-01-21 14:34:52 -05002444 /*
2445 * We are already holding a reference to this inode from
2446 * write_cache_pages. We need to hold it because the space reservation
2447 * takes place outside of the page lock, and we can't trust
2448 * page->mapping outside of the page lock.
2449 */
2450 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002451 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002452 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002453 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002454 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002455 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002456 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002457
2458 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002459}
2460
Yan Zhengd899e052008-10-30 14:25:28 -04002461static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002462 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002463 struct btrfs_file_extent_item *stack_fi,
2464 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002465{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002466 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002467 struct btrfs_path *path;
2468 struct extent_buffer *leaf;
2469 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002470 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2471 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2472 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2473 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002474 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002475 int ret;
2476
2477 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002478 if (!path)
2479 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002480
Chris Masona1ed8352009-09-11 12:27:37 -04002481 /*
2482 * we may be replacing one extent in the tree with another.
2483 * The new extent is pinned in the extent map, and we don't want
2484 * to drop it from the cache until it is completely in the btree.
2485 *
2486 * So, tell btrfs_drop_extents to leave this extent in the cache.
2487 * the caller is expected to unpin it and allow it to be merged
2488 * with the others.
2489 */
Nikolay Borisovc553f942020-06-03 08:55:19 +03002490 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002491 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002492 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002493 if (ret)
2494 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002495
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002496 if (!extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002497 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002498 ins.offset = file_pos;
2499 ins.type = BTRFS_EXTENT_DATA_KEY;
2500
2501 path->leave_spinning = 1;
2502 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002503 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002504 if (ret)
2505 goto out;
2506 }
Yan Zhengd899e052008-10-30 14:25:28 -04002507 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002508 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2509 write_extent_buffer(leaf, stack_fi,
2510 btrfs_item_ptr_offset(leaf, path->slots[0]),
2511 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002512
Yan Zhengd899e052008-10-30 14:25:28 -04002513 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002514 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002515
Nikolay Borisovc553f942020-06-03 08:55:19 +03002516 inode_add_bytes(&inode->vfs_inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002517
2518 ins.objectid = disk_bytenr;
2519 ins.offset = disk_num_bytes;
2520 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002521
Nikolay Borisovc553f942020-06-03 08:55:19 +03002522 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002523 if (ret)
2524 goto out;
2525
Nikolay Borisovc553f942020-06-03 08:55:19 +03002526 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002527 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002528out:
Yan Zhengd899e052008-10-30 14:25:28 -04002529 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002530
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002531 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002532}
2533
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002534static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002535 u64 start, u64 len)
2536{
David Sterba32da53862019-10-29 19:20:18 +01002537 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002538
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002539 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002540 ASSERT(cache);
2541
2542 spin_lock(&cache->lock);
2543 cache->delalloc_bytes -= len;
2544 spin_unlock(&cache->lock);
2545
2546 btrfs_put_block_group(cache);
2547}
2548
Qu Wenruo203f44c52020-06-10 09:04:40 +08002549static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002550 struct btrfs_ordered_extent *oe)
2551{
2552 struct btrfs_file_extent_item stack_fi;
2553 u64 logical_len;
2554
2555 memset(&stack_fi, 0, sizeof(stack_fi));
2556 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2557 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2558 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2559 oe->disk_num_bytes);
2560 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2561 logical_len = oe->truncated_len;
2562 else
2563 logical_len = oe->num_bytes;
2564 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2565 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2566 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2567 /* Encryption and other encoding is reserved and all 0 */
2568
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002569 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2570 oe->file_offset, &stack_fi,
2571 oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002572}
2573
2574/*
2575 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002576 * an ordered extent if the range of bytes in the file it covers are
2577 * fully written.
2578 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002579static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002580{
Josef Bacik5fd02042012-05-02 14:00:54 -04002581 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002582 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002583 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002584 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002585 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002586 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002587 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002588 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002589 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002590 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002591 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002592 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002593 bool range_locked = false;
2594 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002595 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002596 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002597
Omar Sandovalbffe6332019-12-02 17:34:19 -08002598 start = ordered_extent->file_offset;
2599 end = start + ordered_extent->num_bytes - 1;
2600
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002601 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2602 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2603 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2604 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002605
Nikolay Borisov8d510122019-10-08 20:43:06 +03002606 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002607
Josef Bacik5fd02042012-05-02 14:00:54 -04002608 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2609 ret = -EIO;
2610 goto out;
2611 }
2612
Omar Sandovalbffe6332019-12-02 17:34:19 -08002613 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002614
Josef Bacik77cef2e2013-08-29 13:57:21 -04002615 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2616 truncated = true;
2617 logical_len = ordered_extent->truncated_len;
2618 /* Truncated the entire extent, don't bother adding */
2619 if (!logical_len)
2620 goto out;
2621 }
2622
Yan, Zhengc2167752009-11-12 09:34:21 +00002623 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002624 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002625
Josef Bacikd923afe2020-01-17 09:02:23 -05002626 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002627 if (freespace_inode)
2628 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002629 else
2630 trans = btrfs_join_transaction(root);
2631 if (IS_ERR(trans)) {
2632 ret = PTR_ERR(trans);
2633 trans = NULL;
2634 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002635 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002636 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002637 ret = btrfs_update_inode_fallback(trans, root, inode);
2638 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002639 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002640 goto out;
2641 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002642
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002643 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002644 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645
Nikolay Borisov8d510122019-10-08 20:43:06 +03002646 if (freespace_inode)
2647 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002648 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002649 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002650 if (IS_ERR(trans)) {
2651 ret = PTR_ERR(trans);
2652 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002653 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002654 }
Chris Masona79b7d42014-05-22 16:18:52 -07002655
Josef Bacik69fe2d72017-10-19 14:15:57 -04002656 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002657
Chris Masonc8b97812008-10-29 14:49:59 -04002658 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002659 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002660 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002661 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002662 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002663 ordered_extent->file_offset,
2664 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002665 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002666 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002667 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002668 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002669 if (!ret) {
2670 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002671 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002672 ordered_extent->disk_bytenr,
2673 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002674 }
Yan Zhengd899e052008-10-30 14:25:28 -04002675 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002676 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002677 ordered_extent->file_offset,
2678 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002679 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002680 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002681 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002682 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002683
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002684 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002685 if (ret) {
2686 btrfs_abort_transaction(trans, ret);
2687 goto out;
2688 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002689
Josef Bacikd923afe2020-01-17 09:02:23 -05002690 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002691 ret = btrfs_update_inode_fallback(trans, root, inode);
2692 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002693 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002694 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002695 }
2696 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002697out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002698 clear_bits = EXTENT_DEFRAG;
2699 if (range_locked)
2700 clear_bits |= EXTENT_LOCKED;
2701 if (clear_new_delalloc_bytes)
2702 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002703 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2704 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002705 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002706
Miao Xiea698d0752012-09-20 01:51:59 -06002707 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002708 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002709
Josef Bacik77cef2e2013-08-29 13:57:21 -04002710 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002711 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002712
2713 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002714 unwritten_start += logical_len;
2715 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002716
2717 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002718 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002719
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002720 /*
2721 * If the ordered extent had an IOERR or something else went
2722 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002723 * back to the allocator. We only free the extent in the
2724 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002725 *
2726 * If we made it past insert_reserved_file_extent before we
2727 * errored out then we don't need to do this as the accounting
2728 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002729 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002730 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002731 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002732 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002733 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2734 /*
2735 * Discard the range before returning it back to the
2736 * free space pool
2737 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002738 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002739 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002740 ordered_extent->disk_bytenr,
2741 ordered_extent->disk_num_bytes,
2742 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002743 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002744 ordered_extent->disk_bytenr,
2745 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002746 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002747 }
2748
Josef Bacik5fd02042012-05-02 14:00:54 -04002749 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002750 * This needs to be done to make sure anybody waiting knows we are done
2751 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002752 */
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03002753 btrfs_remove_ordered_extent(BTRFS_I(inode), ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04002754
Chris Masone6dcd2d2008-07-17 12:53:50 -04002755 /* once for us */
2756 btrfs_put_ordered_extent(ordered_extent);
2757 /* once for the tree */
2758 btrfs_put_ordered_extent(ordered_extent);
2759
Josef Bacik5fd02042012-05-02 14:00:54 -04002760 return ret;
2761}
2762
2763static void finish_ordered_fn(struct btrfs_work *work)
2764{
2765 struct btrfs_ordered_extent *ordered_extent;
2766 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2767 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002768}
2769
Nikolay Borisovc6297322018-11-08 10:18:08 +02002770void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2771 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002772{
Nikolay Borisov3347c482020-08-31 14:42:44 +03002773 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2774 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04002775 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002776 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002777
liubo1abe9b82011-03-24 11:18:59 +00002778 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2779
Chris Mason8b62b722009-09-02 16:53:46 -04002780 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03002781 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2782 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002783 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002784
Nikolay Borisov3347c482020-08-31 14:42:44 +03002785 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002786 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002787 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002788 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002789
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002790 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002791 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002792}
2793
Omar Sandoval47df7762020-04-16 14:46:17 -07002794static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2795 int icsum, struct page *page, int pgoff, u64 start,
2796 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002797{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002798 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2799 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002800 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002801 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2802 u8 *csum_expected;
2803 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002804
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002805 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002806
2807 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002808 shash->tfm = fs_info->csum_shash;
2809
Eric Biggersfd080012020-04-30 23:51:59 -07002810 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002811
2812 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002813 goto zeroit;
2814
2815 kunmap_atomic(kaddr);
2816 return 0;
2817zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002818 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2819 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03002820 if (io_bio->device)
2821 btrfs_dev_stat_inc_and_print(io_bio->device,
2822 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08002823 memset(kaddr + pgoff, 1, len);
2824 flush_dcache_page(page);
2825 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002826 return -EIO;
2827}
2828
Chris Masond352ac62008-09-29 15:18:18 -04002829/*
Chris Masond352ac62008-09-29 15:18:18 -04002830 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002831 * if there's a match, we allow the bio to finish. If not, the code in
2832 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002833 */
Nikolay Borisov9a446d62020-09-18 16:34:33 +03002834int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset,
2835 struct page *page, u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002836{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002837 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002838 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002839 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002840 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002841
Chris Masond20f7042008-12-08 16:58:54 -05002842 if (PageChecked(page)) {
2843 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002844 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002845 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002846
2847 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002848 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002849
Yan Zheng17d217f2008-12-12 10:03:38 -05002850 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002851 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002852 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002853 return 0;
2854 }
2855
Miao Xiefacc8a222013-07-25 19:22:34 +08002856 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002857 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2858 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002859}
Chris Masonb888db22007-08-27 16:49:44 -04002860
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002861/*
2862 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2863 *
2864 * @inode: The inode we want to perform iput on
2865 *
2866 * This function uses the generic vfs_inode::i_count to track whether we should
2867 * just decrement it (in case it's > 1) or if this is the last iput then link
2868 * the inode to the delayed iput machinery. Delayed iputs are processed at
2869 * transaction commit time/superblock commit/cleaner kthread.
2870 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002871void btrfs_add_delayed_iput(struct inode *inode)
2872{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002873 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002874 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002875
2876 if (atomic_add_unless(&inode->i_count, -1, 1))
2877 return;
2878
Josef Bacik034f7842018-12-03 11:06:52 -05002879 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002880 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002881 ASSERT(list_empty(&binode->delayed_iput));
2882 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002883 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002884 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2885 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002886}
2887
Josef Bacik63611e72019-06-18 10:59:18 -04002888static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2889 struct btrfs_inode *inode)
2890{
2891 list_del_init(&inode->delayed_iput);
2892 spin_unlock(&fs_info->delayed_iput_lock);
2893 iput(&inode->vfs_inode);
2894 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2895 wake_up(&fs_info->delayed_iputs_wait);
2896 spin_lock(&fs_info->delayed_iput_lock);
2897}
2898
2899static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2900 struct btrfs_inode *inode)
2901{
2902 if (!list_empty(&inode->delayed_iput)) {
2903 spin_lock(&fs_info->delayed_iput_lock);
2904 if (!list_empty(&inode->delayed_iput))
2905 run_delayed_iput_locked(fs_info, inode);
2906 spin_unlock(&fs_info->delayed_iput_lock);
2907 }
2908}
2909
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002910void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002911{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002912
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002913 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002914 while (!list_empty(&fs_info->delayed_iputs)) {
2915 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002916
David Sterba8089fe62015-11-19 14:15:51 +01002917 inode = list_first_entry(&fs_info->delayed_iputs,
2918 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002919 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002920 }
David Sterba8089fe62015-11-19 14:15:51 +01002921 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002922}
2923
Josef Bacik034f7842018-12-03 11:06:52 -05002924/**
2925 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2926 * @fs_info - the fs_info for this fs
2927 * @return - EINTR if we were killed, 0 if nothing's pending
2928 *
2929 * This will wait on any delayed iputs that are currently running with KILLABLE
2930 * set. Once they are all done running we will return, unless we are killed in
2931 * which case we return EINTR. This helps in user operations like fallocate etc
2932 * that might get blocked on the iputs.
2933 */
2934int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2935{
2936 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2937 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2938 if (ret)
2939 return -EINTR;
2940 return 0;
2941}
2942
Yan, Zhengd68fc572010-05-16 10:49:58 -04002943/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002944 * This creates an orphan entry for the given inode in case something goes wrong
2945 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002946 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002947int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002948 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002949{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002950 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002951
Omar Sandoval27919062018-05-11 13:13:37 -07002952 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2953 if (ret && ret != -EEXIST) {
2954 btrfs_abort_transaction(trans, ret);
2955 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002956 }
2957
Yan, Zhengd68fc572010-05-16 10:49:58 -04002958 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002959}
2960
2961/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002962 * We have done the delete so we can go ahead and remove the orphan item for
2963 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002964 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002965static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002966 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002967{
Omar Sandoval27919062018-05-11 13:13:37 -07002968 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002969}
2970
2971/*
2972 * this cleans up any orphans that may be left on the list from the last use
2973 * of this root.
2974 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002975int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002976{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002977 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002978 struct btrfs_path *path;
2979 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002980 struct btrfs_key key, found_key;
2981 struct btrfs_trans_handle *trans;
2982 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002983 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002984 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002985
Yan, Zhengd68fc572010-05-16 10:49:58 -04002986 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002987 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002988
2989 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002990 if (!path) {
2991 ret = -ENOMEM;
2992 goto out;
2993 }
David Sterbae4058b52015-11-27 16:31:35 +01002994 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002995
2996 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02002997 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04002998 key.offset = (u64)-1;
2999
Josef Bacik7b128762008-07-24 12:17:14 -04003000 while (1) {
3001 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003002 if (ret < 0)
3003 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003004
3005 /*
3006 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003007 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003008 * find the key and see if we have stuff that matches
3009 */
3010 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003011 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003012 if (path->slots[0] == 0)
3013 break;
3014 path->slots[0]--;
3015 }
3016
3017 /* pull out the item */
3018 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003019 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3020
3021 /* make sure the item matches what we want */
3022 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3023 break;
David Sterba962a2982014-06-04 18:41:45 +02003024 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003025 break;
3026
3027 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003028 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003029
3030 /*
3031 * this is where we are basically btrfs_lookup, without the
3032 * crossing root thing. we store the inode number in the
3033 * offset of the orphan item.
3034 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003035
3036 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003037 btrfs_err(fs_info,
3038 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003039 ret = -EINVAL;
3040 goto out;
3041 }
3042
3043 last_objectid = found_key.offset;
3044
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003045 found_key.objectid = found_key.offset;
3046 found_key.type = BTRFS_INODE_ITEM_KEY;
3047 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003048 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303049 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003050 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003051 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003052
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003053 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003054 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003055 int is_dead_root = 0;
3056
3057 /*
3058 * this is an orphan in the tree root. Currently these
3059 * could come from 2 sources:
3060 * a) a snapshot deletion in progress
3061 * b) a free space cache inode
3062 * We need to distinguish those two, as the snapshot
3063 * orphan must not get deleted.
3064 * find_dead_roots already ran before us, so if this
3065 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003066 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003067 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003068
3069 spin_lock(&fs_info->fs_roots_radix_lock);
3070 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3071 (unsigned long)found_key.objectid);
3072 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3073 is_dead_root = 1;
3074 spin_unlock(&fs_info->fs_roots_radix_lock);
3075
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003076 if (is_dead_root) {
3077 /* prevent this orphan from being found again */
3078 key.offset = found_key.objectid - 1;
3079 continue;
3080 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003081
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003082 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003083
Josef Bacika8c9e572011-09-21 16:55:59 -04003084 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003085 * If we have an inode with links, there are a couple of
3086 * possibilities. Old kernels (before v3.12) used to create an
3087 * orphan item for truncate indicating that there were possibly
3088 * extent items past i_size that needed to be deleted. In v3.12,
3089 * truncate was changed to update i_size in sync with the extent
3090 * items, but the (useless) orphan item was still created. Since
3091 * v4.18, we don't create the orphan item for truncate at all.
3092 *
3093 * So, this item could mean that we need to do a truncate, but
3094 * only if this filesystem was last used on a pre-v3.12 kernel
3095 * and was not cleanly unmounted. The odds of that are quite
3096 * slim, and it's a pain to do the truncate now, so just delete
3097 * the orphan item.
3098 *
3099 * It's also possible that this orphan item was supposed to be
3100 * deleted but wasn't. The inode number may have been reused,
3101 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003102 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003103 if (ret == -ENOENT || inode->i_nlink) {
3104 if (!ret)
3105 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003106 trans = btrfs_start_transaction(root, 1);
3107 if (IS_ERR(trans)) {
3108 ret = PTR_ERR(trans);
3109 goto out;
3110 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003111 btrfs_debug(fs_info, "auto deleting %Lu",
3112 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003113 ret = btrfs_del_orphan_item(trans, root,
3114 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003115 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003116 if (ret)
3117 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003118 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003119 }
Josef Bacik7b128762008-07-24 12:17:14 -04003120
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003121 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003122
3123 /* this will do delete_inode and everything for us */
3124 iput(inode);
3125 }
Miao Xie3254c872011-11-10 20:45:05 -05003126 /* release the path since we're done with it */
3127 btrfs_release_path(path);
3128
Yan, Zhengd68fc572010-05-16 10:49:58 -04003129 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3130
Omar Sandovala575cee2018-05-11 13:13:38 -07003131 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003132 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003133 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003134 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003135 }
Josef Bacik7b128762008-07-24 12:17:14 -04003136
3137 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003138 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003139
3140out:
3141 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003142 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003143 btrfs_free_path(path);
3144 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003145}
3146
Chris Masond352ac62008-09-29 15:18:18 -04003147/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003148 * very simple check to peek ahead in the leaf looking for xattrs. If we
3149 * don't find any xattrs, we know there can't be any acls.
3150 *
3151 * slot is the slot the inode is in, objectid is the objectid of the inode
3152 */
3153static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003154 int slot, u64 objectid,
3155 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003156{
3157 u32 nritems = btrfs_header_nritems(leaf);
3158 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003159 static u64 xattr_access = 0;
3160 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003161 int scanned = 0;
3162
Josef Bacikf23b5a52013-06-19 10:16:26 -04003163 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003164 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3165 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3166 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3167 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003168 }
3169
Chris Mason46a53cc2009-04-27 11:47:50 -04003170 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003171 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003172 while (slot < nritems) {
3173 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3174
3175 /* we found a different objectid, there must not be acls */
3176 if (found_key.objectid != objectid)
3177 return 0;
3178
3179 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003180 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003181 if (*first_xattr_slot == -1)
3182 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003183 if (found_key.offset == xattr_access ||
3184 found_key.offset == xattr_default)
3185 return 1;
3186 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003187
3188 /*
3189 * we found a key greater than an xattr key, there can't
3190 * be any acls later on
3191 */
3192 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3193 return 0;
3194
3195 slot++;
3196 scanned++;
3197
3198 /*
3199 * it goes inode, inode backrefs, xattrs, extents,
3200 * so if there are a ton of hard links to an inode there can
3201 * be a lot of backrefs. Don't waste time searching too hard,
3202 * this is just an optimization
3203 */
3204 if (scanned >= 8)
3205 break;
3206 }
3207 /* we hit the end of the leaf before we found an xattr or
3208 * something larger than an xattr. We have to assume the inode
3209 * has acls
3210 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003211 if (*first_xattr_slot == -1)
3212 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003213 return 1;
3214}
3215
3216/*
Chris Masond352ac62008-09-29 15:18:18 -04003217 * read an inode from the btree into the in-memory inode
3218 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003219static int btrfs_read_locked_inode(struct inode *inode,
3220 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003221{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003222 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003223 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003224 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003225 struct btrfs_inode_item *inode_item;
3226 struct btrfs_root *root = BTRFS_I(inode)->root;
3227 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003228 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003229 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003230 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003231 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003232 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003233 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003234
3235 ret = btrfs_fill_inode(inode, &rdev);
3236 if (!ret)
3237 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003238
Filipe Manana4222ea72018-10-24 10:13:03 +01003239 if (!path) {
3240 path = btrfs_alloc_path();
3241 if (!path)
3242 return -ENOMEM;
3243 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003244
Chris Mason39279cc2007-06-12 06:35:45 -04003245 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003246
Chris Mason39279cc2007-06-12 06:35:45 -04003247 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003248 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003249 if (path != in_path)
3250 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003251 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003252 }
Chris Mason39279cc2007-06-12 06:35:45 -04003253
Chris Mason5f39d392007-10-15 16:14:19 -04003254 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003255
3256 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003257 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003258
Chris Mason5f39d392007-10-15 16:14:19 -04003259 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3260 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003261 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003262 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003263 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3264 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003265 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003266 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3267 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003268
David Sterbaa937b972014-12-12 17:39:12 +01003269 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3270 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003271
David Sterbaa937b972014-12-12 17:39:12 +01003272 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3273 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003274
David Sterbaa937b972014-12-12 17:39:12 +01003275 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3276 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003277
chandan r9cc97d62012-07-04 12:48:07 +05303278 BTRFS_I(inode)->i_otime.tv_sec =
3279 btrfs_timespec_sec(leaf, &inode_item->otime);
3280 BTRFS_I(inode)->i_otime.tv_nsec =
3281 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003282
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003283 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003284 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003285 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3286
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003287 inode_set_iversion_queried(inode,
3288 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003289 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003290 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003291 rdev = btrfs_inode_rdev(leaf, inode_item);
3292
Josef Bacikaec74772008-07-24 12:12:38 -04003293 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003294 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003295
3296cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003297 /*
3298 * If we were modified in the current generation and evicted from memory
3299 * and then re-read we need to do a full sync since we don't have any
3300 * idea about which extents were modified before we were evicted from
3301 * cache.
3302 *
3303 * This is required for both inode re-read from disk and delayed inode
3304 * in delayed_nodes_tree.
3305 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003306 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003307 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3308 &BTRFS_I(inode)->runtime_flags);
3309
Filipe Mananabde6c242015-07-24 00:00:19 +01003310 /*
3311 * We don't persist the id of the transaction where an unlink operation
3312 * against the inode was last made. So here we assume the inode might
3313 * have been evicted, and therefore the exact value of last_unlink_trans
3314 * lost, and set it to last_trans to avoid metadata inconsistencies
3315 * between the inode and its parent if the inode is fsync'ed and the log
3316 * replayed. For example, in the scenario:
3317 *
3318 * touch mydir/foo
3319 * ln mydir/foo mydir/bar
3320 * sync
3321 * unlink mydir/bar
3322 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3323 * xfs_io -c fsync mydir/foo
3324 * <power failure>
3325 * mount fs, triggers fsync log replay
3326 *
3327 * We must make sure that when we fsync our inode foo we also log its
3328 * parent inode, otherwise after log replay the parent still has the
3329 * dentry with the "bar" name but our inode foo has a link count of 1
3330 * and doesn't have an inode ref with the name "bar" anymore.
3331 *
3332 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003333 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003334 * transaction commits on fsync if our inode is a directory, or if our
3335 * inode is not a directory, logging its parent unnecessarily.
3336 */
3337 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3338
Filipe Manana3ebac172020-07-15 12:30:43 +01003339 /*
3340 * Same logic as for last_unlink_trans. We don't persist the generation
3341 * of the last transaction where this inode was used for a reflink
3342 * operation, so after eviction and reloading the inode we must be
3343 * pessimistic and assume the last transaction that modified the inode.
3344 */
3345 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3346
Miao Xie67de1172013-12-26 13:07:06 +08003347 path->slots[0]++;
3348 if (inode->i_nlink != 1 ||
3349 path->slots[0] >= btrfs_header_nritems(leaf))
3350 goto cache_acl;
3351
3352 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003353 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003354 goto cache_acl;
3355
3356 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3357 if (location.type == BTRFS_INODE_REF_KEY) {
3358 struct btrfs_inode_ref *ref;
3359
3360 ref = (struct btrfs_inode_ref *)ptr;
3361 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3362 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3363 struct btrfs_inode_extref *extref;
3364
3365 extref = (struct btrfs_inode_extref *)ptr;
3366 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3367 extref);
3368 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003369cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003370 /*
3371 * try to precache a NULL acl entry for files that don't have
3372 * any xattrs or acls
3373 */
Li Zefan33345d012011-04-20 10:31:50 +08003374 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003375 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003376 if (first_xattr_slot != -1) {
3377 path->slots[0] = first_xattr_slot;
3378 ret = btrfs_load_inode_props(inode, path);
3379 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003380 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003381 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003382 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003383 root->root_key.objectid, ret);
3384 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003385 if (path != in_path)
3386 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003387
Al Viro72c04902009-06-24 16:58:48 -04003388 if (!maybe_acls)
3389 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003390
Chris Mason39279cc2007-06-12 06:35:45 -04003391 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003392 case S_IFREG:
3393 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003394 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003395 inode->i_fop = &btrfs_file_operations;
3396 inode->i_op = &btrfs_file_inode_operations;
3397 break;
3398 case S_IFDIR:
3399 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003400 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003401 break;
3402 case S_IFLNK:
3403 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003404 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003405 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003406 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003407 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003408 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003409 init_special_inode(inode, inode->i_mode, rdev);
3410 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003411 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003412
David Sterba7b6a2212018-03-26 18:40:21 +02003413 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003414 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003415}
3416
Chris Masond352ac62008-09-29 15:18:18 -04003417/*
3418 * given a leaf and an inode, copy the inode fields into the leaf
3419 */
Chris Masone02119d2008-09-05 16:13:11 -04003420static void fill_inode_item(struct btrfs_trans_handle *trans,
3421 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003422 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003423 struct inode *inode)
3424{
Liu Bo51fab692012-12-27 09:01:21 +00003425 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003426
David Sterbac82f8232019-08-09 17:48:21 +02003427 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003428
David Sterbacc4c13d2020-04-29 02:15:56 +02003429 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3430 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3431 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3432 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3433 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003434
David Sterbacc4c13d2020-04-29 02:15:56 +02003435 btrfs_set_token_timespec_sec(&token, &item->atime,
3436 inode->i_atime.tv_sec);
3437 btrfs_set_token_timespec_nsec(&token, &item->atime,
3438 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003439
David Sterbacc4c13d2020-04-29 02:15:56 +02003440 btrfs_set_token_timespec_sec(&token, &item->mtime,
3441 inode->i_mtime.tv_sec);
3442 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3443 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003444
David Sterbacc4c13d2020-04-29 02:15:56 +02003445 btrfs_set_token_timespec_sec(&token, &item->ctime,
3446 inode->i_ctime.tv_sec);
3447 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3448 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003449
David Sterbacc4c13d2020-04-29 02:15:56 +02003450 btrfs_set_token_timespec_sec(&token, &item->otime,
3451 BTRFS_I(inode)->i_otime.tv_sec);
3452 btrfs_set_token_timespec_nsec(&token, &item->otime,
3453 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303454
David Sterbacc4c13d2020-04-29 02:15:56 +02003455 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3456 btrfs_set_token_inode_generation(&token, item,
3457 BTRFS_I(inode)->generation);
3458 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3459 btrfs_set_token_inode_transid(&token, item, trans->transid);
3460 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3461 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3462 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003463}
3464
Chris Masond352ac62008-09-29 15:18:18 -04003465/*
3466 * copy everything in the in-memory inode into the btree.
3467 */
Chris Mason21151332011-11-10 20:39:08 -05003468static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003469 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003470{
3471 struct btrfs_inode_item *inode_item;
3472 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003473 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003474 int ret;
3475
3476 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003477 if (!path)
3478 return -ENOMEM;
3479
Chris Masonb9473432009-03-13 11:00:37 -04003480 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003481 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3482 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003483 if (ret) {
3484 if (ret > 0)
3485 ret = -ENOENT;
3486 goto failed;
3487 }
3488
Chris Mason5f39d392007-10-15 16:14:19 -04003489 leaf = path->nodes[0];
3490 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003491 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003492
Chris Masone02119d2008-09-05 16:13:11 -04003493 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003494 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovd9094412020-06-05 10:41:13 +03003495 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04003496 ret = 0;
3497failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003498 btrfs_free_path(path);
3499 return ret;
3500}
3501
Chris Masond352ac62008-09-29 15:18:18 -04003502/*
Chris Mason21151332011-11-10 20:39:08 -05003503 * copy everything in the in-memory inode into the btree.
3504 */
3505noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3506 struct btrfs_root *root, struct inode *inode)
3507{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003508 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003509 int ret;
3510
3511 /*
3512 * If the inode is a free space inode, we can deadlock during commit
3513 * if we put it into the delayed code.
3514 *
3515 * The data relocation inode should also be directly updated
3516 * without delay
3517 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003518 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003519 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003520 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003521 btrfs_update_root_times(trans, root);
3522
Chris Mason21151332011-11-10 20:39:08 -05003523 ret = btrfs_delayed_update_inode(trans, root, inode);
3524 if (!ret)
Nikolay Borisovd9094412020-06-05 10:41:13 +03003525 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason21151332011-11-10 20:39:08 -05003526 return ret;
3527 }
3528
3529 return btrfs_update_inode_item(trans, root, inode);
3530}
3531
Josef Bacikbe6aef62012-10-22 15:43:12 -04003532noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3533 struct btrfs_root *root,
3534 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003535{
3536 int ret;
3537
3538 ret = btrfs_update_inode(trans, root, inode);
3539 if (ret == -ENOSPC)
3540 return btrfs_update_inode_item(trans, root, inode);
3541 return ret;
3542}
3543
3544/*
Chris Masond352ac62008-09-29 15:18:18 -04003545 * unlink helper that gets used here in inode.c and in the tree logging
3546 * recovery code. It remove a link in a directory with a given name, and
3547 * also drops the back refs in the inode to the directory
3548 */
Al Viro92986792011-03-04 17:14:37 +00003549static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3550 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003551 struct btrfs_inode *dir,
3552 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003553 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003554{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003555 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003556 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003557 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003558 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003559 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003560 u64 ino = btrfs_ino(inode);
3561 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003562
3563 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003564 if (!path) {
3565 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003566 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003567 }
3568
Chris Masonb9473432009-03-13 11:00:37 -04003569 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003570 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003571 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003572 if (IS_ERR_OR_NULL(di)) {
3573 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003574 goto err;
3575 }
Chris Mason39279cc2007-06-12 06:35:45 -04003576 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003577 if (ret)
3578 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003579 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003580
Miao Xie67de1172013-12-26 13:07:06 +08003581 /*
3582 * If we don't have dir index, we have to get it by looking up
3583 * the inode ref, since we get the inode ref, remove it directly,
3584 * it is unnecessary to do delayed deletion.
3585 *
3586 * But if we have dir index, needn't search inode ref to get it.
3587 * Since the inode ref is close to the inode item, it is better
3588 * that we delay to delete it, and just do this deletion when
3589 * we update the inode item.
3590 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003591 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003592 ret = btrfs_delayed_delete_inode_ref(inode);
3593 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003594 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003595 goto skip_backref;
3596 }
3597 }
3598
Li Zefan33345d012011-04-20 10:31:50 +08003599 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3600 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003601 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003602 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003603 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003604 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003605 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003606 goto err;
3607 }
Miao Xie67de1172013-12-26 13:07:06 +08003608skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003609 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003610 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003611 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003612 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003613 }
Chris Mason39279cc2007-06-12 06:35:45 -04003614
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003615 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3616 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003617 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003618 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003619 goto err;
3620 }
Chris Masone02119d2008-09-05 16:13:11 -04003621
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003622 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3623 index);
Chris Mason6418c962010-10-30 07:34:24 -04003624 if (ret == -ENOENT)
3625 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003626 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003627 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003628
3629 /*
3630 * If we have a pending delayed iput we could end up with the final iput
3631 * being run in btrfs-cleaner context. If we have enough of these built
3632 * up we can end up burning a lot of time in btrfs-cleaner without any
3633 * way to throttle the unlinks. Since we're currently holding a ref on
3634 * the inode we can run the delayed iput here without any issues as the
3635 * final iput won't be done until after we drop the ref we're currently
3636 * holding.
3637 */
3638 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003639err:
3640 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003641 if (ret)
3642 goto out;
3643
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003644 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003645 inode_inc_iversion(&inode->vfs_inode);
3646 inode_inc_iversion(&dir->vfs_inode);
3647 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3648 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3649 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003650out:
Chris Mason39279cc2007-06-12 06:35:45 -04003651 return ret;
3652}
3653
Al Viro92986792011-03-04 17:14:37 +00003654int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3655 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003656 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003657 const char *name, int name_len)
3658{
3659 int ret;
3660 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3661 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003662 drop_nlink(&inode->vfs_inode);
3663 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003664 }
3665 return ret;
3666}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003667
3668/*
3669 * helper to start transaction for unlink and rmdir.
3670 *
Josef Bacikd52be812013-05-29 14:54:47 -04003671 * unlink and rmdir are special in btrfs, they do not always free space, so
3672 * if we cannot make our reservations the normal way try and see if there is
3673 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3674 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003675 */
Josef Bacikd52be812013-05-29 14:54:47 -04003676static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003677{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003678 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003679
Josef Bacike70bea52011-10-11 14:18:24 -04003680 /*
3681 * 1 for the possible orphan item
3682 * 1 for the dir item
3683 * 1 for the dir index
3684 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003685 * 1 for the inode
3686 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003687 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003688}
3689
Chris Mason39279cc2007-06-12 06:35:45 -04003690static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3691{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003692 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003693 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003694 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003695 int ret;
3696
Josef Bacikd52be812013-05-29 14:54:47 -04003697 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003698 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003699 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003700
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003701 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3702 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003703
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003704 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3705 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3706 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003707 if (ret)
3708 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003709
Yan, Zhenga22285a2010-05-16 10:48:46 -04003710 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003711 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003712 if (ret)
3713 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003714 }
Josef Bacik7b128762008-07-24 12:17:14 -04003715
Tsutomu Itohb5324022011-07-19 07:27:20 +00003716out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003717 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003718 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003719 return ret;
3720}
3721
Misono Tomohirof60a2362018-04-18 11:34:52 +09003722static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003723 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003724{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003725 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003726 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003727 struct btrfs_path *path;
3728 struct extent_buffer *leaf;
3729 struct btrfs_dir_item *di;
3730 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003731 const char *name = dentry->d_name.name;
3732 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003733 u64 index;
3734 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003735 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003736 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003737
Josef Bacik045d3962019-12-18 17:20:27 -05003738 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3739 objectid = inode->root->root_key.objectid;
3740 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3741 objectid = inode->location.objectid;
3742 } else {
3743 WARN_ON(1);
3744 return -EINVAL;
3745 }
3746
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003747 path = btrfs_alloc_path();
3748 if (!path)
3749 return -ENOMEM;
3750
Li Zefan33345d012011-04-20 10:31:50 +08003751 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003752 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003753 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003754 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003755 goto out;
3756 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003757
3758 leaf = path->nodes[0];
3759 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3760 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3761 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003762 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003763 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003764 goto out;
3765 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003766 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003767
Josef Bacikd49d3282019-12-18 17:20:28 -05003768 /*
3769 * This is a placeholder inode for a subvolume we didn't have a
3770 * reference to at the time of the snapshot creation. In the meantime
3771 * we could have renamed the real subvol link into our snapshot, so
3772 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3773 * Instead simply lookup the dir_index_item for this entry so we can
3774 * remove it. Otherwise we know we have a ref to the root and we can
3775 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3776 */
3777 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003778 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003779 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003780 if (IS_ERR_OR_NULL(di)) {
3781 if (!di)
3782 ret = -ENOENT;
3783 else
3784 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003785 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003786 goto out;
3787 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003788
3789 leaf = path->nodes[0];
3790 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003791 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003792 btrfs_release_path(path);
3793 } else {
3794 ret = btrfs_del_root_ref(trans, objectid,
3795 root->root_key.objectid, dir_ino,
3796 &index, name, name_len);
3797 if (ret) {
3798 btrfs_abort_transaction(trans, ret);
3799 goto out;
3800 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003801 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003802
Lu Fengqi9add2942018-08-01 11:32:26 +08003803 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003804 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003805 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003806 goto out;
3807 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003808
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003809 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003810 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003811 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003812 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003813 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003814 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003815out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003816 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003817 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003818}
3819
Misono Tomohiroec42f162018-04-18 11:34:13 +09003820/*
3821 * Helper to check if the subvolume references other subvolumes or if it's
3822 * default.
3823 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003824static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003825{
3826 struct btrfs_fs_info *fs_info = root->fs_info;
3827 struct btrfs_path *path;
3828 struct btrfs_dir_item *di;
3829 struct btrfs_key key;
3830 u64 dir_id;
3831 int ret;
3832
3833 path = btrfs_alloc_path();
3834 if (!path)
3835 return -ENOMEM;
3836
3837 /* Make sure this root isn't set as the default subvol */
3838 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3839 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3840 dir_id, "default", 7, 0);
3841 if (di && !IS_ERR(di)) {
3842 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3843 if (key.objectid == root->root_key.objectid) {
3844 ret = -EPERM;
3845 btrfs_err(fs_info,
3846 "deleting default subvolume %llu is not allowed",
3847 key.objectid);
3848 goto out;
3849 }
3850 btrfs_release_path(path);
3851 }
3852
3853 key.objectid = root->root_key.objectid;
3854 key.type = BTRFS_ROOT_REF_KEY;
3855 key.offset = (u64)-1;
3856
3857 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3858 if (ret < 0)
3859 goto out;
3860 BUG_ON(ret == 0);
3861
3862 ret = 0;
3863 if (path->slots[0] > 0) {
3864 path->slots[0]--;
3865 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3866 if (key.objectid == root->root_key.objectid &&
3867 key.type == BTRFS_ROOT_REF_KEY)
3868 ret = -ENOTEMPTY;
3869 }
3870out:
3871 btrfs_free_path(path);
3872 return ret;
3873}
3874
Nikolay Borisov20a68002018-04-27 14:36:24 +03003875/* Delete all dentries for inodes belonging to the root */
3876static void btrfs_prune_dentries(struct btrfs_root *root)
3877{
3878 struct btrfs_fs_info *fs_info = root->fs_info;
3879 struct rb_node *node;
3880 struct rb_node *prev;
3881 struct btrfs_inode *entry;
3882 struct inode *inode;
3883 u64 objectid = 0;
3884
3885 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3886 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3887
3888 spin_lock(&root->inode_lock);
3889again:
3890 node = root->inode_tree.rb_node;
3891 prev = NULL;
3892 while (node) {
3893 prev = node;
3894 entry = rb_entry(node, struct btrfs_inode, rb_node);
3895
David Sterba37508512018-06-29 10:56:40 +02003896 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003897 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003898 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003899 node = node->rb_right;
3900 else
3901 break;
3902 }
3903 if (!node) {
3904 while (prev) {
3905 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003906 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003907 node = prev;
3908 break;
3909 }
3910 prev = rb_next(prev);
3911 }
3912 }
3913 while (node) {
3914 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003915 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003916 inode = igrab(&entry->vfs_inode);
3917 if (inode) {
3918 spin_unlock(&root->inode_lock);
3919 if (atomic_read(&inode->i_count) > 1)
3920 d_prune_aliases(inode);
3921 /*
3922 * btrfs_drop_inode will have it removed from the inode
3923 * cache when its usage count hits zero.
3924 */
3925 iput(inode);
3926 cond_resched();
3927 spin_lock(&root->inode_lock);
3928 goto again;
3929 }
3930
3931 if (cond_resched_lock(&root->inode_lock))
3932 goto again;
3933
3934 node = rb_next(node);
3935 }
3936 spin_unlock(&root->inode_lock);
3937}
3938
Misono Tomohirof60a2362018-04-18 11:34:52 +09003939int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3940{
3941 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3942 struct btrfs_root *root = BTRFS_I(dir)->root;
3943 struct inode *inode = d_inode(dentry);
3944 struct btrfs_root *dest = BTRFS_I(inode)->root;
3945 struct btrfs_trans_handle *trans;
3946 struct btrfs_block_rsv block_rsv;
3947 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003948 int ret;
3949 int err;
3950
3951 /*
3952 * Don't allow to delete a subvolume with send in progress. This is
3953 * inside the inode lock so the error handling that has to drop the bit
3954 * again is not run concurrently.
3955 */
3956 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003957 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003958 spin_unlock(&dest->root_item_lock);
3959 btrfs_warn(fs_info,
3960 "attempt to delete subvolume %llu during send",
3961 dest->root_key.objectid);
3962 return -EPERM;
3963 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003964 root_flags = btrfs_root_flags(&dest->root_item);
3965 btrfs_set_root_flags(&dest->root_item,
3966 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3967 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003968
3969 down_write(&fs_info->subvol_sem);
3970
3971 err = may_destroy_subvol(dest);
3972 if (err)
3973 goto out_up_write;
3974
3975 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3976 /*
3977 * One for dir inode,
3978 * two for dir entries,
3979 * two for root ref/backref.
3980 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003981 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003982 if (err)
3983 goto out_up_write;
3984
3985 trans = btrfs_start_transaction(root, 0);
3986 if (IS_ERR(trans)) {
3987 err = PTR_ERR(trans);
3988 goto out_release;
3989 }
3990 trans->block_rsv = &block_rsv;
3991 trans->bytes_reserved = block_rsv.size;
3992
3993 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3994
Josef Bacik045d3962019-12-18 17:20:27 -05003995 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003996 if (ret) {
3997 err = ret;
3998 btrfs_abort_transaction(trans, ret);
3999 goto out_end_trans;
4000 }
4001
4002 btrfs_record_root_in_trans(trans, dest);
4003
4004 memset(&dest->root_item.drop_progress, 0,
4005 sizeof(dest->root_item.drop_progress));
4006 dest->root_item.drop_level = 0;
4007 btrfs_set_root_refs(&dest->root_item, 0);
4008
4009 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4010 ret = btrfs_insert_orphan_item(trans,
4011 fs_info->tree_root,
4012 dest->root_key.objectid);
4013 if (ret) {
4014 btrfs_abort_transaction(trans, ret);
4015 err = ret;
4016 goto out_end_trans;
4017 }
4018 }
4019
Lu Fengqid1957792018-05-29 15:01:54 +08004020 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004021 BTRFS_UUID_KEY_SUBVOL,
4022 dest->root_key.objectid);
4023 if (ret && ret != -ENOENT) {
4024 btrfs_abort_transaction(trans, ret);
4025 err = ret;
4026 goto out_end_trans;
4027 }
4028 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004029 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004030 dest->root_item.received_uuid,
4031 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4032 dest->root_key.objectid);
4033 if (ret && ret != -ENOENT) {
4034 btrfs_abort_transaction(trans, ret);
4035 err = ret;
4036 goto out_end_trans;
4037 }
4038 }
4039
Qu Wenruo082b6c92020-06-16 10:17:37 +08004040 free_anon_bdev(dest->anon_dev);
4041 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004042out_end_trans:
4043 trans->block_rsv = NULL;
4044 trans->bytes_reserved = 0;
4045 ret = btrfs_end_transaction(trans);
4046 if (ret && !err)
4047 err = ret;
4048 inode->i_flags |= S_DEAD;
4049out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004050 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004051out_up_write:
4052 up_write(&fs_info->subvol_sem);
4053 if (err) {
4054 spin_lock(&dest->root_item_lock);
4055 root_flags = btrfs_root_flags(&dest->root_item);
4056 btrfs_set_root_flags(&dest->root_item,
4057 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4058 spin_unlock(&dest->root_item_lock);
4059 } else {
4060 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004061 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004062 ASSERT(dest->send_in_progress == 0);
4063
4064 /* the last ref */
4065 if (dest->ino_cache_inode) {
4066 iput(dest->ino_cache_inode);
4067 dest->ino_cache_inode = NULL;
4068 }
4069 }
4070
4071 return err;
4072}
4073
Chris Mason39279cc2007-06-12 06:35:45 -04004074static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4075{
David Howells2b0143b2015-03-17 22:25:59 +00004076 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004077 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004078 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004079 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004080 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004081
David Sterbab3ae2442012-09-13 16:04:34 -06004082 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004083 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004084 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004085 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004086
Josef Bacikd52be812013-05-29 14:54:47 -04004087 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004088 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004089 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004090
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004091 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004092 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004093 goto out;
4094 }
4095
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004096 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004097 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004098 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004099
Filipe Manana44f714d2016-06-06 16:11:13 +01004100 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4101
Chris Mason39279cc2007-06-12 06:35:45 -04004102 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004103 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4104 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4105 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004106 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004107 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004108 /*
4109 * Propagate the last_unlink_trans value of the deleted dir to
4110 * its parent directory. This is to prevent an unrecoverable
4111 * log tree in the case we do something like this:
4112 * 1) create dir foo
4113 * 2) create snapshot under dir foo
4114 * 3) delete the snapshot
4115 * 4) rmdir foo
4116 * 5) mkdir foo
4117 * 6) fsync foo or some file inside foo
4118 */
4119 if (last_unlink_trans >= trans->transid)
4120 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4121 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004122out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004123 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004124 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004125
Chris Mason39279cc2007-06-12 06:35:45 -04004126 return err;
4127}
4128
Josef Bacikddfae632017-10-19 14:16:02 -04004129/*
4130 * Return this if we need to call truncate_block for the last bit of the
4131 * truncate.
4132 */
4133#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004134
Chris Mason323ac952008-10-01 19:05:46 -04004135/*
Chris Mason39279cc2007-06-12 06:35:45 -04004136 * this can truncate away extent items, csum items and directory items.
4137 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004138 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004139 *
4140 * csum items that cross the new i_size are truncated to the new size
4141 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004142 *
4143 * min_type is the minimum key type to truncate down to. If set to 0, this
4144 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004145 */
Yan, Zheng80825102009-11-12 09:35:36 +00004146int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4147 struct btrfs_root *root,
4148 struct inode *inode,
4149 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004150{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004151 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004152 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004153 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004154 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004155 struct btrfs_key key;
4156 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004157 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004158 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004159 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004160 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004161 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004162 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004163 int found_extent;
4164 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004165 int pending_del_nr = 0;
4166 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004167 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004168 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004169 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004170 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004171 bool be_nice = false;
4172 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004173 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4174 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004175
4176 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004177
Chris Mason28ed1342014-12-17 09:41:04 -08004178 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004179 * For non-free space inodes and non-shareable roots, we want to back
4180 * off from time to time. This means all inodes in subvolume roots,
4181 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004182 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004183 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004184 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004185 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004186
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004187 path = btrfs_alloc_path();
4188 if (!path)
4189 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004190 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004191
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004192 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004193 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4194 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004195
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004196 /*
4197 * We want to drop from the next block forward in case this
4198 * new size is not block aligned since we will be keeping the
4199 * last block of the extent just the way it is.
4200 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004201 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004202 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004203 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004204 }
Yan, Zheng80825102009-11-12 09:35:36 +00004205
Miao Xie16cdcec2011-04-22 18:12:22 +08004206 /*
4207 * This function is also used to drop the items in the log tree before
4208 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004209 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004210 * items.
4211 */
4212 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004213 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004214
Li Zefan33345d012011-04-20 10:31:50 +08004215 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004216 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004217 key.type = (u8)-1;
4218
Chris Mason85e21ba2008-01-29 15:11:36 -05004219search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004220 /*
4221 * with a 16K leaf size and 128MB extents, you can actually queue
4222 * up a huge file in a single leaf. Most of the time that
4223 * bytes_deleted is > 0, it will be huge by the time we get here
4224 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004225 if (be_nice && bytes_deleted > SZ_32M &&
4226 btrfs_should_end_transaction(trans)) {
4227 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004228 goto out;
4229 }
Chris Masond3977122009-01-05 21:25:51 -05004230
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004231 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4232 if (ret < 0)
4233 goto out;
4234
Chris Mason85e21ba2008-01-29 15:11:36 -05004235 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004236 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004237 /* there are no items in the tree for us to truncate, we're
4238 * done
4239 */
Yan, Zheng80825102009-11-12 09:35:36 +00004240 if (path->slots[0] == 0)
4241 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004242 path->slots[0]--;
4243 }
4244
Chris Masond3977122009-01-05 21:25:51 -05004245 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004246 u64 clear_start = 0, clear_len = 0;
4247
Chris Mason39279cc2007-06-12 06:35:45 -04004248 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004249 leaf = path->nodes[0];
4250 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004251 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004252
Li Zefan33345d012011-04-20 10:31:50 +08004253 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004254 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004255
Chris Mason85e21ba2008-01-29 15:11:36 -05004256 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004257 break;
4258
Chris Mason5f39d392007-10-15 16:14:19 -04004259 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004260 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004261 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004262 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004263 extent_type = btrfs_file_extent_type(leaf, fi);
4264 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004265 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004266 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004267
4268 trace_btrfs_truncate_show_fi_regular(
4269 BTRFS_I(inode), leaf, fi,
4270 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004271 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004272 item_end += btrfs_file_extent_ram_bytes(leaf,
4273 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004274
4275 trace_btrfs_truncate_show_fi_inline(
4276 BTRFS_I(inode), leaf, fi, path->slots[0],
4277 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004278 }
Yan008630c2007-11-07 13:31:09 -05004279 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004280 }
Yan, Zheng80825102009-11-12 09:35:36 +00004281 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004282 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004283 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004284 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004285 break;
4286 if (found_key.offset >= new_size)
4287 del_item = 1;
4288 else
4289 del_item = 0;
4290 }
Chris Mason39279cc2007-06-12 06:35:45 -04004291 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004292 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004293 if (found_type != BTRFS_EXTENT_DATA_KEY)
4294 goto delete;
4295
4296 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004297 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004298
4299 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004300 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004301 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004302 u64 orig_num_bytes =
4303 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004304 extent_num_bytes = ALIGN(new_size -
4305 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004306 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004307 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004308 btrfs_set_file_extent_num_bytes(leaf, fi,
4309 extent_num_bytes);
4310 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004311 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004312 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004313 &root->state) &&
4314 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004315 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004316 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004317 } else {
Chris Masondb945352007-10-15 16:15:53 -04004318 extent_num_bytes =
4319 btrfs_file_extent_disk_num_bytes(leaf,
4320 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004321 extent_offset = found_key.offset -
4322 btrfs_file_extent_offset(leaf, fi);
4323
Chris Mason39279cc2007-06-12 06:35:45 -04004324 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004325 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004326 if (extent_start != 0) {
4327 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004328 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004329 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004330 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004331 }
4332 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004333 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004334 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004335 /*
4336 * we can't truncate inline items that have had
4337 * special encodings
4338 */
4339 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004340 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004341 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4342 btrfs_file_extent_compression(leaf, fi) == 0) {
4343 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004344
Josef Bacikddfae632017-10-19 14:16:02 -04004345 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4346 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004347 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004348 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004349 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004350 * We have to bail so the last_size is set to
4351 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004352 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004353 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004354 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004355 } else {
4356 /*
4357 * Inline extents are special, we just treat
4358 * them as a full sector worth in the file
4359 * extent tree just for simplicity sake.
4360 */
4361 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004362 }
Josef Bacikddfae632017-10-19 14:16:02 -04004363
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004364 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004365 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004366 }
Chris Mason179e29e2007-11-01 11:28:41 -04004367delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004368 /*
4369 * We use btrfs_truncate_inode_items() to clean up log trees for
4370 * multiple fsyncs, and in this case we don't want to clear the
4371 * file extent range because it's just the log.
4372 */
4373 if (root == BTRFS_I(inode)->root) {
4374 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4375 clear_start, clear_len);
4376 if (ret) {
4377 btrfs_abort_transaction(trans, ret);
4378 break;
4379 }
4380 }
4381
Josef Bacikddfae632017-10-19 14:16:02 -04004382 if (del_item)
4383 last_size = found_key.offset;
4384 else
4385 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004386 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004387 if (!pending_del_nr) {
4388 /* no pending yet, add ourselves */
4389 pending_del_slot = path->slots[0];
4390 pending_del_nr = 1;
4391 } else if (pending_del_nr &&
4392 path->slots[0] + 1 == pending_del_slot) {
4393 /* hop on the pending chunk */
4394 pending_del_nr++;
4395 pending_del_slot = path->slots[0];
4396 } else {
Chris Masond3977122009-01-05 21:25:51 -05004397 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004398 }
Chris Mason39279cc2007-06-12 06:35:45 -04004399 } else {
4400 break;
4401 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004402 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004403
Miao Xie27cdeb72014-04-02 19:51:05 +08004404 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004405 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004406 struct btrfs_ref ref = { 0 };
4407
Chris Mason28ed1342014-12-17 09:41:04 -08004408 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004409
4410 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4411 extent_start, extent_num_bytes, 0);
4412 ref.real_root = root->root_key.objectid;
4413 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4414 ino, extent_offset);
4415 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004416 if (ret) {
4417 btrfs_abort_transaction(trans, ret);
4418 break;
4419 }
Chris Mason28f75a02015-02-04 06:59:29 -08004420 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004421 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004422 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004423 }
Chris Mason39279cc2007-06-12 06:35:45 -04004424 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004425
Yan, Zheng80825102009-11-12 09:35:36 +00004426 if (found_type == BTRFS_INODE_ITEM_KEY)
4427 break;
4428
4429 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004430 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004431 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004432 if (pending_del_nr) {
4433 ret = btrfs_del_items(trans, root, path,
4434 pending_del_slot,
4435 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004436 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004437 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004438 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004439 }
Yan, Zheng80825102009-11-12 09:35:36 +00004440 pending_del_nr = 0;
4441 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004442 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004443
Chris Mason28f75a02015-02-04 06:59:29 -08004444 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004445 * We can generate a lot of delayed refs, so we need to
4446 * throttle every once and a while and make sure we're
4447 * adding enough space to keep up with the work we are
4448 * generating. Since we hold a transaction here we
4449 * can't flush, and we don't want to FLUSH_LIMIT because
4450 * we could have generated too many delayed refs to
4451 * actually allocate, so just bail if we're short and
4452 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004453 */
Josef Bacik28bad212018-12-03 10:20:38 -05004454 if (should_throttle) {
4455 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4456 BTRFS_RESERVE_NO_FLUSH);
4457 if (ret) {
4458 ret = -EAGAIN;
4459 break;
4460 }
Chris Mason28f75a02015-02-04 06:59:29 -08004461 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004462 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004463 } else {
4464 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004465 }
Chris Mason39279cc2007-06-12 06:35:45 -04004466 }
Yan, Zheng80825102009-11-12 09:35:36 +00004467out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004468 if (ret >= 0 && pending_del_nr) {
4469 int err;
4470
4471 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004472 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004473 if (err) {
4474 btrfs_abort_transaction(trans, err);
4475 ret = err;
4476 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004477 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004478 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4479 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004480 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004481 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004482 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004483 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4484 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004485 }
Chris Mason28ed1342014-12-17 09:41:04 -08004486
Chris Mason39279cc2007-06-12 06:35:45 -04004487 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004488 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004489}
4490
Chris Masona52d9a82007-08-27 16:49:44 -04004491/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304492 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004493 * @inode - inode that we're zeroing
4494 * @from - the offset to start zeroing
4495 * @len - the length to zero, 0 to zero the entire range respective to the
4496 * offset
4497 * @front - zero up to the offset instead of from the offset on
4498 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304499 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004500 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004501 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304502int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004503 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004504{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004505 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004506 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004507 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4508 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004509 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004510 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004511 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004512 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004513 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004514 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304515 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004516 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004517 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004518 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004519 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304520 u64 block_start;
4521 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004522
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004523 if (IS_ALIGNED(offset, blocksize) &&
4524 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004525 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304526
Josef Bacik8b62f872017-10-19 14:15:55 -04004527 block_start = round_down(from, blocksize);
4528 block_end = block_start + blocksize - 1;
4529
Nikolay Borisov36ea6f32020-06-03 08:55:41 +03004530 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved,
4531 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004532 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004533 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4534 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004535 /* For nocow case, no need to reserve data space */
4536 only_release_metadata = true;
4537 } else {
4538 goto out;
4539 }
4540 }
4541 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4542 if (ret < 0) {
4543 if (!only_release_metadata)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03004544 btrfs_free_reserved_data_space(BTRFS_I(inode),
4545 data_reserved, block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004546 goto out;
4547 }
Chris Mason211c17f2008-05-15 09:13:45 -04004548again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004549 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004550 if (!page) {
Nikolay Borisov86d52922020-06-03 08:55:40 +03004551 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004552 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004553 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004554 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004555 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004556 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004557
Chris Masona52d9a82007-08-27 16:49:44 -04004558 if (!PageUptodate(page)) {
4559 ret = btrfs_readpage(NULL, page);
4560 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004561 if (page->mapping != mapping) {
4562 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004563 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004564 goto again;
4565 }
Chris Masona52d9a82007-08-27 16:49:44 -04004566 if (!PageUptodate(page)) {
4567 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004568 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004569 }
4570 }
Chris Mason211c17f2008-05-15 09:13:45 -04004571 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004572
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304573 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004574 set_page_extent_mapped(page);
4575
Nikolay Borisovc3504372020-06-03 08:55:03 +03004576 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004577 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304578 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004579 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004580 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004581 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004582 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004583 btrfs_put_ordered_extent(ordered);
4584 goto again;
4585 }
4586
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304587 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004588 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4589 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004590
Nikolay Borisovc2566f22020-06-03 08:55:35 +03004591 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004592 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004593 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304594 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004595 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004596 goto out_unlock;
4597 }
4598
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304599 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004600 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304601 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004602 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004603 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304604 memset(kaddr + (block_start - page_offset(page)),
4605 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004606 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304607 memset(kaddr + (block_start - page_offset(page)) + offset,
4608 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004609 flush_dcache_page(page);
4610 kunmap(page);
4611 }
Chris Mason247e7432008-07-17 12:53:51 -04004612 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004613 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004614 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004615
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004616 if (only_release_metadata)
4617 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4618 block_end, EXTENT_NORESERVE, NULL, NULL,
4619 GFP_NOFS);
4620
Chris Mason89642222008-07-24 09:41:53 -04004621out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004622 if (ret) {
4623 if (only_release_metadata)
4624 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4625 blocksize, true);
4626 else
Nikolay Borisov86d52922020-06-03 08:55:40 +03004627 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004628 block_start, blocksize, true);
4629 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004630 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004631 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004632 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004633out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004634 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004635 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004636 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004637 return ret;
4638}
4639
Josef Bacik16e75492013-10-22 12:18:51 -04004640static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4641 u64 offset, u64 len)
4642{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004643 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004644 struct btrfs_trans_handle *trans;
4645 int ret;
4646
4647 /*
4648 * Still need to make sure the inode looks like it's been updated so
4649 * that any holes get logged if we fsync.
4650 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004651 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4652 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004653 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4654 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4655 return 0;
4656 }
4657
4658 /*
4659 * 1 - for the one we're dropping
4660 * 1 - for the one we're adding
4661 * 1 - for updating the inode.
4662 */
4663 trans = btrfs_start_transaction(root, 3);
4664 if (IS_ERR(trans))
4665 return PTR_ERR(trans);
4666
4667 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4668 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004669 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004670 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004671 return ret;
4672 }
4673
David Sterbaf85b7372017-01-20 14:54:07 +01004674 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4675 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004676 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004677 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004678 else
4679 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004680 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004681 return ret;
4682}
4683
Josef Bacik695a0d02011-03-04 15:46:53 -05004684/*
4685 * This function puts in dummy file extents for the area we're creating a hole
4686 * for. So if we are truncating this file to a larger size we need to insert
4687 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4688 * the range between oldsize and size
4689 */
Josef Bacika41ad392011-01-31 15:30:16 -05004690int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004691{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004692 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004693 struct btrfs_root *root = BTRFS_I(inode)->root;
4694 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004695 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004696 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004697 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004698 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4699 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004700 u64 last_byte;
4701 u64 cur_offset;
4702 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004703 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004704
Josef Bacika71754f2013-06-17 17:14:39 -04004705 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304706 * If our size started in the middle of a block we need to zero out the
4707 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004708 * expose stale data.
4709 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304710 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004711 if (err)
4712 return err;
4713
Yan Zheng9036c102008-10-30 14:19:41 -04004714 if (size <= hole_start)
4715 return 0;
4716
David Sterbab272ae22020-02-05 19:09:33 +01004717 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004718 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004719 cur_offset = hole_start;
4720 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004721 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004722 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004723 if (IS_ERR(em)) {
4724 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004725 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004726 break;
4727 }
Yan Zheng9036c102008-10-30 14:19:41 -04004728 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004729 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004730 hole_size = last_byte - cur_offset;
4731
Yan, Zheng80825102009-11-12 09:35:36 +00004732 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004733 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004734
Josef Bacik16e75492013-10-22 12:18:51 -04004735 err = maybe_insert_hole(root, inode, cur_offset,
4736 hole_size);
4737 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004738 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004739
4740 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4741 cur_offset, hole_size);
4742 if (err)
4743 break;
4744
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004745 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004746 cur_offset + hole_size - 1, 0);
4747 hole_em = alloc_extent_map();
4748 if (!hole_em) {
4749 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4750 &BTRFS_I(inode)->runtime_flags);
4751 goto next;
4752 }
4753 hole_em->start = cur_offset;
4754 hole_em->len = hole_size;
4755 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004756
Josef Bacik5dc562c2012-08-17 13:14:17 -04004757 hole_em->block_start = EXTENT_MAP_HOLE;
4758 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004759 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004760 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004761 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004762 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004763
4764 while (1) {
4765 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004766 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004767 write_unlock(&em_tree->lock);
4768 if (err != -EEXIST)
4769 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004770 btrfs_drop_extent_cache(BTRFS_I(inode),
4771 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004772 cur_offset +
4773 hole_size - 1, 0);
4774 }
4775 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004776 } else {
4777 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4778 cur_offset, hole_size);
4779 if (err)
4780 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004781 }
Josef Bacik16e75492013-10-22 12:18:51 -04004782next:
Yan Zheng9036c102008-10-30 14:19:41 -04004783 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004784 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004785 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004786 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004787 break;
4788 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004789 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004790 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004791 return err;
4792}
4793
Eric Sandeen3972f262013-01-12 02:57:22 +00004794static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004795{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004796 struct btrfs_root *root = BTRFS_I(inode)->root;
4797 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004798 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004799 loff_t newsize = attr->ia_size;
4800 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004801 int ret;
4802
Eric Sandeen3972f262013-01-12 02:57:22 +00004803 /*
4804 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4805 * special case where we need to update the times despite not having
4806 * these flags set. For all other operations the VFS set these flags
4807 * explicitly if it wants a timestamp update.
4808 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004809 if (newsize != oldsize) {
4810 inode_inc_iversion(inode);
4811 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4812 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004813 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004814 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004815
Josef Bacika41ad392011-01-31 15:30:16 -05004816 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004817 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004818 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004819 * This is to ensure the snapshot captures a fully consistent
4820 * state of this file - if the snapshot captures this expanding
4821 * truncation, it must capture all writes that happened before
4822 * this truncation.
4823 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004824 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004825 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004826 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004827 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004828 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004829 }
Yan, Zheng80825102009-11-12 09:35:36 +00004830
Miao Xief4a2f4c2011-12-14 20:12:01 -05004831 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004832 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004833 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004834 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004835 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004836
4837 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004838 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304839 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004840 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004841 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004842 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004843 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004844
Josef Bacika41ad392011-01-31 15:30:16 -05004845 /*
4846 * We're truncating a file that used to have good data down to
4847 * zero. Make sure it gets into the ordered flush list so that
4848 * any new writes get down to disk quickly.
4849 */
4850 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004851 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4852 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004853
Josef Bacika41ad392011-01-31 15:30:16 -05004854 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004855
David Sterba8e0fa5d2020-06-09 19:21:48 +02004856 /* Disable nonlocked read DIO to avoid the endless truncate */
4857 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004858 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004859 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004860
Filipe Manana213e8c52018-02-06 20:40:31 +00004861 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004862 if (ret && inode->i_nlink) {
4863 int err;
4864
4865 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004866 * Truncate failed, so fix up the in-memory size. We
4867 * adjusted disk_i_size down as we removed extents, so
4868 * wait for disk_i_size to be stable and then update the
4869 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004870 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004871 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004872 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004873 return err;
4874 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004875 }
Yan, Zheng80825102009-11-12 09:35:36 +00004876 }
4877
Josef Bacika41ad392011-01-31 15:30:16 -05004878 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004879}
4880
Chris Mason39279cc2007-06-12 06:35:45 -04004881static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4882{
David Howells2b0143b2015-03-17 22:25:59 +00004883 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004884 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004885 int err;
4886
Li Zefanb83cc962010-12-20 16:04:08 +08004887 if (btrfs_root_readonly(root))
4888 return -EROFS;
4889
Jan Kara31051c82016-05-26 16:55:18 +02004890 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004891 if (err)
4892 return err;
4893
Chris Mason5a3f23d2009-03-31 13:27:11 -04004894 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004895 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004896 if (err)
4897 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004898 }
Yan Zheng9036c102008-10-30 14:19:41 -04004899
Christoph Hellwig10257742010-06-04 11:30:02 +02004900 if (attr->ia_valid) {
4901 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004902 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004903 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004904
Josef Bacik22c44fe2011-11-30 10:45:38 -05004905 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004906 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004907 }
4908
Chris Mason39279cc2007-06-12 06:35:45 -04004909 return err;
4910}
Chris Mason61295eb2008-01-14 16:24:38 -05004911
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004912/*
4913 * While truncating the inode pages during eviction, we get the VFS calling
4914 * btrfs_invalidatepage() against each page of the inode. This is slow because
4915 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4916 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4917 * extent_state structures over and over, wasting lots of time.
4918 *
4919 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4920 * those expensive operations on a per page basis and do only the ordered io
4921 * finishing, while we release here the extent_map and extent_state structures,
4922 * without the excessive merging and splitting.
4923 */
4924static void evict_inode_truncate_pages(struct inode *inode)
4925{
4926 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4927 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4928 struct rb_node *node;
4929
4930 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004931 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004932
4933 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004934 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004935 struct extent_map *em;
4936
Liu Bo07e1ce02018-08-23 03:51:52 +08004937 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004938 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004939 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4940 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004941 remove_extent_mapping(map_tree, em);
4942 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004943 if (need_resched()) {
4944 write_unlock(&map_tree->lock);
4945 cond_resched();
4946 write_lock(&map_tree->lock);
4947 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004948 }
4949 write_unlock(&map_tree->lock);
4950
Filipe Manana6ca07092015-05-26 00:55:42 +01004951 /*
4952 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004953 * We can have ongoing bios started by readahead that have
4954 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004955 * still in progress (unlocked the pages in the bio but did not yet
4956 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004957 * ranges can still be locked and eviction started because before
4958 * submitting those bios, which are executed by a separate task (work
4959 * queue kthread), inode references (inode->i_count) were not taken
4960 * (which would be dropped in the end io callback of each bio).
4961 * Therefore here we effectively end up waiting for those bios and
4962 * anyone else holding locked ranges without having bumped the inode's
4963 * reference count - if we don't do it, when they access the inode's
4964 * io_tree to unlock a range it may be too late, leading to an
4965 * use-after-free issue.
4966 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004967 spin_lock(&io_tree->lock);
4968 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4969 struct extent_state *state;
4970 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004971 u64 start;
4972 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004973 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004974
4975 node = rb_first(&io_tree->state);
4976 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004977 start = state->start;
4978 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004979 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004980 spin_unlock(&io_tree->lock);
4981
David Sterbaff13db42015-12-03 14:30:40 +01004982 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004983
4984 /*
4985 * If still has DELALLOC flag, the extent didn't reach disk,
4986 * and its reserved space won't be freed by delayed_ref.
4987 * So we need to free its reserved space here.
4988 * (Refer to comment in btrfs_invalidatepage, case 2)
4989 *
4990 * Note, end is the bytenr of last byte, so we need + 1 here.
4991 */
Filipe Manana421f0922018-10-12 13:02:48 +01004992 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03004993 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
4994 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004995
Filipe Manana6ca07092015-05-26 00:55:42 +01004996 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07004997 EXTENT_LOCKED | EXTENT_DELALLOC |
4998 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
4999 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005000
Filipe Manana7064dd52014-08-08 02:47:05 +01005001 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005002 spin_lock(&io_tree->lock);
5003 }
5004 spin_unlock(&io_tree->lock);
5005}
5006
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005007static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005008 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005009{
5010 struct btrfs_fs_info *fs_info = root->fs_info;
5011 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005012 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005013 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005014 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005015
Josef Bacikd3984c92019-08-01 18:19:37 -04005016 /*
5017 * Eviction should be taking place at some place safe because of our
5018 * delayed iputs. However the normal flushing code will run delayed
5019 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5020 *
5021 * We reserve the delayed_refs_extra here again because we can't use
5022 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5023 * above. We reserve our extra bit here because we generate a ton of
5024 * delayed refs activity by truncating.
5025 *
5026 * If we cannot make our reservation we'll attempt to steal from the
5027 * global reserve, because we really want to be able to free up space.
5028 */
5029 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5030 BTRFS_RESERVE_FLUSH_EVICT);
5031 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005032 /*
5033 * Try to steal from the global reserve if there is space for
5034 * it.
5035 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005036 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5037 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5038 btrfs_warn(fs_info,
5039 "could not allocate space for delete; will truncate on mount");
5040 return ERR_PTR(-ENOSPC);
5041 }
5042 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005043 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005044
5045 trans = btrfs_join_transaction(root);
5046 if (IS_ERR(trans))
5047 return trans;
5048
5049 if (delayed_refs_extra) {
5050 trans->block_rsv = &fs_info->trans_block_rsv;
5051 trans->bytes_reserved = delayed_refs_extra;
5052 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5053 delayed_refs_extra, 1);
5054 }
5055 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005056}
5057
Al Virobd555972010-06-07 11:35:40 -04005058void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005059{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005060 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005061 struct btrfs_trans_handle *trans;
5062 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005063 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005064 int ret;
5065
liubo1abe9b82011-03-24 11:18:59 +00005066 trace_btrfs_inode_evict(inode);
5067
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005068 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005069 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005070 return;
5071 }
5072
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005073 evict_inode_truncate_pages(inode);
5074
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005075 if (inode->i_nlink &&
5076 ((btrfs_root_refs(&root->root_item) != 0 &&
5077 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005078 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005079 goto no_delete;
5080
Omar Sandoval27919062018-05-11 13:13:37 -07005081 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005082 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005083
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005084 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005085
Omar Sandoval7b40b692018-05-11 13:13:33 -07005086 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005087 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005088
Yan, Zheng76dda932009-09-21 16:00:26 -04005089 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005090 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5091 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005092 goto no_delete;
5093 }
5094
Nikolay Borisovaa790212017-01-10 20:35:40 +02005095 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005096 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005097 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005098
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005099 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005100 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005101 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005102 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005103 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005104
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005105 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005106
Yan, Zheng80825102009-11-12 09:35:36 +00005107 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005108 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005109 if (IS_ERR(trans))
5110 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005111
5112 trans->block_rsv = rsv;
5113
Yan, Zhengd68fc572010-05-16 10:49:58 -04005114 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005115 trans->block_rsv = &fs_info->trans_block_rsv;
5116 btrfs_end_transaction(trans);
5117 btrfs_btree_balance_dirty(fs_info);
5118 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5119 goto free_rsv;
5120 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005121 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005122 }
5123
Josef Bacik4ef31a42013-08-13 14:10:08 -04005124 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005125 * Errors here aren't a big deal, it just means we leave orphan items in
5126 * the tree. They will be cleaned up on the next mount. If the inode
5127 * number gets reused, cleanup deletes the orphan item without doing
5128 * anything, and unlink reuses the existing orphan item.
5129 *
5130 * If it turns out that we are dropping too many of these, we might want
5131 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005132 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005133 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005134 if (!IS_ERR(trans)) {
5135 trans->block_rsv = rsv;
5136 btrfs_orphan_del(trans, BTRFS_I(inode));
5137 trans->block_rsv = &fs_info->trans_block_rsv;
5138 btrfs_end_transaction(trans);
5139 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005140
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005141 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005142 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005143 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005144
Omar Sandoval27919062018-05-11 13:13:37 -07005145free_rsv:
5146 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005147no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005148 /*
5149 * If we didn't successfully delete, the orphan item will still be in
5150 * the tree and we'll retry on the next mount. Again, we might also want
5151 * to retry these periodically in the future.
5152 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005153 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005154 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005155}
5156
5157/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005158 * Return the key found in the dir entry in the location pointer, fill @type
5159 * with BTRFS_FT_*, and return 0.
5160 *
Su Yue005d6712018-03-05 17:13:37 +08005161 * If no dir entries were found, returns -ENOENT.
5162 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005163 */
5164static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005165 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005166{
5167 const char *name = dentry->d_name.name;
5168 int namelen = dentry->d_name.len;
5169 struct btrfs_dir_item *di;
5170 struct btrfs_path *path;
5171 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005172 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005173
5174 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005175 if (!path)
5176 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005177
David Sterbaf85b7372017-01-20 14:54:07 +01005178 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5179 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005180 if (IS_ERR_OR_NULL(di)) {
5181 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005182 goto out;
5183 }
Chris Masond3977122009-01-05 21:25:51 -05005184
Chris Mason5f39d392007-10-15 16:14:19 -04005185 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005186 if (location->type != BTRFS_INODE_ITEM_KEY &&
5187 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005188 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005189 btrfs_warn(root->fs_info,
5190"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5191 __func__, name, btrfs_ino(BTRFS_I(dir)),
5192 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005193 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005194 if (!ret)
5195 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005196out:
Chris Mason39279cc2007-06-12 06:35:45 -04005197 btrfs_free_path(path);
5198 return ret;
5199}
5200
5201/*
5202 * when we hit a tree root in a directory, the btrfs part of the inode
5203 * needs to be changed to reflect the root directory of the tree root. This
5204 * is kind of like crossing a mount point.
5205 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005206static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005207 struct inode *dir,
5208 struct dentry *dentry,
5209 struct btrfs_key *location,
5210 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005211{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005212 struct btrfs_path *path;
5213 struct btrfs_root *new_root;
5214 struct btrfs_root_ref *ref;
5215 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005216 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005217 int ret;
5218 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005219
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005220 path = btrfs_alloc_path();
5221 if (!path) {
5222 err = -ENOMEM;
5223 goto out;
5224 }
Chris Mason39279cc2007-06-12 06:35:45 -04005225
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005226 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005227 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5228 key.type = BTRFS_ROOT_REF_KEY;
5229 key.offset = location->objectid;
5230
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005231 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005232 if (ret) {
5233 if (ret < 0)
5234 err = ret;
5235 goto out;
5236 }
Chris Mason39279cc2007-06-12 06:35:45 -04005237
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005238 leaf = path->nodes[0];
5239 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005240 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005241 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5242 goto out;
5243
5244 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5245 (unsigned long)(ref + 1),
5246 dentry->d_name.len);
5247 if (ret)
5248 goto out;
5249
David Sterbab3b4aa72011-04-21 01:20:15 +02005250 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005251
David Sterba56e93572020-05-15 19:35:55 +02005252 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005253 if (IS_ERR(new_root)) {
5254 err = PTR_ERR(new_root);
5255 goto out;
5256 }
5257
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005258 *sub_root = new_root;
5259 location->objectid = btrfs_root_dirid(&new_root->root_item);
5260 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005261 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005262 err = 0;
5263out:
5264 btrfs_free_path(path);
5265 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005266}
5267
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005268static void inode_tree_add(struct inode *inode)
5269{
5270 struct btrfs_root *root = BTRFS_I(inode)->root;
5271 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005272 struct rb_node **p;
5273 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005274 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005275 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005276
Al Viro1d3382cb2010-10-23 15:19:20 -04005277 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005278 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005279 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005280 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005281 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005282 while (*p) {
5283 parent = *p;
5284 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5285
David Sterba37508512018-06-29 10:56:40 +02005286 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005287 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005288 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005289 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005290 else {
5291 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005292 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005293 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005294 RB_CLEAR_NODE(parent);
5295 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005296 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005297 }
5298 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005299 rb_link_node(new, parent, p);
5300 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005301 spin_unlock(&root->inode_lock);
5302}
5303
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005304static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005305{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005306 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005307 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005308
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005309 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005310 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5311 rb_erase(&inode->rb_node, &root->inode_tree);
5312 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005313 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005314 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005315 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005316
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005317 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005318 spin_lock(&root->inode_lock);
5319 empty = RB_EMPTY_ROOT(&root->inode_tree);
5320 spin_unlock(&root->inode_lock);
5321 if (empty)
5322 btrfs_add_dead_root(root);
5323 }
5324}
5325
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005326
Chris Masone02119d2008-09-05 16:13:11 -04005327static int btrfs_init_locked_inode(struct inode *inode, void *p)
5328{
5329 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005330
5331 inode->i_ino = args->ino;
5332 BTRFS_I(inode)->location.objectid = args->ino;
5333 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5334 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005335 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5336 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005337 return 0;
5338}
5339
5340static int btrfs_find_actor(struct inode *inode, void *opaque)
5341{
5342 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005343
5344 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005345 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005346}
5347
David Sterba0202e832020-05-15 19:35:59 +02005348static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005349 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005350{
5351 struct inode *inode;
5352 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005353 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005354
David Sterba0202e832020-05-15 19:35:59 +02005355 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005356 args.root = root;
5357
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005358 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005359 btrfs_init_locked_inode,
5360 (void *)&args);
5361 return inode;
5362}
5363
David Sterba4c66e0d2019-10-03 19:09:35 +02005364/*
David Sterba0202e832020-05-15 19:35:59 +02005365 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005366 * Path can be preallocated to prevent recursing back to iget through
5367 * allocator. NULL is also valid but may require an additional allocation
5368 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305369 */
David Sterba0202e832020-05-15 19:35:59 +02005370struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005371 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305372{
5373 struct inode *inode;
5374
David Sterba0202e832020-05-15 19:35:59 +02005375 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305376 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005377 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305378
5379 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005380 int ret;
5381
Filipe Manana4222ea72018-10-24 10:13:03 +01005382 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005383 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005384 inode_tree_add(inode);
5385 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005386 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005387 iget_failed(inode);
5388 /*
5389 * ret > 0 can come from btrfs_search_slot called by
5390 * btrfs_read_locked_inode, this means the inode item
5391 * was not found.
5392 */
5393 if (ret > 0)
5394 ret = -ENOENT;
5395 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005396 }
5397 }
5398
Balaji Rao1a54ef82008-07-21 02:01:04 +05305399 return inode;
5400}
5401
David Sterba0202e832020-05-15 19:35:59 +02005402struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005403{
David Sterba0202e832020-05-15 19:35:59 +02005404 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005405}
5406
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005407static struct inode *new_simple_dir(struct super_block *s,
5408 struct btrfs_key *key,
5409 struct btrfs_root *root)
5410{
5411 struct inode *inode = new_inode(s);
5412
5413 if (!inode)
5414 return ERR_PTR(-ENOMEM);
5415
Josef Bacik5c8fd992020-02-14 16:11:43 -05005416 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005417 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005418 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005419
5420 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005421 /*
5422 * We only need lookup, the rest is read-only and there's no inode
5423 * associated with the dentry
5424 */
5425 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005426 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005427 inode->i_fop = &simple_dir_operations;
5428 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005429 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305430 inode->i_atime = inode->i_mtime;
5431 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005432 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005433
5434 return inode;
5435}
5436
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005437static inline u8 btrfs_inode_type(struct inode *inode)
5438{
5439 /*
5440 * Compile-time asserts that generic FT_* types still match
5441 * BTRFS_FT_* types
5442 */
5443 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5444 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5445 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5446 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5447 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5448 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5449 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5450 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5451
5452 return fs_umode_to_ftype(inode->i_mode);
5453}
5454
Chris Mason3de45862008-11-17 21:02:50 -05005455struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005456{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005457 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005458 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005459 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005460 struct btrfs_root *sub_root = root;
5461 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005462 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005463 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005464
5465 if (dentry->d_name.len > BTRFS_NAME_LEN)
5466 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005467
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005468 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005469 if (ret < 0)
5470 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005471
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005472 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005473 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005474 if (IS_ERR(inode))
5475 return inode;
5476
5477 /* Do extra check against inode mode with di_type */
5478 if (btrfs_inode_type(inode) != di_type) {
5479 btrfs_crit(fs_info,
5480"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5481 inode->i_mode, btrfs_inode_type(inode),
5482 di_type);
5483 iput(inode);
5484 return ERR_PTR(-EUCLEAN);
5485 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005486 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005487 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005488
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005489 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005490 &location, &sub_root);
5491 if (ret < 0) {
5492 if (ret != -ENOENT)
5493 inode = ERR_PTR(ret);
5494 else
5495 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5496 } else {
David Sterba0202e832020-05-15 19:35:59 +02005497 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005498 }
Josef Bacik87270022020-01-24 09:32:31 -05005499 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005500 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005501
Julia Lawall34d19ba2011-01-24 19:55:19 +00005502 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005503 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005504 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005505 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005506 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005507 if (ret) {
5508 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005509 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005510 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005511 }
5512
Chris Mason3de45862008-11-17 21:02:50 -05005513 return inode;
5514}
5515
Nick Pigginfe15ce42011-01-07 17:49:23 +11005516static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005517{
5518 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005519 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005520
Li Zefan848cce02012-02-21 17:04:28 +08005521 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005522 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005523
Li Zefan848cce02012-02-21 17:04:28 +08005524 if (inode) {
5525 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005526 if (btrfs_root_refs(&root->root_item) == 0)
5527 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005528
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005529 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005530 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005531 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005532 return 0;
5533}
5534
Chris Mason3de45862008-11-17 21:02:50 -05005535static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005536 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005537{
Al Viro3837d202018-10-10 16:38:27 -04005538 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005539
Al Viro3837d202018-10-10 16:38:27 -04005540 if (inode == ERR_PTR(-ENOENT))
5541 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005542 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005543}
5544
Josef Bacik23b5ec72017-07-24 15:14:25 -04005545/*
5546 * All this infrastructure exists because dir_emit can fault, and we are holding
5547 * the tree lock when doing readdir. For now just allocate a buffer and copy
5548 * our information into that, and then dir_emit from the buffer. This is
5549 * similar to what NFS does, only we don't keep the buffer around in pagecache
5550 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5551 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5552 * tree lock.
5553 */
5554static int btrfs_opendir(struct inode *inode, struct file *file)
5555{
5556 struct btrfs_file_private *private;
5557
5558 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5559 if (!private)
5560 return -ENOMEM;
5561 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5562 if (!private->filldir_buf) {
5563 kfree(private);
5564 return -ENOMEM;
5565 }
5566 file->private_data = private;
5567 return 0;
5568}
5569
5570struct dir_entry {
5571 u64 ino;
5572 u64 offset;
5573 unsigned type;
5574 int name_len;
5575};
5576
5577static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5578{
5579 while (entries--) {
5580 struct dir_entry *entry = addr;
5581 char *name = (char *)(entry + 1);
5582
David Sterba92d32172018-04-16 21:10:14 +02005583 ctx->pos = get_unaligned(&entry->offset);
5584 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5585 get_unaligned(&entry->ino),
5586 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005587 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005588 addr += sizeof(struct dir_entry) +
5589 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005590 ctx->pos++;
5591 }
5592 return 0;
5593}
5594
Al Viro9cdda8d2013-05-22 16:48:09 -04005595static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005596{
Al Viro9cdda8d2013-05-22 16:48:09 -04005597 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005598 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005599 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005600 struct btrfs_dir_item *di;
5601 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005602 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005603 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005604 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005605 struct list_head ins_list;
5606 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005607 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005608 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005609 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005610 char *name_ptr;
5611 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005612 int entries = 0;
5613 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005614 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005615 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005616
Al Viro9cdda8d2013-05-22 16:48:09 -04005617 if (!dir_emit_dots(file, ctx))
5618 return 0;
5619
David Woodhouse49593bf2008-08-17 17:08:36 +01005620 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005621 if (!path)
5622 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005623
Josef Bacik23b5ec72017-07-24 15:14:25 -04005624 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005625 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005626
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005627 INIT_LIST_HEAD(&ins_list);
5628 INIT_LIST_HEAD(&del_list);
5629 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005630
Josef Bacik23b5ec72017-07-24 15:14:25 -04005631again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005632 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005633 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005634 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005635
Chris Mason39279cc2007-06-12 06:35:45 -04005636 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5637 if (ret < 0)
5638 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005639
5640 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005641 struct dir_entry *entry;
5642
Chris Mason5f39d392007-10-15 16:14:19 -04005643 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005644 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005645 if (slot >= btrfs_header_nritems(leaf)) {
5646 ret = btrfs_next_leaf(root, path);
5647 if (ret < 0)
5648 goto err;
5649 else if (ret > 0)
5650 break;
5651 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005652 }
Chris Mason3de45862008-11-17 21:02:50 -05005653
Chris Mason5f39d392007-10-15 16:14:19 -04005654 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5655
5656 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005657 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005658 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005659 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005660 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005661 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005662 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005663 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005664 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005665 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005666 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5667 PAGE_SIZE) {
5668 btrfs_release_path(path);
5669 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5670 if (ret)
5671 goto nopos;
5672 addr = private->filldir_buf;
5673 entries = 0;
5674 total_len = 0;
5675 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005676 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005677
5678 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005679 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005680 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005681 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5682 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005683 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005684 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005685 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005686 put_unaligned(location.objectid, &entry->ino);
5687 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005688 entries++;
5689 addr += sizeof(struct dir_entry) + name_len;
5690 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005691next:
5692 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005693 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005694 btrfs_release_path(path);
5695
5696 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5697 if (ret)
5698 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005699
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005700 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005701 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005702 goto nopos;
5703
Zach Browndb62efb2013-07-11 16:19:42 -07005704 /*
5705 * Stop new entries from being returned after we return the last
5706 * entry.
5707 *
5708 * New directory entries are assigned a strictly increasing
5709 * offset. This means that new entries created during readdir
5710 * are *guaranteed* to be seen in the future by that readdir.
5711 * This has broken buggy programs which operate on names as
5712 * they're returned by readdir. Until we re-use freed offsets
5713 * we have this hack to stop new entries from being returned
5714 * under the assumption that they'll never reach this huge
5715 * offset.
5716 *
5717 * This is being careful not to overflow 32bit loff_t unless the
5718 * last entry requires it because doing so has broken 32bit apps
5719 * in the past.
5720 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005721 if (ctx->pos >= INT_MAX)
5722 ctx->pos = LLONG_MAX;
5723 else
5724 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005725nopos:
5726 ret = 0;
5727err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005728 if (put)
5729 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005730 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005731 return ret;
5732}
5733
Chris Mason39279cc2007-06-12 06:35:45 -04005734/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005735 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005736 * inode changes. But, it is most likely to find the inode in cache.
5737 * FIXME, needs more benchmarking...there are no reasons other than performance
5738 * to keep or drop this code.
5739 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005740static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005741{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005742 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005743 struct btrfs_root *root = BTRFS_I(inode)->root;
5744 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005745 int ret;
5746
Josef Bacik72ac3c02012-05-23 14:13:11 -04005747 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005748 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005749
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005750 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005751 if (IS_ERR(trans))
5752 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005753
5754 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005755 if (ret && ret == -ENOSPC) {
5756 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005757 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005758 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005759 if (IS_ERR(trans))
5760 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005761
Chris Mason94b60442010-05-26 11:02:00 -04005762 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005763 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005764 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005765 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005766 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005767
5768 return ret;
5769}
5770
5771/*
5772 * This is a copy of file_update_time. We need this so we can return error on
5773 * ENOSPC for updating the inode in the case of file write and mmap writes.
5774 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005775static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005776 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005777{
Alexander Block2bc5565282012-06-15 09:49:33 +02005778 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005779 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005780
5781 if (btrfs_root_readonly(root))
5782 return -EROFS;
5783
Josef Bacike41f9412012-03-26 09:46:47 -04005784 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005785 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005786 if (flags & S_CTIME)
5787 inode->i_ctime = *now;
5788 if (flags & S_MTIME)
5789 inode->i_mtime = *now;
5790 if (flags & S_ATIME)
5791 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005792 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005793}
5794
Chris Masond352ac62008-09-29 15:18:18 -04005795/*
5796 * find the highest existing sequence number in a directory
5797 * and then set the in-memory index_cnt variable to reflect
5798 * free sequence numbers
5799 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005800static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005801{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005802 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005803 struct btrfs_key key, found_key;
5804 struct btrfs_path *path;
5805 struct extent_buffer *leaf;
5806 int ret;
5807
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005808 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005809 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005810 key.offset = (u64)-1;
5811
5812 path = btrfs_alloc_path();
5813 if (!path)
5814 return -ENOMEM;
5815
5816 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5817 if (ret < 0)
5818 goto out;
5819 /* FIXME: we should be able to handle this */
5820 if (ret == 0)
5821 goto out;
5822 ret = 0;
5823
5824 /*
5825 * MAGIC NUMBER EXPLANATION:
5826 * since we search a directory based on f_pos we have to start at 2
5827 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5828 * else has to start at 2
5829 */
5830 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005831 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005832 goto out;
5833 }
5834
5835 path->slots[0]--;
5836
5837 leaf = path->nodes[0];
5838 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5839
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005840 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005841 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005842 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005843 goto out;
5844 }
5845
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005846 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005847out:
5848 btrfs_free_path(path);
5849 return ret;
5850}
5851
Chris Masond352ac62008-09-29 15:18:18 -04005852/*
5853 * helper to find a free sequence number in a given directory. This current
5854 * code is very simple, later versions will do smarter things in the btree
5855 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005856int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005857{
5858 int ret = 0;
5859
Nikolay Borisov877574e2017-02-20 13:50:33 +02005860 if (dir->index_cnt == (u64)-1) {
5861 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005862 if (ret) {
5863 ret = btrfs_set_inode_index_count(dir);
5864 if (ret)
5865 return ret;
5866 }
Josef Bacikaec74772008-07-24 12:12:38 -04005867 }
5868
Nikolay Borisov877574e2017-02-20 13:50:33 +02005869 *index = dir->index_cnt;
5870 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005871
5872 return ret;
5873}
5874
Chris Masonb0d5d102014-09-08 13:08:51 -07005875static int btrfs_insert_inode_locked(struct inode *inode)
5876{
5877 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005878
5879 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005880 args.root = BTRFS_I(inode)->root;
5881
5882 return insert_inode_locked4(inode,
5883 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5884 btrfs_find_actor, &args);
5885}
5886
Anand Jain19aee8d2017-07-18 17:37:05 +08005887/*
5888 * Inherit flags from the parent inode.
5889 *
5890 * Currently only the compression flags and the cow flags are inherited.
5891 */
5892static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5893{
5894 unsigned int flags;
5895
5896 if (!dir)
5897 return;
5898
5899 flags = BTRFS_I(dir)->flags;
5900
5901 if (flags & BTRFS_INODE_NOCOMPRESS) {
5902 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5903 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5904 } else if (flags & BTRFS_INODE_COMPRESS) {
5905 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5906 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5907 }
5908
5909 if (flags & BTRFS_INODE_NODATACOW) {
5910 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5911 if (S_ISREG(inode->i_mode))
5912 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5913 }
5914
David Sterba7b6a2212018-03-26 18:40:21 +02005915 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005916}
5917
Chris Mason39279cc2007-06-12 06:35:45 -04005918static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5919 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005920 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005921 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005922 u64 ref_objectid, u64 objectid,
5923 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005924{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005925 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005926 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005927 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005928 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005929 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005930 struct btrfs_inode_ref *ref;
5931 struct btrfs_key key[2];
5932 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005933 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005934 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005935 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005936 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005937
Chris Mason5f39d392007-10-15 16:14:19 -04005938 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005939 if (!path)
5940 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005941
Josef Bacik11a19a92019-09-09 10:12:04 -04005942 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005943 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005944 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005945 if (!inode) {
5946 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005947 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005948 }
Chris Mason39279cc2007-06-12 06:35:45 -04005949
Li Zefan581bb052011-04-20 10:06:11 +08005950 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005951 * O_TMPFILE, set link count to 0, so that after this point,
5952 * we fill in an inode item with the correct link count.
5953 */
5954 if (!name)
5955 set_nlink(inode, 0);
5956
5957 /*
Li Zefan581bb052011-04-20 10:06:11 +08005958 * we have to initialize this early, so we can reclaim the inode
5959 * number if we fail afterwards in this function.
5960 */
5961 inode->i_ino = objectid;
5962
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005963 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005964 trace_btrfs_inode_request(dir);
5965
Nikolay Borisov877574e2017-02-20 13:50:33 +02005966 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005967 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005968 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005969 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005970 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005971 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005972 } else if (dir) {
5973 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005974 }
5975 /*
5976 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005977 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005978 * number
5979 */
5980 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005981 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005982 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005983 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005984 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005985
Josef Bacik5dc562c2012-08-17 13:14:17 -04005986 /*
5987 * We could have gotten an inode number from somebody who was fsynced
5988 * and then removed in this same transaction, so let's just set full
5989 * sync since it will be a full sync anyway and this will blow away the
5990 * old info in the log.
5991 */
5992 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5993
Chris Mason9c583092008-01-29 15:15:18 -05005994 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005995 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05005996 key[0].offset = 0;
5997
Chris Mason9c583092008-01-29 15:15:18 -05005998 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005999
6000 if (name) {
6001 /*
6002 * Start new inodes with an inode_ref. This is slightly more
6003 * efficient for small numbers of hard links since they will
6004 * be packed into one item. Extended refs will kick in if we
6005 * add more hard links than can fit in the ref item.
6006 */
6007 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006008 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006009 key[1].offset = ref_objectid;
6010
6011 sizes[1] = name_len + sizeof(*ref);
6012 }
Chris Mason9c583092008-01-29 15:15:18 -05006013
Chris Masonb0d5d102014-09-08 13:08:51 -07006014 location = &BTRFS_I(inode)->location;
6015 location->objectid = objectid;
6016 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006017 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006018
6019 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006020 if (ret < 0) {
6021 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006022 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006023 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006024
Chris Masonb9473432009-03-13 11:00:37 -04006025 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006026 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006027 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006028 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006029
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006030 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006031 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306032
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006033 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306034 inode->i_atime = inode->i_mtime;
6035 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006036 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306037
Chris Mason5f39d392007-10-15 16:14:19 -04006038 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6039 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006040 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006041 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006042 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006043
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006044 if (name) {
6045 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6046 struct btrfs_inode_ref);
6047 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6048 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6049 ptr = (unsigned long)(ref + 1);
6050 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6051 }
Chris Mason9c583092008-01-29 15:15:18 -05006052
Chris Mason5f39d392007-10-15 16:14:19 -04006053 btrfs_mark_buffer_dirty(path->nodes[0]);
6054 btrfs_free_path(path);
6055
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006056 btrfs_inherit_iflags(inode, dir);
6057
Al Viro569254b2011-07-24 17:08:40 -04006058 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006059 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006060 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006061 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006062 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6063 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006064 }
6065
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006066 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006067
6068 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006069 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006070
Alexander Block8ea05e32012-07-25 17:35:53 +02006071 btrfs_update_root_times(trans, root);
6072
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006073 ret = btrfs_inode_inherit_props(trans, inode, dir);
6074 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006075 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006076 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006077 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006078
Chris Mason39279cc2007-06-12 06:35:45 -04006079 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006080
6081fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006082 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006083fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006084 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006085 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006086 btrfs_free_path(path);
6087 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006088}
6089
Chris Masond352ac62008-09-29 15:18:18 -04006090/*
6091 * utility function to add 'inode' into 'parent_inode' with
6092 * a give name and a given sequence number.
6093 * if 'add_backref' is true, also insert a backref from the
6094 * inode to the parent directory.
6095 */
Chris Masone02119d2008-09-05 16:13:11 -04006096int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006097 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006098 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006099{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006100 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006101 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006102 struct btrfs_root *root = parent_inode->root;
6103 u64 ino = btrfs_ino(inode);
6104 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006105
Li Zefan33345d012011-04-20 10:31:50 +08006106 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006107 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006108 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006109 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006110 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006111 key.offset = 0;
6112 }
Chris Mason39279cc2007-06-12 06:35:45 -04006113
Li Zefan33345d012011-04-20 10:31:50 +08006114 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006115 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006116 root->root_key.objectid, parent_ino,
6117 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006118 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006119 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6120 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006121 }
6122
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006123 /* Nothing to clean up yet */
6124 if (ret)
6125 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006126
Lu Fengqi684572d2018-08-04 21:10:57 +08006127 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006128 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006129 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006130 goto fail_dir_item;
6131 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006132 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006133 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006134 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006135
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006136 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006137 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006138 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006139 /*
6140 * If we are replaying a log tree, we do not want to update the mtime
6141 * and ctime of the parent directory with the current time, since the
6142 * log replay procedure is responsible for setting them to their correct
6143 * values (the ones it had when the fsync was done).
6144 */
6145 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6146 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6147
6148 parent_inode->vfs_inode.i_mtime = now;
6149 parent_inode->vfs_inode.i_ctime = now;
6150 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006151 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006152 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006153 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006154 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006155
6156fail_dir_item:
6157 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6158 u64 local_index;
6159 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006160 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006161 root->root_key.objectid, parent_ino,
6162 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006163 if (err)
6164 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006165 } else if (add_backref) {
6166 u64 local_index;
6167 int err;
6168
6169 err = btrfs_del_inode_ref(trans, root, name, name_len,
6170 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006171 if (err)
6172 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006173 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006174
6175 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006176 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006177}
6178
6179static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006180 struct btrfs_inode *dir, struct dentry *dentry,
6181 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006182{
Josef Bacika1b075d2010-11-19 20:36:11 +00006183 int err = btrfs_add_link(trans, dir, inode,
6184 dentry->d_name.name, dentry->d_name.len,
6185 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006186 if (err > 0)
6187 err = -EEXIST;
6188 return err;
6189}
6190
Josef Bacik618e21d2007-07-11 10:18:17 -04006191static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006192 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006193{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006194 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006195 struct btrfs_trans_handle *trans;
6196 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006197 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006198 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006199 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006200 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006201
Josef Bacik9ed74f22009-09-11 16:12:44 -04006202 /*
6203 * 2 for inode item and ref
6204 * 2 for dir items
6205 * 1 for xattr if selinux is on
6206 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006207 trans = btrfs_start_transaction(root, 5);
6208 if (IS_ERR(trans))
6209 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006210
Li Zefan581bb052011-04-20 10:06:11 +08006211 err = btrfs_find_free_ino(root, &objectid);
6212 if (err)
6213 goto out_unlock;
6214
Josef Bacikaec74772008-07-24 12:12:38 -04006215 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006216 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6217 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006218 if (IS_ERR(inode)) {
6219 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006220 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006221 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006222 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006223
Casey Schauflerad19db72011-12-15 10:09:07 -05006224 /*
6225 * If the active LSM wants to access the inode during
6226 * d_instantiate it needs these. Smack checks to see
6227 * if the filesystem supports xattrs by looking at the
6228 * ops vector.
6229 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006230 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006231 init_special_inode(inode, inode->i_mode, rdev);
6232
6233 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006234 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006235 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006236
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006237 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6238 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006239 if (err)
6240 goto out_unlock;
6241
6242 btrfs_update_inode(trans, root, inode);
6243 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006244
Josef Bacik618e21d2007-07-11 10:18:17 -04006245out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006246 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006247 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006248 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006249 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006250 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006251 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006252 return err;
6253}
6254
Chris Mason39279cc2007-06-12 06:35:45 -04006255static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006256 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006257{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006258 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006259 struct btrfs_trans_handle *trans;
6260 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006261 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006262 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006263 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006264 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006265
Josef Bacik9ed74f22009-09-11 16:12:44 -04006266 /*
6267 * 2 for inode item and ref
6268 * 2 for dir items
6269 * 1 for xattr if selinux is on
6270 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006271 trans = btrfs_start_transaction(root, 5);
6272 if (IS_ERR(trans))
6273 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006274
Li Zefan581bb052011-04-20 10:06:11 +08006275 err = btrfs_find_free_ino(root, &objectid);
6276 if (err)
6277 goto out_unlock;
6278
Josef Bacikaec74772008-07-24 12:12:38 -04006279 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006280 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6281 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006282 if (IS_ERR(inode)) {
6283 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006284 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006285 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006286 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006287 /*
6288 * If the active LSM wants to access the inode during
6289 * d_instantiate it needs these. Smack checks to see
6290 * if the filesystem supports xattrs by looking at the
6291 * ops vector.
6292 */
6293 inode->i_fop = &btrfs_file_operations;
6294 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006295 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006296
6297 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6298 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006299 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006300
6301 err = btrfs_update_inode(trans, root, inode);
6302 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006303 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006304
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006305 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6306 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006307 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006308 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006309
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006310 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006311 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006312
Chris Mason39279cc2007-06-12 06:35:45 -04006313out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006314 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006315 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006316 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006317 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006318 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006319 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006320 return err;
6321}
6322
6323static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6324 struct dentry *dentry)
6325{
Filipe Manana271dba452016-01-05 16:24:05 +00006326 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006327 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006328 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006329 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006330 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006331 int err;
6332 int drop_inode = 0;
6333
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006334 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006335 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006336 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006337
Mark Fashehf1863732012-08-08 11:32:27 -07006338 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006339 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006340
Nikolay Borisov877574e2017-02-20 13:50:33 +02006341 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006342 if (err)
6343 goto fail;
6344
Yan, Zhenga22285a2010-05-16 10:48:46 -04006345 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006346 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006347 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006348 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006349 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006350 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006351 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006352 if (IS_ERR(trans)) {
6353 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006354 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006355 goto fail;
6356 }
Chris Mason5f39d392007-10-15 16:14:19 -04006357
Miao Xie67de1172013-12-26 13:07:06 +08006358 /* There are several dir indexes for this inode, clear the cache. */
6359 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006360 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006361 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006362 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006363 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006364 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006365
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006366 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6367 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006368
Yan, Zhenga5719522009-09-24 09:17:31 -04006369 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006370 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006371 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006372 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006373
Yan, Zhenga5719522009-09-24 09:17:31 -04006374 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006375 if (err)
6376 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006377 if (inode->i_nlink == 1) {
6378 /*
6379 * If new hard link count is 1, it's a file created
6380 * with open(2) O_TMPFILE flag.
6381 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006382 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006383 if (err)
6384 goto fail;
6385 }
Al Viro08c422c2011-12-23 07:58:13 -05006386 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006387 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006388 }
Chris Mason39279cc2007-06-12 06:35:45 -04006389
Chris Mason1832a6d2007-12-21 16:27:21 -05006390fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006391 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006392 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006393 if (drop_inode) {
6394 inode_dec_link_count(inode);
6395 iput(inode);
6396 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006397 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006398 return err;
6399}
6400
Al Viro18bb1db2011-07-26 01:41:39 -04006401static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006402{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006403 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006404 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006405 struct btrfs_trans_handle *trans;
6406 struct btrfs_root *root = BTRFS_I(dir)->root;
6407 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006408 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006409 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006410
Josef Bacik9ed74f22009-09-11 16:12:44 -04006411 /*
6412 * 2 items for inode and ref
6413 * 2 items for dir items
6414 * 1 for xattr if selinux is on
6415 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006416 trans = btrfs_start_transaction(root, 5);
6417 if (IS_ERR(trans))
6418 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006419
Li Zefan581bb052011-04-20 10:06:11 +08006420 err = btrfs_find_free_ino(root, &objectid);
6421 if (err)
6422 goto out_fail;
6423
Josef Bacikaec74772008-07-24 12:12:38 -04006424 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006425 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6426 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006427 if (IS_ERR(inode)) {
6428 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006429 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006430 goto out_fail;
6431 }
Chris Mason5f39d392007-10-15 16:14:19 -04006432
Chris Masonb0d5d102014-09-08 13:08:51 -07006433 /* these must be set before we unlock the inode */
6434 inode->i_op = &btrfs_dir_inode_operations;
6435 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006436
Eric Paris2a7dba32011-02-01 11:05:39 -05006437 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006438 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006439 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006440
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006441 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006442 err = btrfs_update_inode(trans, root, inode);
6443 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006444 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006445
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006446 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6447 dentry->d_name.name,
6448 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006449 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006450 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006451
Al Viro1e2e5472018-05-04 08:23:01 -04006452 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006453
6454out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006455 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006456 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006457 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006458 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006459 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006460 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006461 return err;
6462}
6463
Chris Masonc8b97812008-10-29 14:49:59 -04006464static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006465 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006466 size_t pg_offset, u64 extent_offset,
6467 struct btrfs_file_extent_item *item)
6468{
6469 int ret;
6470 struct extent_buffer *leaf = path->nodes[0];
6471 char *tmp;
6472 size_t max_size;
6473 unsigned long inline_size;
6474 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006475 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006476
6477 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006478 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006479 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6480 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006481 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006482 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006483 if (!tmp)
6484 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006485 ptr = btrfs_file_extent_inline_start(item);
6486
6487 read_extent_buffer(leaf, tmp, ptr, inline_size);
6488
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006489 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006490 ret = btrfs_decompress(compress_type, tmp, page,
6491 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006492
6493 /*
6494 * decompression code contains a memset to fill in any space between the end
6495 * of the uncompressed data and the end of max_size in case the decompressed
6496 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6497 * the end of an inline extent and the beginning of the next block, so we
6498 * cover that region here.
6499 */
6500
6501 if (max_size + pg_offset < PAGE_SIZE) {
6502 char *map = kmap(page);
6503 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6504 kunmap(page);
6505 }
Chris Masonc8b97812008-10-29 14:49:59 -04006506 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006507 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006508}
6509
Omar Sandoval39b07b52019-12-02 17:34:23 -08006510/**
6511 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6512 * @inode: file to search in
6513 * @page: page to read extent data into if the extent is inline
6514 * @pg_offset: offset into @page to copy to
6515 * @start: file offset
6516 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006517 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006518 * This returns the first &struct extent_map which overlaps with the given
6519 * range, reading it from the B-tree and caching it if necessary. Note that
6520 * there may be more extents which overlap the given range after the returned
6521 * extent_map.
6522 *
6523 * If @page is not NULL and the extent is inline, this also reads the extent
6524 * data directly into the page and marks the extent up to date in the io_tree.
6525 *
6526 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006527 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006528struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006529 struct page *page, size_t pg_offset,
6530 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006531{
David Sterba3ffbd682018-06-29 10:56:42 +02006532 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006533 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006534 u64 extent_start = 0;
6535 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006536 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006537 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006538 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006539 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006540 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006541 struct extent_buffer *leaf;
6542 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006543 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006544 struct extent_map_tree *em_tree = &inode->extent_tree;
6545 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006546
Chris Mason890871b2009-09-02 16:24:52 -04006547 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006548 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006549 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006550
Chris Masona52d9a82007-08-27 16:49:44 -04006551 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006552 if (em->start > start || em->start + em->len <= start)
6553 free_extent_map(em);
6554 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006555 free_extent_map(em);
6556 else
6557 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006558 }
David Sterba172ddd62011-04-21 00:48:27 +02006559 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006560 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006561 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006562 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006563 }
Chris Masond1310b22008-01-24 16:13:08 -05006564 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006565 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006566 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006567 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006568
Liu Bobee6ec82018-08-17 05:05:28 +08006569 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006570 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006571 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006572 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006573 }
6574
Liu Bobee6ec82018-08-17 05:05:28 +08006575 /* Chances are we'll be called again, so go ahead and do readahead */
6576 path->reada = READA_FORWARD;
6577
Liu Boe49aabd2018-08-25 13:47:09 +08006578 /*
6579 * Unless we're going to uncompress the inline extent, no sleep would
6580 * happen.
6581 */
6582 path->leave_spinning = 1;
6583
Josef Bacik51899412020-08-20 11:46:01 -04006584 path->recurse = btrfs_is_free_space_inode(inode);
6585
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006586 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006587 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006588 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006589 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006590 if (path->slots[0] == 0)
6591 goto not_found;
6592 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006593 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006594 }
6595
Chris Mason5f39d392007-10-15 16:14:19 -04006596 leaf = path->nodes[0];
6597 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006598 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006599 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006600 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006601 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006602 /*
6603 * If we backup past the first extent we want to move forward
6604 * and see if there is an extent in front of us, otherwise we'll
6605 * say there is a hole for our whole search range which can
6606 * cause problems.
6607 */
6608 extent_end = start;
6609 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006610 }
6611
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006612 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006613 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006614 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006615 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6616 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006617 /* Only regular file could have regular/prealloc extent */
6618 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006619 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006620 btrfs_crit(fs_info,
6621 "regular/prealloc extent found for non-regular inode %llu",
6622 btrfs_ino(inode));
6623 goto out;
6624 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006625 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6626 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006627 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006628 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6629 path->slots[0],
6630 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006631 }
Josef Bacik25a50342013-10-14 12:08:38 -04006632next:
Yan Zheng9036c102008-10-30 14:19:41 -04006633 if (start >= extent_end) {
6634 path->slots[0]++;
6635 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6636 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006637 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006638 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006639 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006640 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006641
Yan Zheng9036c102008-10-30 14:19:41 -04006642 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006643 }
Yan Zheng9036c102008-10-30 14:19:41 -04006644 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6645 if (found_key.objectid != objectid ||
6646 found_key.type != BTRFS_EXTENT_DATA_KEY)
6647 goto not_found;
6648 if (start + len <= found_key.offset)
6649 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006650 if (start > found_key.offset)
6651 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006652
6653 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006654 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006655 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006656 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006657 em->block_start = EXTENT_MAP_HOLE;
6658 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006659 }
6660
Omar Sandoval39b07b52019-12-02 17:34:23 -08006661 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006662
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006663 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6664 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006665 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006666 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006667 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006668 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006669 size_t size;
6670 size_t extent_offset;
6671 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006672
Omar Sandoval39b07b52019-12-02 17:34:23 -08006673 if (!page)
Yan689f9342007-10-29 11:41:07 -04006674 goto out;
Yan689f9342007-10-29 11:41:07 -04006675
Qu Wenruoe41ca582018-06-06 15:41:49 +08006676 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006677 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006678 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6679 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006680 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006681 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006682 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006683 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006684 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006685
6686 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006687 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006688 if (btrfs_file_extent_compression(leaf, item) !=
6689 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006690 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006691 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006692 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04006693 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04006694 } else {
6695 map = kmap(page);
6696 read_extent_buffer(leaf, map + pg_offset, ptr,
6697 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006698 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006699 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006700 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006701 copy_size);
6702 }
Chris Masonc8b97812008-10-29 14:49:59 -04006703 kunmap(page);
6704 }
Chris Mason179e29e2007-11-01 11:28:41 -04006705 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006706 }
Chris Masond1310b22008-01-24 16:13:08 -05006707 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006708 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006709 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006710 }
6711not_found:
6712 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006713 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006714 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006715 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006716insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006717 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02006718 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006719 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006720 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006721 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6722 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006723 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04006724 goto out;
6725 }
Chris Masond1310b22008-01-24 16:13:08 -05006726
Chris Mason890871b2009-09-02 16:24:52 -04006727 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006728 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006729 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006730out:
Liu Boc6414282018-08-23 07:36:17 +08006731 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006732
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006733 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006734
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006735 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04006736 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006737 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04006738 }
6739 return em;
6740}
6741
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006742struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006743 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006744{
6745 struct extent_map *em;
6746 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006747 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006748 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006749 u64 delalloc_len;
6750 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006751 int err = 0;
6752
Omar Sandoval39b07b52019-12-02 17:34:23 -08006753 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006754 if (IS_ERR(em))
6755 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006756 /*
6757 * If our em maps to:
6758 * - a hole or
6759 * - a pre-alloc extent,
6760 * there might actually be delalloc bytes behind it.
6761 */
6762 if (em->block_start != EXTENT_MAP_HOLE &&
6763 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6764 return em;
6765 else
6766 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006767
6768 /* check to see if we've wrapped (len == -1 or similar) */
6769 end = start + len;
6770 if (end < start)
6771 end = (u64)-1;
6772 else
6773 end -= 1;
6774
6775 em = NULL;
6776
6777 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006778 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006779 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006780 delalloc_end = delalloc_start + delalloc_len;
6781 if (delalloc_end < delalloc_start)
6782 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006783
6784 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006785 * We didn't find anything useful, return the original results from
6786 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006787 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006788 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006789 em = hole_em;
6790 hole_em = NULL;
6791 goto out;
6792 }
6793
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006794 /*
6795 * Adjust the delalloc_start to make sure it doesn't go backwards from
6796 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006797 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006798 delalloc_start = max(start, delalloc_start);
6799 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006800
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006801 if (delalloc_len > 0) {
6802 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006803 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006804 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006805
David Sterba172ddd62011-04-21 00:48:27 +02006806 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006807 if (!em) {
6808 err = -ENOMEM;
6809 goto out;
6810 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006811
6812 ASSERT(hole_em);
6813 /*
6814 * When btrfs_get_extent can't find anything it returns one
6815 * huge hole
6816 *
6817 * Make sure what it found really fits our range, and adjust to
6818 * make sure it is based on the start from the caller
6819 */
6820 if (hole_end <= start || hole_em->start > end) {
6821 free_extent_map(hole_em);
6822 hole_em = NULL;
6823 } else {
6824 hole_start = max(hole_em->start, start);
6825 hole_len = hole_end - hole_start;
6826 }
6827
6828 if (hole_em && delalloc_start > hole_start) {
6829 /*
6830 * Our hole starts before our delalloc, so we have to
6831 * return just the parts of the hole that go until the
6832 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006833 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006834 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006835 em->start = hole_start;
6836 em->orig_start = hole_start;
6837 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006838 * Don't adjust block start at all, it is fixed at
6839 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006840 */
6841 em->block_start = hole_em->block_start;
6842 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006843 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6844 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006845 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006846 /*
6847 * Hole is out of passed range or it starts after
6848 * delalloc range
6849 */
6850 em->start = delalloc_start;
6851 em->len = delalloc_len;
6852 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006853 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006854 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006855 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006856 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006857 return hole_em;
6858 }
6859out:
6860
6861 free_extent_map(hole_em);
6862 if (err) {
6863 free_extent_map(em);
6864 return ERR_PTR(err);
6865 }
6866 return em;
6867}
6868
Nikolay Borisov64f54182020-06-03 08:55:31 +03006869static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006870 const u64 start,
6871 const u64 len,
6872 const u64 orig_start,
6873 const u64 block_start,
6874 const u64 block_len,
6875 const u64 orig_block_len,
6876 const u64 ram_bytes,
6877 const int type)
6878{
6879 struct extent_map *em = NULL;
6880 int ret;
6881
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006882 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03006883 em = create_io_em(inode, start, len, orig_start, block_start,
6884 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08006885 BTRFS_COMPRESS_NONE, /* compress_type */
6886 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006887 if (IS_ERR(em))
6888 goto out;
6889 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03006890 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
6891 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006892 if (ret) {
6893 if (em) {
6894 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03006895 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006896 }
6897 em = ERR_PTR(ret);
6898 }
6899 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006900
6901 return em;
6902}
6903
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006904static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006905 u64 start, u64 len)
6906{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006907 struct btrfs_root *root = inode->root;
6908 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04006909 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006910 struct btrfs_key ins;
6911 u64 alloc_hint;
6912 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006913
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006914 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006915 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006916 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006917 if (ret)
6918 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006919
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006920 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006921 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006922 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006923 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006924 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006925 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
6926 1);
Josef Bacik00361582013-08-14 14:02:47 -04006927
Josef Bacik4b46fce2010-05-23 11:00:55 -04006928 return em;
6929}
6930
Chris Mason46bfbb52010-05-26 11:04:10 -04006931/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006932 * Check if we can do nocow write into the range [@offset, @offset + @len)
6933 *
6934 * @offset: File offset
6935 * @len: The length to write, will be updated to the nocow writeable
6936 * range
6937 * @orig_start: (optional) Return the original file offset of the file extent
6938 * @orig_len: (optional) Return the original on-disk length of the file extent
6939 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07006940 * @strict: if true, omit optimizations that might force us into unnecessary
6941 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006942 *
6943 * This function will flush ordered extents in the range to ensure proper
6944 * nocow checks for (nowait == false) case.
6945 *
6946 * Return:
6947 * >0 and update @len if we can do nocow write
6948 * 0 if we can't do nocow write
6949 * <0 if error happened
6950 *
6951 * NOTE: This only checks the file extents, caller is responsible to wait for
6952 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04006953 */
Josef Bacik00361582013-08-14 14:02:47 -04006954noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006955 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07006956 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04006957{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006958 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006959 struct btrfs_path *path;
6960 int ret;
6961 struct extent_buffer *leaf;
6962 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006963 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006964 struct btrfs_file_extent_item *fi;
6965 struct btrfs_key key;
6966 u64 disk_bytenr;
6967 u64 backref_offset;
6968 u64 extent_end;
6969 u64 num_bytes;
6970 int slot;
6971 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006972 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006973
Chris Mason46bfbb52010-05-26 11:04:10 -04006974 path = btrfs_alloc_path();
6975 if (!path)
6976 return -ENOMEM;
6977
David Sterbaf85b7372017-01-20 14:54:07 +01006978 ret = btrfs_lookup_file_extent(NULL, root, path,
6979 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006980 if (ret < 0)
6981 goto out;
6982
6983 slot = path->slots[0];
6984 if (ret == 1) {
6985 if (slot == 0) {
6986 /* can't find the item, must cow */
6987 ret = 0;
6988 goto out;
6989 }
6990 slot--;
6991 }
6992 ret = 0;
6993 leaf = path->nodes[0];
6994 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006995 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04006996 key.type != BTRFS_EXTENT_DATA_KEY) {
6997 /* not our file or wrong item type, must cow */
6998 goto out;
6999 }
7000
7001 if (key.offset > offset) {
7002 /* Wrong offset, must cow */
7003 goto out;
7004 }
7005
7006 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7007 found_type = btrfs_file_extent_type(leaf, fi);
7008 if (found_type != BTRFS_FILE_EXTENT_REG &&
7009 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7010 /* not a regular extent, must cow */
7011 goto out;
7012 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007013
7014 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7015 goto out;
7016
Miao Xiee77751a2013-12-27 21:11:50 +08007017 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7018 if (extent_end <= offset)
7019 goto out;
7020
Chris Mason46bfbb52010-05-26 11:04:10 -04007021 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007022 if (disk_bytenr == 0)
7023 goto out;
7024
7025 if (btrfs_file_extent_compression(leaf, fi) ||
7026 btrfs_file_extent_encryption(leaf, fi) ||
7027 btrfs_file_extent_other_encoding(leaf, fi))
7028 goto out;
7029
Ethan Lien78d42952018-05-17 14:58:29 +08007030 /*
7031 * Do the same check as in btrfs_cross_ref_exist but without the
7032 * unnecessary search.
7033 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007034 if (!strict &&
7035 (btrfs_file_extent_generation(leaf, fi) <=
7036 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007037 goto out;
7038
Chris Mason46bfbb52010-05-26 11:04:10 -04007039 backref_offset = btrfs_file_extent_offset(leaf, fi);
7040
Josef Bacik7ee9e442013-06-21 16:37:03 -04007041 if (orig_start) {
7042 *orig_start = key.offset - backref_offset;
7043 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7044 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7045 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007046
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007047 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007048 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007049
7050 num_bytes = min(offset + *len, extent_end) - offset;
7051 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7052 u64 range_end;
7053
Jeff Mahoneyda170662016-06-15 09:22:56 -04007054 range_end = round_up(offset + num_bytes,
7055 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007056 ret = test_range_bit(io_tree, offset, range_end,
7057 EXTENT_DELALLOC, 0, NULL);
7058 if (ret) {
7059 ret = -EAGAIN;
7060 goto out;
7061 }
7062 }
7063
Josef Bacik1bda19e2013-10-18 12:10:36 -04007064 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007065
7066 /*
7067 * look for other files referencing this extent, if we
7068 * find any we must cow
7069 */
Josef Bacik00361582013-08-14 14:02:47 -04007070
Liu Boe4c3b2d2017-01-30 12:25:28 -08007071 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007072 key.offset - backref_offset, disk_bytenr,
7073 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007074 if (ret) {
7075 ret = 0;
7076 goto out;
7077 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007078
7079 /*
7080 * adjust disk_bytenr and num_bytes to cover just the bytes
7081 * in this extent we are about to write. If there
7082 * are any csums in that range we have to cow in order
7083 * to keep the csums correct
7084 */
7085 disk_bytenr += backref_offset;
7086 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007087 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7088 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007089 /*
7090 * all of the above have passed, it is safe to overwrite this extent
7091 * without cow
7092 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007093 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007094 ret = 1;
7095out:
7096 btrfs_free_path(path);
7097 return ret;
7098}
7099
Josef Bacikeb838e72012-07-31 16:28:48 -04007100static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007101 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007102{
7103 struct btrfs_ordered_extent *ordered;
7104 int ret = 0;
7105
7106 while (1) {
7107 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007108 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007109 /*
7110 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007111 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007112 * extents in this range.
7113 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007114 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007115 lockend - lockstart + 1);
7116
7117 /*
7118 * We need to make sure there are no buffered pages in this
7119 * range either, we could have raced between the invalidate in
7120 * generic_file_direct_write and locking the extent. The
7121 * invalidate needs to happen so that reads after a write do not
7122 * get stale data.
7123 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007124 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007125 (!writing || !filemap_range_has_page(inode->i_mapping,
7126 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007127 break;
7128
7129 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007130 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007131
7132 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007133 /*
7134 * If we are doing a DIO read and the ordered extent we
7135 * found is for a buffered write, we can not wait for it
7136 * to complete and retry, because if we do so we can
7137 * deadlock with concurrent buffered writes on page
7138 * locks. This happens only if our DIO read covers more
7139 * than one extent map, if at this point has already
7140 * created an ordered extent for a previous extent map
7141 * and locked its range in the inode's io tree, and a
7142 * concurrent write against that previous extent map's
7143 * range and this range started (we unlock the ranges
7144 * in the io tree only when the bios complete and
7145 * buffered writes always lock pages before attempting
7146 * to lock range in the io tree).
7147 */
7148 if (writing ||
7149 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007150 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007151 else
7152 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007153 btrfs_put_ordered_extent(ordered);
7154 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007155 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007156 * We could trigger writeback for this range (and wait
7157 * for it to complete) and then invalidate the pages for
7158 * this range (through invalidate_inode_pages2_range()),
7159 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007160 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007161 * triggered a readahead) on a page lock due to an
7162 * ordered dio extent we created before but did not have
7163 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007164 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007165 * ordered extent to complete while holding a lock on
7166 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007167 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007168 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007169 }
7170
Filipe Mananaade77022016-02-18 14:28:55 +00007171 if (ret)
7172 break;
7173
Josef Bacikeb838e72012-07-31 16:28:48 -04007174 cond_resched();
7175 }
7176
7177 return ret;
7178}
7179
Liu Bo6f9994d2017-01-31 07:50:22 -08007180/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007181static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7182 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007183 u64 block_len, u64 orig_block_len,
7184 u64 ram_bytes, int compress_type,
7185 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007186{
7187 struct extent_map_tree *em_tree;
7188 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007189 int ret;
7190
Liu Bo6f9994d2017-01-31 07:50:22 -08007191 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7192 type == BTRFS_ORDERED_COMPRESSED ||
7193 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007194 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007195
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007196 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007197 em = alloc_extent_map();
7198 if (!em)
7199 return ERR_PTR(-ENOMEM);
7200
7201 em->start = start;
7202 em->orig_start = orig_start;
7203 em->len = len;
7204 em->block_len = block_len;
7205 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007206 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007207 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007208 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007209 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007210 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007211 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007212 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007213 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7214 em->compress_type = compress_type;
7215 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007216
7217 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007218 btrfs_drop_extent_cache(inode, em->start,
7219 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007220 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007221 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007222 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007223 /*
7224 * The caller has taken lock_extent(), who could race with us
7225 * to add em?
7226 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007227 } while (ret == -EEXIST);
7228
7229 if (ret) {
7230 free_extent_map(em);
7231 return ERR_PTR(ret);
7232 }
7233
Liu Bo6f9994d2017-01-31 07:50:22 -08007234 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007235 return em;
7236}
7237
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007238
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007239static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007240 struct inode *inode,
7241 struct btrfs_dio_data *dio_data,
7242 u64 start, u64 len)
7243{
7244 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7245 struct extent_map *em = *map;
7246 int ret = 0;
7247
7248 /*
7249 * We don't allocate a new extent in the following cases
7250 *
7251 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7252 * existing extent.
7253 * 2) The extent is marked as PREALLOC. We're good to go here and can
7254 * just use the extent.
7255 *
7256 */
7257 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7258 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7259 em->block_start != EXTENT_MAP_HOLE)) {
7260 int type;
7261 u64 block_start, orig_start, orig_block_len, ram_bytes;
7262
7263 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7264 type = BTRFS_ORDERED_PREALLOC;
7265 else
7266 type = BTRFS_ORDERED_NOCOW;
7267 len = min(len, em->len - (start - em->start));
7268 block_start = em->block_start + (start - em->start);
7269
7270 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007271 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007272 btrfs_inc_nocow_writers(fs_info, block_start)) {
7273 struct extent_map *em2;
7274
Nikolay Borisov64f54182020-06-03 08:55:31 +03007275 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007276 orig_start, block_start,
7277 len, orig_block_len,
7278 ram_bytes, type);
7279 btrfs_dec_nocow_writers(fs_info, block_start);
7280 if (type == BTRFS_ORDERED_PREALLOC) {
7281 free_extent_map(em);
7282 *map = em = em2;
7283 }
7284
7285 if (em2 && IS_ERR(em2)) {
7286 ret = PTR_ERR(em2);
7287 goto out;
7288 }
7289 /*
7290 * For inode marked NODATACOW or extent marked PREALLOC,
7291 * use the existing or preallocated extent, so does not
7292 * need to adjust btrfs_space_info's bytes_may_use.
7293 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007294 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007295 goto skip_cow;
7296 }
7297 }
7298
7299 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007300 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007301 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007302 if (IS_ERR(em)) {
7303 ret = PTR_ERR(em);
7304 goto out;
7305 }
7306
7307 len = min(len, em->len - (start - em->start));
7308
7309skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007310 /*
7311 * Need to update the i_size under the extent lock so buffered
7312 * readers will get the updated i_size when we unlock.
7313 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007314 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007315 i_size_write(inode, start + len);
7316
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007317 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007318out:
7319 return ret;
7320}
7321
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007322static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7323 loff_t length, unsigned int flags, struct iomap *iomap,
7324 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007325{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007326 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007327 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007328 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307329 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007330 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007331 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007332 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007333 u64 len = length;
7334 bool unlock_extents = false;
Josef Bacik0eb79292020-09-03 11:16:51 -04007335 bool sync = (current->journal_info == BTRFS_DIO_SYNC_STUB);
7336
7337 /*
7338 * We used current->journal_info here to see if we were sync, but
7339 * there's a lot of tests in the enospc machinery to not do flushing if
7340 * we have a journal_info set, so we need to clear this out and re-set
7341 * it in iomap_end.
7342 */
7343 ASSERT(current->journal_info == NULL ||
7344 current->journal_info == BTRFS_DIO_SYNC_STUB);
7345 current->journal_info = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007346
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007347 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007348 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007349
Josef Bacikc3298612012-08-03 16:49:19 -04007350 lockstart = start;
7351 lockend = start + len - 1;
7352
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007353 /*
7354 * The generic stuff only does filemap_write_and_wait_range, which
7355 * isn't enough if we've written compressed pages to this area, so we
7356 * need to flush the dirty pages again to make absolutely sure that any
7357 * outstanding dirty pages are on disk.
7358 */
7359 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7360 &BTRFS_I(inode)->runtime_flags)) {
7361 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7362 start + length - 1);
7363 if (ret)
7364 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007365 }
7366
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007367 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7368 if (!dio_data)
7369 return -ENOMEM;
7370
Josef Bacik0eb79292020-09-03 11:16:51 -04007371 dio_data->sync = sync;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007372 dio_data->length = length;
7373 if (write) {
7374 dio_data->reserve = round_up(length, fs_info->sectorsize);
7375 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7376 &dio_data->data_reserved,
7377 start, dio_data->reserve);
7378 if (ret) {
7379 extent_changeset_free(dio_data->data_reserved);
7380 kfree(dio_data);
7381 return ret;
7382 }
7383 }
7384 iomap->private = dio_data;
7385
7386
Josef Bacikeb838e72012-07-31 16:28:48 -04007387 /*
7388 * If this errors out it's because we couldn't invalidate pagecache for
7389 * this range and we need to fallback to buffered.
7390 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007391 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007392 ret = -ENOTBLK;
7393 goto err;
7394 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007395
Omar Sandoval39b07b52019-12-02 17:34:23 -08007396 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007397 if (IS_ERR(em)) {
7398 ret = PTR_ERR(em);
7399 goto unlock_err;
7400 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007401
7402 /*
7403 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7404 * io. INLINE is special, and we could probably kludge it in here, but
7405 * it's still buffered so for safety lets just fall back to the generic
7406 * buffered path.
7407 *
7408 * For COMPRESSED we _have_ to read the entire extent in so we can
7409 * decompress it, so there will be buffering required no matter what we
7410 * do, so go ahead and fallback to buffered.
7411 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007412 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007413 * to buffered IO. Don't blame me, this is the price we pay for using
7414 * the generic code.
7415 */
7416 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7417 em->block_start == EXTENT_MAP_INLINE) {
7418 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007419 ret = -ENOTBLK;
7420 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007421 }
7422
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007423 len = min(len, em->len - (start - em->start));
7424 if (write) {
7425 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7426 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007427 if (ret < 0)
7428 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007429 unlock_extents = true;
7430 /* Recalc len in case the new em is smaller than requested */
7431 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007432 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007433 /*
7434 * We need to unlock only the end area that we aren't using.
7435 * The rest is going to be unlocked by the endio routine.
7436 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007437 lockstart = start + len;
7438 if (lockstart < lockend)
7439 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007440 }
7441
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007442 if (unlock_extents)
7443 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7444 lockstart, lockend, &cached_state);
7445 else
7446 free_extent_state(cached_state);
7447
7448 /*
7449 * Translate extent map information to iomap.
7450 * We trim the extents (and move the addr) even though iomap code does
7451 * that, since we have locked only the parts we are performing I/O in.
7452 */
7453 if ((em->block_start == EXTENT_MAP_HOLE) ||
7454 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7455 iomap->addr = IOMAP_NULL_ADDR;
7456 iomap->type = IOMAP_HOLE;
7457 } else {
7458 iomap->addr = em->block_start + (start - em->start);
7459 iomap->type = IOMAP_MAPPED;
7460 }
7461 iomap->offset = start;
7462 iomap->bdev = fs_info->fs_devices->latest_bdev;
7463 iomap->length = len;
7464
Josef Bacik4b46fce2010-05-23 11:00:55 -04007465 free_extent_map(em);
7466
7467 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007468
7469unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007470 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7471 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007472err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007473 if (dio_data) {
7474 btrfs_delalloc_release_space(BTRFS_I(inode),
7475 dio_data->data_reserved, start,
7476 dio_data->reserve, true);
7477 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7478 extent_changeset_free(dio_data->data_reserved);
7479 kfree(dio_data);
7480 }
7481 return ret;
7482}
7483
7484static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7485 ssize_t written, unsigned int flags, struct iomap *iomap)
7486{
7487 int ret = 0;
7488 struct btrfs_dio_data *dio_data = iomap->private;
7489 size_t submitted = dio_data->submitted;
7490 const bool write = !!(flags & IOMAP_WRITE);
7491
7492 if (!write && (iomap->type == IOMAP_HOLE)) {
7493 /* If reading from a hole, unlock and return */
7494 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7495 goto out;
7496 }
7497
7498 if (submitted < length) {
7499 pos += submitted;
7500 length -= submitted;
7501 if (write)
7502 __endio_write_update_ordered(BTRFS_I(inode), pos,
7503 length, false);
7504 else
7505 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7506 pos + length - 1);
7507 ret = -ENOTBLK;
7508 }
7509
7510 if (write) {
7511 if (dio_data->reserve)
7512 btrfs_delalloc_release_space(BTRFS_I(inode),
7513 dio_data->data_reserved, pos,
7514 dio_data->reserve, true);
7515 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7516 extent_changeset_free(dio_data->data_reserved);
7517 }
7518out:
Josef Bacik0eb79292020-09-03 11:16:51 -04007519 /*
7520 * We're all done, we can re-set the current->journal_info now safely
7521 * for our endio.
7522 */
7523 if (dio_data->sync) {
7524 ASSERT(current->journal_info == NULL);
7525 current->journal_info = BTRFS_DIO_SYNC_STUB;
7526 }
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007527 kfree(dio_data);
7528 iomap->private = NULL;
7529
Josef Bacikeb838e72012-07-31 16:28:48 -04007530 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007531}
7532
Omar Sandoval769b4f22020-04-16 14:46:22 -07007533static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7534{
7535 /*
7536 * This implies a barrier so that stores to dio_bio->bi_status before
7537 * this and loads of dio_bio->bi_status after this are fully ordered.
7538 */
7539 if (!refcount_dec_and_test(&dip->refs))
7540 return;
7541
7542 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007543 __endio_write_update_ordered(BTRFS_I(dip->inode),
7544 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007545 dip->bytes,
7546 !dip->dio_bio->bi_status);
7547 } else {
7548 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7549 dip->logical_offset,
7550 dip->logical_offset + dip->bytes - 1);
7551 }
7552
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007553 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007554 kfree(dip);
7555}
7556
Omar Sandoval77d5d682020-04-16 14:46:25 -07007557static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7558 int mirror_num,
7559 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007560{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007561 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007562 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007563 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007564
Mike Christie37226b22016-06-05 14:31:52 -05007565 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007566
Omar Sandoval5c047a62020-04-16 14:46:24 -07007567 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007568 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007569 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007570
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007571 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007572 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007573 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007574 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007575 return ret;
7576}
7577
Omar Sandoval769b4f22020-04-16 14:46:22 -07007578static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7579 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007580 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007581{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007582 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7583 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007584 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007585 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7586 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007587 struct bio_vec bvec;
7588 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007589 u64 start = io_bio->logical;
7590 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007591 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007592
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007593 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7594 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307595
Liu Bo17347ce2017-05-15 15:33:27 -07007596 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7597 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007598 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007599 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007600 if (uptodate &&
7601 (!csum || !check_data_csum(inode, io_bio, icsum,
7602 bvec.bv_page, pgoff,
7603 start, sectorsize))) {
7604 clean_io_failure(fs_info, failure_tree, io_tree,
7605 start, bvec.bv_page,
7606 btrfs_ino(BTRFS_I(inode)),
7607 pgoff);
7608 } else {
7609 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007610
Omar Sandoval77d5d682020-04-16 14:46:25 -07007611 status = btrfs_submit_read_repair(inode,
7612 &io_bio->bio,
7613 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007614 bvec.bv_page, pgoff,
7615 start,
7616 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007617 io_bio->mirror_num,
7618 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007619 if (status)
7620 err = status;
7621 }
7622 start += sectorsize;
7623 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307624 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307625 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007626 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007627 return err;
7628}
7629
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007630static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007631 const u64 offset, const u64 bytes,
7632 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007633{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007634 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007635 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007636 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007637 u64 ordered_offset = offset;
7638 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007639 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007640
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007641 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007642 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007643 else
Qu Wenruo524272602017-03-08 10:25:52 +08007644 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007645
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007646 while (ordered_offset < offset + bytes) {
7647 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007648 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007649 &ordered_offset,
7650 ordered_bytes,
7651 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007652 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7653 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007654 btrfs_queue_work(wq, &ordered->work);
7655 }
7656 /*
7657 * If btrfs_dec_test_ordered_pending does not find any ordered
7658 * extent in the range, we can exit.
7659 */
7660 if (ordered_offset == last_offset)
7661 return;
7662 /*
7663 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007664 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007665 */
7666 if (ordered_offset < offset + bytes) {
7667 ordered_bytes = offset + bytes - ordered_offset;
7668 ordered = NULL;
7669 }
Chris Mason163cf092010-11-28 19:56:33 -05007670 }
Filipe Manana14543772015-11-24 16:23:54 +00007671}
7672
David Sterbad0ee3932018-03-08 14:35:48 +01007673static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007674 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007675{
Josef Bacikc6100a42017-05-05 11:57:13 -04007676 struct inode *inode = private_data;
Johannes Thumshirnc965d642020-07-28 20:25:41 +09007677
7678 return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007679}
7680
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007681static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007682{
7683 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007684 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007685
Miao Xie8b110e32014-09-12 18:44:03 +08007686 if (err)
7687 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007688 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007689 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7690 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007691 (unsigned long long)bio->bi_iter.bi_sector,
7692 bio->bi_iter.bi_size, err);
7693
Omar Sandoval769b4f22020-04-16 14:46:22 -07007694 if (bio_op(bio) == REQ_OP_READ) {
7695 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007696 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007697 }
7698
Omar Sandoval769b4f22020-04-16 14:46:22 -07007699 if (err)
7700 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007701
Miao Xiee65e1532010-11-22 03:04:43 +00007702 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007703 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007704}
7705
David Sterbad0ee3932018-03-08 14:35:48 +01007706static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7707 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007708{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007709 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007710 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007711 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007712 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007713
Liu Bo4c274bc2017-11-01 17:19:27 -06007714 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007715 if (async_submit)
7716 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7717
Josef Bacik5fd02042012-05-02 14:00:54 -04007718 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007719 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007720 if (ret)
7721 goto err;
7722 }
Miao Xiee65e1532010-11-22 03:04:43 +00007723
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007724 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007725 goto map;
7726
7727 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007728 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7729 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007730 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007731 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007732 } else if (write) {
7733 /*
7734 * If we aren't doing async submit, calculate the csum of the
7735 * bio now.
7736 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007737 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007738 if (ret)
7739 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007740 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007741 u64 csum_offset;
7742
7743 csum_offset = file_offset - dip->logical_offset;
7744 csum_offset >>= inode->i_sb->s_blocksize_bits;
7745 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7746 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007747 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007748map:
Chris Mason08635ba2019-07-10 12:28:14 -07007749 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007750err:
Miao Xiee65e1532010-11-22 03:04:43 +00007751 return ret;
7752}
7753
Omar Sandovalc36cac22020-04-16 14:46:13 -07007754/*
7755 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7756 * or ordered extents whether or not we submit any bios.
7757 */
7758static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7759 struct inode *inode,
7760 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007761{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007762 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007763 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7764 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007765 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007766
Omar Sandoval85879572020-04-16 14:46:21 -07007767 dip_size = sizeof(*dip);
7768 if (!write && csum) {
7769 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7770 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7771 size_t nblocks;
7772
7773 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7774 dip_size += csum_size * nblocks;
7775 }
7776
7777 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007778 if (!dip)
7779 return NULL;
7780
Omar Sandovalc36cac22020-04-16 14:46:13 -07007781 dip->inode = inode;
7782 dip->logical_offset = file_offset;
7783 dip->bytes = dio_bio->bi_iter.bi_size;
7784 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007785 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007786 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007787 return dip;
7788}
7789
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007790static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
7791 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007792{
7793 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007794 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007795 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007796 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7797 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007798 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007799 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007800 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007801 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007802 u64 submit_len;
7803 int clone_offset = 0;
7804 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307805 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007806 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007807 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007808 struct btrfs_dio_data *dio_data = iomap->private;
Miao Xiee65e1532010-11-22 03:04:43 +00007809
Omar Sandovalc36cac22020-04-16 14:46:13 -07007810 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7811 if (!dip) {
7812 if (!write) {
7813 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7814 file_offset + dio_bio->bi_iter.bi_size - 1);
7815 }
7816 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007817 bio_endio(dio_bio);
7818 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04007819 }
7820
Omar Sandoval85879572020-04-16 14:46:21 -07007821 if (!write && csum) {
7822 /*
7823 * Load the csums up front to reduce csum tree searches and
7824 * contention when submitting bios.
7825 */
7826 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7827 dip->csums);
7828 if (status != BLK_STS_OK)
7829 goto out_err;
7830 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007831
Omar Sandoval769b4f22020-04-16 14:46:22 -07007832 start_sector = dio_bio->bi_iter.bi_sector;
7833 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007834
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007835 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007836 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7837 start_sector << 9, submit_len,
7838 &geom);
7839 if (ret) {
7840 status = errno_to_blk_status(ret);
7841 goto out_err;
7842 }
7843 ASSERT(geom.len <= INT_MAX);
7844
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007845 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007846
Liu Bo725130b2017-05-16 09:51:39 -07007847 /*
7848 * This will never fail as it's passing GPF_NOFS and
7849 * the allocation is backed by btrfs_bioset.
7850 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007851 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007852 bio->bi_private = dip;
7853 bio->bi_end_io = btrfs_end_dio_bio;
7854 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007855
Liu Bo725130b2017-05-16 09:51:39 -07007856 ASSERT(submit_len >= clone_len);
7857 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007858
Liu Bo725130b2017-05-16 09:51:39 -07007859 /*
7860 * Increase the count before we submit the bio so we know
7861 * the end IO handler won't happen before we increase the
7862 * count. Otherwise, the dip might get freed before we're
7863 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007864 *
7865 * We transfer the initial reference to the last bio, so we
7866 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007867 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007868 if (submit_len > 0) {
7869 refcount_inc(&dip->refs);
7870 /*
7871 * If we are submitting more than one bio, submit them
7872 * all asynchronously. The exception is RAID 5 or 6, as
7873 * asynchronous checksums make it difficult to collect
7874 * full stripe writes.
7875 */
7876 if (!raid56)
7877 async_submit = 1;
7878 }
Miao Xiee65e1532010-11-22 03:04:43 +00007879
David Sterbad0ee3932018-03-08 14:35:48 +01007880 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007881 async_submit);
7882 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007883 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007884 if (submit_len > 0)
7885 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007886 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007887 }
Liu Bo725130b2017-05-16 09:51:39 -07007888
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007889 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07007890 clone_offset += clone_len;
7891 start_sector += clone_len >> 9;
7892 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007893 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007894 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00007895
Miao Xiee65e1532010-11-22 03:04:43 +00007896out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007897 dip->dio_bio->bi_status = status;
7898 btrfs_dio_private_put(dip);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007899 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00007900}
7901
David Sterbaf4c48b42020-06-09 19:19:27 +02007902static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7903 const struct iov_iter *iter, loff_t offset)
7904{
7905 int seg;
7906 int i;
7907 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7908 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007909
David Sterbaf4c48b42020-06-09 19:19:27 +02007910 if (offset & blocksize_mask)
7911 goto out;
7912
7913 if (iov_iter_alignment(iter) & blocksize_mask)
7914 goto out;
7915
7916 /* If this is a write we don't need to check anymore */
7917 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7918 return 0;
7919 /*
7920 * Check to make sure we don't have duplicate iov_base's in this
7921 * iovec, if so return EINVAL, otherwise we'll get csum errors
7922 * when reading back.
7923 */
7924 for (seg = 0; seg < iter->nr_segs; seg++) {
7925 for (i = seg + 1; i < iter->nr_segs; i++) {
7926 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7927 goto out;
7928 }
7929 }
7930 retval = 0;
7931out:
7932 return retval;
7933}
7934
Josef Bacik0eb79292020-09-03 11:16:51 -04007935static inline int btrfs_maybe_fsync_end_io(struct kiocb *iocb, ssize_t size,
7936 int error, unsigned flags)
7937{
7938 /*
7939 * Now if we're still in the context of our submitter we know we can't
7940 * safely run generic_write_sync(), so clear our flag here so that the
7941 * caller knows to follow up with a sync.
7942 */
7943 if (current->journal_info == BTRFS_DIO_SYNC_STUB) {
7944 current->journal_info = NULL;
7945 return error;
7946 }
7947
7948 if (error)
7949 return error;
7950
7951 if (size) {
7952 iocb->ki_flags |= IOCB_DSYNC;
7953 return generic_write_sync(iocb, size);
7954 }
7955
7956 return 0;
7957}
7958
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007959static const struct iomap_ops btrfs_dio_iomap_ops = {
7960 .iomap_begin = btrfs_dio_iomap_begin,
7961 .iomap_end = btrfs_dio_iomap_end,
7962};
7963
7964static const struct iomap_dio_ops btrfs_dio_ops = {
7965 .submit_io = btrfs_submit_direct,
7966};
7967
Josef Bacik0eb79292020-09-03 11:16:51 -04007968static const struct iomap_dio_ops btrfs_sync_dops = {
7969 .submit_io = btrfs_submit_direct,
7970 .end_io = btrfs_maybe_fsync_end_io,
7971};
7972
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007973ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007974{
7975 struct file *file = iocb->ki_filp;
7976 struct inode *inode = file->f_mapping->host;
7977 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7978 struct extent_changeset *data_reserved = NULL;
7979 loff_t offset = iocb->ki_pos;
7980 size_t count = 0;
7981 bool relock = false;
7982 ssize_t ret;
7983
7984 if (check_direct_IO(fs_info, iter, offset))
7985 return 0;
7986
7987 count = iov_iter_count(iter);
7988 if (iov_iter_rw(iter) == WRITE) {
7989 /*
7990 * If the write DIO is beyond the EOF, we need update
7991 * the isize, but it is protected by i_mutex. So we can
7992 * not unlock the i_mutex at this case.
7993 */
7994 if (offset + count <= inode->i_size) {
7995 inode_unlock(inode);
7996 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02007997 }
7998 down_read(&BTRFS_I(inode)->dio_sem);
7999 }
8000
Josef Bacik0eb79292020-09-03 11:16:51 -04008001 /*
8002 * We have are actually a sync iocb, so we need our fancy endio to know
8003 * if we need to sync.
8004 */
8005 if (current->journal_info)
8006 ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops,
8007 &btrfs_sync_dops, is_sync_kiocb(iocb));
8008 else
8009 ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops,
8010 &btrfs_dio_ops, is_sync_kiocb(iocb));
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008011
8012 if (ret == -ENOTBLK)
8013 ret = 0;
8014
8015 if (iov_iter_rw(iter) == WRITE)
David Sterbaf4c48b42020-06-09 19:19:27 +02008016 up_read(&BTRFS_I(inode)->dio_sem);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008017
David Sterbaf4c48b42020-06-09 19:19:27 +02008018 if (relock)
8019 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02008020
David Sterbaf4c48b42020-06-09 19:19:27 +02008021 extent_changeset_free(data_reserved);
8022 return ret;
8023}
Chris Mason16432982008-04-10 10:23:21 -04008024
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008025static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008026 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008027{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008028 int ret;
8029
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008030 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008031 if (ret)
8032 return ret;
8033
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008034 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008035}
8036
Chris Mason9ebefb182007-06-15 13:50:00 -04008037int btrfs_readpage(struct file *file, struct page *page)
8038{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008039 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8040 u64 start = page_offset(page);
8041 u64 end = start + PAGE_SIZE - 1;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008042 unsigned long bio_flags = 0;
Nikolay Borisov0f208812020-09-14 14:39:16 +03008043 struct bio *bio = NULL;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008044 int ret;
8045
Nikolay Borisov0f208812020-09-14 14:39:16 +03008046 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8047
8048 ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008049 if (bio)
8050 ret = submit_one_bio(bio, 0, bio_flags);
8051 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008052}
Chris Mason1832a6d2007-12-21 16:27:21 -05008053
Chris Mason39279cc2007-06-12 06:35:45 -04008054static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8055{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008056 struct inode *inode = page->mapping->host;
8057 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008058
8059 if (current->flags & PF_MEMALLOC) {
8060 redirty_page_for_writepage(wbc, page);
8061 unlock_page(page);
8062 return 0;
8063 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008064
8065 /*
8066 * If we are under memory pressure we will call this directly from the
8067 * VM, we need to make sure we have the inode referenced for the ordered
8068 * extent. If not just return like we didn't do anything.
8069 */
8070 if (!igrab(inode)) {
8071 redirty_page_for_writepage(wbc, page);
8072 return AOP_WRITEPAGE_ACTIVATE;
8073 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008074 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008075 btrfs_add_delayed_iput(inode);
8076 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008077}
Chris Mason39279cc2007-06-12 06:35:45 -04008078
Eric Sandeen48a3b632013-04-25 20:41:01 +00008079static int btrfs_writepages(struct address_space *mapping,
8080 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008081{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008082 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008083}
8084
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008085static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008086{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008087 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008088}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008089
Chris Masone6dcd2d2008-07-17 12:53:50 -04008090static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008091{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008092 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008093 if (ret == 1)
8094 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008095 return ret;
8096}
Chris Mason39279cc2007-06-12 06:35:45 -04008097
Chris Masone6dcd2d2008-07-17 12:53:50 -04008098static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8099{
Chris Mason98509cf2008-09-11 15:51:43 -04008100 if (PageWriteback(page) || PageDirty(page))
8101 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008102 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008103}
8104
Roman Gushchinf8e66082020-03-04 16:57:35 -08008105#ifdef CONFIG_MIGRATION
8106static int btrfs_migratepage(struct address_space *mapping,
8107 struct page *newpage, struct page *page,
8108 enum migrate_mode mode)
8109{
8110 int ret;
8111
8112 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8113 if (ret != MIGRATEPAGE_SUCCESS)
8114 return ret;
8115
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008116 if (page_has_private(page))
8117 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008118
8119 if (PagePrivate2(page)) {
8120 ClearPagePrivate2(page);
8121 SetPagePrivate2(newpage);
8122 }
8123
8124 if (mode != MIGRATE_SYNC_NO_COPY)
8125 migrate_page_copy(newpage, page);
8126 else
8127 migrate_page_states(newpage, page);
8128 return MIGRATEPAGE_SUCCESS;
8129}
8130#endif
8131
Lukas Czernerd47992f2013-05-21 23:17:23 -04008132static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8133 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008134{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008135 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8136 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008137 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008138 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008139 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008140 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308141 u64 start;
8142 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008143 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008144
Chris Mason8b62b722009-09-02 16:53:46 -04008145 /*
8146 * we have the page locked, so new writeback can't start,
8147 * and the dirty bit won't be cleared while we are here.
8148 *
8149 * Wait for IO on this page so that we can safely clear
8150 * the PagePrivate2 bit and do ordered accounting
8151 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008152 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008153
Chris Masone6dcd2d2008-07-17 12:53:50 -04008154 if (offset) {
8155 btrfs_releasepage(page, GFP_NOFS);
8156 return;
8157 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008158
8159 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008160 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308161again:
8162 start = page_start;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008163 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008164 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008165 end = min(page_end,
8166 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008167 /*
8168 * IO on this page will never be started, so we need
8169 * to account for any ordered extents now
8170 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008171 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308172 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008173 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008174 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008175 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008176 /*
8177 * whoever cleared the private bit is responsible
8178 * for the finish_ordered_io
8179 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008180 if (TestClearPagePrivate2(page)) {
8181 struct btrfs_ordered_inode_tree *tree;
8182 u64 new_len;
8183
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008184 tree = &inode->ordered_tree;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008185
8186 spin_lock_irq(&tree->lock);
8187 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308188 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008189 if (new_len < ordered->truncated_len)
8190 ordered->truncated_len = new_len;
8191 spin_unlock_irq(&tree->lock);
8192
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008193 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8194 start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308195 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008196 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008197 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008198 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008199 if (!inode_evicting) {
8200 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308201 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008202 &cached_state);
8203 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308204
8205 start = end + 1;
8206 if (start < page_end)
8207 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008208 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008209
Qu Wenruob9d0b382015-09-29 10:35:16 +08008210 /*
8211 * Qgroup reserved space handler
8212 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008213 * 1) Already written to disk or ordered extent already submitted
8214 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8215 * Qgroup will be handled by its qgroup_record then.
8216 * btrfs_qgroup_free_data() call will do nothing here.
8217 *
8218 * 2) Not written to disk yet
8219 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8220 * bit of its io_tree, and free the qgroup reserved data space.
8221 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008222 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008223 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008224 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008225 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008226 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8227 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008228 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008229
8230 __btrfs_releasepage(page, GFP_NOFS);
8231 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008232
Chris Mason4a096752008-07-21 10:29:44 -04008233 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008234 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008235}
8236
Chris Mason9ebefb182007-06-15 13:50:00 -04008237/*
8238 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8239 * called from a page fault handler when a page is first dirtied. Hence we must
8240 * be careful to check for EOF conditions here. We set the page up correctly
8241 * for a written page which means we get ENOSPC checking when writing into
8242 * holes and correct delalloc and unwritten extent mapping on filesystems that
8243 * support these features.
8244 *
8245 * We are not allowed to take the i_mutex here so we have to play games to
8246 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008247 * truncate_setsize() writes the inode size before removing pages, once we have
8248 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008249 * beyond EOF, then the page is guaranteed safe against truncation until we
8250 * unlock the page.
8251 */
Souptick Joardera528a242018-06-06 19:54:44 +05308252vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008253{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008254 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008255 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008256 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008257 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8258 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008259 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008260 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008261 char *kaddr;
8262 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008263 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308264 vm_fault_t ret;
8265 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008266 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308267 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008268 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008269 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308270 u64 end;
8271
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008272 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008273
Jan Karab2b5ef52012-06-12 16:20:45 +02008274 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008275 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008276 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308277 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008278
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308279 /*
8280 * Reserving delalloc space after obtaining the page lock can lead to
8281 * deadlock. For example, if a dirty page is locked by this function
8282 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8283 * dirty page write out, then the btrfs_writepage() function could
8284 * end up waiting indefinitely to get a lock on the page currently
8285 * being processed by btrfs_page_mkwrite() function.
8286 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008287 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8288 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308289 if (!ret2) {
8290 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008291 reserved = 1;
8292 }
Souptick Joardera528a242018-06-06 19:54:44 +05308293 if (ret2) {
8294 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008295 if (reserved)
8296 goto out;
8297 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008298 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008299
Nick Piggin56a76f82009-03-31 15:23:23 -07008300 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008301again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008302 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008303 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008304
Chris Mason9ebefb182007-06-15 13:50:00 -04008305 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008306 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008307 /* page got truncated out from underneath us */
8308 goto out_unlock;
8309 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008310 wait_on_page_writeback(page);
8311
David Sterbaff13db42015-12-03 14:30:40 +01008312 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008313 set_page_extent_mapped(page);
8314
Chris Masoneb84ae02008-07-17 13:53:27 -04008315 /*
8316 * we can't set the delalloc bits if there are pending ordered
8317 * extents. Drop our locks and wait for them to finish
8318 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008319 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8320 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008321 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008322 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008323 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008324 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008325 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008326 btrfs_put_ordered_extent(ordered);
8327 goto again;
8328 }
8329
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008330 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008331 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008332 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008333 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308334 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008335 btrfs_delalloc_release_space(BTRFS_I(inode),
8336 data_reserved, page_start,
8337 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308338 }
8339 }
8340
Josef Bacikfbf19082009-10-01 17:10:23 -04008341 /*
Liu Bo54160342016-12-13 12:15:19 -08008342 * page_mkwrite gets called when the page is firstly dirtied after it's
8343 * faulted in, but write(2) could also dirty a page and set delalloc
8344 * bits, thus in this case for space account reason, we still need to
8345 * clear any delalloc bits within this page range since we have to
8346 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008347 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308348 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008349 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8350 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008351
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008352 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008353 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308354 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008355 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008356 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008357 ret = VM_FAULT_SIGBUS;
8358 goto out_unlock;
8359 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008360
8361 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008362 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008363 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008364 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008365 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008366
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008367 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008368 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008369 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008370 flush_dcache_page(page);
8371 kunmap(page);
8372 }
Chris Mason247e7432008-07-17 12:53:51 -04008373 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008374 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008375 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008376
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008377 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008378 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008379 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008380
David Sterbae43bbe52017-12-12 21:43:52 +01008381 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008382
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008383 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8384 sb_end_pagefault(inode->i_sb);
8385 extent_changeset_free(data_reserved);
8386 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008387
8388out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008389 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008390out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008391 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008392 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008393 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008394out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008395 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008396 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008397 return ret;
8398}
8399
Filipe Manana213e8c52018-02-06 20:40:31 +00008400static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008401{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008402 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008403 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008404 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008405 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008406 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008407 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008408 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008409
Filipe Manana213e8c52018-02-06 20:40:31 +00008410 if (!skip_writeback) {
8411 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8412 (u64)-1);
8413 if (ret)
8414 return ret;
8415 }
Chris Mason39279cc2007-06-12 06:35:45 -04008416
Josef Bacikfcb80c22011-05-03 10:40:22 -04008417 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008418 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8419 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008420 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008421 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008422 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008423 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008424 * be free'd up by the truncate operation, but also have some slack
8425 * space reserved in case it uses space during the truncate (thank you
8426 * very much snapshotting).
8427 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008428 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008429 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008430 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008431 * doesn't end up using space reserved for updating the inode. We also
8432 * need to be able to stop the transaction and start a new one, which
8433 * means we need to be able to update the inode several times, and we
8434 * have no idea of knowing how many times that will be, so we can't just
8435 * reserve 1 item for the entirety of the operation, so that has to be
8436 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008437 *
8438 * So that leaves us with
8439 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008440 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008441 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008442 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008443 * updating the inode.
8444 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008445 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008446 if (!rsv)
8447 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008448 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008449 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008450
Josef Bacik907cbce2011-08-08 13:46:15 -04008451 /*
Josef Bacik07127182011-08-19 10:29:59 -04008452 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008453 * 1 for updating the inode.
8454 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008455 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008456 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008457 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008458 goto out;
8459 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008460
Josef Bacik907cbce2011-08-08 13:46:15 -04008461 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008462 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008463 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008464 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008465
Chris Mason5a3f23d2009-03-31 13:27:11 -04008466 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008467 * So if we truncate and then write and fsync we normally would just
8468 * write the extents that changed, which is a problem if we need to
8469 * first truncate that entire inode. So set this flag so we write out
8470 * all of the extents in the inode to the sync log so we're completely
8471 * safe.
8472 */
8473 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008474 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008475
Yan, Zheng80825102009-11-12 09:35:36 +00008476 while (1) {
8477 ret = btrfs_truncate_inode_items(trans, root, inode,
8478 inode->i_size,
8479 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008480 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008481 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008482 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008483
Yan, Zheng80825102009-11-12 09:35:36 +00008484 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008485 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008486 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008487
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008488 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008489 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008490
8491 trans = btrfs_start_transaction(root, 2);
8492 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008493 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008494 trans = NULL;
8495 break;
8496 }
8497
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008498 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008499 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008500 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008501 BUG_ON(ret); /* shouldn't happen */
8502 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008503 }
8504
Josef Bacikddfae632017-10-19 14:16:02 -04008505 /*
8506 * We can't call btrfs_truncate_block inside a trans handle as we could
8507 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8508 * we've truncated everything except the last little bit, and can do
8509 * btrfs_truncate_block and then update the disk_i_size.
8510 */
8511 if (ret == NEED_TRUNCATE_BLOCK) {
8512 btrfs_end_transaction(trans);
8513 btrfs_btree_balance_dirty(fs_info);
8514
8515 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8516 if (ret)
8517 goto out;
8518 trans = btrfs_start_transaction(root, 1);
8519 if (IS_ERR(trans)) {
8520 ret = PTR_ERR(trans);
8521 goto out;
8522 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008523 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008524 }
8525
Chris Mason917c16b2011-11-08 14:49:59 -05008526 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008527 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008528
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008529 trans->block_rsv = &fs_info->trans_block_rsv;
8530 ret2 = btrfs_update_inode(trans, root, inode);
8531 if (ret2 && !ret)
8532 ret = ret2;
8533
8534 ret2 = btrfs_end_transaction(trans);
8535 if (ret2 && !ret)
8536 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008537 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008538 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008539out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008540 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008541
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008542 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008543}
8544
Sven Wegener3b963622008-06-09 21:57:42 -04008545/*
Chris Masond352ac62008-09-29 15:18:18 -04008546 * create a new subvolume directory/inode (helper for the ioctl).
8547 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008548int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008549 struct btrfs_root *new_root,
8550 struct btrfs_root *parent_root,
8551 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008552{
Chris Mason39279cc2007-06-12 06:35:45 -04008553 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008554 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008555 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008556
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008557 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8558 new_dirid, new_dirid,
8559 S_IFDIR | (~current_umask() & S_IRWXUGO),
8560 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008561 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008562 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008563 inode->i_op = &btrfs_dir_inode_operations;
8564 inode->i_fop = &btrfs_dir_file_operations;
8565
Miklos Szeredibfe86842011-10-28 14:13:29 +02008566 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008567 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008568 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008569
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008570 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8571 if (err)
8572 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008573 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008574 new_root->root_key.objectid, err);
8575
Yan, Zheng76dda932009-09-21 16:00:26 -04008576 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008577
Yan, Zheng76dda932009-09-21 16:00:26 -04008578 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008579 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008580}
8581
Chris Mason39279cc2007-06-12 06:35:45 -04008582struct inode *btrfs_alloc_inode(struct super_block *sb)
8583{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008584 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008585 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008586 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008587
David Sterba712e36c2017-10-31 17:08:27 +01008588 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008589 if (!ei)
8590 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008591
8592 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008593 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008594 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008595 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008596 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008597 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008598 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008599 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008600 ei->disk_i_size = 0;
8601 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008602 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008603 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008604 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008605 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008606 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008607 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008608
Josef Bacik9e0baf62011-07-15 15:16:44 +00008609 spin_lock_init(&ei->lock);
8610 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008611 if (sb->s_magic != BTRFS_TEST_MAGIC)
8612 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8613 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008614 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008615 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008616 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008617
Miao Xie16cdcec2011-04-22 18:12:22 +08008618 ei->delayed_node = NULL;
8619
chandan r9cc97d62012-07-04 12:48:07 +05308620 ei->i_otime.tv_sec = 0;
8621 ei->i_otime.tv_nsec = 0;
8622
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008623 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008624 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008625 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8626 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8627 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008628 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8629 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008630 ei->io_tree.track_uptodate = true;
8631 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008632 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008633 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008634 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008635 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008636 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008637 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008638 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008639
8640 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008641}
8642
Josef Bacikaaedb552013-10-11 14:44:09 -04008643#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8644void btrfs_test_destroy_inode(struct inode *inode)
8645{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008646 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008647 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8648}
8649#endif
8650
Al Viro26602ca2019-04-10 15:14:41 -04008651void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008652{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008653 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8654}
8655
Nikolay Borisov633cc812020-09-18 12:15:49 +03008656void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008657{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008658 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008659 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8660 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008661
Nikolay Borisov633cc812020-09-18 12:15:49 +03008662 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8663 WARN_ON(vfs_inode->i_data.nrpages);
8664 WARN_ON(inode->block_rsv.reserved);
8665 WARN_ON(inode->block_rsv.size);
8666 WARN_ON(inode->outstanding_extents);
8667 WARN_ON(inode->delalloc_bytes);
8668 WARN_ON(inode->new_delalloc_bytes);
8669 WARN_ON(inode->csum_bytes);
8670 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008671
Chris Mason5a3f23d2009-03-31 13:27:11 -04008672 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008673 * This can happen where we create an inode, but somebody else also
8674 * created the same inode and we need to destroy the one we already
8675 * created.
8676 */
8677 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008678 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008679
Chris Masond3977122009-01-05 21:25:51 -05008680 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008681 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008682 if (!ordered)
8683 break;
8684 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008685 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008686 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008687 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008688 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008689 btrfs_put_ordered_extent(ordered);
8690 btrfs_put_ordered_extent(ordered);
8691 }
8692 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008693 btrfs_qgroup_check_reserved_leak(inode);
8694 inode_tree_del(inode);
8695 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8696 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8697 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008698}
8699
Al Viro45321ac2010-06-07 13:43:19 -04008700int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008701{
8702 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008703
Naohiro Aota6379ef92013-06-06 09:56:34 +00008704 if (root == NULL)
8705 return 1;
8706
Liu Bofa6ac872013-02-20 14:10:23 +00008707 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008708 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008709 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008710 else
Al Viro45321ac2010-06-07 13:43:19 -04008711 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008712}
8713
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008714static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008715{
8716 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8717
8718 inode_init_once(&ei->vfs_inode);
8719}
8720
David Sterbae67c7182018-02-19 17:24:18 +01008721void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008722{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008723 /*
8724 * Make sure all delayed rcu free inodes are flushed before we
8725 * destroy cache.
8726 */
8727 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008728 kmem_cache_destroy(btrfs_inode_cachep);
8729 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008730 kmem_cache_destroy(btrfs_path_cachep);
8731 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008732 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008733}
8734
Liu Bof5c29bd2017-11-02 17:21:50 -06008735int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008736{
David Sterba837e1972012-09-07 03:00:48 -06008737 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008738 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008739 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8740 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008741 if (!btrfs_inode_cachep)
8742 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008743
David Sterba837e1972012-09-07 03:00:48 -06008744 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008745 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008746 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008747 if (!btrfs_trans_handle_cachep)
8748 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008749
David Sterba837e1972012-09-07 03:00:48 -06008750 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008751 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008752 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008753 if (!btrfs_path_cachep)
8754 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008755
David Sterba837e1972012-09-07 03:00:48 -06008756 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008757 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008758 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008759 if (!btrfs_free_space_cachep)
8760 goto fail;
8761
Christophe Leroy3acd4852019-08-21 15:05:55 +00008762 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8763 PAGE_SIZE, PAGE_SIZE,
8764 SLAB_RED_ZONE, NULL);
8765 if (!btrfs_free_space_bitmap_cachep)
8766 goto fail;
8767
Chris Mason39279cc2007-06-12 06:35:45 -04008768 return 0;
8769fail:
8770 btrfs_destroy_cachep();
8771 return -ENOMEM;
8772}
8773
David Howellsa528d352017-01-31 16:46:22 +00008774static int btrfs_getattr(const struct path *path, struct kstat *stat,
8775 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008776{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008777 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008778 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008779 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008780 u32 bi_flags = BTRFS_I(inode)->flags;
8781
8782 stat->result_mask |= STATX_BTIME;
8783 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8784 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8785 if (bi_flags & BTRFS_INODE_APPEND)
8786 stat->attributes |= STATX_ATTR_APPEND;
8787 if (bi_flags & BTRFS_INODE_COMPRESS)
8788 stat->attributes |= STATX_ATTR_COMPRESSED;
8789 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8790 stat->attributes |= STATX_ATTR_IMMUTABLE;
8791 if (bi_flags & BTRFS_INODE_NODUMP)
8792 stat->attributes |= STATX_ATTR_NODUMP;
8793
8794 stat->attributes_mask |= (STATX_ATTR_APPEND |
8795 STATX_ATTR_COMPRESSED |
8796 STATX_ATTR_IMMUTABLE |
8797 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008798
Chris Mason39279cc2007-06-12 06:35:45 -04008799 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008800 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008801
8802 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008803 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008804 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008805 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008806 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008807 return 0;
8808}
8809
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008810static int btrfs_rename_exchange(struct inode *old_dir,
8811 struct dentry *old_dentry,
8812 struct inode *new_dir,
8813 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008814{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008815 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008816 struct btrfs_trans_handle *trans;
8817 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008818 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008819 struct inode *new_inode = new_dentry->d_inode;
8820 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008821 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008822 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8823 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008824 u64 old_idx = 0;
8825 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008826 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01008827 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008828 bool root_log_pinned = false;
8829 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008830
8831 /* we only allow rename subvolume link between subvolumes */
8832 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8833 return -EXDEV;
8834
8835 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008836 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8837 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008838 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008839
8840 /*
8841 * We want to reserve the absolute worst case amount of items. So if
8842 * both inodes are subvols and we need to unlink them then that would
8843 * require 4 item modifications, but if they are both normal inodes it
8844 * would require 5 item modifications, so we'll assume their normal
8845 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8846 * should cover the worst case number of items we'll modify.
8847 */
8848 trans = btrfs_start_transaction(root, 12);
8849 if (IS_ERR(trans)) {
8850 ret = PTR_ERR(trans);
8851 goto out_notrans;
8852 }
8853
Josef Bacik3e174092019-11-15 15:43:06 -05008854 if (dest != root)
8855 btrfs_record_root_in_trans(trans, dest);
8856
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008857 /*
8858 * We need to find a free sequence number both in the source and
8859 * in the destination directory for the exchange.
8860 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008861 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008862 if (ret)
8863 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008864 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008865 if (ret)
8866 goto out_fail;
8867
8868 BTRFS_I(old_inode)->dir_index = 0ULL;
8869 BTRFS_I(new_inode)->dir_index = 0ULL;
8870
8871 /* Reference for the source. */
8872 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8873 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008874 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008875 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008876 btrfs_pin_log_trans(root);
8877 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008878 ret = btrfs_insert_inode_ref(trans, dest,
8879 new_dentry->d_name.name,
8880 new_dentry->d_name.len,
8881 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008882 btrfs_ino(BTRFS_I(new_dir)),
8883 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008884 if (ret)
8885 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008886 }
8887
8888 /* And now for the dest. */
8889 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8890 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008891 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008892 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008893 btrfs_pin_log_trans(dest);
8894 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008895 ret = btrfs_insert_inode_ref(trans, root,
8896 old_dentry->d_name.name,
8897 old_dentry->d_name.len,
8898 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008899 btrfs_ino(BTRFS_I(old_dir)),
8900 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008901 if (ret)
8902 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008903 }
8904
8905 /* Update inode version and ctime/mtime. */
8906 inode_inc_iversion(old_dir);
8907 inode_inc_iversion(new_dir);
8908 inode_inc_iversion(old_inode);
8909 inode_inc_iversion(new_inode);
8910 old_dir->i_ctime = old_dir->i_mtime = ctime;
8911 new_dir->i_ctime = new_dir->i_mtime = ctime;
8912 old_inode->i_ctime = ctime;
8913 new_inode->i_ctime = ctime;
8914
8915 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008916 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8917 BTRFS_I(old_inode), 1);
8918 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8919 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008920 }
8921
8922 /* src is a subvolume */
8923 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008924 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008925 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008926 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8927 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008928 old_dentry->d_name.name,
8929 old_dentry->d_name.len);
8930 if (!ret)
8931 ret = btrfs_update_inode(trans, root, old_inode);
8932 }
8933 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008934 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008935 goto out_fail;
8936 }
8937
8938 /* dest is a subvolume */
8939 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008940 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008941 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008942 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8943 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008944 new_dentry->d_name.name,
8945 new_dentry->d_name.len);
8946 if (!ret)
8947 ret = btrfs_update_inode(trans, dest, new_inode);
8948 }
8949 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008950 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008951 goto out_fail;
8952 }
8953
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008954 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008955 new_dentry->d_name.name,
8956 new_dentry->d_name.len, 0, old_idx);
8957 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008958 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008959 goto out_fail;
8960 }
8961
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008962 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008963 old_dentry->d_name.name,
8964 old_dentry->d_name.len, 0, new_idx);
8965 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008966 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008967 goto out_fail;
8968 }
8969
8970 if (old_inode->i_nlink == 1)
8971 BTRFS_I(old_inode)->dir_index = old_idx;
8972 if (new_inode->i_nlink == 1)
8973 BTRFS_I(new_inode)->dir_index = new_idx;
8974
Filipe Manana86e8aa02016-05-05 02:02:27 +01008975 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01008976 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
8977 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008978 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008979 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008980 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008981 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01008982 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
8983 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008984 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008985 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008986 }
8987out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008988 /*
8989 * If we have pinned a log and an error happened, we unpin tasks
8990 * trying to sync the log and force them to fallback to a transaction
8991 * commit if the log currently contains any of the inodes involved in
8992 * this rename operation (to ensure we do not persist a log with an
8993 * inconsistent state for any of these inodes or leading to any
8994 * inconsistencies when replayed). If the transaction was aborted, the
8995 * abortion reason is propagated to userspace when attempting to commit
8996 * the transaction. If the log does not contain any of these inodes, we
8997 * allow the tasks to sync it.
8998 */
8999 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009000 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9001 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9002 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009003 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009004 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009005 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009006
9007 if (root_log_pinned) {
9008 btrfs_end_log_trans(root);
9009 root_log_pinned = false;
9010 }
9011 if (dest_log_pinned) {
9012 btrfs_end_log_trans(dest);
9013 dest_log_pinned = false;
9014 }
9015 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009016 ret2 = btrfs_end_transaction(trans);
9017 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009018out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009019 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9020 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009021 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009022
9023 return ret;
9024}
9025
9026static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9027 struct btrfs_root *root,
9028 struct inode *dir,
9029 struct dentry *dentry)
9030{
9031 int ret;
9032 struct inode *inode;
9033 u64 objectid;
9034 u64 index;
9035
9036 ret = btrfs_find_free_ino(root, &objectid);
9037 if (ret)
9038 return ret;
9039
9040 inode = btrfs_new_inode(trans, root, dir,
9041 dentry->d_name.name,
9042 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009043 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009044 objectid,
9045 S_IFCHR | WHITEOUT_MODE,
9046 &index);
9047
9048 if (IS_ERR(inode)) {
9049 ret = PTR_ERR(inode);
9050 return ret;
9051 }
9052
9053 inode->i_op = &btrfs_special_inode_operations;
9054 init_special_inode(inode, inode->i_mode,
9055 WHITEOUT_DEV);
9056
9057 ret = btrfs_init_inode_security(trans, inode, dir,
9058 &dentry->d_name);
9059 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009060 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009061
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009062 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9063 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009064 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009065 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009066
9067 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009068out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009069 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009070 if (ret)
9071 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009072 iput(inode);
9073
Filipe Mananac9901612016-05-05 01:41:57 +01009074 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009075}
9076
Chris Mason39279cc2007-06-12 06:35:45 -04009077static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009078 struct inode *new_dir, struct dentry *new_dentry,
9079 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009080{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009081 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009082 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009083 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009084 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9085 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009086 struct inode *new_inode = d_inode(new_dentry);
9087 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009088 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009089 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009090 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009091 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009092 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009093
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009094 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009095 return -EPERM;
9096
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009097 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009098 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009099 return -EXDEV;
9100
Li Zefan33345d012011-04-20 10:31:50 +08009101 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009102 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009103 return -ENOTEMPTY;
9104
Chris Mason39279cc2007-06-12 06:35:45 -04009105 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009106 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009107 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009108
9109
9110 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009111 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009112 new_dentry->d_name.name,
9113 new_dentry->d_name.len);
9114
9115 if (ret) {
9116 if (ret == -EEXIST) {
9117 /* we shouldn't get
9118 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309119 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009120 return ret;
9121 }
9122 } else {
9123 /* maybe -EOVERFLOW */
9124 return ret;
9125 }
9126 }
9127 ret = 0;
9128
Chris Mason5a3f23d2009-03-31 13:27:11 -04009129 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009130 * we're using rename to replace one file with another. Start IO on it
9131 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009132 */
Chris Mason8d875f92014-08-12 10:47:42 -07009133 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009134 filemap_flush(old_inode->i_mapping);
9135
Yan, Zheng76dda932009-09-21 16:00:26 -04009136 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009137 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009138 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009139 /*
9140 * We want to reserve the absolute worst case amount of items. So if
9141 * both inodes are subvols and we need to unlink them then that would
9142 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009143 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009144 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9145 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009146 * If our rename has the whiteout flag, we need more 5 units for the
9147 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9148 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009149 */
Filipe Manana5062af32016-05-05 10:26:26 +01009150 trans_num_items = 11;
9151 if (flags & RENAME_WHITEOUT)
9152 trans_num_items += 5;
9153 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009154 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009155 ret = PTR_ERR(trans);
9156 goto out_notrans;
9157 }
Chris Mason5f39d392007-10-15 16:14:19 -04009158
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009159 if (dest != root)
9160 btrfs_record_root_in_trans(trans, dest);
9161
Nikolay Borisov877574e2017-02-20 13:50:33 +02009162 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009163 if (ret)
9164 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009165
Miao Xie67de1172013-12-26 13:07:06 +08009166 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009167 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009168 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009169 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009170 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009171 btrfs_pin_log_trans(root);
9172 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009173 ret = btrfs_insert_inode_ref(trans, dest,
9174 new_dentry->d_name.name,
9175 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009176 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009177 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009178 if (ret)
9179 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009180 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009181
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009182 inode_inc_iversion(old_dir);
9183 inode_inc_iversion(new_dir);
9184 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009185 old_dir->i_ctime = old_dir->i_mtime =
9186 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009187 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009188
Chris Mason12fcfd22009-03-24 10:24:20 -04009189 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009190 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9191 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009192
Li Zefan33345d012011-04-20 10:31:50 +08009193 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009194 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009195 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009196 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9197 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009198 old_dentry->d_name.name,
9199 old_dentry->d_name.len);
9200 if (!ret)
9201 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009202 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009203 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009204 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009205 goto out_fail;
9206 }
Chris Mason39279cc2007-06-12 06:35:45 -04009207
9208 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009209 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009210 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009211 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009212 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009213 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009214 BUG_ON(new_inode->i_nlink == 0);
9215 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009216 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9217 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009218 new_dentry->d_name.name,
9219 new_dentry->d_name.len);
9220 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009221 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009222 ret = btrfs_orphan_add(trans,
9223 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009224 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009225 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009226 goto out_fail;
9227 }
Chris Mason39279cc2007-06-12 06:35:45 -04009228 }
Josef Bacikaec74772008-07-24 12:12:38 -04009229
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009230 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009231 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009232 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009233 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009234 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009235 goto out_fail;
9236 }
Chris Mason39279cc2007-06-12 06:35:45 -04009237
Miao Xie67de1172013-12-26 13:07:06 +08009238 if (old_inode->i_nlink == 1)
9239 BTRFS_I(old_inode)->dir_index = index;
9240
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009241 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009242 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9243 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009244 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009245 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009246 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009247
9248 if (flags & RENAME_WHITEOUT) {
9249 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9250 old_dentry);
9251
9252 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009253 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009254 goto out_fail;
9255 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009256 }
Chris Mason39279cc2007-06-12 06:35:45 -04009257out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009258 /*
9259 * If we have pinned the log and an error happened, we unpin tasks
9260 * trying to sync the log and force them to fallback to a transaction
9261 * commit if the log currently contains any of the inodes involved in
9262 * this rename operation (to ensure we do not persist a log with an
9263 * inconsistent state for any of these inodes or leading to any
9264 * inconsistencies when replayed). If the transaction was aborted, the
9265 * abortion reason is propagated to userspace when attempting to commit
9266 * the transaction. If the log does not contain any of these inodes, we
9267 * allow the tasks to sync it.
9268 */
9269 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009270 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9271 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9272 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009273 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009274 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009275 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009276
9277 btrfs_end_log_trans(root);
9278 log_pinned = false;
9279 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009280 ret2 = btrfs_end_transaction(trans);
9281 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009282out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009283 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009284 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009285
Chris Mason39279cc2007-06-12 06:35:45 -04009286 return ret;
9287}
9288
Miklos Szeredi80ace852014-07-23 15:15:32 +02009289static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9290 struct inode *new_dir, struct dentry *new_dentry,
9291 unsigned int flags)
9292{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009293 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009294 return -EINVAL;
9295
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009296 if (flags & RENAME_EXCHANGE)
9297 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9298 new_dentry);
9299
9300 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009301}
9302
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009303struct btrfs_delalloc_work {
9304 struct inode *inode;
9305 struct completion completion;
9306 struct list_head list;
9307 struct btrfs_work work;
9308};
9309
Miao Xie8ccf6f192012-10-25 09:28:04 +00009310static void btrfs_run_delalloc_work(struct btrfs_work *work)
9311{
9312 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009313 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009314
9315 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9316 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009317 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009318 filemap_flush(inode->i_mapping);
9319 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9320 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009321 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009322
Nikolay Borisov076da912018-04-23 10:54:16 +03009323 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009324 complete(&delalloc_work->completion);
9325}
9326
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009327static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009328{
9329 struct btrfs_delalloc_work *work;
9330
David Sterba100d5702015-12-08 14:39:32 +01009331 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009332 if (!work)
9333 return NULL;
9334
9335 init_completion(&work->completion);
9336 INIT_LIST_HEAD(&work->list);
9337 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009338 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009339
9340 return work;
9341}
9342
Chris Masond352ac62008-09-29 15:18:18 -04009343/*
9344 * some fairly slow code that needs optimization. This walks the list
9345 * of all the inodes with pending delalloc and forces them to disk.
9346 */
Josef Bacikb4912132020-07-21 10:22:12 -04009347static int start_delalloc_inodes(struct btrfs_root *root, u64 *nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009348{
Chris Masonea8c2812008-08-04 23:17:27 -04009349 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009350 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009351 struct btrfs_delalloc_work *work, *next;
9352 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009353 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009354 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009355
Miao Xie8ccf6f192012-10-25 09:28:04 +00009356 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009357 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009358
Miao Xie573bfb72014-03-06 13:55:03 +08009359 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009360 spin_lock(&root->delalloc_lock);
9361 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009362 while (!list_empty(&splice)) {
9363 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009364 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009365
Miao Xieeb73c1b2013-05-15 07:48:22 +00009366 list_move_tail(&binode->delalloc_inodes,
9367 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009368 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009369 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009370 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009371 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009372 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009373 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009374
Ethan Lien3cd24c62018-11-01 14:49:03 +08009375 if (snapshot)
9376 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9377 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009378 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009379 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009380 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009381 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009382 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009383 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009384 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009385 btrfs_queue_work(root->fs_info->flush_workers,
9386 &work->work);
Josef Bacikb4912132020-07-21 10:22:12 -04009387 if (*nr != U64_MAX) {
9388 (*nr)--;
9389 if (*nr == 0)
9390 goto out;
9391 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009392 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009393 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009394 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009395 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009396
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009397out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009398 list_for_each_entry_safe(work, next, &works, list) {
9399 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009400 wait_for_completion(&work->completion);
9401 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009402 }
9403
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009404 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009405 spin_lock(&root->delalloc_lock);
9406 list_splice_tail(&splice, &root->delalloc_inodes);
9407 spin_unlock(&root->delalloc_lock);
9408 }
Miao Xie573bfb72014-03-06 13:55:03 +08009409 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009410 return ret;
9411}
9412
Ethan Lien3cd24c62018-11-01 14:49:03 +08009413int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009414{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009415 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb4912132020-07-21 10:22:12 -04009416 u64 nr = U64_MAX;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009417
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009418 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009419 return -EROFS;
9420
Josef Bacikb4912132020-07-21 10:22:12 -04009421 return start_delalloc_inodes(root, &nr, true);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009422}
9423
Josef Bacikb4912132020-07-21 10:22:12 -04009424int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009425{
9426 struct btrfs_root *root;
9427 struct list_head splice;
9428 int ret;
9429
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009430 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009431 return -EROFS;
9432
9433 INIT_LIST_HEAD(&splice);
9434
Miao Xie573bfb72014-03-06 13:55:03 +08009435 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009436 spin_lock(&fs_info->delalloc_root_lock);
9437 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009438 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009439 root = list_first_entry(&splice, struct btrfs_root,
9440 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009441 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009442 BUG_ON(!root);
9443 list_move_tail(&root->delalloc_root,
9444 &fs_info->delalloc_roots);
9445 spin_unlock(&fs_info->delalloc_root_lock);
9446
Josef Bacikb4912132020-07-21 10:22:12 -04009447 ret = start_delalloc_inodes(root, &nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009448 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009449 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009450 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009451 spin_lock(&fs_info->delalloc_root_lock);
9452 }
9453 spin_unlock(&fs_info->delalloc_root_lock);
9454
Miao Xie6c255e62014-03-06 13:55:01 +08009455 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009456out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009457 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009458 spin_lock(&fs_info->delalloc_root_lock);
9459 list_splice_tail(&splice, &fs_info->delalloc_roots);
9460 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009461 }
Miao Xie573bfb72014-03-06 13:55:03 +08009462 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009463 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009464}
9465
Chris Mason39279cc2007-06-12 06:35:45 -04009466static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9467 const char *symname)
9468{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009469 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009470 struct btrfs_trans_handle *trans;
9471 struct btrfs_root *root = BTRFS_I(dir)->root;
9472 struct btrfs_path *path;
9473 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009474 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009475 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009476 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309477 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009478 int name_len;
9479 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009480 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009481 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009482 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009483
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009484 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009485 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009486 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009487
Josef Bacik9ed74f22009-09-11 16:12:44 -04009488 /*
9489 * 2 items for inode item and ref
9490 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009491 * 1 item for updating parent inode item
9492 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009493 * 1 item for xattr if selinux is on
9494 */
Filipe Manana9269d122015-12-31 18:16:29 +00009495 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009496 if (IS_ERR(trans))
9497 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009498
Li Zefan581bb052011-04-20 10:06:11 +08009499 err = btrfs_find_free_ino(root, &objectid);
9500 if (err)
9501 goto out_unlock;
9502
Josef Bacikaec74772008-07-24 12:12:38 -04009503 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009504 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9505 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009506 if (IS_ERR(inode)) {
9507 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009508 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009509 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009510 }
Chris Mason39279cc2007-06-12 06:35:45 -04009511
Casey Schauflerad19db72011-12-15 10:09:07 -05009512 /*
9513 * If the active LSM wants to access the inode during
9514 * d_instantiate it needs these. Smack checks to see
9515 * if the filesystem supports xattrs by looking at the
9516 * ops vector.
9517 */
9518 inode->i_fop = &btrfs_file_operations;
9519 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009520 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009521 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9522
9523 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9524 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009525 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009526
Chris Mason39279cc2007-06-12 06:35:45 -04009527 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009528 if (!path) {
9529 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009530 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009531 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009532 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009533 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009534 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009535 datasize = btrfs_file_extent_calc_inline_size(name_len);
9536 err = btrfs_insert_empty_item(trans, root, path, &key,
9537 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009538 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009539 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009540 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009541 }
Chris Mason5f39d392007-10-15 16:14:19 -04009542 leaf = path->nodes[0];
9543 ei = btrfs_item_ptr(leaf, path->slots[0],
9544 struct btrfs_file_extent_item);
9545 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9546 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009547 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009548 btrfs_set_file_extent_encryption(leaf, ei, 0);
9549 btrfs_set_file_extent_compression(leaf, ei, 0);
9550 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9551 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9552
Chris Mason39279cc2007-06-12 06:35:45 -04009553 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009554 write_extent_buffer(leaf, symname, ptr, name_len);
9555 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009556 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009557
Chris Mason39279cc2007-06-12 06:35:45 -04009558 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009559 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009560 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009561 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009562 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009563 /*
9564 * Last step, add directory indexes for our symlink inode. This is the
9565 * last step to avoid extra cleanup of these indexes if an error happens
9566 * elsewhere above.
9567 */
9568 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009569 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9570 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009571 if (err)
9572 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009573
Al Viro1e2e5472018-05-04 08:23:01 -04009574 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009575
9576out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009577 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009578 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009579 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009580 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009581 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009582 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009583 return err;
9584}
Chris Mason16432982008-04-10 10:23:21 -04009585
Filipe Manana8fccebf2020-09-08 11:27:20 +01009586static struct btrfs_trans_handle *insert_prealloc_file_extent(
9587 struct btrfs_trans_handle *trans_in,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009588 struct inode *inode, struct btrfs_key *ins,
9589 u64 file_offset)
9590{
9591 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009592 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009593 struct btrfs_trans_handle *trans = trans_in;
9594 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009595 u64 start = ins->objectid;
9596 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009597 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009598
9599 memset(&stack_fi, 0, sizeof(stack_fi));
9600
9601 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9602 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9603 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9604 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9605 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9606 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9607 /* Encryption and other encoding is reserved and all 0 */
9608
Nikolay Borisov72b7d152020-06-03 08:55:18 +03009609 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009610 if (ret < 0)
Filipe Manana8fccebf2020-09-08 11:27:20 +01009611 return ERR_PTR(ret);
9612
9613 if (trans) {
9614 ret = insert_reserved_file_extent(trans, BTRFS_I(inode),
9615 file_offset, &stack_fi, ret);
9616 if (ret)
9617 return ERR_PTR(ret);
9618 return trans;
9619 }
9620
9621 extent_info.disk_offset = start;
9622 extent_info.disk_len = len;
9623 extent_info.data_offset = 0;
9624 extent_info.data_len = len;
9625 extent_info.file_offset = file_offset;
9626 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009627 extent_info.is_new_extent = true;
9628 extent_info.qgroup_reserved = ret;
9629 extent_info.insertions = 0;
9630
9631 path = btrfs_alloc_path();
9632 if (!path)
9633 return ERR_PTR(-ENOMEM);
9634
Filipe Manana306bfec2020-09-08 11:27:23 +01009635 ret = btrfs_replace_file_extents(inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009636 file_offset + len - 1, &extent_info,
9637 &trans);
9638 btrfs_free_path(path);
9639 if (ret)
9640 return ERR_PTR(ret);
9641
9642 return trans;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009643}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009644
Josef Bacik0af3d002010-06-21 14:48:16 -04009645static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9646 u64 start, u64 num_bytes, u64 min_size,
9647 loff_t actual_len, u64 *alloc_hint,
9648 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009649{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009650 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009651 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9652 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009653 struct btrfs_root *root = BTRFS_I(inode)->root;
9654 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009655 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009656 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009657 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009658 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009659 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009660 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009661 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009662 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009663
Josef Bacik0af3d002010-06-21 14:48:16 -04009664 if (trans)
9665 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009666 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009667 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009668 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009669 /*
9670 * If we are severely fragmented we could end up with really
9671 * small allocations, so if the allocator is returning small
9672 * chunks lets make its job easier by only searching for those
9673 * sized chunks.
9674 */
9675 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009676 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9677 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009678 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -04009679 break;
Josef Bacikb778cf92020-02-13 10:47:31 -05009680
9681 /*
9682 * We've reserved this space, and thus converted it from
9683 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9684 * from here on out we will only need to clear our reservation
9685 * for the remaining unreserved area, so advance our
9686 * clear_offset by our extent size.
9687 */
9688 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009689 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009690
Josef Bacik0b670dc2015-09-23 17:11:16 -04009691 last_alloc = ins.offset;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009692 trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
9693 if (IS_ERR(trans)) {
9694 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009695 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009696 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009697 break;
9698 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009699
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009700 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009701 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009702
Josef Bacik5dc562c2012-08-17 13:14:17 -04009703 em = alloc_extent_map();
9704 if (!em) {
9705 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9706 &BTRFS_I(inode)->runtime_flags);
9707 goto next;
9708 }
9709
9710 em->start = cur_offset;
9711 em->orig_start = cur_offset;
9712 em->len = ins.offset;
9713 em->block_start = ins.objectid;
9714 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009715 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009716 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009717 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9718 em->generation = trans->transid;
9719
9720 while (1) {
9721 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009722 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009723 write_unlock(&em_tree->lock);
9724 if (ret != -EEXIST)
9725 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009726 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009727 cur_offset + ins.offset - 1,
9728 0);
9729 }
9730 free_extent_map(em);
9731next:
Yan Zhengd899e052008-10-30 14:25:28 -04009732 num_bytes -= ins.offset;
9733 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009734 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009735
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009736 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009737 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009738 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009739 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009740 (actual_len > inode->i_size) &&
9741 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009742 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009743 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009744 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009745 i_size = cur_offset;
9746 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009747 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009748 }
9749
Yan Zhengd899e052008-10-30 14:25:28 -04009750 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009751
9752 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009753 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009754 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009755 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009756 break;
9757 }
Yan Zhengd899e052008-10-30 14:25:28 -04009758
Filipe Manana8fccebf2020-09-08 11:27:20 +01009759 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009760 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009761 trans = NULL;
9762 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009763 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009764 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009765 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009766 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009767 return ret;
9768}
9769
Josef Bacik0af3d002010-06-21 14:48:16 -04009770int btrfs_prealloc_file_range(struct inode *inode, int mode,
9771 u64 start, u64 num_bytes, u64 min_size,
9772 loff_t actual_len, u64 *alloc_hint)
9773{
9774 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9775 min_size, actual_len, alloc_hint,
9776 NULL);
9777}
9778
9779int btrfs_prealloc_file_range_trans(struct inode *inode,
9780 struct btrfs_trans_handle *trans, int mode,
9781 u64 start, u64 num_bytes, u64 min_size,
9782 loff_t actual_len, u64 *alloc_hint)
9783{
9784 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9785 min_size, actual_len, alloc_hint, trans);
9786}
9787
Chris Masone6dcd2d2008-07-17 12:53:50 -04009788static int btrfs_set_page_dirty(struct page *page)
9789{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009790 return __set_page_dirty_nobuffers(page);
9791}
9792
Al Viro10556cb22011-06-20 19:28:19 -04009793static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009794{
Li Zefanb83cc962010-12-20 16:04:08 +08009795 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009796 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009797
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009798 if (mask & MAY_WRITE &&
9799 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9800 if (btrfs_root_readonly(root))
9801 return -EROFS;
9802 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9803 return -EACCES;
9804 }
Al Viro2830ba72011-06-20 19:16:29 -04009805 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009806}
Chris Mason39279cc2007-06-12 06:35:45 -04009807
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009808static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9809{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009810 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009811 struct btrfs_trans_handle *trans;
9812 struct btrfs_root *root = BTRFS_I(dir)->root;
9813 struct inode *inode = NULL;
9814 u64 objectid;
9815 u64 index;
9816 int ret = 0;
9817
9818 /*
9819 * 5 units required for adding orphan entry
9820 */
9821 trans = btrfs_start_transaction(root, 5);
9822 if (IS_ERR(trans))
9823 return PTR_ERR(trans);
9824
9825 ret = btrfs_find_free_ino(root, &objectid);
9826 if (ret)
9827 goto out;
9828
9829 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009830 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009831 if (IS_ERR(inode)) {
9832 ret = PTR_ERR(inode);
9833 inode = NULL;
9834 goto out;
9835 }
9836
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009837 inode->i_fop = &btrfs_file_operations;
9838 inode->i_op = &btrfs_file_inode_operations;
9839
9840 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009841 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9842
Chris Masonb0d5d102014-09-08 13:08:51 -07009843 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9844 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009845 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009846
9847 ret = btrfs_update_inode(trans, root, inode);
9848 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009849 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009850 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009851 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009852 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009853
Filipe Manana5762b5c2014-08-01 00:10:32 +01009854 /*
9855 * We set number of links to 0 in btrfs_new_inode(), and here we set
9856 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9857 * through:
9858 *
9859 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9860 */
9861 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009862 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009863 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009864 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009865out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009866 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009867 if (ret && inode)
9868 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009869 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009870 return ret;
9871}
9872
David Sterba5cdc84b2018-07-18 20:32:52 +02009873void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009874{
David Sterba5cdc84b2018-07-18 20:32:52 +02009875 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009876 unsigned long index = start >> PAGE_SHIFT;
9877 unsigned long end_index = end >> PAGE_SHIFT;
9878 struct page *page;
9879
9880 while (index <= end_index) {
9881 page = find_get_page(inode->i_mapping, index);
9882 ASSERT(page); /* Pages should be in the extent_io_tree */
9883 set_page_writeback(page);
9884 put_page(page);
9885 index++;
9886 }
9887}
9888
Omar Sandovaled46ff32016-11-03 10:28:14 -07009889#ifdef CONFIG_SWAP
9890/*
9891 * Add an entry indicating a block group or device which is pinned by a
9892 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9893 * negative errno on failure.
9894 */
9895static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9896 bool is_block_group)
9897{
9898 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9899 struct btrfs_swapfile_pin *sp, *entry;
9900 struct rb_node **p;
9901 struct rb_node *parent = NULL;
9902
9903 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9904 if (!sp)
9905 return -ENOMEM;
9906 sp->ptr = ptr;
9907 sp->inode = inode;
9908 sp->is_block_group = is_block_group;
9909
9910 spin_lock(&fs_info->swapfile_pins_lock);
9911 p = &fs_info->swapfile_pins.rb_node;
9912 while (*p) {
9913 parent = *p;
9914 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9915 if (sp->ptr < entry->ptr ||
9916 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9917 p = &(*p)->rb_left;
9918 } else if (sp->ptr > entry->ptr ||
9919 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9920 p = &(*p)->rb_right;
9921 } else {
9922 spin_unlock(&fs_info->swapfile_pins_lock);
9923 kfree(sp);
9924 return 1;
9925 }
9926 }
9927 rb_link_node(&sp->node, parent, p);
9928 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9929 spin_unlock(&fs_info->swapfile_pins_lock);
9930 return 0;
9931}
9932
9933/* Free all of the entries pinned by this swapfile. */
9934static void btrfs_free_swapfile_pins(struct inode *inode)
9935{
9936 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9937 struct btrfs_swapfile_pin *sp;
9938 struct rb_node *node, *next;
9939
9940 spin_lock(&fs_info->swapfile_pins_lock);
9941 node = rb_first(&fs_info->swapfile_pins);
9942 while (node) {
9943 next = rb_next(node);
9944 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9945 if (sp->inode == inode) {
9946 rb_erase(&sp->node, &fs_info->swapfile_pins);
9947 if (sp->is_block_group)
9948 btrfs_put_block_group(sp->ptr);
9949 kfree(sp);
9950 }
9951 node = next;
9952 }
9953 spin_unlock(&fs_info->swapfile_pins_lock);
9954}
9955
9956struct btrfs_swap_info {
9957 u64 start;
9958 u64 block_start;
9959 u64 block_len;
9960 u64 lowest_ppage;
9961 u64 highest_ppage;
9962 unsigned long nr_pages;
9963 int nr_extents;
9964};
9965
9966static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9967 struct btrfs_swap_info *bsi)
9968{
9969 unsigned long nr_pages;
9970 u64 first_ppage, first_ppage_reported, next_ppage;
9971 int ret;
9972
9973 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9974 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9975 PAGE_SIZE) >> PAGE_SHIFT;
9976
9977 if (first_ppage >= next_ppage)
9978 return 0;
9979 nr_pages = next_ppage - first_ppage;
9980
9981 first_ppage_reported = first_ppage;
9982 if (bsi->start == 0)
9983 first_ppage_reported++;
9984 if (bsi->lowest_ppage > first_ppage_reported)
9985 bsi->lowest_ppage = first_ppage_reported;
9986 if (bsi->highest_ppage < (next_ppage - 1))
9987 bsi->highest_ppage = next_ppage - 1;
9988
9989 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
9990 if (ret < 0)
9991 return ret;
9992 bsi->nr_extents += ret;
9993 bsi->nr_pages += nr_pages;
9994 return 0;
9995}
9996
9997static void btrfs_swap_deactivate(struct file *file)
9998{
9999 struct inode *inode = file_inode(file);
10000
10001 btrfs_free_swapfile_pins(inode);
10002 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10003}
10004
10005static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10006 sector_t *span)
10007{
10008 struct inode *inode = file_inode(file);
10009 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10010 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10011 struct extent_state *cached_state = NULL;
10012 struct extent_map *em = NULL;
10013 struct btrfs_device *device = NULL;
10014 struct btrfs_swap_info bsi = {
10015 .lowest_ppage = (sector_t)-1ULL,
10016 };
10017 int ret = 0;
10018 u64 isize;
10019 u64 start;
10020
10021 /*
10022 * If the swap file was just created, make sure delalloc is done. If the
10023 * file changes again after this, the user is doing something stupid and
10024 * we don't really care.
10025 */
10026 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10027 if (ret)
10028 return ret;
10029
10030 /*
10031 * The inode is locked, so these flags won't change after we check them.
10032 */
10033 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10034 btrfs_warn(fs_info, "swapfile must not be compressed");
10035 return -EINVAL;
10036 }
10037 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10038 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10039 return -EINVAL;
10040 }
10041 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10042 btrfs_warn(fs_info, "swapfile must not be checksummed");
10043 return -EINVAL;
10044 }
10045
10046 /*
10047 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010048 * under us. The exclop protection makes sure they aren't running/won't
10049 * run concurrently while we are mapping the swap extents, and
10050 * fs_info->swapfile_pins prevents them from running while the swap
10051 * file is active and moving the extents. Note that this also prevents
10052 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010053 * really worth the trouble to allow it.
10054 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010055 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010056 btrfs_warn(fs_info,
10057 "cannot activate swapfile while exclusive operation is running");
10058 return -EBUSY;
10059 }
10060 /*
10061 * Snapshots can create extents which require COW even if NODATACOW is
10062 * set. We use this counter to prevent snapshots. We must increment it
10063 * before walking the extents because we don't want a concurrent
10064 * snapshot to run after we've already checked the extents.
10065 */
10066 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10067
10068 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10069
10070 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10071 start = 0;
10072 while (start < isize) {
10073 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010074 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010075 u64 len = isize - start;
10076
Omar Sandoval39b07b52019-12-02 17:34:23 -080010077 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010078 if (IS_ERR(em)) {
10079 ret = PTR_ERR(em);
10080 goto out;
10081 }
10082
10083 if (em->block_start == EXTENT_MAP_HOLE) {
10084 btrfs_warn(fs_info, "swapfile must not have holes");
10085 ret = -EINVAL;
10086 goto out;
10087 }
10088 if (em->block_start == EXTENT_MAP_INLINE) {
10089 /*
10090 * It's unlikely we'll ever actually find ourselves
10091 * here, as a file small enough to fit inline won't be
10092 * big enough to store more than the swap header, but in
10093 * case something changes in the future, let's catch it
10094 * here rather than later.
10095 */
10096 btrfs_warn(fs_info, "swapfile must not be inline");
10097 ret = -EINVAL;
10098 goto out;
10099 }
10100 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10101 btrfs_warn(fs_info, "swapfile must not be compressed");
10102 ret = -EINVAL;
10103 goto out;
10104 }
10105
10106 logical_block_start = em->block_start + (start - em->start);
10107 len = min(len, em->len - (start - em->start));
10108 free_extent_map(em);
10109 em = NULL;
10110
Boris Burkova84d5d42020-08-18 11:00:05 -070010111 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010112 if (ret < 0) {
10113 goto out;
10114 } else if (ret) {
10115 ret = 0;
10116 } else {
10117 btrfs_warn(fs_info,
10118 "swapfile must not be copy-on-write");
10119 ret = -EINVAL;
10120 goto out;
10121 }
10122
10123 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10124 if (IS_ERR(em)) {
10125 ret = PTR_ERR(em);
10126 goto out;
10127 }
10128
10129 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10130 btrfs_warn(fs_info,
10131 "swapfile must have single data profile");
10132 ret = -EINVAL;
10133 goto out;
10134 }
10135
10136 if (device == NULL) {
10137 device = em->map_lookup->stripes[0].dev;
10138 ret = btrfs_add_swapfile_pin(inode, device, false);
10139 if (ret == 1)
10140 ret = 0;
10141 else if (ret)
10142 goto out;
10143 } else if (device != em->map_lookup->stripes[0].dev) {
10144 btrfs_warn(fs_info, "swapfile must be on one device");
10145 ret = -EINVAL;
10146 goto out;
10147 }
10148
10149 physical_block_start = (em->map_lookup->stripes[0].physical +
10150 (logical_block_start - em->start));
10151 len = min(len, em->len - (logical_block_start - em->start));
10152 free_extent_map(em);
10153 em = NULL;
10154
10155 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10156 if (!bg) {
10157 btrfs_warn(fs_info,
10158 "could not find block group containing swapfile");
10159 ret = -EINVAL;
10160 goto out;
10161 }
10162
10163 ret = btrfs_add_swapfile_pin(inode, bg, true);
10164 if (ret) {
10165 btrfs_put_block_group(bg);
10166 if (ret == 1)
10167 ret = 0;
10168 else
10169 goto out;
10170 }
10171
10172 if (bsi.block_len &&
10173 bsi.block_start + bsi.block_len == physical_block_start) {
10174 bsi.block_len += len;
10175 } else {
10176 if (bsi.block_len) {
10177 ret = btrfs_add_swap_extent(sis, &bsi);
10178 if (ret)
10179 goto out;
10180 }
10181 bsi.start = start;
10182 bsi.block_start = physical_block_start;
10183 bsi.block_len = len;
10184 }
10185
10186 start += len;
10187 }
10188
10189 if (bsi.block_len)
10190 ret = btrfs_add_swap_extent(sis, &bsi);
10191
10192out:
10193 if (!IS_ERR_OR_NULL(em))
10194 free_extent_map(em);
10195
10196 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10197
10198 if (ret)
10199 btrfs_swap_deactivate(file);
10200
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010201 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010202
10203 if (ret)
10204 return ret;
10205
10206 if (device)
10207 sis->bdev = device->bdev;
10208 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10209 sis->max = bsi.nr_pages;
10210 sis->pages = bsi.nr_pages - 1;
10211 sis->highest_bit = bsi.nr_pages - 1;
10212 return bsi.nr_extents;
10213}
10214#else
10215static void btrfs_swap_deactivate(struct file *file)
10216{
10217}
10218
10219static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10220 sector_t *span)
10221{
10222 return -EOPNOTSUPP;
10223}
10224#endif
10225
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010226static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010227 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010228 .lookup = btrfs_lookup,
10229 .create = btrfs_create,
10230 .unlink = btrfs_unlink,
10231 .link = btrfs_link,
10232 .mkdir = btrfs_mkdir,
10233 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010234 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010235 .symlink = btrfs_symlink,
10236 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010237 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010238 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010239 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010240 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010241 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010242 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010243 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010244};
Yan, Zheng76dda932009-09-21 16:00:26 -040010245
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010246static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010247 .llseek = generic_file_llseek,
10248 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010249 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010250 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010251 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010252#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010253 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010254#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010255 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010256 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010257};
10258
David Sterba20e55062015-11-19 11:42:28 +010010259static const struct extent_io_ops btrfs_extent_io_ops = {
Nikolay Borisov908930f2020-09-18 16:34:37 +030010260 .submit_bio_hook = NULL
Chris Mason07157aa2007-08-30 08:50:51 -040010261};
10262
Chris Mason35054392009-01-21 13:11:13 -050010263/*
10264 * btrfs doesn't support the bmap operation because swapfiles
10265 * use bmap to make a mapping of extents in the file. They assume
10266 * these extents won't change over the life of the file and they
10267 * use the bmap result to do IO directly to the drive.
10268 *
10269 * the btrfs bmap call would return logical addresses that aren't
10270 * suitable for IO and they also will change frequently as COW
10271 * operations happen. So, swapfile + btrfs == corruption.
10272 *
10273 * For now we're avoiding this by dropping bmap.
10274 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010275static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010276 .readpage = btrfs_readpage,
10277 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010278 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010279 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010280 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010281 .invalidatepage = btrfs_invalidatepage,
10282 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010283#ifdef CONFIG_MIGRATION
10284 .migratepage = btrfs_migratepage,
10285#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010286 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010287 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010288 .swap_activate = btrfs_swap_activate,
10289 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010290};
10291
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010292static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010293 .getattr = btrfs_getattr,
10294 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010295 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010296 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010297 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010298 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010299 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010300 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010301};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010302static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010303 .getattr = btrfs_getattr,
10304 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010305 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010306 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010307 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010308 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010309 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010310};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010311static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010312 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010313 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010314 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010315 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010316 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010317 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010318};
Yan, Zheng76dda932009-09-21 16:00:26 -040010319
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010320const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010321 .d_delete = btrfs_dentry_delete,
10322};