blob: 77ab9eada11d2fb8a0b2b235ee3c71f20dc82234 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Chris Mason6cbd5572007-06-12 09:07:21 -04002/*
3 * Copyright (C) 2007 Oracle. All rights reserved.
Chris Mason6cbd5572007-06-12 09:07:21 -04004 */
5
Herbert Xu79990962020-06-12 16:57:37 +10006#include <crypto/hash.h>
Chris Mason8f18cf12008-04-25 16:53:30 -04007#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -05008#include <linux/bio.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -04009#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040010#include <linux/fs.h>
11#include <linux/pagemap.h>
12#include <linux/highmem.h>
13#include <linux/time.h>
14#include <linux/init.h>
15#include <linux/string.h>
Chris Mason39279cc2007-06-12 06:35:45 -040016#include <linux/backing-dev.h>
Chris Mason39279cc2007-06-12 06:35:45 -040017#include <linux/writeback.h>
Chris Mason39279cc2007-06-12 06:35:45 -040018#include <linux/compat.h>
Josef Bacik5103e942007-11-16 11:45:54 -050019#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040020#include <linux/posix_acl.h>
Yan Zhengd899e052008-10-30 14:25:28 -040021#include <linux/falloc.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
David Sterba7a36dde2011-05-06 15:33:15 +020023#include <linux/ratelimit.h>
Filipe Brandenburger55e301f2013-01-29 06:04:50 +000024#include <linux/btrfs.h>
David Woodhouse53b381b2013-01-29 18:40:14 -050025#include <linux/blkdev.h>
Josef Bacikf23b5a52013-06-19 10:16:26 -040026#include <linux/posix_acl_xattr.h>
Christoph Hellwige2e40f22015-02-22 08:58:50 -080027#include <linux/uio.h>
Josef Bacik69fe2d72017-10-19 14:15:57 -040028#include <linux/magic.h>
Jeff Laytonae5e1652018-01-29 06:41:30 -050029#include <linux/iversion.h>
Omar Sandovaled46ff32016-11-03 10:28:14 -070030#include <linux/swap.h>
Roman Gushchinf8e66082020-03-04 16:57:35 -080031#include <linux/migrate.h>
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +030032#include <linux/sched/mm.h>
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050033#include <linux/iomap.h>
David Sterba92d32172018-04-16 21:10:14 +020034#include <asm/unaligned.h>
David Sterba602cbe92019-08-21 18:48:25 +020035#include "misc.h"
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "print-tree.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040041#include "ordered-data.h"
Christoph Hellwig95819c02008-08-28 06:21:17 -040042#include "xattr.h"
Chris Masone02119d2008-09-05 16:13:11 -040043#include "tree-log.h"
Jan Schmidt4a54c8c2011-07-22 15:41:52 +020044#include "volumes.h"
Chris Masonc8b97812008-10-29 14:49:59 -040045#include "compression.h"
Chris Masonb4ce94d2009-02-04 09:25:08 -050046#include "locking.h"
Josef Bacikdc89e982011-01-28 17:05:48 -050047#include "free-space-cache.h"
Li Zefan581bb052011-04-20 10:06:11 +080048#include "inode-map.h"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000049#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080050#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040051#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040052#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010053#include "space-info.h"
Chris Mason39279cc2007-06-12 06:35:45 -040054
55struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020056 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040057 struct btrfs_root *root;
58};
59
Filipe Mananaf28a4922015-12-08 19:23:20 +000060struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000061 u64 reserve;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050062 loff_t length;
63 ssize_t submitted;
64 struct extent_changeset *data_reserved;
Josef Bacik0eb79292020-09-03 11:16:51 -040065 bool sync;
Filipe Mananaf28a4922015-12-08 19:23:20 +000066};
67
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_dir_inode_operations;
69static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070070static const struct inode_operations btrfs_special_inode_operations;
71static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070072static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070073static const struct file_operations btrfs_dir_file_operations;
David Sterba20e55062015-11-19 11:42:28 +010074static const struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040075
76static struct kmem_cache *btrfs_inode_cachep;
77struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050079struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000080struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040081
Eric Sandeen3972f262013-01-12 02:57:22 +000082static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000083static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040084static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030085static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050086 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030087 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030088 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030089static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
90 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080091 u64 block_len, u64 orig_block_len,
92 u64 ram_bytes, int compress_type,
93 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040094
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030095static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080096 const u64 offset, const u64 bytes,
97 const bool uptodate);
98
99/*
100 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100101 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800102 *
103 * NOTE: caller must ensure that when an error happens, it can not call
104 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
105 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
106 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200107 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800108 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300109static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200110 struct page *locked_page,
111 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800112{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900113 unsigned long index = offset >> PAGE_SHIFT;
114 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200115 u64 page_start = page_offset(locked_page);
116 u64 page_end = page_start + PAGE_SIZE - 1;
117
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900118 struct page *page;
119
120 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300121 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900122 index++;
123 if (!page)
124 continue;
125 ClearPagePrivate2(page);
126 put_page(page);
127 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200128
129 /*
130 * In case this page belongs to the delalloc range being instantiated
131 * then skip it, since the first page of a range is going to be
132 * properly cleaned up by the caller of run_delalloc_range
133 */
134 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
135 offset += PAGE_SIZE;
136 bytes -= PAGE_SIZE;
137 }
138
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300139 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800140}
141
Eric Sandeen48a3b632013-04-25 20:41:01 +0000142static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400143
Josef Bacik6a3891c2015-03-16 17:38:52 -0400144#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
145void btrfs_test_inode_set_ops(struct inode *inode)
146{
147 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
148}
149#endif
150
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000151static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500152 struct inode *inode, struct inode *dir,
153 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500154{
155 int err;
156
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000157 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500158 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500159 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500160 return err;
161}
162
Chris Masond352ac62008-09-29 15:18:18 -0400163/*
Chris Masonc8b97812008-10-29 14:49:59 -0400164 * this does all the hard work for inserting an inline extent into
165 * the btree. The caller should have done a btrfs_drop_extents so that
166 * no overlapping inline items exist in the btree
167 */
Chris Mason40f76582014-05-21 13:35:51 -0700168static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000169 struct btrfs_path *path, int extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400170 struct btrfs_root *root, struct inode *inode,
171 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000172 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400173 struct page **compressed_pages)
174{
Chris Masonc8b97812008-10-29 14:49:59 -0400175 struct extent_buffer *leaf;
176 struct page *page = NULL;
177 char *kaddr;
178 unsigned long ptr;
179 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400180 int ret;
181 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400182 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400183
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800184 ASSERT((compressed_size > 0 && compressed_pages) ||
185 (compressed_size == 0 && !compressed_pages));
186
Li Zefanfe3f5662011-03-28 08:30:38 +0000187 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400188 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400189
Chris Masonc8b97812008-10-29 14:49:59 -0400190 inode_add_bytes(inode, size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000191
192 if (!extent_inserted) {
193 struct btrfs_key key;
194 size_t datasize;
195
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200196 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000197 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200198 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000199
200 datasize = btrfs_file_extent_calc_inline_size(cur_size);
201 path->leave_spinning = 1;
202 ret = btrfs_insert_empty_item(trans, root, path, &key,
203 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200204 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000205 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400206 }
207 leaf = path->nodes[0];
208 ei = btrfs_item_ptr(leaf, path->slots[0],
209 struct btrfs_file_extent_item);
210 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
211 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
212 btrfs_set_file_extent_encryption(leaf, ei, 0);
213 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
214 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
215 ptr = btrfs_file_extent_inline_start(ei);
216
Li Zefan261507a02010-12-17 14:21:50 +0800217 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400218 struct page *cpage;
219 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500220 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400221 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500222 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300223 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400224
Cong Wang7ac687d2011-11-25 23:14:28 +0800225 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400226 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800227 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400228
229 i++;
230 ptr += cur_size;
231 compressed_size -= cur_size;
232 }
233 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800234 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400235 } else {
236 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300237 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400238 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800239 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100240 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400241 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800242 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300243 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400244 }
245 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000246 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400247
Yan, Zhengc2167752009-11-12 09:34:21 +0000248 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500249 * We align size to sectorsize for inline extents just for simplicity
250 * sake.
251 */
252 size = ALIGN(size, root->fs_info->sectorsize);
253 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
254 if (ret)
255 goto fail;
256
257 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000258 * we're an inline extent, so nobody can
259 * extend the file past i_size without locking
260 * a page we already have locked.
261 *
262 * We must do any isize and inode updates
263 * before we unlock the pages. Otherwise we
264 * could end up racing with unlink.
265 */
Chris Masonc8b97812008-10-29 14:49:59 -0400266 BTRFS_I(inode)->disk_i_size = inode->i_size;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100267 ret = btrfs_update_inode(trans, root, inode);
Yan, Zhengc2167752009-11-12 09:34:21 +0000268
Chris Masonc8b97812008-10-29 14:49:59 -0400269fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200270 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400271}
272
273
274/*
275 * conditionally insert an inline extent into the file. This
276 * does the checks required to make sure the data is small enough
277 * to fit as an inline extent.
278 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300279static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400280 u64 end, size_t compressed_size,
281 int compress_type,
282 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400283{
Nikolay Borisova0349402020-06-03 08:55:12 +0300284 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400285 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400286 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300287 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400288 u64 actual_end = min(end + 1, isize);
289 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400290 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400291 u64 data_len = inline_len;
292 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000293 struct btrfs_path *path;
294 int extent_inserted = 0;
295 u32 extent_item_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400296
297 if (compressed_size)
298 data_len = compressed_size;
299
300 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400301 actual_end > fs_info->sectorsize ||
302 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400303 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400304 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400305 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400306 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400307 return 1;
308 }
309
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000310 path = btrfs_alloc_path();
311 if (!path)
312 return -ENOMEM;
313
Josef Bacik00361582013-08-14 14:02:47 -0400314 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000315 if (IS_ERR(trans)) {
316 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400317 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000318 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300319 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400320
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000321 if (compressed_size && compressed_pages)
322 extent_item_size = btrfs_file_extent_calc_inline_size(
323 compressed_size);
324 else
325 extent_item_size = btrfs_file_extent_calc_inline_size(
326 inline_len);
327
Nikolay Borisova0349402020-06-03 08:55:12 +0300328 ret = __btrfs_drop_extents(trans, root, inode, path, start, aligned_end,
329 NULL, 1, 1, extent_item_size,
330 &extent_inserted);
Josef Bacik00361582013-08-14 14:02:47 -0400331 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400332 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400333 goto out;
334 }
Chris Masonc8b97812008-10-29 14:49:59 -0400335
336 if (isize > actual_end)
337 inline_len = min_t(u64, isize, actual_end);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000338 ret = insert_inline_extent(trans, path, extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300339 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400340 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000341 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400342 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400343 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400344 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400345 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400346 ret = 1;
347 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100348 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400349
Nikolay Borisova0349402020-06-03 08:55:12 +0300350 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
351 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400352out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800353 /*
354 * Don't forget to free the reserved space, as for inlined extent
355 * it won't count as data extent, free them directly here.
356 * And at reserve time, it's always aligned to page size, so
357 * just free one page here.
358 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300359 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000360 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400361 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400362 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400363}
364
Chris Mason771ed682008-11-06 22:02:51 -0500365struct async_extent {
366 u64 start;
367 u64 ram_size;
368 u64 compressed_size;
369 struct page **pages;
370 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800371 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500372 struct list_head list;
373};
374
Nikolay Borisov97db1202019-03-12 17:20:24 +0200375struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500376 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500377 struct page *locked_page;
378 u64 start;
379 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600380 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500381 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700382 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500383 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200384 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500385};
386
Nikolay Borisov97db1202019-03-12 17:20:24 +0200387struct async_cow {
388 /* Number of chunks in flight; must be first in the structure */
389 atomic_t num_chunks;
390 struct async_chunk chunks[];
391};
392
393static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500394 u64 start, u64 ram_size,
395 u64 compressed_size,
396 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800397 unsigned long nr_pages,
398 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500399{
400 struct async_extent *async_extent;
401
402 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100403 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500404 async_extent->start = start;
405 async_extent->ram_size = ram_size;
406 async_extent->compressed_size = compressed_size;
407 async_extent->pages = pages;
408 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800409 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500410 list_add_tail(&async_extent->list, &cow->extents);
411 return 0;
412}
413
Qu Wenruo42c16da2019-07-01 05:12:46 +0000414/*
415 * Check if the inode has flags compatible with compression
416 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300417static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000418{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300419 if (inode->flags & BTRFS_INODE_NODATACOW ||
420 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000421 return false;
422 return true;
423}
424
425/*
426 * Check if the inode needs to be submitted to compression, based on mount
427 * options, defragmentation, properties or heuristics.
428 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300429static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
430 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800431{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300432 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800433
Nikolay Borisov808a1292020-06-03 08:55:27 +0300434 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000435 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
436 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300437 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000438 return 0;
439 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800440 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400441 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800442 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200443 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300444 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200445 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800446 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300447 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800448 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400449 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300450 inode->flags & BTRFS_INODE_COMPRESS ||
451 inode->prop_compress)
452 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800453 return 0;
454}
455
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200456static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800457 u64 start, u64 end, u64 num_bytes, u64 small_write)
458{
459 /* If this is a small write inside eof, kick off a defrag */
460 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200461 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800462 btrfs_add_inode_defrag(NULL, inode);
463}
464
Chris Masonc8b97812008-10-29 14:49:59 -0400465/*
Chris Mason771ed682008-11-06 22:02:51 -0500466 * we create compressed extents in two phases. The first
467 * phase compresses a range of pages that have already been
468 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400469 *
Chris Mason771ed682008-11-06 22:02:51 -0500470 * This is done inside an ordered work queue, and the compression
471 * is spread across many cpus. The actual IO submission is step
472 * two, and the ordered work queue takes care of making sure that
473 * happens in the same order things were put onto the queue by
474 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400475 *
Chris Mason771ed682008-11-06 22:02:51 -0500476 * If this code finds it can't get good compression, it puts an
477 * entry onto the work queue to write the uncompressed bytes. This
478 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300479 * are written in the same order that the flusher thread sent them
480 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400481 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300482static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400483{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200484 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400485 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400486 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200487 u64 start = async_chunk->start;
488 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400489 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400490 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400491 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400492 struct page **pages = NULL;
493 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400494 unsigned long total_compressed = 0;
495 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400496 int i;
497 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400498 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300499 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400500 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400501
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200502 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
503 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400504
Josef Bacikd98da492019-10-11 09:03:54 -0400505 /*
506 * We need to save i_size before now because it could change in between
507 * us evaluating the size and assigning it. This is because we lock and
508 * unlock the page in truncate and fallocate, and then modify the i_size
509 * later on.
510 *
511 * The barriers are to emulate READ_ONCE, remove that once i_size_read
512 * does that for us.
513 */
514 barrier();
515 i_size = i_size_read(inode);
516 barrier();
517 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400518again:
519 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300520 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100521 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
522 nr_pages = min_t(unsigned long, nr_pages,
523 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400524
Chris Masonf03d9301f2009-02-04 09:31:06 -0500525 /*
526 * we don't want to send crud past the end of i_size through
527 * compression, that's just a waste of CPU time. So, if the
528 * end of the file is before the start of our current
529 * requested range of bytes, we bail out to the uncompressed
530 * cleanup code that can deal with all of this.
531 *
532 * It isn't really the fastest way to fix things, but this is a
533 * very uncommon corner.
534 */
535 if (actual_end <= start)
536 goto cleanup_and_bail_uncompressed;
537
Chris Masonc8b97812008-10-29 14:49:59 -0400538 total_compressed = actual_end - start;
539
Shilong Wang4bcbb332014-10-07 18:44:35 -0400540 /*
541 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400542 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400543 */
544 if (total_compressed <= blocksize &&
545 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
546 goto cleanup_and_bail_uncompressed;
547
David Sterba069eac72017-02-14 19:36:54 +0100548 total_compressed = min_t(unsigned long, total_compressed,
549 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400550 total_in = 0;
551 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400552
Chris Mason771ed682008-11-06 22:02:51 -0500553 /*
554 * we do compression for mount -o compress and when the
555 * inode has not been flagged as nocompress. This flag can
556 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400557 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300558 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400559 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100560 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800561 if (!pages) {
562 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100563 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800564 goto cont;
565 }
Chris Mason179e29e2007-11-01 11:28:41 -0400566
David Sterbaeec63c62017-07-17 19:41:31 +0200567 if (BTRFS_I(inode)->defrag_compress)
568 compress_type = BTRFS_I(inode)->defrag_compress;
569 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200570 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800571
Chris Mason4adaa612013-03-26 13:07:00 -0400572 /*
573 * we need to call clear_page_dirty_for_io on each
574 * page in the range. Otherwise applications with the file
575 * mmap'd can wander in and change the page contents while
576 * we are compressing them.
577 *
578 * If the compression fails for any reason, we set the pages
579 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300580 *
581 * Note that the remaining part is redirtied, the start pointer
582 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400583 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300584 if (!redirty) {
585 extent_range_clear_dirty_for_io(inode, start, end);
586 redirty = 1;
587 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200588
589 /* Compression level is applied here and only here */
590 ret = btrfs_compress_pages(
591 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800592 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100593 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100594 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800595 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100596 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400597
598 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100599 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100600 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400601 char *kaddr;
602
603 /* zero the tail end of the last page, we might be
604 * sending it down to disk
605 */
606 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800607 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400608 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300609 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800610 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400611 }
612 will_compress = 1;
613 }
614 }
Li Zefan560f7d72011-09-08 10:22:01 +0800615cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400616 if (start == 0) {
617 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300618 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400619 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500620 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400621 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300622 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
623 0, BTRFS_COMPRESS_NONE,
624 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400625 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500626 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300627 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000628 total_compressed,
629 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400630 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100631 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400632 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400633 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
634 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100635 unsigned long page_error_op;
636
Filipe Mananae6eb4312014-10-10 10:45:12 +0100637 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400638
Chris Mason771ed682008-11-06 22:02:51 -0500639 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100640 * inline extent creation worked or returned error,
641 * we don't need to create any more async work items.
642 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400643 *
644 * We use DO_ACCOUNTING here because we need the
645 * delalloc_release_metadata to be done _after_ we drop
646 * our outstanding extent for clearing delalloc for this
647 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500648 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300649 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
650 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300651 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800652 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400653 PAGE_CLEAR_DIRTY |
654 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100655 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400656 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300657
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800658 /*
659 * Ensure we only free the compressed pages if we have
660 * them allocated, as we can still reach here with
661 * inode_need_compress() == false.
662 */
663 if (pages) {
664 for (i = 0; i < nr_pages; i++) {
665 WARN_ON(pages[i]->mapping);
666 put_page(pages[i]);
667 }
668 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300669 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300670 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400671 }
672 }
673
674 if (will_compress) {
675 /*
676 * we aren't doing an inline extent round the compressed size
677 * up to a block size boundary so the allocator does sane
678 * things
679 */
Qu Wenruofda28322013-02-26 08:10:22 +0000680 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400681
682 /*
683 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300684 * win, compare the page count read with the blocks on disk,
685 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400686 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300687 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300688 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300689 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700690
691 /*
692 * The async work queues will take care of doing actual
693 * allocation on disk for these compressed pages, and
694 * will submit them to the elevator.
695 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200696 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100697 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700698 compress_type);
699
Timofey Titovets11708622017-10-03 18:06:01 +0300700 if (start + total_in < end) {
701 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700702 pages = NULL;
703 cond_resched();
704 goto again;
705 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300706 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400707 }
708 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700709 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400710 /*
711 * the compression code ran but failed to make things smaller,
712 * free any pages it allocated and our page pointer array
713 */
David Sterba4d3a8002017-02-14 19:04:07 +0100714 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400715 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300716 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400717 }
718 kfree(pages);
719 pages = NULL;
720 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100721 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400722
723 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400724 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200725 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500726 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500727 }
Chris Masonc8b97812008-10-29 14:49:59 -0400728 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500729cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700730 /*
731 * No compression, but we still need to write the pages in the file
732 * we've been given so far. redirty the locked page if it corresponds
733 * to our extent and set things up for the async work queue to run
734 * cow_file_range to do the normal delalloc dance.
735 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700736 if (async_chunk->locked_page &&
737 (page_offset(async_chunk->locked_page) >= start &&
738 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200739 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700740 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700741 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700742
743 if (redirty)
744 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200745 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700746 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300747 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500748
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300749 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500750}
Chris Mason771ed682008-11-06 22:02:51 -0500751
Filipe Manana40ae8372014-10-06 22:14:24 +0100752static void free_async_extent_pages(struct async_extent *async_extent)
753{
754 int i;
755
756 if (!async_extent->pages)
757 return;
758
759 for (i = 0; i < async_extent->nr_pages; i++) {
760 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300761 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100762 }
763 kfree(async_extent->pages);
764 async_extent->nr_pages = 0;
765 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500766}
767
768/*
769 * phase two of compressed writeback. This is the ordered portion
770 * of the code, which only gets called in the order the work was
771 * queued. We walk all the async extents created by compress_file_range
772 * and send them down to the disk.
773 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200774static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500775{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300776 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
777 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500778 struct async_extent *async_extent;
779 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500780 struct btrfs_key ins;
781 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300782 struct btrfs_root *root = inode->root;
783 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500784 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500785
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500786again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200787 while (!list_empty(&async_chunk->extents)) {
788 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500789 struct async_extent, list);
790 list_del(&async_extent->list);
791
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500792retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200793 lock_extent(io_tree, async_extent->start,
794 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500795 /* did the compression code fall back to uncompressed IO? */
796 if (!async_extent->pages) {
797 int page_started = 0;
798 unsigned long nr_written = 0;
799
Chris Mason771ed682008-11-06 22:02:51 -0500800 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300801 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500802 async_extent->start,
803 async_extent->start +
804 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300805 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500806
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100807 /* JDM XXX */
808
Chris Mason771ed682008-11-06 22:02:51 -0500809 /*
810 * if page_started, cow_file_range inserted an
811 * inline extent and took care of all the unlocking
812 * and IO for us. Otherwise, we need to submit
813 * all those pages down to the drive.
814 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500815 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300816 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200817 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500818 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500819 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500820 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700821 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200822 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500823 kfree(async_extent);
824 cond_resched();
825 continue;
826 }
827
Wang Xiaoguang18513092016-07-25 15:51:40 +0800828 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500829 async_extent->compressed_size,
830 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800831 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500832 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100833 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500834
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400835 if (ret == -ENOSPC) {
836 unlock_extent(io_tree, async_extent->start,
837 async_extent->start +
838 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800839
840 /*
841 * we need to redirty the pages if we decide to
842 * fallback to uncompressed IO, otherwise we
843 * will not submit these pages down to lower
844 * layers.
845 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300846 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800847 async_extent->start,
848 async_extent->start +
849 async_extent->ram_size - 1);
850
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100851 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400852 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500853 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500854 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000855 /*
856 * here we're doing allocation and writeback of the
857 * compressed pages
858 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300859 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800860 async_extent->ram_size, /* len */
861 async_extent->start, /* orig_start */
862 ins.objectid, /* block_start */
863 ins.offset, /* block_len */
864 ins.offset, /* orig_block_len */
865 async_extent->ram_size, /* ram_bytes */
866 async_extent->compress_type,
867 BTRFS_ORDERED_COMPRESSED);
868 if (IS_ERR(em))
869 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500870 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800871 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500872
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300873 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800874 async_extent->start,
875 ins.objectid,
876 async_extent->ram_size,
877 ins.offset,
878 BTRFS_ORDERED_COMPRESSED,
879 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100880 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300881 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100882 async_extent->start +
883 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500884 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100885 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400886 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500887
Chris Mason771ed682008-11-06 22:02:51 -0500888 /*
889 * clear dirty, set writeback and unlock the pages.
890 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300891 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400892 async_extent->start +
893 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400894 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
895 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400896 PAGE_SET_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300897 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500898 async_extent->ram_size,
899 ins.objectid,
900 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600901 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700902 async_chunk->write_flags,
903 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100904 struct page *p = async_extent->pages[0];
905 const u64 start = async_extent->start;
906 const u64 end = start + async_extent->ram_size - 1;
907
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300908 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200909 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200910
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100911 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300912 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100913 PAGE_END_WRITEBACK |
914 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100915 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100916 }
Chris Mason771ed682008-11-06 22:02:51 -0500917 alloc_hint = ins.objectid + ins.offset;
918 kfree(async_extent);
919 cond_resched();
920 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100921 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500922out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400923 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400924 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100925out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300926 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500927 async_extent->start +
928 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400929 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100930 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400931 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
932 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100933 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
934 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100935 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100936 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500937 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500938}
939
Nikolay Borisov43c69842020-06-03 08:55:02 +0300940static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400941 u64 num_bytes)
942{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300943 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400944 struct extent_map *em;
945 u64 alloc_hint = 0;
946
947 read_lock(&em_tree->lock);
948 em = search_extent_mapping(em_tree, start, num_bytes);
949 if (em) {
950 /*
951 * if block start isn't an actual block number then find the
952 * first block in this inode and use that as a hint. If that
953 * block is also bogus then just don't worry about it.
954 */
955 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
956 free_extent_map(em);
957 em = search_extent_mapping(em_tree, 0, 0);
958 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
959 alloc_hint = em->block_start;
960 if (em)
961 free_extent_map(em);
962 } else {
963 alloc_hint = em->block_start;
964 free_extent_map(em);
965 }
966 }
967 read_unlock(&em_tree->lock);
968
969 return alloc_hint;
970}
971
Chris Mason771ed682008-11-06 22:02:51 -0500972/*
973 * when extent_io.c finds a delayed allocation range in the file,
974 * the call backs end up in this code. The basic idea is to
975 * allocate extents on disk for the range, and create ordered data structs
976 * in ram to track those extents.
977 *
978 * locked_page is the page that writepage had locked already. We use
979 * it to make sure we don't do extra locks or unlocks.
980 *
981 * *page_started is set to one if we unlock locked_page and do everything
982 * required to start IO on it. It may be clean and already done with
983 * IO when we return.
984 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300985static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -0400986 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300987 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300988 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -0500989{
Nikolay Borisov6e26c442020-06-03 08:55:14 +0300990 struct btrfs_root *root = inode->root;
991 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500992 u64 alloc_hint = 0;
993 u64 num_bytes;
994 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +0000995 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +0100996 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400997 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -0500998 struct btrfs_key ins;
999 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001000 unsigned clear_bits;
1001 unsigned long page_ops;
1002 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001003 int ret = 0;
1004
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001005 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001006 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001007 ret = -EINVAL;
1008 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001009 }
Chris Mason771ed682008-11-06 22:02:51 -05001010
Qu Wenruofda28322013-02-26 08:10:22 +00001011 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001012 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001013 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001014
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001015 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001016
Chris Mason771ed682008-11-06 22:02:51 -05001017 if (start == 0) {
1018 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001019 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001020 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001021 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001022 /*
1023 * We use DO_ACCOUNTING here because we need the
1024 * delalloc_release_metadata to be run _after_ we drop
1025 * our outstanding extent for clearing delalloc for this
1026 * range.
1027 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001028 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001029 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001030 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1031 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001032 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1033 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001034 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001035 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001036 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001037 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001038 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001039 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001040 }
1041 }
Chris Masonc8b97812008-10-29 14:49:59 -04001042
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001043 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1044 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001045
Filipe Manana432cd2a2020-06-08 13:32:55 +01001046 /*
1047 * Relocation relies on the relocated extents to have exactly the same
1048 * size as the original extents. Normally writeback for relocation data
1049 * extents follows a NOCOW path because relocation preallocates the
1050 * extents. However, due to an operation such as scrub turning a block
1051 * group to RO mode, it may fallback to COW mode, so we must make sure
1052 * an extent allocated during COW has exactly the requested size and can
1053 * not be split into smaller extents, otherwise relocation breaks and
1054 * fails during the stage where it updates the bytenr of file extent
1055 * items.
1056 */
1057 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1058 min_alloc_size = num_bytes;
1059 else
1060 min_alloc_size = fs_info->sectorsize;
1061
Anand Jain3752d222018-02-15 12:29:38 +08001062 while (num_bytes > 0) {
1063 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001064 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001065 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001066 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001067 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001068 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001069 cur_alloc_size = ins.offset;
1070 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001071
Chris Mason771ed682008-11-06 22:02:51 -05001072 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001073 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001074 start, /* orig_start */
1075 ins.objectid, /* block_start */
1076 ins.offset, /* block_len */
1077 ins.offset, /* orig_block_len */
1078 ram_size, /* ram_bytes */
1079 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001080 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001081 if (IS_ERR(em)) {
1082 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001083 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001084 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001085 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001086
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001087 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1088 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001089 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001090 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001091
Yan Zheng17d217f2008-12-12 10:03:38 -05001092 if (root->root_key.objectid ==
1093 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001094 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001095 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001096 /*
1097 * Only drop cache here, and process as normal.
1098 *
1099 * We must not allow extent_clear_unlock_delalloc()
1100 * at out_unlock label to free meta of this ordered
1101 * extent, as its meta should be freed by
1102 * btrfs_finish_ordered_io().
1103 *
1104 * So we must continue until @start is increased to
1105 * skip current ordered extent.
1106 */
Josef Bacik00361582013-08-14 14:02:47 -04001107 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001108 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001109 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001110 }
1111
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001112 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001113
Chris Masonc8b97812008-10-29 14:49:59 -04001114 /* we're not doing compressed IO, don't unlock the first
1115 * page (which the caller expects to stay locked), don't
1116 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001117 *
1118 * Do set the Private2 bit so we know this page was properly
1119 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001120 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001121 page_ops = unlock ? PAGE_UNLOCK : 0;
1122 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001123
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001124 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001125 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001126 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001127 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001128 if (num_bytes < cur_alloc_size)
1129 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001130 else
Anand Jain3752d222018-02-15 12:29:38 +08001131 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001132 alloc_hint = ins.objectid + ins.offset;
1133 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001134 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001135
1136 /*
1137 * btrfs_reloc_clone_csums() error, since start is increased
1138 * extent_clear_unlock_delalloc() at out_unlock label won't
1139 * free metadata of current ordered extent, we're OK to exit.
1140 */
1141 if (ret)
1142 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001143 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001144out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001145 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001146
Filipe Mananad9f85962014-08-25 10:43:00 +01001147out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001148 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001149out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001150 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001151 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001152out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001153 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1154 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001155 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1156 PAGE_END_WRITEBACK;
1157 /*
1158 * If we reserved an extent for our delalloc range (or a subrange) and
1159 * failed to create the respective ordered extent, then it means that
1160 * when we reserved the extent we decremented the extent's size from
1161 * the data space_info's bytes_may_use counter and incremented the
1162 * space_info's bytes_reserved counter by the same amount. We must make
1163 * sure extent_clear_unlock_delalloc() does not try to decrement again
1164 * the data space_info's bytes_may_use counter, therefore we do not pass
1165 * it the flag EXTENT_CLEAR_DATA_RESV.
1166 */
1167 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001168 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001169 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001170 locked_page,
1171 clear_bits,
1172 page_ops);
1173 start += cur_alloc_size;
1174 if (start >= end)
1175 goto out;
1176 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001177 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001178 clear_bits | EXTENT_CLEAR_DATA_RESV,
1179 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001180 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001181}
Chris Masonc8b97812008-10-29 14:49:59 -04001182
Chris Mason771ed682008-11-06 22:02:51 -05001183/*
1184 * work queue call back to started compression on a file and pages
1185 */
1186static noinline void async_cow_start(struct btrfs_work *work)
1187{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001188 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001189 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001190
Nikolay Borisovb5326272019-03-12 17:20:25 +02001191 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001192
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001193 compressed_extents = compress_file_range(async_chunk);
1194 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001195 btrfs_add_delayed_iput(async_chunk->inode);
1196 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001197 }
Chris Mason771ed682008-11-06 22:02:51 -05001198}
1199
1200/*
1201 * work queue call back to submit previously compressed pages
1202 */
1203static noinline void async_cow_submit(struct btrfs_work *work)
1204{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001205 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1206 work);
1207 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001208 unsigned long nr_pages;
1209
Nikolay Borisovb5326272019-03-12 17:20:25 +02001210 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001211 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001212
David Sterba093258e2018-02-26 16:15:17 +01001213 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001214 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001215 5 * SZ_1M)
1216 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001217
Nikolay Borisov4546d172019-01-03 10:50:03 +02001218 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001219 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001220 * in which case we don't have anything to submit, yet we need to
1221 * always adjust ->async_delalloc_pages as its paired with the init
1222 * happening in cow_file_range_async
1223 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001224 if (async_chunk->inode)
1225 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001226}
Chris Masonc8b97812008-10-29 14:49:59 -04001227
Chris Mason771ed682008-11-06 22:02:51 -05001228static noinline void async_cow_free(struct btrfs_work *work)
1229{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001230 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001231
Nikolay Borisovb5326272019-03-12 17:20:25 +02001232 async_chunk = container_of(work, struct async_chunk, work);
1233 if (async_chunk->inode)
1234 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001235 if (async_chunk->blkcg_css)
1236 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001237 /*
1238 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001239 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001240 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001241 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001242 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001243}
1244
Nikolay Borisov751b6432020-06-03 08:55:22 +03001245static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001246 struct writeback_control *wbc,
1247 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001248 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001249 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001250{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001251 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001252 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001253 struct async_cow *ctx;
1254 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001255 unsigned long nr_pages;
1256 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001257 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1258 int i;
1259 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001260 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001261 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001262
Nikolay Borisov751b6432020-06-03 08:55:22 +03001263 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001264
Nikolay Borisov751b6432020-06-03 08:55:22 +03001265 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001266 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1267 num_chunks = 1;
1268 should_compress = false;
1269 } else {
1270 should_compress = true;
1271 }
1272
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001273 nofs_flag = memalloc_nofs_save();
1274 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1275 memalloc_nofs_restore(nofs_flag);
1276
Nikolay Borisov97db1202019-03-12 17:20:24 +02001277 if (!ctx) {
1278 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1279 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1280 EXTENT_DO_ACCOUNTING;
1281 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1282 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1283 PAGE_SET_ERROR;
1284
Nikolay Borisov751b6432020-06-03 08:55:22 +03001285 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1286 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001287 return -ENOMEM;
1288 }
1289
1290 async_chunk = ctx->chunks;
1291 atomic_set(&ctx->num_chunks, num_chunks);
1292
1293 for (i = 0; i < num_chunks; i++) {
1294 if (should_compress)
1295 cur_end = min(end, start + SZ_512K - 1);
1296 else
1297 cur_end = end;
1298
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001299 /*
1300 * igrab is called higher up in the call chain, take only the
1301 * lightweight reference for the callback lifetime
1302 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001303 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001304 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001305 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001306 async_chunk[i].start = start;
1307 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001308 async_chunk[i].write_flags = write_flags;
1309 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001310
Chris Mason1d53c9e2019-07-10 12:28:16 -07001311 /*
1312 * The locked_page comes all the way from writepage and its
1313 * the original page we were actually given. As we spread
1314 * this large delalloc region across multiple async_chunk
1315 * structs, only the first struct needs a pointer to locked_page
1316 *
1317 * This way we don't need racey decisions about who is supposed
1318 * to unlock it.
1319 */
1320 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001321 /*
1322 * Depending on the compressibility, the pages might or
1323 * might not go through async. We want all of them to
1324 * be accounted against wbc once. Let's do it here
1325 * before the paths diverge. wbc accounting is used
1326 * only for foreign writeback detection and doesn't
1327 * need full accuracy. Just account the whole thing
1328 * against the first page.
1329 */
1330 wbc_account_cgroup_owner(wbc, locked_page,
1331 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001332 async_chunk[i].locked_page = locked_page;
1333 locked_page = NULL;
1334 } else {
1335 async_chunk[i].locked_page = NULL;
1336 }
1337
Chris Masonec39f762019-07-10 12:28:17 -07001338 if (blkcg_css != blkcg_root_css) {
1339 css_get(blkcg_css);
1340 async_chunk[i].blkcg_css = blkcg_css;
1341 } else {
1342 async_chunk[i].blkcg_css = NULL;
1343 }
1344
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001345 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1346 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001347
Nikolay Borisov97db1202019-03-12 17:20:24 +02001348 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001349 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001350
Nikolay Borisov97db1202019-03-12 17:20:24 +02001351 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001352
Chris Mason771ed682008-11-06 22:02:51 -05001353 *nr_written += nr_pages;
1354 start = cur_end + 1;
1355 }
1356 *page_started = 1;
1357 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001358}
1359
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001360static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001361 u64 bytenr, u64 num_bytes)
1362{
1363 int ret;
1364 struct btrfs_ordered_sum *sums;
1365 LIST_HEAD(list);
1366
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001367 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001368 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001369 if (ret == 0 && list_empty(&list))
1370 return 0;
1371
1372 while (!list_empty(&list)) {
1373 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1374 list_del(&sums->list);
1375 kfree(sums);
1376 }
Liu Bo58113752018-01-31 17:09:13 -07001377 if (ret < 0)
1378 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001379 return 1;
1380}
1381
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001382static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001383 const u64 start, const u64 end,
1384 int *page_started, unsigned long *nr_written)
1385{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001386 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1387 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001388 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001389 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001390 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001391 u64 range_start = start;
1392 u64 count;
1393
1394 /*
1395 * If EXTENT_NORESERVE is set it means that when the buffered write was
1396 * made we had not enough available data space and therefore we did not
1397 * reserve data space for it, since we though we could do NOCOW for the
1398 * respective file range (either there is prealloc extent or the inode
1399 * has the NOCOW bit set).
1400 *
1401 * However when we need to fallback to COW mode (because for example the
1402 * block group for the corresponding extent was turned to RO mode by a
1403 * scrub or relocation) we need to do the following:
1404 *
1405 * 1) We increment the bytes_may_use counter of the data space info.
1406 * If COW succeeds, it allocates a new data extent and after doing
1407 * that it decrements the space info's bytes_may_use counter and
1408 * increments its bytes_reserved counter by the same amount (we do
1409 * this at btrfs_add_reserved_bytes()). So we need to increment the
1410 * bytes_may_use counter to compensate (when space is reserved at
1411 * buffered write time, the bytes_may_use counter is incremented);
1412 *
1413 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1414 * that if the COW path fails for any reason, it decrements (through
1415 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1416 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001417 *
1418 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001419 * space cache inode or an inode of the data relocation tree, we must
1420 * also increment bytes_may_use of the data space_info for the same
1421 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001422 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001423 * group that contains that extent to RO mode and therefore force COW
1424 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001425 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001426 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001427 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001428 if (count > 0 || is_space_ino || is_reloc_ino) {
1429 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001430 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001431 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1432
Filipe Manana6bd335b2020-06-08 13:33:05 +01001433 if (is_space_ino || is_reloc_ino)
1434 bytes = range_bytes;
1435
Filipe Manana467dc472020-05-27 11:16:07 +01001436 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001437 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001438 spin_unlock(&sinfo->lock);
1439
Filipe Manana2166e5e2020-05-27 11:16:19 +01001440 if (count > 0)
1441 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1442 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001443 }
1444
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001445 return cow_file_range(inode, locked_page, start, end, page_started,
1446 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001447}
1448
Chris Masond352ac62008-09-29 15:18:18 -04001449/*
1450 * when nowcow writeback call back. This checks for snapshots or COW copies
1451 * of the extents that exist in the file, and COWs the file as required.
1452 *
1453 * If no cow copies or snapshots exist, we write directly to the existing
1454 * blocks on disk
1455 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001456static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001457 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001458 const u64 start, const u64 end,
1459 int *page_started, int force,
1460 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001461{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001462 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1463 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001464 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001465 u64 cow_start = (u64)-1;
1466 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001467 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001468 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001469 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1470 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001471 bool nocow = false;
1472 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001473
1474 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001475 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001476 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001477 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001478 EXTENT_DO_ACCOUNTING |
1479 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001480 PAGE_CLEAR_DIRTY |
1481 PAGE_SET_WRITEBACK |
1482 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001483 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001484 }
Li Zefan82d59022011-04-20 10:33:24 +08001485
Yan Zheng80ff3852008-10-30 14:20:02 -04001486 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001487 struct btrfs_key found_key;
1488 struct btrfs_file_extent_item *fi;
1489 struct extent_buffer *leaf;
1490 u64 extent_end;
1491 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001492 u64 num_bytes = 0;
1493 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001494 u64 ram_bytes;
1495 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001496
1497 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001498
Liu Boe4c3b2d2017-01-30 12:25:28 -08001499 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001500 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001501 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001502 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001503
1504 /*
1505 * If there is no extent for our range when doing the initial
1506 * search, then go back to the previous slot as it will be the
1507 * one containing the search offset
1508 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001509 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1510 leaf = path->nodes[0];
1511 btrfs_item_key_to_cpu(leaf, &found_key,
1512 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001513 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001514 found_key.type == BTRFS_EXTENT_DATA_KEY)
1515 path->slots[0]--;
1516 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001517 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001518next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001519 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001520 leaf = path->nodes[0];
1521 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1522 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001523 if (ret < 0) {
1524 if (cow_start != (u64)-1)
1525 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001526 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001527 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001528 if (ret > 0)
1529 break;
1530 leaf = path->nodes[0];
1531 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001532
Yan Zheng80ff3852008-10-30 14:20:02 -04001533 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001534
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001535 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001536 if (found_key.objectid > ino)
1537 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001538 /*
1539 * Keep searching until we find an EXTENT_ITEM or there are no
1540 * more extents for this inode
1541 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001542 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1543 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1544 path->slots[0]++;
1545 goto next_slot;
1546 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001547
1548 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001549 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001550 found_key.offset > end)
1551 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001552
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001553 /*
1554 * If the found extent starts after requested offset, then
1555 * adjust extent_end to be right before this extent begins
1556 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001557 if (found_key.offset > cur_offset) {
1558 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001559 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001560 goto out_check;
1561 }
Chris Masonc31f8832008-01-08 15:46:31 -05001562
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001563 /*
1564 * Found extent which begins before our range and potentially
1565 * intersect it
1566 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001567 fi = btrfs_item_ptr(leaf, path->slots[0],
1568 struct btrfs_file_extent_item);
1569 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001570
Josef Bacikcc95bef2013-04-04 14:31:27 -04001571 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001572 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1573 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001574 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001575 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001576 extent_end = found_key.offset +
1577 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001578 disk_num_bytes =
1579 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001580 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001581 * If the extent we got ends before our current offset,
1582 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001583 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001584 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001585 path->slots[0]++;
1586 goto next_slot;
1587 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001588 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001589 if (disk_bytenr == 0)
1590 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001591 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001592 if (btrfs_file_extent_compression(leaf, fi) ||
1593 btrfs_file_extent_encryption(leaf, fi) ||
1594 btrfs_file_extent_other_encoding(leaf, fi))
1595 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001596 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001597 * If extent is created before the last volume's snapshot
1598 * this implies the extent is shared, hence we can't do
1599 * nocow. This is the same check as in
1600 * btrfs_cross_ref_exist but without calling
1601 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001602 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001603 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001604 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001605 btrfs_root_last_snapshot(&root->root_item))
1606 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001607 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1608 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001609 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001610 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001611 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001612 ret = btrfs_cross_ref_exist(root, ino,
1613 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001614 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001615 if (ret) {
1616 /*
1617 * ret could be -EIO if the above fails to read
1618 * metadata.
1619 */
1620 if (ret < 0) {
1621 if (cow_start != (u64)-1)
1622 cur_offset = cow_start;
1623 goto error;
1624 }
1625
Nikolay Borisov3e024842019-08-21 10:42:03 +03001626 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001627 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001628 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001629 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001630 disk_bytenr += cur_offset - found_key.offset;
1631 num_bytes = min(end + 1, extent_end) - cur_offset;
1632 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001633 * If there are pending snapshots for this root, we
1634 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001635 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001636 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001637 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001638 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001639 * force cow if csum exists in the range.
1640 * this ensure that csum for a given extent are
1641 * either valid or do not exist.
1642 */
Liu Bo58113752018-01-31 17:09:13 -07001643 ret = csum_exist_in_range(fs_info, disk_bytenr,
1644 num_bytes);
1645 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001646 /*
1647 * ret could be -EIO if the above fails to read
1648 * metadata.
1649 */
1650 if (ret < 0) {
1651 if (cow_start != (u64)-1)
1652 cur_offset = cow_start;
1653 goto error;
1654 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001655 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001656 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001657 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001658 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001659 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001660 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001661 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001662 extent_end = found_key.offset + ram_bytes;
1663 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001664 /* Skip extents outside of our requested range */
1665 if (extent_end <= start) {
1666 path->slots[0]++;
1667 goto next_slot;
1668 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001669 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001670 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001671 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001672 }
1673out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001674 /*
1675 * If nocow is false then record the beginning of the range
1676 * that needs to be COWed
1677 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001678 if (!nocow) {
1679 if (cow_start == (u64)-1)
1680 cow_start = cur_offset;
1681 cur_offset = extent_end;
1682 if (cur_offset > end)
1683 break;
1684 path->slots[0]++;
1685 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001686 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001687
David Sterbab3b4aa72011-04-21 01:20:15 +02001688 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001689
1690 /*
1691 * COW range from cow_start to found_key.offset - 1. As the key
1692 * will contain the beginning of the first extent that can be
1693 * NOCOW, following one which needs to be COW'ed
1694 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001695 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001696 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001697 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001698 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001699 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001700 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001701 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001702 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001703
Yan Zhengd899e052008-10-30 14:25:28 -04001704 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001705 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001706 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001707
Nikolay Borisov968322c2020-06-03 08:55:21 +03001708 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001709 orig_start,
1710 disk_bytenr, /* block_start */
1711 num_bytes, /* block_len */
1712 disk_num_bytes, /* orig_block_len */
1713 ram_bytes, BTRFS_COMPRESS_NONE,
1714 BTRFS_ORDERED_PREALLOC);
1715 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001716 ret = PTR_ERR(em);
1717 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001718 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001719 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001720 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001721 disk_bytenr, num_bytes,
1722 num_bytes,
1723 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001724 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001725 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001726 cur_offset + num_bytes - 1,
1727 0);
1728 goto error;
1729 }
Yan Zhengd899e052008-10-30 14:25:28 -04001730 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001731 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001732 disk_bytenr, num_bytes,
1733 num_bytes,
1734 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001735 if (ret)
1736 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001737 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001738
Filipe Mananaf78c4362016-05-09 13:15:41 +01001739 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001740 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001741 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001742
Yan, Zhengefa56462010-05-16 10:49:59 -04001743 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001744 BTRFS_DATA_RELOC_TREE_OBJECTID)
1745 /*
1746 * Error handled later, as we must prevent
1747 * extent_clear_unlock_delalloc() in error handler
1748 * from freeing metadata of created ordered extent.
1749 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001750 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001751 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001752
Nikolay Borisov968322c2020-06-03 08:55:21 +03001753 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001754 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001755 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001756 EXTENT_DELALLOC |
1757 EXTENT_CLEAR_DATA_RESV,
1758 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1759
Yan Zheng80ff3852008-10-30 14:20:02 -04001760 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001761
1762 /*
1763 * btrfs_reloc_clone_csums() error, now we're OK to call error
1764 * handler, as metadata for created ordered extent will only
1765 * be freed by btrfs_finish_ordered_io().
1766 */
1767 if (ret)
1768 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001769 if (cur_offset > end)
1770 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001771 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001772 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001773
Robbie Ko506481b2018-10-30 18:04:04 +08001774 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001775 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001776
Yan Zheng80ff3852008-10-30 14:20:02 -04001777 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001778 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001779 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1780 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001781 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001782 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001783 }
1784
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001785error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001786 if (nocow)
1787 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1788
Josef Bacik17ca04a2012-05-31 15:58:55 -04001789 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001790 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001791 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001792 EXTENT_DELALLOC | EXTENT_DEFRAG |
1793 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1794 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001795 PAGE_SET_WRITEBACK |
1796 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001797 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001798 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001799}
1800
Nikolay Borisov0c494222020-06-03 08:55:28 +03001801static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001802{
1803
Nikolay Borisov0c494222020-06-03 08:55:28 +03001804 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1805 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001806 return 0;
1807
1808 /*
1809 * @defrag_bytes is a hint value, no spinlock held here,
1810 * if is not zero, it means the file is defragging.
1811 * Force cow if given extent needs to be defragged.
1812 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001813 if (inode->defrag_bytes &&
1814 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001815 return 1;
1816
1817 return 0;
1818}
1819
Chris Masond352ac62008-09-29 15:18:18 -04001820/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001821 * Function to process delayed allocation (create CoW) for ranges which are
1822 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001823 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001824int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001825 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1826 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001827{
Chris Masonbe20aa92007-12-17 20:14:01 -05001828 int ret;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001829 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001830
Nikolay Borisov98456b92020-06-03 08:55:29 +03001831 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1832 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001833 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001834 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1835 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001836 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001837 } else if (!inode_can_compress(inode) ||
1838 !inode_need_compress(inode, start, end)) {
1839 ret = cow_file_range(inode, locked_page, start, end,
1840 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001841 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001842 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1843 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001844 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001845 }
Qu Wenruo524272602017-03-08 10:25:52 +08001846 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001847 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001848 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001849 return ret;
1850}
1851
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001852void btrfs_split_delalloc_extent(struct inode *inode,
1853 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001854{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001855 u64 size;
1856
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001857 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001858 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001859 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001860
Josef Bacikdcab6a32015-02-11 15:08:59 -05001861 size = orig->end - orig->start + 1;
1862 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001863 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001864 u64 new_size;
1865
1866 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001867 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001868 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001869 */
1870 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001871 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001872 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001873 num_extents += count_max_extents(new_size);
1874 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001875 return;
1876 }
1877
Josef Bacik9e0baf62011-07-15 15:16:44 +00001878 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001879 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001880 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001881}
1882
1883/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001884 * Handle merged delayed allocation extents so we can keep track of new extents
1885 * that are just merged onto old extents, such as when we are doing sequential
1886 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001887 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001888void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1889 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001890{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001891 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001892 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001893
Josef Bacik9ed74f22009-09-11 16:12:44 -04001894 /* not delalloc, ignore it */
1895 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001896 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001897
Josef Bacik8461a3d2015-03-13 15:12:08 -04001898 if (new->start > other->start)
1899 new_size = new->end - other->start + 1;
1900 else
1901 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001902
1903 /* we're not bigger than the max, unreserve the space and go */
1904 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1905 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001906 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001907 spin_unlock(&BTRFS_I(inode)->lock);
1908 return;
1909 }
1910
1911 /*
Josef Bacikba117212015-03-13 15:01:24 -04001912 * We have to add up either side to figure out how many extents were
1913 * accounted for before we merged into one big extent. If the number of
1914 * extents we accounted for is <= the amount we need for the new range
1915 * then we can return, otherwise drop. Think of it like this
1916 *
1917 * [ 4k][MAX_SIZE]
1918 *
1919 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1920 * need 2 outstanding extents, on one side we have 1 and the other side
1921 * we have 1 so they are == and we can return. But in this case
1922 *
1923 * [MAX_SIZE+4k][MAX_SIZE+4k]
1924 *
1925 * Each range on their own accounts for 2 extents, but merged together
1926 * they are only 3 extents worth of accounting, so we need to drop in
1927 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001928 */
Josef Bacikba117212015-03-13 15:01:24 -04001929 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001930 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001931 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001932 num_extents += count_max_extents(old_size);
1933 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001934 return;
1935
Josef Bacik9e0baf62011-07-15 15:16:44 +00001936 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001937 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001938 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001939}
1940
Miao Xieeb73c1b2013-05-15 07:48:22 +00001941static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1942 struct inode *inode)
1943{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001944 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1945
Miao Xieeb73c1b2013-05-15 07:48:22 +00001946 spin_lock(&root->delalloc_lock);
1947 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1948 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1949 &root->delalloc_inodes);
1950 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1951 &BTRFS_I(inode)->runtime_flags);
1952 root->nr_delalloc_inodes++;
1953 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001954 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001955 BUG_ON(!list_empty(&root->delalloc_root));
1956 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001957 &fs_info->delalloc_roots);
1958 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001959 }
1960 }
1961 spin_unlock(&root->delalloc_lock);
1962}
1963
Nikolay Borisov2b877332018-04-27 12:21:51 +03001964
1965void __btrfs_del_delalloc_inode(struct btrfs_root *root,
1966 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00001967{
David Sterba3ffbd682018-06-29 10:56:42 +02001968 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001969
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001970 if (!list_empty(&inode->delalloc_inodes)) {
1971 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001972 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02001973 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001974 root->nr_delalloc_inodes--;
1975 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03001976 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001977 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001978 BUG_ON(list_empty(&root->delalloc_root));
1979 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001980 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001981 }
1982 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03001983}
1984
1985static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1986 struct btrfs_inode *inode)
1987{
1988 spin_lock(&root->delalloc_lock);
1989 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001990 spin_unlock(&root->delalloc_lock);
1991}
1992
Chris Masond352ac62008-09-29 15:18:18 -04001993/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001994 * Properly track delayed allocation bytes in the inode and to maintain the
1995 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04001996 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02001997void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
1998 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05001999{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002000 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2001
Wang Shilong47059d92014-07-03 18:22:07 +08002002 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2003 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002004 /*
2005 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002006 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002007 * bit, which is only set or cleared with irqs on
2008 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002009 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002010 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002011 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002012 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002013 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002014
Josef Bacik8b62f872017-10-19 14:15:55 -04002015 spin_lock(&BTRFS_I(inode)->lock);
2016 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2017 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002018
Josef Bacik6a3891c2015-03-16 17:38:52 -04002019 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002020 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002021 return;
2022
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002023 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2024 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002025 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002026 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002027 if (*bits & EXTENT_DEFRAG)
2028 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002029 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002030 &BTRFS_I(inode)->runtime_flags))
2031 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002032 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002033 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002034
2035 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2036 (*bits & EXTENT_DELALLOC_NEW)) {
2037 spin_lock(&BTRFS_I(inode)->lock);
2038 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2039 state->start;
2040 spin_unlock(&BTRFS_I(inode)->lock);
2041 }
Chris Mason291d6732008-01-29 15:55:23 -05002042}
2043
Chris Masond352ac62008-09-29 15:18:18 -04002044/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002045 * Once a range is no longer delalloc this function ensures that proper
2046 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002047 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002048void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2049 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002050{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002051 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2052 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002053 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002054 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002055
Filipe Manana4a4b9642017-07-27 19:52:55 +01002056 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2057 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002058 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002059 spin_unlock(&inode->lock);
2060 }
Wang Shilong47059d92014-07-03 18:22:07 +08002061
Chris Mason75eff682008-12-15 15:54:40 -05002062 /*
2063 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002064 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002065 * bit, which is only set or cleared with irqs on
2066 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002067 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002068 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002069 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002070
Josef Bacik8b62f872017-10-19 14:15:55 -04002071 spin_lock(&inode->lock);
2072 btrfs_mod_outstanding_extents(inode, -num_extents);
2073 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002074
Josef Bacikb6d08f02013-09-27 14:57:43 -04002075 /*
2076 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002077 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002078 * error.
2079 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002080 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002081 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002082 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002083
Josef Bacik6a3891c2015-03-16 17:38:52 -04002084 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002085 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002086 return;
2087
Filipe Mananaa315e682017-03-06 23:04:20 +00002088 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2089 do_list && !(state->state & EXTENT_NORESERVE) &&
2090 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002091 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002092
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002093 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2094 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002095 spin_lock(&inode->lock);
2096 inode->delalloc_bytes -= len;
2097 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002098 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002099 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002100 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002101 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002102 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002103
2104 if ((state->state & EXTENT_DELALLOC_NEW) &&
2105 (*bits & EXTENT_DELALLOC_NEW)) {
2106 spin_lock(&inode->lock);
2107 ASSERT(inode->new_delalloc_bytes >= len);
2108 inode->new_delalloc_bytes -= len;
2109 spin_unlock(&inode->lock);
2110 }
Chris Mason291d6732008-01-29 15:55:23 -05002111}
2112
Chris Masond352ac62008-09-29 15:18:18 -04002113/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002114 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2115 * in a chunk's stripe. This function ensures that bios do not span a
2116 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002117 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002118 * @page - The page we are about to add to the bio
2119 * @size - size we want to add to the bio
2120 * @bio - bio we want to ensure is smaller than a stripe
2121 * @bio_flags - flags of the bio
2122 *
2123 * return 1 if page cannot be added to the bio
2124 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002125 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002126 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002127int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2128 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002129{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002130 struct inode *inode = page->mapping->host;
2131 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002132 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002133 u64 length = 0;
2134 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002135 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002136 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002137
Chris Mason771ed682008-11-06 22:02:51 -05002138 if (bio_flags & EXTENT_BIO_COMPRESSED)
2139 return 0;
2140
Kent Overstreet4f024f32013-10-11 15:44:27 -07002141 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002142 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002143 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2144 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002145 if (ret < 0)
2146 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002147
2148 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002149 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002150 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002151}
2152
Chris Masond352ac62008-09-29 15:18:18 -04002153/*
2154 * in order to insert checksums into the metadata in large chunks,
2155 * we wait until bio submission time. All the pages in the bio are
2156 * checksummed and sums are attached onto the ordered extent record.
2157 *
2158 * At IO completion time the cums attached on the ordered extent record
2159 * are inserted into the btree
2160 */
David Sterbad0ee3932018-03-08 14:35:48 +01002161static blk_status_t btrfs_submit_bio_start(void *private_data, struct bio *bio,
Chris Masoneaf25d92010-05-25 09:48:28 -04002162 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002163{
Josef Bacikc6100a42017-05-05 11:57:13 -04002164 struct inode *inode = private_data;
Chris Masone0156402008-04-16 11:15:20 -04002165
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002166 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002167}
Chris Masone0156402008-04-16 11:15:20 -04002168
Chris Mason4a69a412008-11-06 22:03:00 -05002169/*
Chris Masoncad321a2008-12-17 14:51:42 -05002170 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002171 * on write, or reading the csums from the tree before a read.
2172 *
2173 * Rules about async/sync submit,
2174 * a) read: sync submit
2175 *
2176 * b) write without checksum: sync submit
2177 *
2178 * c) write with checksum:
2179 * c-1) if bio is issued by fsync: sync submit
2180 * (sync_writers != 0)
2181 *
2182 * c-2) if root is reloc root: sync submit
2183 * (only in case of buffered IO)
2184 *
2185 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002186 */
Nikolay Borisova56b1c72019-04-10 17:24:39 +03002187static blk_status_t btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
Nikolay Borisov50489a52019-04-10 19:46:04 +03002188 int mirror_num,
2189 unsigned long bio_flags)
2190
Chris Mason44b8bd72008-04-16 11:14:51 -04002191{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002192 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302194 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002195 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002196 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002197 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002198
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002199 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
Chris Masoncad321a2008-12-17 14:51:42 -05002200
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002201 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302202 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002203
Mike Christie37226b22016-06-05 14:31:52 -05002204 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002205 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002206 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002207 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002208
Chris Masond20f7042008-12-08 16:58:54 -05002209 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002210 ret = btrfs_submit_compressed_read(inode, bio,
2211 mirror_num,
2212 bio_flags);
2213 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002214 } else if (!skip_sum) {
Omar Sandovaldb72e472019-12-10 10:37:35 -08002215 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002216 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002217 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002218 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002219 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002220 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002221 /* csum items have already been cloned */
2222 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2223 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002224 /* we're doing a write, do the async checksumming */
Josef Bacikc6100a42017-05-05 11:57:13 -04002225 ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, bio_flags,
Nikolay Borisove7681162019-04-10 17:24:41 +03002226 0, inode, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002227 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002228 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002229 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002230 if (ret)
2231 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002232 }
2233
Chris Mason0b86a832008-03-24 15:01:56 -04002234mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002235 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002236
2237out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002238 if (ret) {
2239 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002240 bio_endio(bio);
2241 }
Stefan Behrens61891922012-11-05 18:51:52 +01002242 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002243}
Chris Mason6885f302008-02-20 16:11:05 -05002244
Chris Masond352ac62008-09-29 15:18:18 -04002245/*
2246 * given a list of ordered sums record them in the inode. This happens
2247 * at IO completion time based on sums calculated at bio submission time.
2248 */
Chris Masonba1da2f2008-07-17 12:54:15 -04002249static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
David Sterbadf9f6282017-02-10 19:35:37 +01002250 struct inode *inode, struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002251{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002252 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002253 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002254
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002255 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002256 trans->adding_csums = true;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002257 ret = btrfs_csum_file_blocks(trans,
Chris Masond20f7042008-12-08 16:58:54 -05002258 BTRFS_I(inode)->root->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002259 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002260 if (ret)
2261 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002262 }
2263 return 0;
2264}
2265
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002266int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002267 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002268 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002269{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002270 WARN_ON(PAGE_ALIGNED(end));
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002271 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2272 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002273}
2274
Chris Masond352ac62008-09-29 15:18:18 -04002275/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002276struct btrfs_writepage_fixup {
2277 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002278 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002279 struct btrfs_work work;
2280};
2281
Christoph Hellwigb2950862008-12-02 09:54:17 -05002282static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002283{
2284 struct btrfs_writepage_fixup *fixup;
2285 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002286 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002287 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002288 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002289 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002290 u64 page_start;
2291 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002292 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002293 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002294
2295 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2296 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002297 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002298 page_start = page_offset(page);
2299 page_end = page_offset(page) + PAGE_SIZE - 1;
2300
2301 /*
2302 * This is similar to page_mkwrite, we need to reserve the space before
2303 * we take the page lock.
2304 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002305 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2306 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002307again:
Chris Mason247e7432008-07-17 12:53:51 -04002308 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002309
Chris Mason25f3c502020-01-21 11:51:42 -05002310 /*
2311 * Before we queued this fixup, we took a reference on the page.
2312 * page->mapping may go NULL, but it shouldn't be moved to a different
2313 * address space.
2314 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002315 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2316 /*
2317 * Unfortunately this is a little tricky, either
2318 *
2319 * 1) We got here and our page had already been dealt with and
2320 * we reserved our space, thus ret == 0, so we need to just
2321 * drop our space reservation and bail. This can happen the
2322 * first time we come into the fixup worker, or could happen
2323 * while waiting for the ordered extent.
2324 * 2) Our page was already dealt with, but we happened to get an
2325 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2326 * this case we obviously don't have anything to release, but
2327 * because the page was already dealt with we don't want to
2328 * mark the page with an error, so make sure we're resetting
2329 * ret to 0. This is why we have this check _before_ the ret
2330 * check, because we do not want to have a surprise ENOSPC
2331 * when the page was already properly dealt with.
2332 */
2333 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002334 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2335 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002336 page_start, PAGE_SIZE,
2337 true);
2338 }
2339 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002340 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002341 }
Chris Mason25f3c502020-01-21 11:51:42 -05002342
2343 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002344 * We can't mess with the page state unless it is locked, so now that
2345 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002346 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002347 if (ret)
2348 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002349
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002350 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002351
2352 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002353 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002354 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002355
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002356 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002357 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002358 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2359 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002360 unlock_page(page);
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002361 btrfs_start_ordered_extent(&inode->vfs_inode, ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002362 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002363 goto again;
2364 }
Chris Mason247e7432008-07-17 12:53:51 -04002365
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002366 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002367 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002368 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002369 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002370
Chris Mason25f3c502020-01-21 11:51:42 -05002371 /*
2372 * Everything went as planned, we're now the owner of a dirty page with
2373 * delayed allocation bits set and space reserved for our COW
2374 * destination.
2375 *
2376 * The page was dirty when we started, nothing should have cleaned it.
2377 */
2378 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002379 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002380out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002381 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002382 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002383 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2384 PAGE_SIZE, true);
2385 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002386 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002387out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002388 if (ret) {
2389 /*
2390 * We hit ENOSPC or other errors. Update the mapping and page
2391 * to reflect the errors and clean the page.
2392 */
2393 mapping_set_error(page->mapping, ret);
2394 end_extent_writepage(page, ret, page_start, page_end);
2395 clear_page_dirty_for_io(page);
2396 SetPageError(page);
2397 }
2398 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002399 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002400 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002401 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002402 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002403 /*
2404 * As a precaution, do a delayed iput in case it would be the last iput
2405 * that could need flushing space. Recursing back to fixup worker would
2406 * deadlock.
2407 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002408 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002409}
2410
2411/*
2412 * There are a few paths in the higher layers of the kernel that directly
2413 * set the page dirty bit without asking the filesystem if it is a
2414 * good idea. This causes problems because we want to make sure COW
2415 * properly happens and the data=ordered rules are followed.
2416 *
Chris Masonc8b97812008-10-29 14:49:59 -04002417 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002418 * hasn't been properly setup for IO. We kick off an async process
2419 * to fix it up. The async helper will wait for ordered extents, set
2420 * the delalloc bit and make it safe to write the page.
2421 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002422int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002423{
2424 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002425 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002426 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002427
Chris Mason8b62b722009-09-02 16:53:46 -04002428 /* this page is properly in the ordered list */
2429 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002430 return 0;
2431
Chris Mason25f3c502020-01-21 11:51:42 -05002432 /*
2433 * PageChecked is set below when we create a fixup worker for this page,
2434 * don't try to create another one if we're already PageChecked()
2435 *
2436 * The extent_io writepage code will redirty the page if we send back
2437 * EAGAIN.
2438 */
Chris Mason247e7432008-07-17 12:53:51 -04002439 if (PageChecked(page))
2440 return -EAGAIN;
2441
2442 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2443 if (!fixup)
2444 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002445
Josef Bacikf4b13632020-01-21 14:34:52 -05002446 /*
2447 * We are already holding a reference to this inode from
2448 * write_cache_pages. We need to hold it because the space reservation
2449 * takes place outside of the page lock, and we can't trust
2450 * page->mapping outside of the page lock.
2451 */
2452 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002453 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002454 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002455 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002456 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002457 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002458 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002459
2460 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002461}
2462
Yan Zhengd899e052008-10-30 14:25:28 -04002463static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002464 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002465 struct btrfs_file_extent_item *stack_fi,
2466 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002467{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002468 struct btrfs_root *root = inode->root;
Yan Zhengd899e052008-10-30 14:25:28 -04002469 struct btrfs_path *path;
2470 struct extent_buffer *leaf;
2471 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002472 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2473 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2474 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2475 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002476 int extent_inserted = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04002477 int ret;
2478
2479 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002480 if (!path)
2481 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002482
Chris Masona1ed8352009-09-11 12:27:37 -04002483 /*
2484 * we may be replacing one extent in the tree with another.
2485 * The new extent is pinned in the extent map, and we don't want
2486 * to drop it from the cache until it is completely in the btree.
2487 *
2488 * So, tell btrfs_drop_extents to leave this extent in the cache.
2489 * the caller is expected to unpin it and allow it to be merged
2490 * with the others.
2491 */
Nikolay Borisovc553f942020-06-03 08:55:19 +03002492 ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002493 file_pos + num_bytes, NULL, 0,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002494 1, sizeof(*stack_fi), &extent_inserted);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002495 if (ret)
2496 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002497
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002498 if (!extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002499 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002500 ins.offset = file_pos;
2501 ins.type = BTRFS_EXTENT_DATA_KEY;
2502
2503 path->leave_spinning = 1;
2504 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002505 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002506 if (ret)
2507 goto out;
2508 }
Yan Zhengd899e052008-10-30 14:25:28 -04002509 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002510 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2511 write_extent_buffer(leaf, stack_fi,
2512 btrfs_item_ptr_offset(leaf, path->slots[0]),
2513 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002514
Yan Zhengd899e052008-10-30 14:25:28 -04002515 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002516 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002517
Nikolay Borisovc553f942020-06-03 08:55:19 +03002518 inode_add_bytes(&inode->vfs_inode, num_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -04002519
2520 ins.objectid = disk_bytenr;
2521 ins.offset = disk_num_bytes;
2522 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002523
Nikolay Borisovc553f942020-06-03 08:55:19 +03002524 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002525 if (ret)
2526 goto out;
2527
Nikolay Borisovc553f942020-06-03 08:55:19 +03002528 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002529 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002530out:
Yan Zhengd899e052008-10-30 14:25:28 -04002531 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002532
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002533 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002534}
2535
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002536static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002537 u64 start, u64 len)
2538{
David Sterba32da53862019-10-29 19:20:18 +01002539 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002540
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002541 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002542 ASSERT(cache);
2543
2544 spin_lock(&cache->lock);
2545 cache->delalloc_bytes -= len;
2546 spin_unlock(&cache->lock);
2547
2548 btrfs_put_block_group(cache);
2549}
2550
Qu Wenruo203f44c52020-06-10 09:04:40 +08002551static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
2552 struct inode *inode,
2553 struct btrfs_ordered_extent *oe)
2554{
2555 struct btrfs_file_extent_item stack_fi;
2556 u64 logical_len;
2557
2558 memset(&stack_fi, 0, sizeof(stack_fi));
2559 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2560 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2561 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2562 oe->disk_num_bytes);
2563 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2564 logical_len = oe->truncated_len;
2565 else
2566 logical_len = oe->num_bytes;
2567 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2568 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2569 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2570 /* Encryption and other encoding is reserved and all 0 */
2571
Nikolay Borisovc553f942020-06-03 08:55:19 +03002572 return insert_reserved_file_extent(trans, BTRFS_I(inode), oe->file_offset,
Qu Wenruo7dbeaad2020-06-10 09:04:43 +08002573 &stack_fi, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002574}
2575
2576/*
2577 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002578 * an ordered extent if the range of bytes in the file it covers are
2579 * fully written.
2580 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002581static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002582{
Josef Bacik5fd02042012-05-02 14:00:54 -04002583 struct inode *inode = ordered_extent->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002584 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002585 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002586 struct btrfs_trans_handle *trans = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002587 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002588 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002589 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002590 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002591 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002592 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002593 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002594 bool truncated = false;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002595 bool range_locked = false;
2596 bool clear_new_delalloc_bytes = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002597 bool clear_reserved_extent = true;
Omar Sandoval313facc2019-12-02 17:34:18 -08002598 unsigned int clear_bits;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002599
Omar Sandovalbffe6332019-12-02 17:34:19 -08002600 start = ordered_extent->file_offset;
2601 end = start + ordered_extent->num_bytes - 1;
2602
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002603 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2604 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2605 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
2606 clear_new_delalloc_bytes = true;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002607
Nikolay Borisov8d510122019-10-08 20:43:06 +03002608 freespace_inode = btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik0cb59c92010-07-02 12:14:14 -04002609
Josef Bacik5fd02042012-05-02 14:00:54 -04002610 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2611 ret = -EIO;
2612 goto out;
2613 }
2614
Omar Sandovalbffe6332019-12-02 17:34:19 -08002615 btrfs_free_io_failure_record(BTRFS_I(inode), start, end);
Miao Xief6124962014-09-12 18:44:04 +08002616
Josef Bacik77cef2e2013-08-29 13:57:21 -04002617 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2618 truncated = true;
2619 logical_len = ordered_extent->truncated_len;
2620 /* Truncated the entire extent, don't bother adding */
2621 if (!logical_len)
2622 goto out;
2623 }
2624
Yan, Zhengc2167752009-11-12 09:34:21 +00002625 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002626 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002627
Josef Bacikd923afe2020-01-17 09:02:23 -05002628 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002629 if (freespace_inode)
2630 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002631 else
2632 trans = btrfs_join_transaction(root);
2633 if (IS_ERR(trans)) {
2634 ret = PTR_ERR(trans);
2635 trans = NULL;
2636 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002637 }
Josef Bacik69fe2d72017-10-19 14:15:57 -04002638 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Josef Bacik6c760c02012-11-09 10:53:21 -05002639 ret = btrfs_update_inode_fallback(trans, root, inode);
2640 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002641 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002642 goto out;
2643 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002644
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002645 range_locked = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002646 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002647
Nikolay Borisov8d510122019-10-08 20:43:06 +03002648 if (freespace_inode)
2649 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002650 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002651 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002652 if (IS_ERR(trans)) {
2653 ret = PTR_ERR(trans);
2654 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002655 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002656 }
Chris Masona79b7d42014-05-22 16:18:52 -07002657
Josef Bacik69fe2d72017-10-19 14:15:57 -04002658 trans->block_rsv = &BTRFS_I(inode)->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002659
Chris Masonc8b97812008-10-29 14:49:59 -04002660 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002661 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002662 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002663 BUG_ON(compress_type);
Nikolay Borisov7a6d7062017-02-20 13:50:48 +02002664 ret = btrfs_mark_extent_written(trans, BTRFS_I(inode),
Yan Zhengd899e052008-10-30 14:25:28 -04002665 ordered_extent->file_offset,
2666 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002667 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002668 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002669 BUG_ON(root == fs_info->tree_root);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002670 ret = insert_ordered_extent_file_extent(trans, inode,
2671 ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002672 if (!ret) {
2673 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002674 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002675 ordered_extent->disk_bytenr,
2676 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002677 }
Yan Zhengd899e052008-10-30 14:25:28 -04002678 }
Josef Bacik5dc562c2012-08-17 13:14:17 -04002679 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002680 ordered_extent->file_offset,
2681 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002682 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002683 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002684 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002685 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002686
Nikolay Borisovac01f262018-01-08 10:59:43 +02002687 ret = add_pending_csums(trans, inode, &ordered_extent->list);
2688 if (ret) {
2689 btrfs_abort_transaction(trans, ret);
2690 goto out;
2691 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002692
Josef Bacikd923afe2020-01-17 09:02:23 -05002693 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacik6c760c02012-11-09 10:53:21 -05002694 ret = btrfs_update_inode_fallback(trans, root, inode);
2695 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002696 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002697 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002698 }
2699 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002700out:
Omar Sandoval313facc2019-12-02 17:34:18 -08002701 clear_bits = EXTENT_DEFRAG;
2702 if (range_locked)
2703 clear_bits |= EXTENT_LOCKED;
2704 if (clear_new_delalloc_bytes)
2705 clear_bits |= EXTENT_DELALLOC_NEW;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002706 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, clear_bits,
2707 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002708 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002709
Miao Xiea698d0752012-09-20 01:51:59 -06002710 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002711 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002712
Josef Bacik77cef2e2013-08-29 13:57:21 -04002713 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002714 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002715
2716 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002717 unwritten_start += logical_len;
2718 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002719
2720 /* Drop the cache for the part of the extent we didn't write. */
Omar Sandovalbffe6332019-12-02 17:34:19 -08002721 btrfs_drop_extent_cache(BTRFS_I(inode), unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002722
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002723 /*
2724 * If the ordered extent had an IOERR or something else went
2725 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002726 * back to the allocator. We only free the extent in the
2727 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002728 *
2729 * If we made it past insert_reserved_file_extent before we
2730 * errored out then we don't need to do this as the accounting
2731 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002732 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002733 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002734 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002735 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002736 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2737 /*
2738 * Discard the range before returning it back to the
2739 * free space pool
2740 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002741 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002742 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002743 ordered_extent->disk_bytenr,
2744 ordered_extent->disk_num_bytes,
2745 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002746 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002747 ordered_extent->disk_bytenr,
2748 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002749 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002750 }
2751
Josef Bacik5fd02042012-05-02 14:00:54 -04002752 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002753 * This needs to be done to make sure anybody waiting knows we are done
2754 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002755 */
2756 btrfs_remove_ordered_extent(inode, ordered_extent);
2757
Chris Masone6dcd2d2008-07-17 12:53:50 -04002758 /* once for us */
2759 btrfs_put_ordered_extent(ordered_extent);
2760 /* once for the tree */
2761 btrfs_put_ordered_extent(ordered_extent);
2762
Josef Bacik5fd02042012-05-02 14:00:54 -04002763 return ret;
2764}
2765
2766static void finish_ordered_fn(struct btrfs_work *work)
2767{
2768 struct btrfs_ordered_extent *ordered_extent;
2769 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2770 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002771}
2772
Nikolay Borisovc6297322018-11-08 10:18:08 +02002773void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2774 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002775{
Nikolay Borisov3347c482020-08-31 14:42:44 +03002776 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2777 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04002778 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002779 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002780
liubo1abe9b82011-03-24 11:18:59 +00002781 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2782
Chris Mason8b62b722009-09-02 16:53:46 -04002783 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03002784 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2785 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002786 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002787
Nikolay Borisov3347c482020-08-31 14:42:44 +03002788 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002789 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002790 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002791 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002792
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002793 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002794 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002795}
2796
Omar Sandoval47df7762020-04-16 14:46:17 -07002797static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
2798 int icsum, struct page *page, int pgoff, u64 start,
2799 size_t len)
Miao Xiedc380ae2014-09-12 18:43:55 +08002800{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002801 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2802 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002803 char *kaddr;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002804 u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
2805 u8 *csum_expected;
2806 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002807
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002808 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002809
2810 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002811 shash->tfm = fs_info->csum_shash;
2812
Eric Biggersfd080012020-04-30 23:51:59 -07002813 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002814
2815 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002816 goto zeroit;
2817
2818 kunmap_atomic(kaddr);
2819 return 0;
2820zeroit:
Johannes Thumshirnea41d6b2019-06-03 16:58:58 +02002821 btrfs_print_data_csum_error(BTRFS_I(inode), start, csum, csum_expected,
2822 io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03002823 if (io_bio->device)
2824 btrfs_dev_stat_inc_and_print(io_bio->device,
2825 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08002826 memset(kaddr + pgoff, 1, len);
2827 flush_dcache_page(page);
2828 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002829 return -EIO;
2830}
2831
Chris Masond352ac62008-09-29 15:18:18 -04002832/*
Chris Masond352ac62008-09-29 15:18:18 -04002833 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002834 * if there's a match, we allow the bio to finish. If not, the code in
2835 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002836 */
Miao Xiefacc8a222013-07-25 19:22:34 +08002837static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
2838 u64 phy_offset, struct page *page,
2839 u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002840{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002841 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002842 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002843 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002844 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002845
Chris Masond20f7042008-12-08 16:58:54 -05002846 if (PageChecked(page)) {
2847 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002848 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002849 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02002850
2851 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08002852 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002853
Yan Zheng17d217f2008-12-12 10:03:38 -05002854 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04002855 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02002856 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05002857 return 0;
2858 }
2859
Miao Xiefacc8a222013-07-25 19:22:34 +08002860 phy_offset >>= inode->i_sb->s_blocksize_bits;
Omar Sandoval47df7762020-04-16 14:46:17 -07002861 return check_data_csum(inode, io_bio, phy_offset, page, offset, start,
2862 (size_t)(end - start + 1));
Chris Mason07157aa2007-08-30 08:50:51 -04002863}
Chris Masonb888db22007-08-27 16:49:44 -04002864
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002865/*
2866 * btrfs_add_delayed_iput - perform a delayed iput on @inode
2867 *
2868 * @inode: The inode we want to perform iput on
2869 *
2870 * This function uses the generic vfs_inode::i_count to track whether we should
2871 * just decrement it (in case it's > 1) or if this is the last iput then link
2872 * the inode to the delayed iput machinery. Delayed iputs are processed at
2873 * transaction commit time/superblock commit/cleaner kthread.
2874 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002875void btrfs_add_delayed_iput(struct inode *inode)
2876{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002877 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01002878 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002879
2880 if (atomic_add_unless(&inode->i_count, -1, 1))
2881 return;
2882
Josef Bacik034f7842018-12-03 11:06:52 -05002883 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002884 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02002885 ASSERT(list_empty(&binode->delayed_iput));
2886 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002887 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05002888 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
2889 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002890}
2891
Josef Bacik63611e72019-06-18 10:59:18 -04002892static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
2893 struct btrfs_inode *inode)
2894{
2895 list_del_init(&inode->delayed_iput);
2896 spin_unlock(&fs_info->delayed_iput_lock);
2897 iput(&inode->vfs_inode);
2898 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
2899 wake_up(&fs_info->delayed_iputs_wait);
2900 spin_lock(&fs_info->delayed_iput_lock);
2901}
2902
2903static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
2904 struct btrfs_inode *inode)
2905{
2906 if (!list_empty(&inode->delayed_iput)) {
2907 spin_lock(&fs_info->delayed_iput_lock);
2908 if (!list_empty(&inode->delayed_iput))
2909 run_delayed_iput_locked(fs_info, inode);
2910 spin_unlock(&fs_info->delayed_iput_lock);
2911 }
2912}
2913
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002914void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002915{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002916
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002917 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01002918 while (!list_empty(&fs_info->delayed_iputs)) {
2919 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002920
David Sterba8089fe62015-11-19 14:15:51 +01002921 inode = list_first_entry(&fs_info->delayed_iputs,
2922 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04002923 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002924 }
David Sterba8089fe62015-11-19 14:15:51 +01002925 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00002926}
2927
Josef Bacik034f7842018-12-03 11:06:52 -05002928/**
2929 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
2930 * @fs_info - the fs_info for this fs
2931 * @return - EINTR if we were killed, 0 if nothing's pending
2932 *
2933 * This will wait on any delayed iputs that are currently running with KILLABLE
2934 * set. Once they are all done running we will return, unless we are killed in
2935 * which case we return EINTR. This helps in user operations like fallocate etc
2936 * that might get blocked on the iputs.
2937 */
2938int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
2939{
2940 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
2941 atomic_read(&fs_info->nr_delayed_iputs) == 0);
2942 if (ret)
2943 return -EINTR;
2944 return 0;
2945}
2946
Yan, Zhengd68fc572010-05-16 10:49:58 -04002947/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002948 * This creates an orphan entry for the given inode in case something goes wrong
2949 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04002950 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02002951int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07002952 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002953{
Yan, Zhengd68fc572010-05-16 10:49:58 -04002954 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04002955
Omar Sandoval27919062018-05-11 13:13:37 -07002956 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
2957 if (ret && ret != -EEXIST) {
2958 btrfs_abort_transaction(trans, ret);
2959 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04002960 }
2961
Yan, Zhengd68fc572010-05-16 10:49:58 -04002962 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002963}
2964
2965/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002966 * We have done the delete so we can go ahead and remove the orphan item for
2967 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04002968 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00002969static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02002970 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04002971{
Omar Sandoval27919062018-05-11 13:13:37 -07002972 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04002973}
2974
2975/*
2976 * this cleans up any orphans that may be left on the list from the last use
2977 * of this root.
2978 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002979int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04002980{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002981 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04002982 struct btrfs_path *path;
2983 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04002984 struct btrfs_key key, found_key;
2985 struct btrfs_trans_handle *trans;
2986 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04002987 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07002988 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04002989
Yan, Zhengd68fc572010-05-16 10:49:58 -04002990 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002991 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00002992
2993 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05002994 if (!path) {
2995 ret = -ENOMEM;
2996 goto out;
2997 }
David Sterbae4058b52015-11-27 16:31:35 +01002998 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04002999
3000 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003001 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003002 key.offset = (u64)-1;
3003
Josef Bacik7b128762008-07-24 12:17:14 -04003004 while (1) {
3005 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003006 if (ret < 0)
3007 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003008
3009 /*
3010 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003011 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003012 * find the key and see if we have stuff that matches
3013 */
3014 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003015 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003016 if (path->slots[0] == 0)
3017 break;
3018 path->slots[0]--;
3019 }
3020
3021 /* pull out the item */
3022 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003023 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3024
3025 /* make sure the item matches what we want */
3026 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3027 break;
David Sterba962a2982014-06-04 18:41:45 +02003028 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003029 break;
3030
3031 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003032 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003033
3034 /*
3035 * this is where we are basically btrfs_lookup, without the
3036 * crossing root thing. we store the inode number in the
3037 * offset of the orphan item.
3038 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003039
3040 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003041 btrfs_err(fs_info,
3042 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003043 ret = -EINVAL;
3044 goto out;
3045 }
3046
3047 last_objectid = found_key.offset;
3048
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003049 found_key.objectid = found_key.offset;
3050 found_key.type = BTRFS_INODE_ITEM_KEY;
3051 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003052 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303053 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003054 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003055 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003056
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003057 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003058 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003059 int is_dead_root = 0;
3060
3061 /*
3062 * this is an orphan in the tree root. Currently these
3063 * could come from 2 sources:
3064 * a) a snapshot deletion in progress
3065 * b) a free space cache inode
3066 * We need to distinguish those two, as the snapshot
3067 * orphan must not get deleted.
3068 * find_dead_roots already ran before us, so if this
3069 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003070 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003071 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003072
3073 spin_lock(&fs_info->fs_roots_radix_lock);
3074 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3075 (unsigned long)found_key.objectid);
3076 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3077 is_dead_root = 1;
3078 spin_unlock(&fs_info->fs_roots_radix_lock);
3079
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003080 if (is_dead_root) {
3081 /* prevent this orphan from being found again */
3082 key.offset = found_key.objectid - 1;
3083 continue;
3084 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003085
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003086 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003087
Josef Bacika8c9e572011-09-21 16:55:59 -04003088 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003089 * If we have an inode with links, there are a couple of
3090 * possibilities. Old kernels (before v3.12) used to create an
3091 * orphan item for truncate indicating that there were possibly
3092 * extent items past i_size that needed to be deleted. In v3.12,
3093 * truncate was changed to update i_size in sync with the extent
3094 * items, but the (useless) orphan item was still created. Since
3095 * v4.18, we don't create the orphan item for truncate at all.
3096 *
3097 * So, this item could mean that we need to do a truncate, but
3098 * only if this filesystem was last used on a pre-v3.12 kernel
3099 * and was not cleanly unmounted. The odds of that are quite
3100 * slim, and it's a pain to do the truncate now, so just delete
3101 * the orphan item.
3102 *
3103 * It's also possible that this orphan item was supposed to be
3104 * deleted but wasn't. The inode number may have been reused,
3105 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003106 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003107 if (ret == -ENOENT || inode->i_nlink) {
3108 if (!ret)
3109 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003110 trans = btrfs_start_transaction(root, 1);
3111 if (IS_ERR(trans)) {
3112 ret = PTR_ERR(trans);
3113 goto out;
3114 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003115 btrfs_debug(fs_info, "auto deleting %Lu",
3116 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003117 ret = btrfs_del_orphan_item(trans, root,
3118 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003119 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003120 if (ret)
3121 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003122 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003123 }
Josef Bacik7b128762008-07-24 12:17:14 -04003124
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003125 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003126
3127 /* this will do delete_inode and everything for us */
3128 iput(inode);
3129 }
Miao Xie3254c872011-11-10 20:45:05 -05003130 /* release the path since we're done with it */
3131 btrfs_release_path(path);
3132
Yan, Zhengd68fc572010-05-16 10:49:58 -04003133 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3134
Omar Sandovala575cee2018-05-11 13:13:38 -07003135 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003136 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003137 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003138 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003139 }
Josef Bacik7b128762008-07-24 12:17:14 -04003140
3141 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003142 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003143
3144out:
3145 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003146 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003147 btrfs_free_path(path);
3148 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003149}
3150
Chris Masond352ac62008-09-29 15:18:18 -04003151/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003152 * very simple check to peek ahead in the leaf looking for xattrs. If we
3153 * don't find any xattrs, we know there can't be any acls.
3154 *
3155 * slot is the slot the inode is in, objectid is the objectid of the inode
3156 */
3157static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003158 int slot, u64 objectid,
3159 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003160{
3161 u32 nritems = btrfs_header_nritems(leaf);
3162 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003163 static u64 xattr_access = 0;
3164 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003165 int scanned = 0;
3166
Josef Bacikf23b5a52013-06-19 10:16:26 -04003167 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003168 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3169 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3170 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3171 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003172 }
3173
Chris Mason46a53cc2009-04-27 11:47:50 -04003174 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003175 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003176 while (slot < nritems) {
3177 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3178
3179 /* we found a different objectid, there must not be acls */
3180 if (found_key.objectid != objectid)
3181 return 0;
3182
3183 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003184 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003185 if (*first_xattr_slot == -1)
3186 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003187 if (found_key.offset == xattr_access ||
3188 found_key.offset == xattr_default)
3189 return 1;
3190 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003191
3192 /*
3193 * we found a key greater than an xattr key, there can't
3194 * be any acls later on
3195 */
3196 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3197 return 0;
3198
3199 slot++;
3200 scanned++;
3201
3202 /*
3203 * it goes inode, inode backrefs, xattrs, extents,
3204 * so if there are a ton of hard links to an inode there can
3205 * be a lot of backrefs. Don't waste time searching too hard,
3206 * this is just an optimization
3207 */
3208 if (scanned >= 8)
3209 break;
3210 }
3211 /* we hit the end of the leaf before we found an xattr or
3212 * something larger than an xattr. We have to assume the inode
3213 * has acls
3214 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003215 if (*first_xattr_slot == -1)
3216 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003217 return 1;
3218}
3219
3220/*
Chris Masond352ac62008-09-29 15:18:18 -04003221 * read an inode from the btree into the in-memory inode
3222 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003223static int btrfs_read_locked_inode(struct inode *inode,
3224 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003225{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003226 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003227 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003228 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003229 struct btrfs_inode_item *inode_item;
3230 struct btrfs_root *root = BTRFS_I(inode)->root;
3231 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003232 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003233 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003234 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003235 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003236 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003237 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003238
3239 ret = btrfs_fill_inode(inode, &rdev);
3240 if (!ret)
3241 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003242
Filipe Manana4222ea72018-10-24 10:13:03 +01003243 if (!path) {
3244 path = btrfs_alloc_path();
3245 if (!path)
3246 return -ENOMEM;
3247 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003248
Chris Mason39279cc2007-06-12 06:35:45 -04003249 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003250
Chris Mason39279cc2007-06-12 06:35:45 -04003251 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003252 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003253 if (path != in_path)
3254 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003255 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003256 }
Chris Mason39279cc2007-06-12 06:35:45 -04003257
Chris Mason5f39d392007-10-15 16:14:19 -04003258 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003259
3260 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003261 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003262
Chris Mason5f39d392007-10-15 16:14:19 -04003263 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3264 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003265 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003266 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003267 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3268 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003269 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003270 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3271 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003272
David Sterbaa937b972014-12-12 17:39:12 +01003273 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3274 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003275
David Sterbaa937b972014-12-12 17:39:12 +01003276 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3277 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003278
David Sterbaa937b972014-12-12 17:39:12 +01003279 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3280 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003281
chandan r9cc97d62012-07-04 12:48:07 +05303282 BTRFS_I(inode)->i_otime.tv_sec =
3283 btrfs_timespec_sec(leaf, &inode_item->otime);
3284 BTRFS_I(inode)->i_otime.tv_nsec =
3285 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003286
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003287 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003288 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003289 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3290
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003291 inode_set_iversion_queried(inode,
3292 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003293 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003294 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003295 rdev = btrfs_inode_rdev(leaf, inode_item);
3296
Josef Bacikaec74772008-07-24 12:12:38 -04003297 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003298 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003299
3300cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003301 /*
3302 * If we were modified in the current generation and evicted from memory
3303 * and then re-read we need to do a full sync since we don't have any
3304 * idea about which extents were modified before we were evicted from
3305 * cache.
3306 *
3307 * This is required for both inode re-read from disk and delayed inode
3308 * in delayed_nodes_tree.
3309 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003310 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003311 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3312 &BTRFS_I(inode)->runtime_flags);
3313
Filipe Mananabde6c242015-07-24 00:00:19 +01003314 /*
3315 * We don't persist the id of the transaction where an unlink operation
3316 * against the inode was last made. So here we assume the inode might
3317 * have been evicted, and therefore the exact value of last_unlink_trans
3318 * lost, and set it to last_trans to avoid metadata inconsistencies
3319 * between the inode and its parent if the inode is fsync'ed and the log
3320 * replayed. For example, in the scenario:
3321 *
3322 * touch mydir/foo
3323 * ln mydir/foo mydir/bar
3324 * sync
3325 * unlink mydir/bar
3326 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3327 * xfs_io -c fsync mydir/foo
3328 * <power failure>
3329 * mount fs, triggers fsync log replay
3330 *
3331 * We must make sure that when we fsync our inode foo we also log its
3332 * parent inode, otherwise after log replay the parent still has the
3333 * dentry with the "bar" name but our inode foo has a link count of 1
3334 * and doesn't have an inode ref with the name "bar" anymore.
3335 *
3336 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003337 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003338 * transaction commits on fsync if our inode is a directory, or if our
3339 * inode is not a directory, logging its parent unnecessarily.
3340 */
3341 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3342
Filipe Manana3ebac172020-07-15 12:30:43 +01003343 /*
3344 * Same logic as for last_unlink_trans. We don't persist the generation
3345 * of the last transaction where this inode was used for a reflink
3346 * operation, so after eviction and reloading the inode we must be
3347 * pessimistic and assume the last transaction that modified the inode.
3348 */
3349 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3350
Miao Xie67de1172013-12-26 13:07:06 +08003351 path->slots[0]++;
3352 if (inode->i_nlink != 1 ||
3353 path->slots[0] >= btrfs_header_nritems(leaf))
3354 goto cache_acl;
3355
3356 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003357 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003358 goto cache_acl;
3359
3360 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3361 if (location.type == BTRFS_INODE_REF_KEY) {
3362 struct btrfs_inode_ref *ref;
3363
3364 ref = (struct btrfs_inode_ref *)ptr;
3365 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3366 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3367 struct btrfs_inode_extref *extref;
3368
3369 extref = (struct btrfs_inode_extref *)ptr;
3370 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3371 extref);
3372 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003373cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003374 /*
3375 * try to precache a NULL acl entry for files that don't have
3376 * any xattrs or acls
3377 */
Li Zefan33345d012011-04-20 10:31:50 +08003378 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003379 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003380 if (first_xattr_slot != -1) {
3381 path->slots[0] = first_xattr_slot;
3382 ret = btrfs_load_inode_props(inode, path);
3383 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003384 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003385 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003386 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003387 root->root_key.objectid, ret);
3388 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003389 if (path != in_path)
3390 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003391
Al Viro72c04902009-06-24 16:58:48 -04003392 if (!maybe_acls)
3393 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003394
Chris Mason39279cc2007-06-12 06:35:45 -04003395 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003396 case S_IFREG:
3397 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -05003398 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003399 inode->i_fop = &btrfs_file_operations;
3400 inode->i_op = &btrfs_file_inode_operations;
3401 break;
3402 case S_IFDIR:
3403 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003404 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003405 break;
3406 case S_IFLNK:
3407 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003408 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003409 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003410 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003411 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003412 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003413 init_special_inode(inode, inode->i_mode, rdev);
3414 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003415 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003416
David Sterba7b6a2212018-03-26 18:40:21 +02003417 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003418 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003419}
3420
Chris Masond352ac62008-09-29 15:18:18 -04003421/*
3422 * given a leaf and an inode, copy the inode fields into the leaf
3423 */
Chris Masone02119d2008-09-05 16:13:11 -04003424static void fill_inode_item(struct btrfs_trans_handle *trans,
3425 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003426 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003427 struct inode *inode)
3428{
Liu Bo51fab692012-12-27 09:01:21 +00003429 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003430
David Sterbac82f8232019-08-09 17:48:21 +02003431 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003432
David Sterbacc4c13d2020-04-29 02:15:56 +02003433 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3434 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3435 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3436 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3437 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003438
David Sterbacc4c13d2020-04-29 02:15:56 +02003439 btrfs_set_token_timespec_sec(&token, &item->atime,
3440 inode->i_atime.tv_sec);
3441 btrfs_set_token_timespec_nsec(&token, &item->atime,
3442 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003443
David Sterbacc4c13d2020-04-29 02:15:56 +02003444 btrfs_set_token_timespec_sec(&token, &item->mtime,
3445 inode->i_mtime.tv_sec);
3446 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3447 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003448
David Sterbacc4c13d2020-04-29 02:15:56 +02003449 btrfs_set_token_timespec_sec(&token, &item->ctime,
3450 inode->i_ctime.tv_sec);
3451 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3452 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003453
David Sterbacc4c13d2020-04-29 02:15:56 +02003454 btrfs_set_token_timespec_sec(&token, &item->otime,
3455 BTRFS_I(inode)->i_otime.tv_sec);
3456 btrfs_set_token_timespec_nsec(&token, &item->otime,
3457 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303458
David Sterbacc4c13d2020-04-29 02:15:56 +02003459 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3460 btrfs_set_token_inode_generation(&token, item,
3461 BTRFS_I(inode)->generation);
3462 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3463 btrfs_set_token_inode_transid(&token, item, trans->transid);
3464 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3465 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3466 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003467}
3468
Chris Masond352ac62008-09-29 15:18:18 -04003469/*
3470 * copy everything in the in-memory inode into the btree.
3471 */
Chris Mason21151332011-11-10 20:39:08 -05003472static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Chris Masond3977122009-01-05 21:25:51 -05003473 struct btrfs_root *root, struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003474{
3475 struct btrfs_inode_item *inode_item;
3476 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003477 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003478 int ret;
3479
3480 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003481 if (!path)
3482 return -ENOMEM;
3483
Chris Masonb9473432009-03-13 11:00:37 -04003484 path->leave_spinning = 1;
Miao Xie16cdcec2011-04-22 18:12:22 +08003485 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3486 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003487 if (ret) {
3488 if (ret > 0)
3489 ret = -ENOENT;
3490 goto failed;
3491 }
3492
Chris Mason5f39d392007-10-15 16:14:19 -04003493 leaf = path->nodes[0];
3494 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003495 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003496
Chris Masone02119d2008-09-05 16:13:11 -04003497 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003498 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovd9094412020-06-05 10:41:13 +03003499 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04003500 ret = 0;
3501failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003502 btrfs_free_path(path);
3503 return ret;
3504}
3505
Chris Masond352ac62008-09-29 15:18:18 -04003506/*
Chris Mason21151332011-11-10 20:39:08 -05003507 * copy everything in the in-memory inode into the btree.
3508 */
3509noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3510 struct btrfs_root *root, struct inode *inode)
3511{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003512 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003513 int ret;
3514
3515 /*
3516 * If the inode is a free space inode, we can deadlock during commit
3517 * if we put it into the delayed code.
3518 *
3519 * The data relocation inode should also be directly updated
3520 * without delay
3521 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02003522 if (!btrfs_is_free_space_inode(BTRFS_I(inode))
Josef Bacik1d52c782014-09-18 11:30:44 -04003523 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003524 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003525 btrfs_update_root_times(trans, root);
3526
Chris Mason21151332011-11-10 20:39:08 -05003527 ret = btrfs_delayed_update_inode(trans, root, inode);
3528 if (!ret)
Nikolay Borisovd9094412020-06-05 10:41:13 +03003529 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
Chris Mason21151332011-11-10 20:39:08 -05003530 return ret;
3531 }
3532
3533 return btrfs_update_inode_item(trans, root, inode);
3534}
3535
Josef Bacikbe6aef62012-10-22 15:43:12 -04003536noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3537 struct btrfs_root *root,
3538 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003539{
3540 int ret;
3541
3542 ret = btrfs_update_inode(trans, root, inode);
3543 if (ret == -ENOSPC)
3544 return btrfs_update_inode_item(trans, root, inode);
3545 return ret;
3546}
3547
3548/*
Chris Masond352ac62008-09-29 15:18:18 -04003549 * unlink helper that gets used here in inode.c and in the tree logging
3550 * recovery code. It remove a link in a directory with a given name, and
3551 * also drops the back refs in the inode to the directory
3552 */
Al Viro92986792011-03-04 17:14:37 +00003553static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3554 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003555 struct btrfs_inode *dir,
3556 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003557 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003558{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003559 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003560 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003561 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003562 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003563 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003564 u64 ino = btrfs_ino(inode);
3565 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003566
3567 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003568 if (!path) {
3569 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003570 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003571 }
3572
Chris Masonb9473432009-03-13 11:00:37 -04003573 path->leave_spinning = 1;
Li Zefan33345d012011-04-20 10:31:50 +08003574 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003575 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003576 if (IS_ERR_OR_NULL(di)) {
3577 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003578 goto err;
3579 }
Chris Mason39279cc2007-06-12 06:35:45 -04003580 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003581 if (ret)
3582 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003583 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003584
Miao Xie67de1172013-12-26 13:07:06 +08003585 /*
3586 * If we don't have dir index, we have to get it by looking up
3587 * the inode ref, since we get the inode ref, remove it directly,
3588 * it is unnecessary to do delayed deletion.
3589 *
3590 * But if we have dir index, needn't search inode ref to get it.
3591 * Since the inode ref is close to the inode item, it is better
3592 * that we delay to delete it, and just do this deletion when
3593 * we update the inode item.
3594 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003595 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003596 ret = btrfs_delayed_delete_inode_ref(inode);
3597 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003598 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003599 goto skip_backref;
3600 }
3601 }
3602
Li Zefan33345d012011-04-20 10:31:50 +08003603 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3604 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003605 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003606 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003607 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003608 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003609 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003610 goto err;
3611 }
Miao Xie67de1172013-12-26 13:07:06 +08003612skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003613 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003614 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003615 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003616 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003617 }
Chris Mason39279cc2007-06-12 06:35:45 -04003618
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003619 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3620 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003621 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003622 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003623 goto err;
3624 }
Chris Masone02119d2008-09-05 16:13:11 -04003625
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003626 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3627 index);
Chris Mason6418c962010-10-30 07:34:24 -04003628 if (ret == -ENOENT)
3629 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003630 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003631 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003632
3633 /*
3634 * If we have a pending delayed iput we could end up with the final iput
3635 * being run in btrfs-cleaner context. If we have enough of these built
3636 * up we can end up burning a lot of time in btrfs-cleaner without any
3637 * way to throttle the unlinks. Since we're currently holding a ref on
3638 * the inode we can run the delayed iput here without any issues as the
3639 * final iput won't be done until after we drop the ref we're currently
3640 * holding.
3641 */
3642 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003643err:
3644 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003645 if (ret)
3646 goto out;
3647
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003648 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003649 inode_inc_iversion(&inode->vfs_inode);
3650 inode_inc_iversion(&dir->vfs_inode);
3651 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3652 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3653 ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
Chris Masone02119d2008-09-05 16:13:11 -04003654out:
Chris Mason39279cc2007-06-12 06:35:45 -04003655 return ret;
3656}
3657
Al Viro92986792011-03-04 17:14:37 +00003658int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3659 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003660 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003661 const char *name, int name_len)
3662{
3663 int ret;
3664 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3665 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003666 drop_nlink(&inode->vfs_inode);
3667 ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
Al Viro92986792011-03-04 17:14:37 +00003668 }
3669 return ret;
3670}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003671
3672/*
3673 * helper to start transaction for unlink and rmdir.
3674 *
Josef Bacikd52be812013-05-29 14:54:47 -04003675 * unlink and rmdir are special in btrfs, they do not always free space, so
3676 * if we cannot make our reservations the normal way try and see if there is
3677 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3678 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003679 */
Josef Bacikd52be812013-05-29 14:54:47 -04003680static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003681{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003682 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003683
Josef Bacike70bea52011-10-11 14:18:24 -04003684 /*
3685 * 1 for the possible orphan item
3686 * 1 for the dir item
3687 * 1 for the dir index
3688 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003689 * 1 for the inode
3690 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003691 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003692}
3693
Chris Mason39279cc2007-06-12 06:35:45 -04003694static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3695{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003696 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003697 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003698 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003699 int ret;
3700
Josef Bacikd52be812013-05-29 14:54:47 -04003701 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003702 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003703 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003704
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003705 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3706 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003707
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003708 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3709 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3710 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003711 if (ret)
3712 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003713
Yan, Zhenga22285a2010-05-16 10:48:46 -04003714 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003715 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003716 if (ret)
3717 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003718 }
Josef Bacik7b128762008-07-24 12:17:14 -04003719
Tsutomu Itohb5324022011-07-19 07:27:20 +00003720out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003721 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003722 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003723 return ret;
3724}
3725
Misono Tomohirof60a2362018-04-18 11:34:52 +09003726static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003727 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003728{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003729 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003730 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003731 struct btrfs_path *path;
3732 struct extent_buffer *leaf;
3733 struct btrfs_dir_item *di;
3734 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003735 const char *name = dentry->d_name.name;
3736 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003737 u64 index;
3738 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003739 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003740 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003741
Josef Bacik045d3962019-12-18 17:20:27 -05003742 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3743 objectid = inode->root->root_key.objectid;
3744 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3745 objectid = inode->location.objectid;
3746 } else {
3747 WARN_ON(1);
3748 return -EINVAL;
3749 }
3750
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003751 path = btrfs_alloc_path();
3752 if (!path)
3753 return -ENOMEM;
3754
Li Zefan33345d012011-04-20 10:31:50 +08003755 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003756 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003757 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003758 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003759 goto out;
3760 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003761
3762 leaf = path->nodes[0];
3763 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3764 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3765 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003766 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003767 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003768 goto out;
3769 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003770 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003771
Josef Bacikd49d3282019-12-18 17:20:28 -05003772 /*
3773 * This is a placeholder inode for a subvolume we didn't have a
3774 * reference to at the time of the snapshot creation. In the meantime
3775 * we could have renamed the real subvol link into our snapshot, so
3776 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3777 * Instead simply lookup the dir_index_item for this entry so we can
3778 * remove it. Otherwise we know we have a ref to the root and we can
3779 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3780 */
3781 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003782 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003783 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003784 if (IS_ERR_OR_NULL(di)) {
3785 if (!di)
3786 ret = -ENOENT;
3787 else
3788 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003789 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003790 goto out;
3791 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003792
3793 leaf = path->nodes[0];
3794 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003795 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003796 btrfs_release_path(path);
3797 } else {
3798 ret = btrfs_del_root_ref(trans, objectid,
3799 root->root_key.objectid, dir_ino,
3800 &index, name, name_len);
3801 if (ret) {
3802 btrfs_abort_transaction(trans, ret);
3803 goto out;
3804 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003805 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003806
Lu Fengqi9add2942018-08-01 11:32:26 +08003807 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003808 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003809 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003810 goto out;
3811 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003812
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003813 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003814 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003815 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003816 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003817 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003818 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003819out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003820 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003821 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003822}
3823
Misono Tomohiroec42f162018-04-18 11:34:13 +09003824/*
3825 * Helper to check if the subvolume references other subvolumes or if it's
3826 * default.
3827 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003828static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003829{
3830 struct btrfs_fs_info *fs_info = root->fs_info;
3831 struct btrfs_path *path;
3832 struct btrfs_dir_item *di;
3833 struct btrfs_key key;
3834 u64 dir_id;
3835 int ret;
3836
3837 path = btrfs_alloc_path();
3838 if (!path)
3839 return -ENOMEM;
3840
3841 /* Make sure this root isn't set as the default subvol */
3842 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3843 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3844 dir_id, "default", 7, 0);
3845 if (di && !IS_ERR(di)) {
3846 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3847 if (key.objectid == root->root_key.objectid) {
3848 ret = -EPERM;
3849 btrfs_err(fs_info,
3850 "deleting default subvolume %llu is not allowed",
3851 key.objectid);
3852 goto out;
3853 }
3854 btrfs_release_path(path);
3855 }
3856
3857 key.objectid = root->root_key.objectid;
3858 key.type = BTRFS_ROOT_REF_KEY;
3859 key.offset = (u64)-1;
3860
3861 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
3862 if (ret < 0)
3863 goto out;
3864 BUG_ON(ret == 0);
3865
3866 ret = 0;
3867 if (path->slots[0] > 0) {
3868 path->slots[0]--;
3869 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
3870 if (key.objectid == root->root_key.objectid &&
3871 key.type == BTRFS_ROOT_REF_KEY)
3872 ret = -ENOTEMPTY;
3873 }
3874out:
3875 btrfs_free_path(path);
3876 return ret;
3877}
3878
Nikolay Borisov20a68002018-04-27 14:36:24 +03003879/* Delete all dentries for inodes belonging to the root */
3880static void btrfs_prune_dentries(struct btrfs_root *root)
3881{
3882 struct btrfs_fs_info *fs_info = root->fs_info;
3883 struct rb_node *node;
3884 struct rb_node *prev;
3885 struct btrfs_inode *entry;
3886 struct inode *inode;
3887 u64 objectid = 0;
3888
3889 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
3890 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3891
3892 spin_lock(&root->inode_lock);
3893again:
3894 node = root->inode_tree.rb_node;
3895 prev = NULL;
3896 while (node) {
3897 prev = node;
3898 entry = rb_entry(node, struct btrfs_inode, rb_node);
3899
David Sterba37508512018-06-29 10:56:40 +02003900 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003901 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02003902 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03003903 node = node->rb_right;
3904 else
3905 break;
3906 }
3907 if (!node) {
3908 while (prev) {
3909 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003910 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03003911 node = prev;
3912 break;
3913 }
3914 prev = rb_next(prev);
3915 }
3916 }
3917 while (node) {
3918 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02003919 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03003920 inode = igrab(&entry->vfs_inode);
3921 if (inode) {
3922 spin_unlock(&root->inode_lock);
3923 if (atomic_read(&inode->i_count) > 1)
3924 d_prune_aliases(inode);
3925 /*
3926 * btrfs_drop_inode will have it removed from the inode
3927 * cache when its usage count hits zero.
3928 */
3929 iput(inode);
3930 cond_resched();
3931 spin_lock(&root->inode_lock);
3932 goto again;
3933 }
3934
3935 if (cond_resched_lock(&root->inode_lock))
3936 goto again;
3937
3938 node = rb_next(node);
3939 }
3940 spin_unlock(&root->inode_lock);
3941}
3942
Misono Tomohirof60a2362018-04-18 11:34:52 +09003943int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
3944{
3945 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
3946 struct btrfs_root *root = BTRFS_I(dir)->root;
3947 struct inode *inode = d_inode(dentry);
3948 struct btrfs_root *dest = BTRFS_I(inode)->root;
3949 struct btrfs_trans_handle *trans;
3950 struct btrfs_block_rsv block_rsv;
3951 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09003952 int ret;
3953 int err;
3954
3955 /*
3956 * Don't allow to delete a subvolume with send in progress. This is
3957 * inside the inode lock so the error handling that has to drop the bit
3958 * again is not run concurrently.
3959 */
3960 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08003961 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09003962 spin_unlock(&dest->root_item_lock);
3963 btrfs_warn(fs_info,
3964 "attempt to delete subvolume %llu during send",
3965 dest->root_key.objectid);
3966 return -EPERM;
3967 }
Lu Fengqia7176f72018-08-04 21:10:53 +08003968 root_flags = btrfs_root_flags(&dest->root_item);
3969 btrfs_set_root_flags(&dest->root_item,
3970 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
3971 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003972
3973 down_write(&fs_info->subvol_sem);
3974
3975 err = may_destroy_subvol(dest);
3976 if (err)
3977 goto out_up_write;
3978
3979 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
3980 /*
3981 * One for dir inode,
3982 * two for dir entries,
3983 * two for root ref/backref.
3984 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08003985 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09003986 if (err)
3987 goto out_up_write;
3988
3989 trans = btrfs_start_transaction(root, 0);
3990 if (IS_ERR(trans)) {
3991 err = PTR_ERR(trans);
3992 goto out_release;
3993 }
3994 trans->block_rsv = &block_rsv;
3995 trans->bytes_reserved = block_rsv.size;
3996
3997 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
3998
Josef Bacik045d3962019-12-18 17:20:27 -05003999 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004000 if (ret) {
4001 err = ret;
4002 btrfs_abort_transaction(trans, ret);
4003 goto out_end_trans;
4004 }
4005
4006 btrfs_record_root_in_trans(trans, dest);
4007
4008 memset(&dest->root_item.drop_progress, 0,
4009 sizeof(dest->root_item.drop_progress));
4010 dest->root_item.drop_level = 0;
4011 btrfs_set_root_refs(&dest->root_item, 0);
4012
4013 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4014 ret = btrfs_insert_orphan_item(trans,
4015 fs_info->tree_root,
4016 dest->root_key.objectid);
4017 if (ret) {
4018 btrfs_abort_transaction(trans, ret);
4019 err = ret;
4020 goto out_end_trans;
4021 }
4022 }
4023
Lu Fengqid1957792018-05-29 15:01:54 +08004024 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004025 BTRFS_UUID_KEY_SUBVOL,
4026 dest->root_key.objectid);
4027 if (ret && ret != -ENOENT) {
4028 btrfs_abort_transaction(trans, ret);
4029 err = ret;
4030 goto out_end_trans;
4031 }
4032 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004033 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004034 dest->root_item.received_uuid,
4035 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4036 dest->root_key.objectid);
4037 if (ret && ret != -ENOENT) {
4038 btrfs_abort_transaction(trans, ret);
4039 err = ret;
4040 goto out_end_trans;
4041 }
4042 }
4043
Qu Wenruo082b6c92020-06-16 10:17:37 +08004044 free_anon_bdev(dest->anon_dev);
4045 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004046out_end_trans:
4047 trans->block_rsv = NULL;
4048 trans->bytes_reserved = 0;
4049 ret = btrfs_end_transaction(trans);
4050 if (ret && !err)
4051 err = ret;
4052 inode->i_flags |= S_DEAD;
4053out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004054 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004055out_up_write:
4056 up_write(&fs_info->subvol_sem);
4057 if (err) {
4058 spin_lock(&dest->root_item_lock);
4059 root_flags = btrfs_root_flags(&dest->root_item);
4060 btrfs_set_root_flags(&dest->root_item,
4061 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4062 spin_unlock(&dest->root_item_lock);
4063 } else {
4064 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004065 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004066 ASSERT(dest->send_in_progress == 0);
4067
4068 /* the last ref */
4069 if (dest->ino_cache_inode) {
4070 iput(dest->ino_cache_inode);
4071 dest->ino_cache_inode = NULL;
4072 }
4073 }
4074
4075 return err;
4076}
4077
Chris Mason39279cc2007-06-12 06:35:45 -04004078static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4079{
David Howells2b0143b2015-03-17 22:25:59 +00004080 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004081 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004082 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004083 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004084 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004085
David Sterbab3ae2442012-09-13 16:04:34 -06004086 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004087 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004088 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004089 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004090
Josef Bacikd52be812013-05-29 14:54:47 -04004091 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004092 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004093 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004094
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004095 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004096 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004097 goto out;
4098 }
4099
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004100 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004101 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004102 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004103
Filipe Manana44f714d2016-06-06 16:11:13 +01004104 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4105
Chris Mason39279cc2007-06-12 06:35:45 -04004106 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004107 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4108 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4109 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004110 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004111 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004112 /*
4113 * Propagate the last_unlink_trans value of the deleted dir to
4114 * its parent directory. This is to prevent an unrecoverable
4115 * log tree in the case we do something like this:
4116 * 1) create dir foo
4117 * 2) create snapshot under dir foo
4118 * 3) delete the snapshot
4119 * 4) rmdir foo
4120 * 5) mkdir foo
4121 * 6) fsync foo or some file inside foo
4122 */
4123 if (last_unlink_trans >= trans->transid)
4124 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4125 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004126out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004127 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004128 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004129
Chris Mason39279cc2007-06-12 06:35:45 -04004130 return err;
4131}
4132
Josef Bacikddfae632017-10-19 14:16:02 -04004133/*
4134 * Return this if we need to call truncate_block for the last bit of the
4135 * truncate.
4136 */
4137#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004138
Chris Mason323ac952008-10-01 19:05:46 -04004139/*
Chris Mason39279cc2007-06-12 06:35:45 -04004140 * this can truncate away extent items, csum items and directory items.
4141 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004142 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004143 *
4144 * csum items that cross the new i_size are truncated to the new size
4145 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004146 *
4147 * min_type is the minimum key type to truncate down to. If set to 0, this
4148 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004149 */
Yan, Zheng80825102009-11-12 09:35:36 +00004150int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4151 struct btrfs_root *root,
4152 struct inode *inode,
4153 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004154{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004155 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004156 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004157 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004158 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004159 struct btrfs_key key;
4160 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004161 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004162 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004163 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004164 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004165 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004166 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004167 int found_extent;
4168 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004169 int pending_del_nr = 0;
4170 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004171 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004172 int ret;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004173 u64 ino = btrfs_ino(BTRFS_I(inode));
Chris Mason28ed1342014-12-17 09:41:04 -08004174 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004175 bool be_nice = false;
4176 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004177 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4178 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004179
4180 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004181
Chris Mason28ed1342014-12-17 09:41:04 -08004182 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004183 * For non-free space inodes and non-shareable roots, we want to back
4184 * off from time to time. This means all inodes in subvolume roots,
4185 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004186 */
Nikolay Borisov70ddc552017-02-20 13:50:35 +02004187 if (!btrfs_is_free_space_inode(BTRFS_I(inode)) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004188 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004189 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004190
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004191 path = btrfs_alloc_path();
4192 if (!path)
4193 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004194 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004195
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004196 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004197 lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, (u64)-1,
4198 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004199
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004200 /*
4201 * We want to drop from the next block forward in case this
4202 * new size is not block aligned since we will be keeping the
4203 * last block of the extent just the way it is.
4204 */
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004205 btrfs_drop_extent_cache(BTRFS_I(inode), ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004206 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004207 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004208 }
Yan, Zheng80825102009-11-12 09:35:36 +00004209
Miao Xie16cdcec2011-04-22 18:12:22 +08004210 /*
4211 * This function is also used to drop the items in the log tree before
4212 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004213 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004214 * items.
4215 */
4216 if (min_type == 0 && root == BTRFS_I(inode)->root)
Nikolay Borisov4ccb5c72017-01-10 20:35:38 +02004217 btrfs_kill_delayed_inode_items(BTRFS_I(inode));
Miao Xie16cdcec2011-04-22 18:12:22 +08004218
Li Zefan33345d012011-04-20 10:31:50 +08004219 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004220 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004221 key.type = (u8)-1;
4222
Chris Mason85e21ba2008-01-29 15:11:36 -05004223search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004224 /*
4225 * with a 16K leaf size and 128MB extents, you can actually queue
4226 * up a huge file in a single leaf. Most of the time that
4227 * bytes_deleted is > 0, it will be huge by the time we get here
4228 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004229 if (be_nice && bytes_deleted > SZ_32M &&
4230 btrfs_should_end_transaction(trans)) {
4231 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004232 goto out;
4233 }
Chris Masond3977122009-01-05 21:25:51 -05004234
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004235 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4236 if (ret < 0)
4237 goto out;
4238
Chris Mason85e21ba2008-01-29 15:11:36 -05004239 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004240 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004241 /* there are no items in the tree for us to truncate, we're
4242 * done
4243 */
Yan, Zheng80825102009-11-12 09:35:36 +00004244 if (path->slots[0] == 0)
4245 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004246 path->slots[0]--;
4247 }
4248
Chris Masond3977122009-01-05 21:25:51 -05004249 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004250 u64 clear_start = 0, clear_len = 0;
4251
Chris Mason39279cc2007-06-12 06:35:45 -04004252 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004253 leaf = path->nodes[0];
4254 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004255 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004256
Li Zefan33345d012011-04-20 10:31:50 +08004257 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004258 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004259
Chris Mason85e21ba2008-01-29 15:11:36 -05004260 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004261 break;
4262
Chris Mason5f39d392007-10-15 16:14:19 -04004263 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004264 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004265 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004266 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004267 extent_type = btrfs_file_extent_type(leaf, fi);
4268 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004269 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004270 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004271
4272 trace_btrfs_truncate_show_fi_regular(
4273 BTRFS_I(inode), leaf, fi,
4274 found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004275 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004276 item_end += btrfs_file_extent_ram_bytes(leaf,
4277 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004278
4279 trace_btrfs_truncate_show_fi_inline(
4280 BTRFS_I(inode), leaf, fi, path->slots[0],
4281 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004282 }
Yan008630c2007-11-07 13:31:09 -05004283 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004284 }
Yan, Zheng80825102009-11-12 09:35:36 +00004285 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004286 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004287 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004288 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004289 break;
4290 if (found_key.offset >= new_size)
4291 del_item = 1;
4292 else
4293 del_item = 0;
4294 }
Chris Mason39279cc2007-06-12 06:35:45 -04004295 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004296 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004297 if (found_type != BTRFS_EXTENT_DATA_KEY)
4298 goto delete;
4299
4300 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004301 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004302
4303 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004304 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004305 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004306 u64 orig_num_bytes =
4307 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004308 extent_num_bytes = ALIGN(new_size -
4309 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004310 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004311 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004312 btrfs_set_file_extent_num_bytes(leaf, fi,
4313 extent_num_bytes);
4314 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004315 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004316 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004317 &root->state) &&
4318 extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004319 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004320 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004321 } else {
Chris Masondb945352007-10-15 16:15:53 -04004322 extent_num_bytes =
4323 btrfs_file_extent_disk_num_bytes(leaf,
4324 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004325 extent_offset = found_key.offset -
4326 btrfs_file_extent_offset(leaf, fi);
4327
Chris Mason39279cc2007-06-12 06:35:45 -04004328 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004329 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004330 if (extent_start != 0) {
4331 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004332 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004333 &root->state))
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004334 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004335 }
4336 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004337 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004338 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004339 /*
4340 * we can't truncate inline items that have had
4341 * special encodings
4342 */
4343 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004344 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004345 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4346 btrfs_file_extent_compression(leaf, fi) == 0) {
4347 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004348
Josef Bacikddfae632017-10-19 14:16:02 -04004349 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4350 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004351 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004352 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004353 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004354 * We have to bail so the last_size is set to
4355 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004356 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004357 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004358 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004359 } else {
4360 /*
4361 * Inline extents are special, we just treat
4362 * them as a full sector worth in the file
4363 * extent tree just for simplicity sake.
4364 */
4365 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004366 }
Josef Bacikddfae632017-10-19 14:16:02 -04004367
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004368 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Josef Bacikddfae632017-10-19 14:16:02 -04004369 inode_sub_bytes(inode, item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004370 }
Chris Mason179e29e2007-11-01 11:28:41 -04004371delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004372 /*
4373 * We use btrfs_truncate_inode_items() to clean up log trees for
4374 * multiple fsyncs, and in this case we don't want to clear the
4375 * file extent range because it's just the log.
4376 */
4377 if (root == BTRFS_I(inode)->root) {
4378 ret = btrfs_inode_clear_file_extent_range(BTRFS_I(inode),
4379 clear_start, clear_len);
4380 if (ret) {
4381 btrfs_abort_transaction(trans, ret);
4382 break;
4383 }
4384 }
4385
Josef Bacikddfae632017-10-19 14:16:02 -04004386 if (del_item)
4387 last_size = found_key.offset;
4388 else
4389 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004390 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004391 if (!pending_del_nr) {
4392 /* no pending yet, add ourselves */
4393 pending_del_slot = path->slots[0];
4394 pending_del_nr = 1;
4395 } else if (pending_del_nr &&
4396 path->slots[0] + 1 == pending_del_slot) {
4397 /* hop on the pending chunk */
4398 pending_del_nr++;
4399 pending_del_slot = path->slots[0];
4400 } else {
Chris Masond3977122009-01-05 21:25:51 -05004401 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004402 }
Chris Mason39279cc2007-06-12 06:35:45 -04004403 } else {
4404 break;
4405 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004406 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004407
Miao Xie27cdeb72014-04-02 19:51:05 +08004408 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004409 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004410 struct btrfs_ref ref = { 0 };
4411
Chris Mason28ed1342014-12-17 09:41:04 -08004412 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004413
4414 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4415 extent_start, extent_num_bytes, 0);
4416 ref.real_root = root->root_key.objectid;
4417 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4418 ino, extent_offset);
4419 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004420 if (ret) {
4421 btrfs_abort_transaction(trans, ret);
4422 break;
4423 }
Chris Mason28f75a02015-02-04 06:59:29 -08004424 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004425 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004426 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004427 }
Chris Mason39279cc2007-06-12 06:35:45 -04004428 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004429
Yan, Zheng80825102009-11-12 09:35:36 +00004430 if (found_type == BTRFS_INODE_ITEM_KEY)
4431 break;
4432
4433 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004434 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004435 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004436 if (pending_del_nr) {
4437 ret = btrfs_del_items(trans, root, path,
4438 pending_del_slot,
4439 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004440 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004441 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004442 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004443 }
Yan, Zheng80825102009-11-12 09:35:36 +00004444 pending_del_nr = 0;
4445 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004446 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004447
Chris Mason28f75a02015-02-04 06:59:29 -08004448 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004449 * We can generate a lot of delayed refs, so we need to
4450 * throttle every once and a while and make sure we're
4451 * adding enough space to keep up with the work we are
4452 * generating. Since we hold a transaction here we
4453 * can't flush, and we don't want to FLUSH_LIMIT because
4454 * we could have generated too many delayed refs to
4455 * actually allocate, so just bail if we're short and
4456 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004457 */
Josef Bacik28bad212018-12-03 10:20:38 -05004458 if (should_throttle) {
4459 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4460 BTRFS_RESERVE_NO_FLUSH);
4461 if (ret) {
4462 ret = -EAGAIN;
4463 break;
4464 }
Chris Mason28f75a02015-02-04 06:59:29 -08004465 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004466 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004467 } else {
4468 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004469 }
Chris Mason39279cc2007-06-12 06:35:45 -04004470 }
Yan, Zheng80825102009-11-12 09:35:36 +00004471out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004472 if (ret >= 0 && pending_del_nr) {
4473 int err;
4474
4475 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004476 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004477 if (err) {
4478 btrfs_abort_transaction(trans, err);
4479 ret = err;
4480 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004481 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004482 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4483 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004484 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004485 last_size = new_size;
Josef Bacikd923afe2020-01-17 09:02:23 -05004486 btrfs_inode_safe_disk_i_size_write(inode, last_size);
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004487 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start,
4488 (u64)-1, &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004489 }
Chris Mason28ed1342014-12-17 09:41:04 -08004490
Chris Mason39279cc2007-06-12 06:35:45 -04004491 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004492 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004493}
4494
Chris Masona52d9a82007-08-27 16:49:44 -04004495/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304496 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004497 * @inode - inode that we're zeroing
4498 * @from - the offset to start zeroing
4499 * @len - the length to zero, 0 to zero the entire range respective to the
4500 * offset
4501 * @front - zero up to the offset instead of from the offset on
4502 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304503 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004504 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004505 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304506int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004507 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004508{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004509 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004510 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004511 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4512 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004513 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004514 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004515 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004516 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004517 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004518 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304519 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004520 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004521 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004522 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004523 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304524 u64 block_start;
4525 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004526
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004527 if (IS_ALIGNED(offset, blocksize) &&
4528 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004529 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304530
Josef Bacik8b62f872017-10-19 14:15:55 -04004531 block_start = round_down(from, blocksize);
4532 block_end = block_start + blocksize - 1;
4533
Nikolay Borisov36ea6f32020-06-03 08:55:41 +03004534 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved,
4535 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004536 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004537 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4538 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004539 /* For nocow case, no need to reserve data space */
4540 only_release_metadata = true;
4541 } else {
4542 goto out;
4543 }
4544 }
4545 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4546 if (ret < 0) {
4547 if (!only_release_metadata)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03004548 btrfs_free_reserved_data_space(BTRFS_I(inode),
4549 data_reserved, block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004550 goto out;
4551 }
Chris Mason211c17f2008-05-15 09:13:45 -04004552again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004553 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004554 if (!page) {
Nikolay Borisov86d52922020-06-03 08:55:40 +03004555 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004556 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004557 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004558 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004559 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004560 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004561
Chris Masona52d9a82007-08-27 16:49:44 -04004562 if (!PageUptodate(page)) {
4563 ret = btrfs_readpage(NULL, page);
4564 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004565 if (page->mapping != mapping) {
4566 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004567 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004568 goto again;
4569 }
Chris Masona52d9a82007-08-27 16:49:44 -04004570 if (!PageUptodate(page)) {
4571 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004572 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004573 }
4574 }
Chris Mason211c17f2008-05-15 09:13:45 -04004575 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004576
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304577 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004578 set_page_extent_mapped(page);
4579
Nikolay Borisovc3504372020-06-03 08:55:03 +03004580 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004581 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304582 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004583 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004584 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004585 put_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04004586 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004587 btrfs_put_ordered_extent(ordered);
4588 goto again;
4589 }
4590
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304591 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004592 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4593 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004594
Nikolay Borisovc2566f22020-06-03 08:55:35 +03004595 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004596 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004597 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304598 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004599 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004600 goto out_unlock;
4601 }
4602
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304603 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004604 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304605 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004606 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004607 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304608 memset(kaddr + (block_start - page_offset(page)),
4609 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004610 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304611 memset(kaddr + (block_start - page_offset(page)) + offset,
4612 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004613 flush_dcache_page(page);
4614 kunmap(page);
4615 }
Chris Mason247e7432008-07-17 12:53:51 -04004616 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004617 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004618 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004619
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004620 if (only_release_metadata)
4621 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
4622 block_end, EXTENT_NORESERVE, NULL, NULL,
4623 GFP_NOFS);
4624
Chris Mason89642222008-07-24 09:41:53 -04004625out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004626 if (ret) {
4627 if (only_release_metadata)
4628 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4629 blocksize, true);
4630 else
Nikolay Borisov86d52922020-06-03 08:55:40 +03004631 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004632 block_start, blocksize, true);
4633 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004634 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004635 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004636 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004637out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004638 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004639 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004640 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004641 return ret;
4642}
4643
Josef Bacik16e75492013-10-22 12:18:51 -04004644static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4645 u64 offset, u64 len)
4646{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004647 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik16e75492013-10-22 12:18:51 -04004648 struct btrfs_trans_handle *trans;
4649 int ret;
4650
4651 /*
4652 * Still need to make sure the inode looks like it's been updated so
4653 * that any holes get logged if we fsync.
4654 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004655 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4656 BTRFS_I(inode)->last_trans = fs_info->generation;
Josef Bacik16e75492013-10-22 12:18:51 -04004657 BTRFS_I(inode)->last_sub_trans = root->log_transid;
4658 BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4659 return 0;
4660 }
4661
4662 /*
4663 * 1 - for the one we're dropping
4664 * 1 - for the one we're adding
4665 * 1 - for updating the inode.
4666 */
4667 trans = btrfs_start_transaction(root, 3);
4668 if (IS_ERR(trans))
4669 return PTR_ERR(trans);
4670
4671 ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4672 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004673 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004674 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004675 return ret;
4676 }
4677
David Sterbaf85b7372017-01-20 14:54:07 +01004678 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4679 offset, 0, 0, len, 0, len, 0, 0, 0);
Josef Bacik16e75492013-10-22 12:18:51 -04004680 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04004681 btrfs_abort_transaction(trans, ret);
Josef Bacik16e75492013-10-22 12:18:51 -04004682 else
4683 btrfs_update_inode(trans, root, inode);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004684 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004685 return ret;
4686}
4687
Josef Bacik695a0d02011-03-04 15:46:53 -05004688/*
4689 * This function puts in dummy file extents for the area we're creating a hole
4690 * for. So if we are truncating this file to a larger size we need to insert
4691 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4692 * the range between oldsize and size
4693 */
Josef Bacika41ad392011-01-31 15:30:16 -05004694int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004695{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004696 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004697 struct btrfs_root *root = BTRFS_I(inode)->root;
4698 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004699 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004700 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004701 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004702 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4703 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004704 u64 last_byte;
4705 u64 cur_offset;
4706 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004707 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004708
Josef Bacika71754f2013-06-17 17:14:39 -04004709 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304710 * If our size started in the middle of a block we need to zero out the
4711 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004712 * expose stale data.
4713 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304714 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004715 if (err)
4716 return err;
4717
Yan Zheng9036c102008-10-30 14:19:41 -04004718 if (size <= hole_start)
4719 return 0;
4720
David Sterbab272ae22020-02-05 19:09:33 +01004721 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004722 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004723 cur_offset = hole_start;
4724 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004725 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004726 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004727 if (IS_ERR(em)) {
4728 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004729 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004730 break;
4731 }
Yan Zheng9036c102008-10-30 14:19:41 -04004732 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004733 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004734 hole_size = last_byte - cur_offset;
4735
Yan, Zheng80825102009-11-12 09:35:36 +00004736 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004737 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004738
Josef Bacik16e75492013-10-22 12:18:51 -04004739 err = maybe_insert_hole(root, inode, cur_offset,
4740 hole_size);
4741 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004742 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004743
4744 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4745 cur_offset, hole_size);
4746 if (err)
4747 break;
4748
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004749 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004750 cur_offset + hole_size - 1, 0);
4751 hole_em = alloc_extent_map();
4752 if (!hole_em) {
4753 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4754 &BTRFS_I(inode)->runtime_flags);
4755 goto next;
4756 }
4757 hole_em->start = cur_offset;
4758 hole_em->len = hole_size;
4759 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004760
Josef Bacik5dc562c2012-08-17 13:14:17 -04004761 hole_em->block_start = EXTENT_MAP_HOLE;
4762 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004763 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004764 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004765 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004766 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004767
4768 while (1) {
4769 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004770 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004771 write_unlock(&em_tree->lock);
4772 if (err != -EEXIST)
4773 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004774 btrfs_drop_extent_cache(BTRFS_I(inode),
4775 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004776 cur_offset +
4777 hole_size - 1, 0);
4778 }
4779 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004780 } else {
4781 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4782 cur_offset, hole_size);
4783 if (err)
4784 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004785 }
Josef Bacik16e75492013-10-22 12:18:51 -04004786next:
Yan Zheng9036c102008-10-30 14:19:41 -04004787 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004788 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004789 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004790 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004791 break;
4792 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004793 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004794 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004795 return err;
4796}
4797
Eric Sandeen3972f262013-01-12 02:57:22 +00004798static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004799{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004800 struct btrfs_root *root = BTRFS_I(inode)->root;
4801 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004802 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004803 loff_t newsize = attr->ia_size;
4804 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004805 int ret;
4806
Eric Sandeen3972f262013-01-12 02:57:22 +00004807 /*
4808 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4809 * special case where we need to update the times despite not having
4810 * these flags set. For all other operations the VFS set these flags
4811 * explicitly if it wants a timestamp update.
4812 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004813 if (newsize != oldsize) {
4814 inode_inc_iversion(inode);
4815 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4816 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004817 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004818 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004819
Josef Bacika41ad392011-01-31 15:30:16 -05004820 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004821 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004822 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004823 * This is to ensure the snapshot captures a fully consistent
4824 * state of this file - if the snapshot captures this expanding
4825 * truncation, it must capture all writes that happened before
4826 * this truncation.
4827 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004828 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004829 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004830 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004831 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004832 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004833 }
Yan, Zheng80825102009-11-12 09:35:36 +00004834
Miao Xief4a2f4c2011-12-14 20:12:01 -05004835 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004836 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004837 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004838 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004839 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004840
4841 i_size_write(inode, newsize);
Josef Bacikd923afe2020-01-17 09:02:23 -05004842 btrfs_inode_safe_disk_i_size_write(inode, 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05304843 pagecache_isize_extended(inode, oldsize, newsize);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004844 ret = btrfs_update_inode(trans, root, inode);
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004845 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004846 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05004847 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00004848
Josef Bacika41ad392011-01-31 15:30:16 -05004849 /*
4850 * We're truncating a file that used to have good data down to
4851 * zero. Make sure it gets into the ordered flush list so that
4852 * any new writes get down to disk quickly.
4853 */
4854 if (newsize == 0)
Josef Bacik72ac3c02012-05-23 14:13:11 -04004855 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4856 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00004857
Josef Bacika41ad392011-01-31 15:30:16 -05004858 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00004859
David Sterba8e0fa5d2020-06-09 19:21:48 +02004860 /* Disable nonlocked read DIO to avoid the endless truncate */
4861 btrfs_inode_block_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004862 inode_dio_wait(inode);
David Sterba8e0fa5d2020-06-09 19:21:48 +02004863 btrfs_inode_resume_unlocked_dio(BTRFS_I(inode));
Miao Xie2e60a512013-02-08 07:01:08 +00004864
Filipe Manana213e8c52018-02-06 20:40:31 +00004865 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004866 if (ret && inode->i_nlink) {
4867 int err;
4868
4869 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004870 * Truncate failed, so fix up the in-memory size. We
4871 * adjusted disk_i_size down as we removed extents, so
4872 * wait for disk_i_size to be stable and then update the
4873 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004874 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004875 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004876 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07004877 return err;
4878 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04004879 }
Yan, Zheng80825102009-11-12 09:35:36 +00004880 }
4881
Josef Bacika41ad392011-01-31 15:30:16 -05004882 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00004883}
4884
Chris Mason39279cc2007-06-12 06:35:45 -04004885static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
4886{
David Howells2b0143b2015-03-17 22:25:59 +00004887 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08004888 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004889 int err;
4890
Li Zefanb83cc962010-12-20 16:04:08 +08004891 if (btrfs_root_readonly(root))
4892 return -EROFS;
4893
Jan Kara31051c82016-05-26 16:55:18 +02004894 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04004895 if (err)
4896 return err;
4897
Chris Mason5a3f23d2009-03-31 13:27:11 -04004898 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00004899 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00004900 if (err)
4901 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04004902 }
Yan Zheng9036c102008-10-30 14:19:41 -04004903
Christoph Hellwig10257742010-06-04 11:30:02 +02004904 if (attr->ia_valid) {
4905 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04004906 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05004907 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04004908
Josef Bacik22c44fe2011-11-30 10:45:38 -05004909 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08004910 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02004911 }
4912
Chris Mason39279cc2007-06-12 06:35:45 -04004913 return err;
4914}
Chris Mason61295eb2008-01-14 16:24:38 -05004915
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004916/*
4917 * While truncating the inode pages during eviction, we get the VFS calling
4918 * btrfs_invalidatepage() against each page of the inode. This is slow because
4919 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
4920 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
4921 * extent_state structures over and over, wasting lots of time.
4922 *
4923 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
4924 * those expensive operations on a per page basis and do only the ordered io
4925 * finishing, while we release here the extent_map and extent_state structures,
4926 * without the excessive merging and splitting.
4927 */
4928static void evict_inode_truncate_pages(struct inode *inode)
4929{
4930 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4931 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
4932 struct rb_node *node;
4933
4934 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07004935 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004936
4937 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08004938 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004939 struct extent_map *em;
4940
Liu Bo07e1ce02018-08-23 03:51:52 +08004941 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004942 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08004943 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
4944 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004945 remove_extent_mapping(map_tree, em);
4946 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01004947 if (need_resched()) {
4948 write_unlock(&map_tree->lock);
4949 cond_resched();
4950 write_lock(&map_tree->lock);
4951 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004952 }
4953 write_unlock(&map_tree->lock);
4954
Filipe Manana6ca07092015-05-26 00:55:42 +01004955 /*
4956 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07004957 * We can have ongoing bios started by readahead that have
4958 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01004959 * still in progress (unlocked the pages in the bio but did not yet
4960 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01004961 * ranges can still be locked and eviction started because before
4962 * submitting those bios, which are executed by a separate task (work
4963 * queue kthread), inode references (inode->i_count) were not taken
4964 * (which would be dropped in the end io callback of each bio).
4965 * Therefore here we effectively end up waiting for those bios and
4966 * anyone else holding locked ranges without having bumped the inode's
4967 * reference count - if we don't do it, when they access the inode's
4968 * io_tree to unlock a range it may be too late, leading to an
4969 * use-after-free issue.
4970 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004971 spin_lock(&io_tree->lock);
4972 while (!RB_EMPTY_ROOT(&io_tree->state)) {
4973 struct extent_state *state;
4974 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01004975 u64 start;
4976 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01004977 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004978
4979 node = rb_first(&io_tree->state);
4980 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01004981 start = state->start;
4982 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01004983 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00004984 spin_unlock(&io_tree->lock);
4985
David Sterbaff13db42015-12-03 14:30:40 +01004986 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004987
4988 /*
4989 * If still has DELALLOC flag, the extent didn't reach disk,
4990 * and its reserved space won't be freed by delayed_ref.
4991 * So we need to free its reserved space here.
4992 * (Refer to comment in btrfs_invalidatepage, case 2)
4993 *
4994 * Note, end is the bytenr of last byte, so we need + 1 here.
4995 */
Filipe Manana421f0922018-10-12 13:02:48 +01004996 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03004997 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
4998 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08004999
Filipe Manana6ca07092015-05-26 00:55:42 +01005000 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005001 EXTENT_LOCKED | EXTENT_DELALLOC |
5002 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5003 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005004
Filipe Manana7064dd52014-08-08 02:47:05 +01005005 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005006 spin_lock(&io_tree->lock);
5007 }
5008 spin_unlock(&io_tree->lock);
5009}
5010
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005011static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005012 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005013{
5014 struct btrfs_fs_info *fs_info = root->fs_info;
5015 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005016 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005017 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005018 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005019
Josef Bacikd3984c92019-08-01 18:19:37 -04005020 /*
5021 * Eviction should be taking place at some place safe because of our
5022 * delayed iputs. However the normal flushing code will run delayed
5023 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5024 *
5025 * We reserve the delayed_refs_extra here again because we can't use
5026 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5027 * above. We reserve our extra bit here because we generate a ton of
5028 * delayed refs activity by truncating.
5029 *
5030 * If we cannot make our reservation we'll attempt to steal from the
5031 * global reserve, because we really want to be able to free up space.
5032 */
5033 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5034 BTRFS_RESERVE_FLUSH_EVICT);
5035 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005036 /*
5037 * Try to steal from the global reserve if there is space for
5038 * it.
5039 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005040 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5041 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5042 btrfs_warn(fs_info,
5043 "could not allocate space for delete; will truncate on mount");
5044 return ERR_PTR(-ENOSPC);
5045 }
5046 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005047 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005048
5049 trans = btrfs_join_transaction(root);
5050 if (IS_ERR(trans))
5051 return trans;
5052
5053 if (delayed_refs_extra) {
5054 trans->block_rsv = &fs_info->trans_block_rsv;
5055 trans->bytes_reserved = delayed_refs_extra;
5056 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5057 delayed_refs_extra, 1);
5058 }
5059 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005060}
5061
Al Virobd555972010-06-07 11:35:40 -04005062void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005063{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005064 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005065 struct btrfs_trans_handle *trans;
5066 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005067 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005068 int ret;
5069
liubo1abe9b82011-03-24 11:18:59 +00005070 trace_btrfs_inode_evict(inode);
5071
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005072 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005073 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005074 return;
5075 }
5076
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005077 evict_inode_truncate_pages(inode);
5078
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005079 if (inode->i_nlink &&
5080 ((btrfs_root_refs(&root->root_item) != 0 &&
5081 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005082 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005083 goto no_delete;
5084
Omar Sandoval27919062018-05-11 13:13:37 -07005085 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005086 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005087
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005088 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005089
Omar Sandoval7b40b692018-05-11 13:13:33 -07005090 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005091 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005092
Yan, Zheng76dda932009-09-21 16:00:26 -04005093 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005094 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5095 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005096 goto no_delete;
5097 }
5098
Nikolay Borisovaa790212017-01-10 20:35:40 +02005099 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005100 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005101 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005102
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005103 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005104 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005105 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005106 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005107 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005108
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005109 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005110
Yan, Zheng80825102009-11-12 09:35:36 +00005111 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005112 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005113 if (IS_ERR(trans))
5114 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005115
5116 trans->block_rsv = rsv;
5117
Yan, Zhengd68fc572010-05-16 10:49:58 -04005118 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005119 trans->block_rsv = &fs_info->trans_block_rsv;
5120 btrfs_end_transaction(trans);
5121 btrfs_btree_balance_dirty(fs_info);
5122 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5123 goto free_rsv;
5124 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005125 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005126 }
5127
Josef Bacik4ef31a42013-08-13 14:10:08 -04005128 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005129 * Errors here aren't a big deal, it just means we leave orphan items in
5130 * the tree. They will be cleaned up on the next mount. If the inode
5131 * number gets reused, cleanup deletes the orphan item without doing
5132 * anything, and unlink reuses the existing orphan item.
5133 *
5134 * If it turns out that we are dropping too many of these, we might want
5135 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005136 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005137 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005138 if (!IS_ERR(trans)) {
5139 trans->block_rsv = rsv;
5140 btrfs_orphan_del(trans, BTRFS_I(inode));
5141 trans->block_rsv = &fs_info->trans_block_rsv;
5142 btrfs_end_transaction(trans);
5143 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005144
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005145 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005146 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005147 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005148
Omar Sandoval27919062018-05-11 13:13:37 -07005149free_rsv:
5150 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005151no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005152 /*
5153 * If we didn't successfully delete, the orphan item will still be in
5154 * the tree and we'll retry on the next mount. Again, we might also want
5155 * to retry these periodically in the future.
5156 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005157 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005158 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005159}
5160
5161/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005162 * Return the key found in the dir entry in the location pointer, fill @type
5163 * with BTRFS_FT_*, and return 0.
5164 *
Su Yue005d6712018-03-05 17:13:37 +08005165 * If no dir entries were found, returns -ENOENT.
5166 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005167 */
5168static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005169 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005170{
5171 const char *name = dentry->d_name.name;
5172 int namelen = dentry->d_name.len;
5173 struct btrfs_dir_item *di;
5174 struct btrfs_path *path;
5175 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005176 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005177
5178 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005179 if (!path)
5180 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005181
David Sterbaf85b7372017-01-20 14:54:07 +01005182 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5183 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005184 if (IS_ERR_OR_NULL(di)) {
5185 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005186 goto out;
5187 }
Chris Masond3977122009-01-05 21:25:51 -05005188
Chris Mason5f39d392007-10-15 16:14:19 -04005189 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005190 if (location->type != BTRFS_INODE_ITEM_KEY &&
5191 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005192 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005193 btrfs_warn(root->fs_info,
5194"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5195 __func__, name, btrfs_ino(BTRFS_I(dir)),
5196 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005197 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005198 if (!ret)
5199 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005200out:
Chris Mason39279cc2007-06-12 06:35:45 -04005201 btrfs_free_path(path);
5202 return ret;
5203}
5204
5205/*
5206 * when we hit a tree root in a directory, the btrfs part of the inode
5207 * needs to be changed to reflect the root directory of the tree root. This
5208 * is kind of like crossing a mount point.
5209 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005210static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005211 struct inode *dir,
5212 struct dentry *dentry,
5213 struct btrfs_key *location,
5214 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005215{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005216 struct btrfs_path *path;
5217 struct btrfs_root *new_root;
5218 struct btrfs_root_ref *ref;
5219 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005220 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005221 int ret;
5222 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005223
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005224 path = btrfs_alloc_path();
5225 if (!path) {
5226 err = -ENOMEM;
5227 goto out;
5228 }
Chris Mason39279cc2007-06-12 06:35:45 -04005229
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005230 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005231 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5232 key.type = BTRFS_ROOT_REF_KEY;
5233 key.offset = location->objectid;
5234
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005235 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005236 if (ret) {
5237 if (ret < 0)
5238 err = ret;
5239 goto out;
5240 }
Chris Mason39279cc2007-06-12 06:35:45 -04005241
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005242 leaf = path->nodes[0];
5243 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005244 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005245 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5246 goto out;
5247
5248 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5249 (unsigned long)(ref + 1),
5250 dentry->d_name.len);
5251 if (ret)
5252 goto out;
5253
David Sterbab3b4aa72011-04-21 01:20:15 +02005254 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005255
David Sterba56e93572020-05-15 19:35:55 +02005256 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005257 if (IS_ERR(new_root)) {
5258 err = PTR_ERR(new_root);
5259 goto out;
5260 }
5261
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005262 *sub_root = new_root;
5263 location->objectid = btrfs_root_dirid(&new_root->root_item);
5264 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005265 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005266 err = 0;
5267out:
5268 btrfs_free_path(path);
5269 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005270}
5271
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005272static void inode_tree_add(struct inode *inode)
5273{
5274 struct btrfs_root *root = BTRFS_I(inode)->root;
5275 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005276 struct rb_node **p;
5277 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005278 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005279 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005280
Al Viro1d3382cb2010-10-23 15:19:20 -04005281 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005282 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005283 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005284 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005285 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005286 while (*p) {
5287 parent = *p;
5288 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5289
David Sterba37508512018-06-29 10:56:40 +02005290 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005291 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005292 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005293 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005294 else {
5295 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005296 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005297 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005298 RB_CLEAR_NODE(parent);
5299 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005300 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005301 }
5302 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005303 rb_link_node(new, parent, p);
5304 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005305 spin_unlock(&root->inode_lock);
5306}
5307
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005308static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005309{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005310 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005311 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005312
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005313 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005314 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5315 rb_erase(&inode->rb_node, &root->inode_tree);
5316 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005317 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005318 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005319 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005320
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005321 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005322 spin_lock(&root->inode_lock);
5323 empty = RB_EMPTY_ROOT(&root->inode_tree);
5324 spin_unlock(&root->inode_lock);
5325 if (empty)
5326 btrfs_add_dead_root(root);
5327 }
5328}
5329
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005330
Chris Masone02119d2008-09-05 16:13:11 -04005331static int btrfs_init_locked_inode(struct inode *inode, void *p)
5332{
5333 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005334
5335 inode->i_ino = args->ino;
5336 BTRFS_I(inode)->location.objectid = args->ino;
5337 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5338 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005339 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5340 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005341 return 0;
5342}
5343
5344static int btrfs_find_actor(struct inode *inode, void *opaque)
5345{
5346 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005347
5348 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005349 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005350}
5351
David Sterba0202e832020-05-15 19:35:59 +02005352static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005353 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005354{
5355 struct inode *inode;
5356 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005357 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005358
David Sterba0202e832020-05-15 19:35:59 +02005359 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005360 args.root = root;
5361
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005362 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005363 btrfs_init_locked_inode,
5364 (void *)&args);
5365 return inode;
5366}
5367
David Sterba4c66e0d2019-10-03 19:09:35 +02005368/*
David Sterba0202e832020-05-15 19:35:59 +02005369 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005370 * Path can be preallocated to prevent recursing back to iget through
5371 * allocator. NULL is also valid but may require an additional allocation
5372 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305373 */
David Sterba0202e832020-05-15 19:35:59 +02005374struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005375 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305376{
5377 struct inode *inode;
5378
David Sterba0202e832020-05-15 19:35:59 +02005379 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305380 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005381 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305382
5383 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005384 int ret;
5385
Filipe Manana4222ea72018-10-24 10:13:03 +01005386 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005387 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005388 inode_tree_add(inode);
5389 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005390 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005391 iget_failed(inode);
5392 /*
5393 * ret > 0 can come from btrfs_search_slot called by
5394 * btrfs_read_locked_inode, this means the inode item
5395 * was not found.
5396 */
5397 if (ret > 0)
5398 ret = -ENOENT;
5399 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005400 }
5401 }
5402
Balaji Rao1a54ef82008-07-21 02:01:04 +05305403 return inode;
5404}
5405
David Sterba0202e832020-05-15 19:35:59 +02005406struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005407{
David Sterba0202e832020-05-15 19:35:59 +02005408 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005409}
5410
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005411static struct inode *new_simple_dir(struct super_block *s,
5412 struct btrfs_key *key,
5413 struct btrfs_root *root)
5414{
5415 struct inode *inode = new_inode(s);
5416
5417 if (!inode)
5418 return ERR_PTR(-ENOMEM);
5419
Josef Bacik5c8fd992020-02-14 16:11:43 -05005420 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005421 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005422 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005423
5424 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005425 /*
5426 * We only need lookup, the rest is read-only and there's no inode
5427 * associated with the dentry
5428 */
5429 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005430 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005431 inode->i_fop = &simple_dir_operations;
5432 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005433 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305434 inode->i_atime = inode->i_mtime;
5435 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005436 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005437
5438 return inode;
5439}
5440
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005441static inline u8 btrfs_inode_type(struct inode *inode)
5442{
5443 /*
5444 * Compile-time asserts that generic FT_* types still match
5445 * BTRFS_FT_* types
5446 */
5447 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5448 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5449 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5450 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5451 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5452 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5453 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5454 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5455
5456 return fs_umode_to_ftype(inode->i_mode);
5457}
5458
Chris Mason3de45862008-11-17 21:02:50 -05005459struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005460{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005461 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005462 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005463 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005464 struct btrfs_root *sub_root = root;
5465 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005466 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005467 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005468
5469 if (dentry->d_name.len > BTRFS_NAME_LEN)
5470 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005471
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005472 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005473 if (ret < 0)
5474 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005475
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005476 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005477 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005478 if (IS_ERR(inode))
5479 return inode;
5480
5481 /* Do extra check against inode mode with di_type */
5482 if (btrfs_inode_type(inode) != di_type) {
5483 btrfs_crit(fs_info,
5484"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5485 inode->i_mode, btrfs_inode_type(inode),
5486 di_type);
5487 iput(inode);
5488 return ERR_PTR(-EUCLEAN);
5489 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005490 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005491 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005492
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005493 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005494 &location, &sub_root);
5495 if (ret < 0) {
5496 if (ret != -ENOENT)
5497 inode = ERR_PTR(ret);
5498 else
5499 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5500 } else {
David Sterba0202e832020-05-15 19:35:59 +02005501 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005502 }
Josef Bacik87270022020-01-24 09:32:31 -05005503 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005504 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005505
Julia Lawall34d19ba2011-01-24 19:55:19 +00005506 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005507 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005508 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005509 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005510 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005511 if (ret) {
5512 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005513 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005514 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005515 }
5516
Chris Mason3de45862008-11-17 21:02:50 -05005517 return inode;
5518}
5519
Nick Pigginfe15ce42011-01-07 17:49:23 +11005520static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005521{
5522 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005523 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005524
Li Zefan848cce02012-02-21 17:04:28 +08005525 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005526 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005527
Li Zefan848cce02012-02-21 17:04:28 +08005528 if (inode) {
5529 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005530 if (btrfs_root_refs(&root->root_item) == 0)
5531 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005532
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005533 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005534 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005535 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005536 return 0;
5537}
5538
Chris Mason3de45862008-11-17 21:02:50 -05005539static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005540 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005541{
Al Viro3837d202018-10-10 16:38:27 -04005542 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005543
Al Viro3837d202018-10-10 16:38:27 -04005544 if (inode == ERR_PTR(-ENOENT))
5545 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005546 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005547}
5548
Josef Bacik23b5ec72017-07-24 15:14:25 -04005549/*
5550 * All this infrastructure exists because dir_emit can fault, and we are holding
5551 * the tree lock when doing readdir. For now just allocate a buffer and copy
5552 * our information into that, and then dir_emit from the buffer. This is
5553 * similar to what NFS does, only we don't keep the buffer around in pagecache
5554 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5555 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5556 * tree lock.
5557 */
5558static int btrfs_opendir(struct inode *inode, struct file *file)
5559{
5560 struct btrfs_file_private *private;
5561
5562 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5563 if (!private)
5564 return -ENOMEM;
5565 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5566 if (!private->filldir_buf) {
5567 kfree(private);
5568 return -ENOMEM;
5569 }
5570 file->private_data = private;
5571 return 0;
5572}
5573
5574struct dir_entry {
5575 u64 ino;
5576 u64 offset;
5577 unsigned type;
5578 int name_len;
5579};
5580
5581static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5582{
5583 while (entries--) {
5584 struct dir_entry *entry = addr;
5585 char *name = (char *)(entry + 1);
5586
David Sterba92d32172018-04-16 21:10:14 +02005587 ctx->pos = get_unaligned(&entry->offset);
5588 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5589 get_unaligned(&entry->ino),
5590 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005591 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005592 addr += sizeof(struct dir_entry) +
5593 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005594 ctx->pos++;
5595 }
5596 return 0;
5597}
5598
Al Viro9cdda8d2013-05-22 16:48:09 -04005599static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005600{
Al Viro9cdda8d2013-05-22 16:48:09 -04005601 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005602 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005603 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005604 struct btrfs_dir_item *di;
5605 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005606 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005607 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005608 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005609 struct list_head ins_list;
5610 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005611 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005612 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005613 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005614 char *name_ptr;
5615 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005616 int entries = 0;
5617 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005618 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005619 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005620
Al Viro9cdda8d2013-05-22 16:48:09 -04005621 if (!dir_emit_dots(file, ctx))
5622 return 0;
5623
David Woodhouse49593bf2008-08-17 17:08:36 +01005624 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005625 if (!path)
5626 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005627
Josef Bacik23b5ec72017-07-24 15:14:25 -04005628 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005629 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005630
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005631 INIT_LIST_HEAD(&ins_list);
5632 INIT_LIST_HEAD(&del_list);
5633 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005634
Josef Bacik23b5ec72017-07-24 15:14:25 -04005635again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005636 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005637 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005638 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005639
Chris Mason39279cc2007-06-12 06:35:45 -04005640 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5641 if (ret < 0)
5642 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005643
5644 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005645 struct dir_entry *entry;
5646
Chris Mason5f39d392007-10-15 16:14:19 -04005647 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005648 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005649 if (slot >= btrfs_header_nritems(leaf)) {
5650 ret = btrfs_next_leaf(root, path);
5651 if (ret < 0)
5652 goto err;
5653 else if (ret > 0)
5654 break;
5655 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005656 }
Chris Mason3de45862008-11-17 21:02:50 -05005657
Chris Mason5f39d392007-10-15 16:14:19 -04005658 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5659
5660 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005661 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005662 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005663 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005664 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005665 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005666 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005667 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005668 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005669 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005670 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5671 PAGE_SIZE) {
5672 btrfs_release_path(path);
5673 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5674 if (ret)
5675 goto nopos;
5676 addr = private->filldir_buf;
5677 entries = 0;
5678 total_len = 0;
5679 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005680 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005681
5682 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005683 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005684 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005685 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5686 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005687 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005688 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005689 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005690 put_unaligned(location.objectid, &entry->ino);
5691 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005692 entries++;
5693 addr += sizeof(struct dir_entry) + name_len;
5694 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005695next:
5696 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005697 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005698 btrfs_release_path(path);
5699
5700 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5701 if (ret)
5702 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005703
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005704 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005705 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005706 goto nopos;
5707
Zach Browndb62efb2013-07-11 16:19:42 -07005708 /*
5709 * Stop new entries from being returned after we return the last
5710 * entry.
5711 *
5712 * New directory entries are assigned a strictly increasing
5713 * offset. This means that new entries created during readdir
5714 * are *guaranteed* to be seen in the future by that readdir.
5715 * This has broken buggy programs which operate on names as
5716 * they're returned by readdir. Until we re-use freed offsets
5717 * we have this hack to stop new entries from being returned
5718 * under the assumption that they'll never reach this huge
5719 * offset.
5720 *
5721 * This is being careful not to overflow 32bit loff_t unless the
5722 * last entry requires it because doing so has broken 32bit apps
5723 * in the past.
5724 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005725 if (ctx->pos >= INT_MAX)
5726 ctx->pos = LLONG_MAX;
5727 else
5728 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005729nopos:
5730 ret = 0;
5731err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005732 if (put)
5733 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005734 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005735 return ret;
5736}
5737
Chris Mason39279cc2007-06-12 06:35:45 -04005738/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005739 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005740 * inode changes. But, it is most likely to find the inode in cache.
5741 * FIXME, needs more benchmarking...there are no reasons other than performance
5742 * to keep or drop this code.
5743 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005744static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005745{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005746 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005747 struct btrfs_root *root = BTRFS_I(inode)->root;
5748 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005749 int ret;
5750
Josef Bacik72ac3c02012-05-23 14:13:11 -04005751 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005752 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005753
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005754 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005755 if (IS_ERR(trans))
5756 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005757
5758 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005759 if (ret && ret == -ENOSPC) {
5760 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005761 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005762 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005763 if (IS_ERR(trans))
5764 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005765
Chris Mason94b60442010-05-26 11:02:00 -04005766 ret = btrfs_update_inode(trans, root, inode);
Chris Mason94b60442010-05-26 11:02:00 -04005767 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005768 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005769 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005770 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005771
5772 return ret;
5773}
5774
5775/*
5776 * This is a copy of file_update_time. We need this so we can return error on
5777 * ENOSPC for updating the inode in the case of file write and mmap writes.
5778 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005779static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005780 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005781{
Alexander Block2bc5565282012-06-15 09:49:33 +02005782 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005783 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005784
5785 if (btrfs_root_readonly(root))
5786 return -EROFS;
5787
Josef Bacike41f9412012-03-26 09:46:47 -04005788 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005789 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005790 if (flags & S_CTIME)
5791 inode->i_ctime = *now;
5792 if (flags & S_MTIME)
5793 inode->i_mtime = *now;
5794 if (flags & S_ATIME)
5795 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005796 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005797}
5798
Chris Masond352ac62008-09-29 15:18:18 -04005799/*
5800 * find the highest existing sequence number in a directory
5801 * and then set the in-memory index_cnt variable to reflect
5802 * free sequence numbers
5803 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005804static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005805{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005806 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005807 struct btrfs_key key, found_key;
5808 struct btrfs_path *path;
5809 struct extent_buffer *leaf;
5810 int ret;
5811
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005812 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005813 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005814 key.offset = (u64)-1;
5815
5816 path = btrfs_alloc_path();
5817 if (!path)
5818 return -ENOMEM;
5819
5820 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5821 if (ret < 0)
5822 goto out;
5823 /* FIXME: we should be able to handle this */
5824 if (ret == 0)
5825 goto out;
5826 ret = 0;
5827
5828 /*
5829 * MAGIC NUMBER EXPLANATION:
5830 * since we search a directory based on f_pos we have to start at 2
5831 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5832 * else has to start at 2
5833 */
5834 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005835 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005836 goto out;
5837 }
5838
5839 path->slots[0]--;
5840
5841 leaf = path->nodes[0];
5842 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5843
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005844 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02005845 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005846 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005847 goto out;
5848 }
5849
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005850 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04005851out:
5852 btrfs_free_path(path);
5853 return ret;
5854}
5855
Chris Masond352ac62008-09-29 15:18:18 -04005856/*
5857 * helper to find a free sequence number in a given directory. This current
5858 * code is very simple, later versions will do smarter things in the btree
5859 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02005860int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04005861{
5862 int ret = 0;
5863
Nikolay Borisov877574e2017-02-20 13:50:33 +02005864 if (dir->index_cnt == (u64)-1) {
5865 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08005866 if (ret) {
5867 ret = btrfs_set_inode_index_count(dir);
5868 if (ret)
5869 return ret;
5870 }
Josef Bacikaec74772008-07-24 12:12:38 -04005871 }
5872
Nikolay Borisov877574e2017-02-20 13:50:33 +02005873 *index = dir->index_cnt;
5874 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04005875
5876 return ret;
5877}
5878
Chris Masonb0d5d102014-09-08 13:08:51 -07005879static int btrfs_insert_inode_locked(struct inode *inode)
5880{
5881 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005882
5883 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07005884 args.root = BTRFS_I(inode)->root;
5885
5886 return insert_inode_locked4(inode,
5887 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
5888 btrfs_find_actor, &args);
5889}
5890
Anand Jain19aee8d2017-07-18 17:37:05 +08005891/*
5892 * Inherit flags from the parent inode.
5893 *
5894 * Currently only the compression flags and the cow flags are inherited.
5895 */
5896static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
5897{
5898 unsigned int flags;
5899
5900 if (!dir)
5901 return;
5902
5903 flags = BTRFS_I(dir)->flags;
5904
5905 if (flags & BTRFS_INODE_NOCOMPRESS) {
5906 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
5907 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
5908 } else if (flags & BTRFS_INODE_COMPRESS) {
5909 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
5910 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
5911 }
5912
5913 if (flags & BTRFS_INODE_NODATACOW) {
5914 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
5915 if (S_ISREG(inode->i_mode))
5916 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
5917 }
5918
David Sterba7b6a2212018-03-26 18:40:21 +02005919 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08005920}
5921
Chris Mason39279cc2007-06-12 06:35:45 -04005922static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
5923 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04005924 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05005925 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04005926 u64 ref_objectid, u64 objectid,
5927 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04005928{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005929 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04005930 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04005931 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04005932 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04005933 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05005934 struct btrfs_inode_ref *ref;
5935 struct btrfs_key key[2];
5936 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005937 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05005938 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04005939 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04005940 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04005941
Chris Mason5f39d392007-10-15 16:14:19 -04005942 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005943 if (!path)
5944 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04005945
Josef Bacik11a19a92019-09-09 10:12:04 -04005946 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005947 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04005948 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005949 if (!inode) {
5950 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005951 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005952 }
Chris Mason39279cc2007-06-12 06:35:45 -04005953
Li Zefan581bb052011-04-20 10:06:11 +08005954 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01005955 * O_TMPFILE, set link count to 0, so that after this point,
5956 * we fill in an inode item with the correct link count.
5957 */
5958 if (!name)
5959 set_nlink(inode, 0);
5960
5961 /*
Li Zefan581bb052011-04-20 10:06:11 +08005962 * we have to initialize this early, so we can reclaim the inode
5963 * number if we fail afterwards in this function.
5964 */
5965 inode->i_ino = objectid;
5966
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005967 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00005968 trace_btrfs_inode_request(dir);
5969
Nikolay Borisov877574e2017-02-20 13:50:33 +02005970 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04005971 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00005972 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04005973 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04005974 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04005975 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01005976 } else if (dir) {
5977 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04005978 }
5979 /*
5980 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08005981 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04005982 * number
5983 */
5984 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08005985 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005986 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04005987 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00005988 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04005989
Josef Bacik5dc562c2012-08-17 13:14:17 -04005990 /*
5991 * We could have gotten an inode number from somebody who was fsynced
5992 * and then removed in this same transaction, so let's just set full
5993 * sync since it will be a full sync anyway and this will blow away the
5994 * old info in the log.
5995 */
5996 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
5997
Chris Mason9c583092008-01-29 15:15:18 -05005998 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02005999 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006000 key[0].offset = 0;
6001
Chris Mason9c583092008-01-29 15:15:18 -05006002 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006003
6004 if (name) {
6005 /*
6006 * Start new inodes with an inode_ref. This is slightly more
6007 * efficient for small numbers of hard links since they will
6008 * be packed into one item. Extended refs will kick in if we
6009 * add more hard links than can fit in the ref item.
6010 */
6011 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006012 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006013 key[1].offset = ref_objectid;
6014
6015 sizes[1] = name_len + sizeof(*ref);
6016 }
Chris Mason9c583092008-01-29 15:15:18 -05006017
Chris Masonb0d5d102014-09-08 13:08:51 -07006018 location = &BTRFS_I(inode)->location;
6019 location->objectid = objectid;
6020 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006021 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006022
6023 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006024 if (ret < 0) {
6025 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006026 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006027 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006028
Chris Masonb9473432009-03-13 11:00:37 -04006029 path->leave_spinning = 1;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006030 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006031 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006032 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006033
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006034 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006035 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306036
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006037 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306038 inode->i_atime = inode->i_mtime;
6039 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006040 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306041
Chris Mason5f39d392007-10-15 16:14:19 -04006042 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6043 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006044 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006045 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006046 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006047
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006048 if (name) {
6049 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6050 struct btrfs_inode_ref);
6051 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6052 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6053 ptr = (unsigned long)(ref + 1);
6054 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6055 }
Chris Mason9c583092008-01-29 15:15:18 -05006056
Chris Mason5f39d392007-10-15 16:14:19 -04006057 btrfs_mark_buffer_dirty(path->nodes[0]);
6058 btrfs_free_path(path);
6059
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006060 btrfs_inherit_iflags(inode, dir);
6061
Al Viro569254b2011-07-24 17:08:40 -04006062 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006063 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006064 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006065 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006066 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6067 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006068 }
6069
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006070 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006071
6072 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006073 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006074
Alexander Block8ea05e32012-07-25 17:35:53 +02006075 btrfs_update_root_times(trans, root);
6076
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006077 ret = btrfs_inode_inherit_props(trans, inode, dir);
6078 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006079 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006080 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006081 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006082
Chris Mason39279cc2007-06-12 06:35:45 -04006083 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006084
6085fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006086 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006087fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006088 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006089 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006090 btrfs_free_path(path);
6091 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006092}
6093
Chris Masond352ac62008-09-29 15:18:18 -04006094/*
6095 * utility function to add 'inode' into 'parent_inode' with
6096 * a give name and a given sequence number.
6097 * if 'add_backref' is true, also insert a backref from the
6098 * inode to the parent directory.
6099 */
Chris Masone02119d2008-09-05 16:13:11 -04006100int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006101 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006102 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006103{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006104 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006105 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006106 struct btrfs_root *root = parent_inode->root;
6107 u64 ino = btrfs_ino(inode);
6108 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006109
Li Zefan33345d012011-04-20 10:31:50 +08006110 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006111 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006112 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006113 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006114 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006115 key.offset = 0;
6116 }
Chris Mason39279cc2007-06-12 06:35:45 -04006117
Li Zefan33345d012011-04-20 10:31:50 +08006118 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006119 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006120 root->root_key.objectid, parent_ino,
6121 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006122 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006123 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6124 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006125 }
6126
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006127 /* Nothing to clean up yet */
6128 if (ret)
6129 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006130
Lu Fengqi684572d2018-08-04 21:10:57 +08006131 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006132 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006133 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006134 goto fail_dir_item;
6135 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006136 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006137 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006138 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006139
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006140 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006141 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006142 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006143 /*
6144 * If we are replaying a log tree, we do not want to update the mtime
6145 * and ctime of the parent directory with the current time, since the
6146 * log replay procedure is responsible for setting them to their correct
6147 * values (the ones it had when the fsync was done).
6148 */
6149 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6150 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6151
6152 parent_inode->vfs_inode.i_mtime = now;
6153 parent_inode->vfs_inode.i_ctime = now;
6154 }
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006155 ret = btrfs_update_inode(trans, root, &parent_inode->vfs_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006156 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006157 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006158 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006159
6160fail_dir_item:
6161 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6162 u64 local_index;
6163 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006164 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006165 root->root_key.objectid, parent_ino,
6166 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006167 if (err)
6168 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006169 } else if (add_backref) {
6170 u64 local_index;
6171 int err;
6172
6173 err = btrfs_del_inode_ref(trans, root, name, name_len,
6174 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006175 if (err)
6176 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006177 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006178
6179 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006180 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006181}
6182
6183static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006184 struct btrfs_inode *dir, struct dentry *dentry,
6185 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006186{
Josef Bacika1b075d2010-11-19 20:36:11 +00006187 int err = btrfs_add_link(trans, dir, inode,
6188 dentry->d_name.name, dentry->d_name.len,
6189 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006190 if (err > 0)
6191 err = -EEXIST;
6192 return err;
6193}
6194
Josef Bacik618e21d2007-07-11 10:18:17 -04006195static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006196 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006197{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006198 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006199 struct btrfs_trans_handle *trans;
6200 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006201 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006202 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006203 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006204 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006205
Josef Bacik9ed74f22009-09-11 16:12:44 -04006206 /*
6207 * 2 for inode item and ref
6208 * 2 for dir items
6209 * 1 for xattr if selinux is on
6210 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006211 trans = btrfs_start_transaction(root, 5);
6212 if (IS_ERR(trans))
6213 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006214
Li Zefan581bb052011-04-20 10:06:11 +08006215 err = btrfs_find_free_ino(root, &objectid);
6216 if (err)
6217 goto out_unlock;
6218
Josef Bacikaec74772008-07-24 12:12:38 -04006219 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006220 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6221 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006222 if (IS_ERR(inode)) {
6223 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006224 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006225 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006226 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006227
Casey Schauflerad19db72011-12-15 10:09:07 -05006228 /*
6229 * If the active LSM wants to access the inode during
6230 * d_instantiate it needs these. Smack checks to see
6231 * if the filesystem supports xattrs by looking at the
6232 * ops vector.
6233 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006234 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006235 init_special_inode(inode, inode->i_mode, rdev);
6236
6237 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006238 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006239 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006240
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006241 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6242 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006243 if (err)
6244 goto out_unlock;
6245
6246 btrfs_update_inode(trans, root, inode);
6247 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006248
Josef Bacik618e21d2007-07-11 10:18:17 -04006249out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006250 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006251 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006252 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006253 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006254 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006255 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006256 return err;
6257}
6258
Chris Mason39279cc2007-06-12 06:35:45 -04006259static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006260 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006261{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006262 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006263 struct btrfs_trans_handle *trans;
6264 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006265 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006266 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006267 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006268 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006269
Josef Bacik9ed74f22009-09-11 16:12:44 -04006270 /*
6271 * 2 for inode item and ref
6272 * 2 for dir items
6273 * 1 for xattr if selinux is on
6274 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006275 trans = btrfs_start_transaction(root, 5);
6276 if (IS_ERR(trans))
6277 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006278
Li Zefan581bb052011-04-20 10:06:11 +08006279 err = btrfs_find_free_ino(root, &objectid);
6280 if (err)
6281 goto out_unlock;
6282
Josef Bacikaec74772008-07-24 12:12:38 -04006283 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006284 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6285 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006286 if (IS_ERR(inode)) {
6287 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006288 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006289 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006290 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006291 /*
6292 * If the active LSM wants to access the inode during
6293 * d_instantiate it needs these. Smack checks to see
6294 * if the filesystem supports xattrs by looking at the
6295 * ops vector.
6296 */
6297 inode->i_fop = &btrfs_file_operations;
6298 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006299 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006300
6301 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6302 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006303 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006304
6305 err = btrfs_update_inode(trans, root, inode);
6306 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006307 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006308
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006309 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6310 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006311 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006312 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006313
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006314 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Al Viro1e2e5472018-05-04 08:23:01 -04006315 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006316
Chris Mason39279cc2007-06-12 06:35:45 -04006317out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006318 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006319 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006320 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006321 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006322 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006323 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006324 return err;
6325}
6326
6327static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6328 struct dentry *dentry)
6329{
Filipe Manana271dba452016-01-05 16:24:05 +00006330 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006331 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006332 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006333 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006334 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006335 int err;
6336 int drop_inode = 0;
6337
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006338 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006339 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006340 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006341
Mark Fashehf1863732012-08-08 11:32:27 -07006342 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006343 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006344
Nikolay Borisov877574e2017-02-20 13:50:33 +02006345 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006346 if (err)
6347 goto fail;
6348
Yan, Zhenga22285a2010-05-16 10:48:46 -04006349 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006350 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006351 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006352 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006353 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006354 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006355 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006356 if (IS_ERR(trans)) {
6357 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006358 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006359 goto fail;
6360 }
Chris Mason5f39d392007-10-15 16:14:19 -04006361
Miao Xie67de1172013-12-26 13:07:06 +08006362 /* There are several dir indexes for this inode, clear the cache. */
6363 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006364 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006365 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006366 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006367 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006368 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006369
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006370 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6371 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006372
Yan, Zhenga5719522009-09-24 09:17:31 -04006373 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006374 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006375 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006376 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006377
Yan, Zhenga5719522009-09-24 09:17:31 -04006378 err = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006379 if (err)
6380 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006381 if (inode->i_nlink == 1) {
6382 /*
6383 * If new hard link count is 1, it's a file created
6384 * with open(2) O_TMPFILE flag.
6385 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006386 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006387 if (err)
6388 goto fail;
6389 }
Al Viro08c422c2011-12-23 07:58:13 -05006390 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006391 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006392 }
Chris Mason39279cc2007-06-12 06:35:45 -04006393
Chris Mason1832a6d2007-12-21 16:27:21 -05006394fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006395 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006396 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006397 if (drop_inode) {
6398 inode_dec_link_count(inode);
6399 iput(inode);
6400 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006401 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006402 return err;
6403}
6404
Al Viro18bb1db2011-07-26 01:41:39 -04006405static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006406{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006407 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006408 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006409 struct btrfs_trans_handle *trans;
6410 struct btrfs_root *root = BTRFS_I(dir)->root;
6411 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006412 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006413 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006414
Josef Bacik9ed74f22009-09-11 16:12:44 -04006415 /*
6416 * 2 items for inode and ref
6417 * 2 items for dir items
6418 * 1 for xattr if selinux is on
6419 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006420 trans = btrfs_start_transaction(root, 5);
6421 if (IS_ERR(trans))
6422 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006423
Li Zefan581bb052011-04-20 10:06:11 +08006424 err = btrfs_find_free_ino(root, &objectid);
6425 if (err)
6426 goto out_fail;
6427
Josef Bacikaec74772008-07-24 12:12:38 -04006428 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006429 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6430 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006431 if (IS_ERR(inode)) {
6432 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006433 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006434 goto out_fail;
6435 }
Chris Mason5f39d392007-10-15 16:14:19 -04006436
Chris Masonb0d5d102014-09-08 13:08:51 -07006437 /* these must be set before we unlock the inode */
6438 inode->i_op = &btrfs_dir_inode_operations;
6439 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006440
Eric Paris2a7dba32011-02-01 11:05:39 -05006441 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006442 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006443 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006444
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006445 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason39279cc2007-06-12 06:35:45 -04006446 err = btrfs_update_inode(trans, root, inode);
6447 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006448 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006449
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006450 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6451 dentry->d_name.name,
6452 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006453 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006454 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006455
Al Viro1e2e5472018-05-04 08:23:01 -04006456 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006457
6458out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006459 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006460 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006461 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006462 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006463 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006464 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006465 return err;
6466}
6467
Chris Masonc8b97812008-10-29 14:49:59 -04006468static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006469 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006470 size_t pg_offset, u64 extent_offset,
6471 struct btrfs_file_extent_item *item)
6472{
6473 int ret;
6474 struct extent_buffer *leaf = path->nodes[0];
6475 char *tmp;
6476 size_t max_size;
6477 unsigned long inline_size;
6478 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006479 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006480
6481 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006482 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006483 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6484 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006485 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006486 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006487 if (!tmp)
6488 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006489 ptr = btrfs_file_extent_inline_start(item);
6490
6491 read_extent_buffer(leaf, tmp, ptr, inline_size);
6492
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006493 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006494 ret = btrfs_decompress(compress_type, tmp, page,
6495 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006496
6497 /*
6498 * decompression code contains a memset to fill in any space between the end
6499 * of the uncompressed data and the end of max_size in case the decompressed
6500 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6501 * the end of an inline extent and the beginning of the next block, so we
6502 * cover that region here.
6503 */
6504
6505 if (max_size + pg_offset < PAGE_SIZE) {
6506 char *map = kmap(page);
6507 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6508 kunmap(page);
6509 }
Chris Masonc8b97812008-10-29 14:49:59 -04006510 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006511 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006512}
6513
Omar Sandoval39b07b52019-12-02 17:34:23 -08006514/**
6515 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6516 * @inode: file to search in
6517 * @page: page to read extent data into if the extent is inline
6518 * @pg_offset: offset into @page to copy to
6519 * @start: file offset
6520 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006521 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006522 * This returns the first &struct extent_map which overlaps with the given
6523 * range, reading it from the B-tree and caching it if necessary. Note that
6524 * there may be more extents which overlap the given range after the returned
6525 * extent_map.
6526 *
6527 * If @page is not NULL and the extent is inline, this also reads the extent
6528 * data directly into the page and marks the extent up to date in the io_tree.
6529 *
6530 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006531 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006532struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006533 struct page *page, size_t pg_offset,
6534 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006535{
David Sterba3ffbd682018-06-29 10:56:42 +02006536 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006537 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006538 u64 extent_start = 0;
6539 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006540 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006541 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006542 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006543 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006544 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006545 struct extent_buffer *leaf;
6546 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006547 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006548 struct extent_map_tree *em_tree = &inode->extent_tree;
6549 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006550
Chris Mason890871b2009-09-02 16:24:52 -04006551 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006552 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006553 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006554
Chris Masona52d9a82007-08-27 16:49:44 -04006555 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006556 if (em->start > start || em->start + em->len <= start)
6557 free_extent_map(em);
6558 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006559 free_extent_map(em);
6560 else
6561 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006562 }
David Sterba172ddd62011-04-21 00:48:27 +02006563 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006564 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006565 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006566 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006567 }
Chris Masond1310b22008-01-24 16:13:08 -05006568 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006569 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006570 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006571 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006572
Liu Bobee6ec82018-08-17 05:05:28 +08006573 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006574 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006575 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006576 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006577 }
6578
Liu Bobee6ec82018-08-17 05:05:28 +08006579 /* Chances are we'll be called again, so go ahead and do readahead */
6580 path->reada = READA_FORWARD;
6581
Liu Boe49aabd2018-08-25 13:47:09 +08006582 /*
6583 * Unless we're going to uncompress the inline extent, no sleep would
6584 * happen.
6585 */
6586 path->leave_spinning = 1;
6587
Josef Bacik51899412020-08-20 11:46:01 -04006588 path->recurse = btrfs_is_free_space_inode(inode);
6589
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006590 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006591 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006592 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006593 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006594 if (path->slots[0] == 0)
6595 goto not_found;
6596 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006597 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006598 }
6599
Chris Mason5f39d392007-10-15 16:14:19 -04006600 leaf = path->nodes[0];
6601 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006602 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006603 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006604 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006605 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006606 /*
6607 * If we backup past the first extent we want to move forward
6608 * and see if there is an extent in front of us, otherwise we'll
6609 * say there is a hole for our whole search range which can
6610 * cause problems.
6611 */
6612 extent_end = start;
6613 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006614 }
6615
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006616 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006617 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006618 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006619 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6620 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006621 /* Only regular file could have regular/prealloc extent */
6622 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006623 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006624 btrfs_crit(fs_info,
6625 "regular/prealloc extent found for non-regular inode %llu",
6626 btrfs_ino(inode));
6627 goto out;
6628 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006629 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6630 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006631 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006632 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6633 path->slots[0],
6634 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006635 }
Josef Bacik25a50342013-10-14 12:08:38 -04006636next:
Yan Zheng9036c102008-10-30 14:19:41 -04006637 if (start >= extent_end) {
6638 path->slots[0]++;
6639 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6640 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006641 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006642 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006643 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006644 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006645
Yan Zheng9036c102008-10-30 14:19:41 -04006646 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006647 }
Yan Zheng9036c102008-10-30 14:19:41 -04006648 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6649 if (found_key.objectid != objectid ||
6650 found_key.type != BTRFS_EXTENT_DATA_KEY)
6651 goto not_found;
6652 if (start + len <= found_key.offset)
6653 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006654 if (start > found_key.offset)
6655 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006656
6657 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006658 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006659 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006660 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006661 em->block_start = EXTENT_MAP_HOLE;
6662 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006663 }
6664
Omar Sandoval39b07b52019-12-02 17:34:23 -08006665 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006666
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006667 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6668 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006669 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006670 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006671 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006672 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006673 size_t size;
6674 size_t extent_offset;
6675 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006676
Omar Sandoval39b07b52019-12-02 17:34:23 -08006677 if (!page)
Yan689f9342007-10-29 11:41:07 -04006678 goto out;
Yan689f9342007-10-29 11:41:07 -04006679
Qu Wenruoe41ca582018-06-06 15:41:49 +08006680 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006681 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006682 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6683 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006684 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006685 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006686 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006687 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006688 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006689
6690 btrfs_set_path_blocking(path);
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006691 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006692 if (btrfs_file_extent_compression(leaf, item) !=
6693 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006694 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006695 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006696 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04006697 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04006698 } else {
6699 map = kmap(page);
6700 read_extent_buffer(leaf, map + pg_offset, ptr,
6701 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006702 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006703 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006704 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006705 copy_size);
6706 }
Chris Masonc8b97812008-10-29 14:49:59 -04006707 kunmap(page);
6708 }
Chris Mason179e29e2007-11-01 11:28:41 -04006709 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006710 }
Chris Masond1310b22008-01-24 16:13:08 -05006711 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006712 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006713 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006714 }
6715not_found:
6716 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006717 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006718 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006719 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006720insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006721 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02006722 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006723 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006724 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006725 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6726 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006727 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04006728 goto out;
6729 }
Chris Masond1310b22008-01-24 16:13:08 -05006730
Chris Mason890871b2009-09-02 16:24:52 -04006731 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006732 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006733 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006734out:
Liu Boc6414282018-08-23 07:36:17 +08006735 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006736
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006737 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006738
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006739 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04006740 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006741 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04006742 }
6743 return em;
6744}
6745
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006746struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006747 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006748{
6749 struct extent_map *em;
6750 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006751 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006752 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006753 u64 delalloc_len;
6754 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006755 int err = 0;
6756
Omar Sandoval39b07b52019-12-02 17:34:23 -08006757 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006758 if (IS_ERR(em))
6759 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006760 /*
6761 * If our em maps to:
6762 * - a hole or
6763 * - a pre-alloc extent,
6764 * there might actually be delalloc bytes behind it.
6765 */
6766 if (em->block_start != EXTENT_MAP_HOLE &&
6767 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6768 return em;
6769 else
6770 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006771
6772 /* check to see if we've wrapped (len == -1 or similar) */
6773 end = start + len;
6774 if (end < start)
6775 end = (u64)-1;
6776 else
6777 end -= 1;
6778
6779 em = NULL;
6780
6781 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006782 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006783 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006784 delalloc_end = delalloc_start + delalloc_len;
6785 if (delalloc_end < delalloc_start)
6786 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006787
6788 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006789 * We didn't find anything useful, return the original results from
6790 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006791 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006792 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006793 em = hole_em;
6794 hole_em = NULL;
6795 goto out;
6796 }
6797
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006798 /*
6799 * Adjust the delalloc_start to make sure it doesn't go backwards from
6800 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006801 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006802 delalloc_start = max(start, delalloc_start);
6803 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006804
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006805 if (delalloc_len > 0) {
6806 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006807 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006808 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006809
David Sterba172ddd62011-04-21 00:48:27 +02006810 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006811 if (!em) {
6812 err = -ENOMEM;
6813 goto out;
6814 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006815
6816 ASSERT(hole_em);
6817 /*
6818 * When btrfs_get_extent can't find anything it returns one
6819 * huge hole
6820 *
6821 * Make sure what it found really fits our range, and adjust to
6822 * make sure it is based on the start from the caller
6823 */
6824 if (hole_end <= start || hole_em->start > end) {
6825 free_extent_map(hole_em);
6826 hole_em = NULL;
6827 } else {
6828 hole_start = max(hole_em->start, start);
6829 hole_len = hole_end - hole_start;
6830 }
6831
6832 if (hole_em && delalloc_start > hole_start) {
6833 /*
6834 * Our hole starts before our delalloc, so we have to
6835 * return just the parts of the hole that go until the
6836 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006837 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006838 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006839 em->start = hole_start;
6840 em->orig_start = hole_start;
6841 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006842 * Don't adjust block start at all, it is fixed at
6843 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006844 */
6845 em->block_start = hole_em->block_start;
6846 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00006847 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
6848 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05006849 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006850 /*
6851 * Hole is out of passed range or it starts after
6852 * delalloc range
6853 */
6854 em->start = delalloc_start;
6855 em->len = delalloc_len;
6856 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006857 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006858 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05006859 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02006860 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05006861 return hole_em;
6862 }
6863out:
6864
6865 free_extent_map(hole_em);
6866 if (err) {
6867 free_extent_map(em);
6868 return ERR_PTR(err);
6869 }
6870 return em;
6871}
6872
Nikolay Borisov64f54182020-06-03 08:55:31 +03006873static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006874 const u64 start,
6875 const u64 len,
6876 const u64 orig_start,
6877 const u64 block_start,
6878 const u64 block_len,
6879 const u64 orig_block_len,
6880 const u64 ram_bytes,
6881 const int type)
6882{
6883 struct extent_map *em = NULL;
6884 int ret;
6885
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006886 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03006887 em = create_io_em(inode, start, len, orig_start, block_start,
6888 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08006889 BTRFS_COMPRESS_NONE, /* compress_type */
6890 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006891 if (IS_ERR(em))
6892 goto out;
6893 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03006894 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
6895 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006896 if (ret) {
6897 if (em) {
6898 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03006899 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006900 }
6901 em = ERR_PTR(ret);
6902 }
6903 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006904
6905 return em;
6906}
6907
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006908static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04006909 u64 start, u64 len)
6910{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006911 struct btrfs_root *root = inode->root;
6912 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04006913 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006914 struct btrfs_key ins;
6915 u64 alloc_hint;
6916 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04006917
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006918 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006919 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04006920 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04006921 if (ret)
6922 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04006923
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006924 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006925 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08006926 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006927 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01006928 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03006929 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
6930 1);
Josef Bacik00361582013-08-14 14:02:47 -04006931
Josef Bacik4b46fce2010-05-23 11:00:55 -04006932 return em;
6933}
6934
Chris Mason46bfbb52010-05-26 11:04:10 -04006935/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006936 * Check if we can do nocow write into the range [@offset, @offset + @len)
6937 *
6938 * @offset: File offset
6939 * @len: The length to write, will be updated to the nocow writeable
6940 * range
6941 * @orig_start: (optional) Return the original file offset of the file extent
6942 * @orig_len: (optional) Return the original on-disk length of the file extent
6943 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07006944 * @strict: if true, omit optimizations that might force us into unnecessary
6945 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08006946 *
6947 * This function will flush ordered extents in the range to ensure proper
6948 * nocow checks for (nowait == false) case.
6949 *
6950 * Return:
6951 * >0 and update @len if we can do nocow write
6952 * 0 if we can't do nocow write
6953 * <0 if error happened
6954 *
6955 * NOTE: This only checks the file extents, caller is responsible to wait for
6956 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04006957 */
Josef Bacik00361582013-08-14 14:02:47 -04006958noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04006959 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07006960 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04006961{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006962 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04006963 struct btrfs_path *path;
6964 int ret;
6965 struct extent_buffer *leaf;
6966 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08006967 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04006968 struct btrfs_file_extent_item *fi;
6969 struct btrfs_key key;
6970 u64 disk_bytenr;
6971 u64 backref_offset;
6972 u64 extent_end;
6973 u64 num_bytes;
6974 int slot;
6975 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04006976 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08006977
Chris Mason46bfbb52010-05-26 11:04:10 -04006978 path = btrfs_alloc_path();
6979 if (!path)
6980 return -ENOMEM;
6981
David Sterbaf85b7372017-01-20 14:54:07 +01006982 ret = btrfs_lookup_file_extent(NULL, root, path,
6983 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04006984 if (ret < 0)
6985 goto out;
6986
6987 slot = path->slots[0];
6988 if (ret == 1) {
6989 if (slot == 0) {
6990 /* can't find the item, must cow */
6991 ret = 0;
6992 goto out;
6993 }
6994 slot--;
6995 }
6996 ret = 0;
6997 leaf = path->nodes[0];
6998 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02006999 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007000 key.type != BTRFS_EXTENT_DATA_KEY) {
7001 /* not our file or wrong item type, must cow */
7002 goto out;
7003 }
7004
7005 if (key.offset > offset) {
7006 /* Wrong offset, must cow */
7007 goto out;
7008 }
7009
7010 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7011 found_type = btrfs_file_extent_type(leaf, fi);
7012 if (found_type != BTRFS_FILE_EXTENT_REG &&
7013 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7014 /* not a regular extent, must cow */
7015 goto out;
7016 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007017
7018 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7019 goto out;
7020
Miao Xiee77751a2013-12-27 21:11:50 +08007021 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7022 if (extent_end <= offset)
7023 goto out;
7024
Chris Mason46bfbb52010-05-26 11:04:10 -04007025 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007026 if (disk_bytenr == 0)
7027 goto out;
7028
7029 if (btrfs_file_extent_compression(leaf, fi) ||
7030 btrfs_file_extent_encryption(leaf, fi) ||
7031 btrfs_file_extent_other_encoding(leaf, fi))
7032 goto out;
7033
Ethan Lien78d42952018-05-17 14:58:29 +08007034 /*
7035 * Do the same check as in btrfs_cross_ref_exist but without the
7036 * unnecessary search.
7037 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007038 if (!strict &&
7039 (btrfs_file_extent_generation(leaf, fi) <=
7040 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007041 goto out;
7042
Chris Mason46bfbb52010-05-26 11:04:10 -04007043 backref_offset = btrfs_file_extent_offset(leaf, fi);
7044
Josef Bacik7ee9e442013-06-21 16:37:03 -04007045 if (orig_start) {
7046 *orig_start = key.offset - backref_offset;
7047 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7048 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7049 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007050
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007051 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007052 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007053
7054 num_bytes = min(offset + *len, extent_end) - offset;
7055 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7056 u64 range_end;
7057
Jeff Mahoneyda170662016-06-15 09:22:56 -04007058 range_end = round_up(offset + num_bytes,
7059 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007060 ret = test_range_bit(io_tree, offset, range_end,
7061 EXTENT_DELALLOC, 0, NULL);
7062 if (ret) {
7063 ret = -EAGAIN;
7064 goto out;
7065 }
7066 }
7067
Josef Bacik1bda19e2013-10-18 12:10:36 -04007068 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007069
7070 /*
7071 * look for other files referencing this extent, if we
7072 * find any we must cow
7073 */
Josef Bacik00361582013-08-14 14:02:47 -04007074
Liu Boe4c3b2d2017-01-30 12:25:28 -08007075 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007076 key.offset - backref_offset, disk_bytenr,
7077 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007078 if (ret) {
7079 ret = 0;
7080 goto out;
7081 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007082
7083 /*
7084 * adjust disk_bytenr and num_bytes to cover just the bytes
7085 * in this extent we are about to write. If there
7086 * are any csums in that range we have to cow in order
7087 * to keep the csums correct
7088 */
7089 disk_bytenr += backref_offset;
7090 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007091 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7092 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007093 /*
7094 * all of the above have passed, it is safe to overwrite this extent
7095 * without cow
7096 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007097 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007098 ret = 1;
7099out:
7100 btrfs_free_path(path);
7101 return ret;
7102}
7103
Josef Bacikeb838e72012-07-31 16:28:48 -04007104static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007105 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007106{
7107 struct btrfs_ordered_extent *ordered;
7108 int ret = 0;
7109
7110 while (1) {
7111 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007112 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007113 /*
7114 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007115 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007116 * extents in this range.
7117 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007118 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007119 lockend - lockstart + 1);
7120
7121 /*
7122 * We need to make sure there are no buffered pages in this
7123 * range either, we could have raced between the invalidate in
7124 * generic_file_direct_write and locking the extent. The
7125 * invalidate needs to happen so that reads after a write do not
7126 * get stale data.
7127 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007128 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007129 (!writing || !filemap_range_has_page(inode->i_mapping,
7130 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007131 break;
7132
7133 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007134 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007135
7136 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007137 /*
7138 * If we are doing a DIO read and the ordered extent we
7139 * found is for a buffered write, we can not wait for it
7140 * to complete and retry, because if we do so we can
7141 * deadlock with concurrent buffered writes on page
7142 * locks. This happens only if our DIO read covers more
7143 * than one extent map, if at this point has already
7144 * created an ordered extent for a previous extent map
7145 * and locked its range in the inode's io tree, and a
7146 * concurrent write against that previous extent map's
7147 * range and this range started (we unlock the ranges
7148 * in the io tree only when the bios complete and
7149 * buffered writes always lock pages before attempting
7150 * to lock range in the io tree).
7151 */
7152 if (writing ||
7153 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7154 btrfs_start_ordered_extent(inode, ordered, 1);
7155 else
7156 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007157 btrfs_put_ordered_extent(ordered);
7158 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007159 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007160 * We could trigger writeback for this range (and wait
7161 * for it to complete) and then invalidate the pages for
7162 * this range (through invalidate_inode_pages2_range()),
7163 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007164 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007165 * triggered a readahead) on a page lock due to an
7166 * ordered dio extent we created before but did not have
7167 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007168 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007169 * ordered extent to complete while holding a lock on
7170 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007171 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007172 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007173 }
7174
Filipe Mananaade77022016-02-18 14:28:55 +00007175 if (ret)
7176 break;
7177
Josef Bacikeb838e72012-07-31 16:28:48 -04007178 cond_resched();
7179 }
7180
7181 return ret;
7182}
7183
Liu Bo6f9994d2017-01-31 07:50:22 -08007184/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007185static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7186 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007187 u64 block_len, u64 orig_block_len,
7188 u64 ram_bytes, int compress_type,
7189 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007190{
7191 struct extent_map_tree *em_tree;
7192 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007193 int ret;
7194
Liu Bo6f9994d2017-01-31 07:50:22 -08007195 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7196 type == BTRFS_ORDERED_COMPRESSED ||
7197 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007198 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007199
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007200 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007201 em = alloc_extent_map();
7202 if (!em)
7203 return ERR_PTR(-ENOMEM);
7204
7205 em->start = start;
7206 em->orig_start = orig_start;
7207 em->len = len;
7208 em->block_len = block_len;
7209 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007210 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007211 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007212 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007213 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007214 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007215 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007216 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007217 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7218 em->compress_type = compress_type;
7219 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007220
7221 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007222 btrfs_drop_extent_cache(inode, em->start,
7223 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007224 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007225 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007226 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007227 /*
7228 * The caller has taken lock_extent(), who could race with us
7229 * to add em?
7230 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007231 } while (ret == -EEXIST);
7232
7233 if (ret) {
7234 free_extent_map(em);
7235 return ERR_PTR(ret);
7236 }
7237
Liu Bo6f9994d2017-01-31 07:50:22 -08007238 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007239 return em;
7240}
7241
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007242
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007243static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007244 struct inode *inode,
7245 struct btrfs_dio_data *dio_data,
7246 u64 start, u64 len)
7247{
7248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7249 struct extent_map *em = *map;
7250 int ret = 0;
7251
7252 /*
7253 * We don't allocate a new extent in the following cases
7254 *
7255 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7256 * existing extent.
7257 * 2) The extent is marked as PREALLOC. We're good to go here and can
7258 * just use the extent.
7259 *
7260 */
7261 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7262 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7263 em->block_start != EXTENT_MAP_HOLE)) {
7264 int type;
7265 u64 block_start, orig_start, orig_block_len, ram_bytes;
7266
7267 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7268 type = BTRFS_ORDERED_PREALLOC;
7269 else
7270 type = BTRFS_ORDERED_NOCOW;
7271 len = min(len, em->len - (start - em->start));
7272 block_start = em->block_start + (start - em->start);
7273
7274 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007275 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007276 btrfs_inc_nocow_writers(fs_info, block_start)) {
7277 struct extent_map *em2;
7278
Nikolay Borisov64f54182020-06-03 08:55:31 +03007279 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007280 orig_start, block_start,
7281 len, orig_block_len,
7282 ram_bytes, type);
7283 btrfs_dec_nocow_writers(fs_info, block_start);
7284 if (type == BTRFS_ORDERED_PREALLOC) {
7285 free_extent_map(em);
7286 *map = em = em2;
7287 }
7288
7289 if (em2 && IS_ERR(em2)) {
7290 ret = PTR_ERR(em2);
7291 goto out;
7292 }
7293 /*
7294 * For inode marked NODATACOW or extent marked PREALLOC,
7295 * use the existing or preallocated extent, so does not
7296 * need to adjust btrfs_space_info's bytes_may_use.
7297 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007298 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007299 goto skip_cow;
7300 }
7301 }
7302
7303 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007304 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007305 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007306 if (IS_ERR(em)) {
7307 ret = PTR_ERR(em);
7308 goto out;
7309 }
7310
7311 len = min(len, em->len - (start - em->start));
7312
7313skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007314 /*
7315 * Need to update the i_size under the extent lock so buffered
7316 * readers will get the updated i_size when we unlock.
7317 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007318 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007319 i_size_write(inode, start + len);
7320
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007321 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007322out:
7323 return ret;
7324}
7325
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007326static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7327 loff_t length, unsigned int flags, struct iomap *iomap,
7328 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007329{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007330 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007331 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007332 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307333 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007334 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007335 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007336 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007337 u64 len = length;
7338 bool unlock_extents = false;
Josef Bacik0eb79292020-09-03 11:16:51 -04007339 bool sync = (current->journal_info == BTRFS_DIO_SYNC_STUB);
7340
7341 /*
7342 * We used current->journal_info here to see if we were sync, but
7343 * there's a lot of tests in the enospc machinery to not do flushing if
7344 * we have a journal_info set, so we need to clear this out and re-set
7345 * it in iomap_end.
7346 */
7347 ASSERT(current->journal_info == NULL ||
7348 current->journal_info == BTRFS_DIO_SYNC_STUB);
7349 current->journal_info = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007350
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007351 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007352 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007353
Josef Bacikc3298612012-08-03 16:49:19 -04007354 lockstart = start;
7355 lockend = start + len - 1;
7356
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007357 /*
7358 * The generic stuff only does filemap_write_and_wait_range, which
7359 * isn't enough if we've written compressed pages to this area, so we
7360 * need to flush the dirty pages again to make absolutely sure that any
7361 * outstanding dirty pages are on disk.
7362 */
7363 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7364 &BTRFS_I(inode)->runtime_flags)) {
7365 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7366 start + length - 1);
7367 if (ret)
7368 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007369 }
7370
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007371 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7372 if (!dio_data)
7373 return -ENOMEM;
7374
Josef Bacik0eb79292020-09-03 11:16:51 -04007375 dio_data->sync = sync;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007376 dio_data->length = length;
7377 if (write) {
7378 dio_data->reserve = round_up(length, fs_info->sectorsize);
7379 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7380 &dio_data->data_reserved,
7381 start, dio_data->reserve);
7382 if (ret) {
7383 extent_changeset_free(dio_data->data_reserved);
7384 kfree(dio_data);
7385 return ret;
7386 }
7387 }
7388 iomap->private = dio_data;
7389
7390
Josef Bacikeb838e72012-07-31 16:28:48 -04007391 /*
7392 * If this errors out it's because we couldn't invalidate pagecache for
7393 * this range and we need to fallback to buffered.
7394 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007395 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007396 ret = -ENOTBLK;
7397 goto err;
7398 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007399
Omar Sandoval39b07b52019-12-02 17:34:23 -08007400 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007401 if (IS_ERR(em)) {
7402 ret = PTR_ERR(em);
7403 goto unlock_err;
7404 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007405
7406 /*
7407 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7408 * io. INLINE is special, and we could probably kludge it in here, but
7409 * it's still buffered so for safety lets just fall back to the generic
7410 * buffered path.
7411 *
7412 * For COMPRESSED we _have_ to read the entire extent in so we can
7413 * decompress it, so there will be buffering required no matter what we
7414 * do, so go ahead and fallback to buffered.
7415 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007416 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007417 * to buffered IO. Don't blame me, this is the price we pay for using
7418 * the generic code.
7419 */
7420 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7421 em->block_start == EXTENT_MAP_INLINE) {
7422 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007423 ret = -ENOTBLK;
7424 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007425 }
7426
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007427 len = min(len, em->len - (start - em->start));
7428 if (write) {
7429 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7430 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007431 if (ret < 0)
7432 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007433 unlock_extents = true;
7434 /* Recalc len in case the new em is smaller than requested */
7435 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007436 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007437 /*
7438 * We need to unlock only the end area that we aren't using.
7439 * The rest is going to be unlocked by the endio routine.
7440 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007441 lockstart = start + len;
7442 if (lockstart < lockend)
7443 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007444 }
7445
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007446 if (unlock_extents)
7447 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7448 lockstart, lockend, &cached_state);
7449 else
7450 free_extent_state(cached_state);
7451
7452 /*
7453 * Translate extent map information to iomap.
7454 * We trim the extents (and move the addr) even though iomap code does
7455 * that, since we have locked only the parts we are performing I/O in.
7456 */
7457 if ((em->block_start == EXTENT_MAP_HOLE) ||
7458 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7459 iomap->addr = IOMAP_NULL_ADDR;
7460 iomap->type = IOMAP_HOLE;
7461 } else {
7462 iomap->addr = em->block_start + (start - em->start);
7463 iomap->type = IOMAP_MAPPED;
7464 }
7465 iomap->offset = start;
7466 iomap->bdev = fs_info->fs_devices->latest_bdev;
7467 iomap->length = len;
7468
Josef Bacik4b46fce2010-05-23 11:00:55 -04007469 free_extent_map(em);
7470
7471 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007472
7473unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007474 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7475 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007476err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007477 if (dio_data) {
7478 btrfs_delalloc_release_space(BTRFS_I(inode),
7479 dio_data->data_reserved, start,
7480 dio_data->reserve, true);
7481 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7482 extent_changeset_free(dio_data->data_reserved);
7483 kfree(dio_data);
7484 }
7485 return ret;
7486}
7487
7488static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7489 ssize_t written, unsigned int flags, struct iomap *iomap)
7490{
7491 int ret = 0;
7492 struct btrfs_dio_data *dio_data = iomap->private;
7493 size_t submitted = dio_data->submitted;
7494 const bool write = !!(flags & IOMAP_WRITE);
7495
7496 if (!write && (iomap->type == IOMAP_HOLE)) {
7497 /* If reading from a hole, unlock and return */
7498 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7499 goto out;
7500 }
7501
7502 if (submitted < length) {
7503 pos += submitted;
7504 length -= submitted;
7505 if (write)
7506 __endio_write_update_ordered(BTRFS_I(inode), pos,
7507 length, false);
7508 else
7509 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7510 pos + length - 1);
7511 ret = -ENOTBLK;
7512 }
7513
7514 if (write) {
7515 if (dio_data->reserve)
7516 btrfs_delalloc_release_space(BTRFS_I(inode),
7517 dio_data->data_reserved, pos,
7518 dio_data->reserve, true);
7519 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7520 extent_changeset_free(dio_data->data_reserved);
7521 }
7522out:
Josef Bacik0eb79292020-09-03 11:16:51 -04007523 /*
7524 * We're all done, we can re-set the current->journal_info now safely
7525 * for our endio.
7526 */
7527 if (dio_data->sync) {
7528 ASSERT(current->journal_info == NULL);
7529 current->journal_info = BTRFS_DIO_SYNC_STUB;
7530 }
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007531 kfree(dio_data);
7532 iomap->private = NULL;
7533
Josef Bacikeb838e72012-07-31 16:28:48 -04007534 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007535}
7536
Omar Sandoval769b4f22020-04-16 14:46:22 -07007537static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7538{
7539 /*
7540 * This implies a barrier so that stores to dio_bio->bi_status before
7541 * this and loads of dio_bio->bi_status after this are fully ordered.
7542 */
7543 if (!refcount_dec_and_test(&dip->refs))
7544 return;
7545
7546 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007547 __endio_write_update_ordered(BTRFS_I(dip->inode),
7548 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007549 dip->bytes,
7550 !dip->dio_bio->bi_status);
7551 } else {
7552 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7553 dip->logical_offset,
7554 dip->logical_offset + dip->bytes - 1);
7555 }
7556
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007557 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007558 kfree(dip);
7559}
7560
Omar Sandoval77d5d682020-04-16 14:46:25 -07007561static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7562 int mirror_num,
7563 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007564{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007565 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007566 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007567 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007568
Mike Christie37226b22016-06-05 14:31:52 -05007569 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007570
Omar Sandoval5c047a62020-04-16 14:46:24 -07007571 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007572 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007573 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007574
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007575 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007576 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007577 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007578 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007579 return ret;
7580}
7581
Omar Sandoval769b4f22020-04-16 14:46:22 -07007582static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7583 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007584 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007585{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007586 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7587 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007588 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007589 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7590 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007591 struct bio_vec bvec;
7592 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007593 u64 start = io_bio->logical;
7594 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007595 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007596
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007597 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7598 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307599
Liu Bo17347ce2017-05-15 15:33:27 -07007600 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7601 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007602 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007603 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007604 if (uptodate &&
7605 (!csum || !check_data_csum(inode, io_bio, icsum,
7606 bvec.bv_page, pgoff,
7607 start, sectorsize))) {
7608 clean_io_failure(fs_info, failure_tree, io_tree,
7609 start, bvec.bv_page,
7610 btrfs_ino(BTRFS_I(inode)),
7611 pgoff);
7612 } else {
7613 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007614
Omar Sandoval77d5d682020-04-16 14:46:25 -07007615 status = btrfs_submit_read_repair(inode,
7616 &io_bio->bio,
7617 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007618 bvec.bv_page, pgoff,
7619 start,
7620 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007621 io_bio->mirror_num,
7622 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007623 if (status)
7624 err = status;
7625 }
7626 start += sectorsize;
7627 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307628 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307629 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007630 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007631 return err;
7632}
7633
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007634static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007635 const u64 offset, const u64 bytes,
7636 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007637{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007638 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007639 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007640 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007641 u64 ordered_offset = offset;
7642 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007643 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007644
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007645 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007646 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007647 else
Qu Wenruo524272602017-03-08 10:25:52 +08007648 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007649
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007650 while (ordered_offset < offset + bytes) {
7651 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007652 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007653 &ordered_offset,
7654 ordered_bytes,
7655 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007656 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7657 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007658 btrfs_queue_work(wq, &ordered->work);
7659 }
7660 /*
7661 * If btrfs_dec_test_ordered_pending does not find any ordered
7662 * extent in the range, we can exit.
7663 */
7664 if (ordered_offset == last_offset)
7665 return;
7666 /*
7667 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007668 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007669 */
7670 if (ordered_offset < offset + bytes) {
7671 ordered_bytes = offset + bytes - ordered_offset;
7672 ordered = NULL;
7673 }
Chris Mason163cf092010-11-28 19:56:33 -05007674 }
Filipe Manana14543772015-11-24 16:23:54 +00007675}
7676
David Sterbad0ee3932018-03-08 14:35:48 +01007677static blk_status_t btrfs_submit_bio_start_direct_io(void *private_data,
David Sterbad0779292018-03-08 13:47:33 +01007678 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007679{
Josef Bacikc6100a42017-05-05 11:57:13 -04007680 struct inode *inode = private_data;
Johannes Thumshirnc965d642020-07-28 20:25:41 +09007681
7682 return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007683}
7684
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007685static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007686{
7687 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007688 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007689
Miao Xie8b110e32014-09-12 18:44:03 +08007690 if (err)
7691 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007692 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007693 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7694 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007695 (unsigned long long)bio->bi_iter.bi_sector,
7696 bio->bi_iter.bi_size, err);
7697
Omar Sandoval769b4f22020-04-16 14:46:22 -07007698 if (bio_op(bio) == REQ_OP_READ) {
7699 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007700 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007701 }
7702
Omar Sandoval769b4f22020-04-16 14:46:22 -07007703 if (err)
7704 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007705
Miao Xiee65e1532010-11-22 03:04:43 +00007706 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007707 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007708}
7709
David Sterbad0ee3932018-03-08 14:35:48 +01007710static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7711 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007712{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007713 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007714 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007715 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007716 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007717
Liu Bo4c274bc2017-11-01 17:19:27 -06007718 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007719 if (async_submit)
7720 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7721
Josef Bacik5fd02042012-05-02 14:00:54 -04007722 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007723 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007724 if (ret)
7725 goto err;
7726 }
Miao Xiee65e1532010-11-22 03:04:43 +00007727
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007728 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007729 goto map;
7730
7731 if (write && async_submit) {
Josef Bacikc6100a42017-05-05 11:57:13 -04007732 ret = btrfs_wq_submit_bio(fs_info, bio, 0, 0,
7733 file_offset, inode,
David Sterbae288c082018-07-18 17:36:24 +02007734 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007735 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007736 } else if (write) {
7737 /*
7738 * If we aren't doing async submit, calculate the csum of the
7739 * bio now.
7740 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007741 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007742 if (ret)
7743 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007744 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007745 u64 csum_offset;
7746
7747 csum_offset = file_offset - dip->logical_offset;
7748 csum_offset >>= inode->i_sb->s_blocksize_bits;
7749 csum_offset *= btrfs_super_csum_size(fs_info->super_copy);
7750 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007751 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007752map:
Chris Mason08635ba2019-07-10 12:28:14 -07007753 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007754err:
Miao Xiee65e1532010-11-22 03:04:43 +00007755 return ret;
7756}
7757
Omar Sandovalc36cac22020-04-16 14:46:13 -07007758/*
7759 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7760 * or ordered extents whether or not we submit any bios.
7761 */
7762static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7763 struct inode *inode,
7764 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007765{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007766 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007767 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7768 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007769 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007770
Omar Sandoval85879572020-04-16 14:46:21 -07007771 dip_size = sizeof(*dip);
7772 if (!write && csum) {
7773 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7774 const u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
7775 size_t nblocks;
7776
7777 nblocks = dio_bio->bi_iter.bi_size >> inode->i_sb->s_blocksize_bits;
7778 dip_size += csum_size * nblocks;
7779 }
7780
7781 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007782 if (!dip)
7783 return NULL;
7784
Omar Sandovalc36cac22020-04-16 14:46:13 -07007785 dip->inode = inode;
7786 dip->logical_offset = file_offset;
7787 dip->bytes = dio_bio->bi_iter.bi_size;
7788 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007789 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007790 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007791 return dip;
7792}
7793
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007794static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
7795 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007796{
7797 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007798 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007799 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007800 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7801 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007802 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007803 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007804 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007805 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007806 u64 submit_len;
7807 int clone_offset = 0;
7808 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307809 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007810 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007811 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007812 struct btrfs_dio_data *dio_data = iomap->private;
Miao Xiee65e1532010-11-22 03:04:43 +00007813
Omar Sandovalc36cac22020-04-16 14:46:13 -07007814 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7815 if (!dip) {
7816 if (!write) {
7817 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7818 file_offset + dio_bio->bi_iter.bi_size - 1);
7819 }
7820 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007821 bio_endio(dio_bio);
7822 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04007823 }
7824
Omar Sandoval85879572020-04-16 14:46:21 -07007825 if (!write && csum) {
7826 /*
7827 * Load the csums up front to reduce csum tree searches and
7828 * contention when submitting bios.
7829 */
7830 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7831 dip->csums);
7832 if (status != BLK_STS_OK)
7833 goto out_err;
7834 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007835
Omar Sandoval769b4f22020-04-16 14:46:22 -07007836 start_sector = dio_bio->bi_iter.bi_sector;
7837 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007838
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007839 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007840 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7841 start_sector << 9, submit_len,
7842 &geom);
7843 if (ret) {
7844 status = errno_to_blk_status(ret);
7845 goto out_err;
7846 }
7847 ASSERT(geom.len <= INT_MAX);
7848
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007849 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007850
Liu Bo725130b2017-05-16 09:51:39 -07007851 /*
7852 * This will never fail as it's passing GPF_NOFS and
7853 * the allocation is backed by btrfs_bioset.
7854 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007855 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007856 bio->bi_private = dip;
7857 bio->bi_end_io = btrfs_end_dio_bio;
7858 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007859
Liu Bo725130b2017-05-16 09:51:39 -07007860 ASSERT(submit_len >= clone_len);
7861 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007862
Liu Bo725130b2017-05-16 09:51:39 -07007863 /*
7864 * Increase the count before we submit the bio so we know
7865 * the end IO handler won't happen before we increase the
7866 * count. Otherwise, the dip might get freed before we're
7867 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07007868 *
7869 * We transfer the initial reference to the last bio, so we
7870 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07007871 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007872 if (submit_len > 0) {
7873 refcount_inc(&dip->refs);
7874 /*
7875 * If we are submitting more than one bio, submit them
7876 * all asynchronously. The exception is RAID 5 or 6, as
7877 * asynchronous checksums make it difficult to collect
7878 * full stripe writes.
7879 */
7880 if (!raid56)
7881 async_submit = 1;
7882 }
Miao Xiee65e1532010-11-22 03:04:43 +00007883
David Sterbad0ee3932018-03-08 14:35:48 +01007884 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07007885 async_submit);
7886 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07007887 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007888 if (submit_len > 0)
7889 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07007890 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00007891 }
Liu Bo725130b2017-05-16 09:51:39 -07007892
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007893 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07007894 clone_offset += clone_len;
7895 start_sector += clone_len >> 9;
7896 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007897 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007898 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00007899
Miao Xiee65e1532010-11-22 03:04:43 +00007900out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07007901 dip->dio_bio->bi_status = status;
7902 btrfs_dio_private_put(dip);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007903 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00007904}
7905
David Sterbaf4c48b42020-06-09 19:19:27 +02007906static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
7907 const struct iov_iter *iter, loff_t offset)
7908{
7909 int seg;
7910 int i;
7911 unsigned int blocksize_mask = fs_info->sectorsize - 1;
7912 ssize_t retval = -EINVAL;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007913
David Sterbaf4c48b42020-06-09 19:19:27 +02007914 if (offset & blocksize_mask)
7915 goto out;
7916
7917 if (iov_iter_alignment(iter) & blocksize_mask)
7918 goto out;
7919
7920 /* If this is a write we don't need to check anymore */
7921 if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
7922 return 0;
7923 /*
7924 * Check to make sure we don't have duplicate iov_base's in this
7925 * iovec, if so return EINVAL, otherwise we'll get csum errors
7926 * when reading back.
7927 */
7928 for (seg = 0; seg < iter->nr_segs; seg++) {
7929 for (i = seg + 1; i < iter->nr_segs; i++) {
7930 if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
7931 goto out;
7932 }
7933 }
7934 retval = 0;
7935out:
7936 return retval;
7937}
7938
Josef Bacik0eb79292020-09-03 11:16:51 -04007939static inline int btrfs_maybe_fsync_end_io(struct kiocb *iocb, ssize_t size,
7940 int error, unsigned flags)
7941{
7942 /*
7943 * Now if we're still in the context of our submitter we know we can't
7944 * safely run generic_write_sync(), so clear our flag here so that the
7945 * caller knows to follow up with a sync.
7946 */
7947 if (current->journal_info == BTRFS_DIO_SYNC_STUB) {
7948 current->journal_info = NULL;
7949 return error;
7950 }
7951
7952 if (error)
7953 return error;
7954
7955 if (size) {
7956 iocb->ki_flags |= IOCB_DSYNC;
7957 return generic_write_sync(iocb, size);
7958 }
7959
7960 return 0;
7961}
7962
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007963static const struct iomap_ops btrfs_dio_iomap_ops = {
7964 .iomap_begin = btrfs_dio_iomap_begin,
7965 .iomap_end = btrfs_dio_iomap_end,
7966};
7967
7968static const struct iomap_dio_ops btrfs_dio_ops = {
7969 .submit_io = btrfs_submit_direct,
7970};
7971
Josef Bacik0eb79292020-09-03 11:16:51 -04007972static const struct iomap_dio_ops btrfs_sync_dops = {
7973 .submit_io = btrfs_submit_direct,
7974 .end_io = btrfs_maybe_fsync_end_io,
7975};
7976
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007977ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
David Sterbaf4c48b42020-06-09 19:19:27 +02007978{
7979 struct file *file = iocb->ki_filp;
7980 struct inode *inode = file->f_mapping->host;
7981 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7982 struct extent_changeset *data_reserved = NULL;
7983 loff_t offset = iocb->ki_pos;
7984 size_t count = 0;
7985 bool relock = false;
7986 ssize_t ret;
7987
7988 if (check_direct_IO(fs_info, iter, offset))
7989 return 0;
7990
7991 count = iov_iter_count(iter);
7992 if (iov_iter_rw(iter) == WRITE) {
7993 /*
7994 * If the write DIO is beyond the EOF, we need update
7995 * the isize, but it is protected by i_mutex. So we can
7996 * not unlock the i_mutex at this case.
7997 */
7998 if (offset + count <= inode->i_size) {
7999 inode_unlock(inode);
8000 relock = true;
David Sterbaf4c48b42020-06-09 19:19:27 +02008001 }
8002 down_read(&BTRFS_I(inode)->dio_sem);
8003 }
8004
Josef Bacik0eb79292020-09-03 11:16:51 -04008005 /*
8006 * We have are actually a sync iocb, so we need our fancy endio to know
8007 * if we need to sync.
8008 */
8009 if (current->journal_info)
8010 ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops,
8011 &btrfs_sync_dops, is_sync_kiocb(iocb));
8012 else
8013 ret = iomap_dio_rw(iocb, iter, &btrfs_dio_iomap_ops,
8014 &btrfs_dio_ops, is_sync_kiocb(iocb));
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008015
8016 if (ret == -ENOTBLK)
8017 ret = 0;
8018
8019 if (iov_iter_rw(iter) == WRITE)
David Sterbaf4c48b42020-06-09 19:19:27 +02008020 up_read(&BTRFS_I(inode)->dio_sem);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008021
David Sterbaf4c48b42020-06-09 19:19:27 +02008022 if (relock)
8023 inode_lock(inode);
David Sterba55e20bd2020-06-09 19:56:06 +02008024
David Sterbaf4c48b42020-06-09 19:19:27 +02008025 extent_changeset_free(data_reserved);
8026 return ret;
8027}
Chris Mason16432982008-04-10 10:23:21 -04008028
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008029static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008030 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008031{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008032 int ret;
8033
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008034 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008035 if (ret)
8036 return ret;
8037
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008038 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008039}
8040
Chris Mason9ebefb182007-06-15 13:50:00 -04008041int btrfs_readpage(struct file *file, struct page *page)
8042{
David Sterba71ad38b2020-02-05 19:09:35 +01008043 return extent_read_full_page(page, btrfs_get_extent, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04008044}
Chris Mason1832a6d2007-12-21 16:27:21 -05008045
Chris Mason39279cc2007-06-12 06:35:45 -04008046static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8047{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008048 struct inode *inode = page->mapping->host;
8049 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008050
8051 if (current->flags & PF_MEMALLOC) {
8052 redirty_page_for_writepage(wbc, page);
8053 unlock_page(page);
8054 return 0;
8055 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008056
8057 /*
8058 * If we are under memory pressure we will call this directly from the
8059 * VM, we need to make sure we have the inode referenced for the ordered
8060 * extent. If not just return like we didn't do anything.
8061 */
8062 if (!igrab(inode)) {
8063 redirty_page_for_writepage(wbc, page);
8064 return AOP_WRITEPAGE_ACTIVATE;
8065 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008066 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008067 btrfs_add_delayed_iput(inode);
8068 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008069}
Chris Mason39279cc2007-06-12 06:35:45 -04008070
Eric Sandeen48a3b632013-04-25 20:41:01 +00008071static int btrfs_writepages(struct address_space *mapping,
8072 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008073{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008074 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008075}
8076
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008077static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008078{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008079 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008080}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008081
Chris Masone6dcd2d2008-07-17 12:53:50 -04008082static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008083{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008084 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008085 if (ret == 1)
8086 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008087 return ret;
8088}
Chris Mason39279cc2007-06-12 06:35:45 -04008089
Chris Masone6dcd2d2008-07-17 12:53:50 -04008090static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8091{
Chris Mason98509cf2008-09-11 15:51:43 -04008092 if (PageWriteback(page) || PageDirty(page))
8093 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008094 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008095}
8096
Roman Gushchinf8e66082020-03-04 16:57:35 -08008097#ifdef CONFIG_MIGRATION
8098static int btrfs_migratepage(struct address_space *mapping,
8099 struct page *newpage, struct page *page,
8100 enum migrate_mode mode)
8101{
8102 int ret;
8103
8104 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8105 if (ret != MIGRATEPAGE_SUCCESS)
8106 return ret;
8107
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008108 if (page_has_private(page))
8109 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008110
8111 if (PagePrivate2(page)) {
8112 ClearPagePrivate2(page);
8113 SetPagePrivate2(newpage);
8114 }
8115
8116 if (mode != MIGRATE_SYNC_NO_COPY)
8117 migrate_page_copy(newpage, page);
8118 else
8119 migrate_page_states(newpage, page);
8120 return MIGRATEPAGE_SUCCESS;
8121}
8122#endif
8123
Lukas Czernerd47992f2013-05-21 23:17:23 -04008124static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8125 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008126{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008127 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8128 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008129 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008130 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008131 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008132 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308133 u64 start;
8134 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008135 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Chris Masona52d9a82007-08-27 16:49:44 -04008136
Chris Mason8b62b722009-09-02 16:53:46 -04008137 /*
8138 * we have the page locked, so new writeback can't start,
8139 * and the dirty bit won't be cleared while we are here.
8140 *
8141 * Wait for IO on this page so that we can safely clear
8142 * the PagePrivate2 bit and do ordered accounting
8143 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008144 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008145
Chris Masone6dcd2d2008-07-17 12:53:50 -04008146 if (offset) {
8147 btrfs_releasepage(page, GFP_NOFS);
8148 return;
8149 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008150
8151 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008152 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308153again:
8154 start = page_start;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008155 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008156 if (ordered) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08008157 end = min(page_end,
8158 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008159 /*
8160 * IO on this page will never be started, so we need
8161 * to account for any ordered extents now
8162 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008163 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308164 clear_extent_bit(tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008165 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008166 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008167 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008168 /*
8169 * whoever cleared the private bit is responsible
8170 * for the finish_ordered_io
8171 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008172 if (TestClearPagePrivate2(page)) {
8173 struct btrfs_ordered_inode_tree *tree;
8174 u64 new_len;
8175
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008176 tree = &inode->ordered_tree;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008177
8178 spin_lock_irq(&tree->lock);
8179 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308180 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008181 if (new_len < ordered->truncated_len)
8182 ordered->truncated_len = new_len;
8183 spin_unlock_irq(&tree->lock);
8184
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008185 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8186 start,
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308187 end - start + 1, 1))
Josef Bacik77cef2e2013-08-29 13:57:21 -04008188 btrfs_finish_ordered_io(ordered);
Chris Mason8b62b722009-09-02 16:53:46 -04008189 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008190 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008191 if (!inode_evicting) {
8192 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308193 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008194 &cached_state);
8195 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308196
8197 start = end + 1;
8198 if (start < page_end)
8199 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008200 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008201
Qu Wenruob9d0b382015-09-29 10:35:16 +08008202 /*
8203 * Qgroup reserved space handler
8204 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008205 * 1) Already written to disk or ordered extent already submitted
8206 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8207 * Qgroup will be handled by its qgroup_record then.
8208 * btrfs_qgroup_free_data() call will do nothing here.
8209 *
8210 * 2) Not written to disk yet
8211 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8212 * bit of its io_tree, and free the qgroup reserved data space.
8213 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008214 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008215 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008216 if (!inode_evicting) {
Omar Sandovale1821632019-08-15 14:04:04 -07008217 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008218 EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
8219 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
David Sterbaae0f1622017-10-31 16:37:52 +01008220 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008221
8222 __btrfs_releasepage(page, GFP_NOFS);
8223 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008224
Chris Mason4a096752008-07-21 10:29:44 -04008225 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008226 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008227}
8228
Chris Mason9ebefb182007-06-15 13:50:00 -04008229/*
8230 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8231 * called from a page fault handler when a page is first dirtied. Hence we must
8232 * be careful to check for EOF conditions here. We set the page up correctly
8233 * for a written page which means we get ENOSPC checking when writing into
8234 * holes and correct delalloc and unwritten extent mapping on filesystems that
8235 * support these features.
8236 *
8237 * We are not allowed to take the i_mutex here so we have to play games to
8238 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008239 * truncate_setsize() writes the inode size before removing pages, once we have
8240 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008241 * beyond EOF, then the page is guaranteed safe against truncation until we
8242 * unlock the page.
8243 */
Souptick Joardera528a242018-06-06 19:54:44 +05308244vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008245{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008246 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008247 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008248 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008249 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8250 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008251 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008252 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008253 char *kaddr;
8254 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008255 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308256 vm_fault_t ret;
8257 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008258 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308259 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008260 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008261 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308262 u64 end;
8263
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008264 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008265
Jan Karab2b5ef52012-06-12 16:20:45 +02008266 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008267 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008268 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308269 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008270
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308271 /*
8272 * Reserving delalloc space after obtaining the page lock can lead to
8273 * deadlock. For example, if a dirty page is locked by this function
8274 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8275 * dirty page write out, then the btrfs_writepage() function could
8276 * end up waiting indefinitely to get a lock on the page currently
8277 * being processed by btrfs_page_mkwrite() function.
8278 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008279 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8280 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308281 if (!ret2) {
8282 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008283 reserved = 1;
8284 }
Souptick Joardera528a242018-06-06 19:54:44 +05308285 if (ret2) {
8286 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008287 if (reserved)
8288 goto out;
8289 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008290 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008291
Nick Piggin56a76f82009-03-31 15:23:23 -07008292 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008293again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008294 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008295 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008296
Chris Mason9ebefb182007-06-15 13:50:00 -04008297 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008298 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008299 /* page got truncated out from underneath us */
8300 goto out_unlock;
8301 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008302 wait_on_page_writeback(page);
8303
David Sterbaff13db42015-12-03 14:30:40 +01008304 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008305 set_page_extent_mapped(page);
8306
Chris Masoneb84ae02008-07-17 13:53:27 -04008307 /*
8308 * we can't set the delalloc bits if there are pending ordered
8309 * extents. Drop our locks and wait for them to finish
8310 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008311 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8312 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008313 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008314 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008315 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008316 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04008317 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008318 btrfs_put_ordered_extent(ordered);
8319 goto again;
8320 }
8321
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008322 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008323 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008324 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008325 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308326 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008327 btrfs_delalloc_release_space(BTRFS_I(inode),
8328 data_reserved, page_start,
8329 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308330 }
8331 }
8332
Josef Bacikfbf19082009-10-01 17:10:23 -04008333 /*
Liu Bo54160342016-12-13 12:15:19 -08008334 * page_mkwrite gets called when the page is firstly dirtied after it's
8335 * faulted in, but write(2) could also dirty a page and set delalloc
8336 * bits, thus in this case for space account reason, we still need to
8337 * clear any delalloc bits within this page range since we have to
8338 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008339 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308340 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008341 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8342 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008343
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008344 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008345 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308346 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008347 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008348 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008349 ret = VM_FAULT_SIGBUS;
8350 goto out_unlock;
8351 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008352
8353 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008354 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008355 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008356 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008357 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008358
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008359 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008360 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008361 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008362 flush_dcache_page(page);
8363 kunmap(page);
8364 }
Chris Mason247e7432008-07-17 12:53:51 -04008365 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008366 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008367 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008368
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008369 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008370 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008371 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008372
David Sterbae43bbe52017-12-12 21:43:52 +01008373 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008374
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008375 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8376 sb_end_pagefault(inode->i_sb);
8377 extent_changeset_free(data_reserved);
8378 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008379
8380out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008381 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008382out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008383 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008384 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008385 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008386out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008387 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008388 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008389 return ret;
8390}
8391
Filipe Manana213e8c52018-02-06 20:40:31 +00008392static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008393{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008394 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008395 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008396 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008397 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008398 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008399 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008400 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008401
Filipe Manana213e8c52018-02-06 20:40:31 +00008402 if (!skip_writeback) {
8403 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8404 (u64)-1);
8405 if (ret)
8406 return ret;
8407 }
Chris Mason39279cc2007-06-12 06:35:45 -04008408
Josef Bacikfcb80c22011-05-03 10:40:22 -04008409 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008410 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8411 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008412 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008413 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008414 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008415 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008416 * be free'd up by the truncate operation, but also have some slack
8417 * space reserved in case it uses space during the truncate (thank you
8418 * very much snapshotting).
8419 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008420 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008421 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008422 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008423 * doesn't end up using space reserved for updating the inode. We also
8424 * need to be able to stop the transaction and start a new one, which
8425 * means we need to be able to update the inode several times, and we
8426 * have no idea of knowing how many times that will be, so we can't just
8427 * reserve 1 item for the entirety of the operation, so that has to be
8428 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008429 *
8430 * So that leaves us with
8431 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008432 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008433 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008434 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008435 * updating the inode.
8436 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008437 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008438 if (!rsv)
8439 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008440 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008441 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008442
Josef Bacik907cbce2011-08-08 13:46:15 -04008443 /*
Josef Bacik07127182011-08-19 10:29:59 -04008444 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008445 * 1 for updating the inode.
8446 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008447 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008448 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008449 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008450 goto out;
8451 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008452
Josef Bacik907cbce2011-08-08 13:46:15 -04008453 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008454 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008455 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008456 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008457
Chris Mason5a3f23d2009-03-31 13:27:11 -04008458 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008459 * So if we truncate and then write and fsync we normally would just
8460 * write the extents that changed, which is a problem if we need to
8461 * first truncate that entire inode. So set this flag so we write out
8462 * all of the extents in the inode to the sync log so we're completely
8463 * safe.
8464 */
8465 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008466 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008467
Yan, Zheng80825102009-11-12 09:35:36 +00008468 while (1) {
8469 ret = btrfs_truncate_inode_items(trans, root, inode,
8470 inode->i_size,
8471 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008472 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008473 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008474 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008475
Yan, Zheng80825102009-11-12 09:35:36 +00008476 ret = btrfs_update_inode(trans, root, inode);
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008477 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008478 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008479
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008480 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008481 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008482
8483 trans = btrfs_start_transaction(root, 2);
8484 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008485 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008486 trans = NULL;
8487 break;
8488 }
8489
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008490 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008491 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008492 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008493 BUG_ON(ret); /* shouldn't happen */
8494 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008495 }
8496
Josef Bacikddfae632017-10-19 14:16:02 -04008497 /*
8498 * We can't call btrfs_truncate_block inside a trans handle as we could
8499 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8500 * we've truncated everything except the last little bit, and can do
8501 * btrfs_truncate_block and then update the disk_i_size.
8502 */
8503 if (ret == NEED_TRUNCATE_BLOCK) {
8504 btrfs_end_transaction(trans);
8505 btrfs_btree_balance_dirty(fs_info);
8506
8507 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8508 if (ret)
8509 goto out;
8510 trans = btrfs_start_transaction(root, 1);
8511 if (IS_ERR(trans)) {
8512 ret = PTR_ERR(trans);
8513 goto out;
8514 }
Josef Bacikd923afe2020-01-17 09:02:23 -05008515 btrfs_inode_safe_disk_i_size_write(inode, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008516 }
8517
Chris Mason917c16b2011-11-08 14:49:59 -05008518 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008519 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008520
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008521 trans->block_rsv = &fs_info->trans_block_rsv;
8522 ret2 = btrfs_update_inode(trans, root, inode);
8523 if (ret2 && !ret)
8524 ret = ret2;
8525
8526 ret2 = btrfs_end_transaction(trans);
8527 if (ret2 && !ret)
8528 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008529 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008530 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008531out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008532 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008533
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008534 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008535}
8536
Sven Wegener3b963622008-06-09 21:57:42 -04008537/*
Chris Masond352ac62008-09-29 15:18:18 -04008538 * create a new subvolume directory/inode (helper for the ioctl).
8539 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008540int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008541 struct btrfs_root *new_root,
8542 struct btrfs_root *parent_root,
8543 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008544{
Chris Mason39279cc2007-06-12 06:35:45 -04008545 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008546 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008547 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008548
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008549 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8550 new_dirid, new_dirid,
8551 S_IFDIR | (~current_umask() & S_IRWXUGO),
8552 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008553 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008554 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008555 inode->i_op = &btrfs_dir_inode_operations;
8556 inode->i_fop = &btrfs_dir_file_operations;
8557
Miklos Szeredibfe86842011-10-28 14:13:29 +02008558 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008559 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008560 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008561
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008562 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8563 if (err)
8564 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008565 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008566 new_root->root_key.objectid, err);
8567
Yan, Zheng76dda932009-09-21 16:00:26 -04008568 err = btrfs_update_inode(trans, new_root, inode);
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008569
Yan, Zheng76dda932009-09-21 16:00:26 -04008570 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008571 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008572}
8573
Chris Mason39279cc2007-06-12 06:35:45 -04008574struct inode *btrfs_alloc_inode(struct super_block *sb)
8575{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008576 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008577 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008578 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008579
David Sterba712e36c2017-10-31 17:08:27 +01008580 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008581 if (!ei)
8582 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008583
8584 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008585 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008586 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008587 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008588 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008589 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008590 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008591 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008592 ei->disk_i_size = 0;
8593 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008594 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008595 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008596 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008597 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008598 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008599 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008600
Josef Bacik9e0baf62011-07-15 15:16:44 +00008601 spin_lock_init(&ei->lock);
8602 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008603 if (sb->s_magic != BTRFS_TEST_MAGIC)
8604 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8605 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008606 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008607 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008608 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008609
Miao Xie16cdcec2011-04-22 18:12:22 +08008610 ei->delayed_node = NULL;
8611
chandan r9cc97d62012-07-04 12:48:07 +05308612 ei->i_otime.tv_sec = 0;
8613 ei->i_otime.tv_nsec = 0;
8614
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008615 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008616 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008617 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8618 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8619 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008620 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8621 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008622 ei->io_tree.track_uptodate = true;
8623 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008624 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008625 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008626 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008627 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008628 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008629 RB_CLEAR_NODE(&ei->rb_node);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01008630 init_rwsem(&ei->dio_sem);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008631
8632 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008633}
8634
Josef Bacikaaedb552013-10-11 14:44:09 -04008635#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8636void btrfs_test_destroy_inode(struct inode *inode)
8637{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008638 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008639 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8640}
8641#endif
8642
Al Viro26602ca2019-04-10 15:14:41 -04008643void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008644{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008645 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8646}
8647
Chris Mason39279cc2007-06-12 06:35:45 -04008648void btrfs_destroy_inode(struct inode *inode)
8649{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008650 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008651 struct btrfs_ordered_extent *ordered;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008652 struct btrfs_root *root = BTRFS_I(inode)->root;
8653
Al Virob3d9b7a2012-06-09 13:51:19 -04008654 WARN_ON(!hlist_empty(&inode->i_dentry));
Chris Mason39279cc2007-06-12 06:35:45 -04008655 WARN_ON(inode->i_data.nrpages);
Josef Bacik69fe2d72017-10-19 14:15:57 -04008656 WARN_ON(BTRFS_I(inode)->block_rsv.reserved);
8657 WARN_ON(BTRFS_I(inode)->block_rsv.size);
Josef Bacik9e0baf62011-07-15 15:16:44 +00008658 WARN_ON(BTRFS_I(inode)->outstanding_extents);
Josef Bacik7709cde2011-08-04 10:25:02 -04008659 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008660 WARN_ON(BTRFS_I(inode)->new_delalloc_bytes);
Josef Bacik7709cde2011-08-04 10:25:02 -04008661 WARN_ON(BTRFS_I(inode)->csum_bytes);
Wang Shilong47059d92014-07-03 18:22:07 +08008662 WARN_ON(BTRFS_I(inode)->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008663
Chris Mason5a3f23d2009-03-31 13:27:11 -04008664 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008665 * This can happen where we create an inode, but somebody else also
8666 * created the same inode and we need to destroy the one we already
8667 * created.
8668 */
8669 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008670 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008671
Chris Masond3977122009-01-05 21:25:51 -05008672 while (1) {
Nikolay Borisov6d072c82020-08-31 14:42:39 +03008673 ordered = btrfs_lookup_first_ordered_extent(BTRFS_I(inode),
8674 (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008675 if (!ordered)
8676 break;
8677 else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008678 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008679 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008680 ordered->file_offset, ordered->num_bytes);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008681 btrfs_remove_ordered_extent(inode, ordered);
8682 btrfs_put_ordered_extent(ordered);
8683 btrfs_put_ordered_extent(ordered);
8684 }
8685 }
Nikolay Borisovcfdd4592020-06-03 08:55:46 +03008686 btrfs_qgroup_check_reserved_leak(BTRFS_I(inode));
Nikolay Borisovb79b7242020-08-31 14:42:38 +03008687 inode_tree_del(BTRFS_I(inode));
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008688 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008689 btrfs_inode_clear_file_extent_range(BTRFS_I(inode), 0, (u64)-1);
Josef Bacik5c8fd992020-02-14 16:11:43 -05008690 btrfs_put_root(BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008691}
8692
Al Viro45321ac2010-06-07 13:43:19 -04008693int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008694{
8695 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008696
Naohiro Aota6379ef92013-06-06 09:56:34 +00008697 if (root == NULL)
8698 return 1;
8699
Liu Bofa6ac872013-02-20 14:10:23 +00008700 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008701 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008702 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008703 else
Al Viro45321ac2010-06-07 13:43:19 -04008704 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008705}
8706
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008707static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008708{
8709 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8710
8711 inode_init_once(&ei->vfs_inode);
8712}
8713
David Sterbae67c7182018-02-19 17:24:18 +01008714void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008715{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008716 /*
8717 * Make sure all delayed rcu free inodes are flushed before we
8718 * destroy cache.
8719 */
8720 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008721 kmem_cache_destroy(btrfs_inode_cachep);
8722 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008723 kmem_cache_destroy(btrfs_path_cachep);
8724 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008725 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008726}
8727
Liu Bof5c29bd2017-11-02 17:21:50 -06008728int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008729{
David Sterba837e1972012-09-07 03:00:48 -06008730 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008731 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008732 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8733 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008734 if (!btrfs_inode_cachep)
8735 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008736
David Sterba837e1972012-09-07 03:00:48 -06008737 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008738 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008739 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008740 if (!btrfs_trans_handle_cachep)
8741 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008742
David Sterba837e1972012-09-07 03:00:48 -06008743 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008744 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008745 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008746 if (!btrfs_path_cachep)
8747 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008748
David Sterba837e1972012-09-07 03:00:48 -06008749 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008750 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008751 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008752 if (!btrfs_free_space_cachep)
8753 goto fail;
8754
Christophe Leroy3acd4852019-08-21 15:05:55 +00008755 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8756 PAGE_SIZE, PAGE_SIZE,
8757 SLAB_RED_ZONE, NULL);
8758 if (!btrfs_free_space_bitmap_cachep)
8759 goto fail;
8760
Chris Mason39279cc2007-06-12 06:35:45 -04008761 return 0;
8762fail:
8763 btrfs_destroy_cachep();
8764 return -ENOMEM;
8765}
8766
David Howellsa528d352017-01-31 16:46:22 +00008767static int btrfs_getattr(const struct path *path, struct kstat *stat,
8768 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008769{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008770 u64 delalloc_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008771 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008772 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008773 u32 bi_flags = BTRFS_I(inode)->flags;
8774
8775 stat->result_mask |= STATX_BTIME;
8776 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8777 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8778 if (bi_flags & BTRFS_INODE_APPEND)
8779 stat->attributes |= STATX_ATTR_APPEND;
8780 if (bi_flags & BTRFS_INODE_COMPRESS)
8781 stat->attributes |= STATX_ATTR_COMPRESSED;
8782 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8783 stat->attributes |= STATX_ATTR_IMMUTABLE;
8784 if (bi_flags & BTRFS_INODE_NODUMP)
8785 stat->attributes |= STATX_ATTR_NODUMP;
8786
8787 stat->attributes_mask |= (STATX_ATTR_APPEND |
8788 STATX_ATTR_COMPRESSED |
8789 STATX_ATTR_IMMUTABLE |
8790 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008791
Chris Mason39279cc2007-06-12 06:35:45 -04008792 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008793 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008794
8795 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008796 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008797 spin_unlock(&BTRFS_I(inode)->lock);
David Sterbafadc0d82011-11-20 07:33:38 -05008798 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008799 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008800 return 0;
8801}
8802
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008803static int btrfs_rename_exchange(struct inode *old_dir,
8804 struct dentry *old_dentry,
8805 struct inode *new_dir,
8806 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008807{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008808 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008809 struct btrfs_trans_handle *trans;
8810 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008811 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008812 struct inode *new_inode = new_dentry->d_inode;
8813 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008814 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008815 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8816 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008817 u64 old_idx = 0;
8818 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008819 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01008820 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008821 bool root_log_pinned = false;
8822 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008823
8824 /* we only allow rename subvolume link between subvolumes */
8825 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8826 return -EXDEV;
8827
8828 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008829 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8830 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008831 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008832
8833 /*
8834 * We want to reserve the absolute worst case amount of items. So if
8835 * both inodes are subvols and we need to unlink them then that would
8836 * require 4 item modifications, but if they are both normal inodes it
8837 * would require 5 item modifications, so we'll assume their normal
8838 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8839 * should cover the worst case number of items we'll modify.
8840 */
8841 trans = btrfs_start_transaction(root, 12);
8842 if (IS_ERR(trans)) {
8843 ret = PTR_ERR(trans);
8844 goto out_notrans;
8845 }
8846
Josef Bacik3e174092019-11-15 15:43:06 -05008847 if (dest != root)
8848 btrfs_record_root_in_trans(trans, dest);
8849
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008850 /*
8851 * We need to find a free sequence number both in the source and
8852 * in the destination directory for the exchange.
8853 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008854 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008855 if (ret)
8856 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008857 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008858 if (ret)
8859 goto out_fail;
8860
8861 BTRFS_I(old_inode)->dir_index = 0ULL;
8862 BTRFS_I(new_inode)->dir_index = 0ULL;
8863
8864 /* Reference for the source. */
8865 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8866 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008867 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008868 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008869 btrfs_pin_log_trans(root);
8870 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008871 ret = btrfs_insert_inode_ref(trans, dest,
8872 new_dentry->d_name.name,
8873 new_dentry->d_name.len,
8874 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008875 btrfs_ino(BTRFS_I(new_dir)),
8876 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008877 if (ret)
8878 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008879 }
8880
8881 /* And now for the dest. */
8882 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8883 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008884 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008885 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008886 btrfs_pin_log_trans(dest);
8887 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008888 ret = btrfs_insert_inode_ref(trans, root,
8889 old_dentry->d_name.name,
8890 old_dentry->d_name.len,
8891 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008892 btrfs_ino(BTRFS_I(old_dir)),
8893 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008894 if (ret)
8895 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008896 }
8897
8898 /* Update inode version and ctime/mtime. */
8899 inode_inc_iversion(old_dir);
8900 inode_inc_iversion(new_dir);
8901 inode_inc_iversion(old_inode);
8902 inode_inc_iversion(new_inode);
8903 old_dir->i_ctime = old_dir->i_mtime = ctime;
8904 new_dir->i_ctime = new_dir->i_mtime = ctime;
8905 old_inode->i_ctime = ctime;
8906 new_inode->i_ctime = ctime;
8907
8908 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008909 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8910 BTRFS_I(old_inode), 1);
8911 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8912 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008913 }
8914
8915 /* src is a subvolume */
8916 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008917 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008918 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008919 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8920 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008921 old_dentry->d_name.name,
8922 old_dentry->d_name.len);
8923 if (!ret)
8924 ret = btrfs_update_inode(trans, root, old_inode);
8925 }
8926 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008927 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008928 goto out_fail;
8929 }
8930
8931 /* dest is a subvolume */
8932 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008933 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008934 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008935 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8936 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008937 new_dentry->d_name.name,
8938 new_dentry->d_name.len);
8939 if (!ret)
8940 ret = btrfs_update_inode(trans, dest, new_inode);
8941 }
8942 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008943 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008944 goto out_fail;
8945 }
8946
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008947 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008948 new_dentry->d_name.name,
8949 new_dentry->d_name.len, 0, old_idx);
8950 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008951 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008952 goto out_fail;
8953 }
8954
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008955 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008956 old_dentry->d_name.name,
8957 old_dentry->d_name.len, 0, new_idx);
8958 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008959 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008960 goto out_fail;
8961 }
8962
8963 if (old_inode->i_nlink == 1)
8964 BTRFS_I(old_inode)->dir_index = old_idx;
8965 if (new_inode->i_nlink == 1)
8966 BTRFS_I(new_inode)->dir_index = new_idx;
8967
Filipe Manana86e8aa02016-05-05 02:02:27 +01008968 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01008969 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
8970 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008971 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008972 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008973 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01008974 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01008975 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
8976 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008977 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008978 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008979 }
8980out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01008981 /*
8982 * If we have pinned a log and an error happened, we unpin tasks
8983 * trying to sync the log and force them to fallback to a transaction
8984 * commit if the log currently contains any of the inodes involved in
8985 * this rename operation (to ensure we do not persist a log with an
8986 * inconsistent state for any of these inodes or leading to any
8987 * inconsistencies when replayed). If the transaction was aborted, the
8988 * abortion reason is propagated to userspace when attempting to commit
8989 * the transaction. If the log does not contain any of these inodes, we
8990 * allow the tasks to sync it.
8991 */
8992 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008993 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
8994 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
8995 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01008996 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02008997 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01008998 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01008999
9000 if (root_log_pinned) {
9001 btrfs_end_log_trans(root);
9002 root_log_pinned = false;
9003 }
9004 if (dest_log_pinned) {
9005 btrfs_end_log_trans(dest);
9006 dest_log_pinned = false;
9007 }
9008 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009009 ret2 = btrfs_end_transaction(trans);
9010 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009011out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009012 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9013 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009014 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009015
9016 return ret;
9017}
9018
9019static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9020 struct btrfs_root *root,
9021 struct inode *dir,
9022 struct dentry *dentry)
9023{
9024 int ret;
9025 struct inode *inode;
9026 u64 objectid;
9027 u64 index;
9028
9029 ret = btrfs_find_free_ino(root, &objectid);
9030 if (ret)
9031 return ret;
9032
9033 inode = btrfs_new_inode(trans, root, dir,
9034 dentry->d_name.name,
9035 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009036 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009037 objectid,
9038 S_IFCHR | WHITEOUT_MODE,
9039 &index);
9040
9041 if (IS_ERR(inode)) {
9042 ret = PTR_ERR(inode);
9043 return ret;
9044 }
9045
9046 inode->i_op = &btrfs_special_inode_operations;
9047 init_special_inode(inode, inode->i_mode,
9048 WHITEOUT_DEV);
9049
9050 ret = btrfs_init_inode_security(trans, inode, dir,
9051 &dentry->d_name);
9052 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009053 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009054
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009055 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9056 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009057 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009058 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009059
9060 ret = btrfs_update_inode(trans, root, inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009061out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009062 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009063 if (ret)
9064 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009065 iput(inode);
9066
Filipe Mananac9901612016-05-05 01:41:57 +01009067 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009068}
9069
Chris Mason39279cc2007-06-12 06:35:45 -04009070static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009071 struct inode *new_dir, struct dentry *new_dentry,
9072 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009073{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009074 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009075 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009076 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009077 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9078 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009079 struct inode *new_inode = d_inode(new_dentry);
9080 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009081 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009082 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009083 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009084 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009085 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009086
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009087 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009088 return -EPERM;
9089
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009090 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009091 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009092 return -EXDEV;
9093
Li Zefan33345d012011-04-20 10:31:50 +08009094 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009095 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009096 return -ENOTEMPTY;
9097
Chris Mason39279cc2007-06-12 06:35:45 -04009098 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009099 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009100 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009101
9102
9103 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009104 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009105 new_dentry->d_name.name,
9106 new_dentry->d_name.len);
9107
9108 if (ret) {
9109 if (ret == -EEXIST) {
9110 /* we shouldn't get
9111 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309112 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009113 return ret;
9114 }
9115 } else {
9116 /* maybe -EOVERFLOW */
9117 return ret;
9118 }
9119 }
9120 ret = 0;
9121
Chris Mason5a3f23d2009-03-31 13:27:11 -04009122 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009123 * we're using rename to replace one file with another. Start IO on it
9124 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009125 */
Chris Mason8d875f92014-08-12 10:47:42 -07009126 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009127 filemap_flush(old_inode->i_mapping);
9128
Yan, Zheng76dda932009-09-21 16:00:26 -04009129 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009130 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009131 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009132 /*
9133 * We want to reserve the absolute worst case amount of items. So if
9134 * both inodes are subvols and we need to unlink them then that would
9135 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009136 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009137 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9138 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009139 * If our rename has the whiteout flag, we need more 5 units for the
9140 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9141 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009142 */
Filipe Manana5062af32016-05-05 10:26:26 +01009143 trans_num_items = 11;
9144 if (flags & RENAME_WHITEOUT)
9145 trans_num_items += 5;
9146 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009147 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009148 ret = PTR_ERR(trans);
9149 goto out_notrans;
9150 }
Chris Mason5f39d392007-10-15 16:14:19 -04009151
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009152 if (dest != root)
9153 btrfs_record_root_in_trans(trans, dest);
9154
Nikolay Borisov877574e2017-02-20 13:50:33 +02009155 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009156 if (ret)
9157 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009158
Miao Xie67de1172013-12-26 13:07:06 +08009159 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009160 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009161 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009162 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009163 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009164 btrfs_pin_log_trans(root);
9165 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009166 ret = btrfs_insert_inode_ref(trans, dest,
9167 new_dentry->d_name.name,
9168 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009169 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009170 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009171 if (ret)
9172 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009173 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009174
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009175 inode_inc_iversion(old_dir);
9176 inode_inc_iversion(new_dir);
9177 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009178 old_dir->i_ctime = old_dir->i_mtime =
9179 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009180 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009181
Chris Mason12fcfd22009-03-24 10:24:20 -04009182 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009183 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9184 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009185
Li Zefan33345d012011-04-20 10:31:50 +08009186 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009187 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009188 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009189 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9190 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009191 old_dentry->d_name.name,
9192 old_dentry->d_name.len);
9193 if (!ret)
9194 ret = btrfs_update_inode(trans, root, old_inode);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009195 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009196 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009197 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009198 goto out_fail;
9199 }
Chris Mason39279cc2007-06-12 06:35:45 -04009200
9201 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009202 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009203 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009204 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009205 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009206 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009207 BUG_ON(new_inode->i_nlink == 0);
9208 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009209 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9210 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009211 new_dentry->d_name.name,
9212 new_dentry->d_name.len);
9213 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009214 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009215 ret = btrfs_orphan_add(trans,
9216 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009217 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009218 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009219 goto out_fail;
9220 }
Chris Mason39279cc2007-06-12 06:35:45 -04009221 }
Josef Bacikaec74772008-07-24 12:12:38 -04009222
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009223 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009224 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009225 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009226 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009227 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009228 goto out_fail;
9229 }
Chris Mason39279cc2007-06-12 06:35:45 -04009230
Miao Xie67de1172013-12-26 13:07:06 +08009231 if (old_inode->i_nlink == 1)
9232 BTRFS_I(old_inode)->dir_index = index;
9233
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009234 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009235 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9236 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009237 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009238 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009239 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009240
9241 if (flags & RENAME_WHITEOUT) {
9242 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9243 old_dentry);
9244
9245 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009246 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009247 goto out_fail;
9248 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009249 }
Chris Mason39279cc2007-06-12 06:35:45 -04009250out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009251 /*
9252 * If we have pinned the log and an error happened, we unpin tasks
9253 * trying to sync the log and force them to fallback to a transaction
9254 * commit if the log currently contains any of the inodes involved in
9255 * this rename operation (to ensure we do not persist a log with an
9256 * inconsistent state for any of these inodes or leading to any
9257 * inconsistencies when replayed). If the transaction was aborted, the
9258 * abortion reason is propagated to userspace when attempting to commit
9259 * the transaction. If the log does not contain any of these inodes, we
9260 * allow the tasks to sync it.
9261 */
9262 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009263 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9264 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9265 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009266 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009267 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009268 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009269
9270 btrfs_end_log_trans(root);
9271 log_pinned = false;
9272 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009273 ret2 = btrfs_end_transaction(trans);
9274 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009275out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009276 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009277 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009278
Chris Mason39279cc2007-06-12 06:35:45 -04009279 return ret;
9280}
9281
Miklos Szeredi80ace852014-07-23 15:15:32 +02009282static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9283 struct inode *new_dir, struct dentry *new_dentry,
9284 unsigned int flags)
9285{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009286 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009287 return -EINVAL;
9288
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009289 if (flags & RENAME_EXCHANGE)
9290 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9291 new_dentry);
9292
9293 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009294}
9295
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009296struct btrfs_delalloc_work {
9297 struct inode *inode;
9298 struct completion completion;
9299 struct list_head list;
9300 struct btrfs_work work;
9301};
9302
Miao Xie8ccf6f192012-10-25 09:28:04 +00009303static void btrfs_run_delalloc_work(struct btrfs_work *work)
9304{
9305 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009306 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009307
9308 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9309 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009310 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009311 filemap_flush(inode->i_mapping);
9312 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9313 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009314 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009315
Nikolay Borisov076da912018-04-23 10:54:16 +03009316 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009317 complete(&delalloc_work->completion);
9318}
9319
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009320static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009321{
9322 struct btrfs_delalloc_work *work;
9323
David Sterba100d5702015-12-08 14:39:32 +01009324 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009325 if (!work)
9326 return NULL;
9327
9328 init_completion(&work->completion);
9329 INIT_LIST_HEAD(&work->list);
9330 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009331 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009332
9333 return work;
9334}
9335
Chris Masond352ac62008-09-29 15:18:18 -04009336/*
9337 * some fairly slow code that needs optimization. This walks the list
9338 * of all the inodes with pending delalloc and forces them to disk.
9339 */
Josef Bacikb4912132020-07-21 10:22:12 -04009340static int start_delalloc_inodes(struct btrfs_root *root, u64 *nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009341{
Chris Masonea8c2812008-08-04 23:17:27 -04009342 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009343 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009344 struct btrfs_delalloc_work *work, *next;
9345 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009346 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009347 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009348
Miao Xie8ccf6f192012-10-25 09:28:04 +00009349 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009350 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009351
Miao Xie573bfb72014-03-06 13:55:03 +08009352 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009353 spin_lock(&root->delalloc_lock);
9354 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009355 while (!list_empty(&splice)) {
9356 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009357 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009358
Miao Xieeb73c1b2013-05-15 07:48:22 +00009359 list_move_tail(&binode->delalloc_inodes,
9360 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009361 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009362 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009363 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009364 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009365 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009366 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009367
Ethan Lien3cd24c62018-11-01 14:49:03 +08009368 if (snapshot)
9369 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9370 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009371 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009372 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009373 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009374 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009375 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009376 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009377 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009378 btrfs_queue_work(root->fs_info->flush_workers,
9379 &work->work);
Josef Bacikb4912132020-07-21 10:22:12 -04009380 if (*nr != U64_MAX) {
9381 (*nr)--;
9382 if (*nr == 0)
9383 goto out;
9384 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009385 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009386 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009387 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009388 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009389
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009390out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009391 list_for_each_entry_safe(work, next, &works, list) {
9392 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009393 wait_for_completion(&work->completion);
9394 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009395 }
9396
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009397 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009398 spin_lock(&root->delalloc_lock);
9399 list_splice_tail(&splice, &root->delalloc_inodes);
9400 spin_unlock(&root->delalloc_lock);
9401 }
Miao Xie573bfb72014-03-06 13:55:03 +08009402 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009403 return ret;
9404}
9405
Ethan Lien3cd24c62018-11-01 14:49:03 +08009406int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009407{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009408 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb4912132020-07-21 10:22:12 -04009409 u64 nr = U64_MAX;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009410
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009411 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009412 return -EROFS;
9413
Josef Bacikb4912132020-07-21 10:22:12 -04009414 return start_delalloc_inodes(root, &nr, true);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009415}
9416
Josef Bacikb4912132020-07-21 10:22:12 -04009417int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009418{
9419 struct btrfs_root *root;
9420 struct list_head splice;
9421 int ret;
9422
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009423 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009424 return -EROFS;
9425
9426 INIT_LIST_HEAD(&splice);
9427
Miao Xie573bfb72014-03-06 13:55:03 +08009428 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009429 spin_lock(&fs_info->delalloc_root_lock);
9430 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009431 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009432 root = list_first_entry(&splice, struct btrfs_root,
9433 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009434 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009435 BUG_ON(!root);
9436 list_move_tail(&root->delalloc_root,
9437 &fs_info->delalloc_roots);
9438 spin_unlock(&fs_info->delalloc_root_lock);
9439
Josef Bacikb4912132020-07-21 10:22:12 -04009440 ret = start_delalloc_inodes(root, &nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009441 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009442 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009443 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009444 spin_lock(&fs_info->delalloc_root_lock);
9445 }
9446 spin_unlock(&fs_info->delalloc_root_lock);
9447
Miao Xie6c255e62014-03-06 13:55:01 +08009448 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009449out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009450 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009451 spin_lock(&fs_info->delalloc_root_lock);
9452 list_splice_tail(&splice, &fs_info->delalloc_roots);
9453 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009454 }
Miao Xie573bfb72014-03-06 13:55:03 +08009455 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009456 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009457}
9458
Chris Mason39279cc2007-06-12 06:35:45 -04009459static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9460 const char *symname)
9461{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009462 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009463 struct btrfs_trans_handle *trans;
9464 struct btrfs_root *root = BTRFS_I(dir)->root;
9465 struct btrfs_path *path;
9466 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009467 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009468 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009469 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309470 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009471 int name_len;
9472 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009473 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009474 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009475 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009476
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009477 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009478 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009479 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009480
Josef Bacik9ed74f22009-09-11 16:12:44 -04009481 /*
9482 * 2 items for inode item and ref
9483 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009484 * 1 item for updating parent inode item
9485 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009486 * 1 item for xattr if selinux is on
9487 */
Filipe Manana9269d122015-12-31 18:16:29 +00009488 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009489 if (IS_ERR(trans))
9490 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009491
Li Zefan581bb052011-04-20 10:06:11 +08009492 err = btrfs_find_free_ino(root, &objectid);
9493 if (err)
9494 goto out_unlock;
9495
Josef Bacikaec74772008-07-24 12:12:38 -04009496 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009497 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9498 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009499 if (IS_ERR(inode)) {
9500 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009501 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009502 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009503 }
Chris Mason39279cc2007-06-12 06:35:45 -04009504
Casey Schauflerad19db72011-12-15 10:09:07 -05009505 /*
9506 * If the active LSM wants to access the inode during
9507 * d_instantiate it needs these. Smack checks to see
9508 * if the filesystem supports xattrs by looking at the
9509 * ops vector.
9510 */
9511 inode->i_fop = &btrfs_file_operations;
9512 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009513 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009514 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9515
9516 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9517 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009518 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009519
Chris Mason39279cc2007-06-12 06:35:45 -04009520 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009521 if (!path) {
9522 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009523 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009524 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009525 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009526 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009527 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009528 datasize = btrfs_file_extent_calc_inline_size(name_len);
9529 err = btrfs_insert_empty_item(trans, root, path, &key,
9530 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009531 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009532 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009533 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009534 }
Chris Mason5f39d392007-10-15 16:14:19 -04009535 leaf = path->nodes[0];
9536 ei = btrfs_item_ptr(leaf, path->slots[0],
9537 struct btrfs_file_extent_item);
9538 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9539 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009540 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009541 btrfs_set_file_extent_encryption(leaf, ei, 0);
9542 btrfs_set_file_extent_compression(leaf, ei, 0);
9543 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9544 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9545
Chris Mason39279cc2007-06-12 06:35:45 -04009546 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009547 write_extent_buffer(leaf, symname, ptr, name_len);
9548 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009549 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009550
Chris Mason39279cc2007-06-12 06:35:45 -04009551 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009552 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009553 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009554 btrfs_i_size_write(BTRFS_I(inode), name_len);
Chris Mason54aa1f42007-06-22 14:16:25 -04009555 err = btrfs_update_inode(trans, root, inode);
Filipe Mananad50866d2015-12-31 18:08:24 +00009556 /*
9557 * Last step, add directory indexes for our symlink inode. This is the
9558 * last step to avoid extra cleanup of these indexes if an error happens
9559 * elsewhere above.
9560 */
9561 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009562 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9563 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009564 if (err)
9565 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009566
Al Viro1e2e5472018-05-04 08:23:01 -04009567 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009568
9569out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009570 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009571 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009572 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009573 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009574 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009575 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009576 return err;
9577}
Chris Mason16432982008-04-10 10:23:21 -04009578
Filipe Manana8fccebf2020-09-08 11:27:20 +01009579static struct btrfs_trans_handle *insert_prealloc_file_extent(
9580 struct btrfs_trans_handle *trans_in,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009581 struct inode *inode, struct btrfs_key *ins,
9582 u64 file_offset)
9583{
9584 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009585 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009586 struct btrfs_trans_handle *trans = trans_in;
9587 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009588 u64 start = ins->objectid;
9589 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009590 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009591
9592 memset(&stack_fi, 0, sizeof(stack_fi));
9593
9594 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9595 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9596 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9597 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9598 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9599 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9600 /* Encryption and other encoding is reserved and all 0 */
9601
Nikolay Borisov72b7d152020-06-03 08:55:18 +03009602 ret = btrfs_qgroup_release_data(BTRFS_I(inode), file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009603 if (ret < 0)
Filipe Manana8fccebf2020-09-08 11:27:20 +01009604 return ERR_PTR(ret);
9605
9606 if (trans) {
9607 ret = insert_reserved_file_extent(trans, BTRFS_I(inode),
9608 file_offset, &stack_fi, ret);
9609 if (ret)
9610 return ERR_PTR(ret);
9611 return trans;
9612 }
9613
9614 extent_info.disk_offset = start;
9615 extent_info.disk_len = len;
9616 extent_info.data_offset = 0;
9617 extent_info.data_len = len;
9618 extent_info.file_offset = file_offset;
9619 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009620 extent_info.is_new_extent = true;
9621 extent_info.qgroup_reserved = ret;
9622 extent_info.insertions = 0;
9623
9624 path = btrfs_alloc_path();
9625 if (!path)
9626 return ERR_PTR(-ENOMEM);
9627
9628 ret = btrfs_punch_hole_range(inode, path, file_offset,
9629 file_offset + len - 1, &extent_info,
9630 &trans);
9631 btrfs_free_path(path);
9632 if (ret)
9633 return ERR_PTR(ret);
9634
9635 return trans;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009636}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009637
Josef Bacik0af3d002010-06-21 14:48:16 -04009638static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9639 u64 start, u64 num_bytes, u64 min_size,
9640 loff_t actual_len, u64 *alloc_hint,
9641 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009642{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009643 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009644 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9645 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009646 struct btrfs_root *root = BTRFS_I(inode)->root;
9647 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009648 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009649 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009650 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009651 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009652 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009653 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009654 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009655 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009656
Josef Bacik0af3d002010-06-21 14:48:16 -04009657 if (trans)
9658 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009659 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009660 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009661 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009662 /*
9663 * If we are severely fragmented we could end up with really
9664 * small allocations, so if the allocator is returning small
9665 * chunks lets make its job easier by only searching for those
9666 * sized chunks.
9667 */
9668 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009669 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9670 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009671 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -04009672 break;
Josef Bacikb778cf92020-02-13 10:47:31 -05009673
9674 /*
9675 * We've reserved this space, and thus converted it from
9676 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9677 * from here on out we will only need to clear our reservation
9678 * for the remaining unreserved area, so advance our
9679 * clear_offset by our extent size.
9680 */
9681 clear_offset += ins.offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009682 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009683
Josef Bacik0b670dc2015-09-23 17:11:16 -04009684 last_alloc = ins.offset;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009685 trans = insert_prealloc_file_extent(trans, inode, &ins, cur_offset);
9686 if (IS_ERR(trans)) {
9687 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009688 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009689 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009690 break;
9691 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009692
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009693 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009694 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009695
Josef Bacik5dc562c2012-08-17 13:14:17 -04009696 em = alloc_extent_map();
9697 if (!em) {
9698 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9699 &BTRFS_I(inode)->runtime_flags);
9700 goto next;
9701 }
9702
9703 em->start = cur_offset;
9704 em->orig_start = cur_offset;
9705 em->len = ins.offset;
9706 em->block_start = ins.objectid;
9707 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009708 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009709 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009710 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9711 em->generation = trans->transid;
9712
9713 while (1) {
9714 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009715 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009716 write_unlock(&em_tree->lock);
9717 if (ret != -EEXIST)
9718 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009719 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009720 cur_offset + ins.offset - 1,
9721 0);
9722 }
9723 free_extent_map(em);
9724next:
Yan Zhengd899e052008-10-30 14:25:28 -04009725 num_bytes -= ins.offset;
9726 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009727 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009728
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009729 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009730 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009731 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009732 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009733 (actual_len > inode->i_size) &&
9734 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009735 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009736 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009737 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009738 i_size = cur_offset;
9739 i_size_write(inode, i_size);
Josef Bacikd923afe2020-01-17 09:02:23 -05009740 btrfs_inode_safe_disk_i_size_write(inode, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009741 }
9742
Yan Zhengd899e052008-10-30 14:25:28 -04009743 ret = btrfs_update_inode(trans, root, inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009744
9745 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009746 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009747 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009748 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009749 break;
9750 }
Yan Zhengd899e052008-10-30 14:25:28 -04009751
Filipe Manana8fccebf2020-09-08 11:27:20 +01009752 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009753 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009754 trans = NULL;
9755 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009756 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009757 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009758 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009759 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009760 return ret;
9761}
9762
Josef Bacik0af3d002010-06-21 14:48:16 -04009763int btrfs_prealloc_file_range(struct inode *inode, int mode,
9764 u64 start, u64 num_bytes, u64 min_size,
9765 loff_t actual_len, u64 *alloc_hint)
9766{
9767 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9768 min_size, actual_len, alloc_hint,
9769 NULL);
9770}
9771
9772int btrfs_prealloc_file_range_trans(struct inode *inode,
9773 struct btrfs_trans_handle *trans, int mode,
9774 u64 start, u64 num_bytes, u64 min_size,
9775 loff_t actual_len, u64 *alloc_hint)
9776{
9777 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9778 min_size, actual_len, alloc_hint, trans);
9779}
9780
Chris Masone6dcd2d2008-07-17 12:53:50 -04009781static int btrfs_set_page_dirty(struct page *page)
9782{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009783 return __set_page_dirty_nobuffers(page);
9784}
9785
Al Viro10556cb22011-06-20 19:28:19 -04009786static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009787{
Li Zefanb83cc962010-12-20 16:04:08 +08009788 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009789 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009790
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009791 if (mask & MAY_WRITE &&
9792 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9793 if (btrfs_root_readonly(root))
9794 return -EROFS;
9795 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9796 return -EACCES;
9797 }
Al Viro2830ba72011-06-20 19:16:29 -04009798 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009799}
Chris Mason39279cc2007-06-12 06:35:45 -04009800
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009801static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9802{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009803 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009804 struct btrfs_trans_handle *trans;
9805 struct btrfs_root *root = BTRFS_I(dir)->root;
9806 struct inode *inode = NULL;
9807 u64 objectid;
9808 u64 index;
9809 int ret = 0;
9810
9811 /*
9812 * 5 units required for adding orphan entry
9813 */
9814 trans = btrfs_start_transaction(root, 5);
9815 if (IS_ERR(trans))
9816 return PTR_ERR(trans);
9817
9818 ret = btrfs_find_free_ino(root, &objectid);
9819 if (ret)
9820 goto out;
9821
9822 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009823 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009824 if (IS_ERR(inode)) {
9825 ret = PTR_ERR(inode);
9826 inode = NULL;
9827 goto out;
9828 }
9829
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009830 inode->i_fop = &btrfs_file_operations;
9831 inode->i_op = &btrfs_file_inode_operations;
9832
9833 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009834 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
9835
Chris Masonb0d5d102014-09-08 13:08:51 -07009836 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9837 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009838 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009839
9840 ret = btrfs_update_inode(trans, root, inode);
9841 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009842 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009843 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009844 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009845 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009846
Filipe Manana5762b5c2014-08-01 00:10:32 +01009847 /*
9848 * We set number of links to 0 in btrfs_new_inode(), and here we set
9849 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9850 * through:
9851 *
9852 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9853 */
9854 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009855 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009856 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009857 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009858out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009859 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009860 if (ret && inode)
9861 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009862 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009863 return ret;
9864}
9865
David Sterba5cdc84b2018-07-18 20:32:52 +02009866void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009867{
David Sterba5cdc84b2018-07-18 20:32:52 +02009868 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009869 unsigned long index = start >> PAGE_SHIFT;
9870 unsigned long end_index = end >> PAGE_SHIFT;
9871 struct page *page;
9872
9873 while (index <= end_index) {
9874 page = find_get_page(inode->i_mapping, index);
9875 ASSERT(page); /* Pages should be in the extent_io_tree */
9876 set_page_writeback(page);
9877 put_page(page);
9878 index++;
9879 }
9880}
9881
Omar Sandovaled46ff32016-11-03 10:28:14 -07009882#ifdef CONFIG_SWAP
9883/*
9884 * Add an entry indicating a block group or device which is pinned by a
9885 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9886 * negative errno on failure.
9887 */
9888static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9889 bool is_block_group)
9890{
9891 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9892 struct btrfs_swapfile_pin *sp, *entry;
9893 struct rb_node **p;
9894 struct rb_node *parent = NULL;
9895
9896 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9897 if (!sp)
9898 return -ENOMEM;
9899 sp->ptr = ptr;
9900 sp->inode = inode;
9901 sp->is_block_group = is_block_group;
9902
9903 spin_lock(&fs_info->swapfile_pins_lock);
9904 p = &fs_info->swapfile_pins.rb_node;
9905 while (*p) {
9906 parent = *p;
9907 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9908 if (sp->ptr < entry->ptr ||
9909 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9910 p = &(*p)->rb_left;
9911 } else if (sp->ptr > entry->ptr ||
9912 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9913 p = &(*p)->rb_right;
9914 } else {
9915 spin_unlock(&fs_info->swapfile_pins_lock);
9916 kfree(sp);
9917 return 1;
9918 }
9919 }
9920 rb_link_node(&sp->node, parent, p);
9921 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9922 spin_unlock(&fs_info->swapfile_pins_lock);
9923 return 0;
9924}
9925
9926/* Free all of the entries pinned by this swapfile. */
9927static void btrfs_free_swapfile_pins(struct inode *inode)
9928{
9929 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9930 struct btrfs_swapfile_pin *sp;
9931 struct rb_node *node, *next;
9932
9933 spin_lock(&fs_info->swapfile_pins_lock);
9934 node = rb_first(&fs_info->swapfile_pins);
9935 while (node) {
9936 next = rb_next(node);
9937 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9938 if (sp->inode == inode) {
9939 rb_erase(&sp->node, &fs_info->swapfile_pins);
9940 if (sp->is_block_group)
9941 btrfs_put_block_group(sp->ptr);
9942 kfree(sp);
9943 }
9944 node = next;
9945 }
9946 spin_unlock(&fs_info->swapfile_pins_lock);
9947}
9948
9949struct btrfs_swap_info {
9950 u64 start;
9951 u64 block_start;
9952 u64 block_len;
9953 u64 lowest_ppage;
9954 u64 highest_ppage;
9955 unsigned long nr_pages;
9956 int nr_extents;
9957};
9958
9959static int btrfs_add_swap_extent(struct swap_info_struct *sis,
9960 struct btrfs_swap_info *bsi)
9961{
9962 unsigned long nr_pages;
9963 u64 first_ppage, first_ppage_reported, next_ppage;
9964 int ret;
9965
9966 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
9967 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
9968 PAGE_SIZE) >> PAGE_SHIFT;
9969
9970 if (first_ppage >= next_ppage)
9971 return 0;
9972 nr_pages = next_ppage - first_ppage;
9973
9974 first_ppage_reported = first_ppage;
9975 if (bsi->start == 0)
9976 first_ppage_reported++;
9977 if (bsi->lowest_ppage > first_ppage_reported)
9978 bsi->lowest_ppage = first_ppage_reported;
9979 if (bsi->highest_ppage < (next_ppage - 1))
9980 bsi->highest_ppage = next_ppage - 1;
9981
9982 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
9983 if (ret < 0)
9984 return ret;
9985 bsi->nr_extents += ret;
9986 bsi->nr_pages += nr_pages;
9987 return 0;
9988}
9989
9990static void btrfs_swap_deactivate(struct file *file)
9991{
9992 struct inode *inode = file_inode(file);
9993
9994 btrfs_free_swapfile_pins(inode);
9995 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
9996}
9997
9998static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
9999 sector_t *span)
10000{
10001 struct inode *inode = file_inode(file);
10002 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10003 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10004 struct extent_state *cached_state = NULL;
10005 struct extent_map *em = NULL;
10006 struct btrfs_device *device = NULL;
10007 struct btrfs_swap_info bsi = {
10008 .lowest_ppage = (sector_t)-1ULL,
10009 };
10010 int ret = 0;
10011 u64 isize;
10012 u64 start;
10013
10014 /*
10015 * If the swap file was just created, make sure delalloc is done. If the
10016 * file changes again after this, the user is doing something stupid and
10017 * we don't really care.
10018 */
10019 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10020 if (ret)
10021 return ret;
10022
10023 /*
10024 * The inode is locked, so these flags won't change after we check them.
10025 */
10026 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10027 btrfs_warn(fs_info, "swapfile must not be compressed");
10028 return -EINVAL;
10029 }
10030 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10031 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10032 return -EINVAL;
10033 }
10034 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10035 btrfs_warn(fs_info, "swapfile must not be checksummed");
10036 return -EINVAL;
10037 }
10038
10039 /*
10040 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010041 * under us. The exclop protection makes sure they aren't running/won't
10042 * run concurrently while we are mapping the swap extents, and
10043 * fs_info->swapfile_pins prevents them from running while the swap
10044 * file is active and moving the extents. Note that this also prevents
10045 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010046 * really worth the trouble to allow it.
10047 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010048 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010049 btrfs_warn(fs_info,
10050 "cannot activate swapfile while exclusive operation is running");
10051 return -EBUSY;
10052 }
10053 /*
10054 * Snapshots can create extents which require COW even if NODATACOW is
10055 * set. We use this counter to prevent snapshots. We must increment it
10056 * before walking the extents because we don't want a concurrent
10057 * snapshot to run after we've already checked the extents.
10058 */
10059 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10060
10061 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10062
10063 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10064 start = 0;
10065 while (start < isize) {
10066 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010067 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010068 u64 len = isize - start;
10069
Omar Sandoval39b07b52019-12-02 17:34:23 -080010070 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010071 if (IS_ERR(em)) {
10072 ret = PTR_ERR(em);
10073 goto out;
10074 }
10075
10076 if (em->block_start == EXTENT_MAP_HOLE) {
10077 btrfs_warn(fs_info, "swapfile must not have holes");
10078 ret = -EINVAL;
10079 goto out;
10080 }
10081 if (em->block_start == EXTENT_MAP_INLINE) {
10082 /*
10083 * It's unlikely we'll ever actually find ourselves
10084 * here, as a file small enough to fit inline won't be
10085 * big enough to store more than the swap header, but in
10086 * case something changes in the future, let's catch it
10087 * here rather than later.
10088 */
10089 btrfs_warn(fs_info, "swapfile must not be inline");
10090 ret = -EINVAL;
10091 goto out;
10092 }
10093 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10094 btrfs_warn(fs_info, "swapfile must not be compressed");
10095 ret = -EINVAL;
10096 goto out;
10097 }
10098
10099 logical_block_start = em->block_start + (start - em->start);
10100 len = min(len, em->len - (start - em->start));
10101 free_extent_map(em);
10102 em = NULL;
10103
Boris Burkova84d5d42020-08-18 11:00:05 -070010104 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010105 if (ret < 0) {
10106 goto out;
10107 } else if (ret) {
10108 ret = 0;
10109 } else {
10110 btrfs_warn(fs_info,
10111 "swapfile must not be copy-on-write");
10112 ret = -EINVAL;
10113 goto out;
10114 }
10115
10116 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10117 if (IS_ERR(em)) {
10118 ret = PTR_ERR(em);
10119 goto out;
10120 }
10121
10122 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10123 btrfs_warn(fs_info,
10124 "swapfile must have single data profile");
10125 ret = -EINVAL;
10126 goto out;
10127 }
10128
10129 if (device == NULL) {
10130 device = em->map_lookup->stripes[0].dev;
10131 ret = btrfs_add_swapfile_pin(inode, device, false);
10132 if (ret == 1)
10133 ret = 0;
10134 else if (ret)
10135 goto out;
10136 } else if (device != em->map_lookup->stripes[0].dev) {
10137 btrfs_warn(fs_info, "swapfile must be on one device");
10138 ret = -EINVAL;
10139 goto out;
10140 }
10141
10142 physical_block_start = (em->map_lookup->stripes[0].physical +
10143 (logical_block_start - em->start));
10144 len = min(len, em->len - (logical_block_start - em->start));
10145 free_extent_map(em);
10146 em = NULL;
10147
10148 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10149 if (!bg) {
10150 btrfs_warn(fs_info,
10151 "could not find block group containing swapfile");
10152 ret = -EINVAL;
10153 goto out;
10154 }
10155
10156 ret = btrfs_add_swapfile_pin(inode, bg, true);
10157 if (ret) {
10158 btrfs_put_block_group(bg);
10159 if (ret == 1)
10160 ret = 0;
10161 else
10162 goto out;
10163 }
10164
10165 if (bsi.block_len &&
10166 bsi.block_start + bsi.block_len == physical_block_start) {
10167 bsi.block_len += len;
10168 } else {
10169 if (bsi.block_len) {
10170 ret = btrfs_add_swap_extent(sis, &bsi);
10171 if (ret)
10172 goto out;
10173 }
10174 bsi.start = start;
10175 bsi.block_start = physical_block_start;
10176 bsi.block_len = len;
10177 }
10178
10179 start += len;
10180 }
10181
10182 if (bsi.block_len)
10183 ret = btrfs_add_swap_extent(sis, &bsi);
10184
10185out:
10186 if (!IS_ERR_OR_NULL(em))
10187 free_extent_map(em);
10188
10189 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10190
10191 if (ret)
10192 btrfs_swap_deactivate(file);
10193
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010194 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010195
10196 if (ret)
10197 return ret;
10198
10199 if (device)
10200 sis->bdev = device->bdev;
10201 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10202 sis->max = bsi.nr_pages;
10203 sis->pages = bsi.nr_pages - 1;
10204 sis->highest_bit = bsi.nr_pages - 1;
10205 return bsi.nr_extents;
10206}
10207#else
10208static void btrfs_swap_deactivate(struct file *file)
10209{
10210}
10211
10212static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10213 sector_t *span)
10214{
10215 return -EOPNOTSUPP;
10216}
10217#endif
10218
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010219static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010220 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010221 .lookup = btrfs_lookup,
10222 .create = btrfs_create,
10223 .unlink = btrfs_unlink,
10224 .link = btrfs_link,
10225 .mkdir = btrfs_mkdir,
10226 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010227 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010228 .symlink = btrfs_symlink,
10229 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010230 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010231 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010232 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010233 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010234 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010235 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010236 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010237};
Yan, Zheng76dda932009-09-21 16:00:26 -040010238
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010239static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010240 .llseek = generic_file_llseek,
10241 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010242 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010243 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010244 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010245#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010246 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010247#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010248 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010249 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010250};
10251
David Sterba20e55062015-11-19 11:42:28 +010010252static const struct extent_io_ops btrfs_extent_io_ops = {
David Sterba4d53ddd2017-02-17 15:27:44 +010010253 /* mandatory callbacks */
Chris Mason065631f2008-02-20 12:07:25 -050010254 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -040010255 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
10256};
10257
Chris Mason35054392009-01-21 13:11:13 -050010258/*
10259 * btrfs doesn't support the bmap operation because swapfiles
10260 * use bmap to make a mapping of extents in the file. They assume
10261 * these extents won't change over the life of the file and they
10262 * use the bmap result to do IO directly to the drive.
10263 *
10264 * the btrfs bmap call would return logical addresses that aren't
10265 * suitable for IO and they also will change frequently as COW
10266 * operations happen. So, swapfile + btrfs == corruption.
10267 *
10268 * For now we're avoiding this by dropping bmap.
10269 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010270static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010271 .readpage = btrfs_readpage,
10272 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010273 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010274 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010275 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010276 .invalidatepage = btrfs_invalidatepage,
10277 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010278#ifdef CONFIG_MIGRATION
10279 .migratepage = btrfs_migratepage,
10280#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010281 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010282 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010283 .swap_activate = btrfs_swap_activate,
10284 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010285};
10286
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010287static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010288 .getattr = btrfs_getattr,
10289 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010290 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010291 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010292 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010293 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010294 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010295 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010296};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010297static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010298 .getattr = btrfs_getattr,
10299 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010300 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010301 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010302 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010303 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010304 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010305};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010306static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010307 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010308 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010309 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010310 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010311 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010312 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010313};
Yan, Zheng76dda932009-09-21 16:00:26 -040010314
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010315const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010316 .d_delete = btrfs_dentry_delete,
10317};