blob: 732a5fab471d9ac0c3f254d9271a071d1a86502e [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;
Filipe Mananaf28a4922015-12-08 19:23:20 +000065};
66
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070067static const struct inode_operations btrfs_dir_inode_operations;
68static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070069static const struct inode_operations btrfs_special_inode_operations;
70static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070071static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070072static const struct file_operations btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040073
74static struct kmem_cache *btrfs_inode_cachep;
75struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040076struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050077struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000078struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040079
Eric Sandeen3972f262013-01-12 02:57:22 +000080static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000081static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040082static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030083static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050084 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030085 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030086 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030087static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
88 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080089 u64 block_len, u64 orig_block_len,
90 u64 ram_bytes, int compress_type,
91 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040092
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030093static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080094 const u64 offset, const u64 bytes,
95 const bool uptodate);
96
97/*
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -050098 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
99 *
100 * ilock_flags can have the following bit set:
101 *
102 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
103 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
104 * return -EAGAIN
105 */
106int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
107{
108 if (ilock_flags & BTRFS_ILOCK_SHARED) {
109 if (ilock_flags & BTRFS_ILOCK_TRY) {
110 if (!inode_trylock_shared(inode))
111 return -EAGAIN;
112 else
113 return 0;
114 }
115 inode_lock_shared(inode);
116 } else {
117 if (ilock_flags & BTRFS_ILOCK_TRY) {
118 if (!inode_trylock(inode))
119 return -EAGAIN;
120 else
121 return 0;
122 }
123 inode_lock(inode);
124 }
125 return 0;
126}
127
128/*
129 * btrfs_inode_unlock - unock inode i_rwsem
130 *
131 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
132 * to decide whether the lock acquired is shared or exclusive.
133 */
134void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
135{
136 if (ilock_flags & BTRFS_ILOCK_SHARED)
137 inode_unlock_shared(inode);
138 else
139 inode_unlock(inode);
140}
141
142/*
Qu Wenruo524272602017-03-08 10:25:52 +0800143 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100144 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800145 *
146 * NOTE: caller must ensure that when an error happens, it can not call
147 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
148 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
149 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200150 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800151 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300152static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200153 struct page *locked_page,
154 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800155{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900156 unsigned long index = offset >> PAGE_SHIFT;
157 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200158 u64 page_start = page_offset(locked_page);
159 u64 page_end = page_start + PAGE_SIZE - 1;
160
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900161 struct page *page;
162
163 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300164 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900165 index++;
166 if (!page)
167 continue;
168 ClearPagePrivate2(page);
169 put_page(page);
170 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200171
172 /*
173 * In case this page belongs to the delalloc range being instantiated
174 * then skip it, since the first page of a range is going to be
175 * properly cleaned up by the caller of run_delalloc_range
176 */
177 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
178 offset += PAGE_SIZE;
179 bytes -= PAGE_SIZE;
180 }
181
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300182 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800183}
184
Eric Sandeen48a3b632013-04-25 20:41:01 +0000185static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400186
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000187static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500188 struct inode *inode, struct inode *dir,
189 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500190{
191 int err;
192
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000193 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500194 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500195 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500196 return err;
197}
198
Chris Masond352ac62008-09-29 15:18:18 -0400199/*
Chris Masonc8b97812008-10-29 14:49:59 -0400200 * this does all the hard work for inserting an inline extent into
201 * the btree. The caller should have done a btrfs_drop_extents so that
202 * no overlapping inline items exist in the btree
203 */
Chris Mason40f76582014-05-21 13:35:51 -0700204static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000205 struct btrfs_path *path, bool extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400206 struct btrfs_root *root, struct inode *inode,
207 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000208 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400209 struct page **compressed_pages)
210{
Chris Masonc8b97812008-10-29 14:49:59 -0400211 struct extent_buffer *leaf;
212 struct page *page = NULL;
213 char *kaddr;
214 unsigned long ptr;
215 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400216 int ret;
217 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400218 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400219
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800220 ASSERT((compressed_size > 0 && compressed_pages) ||
221 (compressed_size == 0 && !compressed_pages));
222
Li Zefanfe3f5662011-03-28 08:30:38 +0000223 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400224 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400225
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000226 if (!extent_inserted) {
227 struct btrfs_key key;
228 size_t datasize;
229
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200230 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000231 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200232 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000233
234 datasize = btrfs_file_extent_calc_inline_size(cur_size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000235 ret = btrfs_insert_empty_item(trans, root, path, &key,
236 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200237 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000238 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400239 }
240 leaf = path->nodes[0];
241 ei = btrfs_item_ptr(leaf, path->slots[0],
242 struct btrfs_file_extent_item);
243 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
244 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
245 btrfs_set_file_extent_encryption(leaf, ei, 0);
246 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
247 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
248 ptr = btrfs_file_extent_inline_start(ei);
249
Li Zefan261507a02010-12-17 14:21:50 +0800250 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400251 struct page *cpage;
252 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500253 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400254 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500255 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300256 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400257
Cong Wang7ac687d2011-11-25 23:14:28 +0800258 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400259 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800260 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400261
262 i++;
263 ptr += cur_size;
264 compressed_size -= cur_size;
265 }
266 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800267 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400268 } else {
269 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300270 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400271 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800272 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100273 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400274 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800275 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300276 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400277 }
278 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000279 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400280
Yan, Zhengc2167752009-11-12 09:34:21 +0000281 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500282 * We align size to sectorsize for inline extents just for simplicity
283 * sake.
284 */
285 size = ALIGN(size, root->fs_info->sectorsize);
286 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
287 if (ret)
288 goto fail;
289
290 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000291 * we're an inline extent, so nobody can
292 * extend the file past i_size without locking
293 * a page we already have locked.
294 *
295 * We must do any isize and inode updates
296 * before we unlock the pages. Otherwise we
297 * could end up racing with unlink.
298 */
Chris Masonc8b97812008-10-29 14:49:59 -0400299 BTRFS_I(inode)->disk_i_size = inode->i_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400300fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200301 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400302}
303
304
305/*
306 * conditionally insert an inline extent into the file. This
307 * does the checks required to make sure the data is small enough
308 * to fit as an inline extent.
309 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300310static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400311 u64 end, size_t compressed_size,
312 int compress_type,
313 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400314{
Filipe Manana5893dfb2020-11-04 11:07:32 +0000315 struct btrfs_drop_extents_args drop_args = { 0 };
Nikolay Borisova0349402020-06-03 08:55:12 +0300316 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400317 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400318 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300319 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400320 u64 actual_end = min(end + 1, isize);
321 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400322 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400323 u64 data_len = inline_len;
324 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000325 struct btrfs_path *path;
Chris Masonc8b97812008-10-29 14:49:59 -0400326
327 if (compressed_size)
328 data_len = compressed_size;
329
330 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400331 actual_end > fs_info->sectorsize ||
332 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400333 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400334 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400335 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400336 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400337 return 1;
338 }
339
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000340 path = btrfs_alloc_path();
341 if (!path)
342 return -ENOMEM;
343
Josef Bacik00361582013-08-14 14:02:47 -0400344 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000345 if (IS_ERR(trans)) {
346 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400347 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000348 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300349 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400350
Filipe Manana5893dfb2020-11-04 11:07:32 +0000351 drop_args.path = path;
352 drop_args.start = start;
353 drop_args.end = aligned_end;
354 drop_args.drop_cache = true;
355 drop_args.replace_extent = true;
356
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000357 if (compressed_size && compressed_pages)
Filipe Manana5893dfb2020-11-04 11:07:32 +0000358 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000359 compressed_size);
360 else
Filipe Manana5893dfb2020-11-04 11:07:32 +0000361 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000362 inline_len);
363
Filipe Manana5893dfb2020-11-04 11:07:32 +0000364 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik00361582013-08-14 14:02:47 -0400365 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400366 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400367 goto out;
368 }
Chris Masonc8b97812008-10-29 14:49:59 -0400369
370 if (isize > actual_end)
371 inline_len = min_t(u64, isize, actual_end);
Filipe Manana5893dfb2020-11-04 11:07:32 +0000372 ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300373 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400374 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000375 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400376 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400377 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400378 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400379 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400380 ret = 1;
381 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100382 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400383
Filipe Manana2766ff62020-11-04 11:07:34 +0000384 btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200385 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +0000386 if (ret && ret != -ENOSPC) {
387 btrfs_abort_transaction(trans, ret);
388 goto out;
389 } else if (ret == -ENOSPC) {
390 ret = 1;
391 goto out;
392 }
393
Nikolay Borisova0349402020-06-03 08:55:12 +0300394 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
395 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
Josef Bacik00361582013-08-14 14:02:47 -0400396out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800397 /*
398 * Don't forget to free the reserved space, as for inlined extent
399 * it won't count as data extent, free them directly here.
400 * And at reserve time, it's always aligned to page size, so
401 * just free one page here.
402 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300403 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000404 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400405 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400406 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400407}
408
Chris Mason771ed682008-11-06 22:02:51 -0500409struct async_extent {
410 u64 start;
411 u64 ram_size;
412 u64 compressed_size;
413 struct page **pages;
414 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800415 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500416 struct list_head list;
417};
418
Nikolay Borisov97db1202019-03-12 17:20:24 +0200419struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500420 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500421 struct page *locked_page;
422 u64 start;
423 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600424 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500425 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700426 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500427 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200428 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500429};
430
Nikolay Borisov97db1202019-03-12 17:20:24 +0200431struct async_cow {
432 /* Number of chunks in flight; must be first in the structure */
433 atomic_t num_chunks;
434 struct async_chunk chunks[];
435};
436
437static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500438 u64 start, u64 ram_size,
439 u64 compressed_size,
440 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800441 unsigned long nr_pages,
442 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500443{
444 struct async_extent *async_extent;
445
446 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100447 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500448 async_extent->start = start;
449 async_extent->ram_size = ram_size;
450 async_extent->compressed_size = compressed_size;
451 async_extent->pages = pages;
452 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800453 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500454 list_add_tail(&async_extent->list, &cow->extents);
455 return 0;
456}
457
Qu Wenruo42c16da2019-07-01 05:12:46 +0000458/*
459 * Check if the inode has flags compatible with compression
460 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300461static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000462{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300463 if (inode->flags & BTRFS_INODE_NODATACOW ||
464 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000465 return false;
466 return true;
467}
468
469/*
470 * Check if the inode needs to be submitted to compression, based on mount
471 * options, defragmentation, properties or heuristics.
472 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300473static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
474 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800475{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300476 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800477
Nikolay Borisov808a1292020-06-03 08:55:27 +0300478 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000479 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
480 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300481 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000482 return 0;
483 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800484 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400485 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800486 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200487 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300488 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200489 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800490 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300491 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800492 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400493 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300494 inode->flags & BTRFS_INODE_COMPRESS ||
495 inode->prop_compress)
496 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800497 return 0;
498}
499
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200500static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800501 u64 start, u64 end, u64 num_bytes, u64 small_write)
502{
503 /* If this is a small write inside eof, kick off a defrag */
504 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200505 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800506 btrfs_add_inode_defrag(NULL, inode);
507}
508
Chris Masonc8b97812008-10-29 14:49:59 -0400509/*
Chris Mason771ed682008-11-06 22:02:51 -0500510 * we create compressed extents in two phases. The first
511 * phase compresses a range of pages that have already been
512 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400513 *
Chris Mason771ed682008-11-06 22:02:51 -0500514 * This is done inside an ordered work queue, and the compression
515 * is spread across many cpus. The actual IO submission is step
516 * two, and the ordered work queue takes care of making sure that
517 * happens in the same order things were put onto the queue by
518 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400519 *
Chris Mason771ed682008-11-06 22:02:51 -0500520 * If this code finds it can't get good compression, it puts an
521 * entry onto the work queue to write the uncompressed bytes. This
522 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300523 * are written in the same order that the flusher thread sent them
524 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400525 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300526static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400527{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200528 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400529 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400530 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200531 u64 start = async_chunk->start;
532 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400533 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400534 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400535 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400536 struct page **pages = NULL;
537 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400538 unsigned long total_compressed = 0;
539 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400540 int i;
541 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400542 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300543 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400544 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400545
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200546 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
547 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400548
Josef Bacikd98da492019-10-11 09:03:54 -0400549 /*
550 * We need to save i_size before now because it could change in between
551 * us evaluating the size and assigning it. This is because we lock and
552 * unlock the page in truncate and fallocate, and then modify the i_size
553 * later on.
554 *
555 * The barriers are to emulate READ_ONCE, remove that once i_size_read
556 * does that for us.
557 */
558 barrier();
559 i_size = i_size_read(inode);
560 barrier();
561 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400562again:
563 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300564 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100565 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
566 nr_pages = min_t(unsigned long, nr_pages,
567 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400568
Chris Masonf03d9301f2009-02-04 09:31:06 -0500569 /*
570 * we don't want to send crud past the end of i_size through
571 * compression, that's just a waste of CPU time. So, if the
572 * end of the file is before the start of our current
573 * requested range of bytes, we bail out to the uncompressed
574 * cleanup code that can deal with all of this.
575 *
576 * It isn't really the fastest way to fix things, but this is a
577 * very uncommon corner.
578 */
579 if (actual_end <= start)
580 goto cleanup_and_bail_uncompressed;
581
Chris Masonc8b97812008-10-29 14:49:59 -0400582 total_compressed = actual_end - start;
583
Shilong Wang4bcbb332014-10-07 18:44:35 -0400584 /*
585 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400586 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400587 */
588 if (total_compressed <= blocksize &&
589 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
590 goto cleanup_and_bail_uncompressed;
591
David Sterba069eac72017-02-14 19:36:54 +0100592 total_compressed = min_t(unsigned long, total_compressed,
593 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400594 total_in = 0;
595 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400596
Chris Mason771ed682008-11-06 22:02:51 -0500597 /*
598 * we do compression for mount -o compress and when the
599 * inode has not been flagged as nocompress. This flag can
600 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400601 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300602 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400603 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100604 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800605 if (!pages) {
606 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100607 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800608 goto cont;
609 }
Chris Mason179e29e2007-11-01 11:28:41 -0400610
David Sterbaeec63c62017-07-17 19:41:31 +0200611 if (BTRFS_I(inode)->defrag_compress)
612 compress_type = BTRFS_I(inode)->defrag_compress;
613 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200614 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800615
Chris Mason4adaa612013-03-26 13:07:00 -0400616 /*
617 * we need to call clear_page_dirty_for_io on each
618 * page in the range. Otherwise applications with the file
619 * mmap'd can wander in and change the page contents while
620 * we are compressing them.
621 *
622 * If the compression fails for any reason, we set the pages
623 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300624 *
625 * Note that the remaining part is redirtied, the start pointer
626 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400627 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300628 if (!redirty) {
629 extent_range_clear_dirty_for_io(inode, start, end);
630 redirty = 1;
631 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200632
633 /* Compression level is applied here and only here */
634 ret = btrfs_compress_pages(
635 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800636 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100637 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100638 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800639 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100640 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400641
642 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100643 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100644 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400645 char *kaddr;
646
647 /* zero the tail end of the last page, we might be
648 * sending it down to disk
649 */
650 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800651 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400652 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300653 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800654 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400655 }
656 will_compress = 1;
657 }
658 }
Li Zefan560f7d72011-09-08 10:22:01 +0800659cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400660 if (start == 0) {
661 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300662 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400663 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500664 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400665 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300666 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
667 0, BTRFS_COMPRESS_NONE,
668 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400669 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500670 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300671 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000672 total_compressed,
673 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400674 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100675 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400676 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400677 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
678 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100679 unsigned long page_error_op;
680
Filipe Mananae6eb4312014-10-10 10:45:12 +0100681 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400682
Chris Mason771ed682008-11-06 22:02:51 -0500683 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100684 * inline extent creation worked or returned error,
685 * we don't need to create any more async work items.
686 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400687 *
688 * We use DO_ACCOUNTING here because we need the
689 * delalloc_release_metadata to be done _after_ we drop
690 * our outstanding extent for clearing delalloc for this
691 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500692 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300693 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
694 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300695 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800696 PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -0400697 PAGE_CLEAR_DIRTY |
698 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100699 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400700 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300701
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800702 /*
703 * Ensure we only free the compressed pages if we have
704 * them allocated, as we can still reach here with
705 * inode_need_compress() == false.
706 */
707 if (pages) {
708 for (i = 0; i < nr_pages; i++) {
709 WARN_ON(pages[i]->mapping);
710 put_page(pages[i]);
711 }
712 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300713 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300714 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400715 }
716 }
717
718 if (will_compress) {
719 /*
720 * we aren't doing an inline extent round the compressed size
721 * up to a block size boundary so the allocator does sane
722 * things
723 */
Qu Wenruofda28322013-02-26 08:10:22 +0000724 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400725
726 /*
727 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300728 * win, compare the page count read with the blocks on disk,
729 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400730 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300731 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300732 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300733 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700734
735 /*
736 * The async work queues will take care of doing actual
737 * allocation on disk for these compressed pages, and
738 * will submit them to the elevator.
739 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200740 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100741 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700742 compress_type);
743
Timofey Titovets11708622017-10-03 18:06:01 +0300744 if (start + total_in < end) {
745 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700746 pages = NULL;
747 cond_resched();
748 goto again;
749 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300750 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400751 }
752 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700753 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400754 /*
755 * the compression code ran but failed to make things smaller,
756 * free any pages it allocated and our page pointer array
757 */
David Sterba4d3a8002017-02-14 19:04:07 +0100758 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400759 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300760 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400761 }
762 kfree(pages);
763 pages = NULL;
764 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100765 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400766
767 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400768 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200769 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500770 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500771 }
Chris Masonc8b97812008-10-29 14:49:59 -0400772 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500773cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700774 /*
775 * No compression, but we still need to write the pages in the file
776 * we've been given so far. redirty the locked page if it corresponds
777 * to our extent and set things up for the async work queue to run
778 * cow_file_range to do the normal delalloc dance.
779 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700780 if (async_chunk->locked_page &&
781 (page_offset(async_chunk->locked_page) >= start &&
782 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200783 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700784 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700785 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700786
787 if (redirty)
788 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200789 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700790 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300791 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500792
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300793 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500794}
Chris Mason771ed682008-11-06 22:02:51 -0500795
Filipe Manana40ae8372014-10-06 22:14:24 +0100796static void free_async_extent_pages(struct async_extent *async_extent)
797{
798 int i;
799
800 if (!async_extent->pages)
801 return;
802
803 for (i = 0; i < async_extent->nr_pages; i++) {
804 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300805 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100806 }
807 kfree(async_extent->pages);
808 async_extent->nr_pages = 0;
809 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500810}
811
812/*
813 * phase two of compressed writeback. This is the ordered portion
814 * of the code, which only gets called in the order the work was
815 * queued. We walk all the async extents created by compress_file_range
816 * and send them down to the disk.
817 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200818static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500819{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300820 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
821 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500822 struct async_extent *async_extent;
823 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500824 struct btrfs_key ins;
825 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300826 struct btrfs_root *root = inode->root;
827 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500828 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500829
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500830again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200831 while (!list_empty(&async_chunk->extents)) {
832 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500833 struct async_extent, list);
834 list_del(&async_extent->list);
835
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500836retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200837 lock_extent(io_tree, async_extent->start,
838 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500839 /* did the compression code fall back to uncompressed IO? */
840 if (!async_extent->pages) {
841 int page_started = 0;
842 unsigned long nr_written = 0;
843
Chris Mason771ed682008-11-06 22:02:51 -0500844 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300845 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500846 async_extent->start,
847 async_extent->start +
848 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300849 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500850
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100851 /* JDM XXX */
852
Chris Mason771ed682008-11-06 22:02:51 -0500853 /*
854 * if page_started, cow_file_range inserted an
855 * inline extent and took care of all the unlocking
856 * and IO for us. Otherwise, we need to submit
857 * all those pages down to the drive.
858 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500859 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300860 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200861 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500862 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500863 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500864 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700865 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200866 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500867 kfree(async_extent);
868 cond_resched();
869 continue;
870 }
871
Wang Xiaoguang18513092016-07-25 15:51:40 +0800872 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500873 async_extent->compressed_size,
874 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800875 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500876 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100877 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500878
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400879 if (ret == -ENOSPC) {
880 unlock_extent(io_tree, async_extent->start,
881 async_extent->start +
882 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800883
884 /*
885 * we need to redirty the pages if we decide to
886 * fallback to uncompressed IO, otherwise we
887 * will not submit these pages down to lower
888 * layers.
889 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300890 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800891 async_extent->start,
892 async_extent->start +
893 async_extent->ram_size - 1);
894
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100895 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400896 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500897 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500898 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000899 /*
900 * here we're doing allocation and writeback of the
901 * compressed pages
902 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300903 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800904 async_extent->ram_size, /* len */
905 async_extent->start, /* orig_start */
906 ins.objectid, /* block_start */
907 ins.offset, /* block_len */
908 ins.offset, /* orig_block_len */
909 async_extent->ram_size, /* ram_bytes */
910 async_extent->compress_type,
911 BTRFS_ORDERED_COMPRESSED);
912 if (IS_ERR(em))
913 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500914 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800915 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500916
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300917 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800918 async_extent->start,
919 ins.objectid,
920 async_extent->ram_size,
921 ins.offset,
922 BTRFS_ORDERED_COMPRESSED,
923 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100924 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300925 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100926 async_extent->start +
927 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500928 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100929 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400930 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500931
Chris Mason771ed682008-11-06 22:02:51 -0500932 /*
933 * clear dirty, set writeback and unlock the pages.
934 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300935 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400936 async_extent->start +
937 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400938 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
939 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400940 PAGE_SET_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300941 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500942 async_extent->ram_size,
943 ins.objectid,
944 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600945 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700946 async_chunk->write_flags,
947 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100948 struct page *p = async_extent->pages[0];
949 const u64 start = async_extent->start;
950 const u64 end = start + async_extent->ram_size - 1;
951
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300952 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200953 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200954
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100955 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300956 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100957 PAGE_END_WRITEBACK |
958 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100959 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100960 }
Chris Mason771ed682008-11-06 22:02:51 -0500961 alloc_hint = ins.objectid + ins.offset;
962 kfree(async_extent);
963 cond_resched();
964 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100965 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500966out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400967 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400968 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100969out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300970 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500971 async_extent->start +
972 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400973 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100974 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400975 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
976 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100977 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
978 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100979 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100980 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500981 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500982}
983
Nikolay Borisov43c69842020-06-03 08:55:02 +0300984static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400985 u64 num_bytes)
986{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300987 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400988 struct extent_map *em;
989 u64 alloc_hint = 0;
990
991 read_lock(&em_tree->lock);
992 em = search_extent_mapping(em_tree, start, num_bytes);
993 if (em) {
994 /*
995 * if block start isn't an actual block number then find the
996 * first block in this inode and use that as a hint. If that
997 * block is also bogus then just don't worry about it.
998 */
999 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
1000 free_extent_map(em);
1001 em = search_extent_mapping(em_tree, 0, 0);
1002 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1003 alloc_hint = em->block_start;
1004 if (em)
1005 free_extent_map(em);
1006 } else {
1007 alloc_hint = em->block_start;
1008 free_extent_map(em);
1009 }
1010 }
1011 read_unlock(&em_tree->lock);
1012
1013 return alloc_hint;
1014}
1015
Chris Mason771ed682008-11-06 22:02:51 -05001016/*
1017 * when extent_io.c finds a delayed allocation range in the file,
1018 * the call backs end up in this code. The basic idea is to
1019 * allocate extents on disk for the range, and create ordered data structs
1020 * in ram to track those extents.
1021 *
1022 * locked_page is the page that writepage had locked already. We use
1023 * it to make sure we don't do extra locks or unlocks.
1024 *
1025 * *page_started is set to one if we unlock locked_page and do everything
1026 * required to start IO on it. It may be clean and already done with
1027 * IO when we return.
1028 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001029static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001030 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001031 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001032 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001033{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001034 struct btrfs_root *root = inode->root;
1035 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001036 u64 alloc_hint = 0;
1037 u64 num_bytes;
1038 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001039 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001040 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001041 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001042 struct btrfs_key ins;
1043 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001044 unsigned clear_bits;
1045 unsigned long page_ops;
1046 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001047 int ret = 0;
1048
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001049 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001050 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001051 ret = -EINVAL;
1052 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001053 }
Chris Mason771ed682008-11-06 22:02:51 -05001054
Qu Wenruofda28322013-02-26 08:10:22 +00001055 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001056 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001057 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001058
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001059 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001060
Chris Mason771ed682008-11-06 22:02:51 -05001061 if (start == 0) {
1062 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001063 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001064 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001065 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001066 /*
1067 * We use DO_ACCOUNTING here because we need the
1068 * delalloc_release_metadata to be run _after_ we drop
1069 * our outstanding extent for clearing delalloc for this
1070 * range.
1071 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001072 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001073 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001074 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1075 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001076 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1077 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001078 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001079 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001080 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001081 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001082 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001083 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001084 }
1085 }
Chris Masonc8b97812008-10-29 14:49:59 -04001086
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001087 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1088 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001089
Filipe Manana432cd2a2020-06-08 13:32:55 +01001090 /*
1091 * Relocation relies on the relocated extents to have exactly the same
1092 * size as the original extents. Normally writeback for relocation data
1093 * extents follows a NOCOW path because relocation preallocates the
1094 * extents. However, due to an operation such as scrub turning a block
1095 * group to RO mode, it may fallback to COW mode, so we must make sure
1096 * an extent allocated during COW has exactly the requested size and can
1097 * not be split into smaller extents, otherwise relocation breaks and
1098 * fails during the stage where it updates the bytenr of file extent
1099 * items.
1100 */
1101 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1102 min_alloc_size = num_bytes;
1103 else
1104 min_alloc_size = fs_info->sectorsize;
1105
Anand Jain3752d222018-02-15 12:29:38 +08001106 while (num_bytes > 0) {
1107 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001108 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001109 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001110 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001111 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001112 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001113 cur_alloc_size = ins.offset;
1114 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001115
Chris Mason771ed682008-11-06 22:02:51 -05001116 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001117 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001118 start, /* orig_start */
1119 ins.objectid, /* block_start */
1120 ins.offset, /* block_len */
1121 ins.offset, /* orig_block_len */
1122 ram_size, /* ram_bytes */
1123 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001124 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001125 if (IS_ERR(em)) {
1126 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001127 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001128 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001129 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001130
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001131 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1132 ram_size, cur_alloc_size, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001133 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001134 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001135
Yan Zheng17d217f2008-12-12 10:03:38 -05001136 if (root->root_key.objectid ==
1137 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001138 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001139 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001140 /*
1141 * Only drop cache here, and process as normal.
1142 *
1143 * We must not allow extent_clear_unlock_delalloc()
1144 * at out_unlock label to free meta of this ordered
1145 * extent, as its meta should be freed by
1146 * btrfs_finish_ordered_io().
1147 *
1148 * So we must continue until @start is increased to
1149 * skip current ordered extent.
1150 */
Josef Bacik00361582013-08-14 14:02:47 -04001151 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001152 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001153 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001154 }
1155
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001156 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001157
Chris Masonc8b97812008-10-29 14:49:59 -04001158 /* we're not doing compressed IO, don't unlock the first
1159 * page (which the caller expects to stay locked), don't
1160 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001161 *
1162 * Do set the Private2 bit so we know this page was properly
1163 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001164 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001165 page_ops = unlock ? PAGE_UNLOCK : 0;
1166 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001167
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001168 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001169 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001170 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001171 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001172 if (num_bytes < cur_alloc_size)
1173 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001174 else
Anand Jain3752d222018-02-15 12:29:38 +08001175 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001176 alloc_hint = ins.objectid + ins.offset;
1177 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001178 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001179
1180 /*
1181 * btrfs_reloc_clone_csums() error, since start is increased
1182 * extent_clear_unlock_delalloc() at out_unlock label won't
1183 * free metadata of current ordered extent, we're OK to exit.
1184 */
1185 if (ret)
1186 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001187 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001188out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001189 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001190
Filipe Mananad9f85962014-08-25 10:43:00 +01001191out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001192 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001193out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001194 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001195 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001196out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001197 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1198 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001199 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1200 PAGE_END_WRITEBACK;
1201 /*
1202 * If we reserved an extent for our delalloc range (or a subrange) and
1203 * failed to create the respective ordered extent, then it means that
1204 * when we reserved the extent we decremented the extent's size from
1205 * the data space_info's bytes_may_use counter and incremented the
1206 * space_info's bytes_reserved counter by the same amount. We must make
1207 * sure extent_clear_unlock_delalloc() does not try to decrement again
1208 * the data space_info's bytes_may_use counter, therefore we do not pass
1209 * it the flag EXTENT_CLEAR_DATA_RESV.
1210 */
1211 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001212 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001213 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001214 locked_page,
1215 clear_bits,
1216 page_ops);
1217 start += cur_alloc_size;
1218 if (start >= end)
1219 goto out;
1220 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001221 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001222 clear_bits | EXTENT_CLEAR_DATA_RESV,
1223 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001224 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001225}
Chris Masonc8b97812008-10-29 14:49:59 -04001226
Chris Mason771ed682008-11-06 22:02:51 -05001227/*
1228 * work queue call back to started compression on a file and pages
1229 */
1230static noinline void async_cow_start(struct btrfs_work *work)
1231{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001232 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001233 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001234
Nikolay Borisovb5326272019-03-12 17:20:25 +02001235 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001236
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001237 compressed_extents = compress_file_range(async_chunk);
1238 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001239 btrfs_add_delayed_iput(async_chunk->inode);
1240 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001241 }
Chris Mason771ed682008-11-06 22:02:51 -05001242}
1243
1244/*
1245 * work queue call back to submit previously compressed pages
1246 */
1247static noinline void async_cow_submit(struct btrfs_work *work)
1248{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001249 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1250 work);
1251 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001252 unsigned long nr_pages;
1253
Nikolay Borisovb5326272019-03-12 17:20:25 +02001254 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001255 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001256
David Sterba093258e2018-02-26 16:15:17 +01001257 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001258 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001259 5 * SZ_1M)
1260 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001261
Nikolay Borisov4546d172019-01-03 10:50:03 +02001262 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001263 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001264 * in which case we don't have anything to submit, yet we need to
1265 * always adjust ->async_delalloc_pages as its paired with the init
1266 * happening in cow_file_range_async
1267 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001268 if (async_chunk->inode)
1269 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001270}
Chris Masonc8b97812008-10-29 14:49:59 -04001271
Chris Mason771ed682008-11-06 22:02:51 -05001272static noinline void async_cow_free(struct btrfs_work *work)
1273{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001274 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001275
Nikolay Borisovb5326272019-03-12 17:20:25 +02001276 async_chunk = container_of(work, struct async_chunk, work);
1277 if (async_chunk->inode)
1278 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001279 if (async_chunk->blkcg_css)
1280 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001281 /*
1282 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001283 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001284 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001285 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001286 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001287}
1288
Nikolay Borisov751b6432020-06-03 08:55:22 +03001289static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001290 struct writeback_control *wbc,
1291 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001292 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001293 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001294{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001295 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001296 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001297 struct async_cow *ctx;
1298 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001299 unsigned long nr_pages;
1300 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001301 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1302 int i;
1303 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001304 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001305 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001306
Nikolay Borisov751b6432020-06-03 08:55:22 +03001307 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001308
Nikolay Borisov751b6432020-06-03 08:55:22 +03001309 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001310 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1311 num_chunks = 1;
1312 should_compress = false;
1313 } else {
1314 should_compress = true;
1315 }
1316
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001317 nofs_flag = memalloc_nofs_save();
1318 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1319 memalloc_nofs_restore(nofs_flag);
1320
Nikolay Borisov97db1202019-03-12 17:20:24 +02001321 if (!ctx) {
1322 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1323 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1324 EXTENT_DO_ACCOUNTING;
1325 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1326 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1327 PAGE_SET_ERROR;
1328
Nikolay Borisov751b6432020-06-03 08:55:22 +03001329 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1330 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001331 return -ENOMEM;
1332 }
1333
1334 async_chunk = ctx->chunks;
1335 atomic_set(&ctx->num_chunks, num_chunks);
1336
1337 for (i = 0; i < num_chunks; i++) {
1338 if (should_compress)
1339 cur_end = min(end, start + SZ_512K - 1);
1340 else
1341 cur_end = end;
1342
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001343 /*
1344 * igrab is called higher up in the call chain, take only the
1345 * lightweight reference for the callback lifetime
1346 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001347 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001348 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001349 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001350 async_chunk[i].start = start;
1351 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001352 async_chunk[i].write_flags = write_flags;
1353 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001354
Chris Mason1d53c9e2019-07-10 12:28:16 -07001355 /*
1356 * The locked_page comes all the way from writepage and its
1357 * the original page we were actually given. As we spread
1358 * this large delalloc region across multiple async_chunk
1359 * structs, only the first struct needs a pointer to locked_page
1360 *
1361 * This way we don't need racey decisions about who is supposed
1362 * to unlock it.
1363 */
1364 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001365 /*
1366 * Depending on the compressibility, the pages might or
1367 * might not go through async. We want all of them to
1368 * be accounted against wbc once. Let's do it here
1369 * before the paths diverge. wbc accounting is used
1370 * only for foreign writeback detection and doesn't
1371 * need full accuracy. Just account the whole thing
1372 * against the first page.
1373 */
1374 wbc_account_cgroup_owner(wbc, locked_page,
1375 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001376 async_chunk[i].locked_page = locked_page;
1377 locked_page = NULL;
1378 } else {
1379 async_chunk[i].locked_page = NULL;
1380 }
1381
Chris Masonec39f762019-07-10 12:28:17 -07001382 if (blkcg_css != blkcg_root_css) {
1383 css_get(blkcg_css);
1384 async_chunk[i].blkcg_css = blkcg_css;
1385 } else {
1386 async_chunk[i].blkcg_css = NULL;
1387 }
1388
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001389 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1390 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001391
Nikolay Borisov97db1202019-03-12 17:20:24 +02001392 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001393 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001394
Nikolay Borisov97db1202019-03-12 17:20:24 +02001395 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001396
Chris Mason771ed682008-11-06 22:02:51 -05001397 *nr_written += nr_pages;
1398 start = cur_end + 1;
1399 }
1400 *page_started = 1;
1401 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001402}
1403
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001404static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001405 u64 bytenr, u64 num_bytes)
1406{
1407 int ret;
1408 struct btrfs_ordered_sum *sums;
1409 LIST_HEAD(list);
1410
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001411 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001412 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001413 if (ret == 0 && list_empty(&list))
1414 return 0;
1415
1416 while (!list_empty(&list)) {
1417 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1418 list_del(&sums->list);
1419 kfree(sums);
1420 }
Liu Bo58113752018-01-31 17:09:13 -07001421 if (ret < 0)
1422 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001423 return 1;
1424}
1425
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001426static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001427 const u64 start, const u64 end,
1428 int *page_started, unsigned long *nr_written)
1429{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001430 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1431 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001432 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001433 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001434 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001435 u64 range_start = start;
1436 u64 count;
1437
1438 /*
1439 * If EXTENT_NORESERVE is set it means that when the buffered write was
1440 * made we had not enough available data space and therefore we did not
1441 * reserve data space for it, since we though we could do NOCOW for the
1442 * respective file range (either there is prealloc extent or the inode
1443 * has the NOCOW bit set).
1444 *
1445 * However when we need to fallback to COW mode (because for example the
1446 * block group for the corresponding extent was turned to RO mode by a
1447 * scrub or relocation) we need to do the following:
1448 *
1449 * 1) We increment the bytes_may_use counter of the data space info.
1450 * If COW succeeds, it allocates a new data extent and after doing
1451 * that it decrements the space info's bytes_may_use counter and
1452 * increments its bytes_reserved counter by the same amount (we do
1453 * this at btrfs_add_reserved_bytes()). So we need to increment the
1454 * bytes_may_use counter to compensate (when space is reserved at
1455 * buffered write time, the bytes_may_use counter is incremented);
1456 *
1457 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1458 * that if the COW path fails for any reason, it decrements (through
1459 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1460 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001461 *
1462 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001463 * space cache inode or an inode of the data relocation tree, we must
1464 * also increment bytes_may_use of the data space_info for the same
1465 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001466 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001467 * group that contains that extent to RO mode and therefore force COW
1468 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001469 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001470 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001471 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001472 if (count > 0 || is_space_ino || is_reloc_ino) {
1473 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001474 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001475 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1476
Filipe Manana6bd335b2020-06-08 13:33:05 +01001477 if (is_space_ino || is_reloc_ino)
1478 bytes = range_bytes;
1479
Filipe Manana467dc472020-05-27 11:16:07 +01001480 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001481 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001482 spin_unlock(&sinfo->lock);
1483
Filipe Manana2166e5e2020-05-27 11:16:19 +01001484 if (count > 0)
1485 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1486 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001487 }
1488
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001489 return cow_file_range(inode, locked_page, start, end, page_started,
1490 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001491}
1492
Chris Masond352ac62008-09-29 15:18:18 -04001493/*
1494 * when nowcow writeback call back. This checks for snapshots or COW copies
1495 * of the extents that exist in the file, and COWs the file as required.
1496 *
1497 * If no cow copies or snapshots exist, we write directly to the existing
1498 * blocks on disk
1499 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001500static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001501 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001502 const u64 start, const u64 end,
1503 int *page_started, int force,
1504 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001505{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001506 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1507 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001508 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001509 u64 cow_start = (u64)-1;
1510 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001511 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001512 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001513 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1514 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001515 bool nocow = false;
1516 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001517
1518 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001519 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001520 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001521 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001522 EXTENT_DO_ACCOUNTING |
1523 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001524 PAGE_CLEAR_DIRTY |
1525 PAGE_SET_WRITEBACK |
1526 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001527 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001528 }
Li Zefan82d59022011-04-20 10:33:24 +08001529
Yan Zheng80ff3852008-10-30 14:20:02 -04001530 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001531 struct btrfs_key found_key;
1532 struct btrfs_file_extent_item *fi;
1533 struct extent_buffer *leaf;
1534 u64 extent_end;
1535 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001536 u64 num_bytes = 0;
1537 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001538 u64 ram_bytes;
1539 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001540
1541 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001542
Liu Boe4c3b2d2017-01-30 12:25:28 -08001543 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001544 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001545 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001546 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001547
1548 /*
1549 * If there is no extent for our range when doing the initial
1550 * search, then go back to the previous slot as it will be the
1551 * one containing the search offset
1552 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001553 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1554 leaf = path->nodes[0];
1555 btrfs_item_key_to_cpu(leaf, &found_key,
1556 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001557 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001558 found_key.type == BTRFS_EXTENT_DATA_KEY)
1559 path->slots[0]--;
1560 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001561 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001562next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001563 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001564 leaf = path->nodes[0];
1565 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1566 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001567 if (ret < 0) {
1568 if (cow_start != (u64)-1)
1569 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001570 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001571 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001572 if (ret > 0)
1573 break;
1574 leaf = path->nodes[0];
1575 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001576
Yan Zheng80ff3852008-10-30 14:20:02 -04001577 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001578
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001579 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001580 if (found_key.objectid > ino)
1581 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001582 /*
1583 * Keep searching until we find an EXTENT_ITEM or there are no
1584 * more extents for this inode
1585 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001586 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1587 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1588 path->slots[0]++;
1589 goto next_slot;
1590 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001591
1592 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001593 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001594 found_key.offset > end)
1595 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001596
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001597 /*
1598 * If the found extent starts after requested offset, then
1599 * adjust extent_end to be right before this extent begins
1600 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001601 if (found_key.offset > cur_offset) {
1602 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001603 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001604 goto out_check;
1605 }
Chris Masonc31f8832008-01-08 15:46:31 -05001606
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001607 /*
1608 * Found extent which begins before our range and potentially
1609 * intersect it
1610 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001611 fi = btrfs_item_ptr(leaf, path->slots[0],
1612 struct btrfs_file_extent_item);
1613 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001614
Josef Bacikcc95bef2013-04-04 14:31:27 -04001615 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001616 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1617 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001618 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001619 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001620 extent_end = found_key.offset +
1621 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001622 disk_num_bytes =
1623 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001624 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001625 * If the extent we got ends before our current offset,
1626 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001627 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001628 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001629 path->slots[0]++;
1630 goto next_slot;
1631 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001632 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001633 if (disk_bytenr == 0)
1634 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001635 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001636 if (btrfs_file_extent_compression(leaf, fi) ||
1637 btrfs_file_extent_encryption(leaf, fi) ||
1638 btrfs_file_extent_other_encoding(leaf, fi))
1639 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001640 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001641 * If extent is created before the last volume's snapshot
1642 * this implies the extent is shared, hence we can't do
1643 * nocow. This is the same check as in
1644 * btrfs_cross_ref_exist but without calling
1645 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001646 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001647 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001648 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001649 btrfs_root_last_snapshot(&root->root_item))
1650 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001651 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1652 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001653 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001654 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001655 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001656 ret = btrfs_cross_ref_exist(root, ino,
1657 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001658 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001659 if (ret) {
1660 /*
1661 * ret could be -EIO if the above fails to read
1662 * metadata.
1663 */
1664 if (ret < 0) {
1665 if (cow_start != (u64)-1)
1666 cur_offset = cow_start;
1667 goto error;
1668 }
1669
Nikolay Borisov3e024842019-08-21 10:42:03 +03001670 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001671 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001672 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001673 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001674 disk_bytenr += cur_offset - found_key.offset;
1675 num_bytes = min(end + 1, extent_end) - cur_offset;
1676 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001677 * If there are pending snapshots for this root, we
1678 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001679 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001680 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001681 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001682 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001683 * force cow if csum exists in the range.
1684 * this ensure that csum for a given extent are
1685 * either valid or do not exist.
1686 */
Liu Bo58113752018-01-31 17:09:13 -07001687 ret = csum_exist_in_range(fs_info, disk_bytenr,
1688 num_bytes);
1689 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001690 /*
1691 * ret could be -EIO if the above fails to read
1692 * metadata.
1693 */
1694 if (ret < 0) {
1695 if (cow_start != (u64)-1)
1696 cur_offset = cow_start;
1697 goto error;
1698 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001699 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001700 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001701 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001702 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001703 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001704 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001705 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001706 extent_end = found_key.offset + ram_bytes;
1707 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001708 /* Skip extents outside of our requested range */
1709 if (extent_end <= start) {
1710 path->slots[0]++;
1711 goto next_slot;
1712 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001713 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001714 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001715 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001716 }
1717out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001718 /*
1719 * If nocow is false then record the beginning of the range
1720 * that needs to be COWed
1721 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001722 if (!nocow) {
1723 if (cow_start == (u64)-1)
1724 cow_start = cur_offset;
1725 cur_offset = extent_end;
1726 if (cur_offset > end)
1727 break;
1728 path->slots[0]++;
1729 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001730 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001731
David Sterbab3b4aa72011-04-21 01:20:15 +02001732 btrfs_release_path(path);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001733
1734 /*
1735 * COW range from cow_start to found_key.offset - 1. As the key
1736 * will contain the beginning of the first extent that can be
1737 * NOCOW, following one which needs to be COW'ed
1738 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001739 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001740 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001741 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001742 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001743 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001744 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001745 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001746 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001747
Yan Zhengd899e052008-10-30 14:25:28 -04001748 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001749 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001750 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001751
Nikolay Borisov968322c2020-06-03 08:55:21 +03001752 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001753 orig_start,
1754 disk_bytenr, /* block_start */
1755 num_bytes, /* block_len */
1756 disk_num_bytes, /* orig_block_len */
1757 ram_bytes, BTRFS_COMPRESS_NONE,
1758 BTRFS_ORDERED_PREALLOC);
1759 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001760 ret = PTR_ERR(em);
1761 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001762 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001763 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001764 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001765 disk_bytenr, num_bytes,
1766 num_bytes,
1767 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001768 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001769 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001770 cur_offset + num_bytes - 1,
1771 0);
1772 goto error;
1773 }
Yan Zhengd899e052008-10-30 14:25:28 -04001774 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001775 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001776 disk_bytenr, num_bytes,
1777 num_bytes,
1778 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001779 if (ret)
1780 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001781 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001782
Filipe Mananaf78c4362016-05-09 13:15:41 +01001783 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001784 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001785 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001786
Yan, Zhengefa56462010-05-16 10:49:59 -04001787 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001788 BTRFS_DATA_RELOC_TREE_OBJECTID)
1789 /*
1790 * Error handled later, as we must prevent
1791 * extent_clear_unlock_delalloc() in error handler
1792 * from freeing metadata of created ordered extent.
1793 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001794 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001795 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001796
Nikolay Borisov968322c2020-06-03 08:55:21 +03001797 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001798 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001799 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001800 EXTENT_DELALLOC |
1801 EXTENT_CLEAR_DATA_RESV,
1802 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1803
Yan Zheng80ff3852008-10-30 14:20:02 -04001804 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001805
1806 /*
1807 * btrfs_reloc_clone_csums() error, now we're OK to call error
1808 * handler, as metadata for created ordered extent will only
1809 * be freed by btrfs_finish_ordered_io().
1810 */
1811 if (ret)
1812 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001813 if (cur_offset > end)
1814 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001815 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001816 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001817
Robbie Ko506481b2018-10-30 18:04:04 +08001818 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001819 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001820
Yan Zheng80ff3852008-10-30 14:20:02 -04001821 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001822 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001823 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1824 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001825 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001826 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001827 }
1828
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001829error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001830 if (nocow)
1831 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1832
Josef Bacik17ca04a2012-05-31 15:58:55 -04001833 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001834 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001835 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001836 EXTENT_DELALLOC | EXTENT_DEFRAG |
1837 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1838 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001839 PAGE_SET_WRITEBACK |
1840 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001841 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001842 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001843}
1844
Nikolay Borisov0c494222020-06-03 08:55:28 +03001845static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001846{
1847
Nikolay Borisov0c494222020-06-03 08:55:28 +03001848 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1849 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001850 return 0;
1851
1852 /*
1853 * @defrag_bytes is a hint value, no spinlock held here,
1854 * if is not zero, it means the file is defragging.
1855 * Force cow if given extent needs to be defragged.
1856 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001857 if (inode->defrag_bytes &&
1858 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001859 return 1;
1860
1861 return 0;
1862}
1863
Chris Masond352ac62008-09-29 15:18:18 -04001864/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001865 * Function to process delayed allocation (create CoW) for ranges which are
1866 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001867 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001868int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001869 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1870 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001871{
Chris Masonbe20aa92007-12-17 20:14:01 -05001872 int ret;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001873 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001874
Nikolay Borisov98456b92020-06-03 08:55:29 +03001875 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1876 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001877 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001878 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1879 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001880 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001881 } else if (!inode_can_compress(inode) ||
1882 !inode_need_compress(inode, start, end)) {
1883 ret = cow_file_range(inode, locked_page, start, end,
1884 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001885 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001886 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1887 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001888 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001889 }
Qu Wenruo524272602017-03-08 10:25:52 +08001890 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001891 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001892 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001893 return ret;
1894}
1895
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001896void btrfs_split_delalloc_extent(struct inode *inode,
1897 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001898{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001899 u64 size;
1900
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001901 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001902 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001903 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001904
Josef Bacikdcab6a32015-02-11 15:08:59 -05001905 size = orig->end - orig->start + 1;
1906 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001907 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001908 u64 new_size;
1909
1910 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001911 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001912 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001913 */
1914 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001915 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001916 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001917 num_extents += count_max_extents(new_size);
1918 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001919 return;
1920 }
1921
Josef Bacik9e0baf62011-07-15 15:16:44 +00001922 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001923 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001924 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001925}
1926
1927/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001928 * Handle merged delayed allocation extents so we can keep track of new extents
1929 * that are just merged onto old extents, such as when we are doing sequential
1930 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001931 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001932void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1933 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001934{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001935 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001936 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001937
Josef Bacik9ed74f22009-09-11 16:12:44 -04001938 /* not delalloc, ignore it */
1939 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001940 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001941
Josef Bacik8461a3d2015-03-13 15:12:08 -04001942 if (new->start > other->start)
1943 new_size = new->end - other->start + 1;
1944 else
1945 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001946
1947 /* we're not bigger than the max, unreserve the space and go */
1948 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1949 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001950 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001951 spin_unlock(&BTRFS_I(inode)->lock);
1952 return;
1953 }
1954
1955 /*
Josef Bacikba117212015-03-13 15:01:24 -04001956 * We have to add up either side to figure out how many extents were
1957 * accounted for before we merged into one big extent. If the number of
1958 * extents we accounted for is <= the amount we need for the new range
1959 * then we can return, otherwise drop. Think of it like this
1960 *
1961 * [ 4k][MAX_SIZE]
1962 *
1963 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1964 * need 2 outstanding extents, on one side we have 1 and the other side
1965 * we have 1 so they are == and we can return. But in this case
1966 *
1967 * [MAX_SIZE+4k][MAX_SIZE+4k]
1968 *
1969 * Each range on their own accounts for 2 extents, but merged together
1970 * they are only 3 extents worth of accounting, so we need to drop in
1971 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001972 */
Josef Bacikba117212015-03-13 15:01:24 -04001973 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001974 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001975 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001976 num_extents += count_max_extents(old_size);
1977 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001978 return;
1979
Josef Bacik9e0baf62011-07-15 15:16:44 +00001980 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001981 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001982 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001983}
1984
Miao Xieeb73c1b2013-05-15 07:48:22 +00001985static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1986 struct inode *inode)
1987{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001988 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1989
Miao Xieeb73c1b2013-05-15 07:48:22 +00001990 spin_lock(&root->delalloc_lock);
1991 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1992 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1993 &root->delalloc_inodes);
1994 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1995 &BTRFS_I(inode)->runtime_flags);
1996 root->nr_delalloc_inodes++;
1997 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001998 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001999 BUG_ON(!list_empty(&root->delalloc_root));
2000 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002001 &fs_info->delalloc_roots);
2002 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002003 }
2004 }
2005 spin_unlock(&root->delalloc_lock);
2006}
2007
Nikolay Borisov2b877332018-04-27 12:21:51 +03002008
2009void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2010 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002011{
David Sterba3ffbd682018-06-29 10:56:42 +02002012 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002013
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002014 if (!list_empty(&inode->delalloc_inodes)) {
2015 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002016 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002017 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002018 root->nr_delalloc_inodes--;
2019 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002020 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002021 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002022 BUG_ON(list_empty(&root->delalloc_root));
2023 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002024 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002025 }
2026 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002027}
2028
2029static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2030 struct btrfs_inode *inode)
2031{
2032 spin_lock(&root->delalloc_lock);
2033 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002034 spin_unlock(&root->delalloc_lock);
2035}
2036
Chris Masond352ac62008-09-29 15:18:18 -04002037/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002038 * Properly track delayed allocation bytes in the inode and to maintain the
2039 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002040 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002041void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2042 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002043{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002044 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2045
Wang Shilong47059d92014-07-03 18:22:07 +08002046 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2047 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002048 /*
2049 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002050 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002051 * bit, which is only set or cleared with irqs on
2052 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002053 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002054 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002055 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002056 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002057 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002058
Josef Bacik8b62f872017-10-19 14:15:55 -04002059 spin_lock(&BTRFS_I(inode)->lock);
2060 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2061 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002062
Josef Bacik6a3891c2015-03-16 17:38:52 -04002063 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002064 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002065 return;
2066
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002067 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2068 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002069 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002070 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002071 if (*bits & EXTENT_DEFRAG)
2072 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002073 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002074 &BTRFS_I(inode)->runtime_flags))
2075 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002076 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002077 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002078
2079 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2080 (*bits & EXTENT_DELALLOC_NEW)) {
2081 spin_lock(&BTRFS_I(inode)->lock);
2082 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2083 state->start;
2084 spin_unlock(&BTRFS_I(inode)->lock);
2085 }
Chris Mason291d6732008-01-29 15:55:23 -05002086}
2087
Chris Masond352ac62008-09-29 15:18:18 -04002088/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002089 * Once a range is no longer delalloc this function ensures that proper
2090 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002091 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002092void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2093 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002094{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002095 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2096 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002097 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002098 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002099
Filipe Manana4a4b9642017-07-27 19:52:55 +01002100 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2101 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002102 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002103 spin_unlock(&inode->lock);
2104 }
Wang Shilong47059d92014-07-03 18:22:07 +08002105
Chris Mason75eff682008-12-15 15:54:40 -05002106 /*
2107 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002108 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002109 * bit, which is only set or cleared with irqs on
2110 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002111 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002112 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002113 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002114
Josef Bacik8b62f872017-10-19 14:15:55 -04002115 spin_lock(&inode->lock);
2116 btrfs_mod_outstanding_extents(inode, -num_extents);
2117 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002118
Josef Bacikb6d08f02013-09-27 14:57:43 -04002119 /*
2120 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002121 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002122 * error.
2123 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002124 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002125 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002126 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002127
Josef Bacik6a3891c2015-03-16 17:38:52 -04002128 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002129 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002130 return;
2131
Filipe Mananaa315e682017-03-06 23:04:20 +00002132 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2133 do_list && !(state->state & EXTENT_NORESERVE) &&
2134 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002135 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002136
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002137 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2138 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002139 spin_lock(&inode->lock);
2140 inode->delalloc_bytes -= len;
2141 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002142 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002143 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002144 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002145 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002146 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002147
2148 if ((state->state & EXTENT_DELALLOC_NEW) &&
2149 (*bits & EXTENT_DELALLOC_NEW)) {
2150 spin_lock(&inode->lock);
2151 ASSERT(inode->new_delalloc_bytes >= len);
2152 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002153 if (*bits & EXTENT_ADD_INODE_BYTES)
2154 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002155 spin_unlock(&inode->lock);
2156 }
Chris Mason291d6732008-01-29 15:55:23 -05002157}
2158
Chris Masond352ac62008-09-29 15:18:18 -04002159/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002160 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2161 * in a chunk's stripe. This function ensures that bios do not span a
2162 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002163 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002164 * @page - The page we are about to add to the bio
2165 * @size - size we want to add to the bio
2166 * @bio - bio we want to ensure is smaller than a stripe
2167 * @bio_flags - flags of the bio
2168 *
2169 * return 1 if page cannot be added to the bio
2170 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002171 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002172 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002173int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2174 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002175{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002176 struct inode *inode = page->mapping->host;
2177 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Kent Overstreet4f024f32013-10-11 15:44:27 -07002178 u64 logical = (u64)bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002179 u64 length = 0;
2180 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002181 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002182 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002183
Chris Mason771ed682008-11-06 22:02:51 -05002184 if (bio_flags & EXTENT_BIO_COMPRESSED)
2185 return 0;
2186
Kent Overstreet4f024f32013-10-11 15:44:27 -07002187 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002188 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002189 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2190 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002191 if (ret < 0)
2192 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002193
2194 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002195 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002196 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002197}
2198
Chris Masond352ac62008-09-29 15:18:18 -04002199/*
2200 * in order to insert checksums into the metadata in large chunks,
2201 * we wait until bio submission time. All the pages in the bio are
2202 * checksummed and sums are attached onto the ordered extent record.
2203 *
2204 * At IO completion time the cums attached on the ordered extent record
2205 * are inserted into the btree
2206 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002207static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
2208 u64 bio_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002209{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002210 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002211}
Chris Masone0156402008-04-16 11:15:20 -04002212
Chris Mason4a69a412008-11-06 22:03:00 -05002213/*
Chris Masoncad321a2008-12-17 14:51:42 -05002214 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002215 * on write, or reading the csums from the tree before a read.
2216 *
2217 * Rules about async/sync submit,
2218 * a) read: sync submit
2219 *
2220 * b) write without checksum: sync submit
2221 *
2222 * c) write with checksum:
2223 * c-1) if bio is issued by fsync: sync submit
2224 * (sync_writers != 0)
2225 *
2226 * c-2) if root is reloc root: sync submit
2227 * (only in case of buffered IO)
2228 *
2229 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002230 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002231blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2232 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002233
Chris Mason44b8bd72008-04-16 11:14:51 -04002234{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002235 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002236 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302237 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002238 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002239 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002240 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002241
Josef Bacik42437a62020-10-16 11:29:18 -04002242 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2243 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002244
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002245 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302246 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002247
Mike Christie37226b22016-06-05 14:31:52 -05002248 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002249 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002250 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002251 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002252
Chris Masond20f7042008-12-08 16:58:54 -05002253 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002254 ret = btrfs_submit_compressed_read(inode, bio,
2255 mirror_num,
2256 bio_flags);
2257 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002258 } else {
2259 /*
2260 * Lookup bio sums does extra checks around whether we
2261 * need to csum or not, which is why we ignore skip_sum
2262 * here.
2263 */
Omar Sandovaldb72e472019-12-10 10:37:35 -08002264 ret = btrfs_lookup_bio_sums(inode, bio, (u64)-1, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002265 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002266 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002267 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002268 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002269 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002270 /* csum items have already been cloned */
2271 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2272 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002273 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002274 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2275 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002276 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002277 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002278 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002279 if (ret)
2280 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002281 }
2282
Chris Mason0b86a832008-03-24 15:01:56 -04002283mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002284 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002285
2286out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002287 if (ret) {
2288 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002289 bio_endio(bio);
2290 }
Stefan Behrens61891922012-11-05 18:51:52 +01002291 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002292}
Chris Mason6885f302008-02-20 16:11:05 -05002293
Chris Masond352ac62008-09-29 15:18:18 -04002294/*
2295 * given a list of ordered sums record them in the inode. This happens
2296 * at IO completion time based on sums calculated at bio submission time.
2297 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002298static int add_pending_csums(struct btrfs_trans_handle *trans,
2299 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002300{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002301 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002302 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002303
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002304 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002305 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002306 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002307 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002308 if (ret)
2309 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002310 }
2311 return 0;
2312}
2313
Filipe Mananac3347302020-11-04 11:07:31 +00002314static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2315 const u64 start,
2316 const u64 len,
2317 struct extent_state **cached_state)
2318{
2319 u64 search_start = start;
2320 const u64 end = start + len - 1;
2321
2322 while (search_start < end) {
2323 const u64 search_len = end - search_start + 1;
2324 struct extent_map *em;
2325 u64 em_len;
2326 int ret = 0;
2327
2328 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2329 if (IS_ERR(em))
2330 return PTR_ERR(em);
2331
2332 if (em->block_start != EXTENT_MAP_HOLE)
2333 goto next;
2334
2335 em_len = em->len;
2336 if (em->start < search_start)
2337 em_len -= search_start - em->start;
2338 if (em_len > search_len)
2339 em_len = search_len;
2340
2341 ret = set_extent_bit(&inode->io_tree, search_start,
2342 search_start + em_len - 1,
2343 EXTENT_DELALLOC_NEW,
2344 NULL, cached_state, GFP_NOFS);
2345next:
2346 search_start = extent_map_end(em);
2347 free_extent_map(em);
2348 if (ret)
2349 return ret;
2350 }
2351 return 0;
2352}
2353
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002354int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002355 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002356 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002357{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002358 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002359
2360 if (start >= i_size_read(&inode->vfs_inode) &&
2361 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2362 /*
2363 * There can't be any extents following eof in this case so just
2364 * set the delalloc new bit for the range directly.
2365 */
2366 extra_bits |= EXTENT_DELALLOC_NEW;
2367 } else {
2368 int ret;
2369
2370 ret = btrfs_find_new_delalloc_bytes(inode, start,
2371 end + 1 - start,
2372 cached_state);
2373 if (ret)
2374 return ret;
2375 }
2376
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002377 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2378 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002379}
2380
Chris Masond352ac62008-09-29 15:18:18 -04002381/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002382struct btrfs_writepage_fixup {
2383 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002384 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002385 struct btrfs_work work;
2386};
2387
Christoph Hellwigb2950862008-12-02 09:54:17 -05002388static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002389{
2390 struct btrfs_writepage_fixup *fixup;
2391 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002392 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002393 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002394 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002395 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002396 u64 page_start;
2397 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002398 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002399 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002400
2401 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2402 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002403 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002404 page_start = page_offset(page);
2405 page_end = page_offset(page) + PAGE_SIZE - 1;
2406
2407 /*
2408 * This is similar to page_mkwrite, we need to reserve the space before
2409 * we take the page lock.
2410 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002411 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2412 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002413again:
Chris Mason247e7432008-07-17 12:53:51 -04002414 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002415
Chris Mason25f3c502020-01-21 11:51:42 -05002416 /*
2417 * Before we queued this fixup, we took a reference on the page.
2418 * page->mapping may go NULL, but it shouldn't be moved to a different
2419 * address space.
2420 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002421 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2422 /*
2423 * Unfortunately this is a little tricky, either
2424 *
2425 * 1) We got here and our page had already been dealt with and
2426 * we reserved our space, thus ret == 0, so we need to just
2427 * drop our space reservation and bail. This can happen the
2428 * first time we come into the fixup worker, or could happen
2429 * while waiting for the ordered extent.
2430 * 2) Our page was already dealt with, but we happened to get an
2431 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2432 * this case we obviously don't have anything to release, but
2433 * because the page was already dealt with we don't want to
2434 * mark the page with an error, so make sure we're resetting
2435 * ret to 0. This is why we have this check _before_ the ret
2436 * check, because we do not want to have a surprise ENOSPC
2437 * when the page was already properly dealt with.
2438 */
2439 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002440 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2441 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002442 page_start, PAGE_SIZE,
2443 true);
2444 }
2445 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002446 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002447 }
Chris Mason25f3c502020-01-21 11:51:42 -05002448
2449 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002450 * We can't mess with the page state unless it is locked, so now that
2451 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002452 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002453 if (ret)
2454 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002455
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002456 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002457
2458 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002459 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002460 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002461
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002462 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002463 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002464 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2465 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002466 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002467 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002468 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002469 goto again;
2470 }
Chris Mason247e7432008-07-17 12:53:51 -04002471
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002472 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002473 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002474 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002475 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002476
Chris Mason25f3c502020-01-21 11:51:42 -05002477 /*
2478 * Everything went as planned, we're now the owner of a dirty page with
2479 * delayed allocation bits set and space reserved for our COW
2480 * destination.
2481 *
2482 * The page was dirty when we started, nothing should have cleaned it.
2483 */
2484 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002485 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002486out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002487 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002488 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002489 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2490 PAGE_SIZE, true);
2491 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002492 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002493out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002494 if (ret) {
2495 /*
2496 * We hit ENOSPC or other errors. Update the mapping and page
2497 * to reflect the errors and clean the page.
2498 */
2499 mapping_set_error(page->mapping, ret);
2500 end_extent_writepage(page, ret, page_start, page_end);
2501 clear_page_dirty_for_io(page);
2502 SetPageError(page);
2503 }
2504 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002505 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002506 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002507 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002508 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002509 /*
2510 * As a precaution, do a delayed iput in case it would be the last iput
2511 * that could need flushing space. Recursing back to fixup worker would
2512 * deadlock.
2513 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002514 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002515}
2516
2517/*
2518 * There are a few paths in the higher layers of the kernel that directly
2519 * set the page dirty bit without asking the filesystem if it is a
2520 * good idea. This causes problems because we want to make sure COW
2521 * properly happens and the data=ordered rules are followed.
2522 *
Chris Masonc8b97812008-10-29 14:49:59 -04002523 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002524 * hasn't been properly setup for IO. We kick off an async process
2525 * to fix it up. The async helper will wait for ordered extents, set
2526 * the delalloc bit and make it safe to write the page.
2527 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002528int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002529{
2530 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002531 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002532 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002533
Chris Mason8b62b722009-09-02 16:53:46 -04002534 /* this page is properly in the ordered list */
2535 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002536 return 0;
2537
Chris Mason25f3c502020-01-21 11:51:42 -05002538 /*
2539 * PageChecked is set below when we create a fixup worker for this page,
2540 * don't try to create another one if we're already PageChecked()
2541 *
2542 * The extent_io writepage code will redirty the page if we send back
2543 * EAGAIN.
2544 */
Chris Mason247e7432008-07-17 12:53:51 -04002545 if (PageChecked(page))
2546 return -EAGAIN;
2547
2548 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2549 if (!fixup)
2550 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002551
Josef Bacikf4b13632020-01-21 14:34:52 -05002552 /*
2553 * We are already holding a reference to this inode from
2554 * write_cache_pages. We need to hold it because the space reservation
2555 * takes place outside of the page lock, and we can't trust
2556 * page->mapping outside of the page lock.
2557 */
2558 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002559 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002560 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002561 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002562 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002563 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002564 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002565
2566 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002567}
2568
Yan Zhengd899e052008-10-30 14:25:28 -04002569static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002570 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002571 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002572 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002573 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002574{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002575 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002576 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002577 struct btrfs_path *path;
2578 struct extent_buffer *leaf;
2579 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002580 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2581 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2582 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2583 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002584 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002585 int ret;
2586
2587 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002588 if (!path)
2589 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002590
Chris Masona1ed8352009-09-11 12:27:37 -04002591 /*
2592 * we may be replacing one extent in the tree with another.
2593 * The new extent is pinned in the extent map, and we don't want
2594 * to drop it from the cache until it is completely in the btree.
2595 *
2596 * So, tell btrfs_drop_extents to leave this extent in the cache.
2597 * the caller is expected to unpin it and allow it to be merged
2598 * with the others.
2599 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002600 drop_args.path = path;
2601 drop_args.start = file_pos;
2602 drop_args.end = file_pos + num_bytes;
2603 drop_args.replace_extent = true;
2604 drop_args.extent_item_size = sizeof(*stack_fi);
2605 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002606 if (ret)
2607 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002608
Filipe Manana5893dfb2020-11-04 11:07:32 +00002609 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002610 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002611 ins.offset = file_pos;
2612 ins.type = BTRFS_EXTENT_DATA_KEY;
2613
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002614 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002615 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002616 if (ret)
2617 goto out;
2618 }
Yan Zhengd899e052008-10-30 14:25:28 -04002619 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002620 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2621 write_extent_buffer(leaf, stack_fi,
2622 btrfs_item_ptr_offset(leaf, path->slots[0]),
2623 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002624
Yan Zhengd899e052008-10-30 14:25:28 -04002625 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002626 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002627
Filipe Manana2766ff62020-11-04 11:07:34 +00002628 /*
2629 * If we dropped an inline extent here, we know the range where it is
2630 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2631 * number of bytes only for that range contaning the inline extent.
2632 * The remaining of the range will be processed when clearning the
2633 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2634 */
2635 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2636 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2637
2638 inline_size = drop_args.bytes_found - inline_size;
2639 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2640 drop_args.bytes_found -= inline_size;
2641 num_bytes -= sectorsize;
2642 }
2643
2644 if (update_inode_bytes)
2645 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002646
2647 ins.objectid = disk_bytenr;
2648 ins.offset = disk_num_bytes;
2649 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002650
Nikolay Borisovc553f942020-06-03 08:55:19 +03002651 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002652 if (ret)
2653 goto out;
2654
Nikolay Borisovc553f942020-06-03 08:55:19 +03002655 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002656 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002657out:
Yan Zhengd899e052008-10-30 14:25:28 -04002658 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002659
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002660 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002661}
2662
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002663static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002664 u64 start, u64 len)
2665{
David Sterba32da53862019-10-29 19:20:18 +01002666 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002667
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002668 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002669 ASSERT(cache);
2670
2671 spin_lock(&cache->lock);
2672 cache->delalloc_bytes -= len;
2673 spin_unlock(&cache->lock);
2674
2675 btrfs_put_block_group(cache);
2676}
2677
Qu Wenruo203f44c52020-06-10 09:04:40 +08002678static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002679 struct btrfs_ordered_extent *oe)
2680{
2681 struct btrfs_file_extent_item stack_fi;
2682 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002683 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002684
2685 memset(&stack_fi, 0, sizeof(stack_fi));
2686 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2687 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2688 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2689 oe->disk_num_bytes);
2690 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2691 logical_len = oe->truncated_len;
2692 else
2693 logical_len = oe->num_bytes;
2694 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2695 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2696 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2697 /* Encryption and other encoding is reserved and all 0 */
2698
Filipe Manana2766ff62020-11-04 11:07:34 +00002699 /*
2700 * For delalloc, when completing an ordered extent we update the inode's
2701 * bytes when clearing the range in the inode's io tree, so pass false
2702 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2703 * except if the ordered extent was truncated.
2704 */
2705 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2706 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2707
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002708 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2709 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002710 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002711}
2712
2713/*
2714 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002715 * an ordered extent if the range of bytes in the file it covers are
2716 * fully written.
2717 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002718static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002719{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002720 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2721 struct btrfs_root *root = inode->root;
2722 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002723 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002724 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002725 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002726 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002727 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002728 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002729 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002730 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002731 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002732 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002733 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002734
Omar Sandovalbffe6332019-12-02 17:34:19 -08002735 start = ordered_extent->file_offset;
2736 end = start + ordered_extent->num_bytes - 1;
2737
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002738 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2739 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2740 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00002741 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002742
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002743 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002744
Josef Bacik5fd02042012-05-02 14:00:54 -04002745 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2746 ret = -EIO;
2747 goto out;
2748 }
2749
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002750 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08002751
Josef Bacik77cef2e2013-08-29 13:57:21 -04002752 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2753 truncated = true;
2754 logical_len = ordered_extent->truncated_len;
2755 /* Truncated the entire extent, don't bother adding */
2756 if (!logical_len)
2757 goto out;
2758 }
2759
Yan, Zhengc2167752009-11-12 09:34:21 +00002760 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002761 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002762
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002763 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002764 if (freespace_inode)
2765 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002766 else
2767 trans = btrfs_join_transaction(root);
2768 if (IS_ERR(trans)) {
2769 ret = PTR_ERR(trans);
2770 trans = NULL;
2771 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002772 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002773 trans->block_rsv = &inode->block_rsv;
2774 ret = btrfs_update_inode_fallback(trans, root, &inode->vfs_inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002775 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002776 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002777 goto out;
2778 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002779
Filipe Manana2766ff62020-11-04 11:07:34 +00002780 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002781 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002782
Nikolay Borisov8d510122019-10-08 20:43:06 +03002783 if (freespace_inode)
2784 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002785 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002786 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002787 if (IS_ERR(trans)) {
2788 ret = PTR_ERR(trans);
2789 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002790 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002791 }
Chris Masona79b7d42014-05-22 16:18:52 -07002792
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002793 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002794
Chris Masonc8b97812008-10-29 14:49:59 -04002795 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002796 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002797 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002798 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002799 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002800 ordered_extent->file_offset,
2801 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002802 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002803 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002804 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002805 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002806 if (!ret) {
2807 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002808 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002809 ordered_extent->disk_bytenr,
2810 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002811 }
Yan Zhengd899e052008-10-30 14:25:28 -04002812 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002813 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002814 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002815 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002816 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002817 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002818 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002819
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002820 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002821 if (ret) {
2822 btrfs_abort_transaction(trans, ret);
2823 goto out;
2824 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002825
Filipe Manana2766ff62020-11-04 11:07:34 +00002826 /*
2827 * If this is a new delalloc range, clear its new delalloc flag to
2828 * update the inode's number of bytes. This needs to be done first
2829 * before updating the inode item.
2830 */
2831 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
2832 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002833 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00002834 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
2835 0, 0, &cached_state);
2836
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002837 btrfs_inode_safe_disk_i_size_write(inode, 0);
2838 ret = btrfs_update_inode_fallback(trans, root, &inode->vfs_inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002839 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002840 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002841 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002842 }
2843 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002844out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002845 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002846 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002847 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002848
Miao Xiea698d0752012-09-20 01:51:59 -06002849 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002850 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002851
Josef Bacik77cef2e2013-08-29 13:57:21 -04002852 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002853 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002854
2855 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002856 unwritten_start += logical_len;
2857 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002858
2859 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002860 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002861
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002862 /*
2863 * If the ordered extent had an IOERR or something else went
2864 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002865 * back to the allocator. We only free the extent in the
2866 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002867 *
2868 * If we made it past insert_reserved_file_extent before we
2869 * errored out then we don't need to do this as the accounting
2870 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002871 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002872 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002873 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002874 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002875 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2876 /*
2877 * Discard the range before returning it back to the
2878 * free space pool
2879 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002880 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002881 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002882 ordered_extent->disk_bytenr,
2883 ordered_extent->disk_num_bytes,
2884 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002885 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002886 ordered_extent->disk_bytenr,
2887 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002888 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002889 }
2890
Josef Bacik5fd02042012-05-02 14:00:54 -04002891 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002892 * This needs to be done to make sure anybody waiting knows we are done
2893 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002894 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002895 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04002896
Chris Masone6dcd2d2008-07-17 12:53:50 -04002897 /* once for us */
2898 btrfs_put_ordered_extent(ordered_extent);
2899 /* once for the tree */
2900 btrfs_put_ordered_extent(ordered_extent);
2901
Josef Bacik5fd02042012-05-02 14:00:54 -04002902 return ret;
2903}
2904
2905static void finish_ordered_fn(struct btrfs_work *work)
2906{
2907 struct btrfs_ordered_extent *ordered_extent;
2908 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2909 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002910}
2911
Nikolay Borisovc6297322018-11-08 10:18:08 +02002912void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2913 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002914{
Nikolay Borisov3347c482020-08-31 14:42:44 +03002915 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2916 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04002917 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002918 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002919
liubo1abe9b82011-03-24 11:18:59 +00002920 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2921
Chris Mason8b62b722009-09-02 16:53:46 -04002922 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03002923 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2924 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002925 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002926
Nikolay Borisov3347c482020-08-31 14:42:44 +03002927 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002928 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002929 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002930 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002931
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002932 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002933 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002934}
2935
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002936/*
2937 * check_data_csum - verify checksum of one sector of uncompressed data
2938 * @inode: the inode
2939 * @io_bio: btrfs_io_bio which contains the csum
2940 * @icsum: checksum index in the io_bio->csum array, size of csum_size
2941 * @page: page where is the data to be verified
2942 * @pgoff: offset inside the page
2943 *
2944 * The length of such check is always one sector size.
2945 */
Omar Sandoval47df7762020-04-16 14:46:17 -07002946static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002947 int icsum, struct page *page, int pgoff)
Miao Xiedc380ae2014-09-12 18:43:55 +08002948{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002949 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2950 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002951 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002952 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02002953 const u32 csum_size = fs_info->csum_size;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002954 u8 *csum_expected;
2955 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002956
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002957 ASSERT(pgoff + len <= PAGE_SIZE);
2958
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002959 csum_expected = ((u8 *)io_bio->csum) + icsum * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002960
2961 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002962 shash->tfm = fs_info->csum_shash;
2963
Eric Biggersfd080012020-04-30 23:51:59 -07002964 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002965
2966 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002967 goto zeroit;
2968
2969 kunmap_atomic(kaddr);
2970 return 0;
2971zeroit:
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002972 btrfs_print_data_csum_error(BTRFS_I(inode), page_offset(page) + pgoff,
2973 csum, csum_expected, io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03002974 if (io_bio->device)
2975 btrfs_dev_stat_inc_and_print(io_bio->device,
2976 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08002977 memset(kaddr + pgoff, 1, len);
2978 flush_dcache_page(page);
2979 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002980 return -EIO;
2981}
2982
Chris Masond352ac62008-09-29 15:18:18 -04002983/*
Chris Masond352ac62008-09-29 15:18:18 -04002984 * when reads are done, we need to check csums to verify the data is correct
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002985 * if there's a match, we allow the bio to finish. If not, the code in
2986 * extent_io.c will try to find good copies for us.
Chris Masond352ac62008-09-29 15:18:18 -04002987 */
Nikolay Borisov9a446d62020-09-18 16:34:33 +03002988int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u64 phy_offset,
2989 struct page *page, u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04002990{
Miao Xie4eee4fa2012-12-21 09:17:45 +00002991 size_t offset = start - page_offset(page);
Chris Mason07157aa2007-08-30 08:50:51 -04002992 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05002993 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04002994 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002995
Chris Masond20f7042008-12-08 16:58:54 -05002996 if (PageChecked(page)) {
2997 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08002998 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05002999 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003000
3001 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003002 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003003
Josef Bacik42437a62020-10-16 11:29:18 -04003004 if (!root->fs_info->csum_root)
3005 return 0;
3006
Yan Zheng17d217f2008-12-12 10:03:38 -05003007 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003008 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003009 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003010 return 0;
3011 }
3012
David Sterba265fdfa2020-07-01 21:19:09 +02003013 phy_offset >>= root->fs_info->sectorsize_bits;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08003014 return check_data_csum(inode, io_bio, phy_offset, page, offset);
Chris Mason07157aa2007-08-30 08:50:51 -04003015}
Chris Masonb888db22007-08-27 16:49:44 -04003016
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003017/*
3018 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3019 *
3020 * @inode: The inode we want to perform iput on
3021 *
3022 * This function uses the generic vfs_inode::i_count to track whether we should
3023 * just decrement it (in case it's > 1) or if this is the last iput then link
3024 * the inode to the delayed iput machinery. Delayed iputs are processed at
3025 * transaction commit time/superblock commit/cleaner kthread.
3026 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003027void btrfs_add_delayed_iput(struct inode *inode)
3028{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003029 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003030 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003031
3032 if (atomic_add_unless(&inode->i_count, -1, 1))
3033 return;
3034
Josef Bacik034f7842018-12-03 11:06:52 -05003035 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003036 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003037 ASSERT(list_empty(&binode->delayed_iput));
3038 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003039 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003040 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3041 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003042}
3043
Josef Bacik63611e72019-06-18 10:59:18 -04003044static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3045 struct btrfs_inode *inode)
3046{
3047 list_del_init(&inode->delayed_iput);
3048 spin_unlock(&fs_info->delayed_iput_lock);
3049 iput(&inode->vfs_inode);
3050 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3051 wake_up(&fs_info->delayed_iputs_wait);
3052 spin_lock(&fs_info->delayed_iput_lock);
3053}
3054
3055static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3056 struct btrfs_inode *inode)
3057{
3058 if (!list_empty(&inode->delayed_iput)) {
3059 spin_lock(&fs_info->delayed_iput_lock);
3060 if (!list_empty(&inode->delayed_iput))
3061 run_delayed_iput_locked(fs_info, inode);
3062 spin_unlock(&fs_info->delayed_iput_lock);
3063 }
3064}
3065
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003066void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003067{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003068
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003069 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003070 while (!list_empty(&fs_info->delayed_iputs)) {
3071 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003072
David Sterba8089fe62015-11-19 14:15:51 +01003073 inode = list_first_entry(&fs_info->delayed_iputs,
3074 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003075 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003076 }
David Sterba8089fe62015-11-19 14:15:51 +01003077 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003078}
3079
Josef Bacik034f7842018-12-03 11:06:52 -05003080/**
3081 * btrfs_wait_on_delayed_iputs - wait on the delayed iputs to be done running
3082 * @fs_info - the fs_info for this fs
3083 * @return - EINTR if we were killed, 0 if nothing's pending
3084 *
3085 * This will wait on any delayed iputs that are currently running with KILLABLE
3086 * set. Once they are all done running we will return, unless we are killed in
3087 * which case we return EINTR. This helps in user operations like fallocate etc
3088 * that might get blocked on the iputs.
3089 */
3090int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3091{
3092 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3093 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3094 if (ret)
3095 return -EINTR;
3096 return 0;
3097}
3098
Yan, Zhengd68fc572010-05-16 10:49:58 -04003099/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003100 * This creates an orphan entry for the given inode in case something goes wrong
3101 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003102 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003103int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003104 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003105{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003106 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003107
Omar Sandoval27919062018-05-11 13:13:37 -07003108 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3109 if (ret && ret != -EEXIST) {
3110 btrfs_abort_transaction(trans, ret);
3111 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003112 }
3113
Yan, Zhengd68fc572010-05-16 10:49:58 -04003114 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003115}
3116
3117/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003118 * We have done the delete so we can go ahead and remove the orphan item for
3119 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003120 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003121static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003122 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003123{
Omar Sandoval27919062018-05-11 13:13:37 -07003124 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003125}
3126
3127/*
3128 * this cleans up any orphans that may be left on the list from the last use
3129 * of this root.
3130 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003131int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003132{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003133 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003134 struct btrfs_path *path;
3135 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003136 struct btrfs_key key, found_key;
3137 struct btrfs_trans_handle *trans;
3138 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003139 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003140 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003141
Yan, Zhengd68fc572010-05-16 10:49:58 -04003142 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003143 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003144
3145 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003146 if (!path) {
3147 ret = -ENOMEM;
3148 goto out;
3149 }
David Sterbae4058b52015-11-27 16:31:35 +01003150 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003151
3152 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003153 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003154 key.offset = (u64)-1;
3155
Josef Bacik7b128762008-07-24 12:17:14 -04003156 while (1) {
3157 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003158 if (ret < 0)
3159 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003160
3161 /*
3162 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003163 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003164 * find the key and see if we have stuff that matches
3165 */
3166 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003167 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003168 if (path->slots[0] == 0)
3169 break;
3170 path->slots[0]--;
3171 }
3172
3173 /* pull out the item */
3174 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003175 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3176
3177 /* make sure the item matches what we want */
3178 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3179 break;
David Sterba962a2982014-06-04 18:41:45 +02003180 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003181 break;
3182
3183 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003184 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003185
3186 /*
3187 * this is where we are basically btrfs_lookup, without the
3188 * crossing root thing. we store the inode number in the
3189 * offset of the orphan item.
3190 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003191
3192 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003193 btrfs_err(fs_info,
3194 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003195 ret = -EINVAL;
3196 goto out;
3197 }
3198
3199 last_objectid = found_key.offset;
3200
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003201 found_key.objectid = found_key.offset;
3202 found_key.type = BTRFS_INODE_ITEM_KEY;
3203 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003204 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303205 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003206 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003207 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003208
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003209 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003210 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003211 int is_dead_root = 0;
3212
3213 /*
3214 * this is an orphan in the tree root. Currently these
3215 * could come from 2 sources:
3216 * a) a snapshot deletion in progress
3217 * b) a free space cache inode
3218 * We need to distinguish those two, as the snapshot
3219 * orphan must not get deleted.
3220 * find_dead_roots already ran before us, so if this
3221 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003222 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003223 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003224
3225 spin_lock(&fs_info->fs_roots_radix_lock);
3226 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3227 (unsigned long)found_key.objectid);
3228 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3229 is_dead_root = 1;
3230 spin_unlock(&fs_info->fs_roots_radix_lock);
3231
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003232 if (is_dead_root) {
3233 /* prevent this orphan from being found again */
3234 key.offset = found_key.objectid - 1;
3235 continue;
3236 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003237
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003238 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003239
Josef Bacika8c9e572011-09-21 16:55:59 -04003240 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003241 * If we have an inode with links, there are a couple of
3242 * possibilities. Old kernels (before v3.12) used to create an
3243 * orphan item for truncate indicating that there were possibly
3244 * extent items past i_size that needed to be deleted. In v3.12,
3245 * truncate was changed to update i_size in sync with the extent
3246 * items, but the (useless) orphan item was still created. Since
3247 * v4.18, we don't create the orphan item for truncate at all.
3248 *
3249 * So, this item could mean that we need to do a truncate, but
3250 * only if this filesystem was last used on a pre-v3.12 kernel
3251 * and was not cleanly unmounted. The odds of that are quite
3252 * slim, and it's a pain to do the truncate now, so just delete
3253 * the orphan item.
3254 *
3255 * It's also possible that this orphan item was supposed to be
3256 * deleted but wasn't. The inode number may have been reused,
3257 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003258 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003259 if (ret == -ENOENT || inode->i_nlink) {
3260 if (!ret)
3261 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003262 trans = btrfs_start_transaction(root, 1);
3263 if (IS_ERR(trans)) {
3264 ret = PTR_ERR(trans);
3265 goto out;
3266 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003267 btrfs_debug(fs_info, "auto deleting %Lu",
3268 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003269 ret = btrfs_del_orphan_item(trans, root,
3270 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003271 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003272 if (ret)
3273 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003274 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003275 }
Josef Bacik7b128762008-07-24 12:17:14 -04003276
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003277 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003278
3279 /* this will do delete_inode and everything for us */
3280 iput(inode);
3281 }
Miao Xie3254c872011-11-10 20:45:05 -05003282 /* release the path since we're done with it */
3283 btrfs_release_path(path);
3284
Yan, Zhengd68fc572010-05-16 10:49:58 -04003285 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3286
Omar Sandovala575cee2018-05-11 13:13:38 -07003287 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003288 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003289 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003290 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003291 }
Josef Bacik7b128762008-07-24 12:17:14 -04003292
3293 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003294 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003295
3296out:
3297 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003298 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003299 btrfs_free_path(path);
3300 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003301}
3302
Chris Masond352ac62008-09-29 15:18:18 -04003303/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003304 * very simple check to peek ahead in the leaf looking for xattrs. If we
3305 * don't find any xattrs, we know there can't be any acls.
3306 *
3307 * slot is the slot the inode is in, objectid is the objectid of the inode
3308 */
3309static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003310 int slot, u64 objectid,
3311 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003312{
3313 u32 nritems = btrfs_header_nritems(leaf);
3314 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003315 static u64 xattr_access = 0;
3316 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003317 int scanned = 0;
3318
Josef Bacikf23b5a52013-06-19 10:16:26 -04003319 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003320 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3321 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3322 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3323 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003324 }
3325
Chris Mason46a53cc2009-04-27 11:47:50 -04003326 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003327 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003328 while (slot < nritems) {
3329 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3330
3331 /* we found a different objectid, there must not be acls */
3332 if (found_key.objectid != objectid)
3333 return 0;
3334
3335 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003336 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003337 if (*first_xattr_slot == -1)
3338 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003339 if (found_key.offset == xattr_access ||
3340 found_key.offset == xattr_default)
3341 return 1;
3342 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003343
3344 /*
3345 * we found a key greater than an xattr key, there can't
3346 * be any acls later on
3347 */
3348 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3349 return 0;
3350
3351 slot++;
3352 scanned++;
3353
3354 /*
3355 * it goes inode, inode backrefs, xattrs, extents,
3356 * so if there are a ton of hard links to an inode there can
3357 * be a lot of backrefs. Don't waste time searching too hard,
3358 * this is just an optimization
3359 */
3360 if (scanned >= 8)
3361 break;
3362 }
3363 /* we hit the end of the leaf before we found an xattr or
3364 * something larger than an xattr. We have to assume the inode
3365 * has acls
3366 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003367 if (*first_xattr_slot == -1)
3368 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003369 return 1;
3370}
3371
3372/*
Chris Masond352ac62008-09-29 15:18:18 -04003373 * read an inode from the btree into the in-memory inode
3374 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003375static int btrfs_read_locked_inode(struct inode *inode,
3376 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003377{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003378 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003379 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003380 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003381 struct btrfs_inode_item *inode_item;
3382 struct btrfs_root *root = BTRFS_I(inode)->root;
3383 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003384 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003385 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003386 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003387 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003388 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003389 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003390
3391 ret = btrfs_fill_inode(inode, &rdev);
3392 if (!ret)
3393 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003394
Filipe Manana4222ea72018-10-24 10:13:03 +01003395 if (!path) {
3396 path = btrfs_alloc_path();
3397 if (!path)
3398 return -ENOMEM;
3399 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003400
Chris Mason39279cc2007-06-12 06:35:45 -04003401 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003402
Chris Mason39279cc2007-06-12 06:35:45 -04003403 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003404 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003405 if (path != in_path)
3406 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003407 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003408 }
Chris Mason39279cc2007-06-12 06:35:45 -04003409
Chris Mason5f39d392007-10-15 16:14:19 -04003410 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003411
3412 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003413 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003414
Chris Mason5f39d392007-10-15 16:14:19 -04003415 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3416 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003417 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003418 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003419 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3420 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003421 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003422 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3423 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003424
David Sterbaa937b972014-12-12 17:39:12 +01003425 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3426 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003427
David Sterbaa937b972014-12-12 17:39:12 +01003428 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3429 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003430
David Sterbaa937b972014-12-12 17:39:12 +01003431 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3432 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003433
chandan r9cc97d62012-07-04 12:48:07 +05303434 BTRFS_I(inode)->i_otime.tv_sec =
3435 btrfs_timespec_sec(leaf, &inode_item->otime);
3436 BTRFS_I(inode)->i_otime.tv_nsec =
3437 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003438
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003439 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003440 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003441 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3442
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003443 inode_set_iversion_queried(inode,
3444 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003445 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003446 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003447 rdev = btrfs_inode_rdev(leaf, inode_item);
3448
Josef Bacikaec74772008-07-24 12:12:38 -04003449 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003450 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003451
3452cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003453 /*
3454 * If we were modified in the current generation and evicted from memory
3455 * and then re-read we need to do a full sync since we don't have any
3456 * idea about which extents were modified before we were evicted from
3457 * cache.
3458 *
3459 * This is required for both inode re-read from disk and delayed inode
3460 * in delayed_nodes_tree.
3461 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003462 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003463 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3464 &BTRFS_I(inode)->runtime_flags);
3465
Filipe Mananabde6c242015-07-24 00:00:19 +01003466 /*
3467 * We don't persist the id of the transaction where an unlink operation
3468 * against the inode was last made. So here we assume the inode might
3469 * have been evicted, and therefore the exact value of last_unlink_trans
3470 * lost, and set it to last_trans to avoid metadata inconsistencies
3471 * between the inode and its parent if the inode is fsync'ed and the log
3472 * replayed. For example, in the scenario:
3473 *
3474 * touch mydir/foo
3475 * ln mydir/foo mydir/bar
3476 * sync
3477 * unlink mydir/bar
3478 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3479 * xfs_io -c fsync mydir/foo
3480 * <power failure>
3481 * mount fs, triggers fsync log replay
3482 *
3483 * We must make sure that when we fsync our inode foo we also log its
3484 * parent inode, otherwise after log replay the parent still has the
3485 * dentry with the "bar" name but our inode foo has a link count of 1
3486 * and doesn't have an inode ref with the name "bar" anymore.
3487 *
3488 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003489 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003490 * transaction commits on fsync if our inode is a directory, or if our
3491 * inode is not a directory, logging its parent unnecessarily.
3492 */
3493 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3494
Filipe Manana3ebac172020-07-15 12:30:43 +01003495 /*
3496 * Same logic as for last_unlink_trans. We don't persist the generation
3497 * of the last transaction where this inode was used for a reflink
3498 * operation, so after eviction and reloading the inode we must be
3499 * pessimistic and assume the last transaction that modified the inode.
3500 */
3501 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3502
Miao Xie67de1172013-12-26 13:07:06 +08003503 path->slots[0]++;
3504 if (inode->i_nlink != 1 ||
3505 path->slots[0] >= btrfs_header_nritems(leaf))
3506 goto cache_acl;
3507
3508 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003509 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003510 goto cache_acl;
3511
3512 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3513 if (location.type == BTRFS_INODE_REF_KEY) {
3514 struct btrfs_inode_ref *ref;
3515
3516 ref = (struct btrfs_inode_ref *)ptr;
3517 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3518 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3519 struct btrfs_inode_extref *extref;
3520
3521 extref = (struct btrfs_inode_extref *)ptr;
3522 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3523 extref);
3524 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003525cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003526 /*
3527 * try to precache a NULL acl entry for files that don't have
3528 * any xattrs or acls
3529 */
Li Zefan33345d012011-04-20 10:31:50 +08003530 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003531 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003532 if (first_xattr_slot != -1) {
3533 path->slots[0] = first_xattr_slot;
3534 ret = btrfs_load_inode_props(inode, path);
3535 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003536 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003537 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003538 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003539 root->root_key.objectid, ret);
3540 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003541 if (path != in_path)
3542 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003543
Al Viro72c04902009-06-24 16:58:48 -04003544 if (!maybe_acls)
3545 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003546
Chris Mason39279cc2007-06-12 06:35:45 -04003547 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003548 case S_IFREG:
3549 inode->i_mapping->a_ops = &btrfs_aops;
3550 inode->i_fop = &btrfs_file_operations;
3551 inode->i_op = &btrfs_file_inode_operations;
3552 break;
3553 case S_IFDIR:
3554 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003555 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003556 break;
3557 case S_IFLNK:
3558 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003559 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003560 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003561 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003562 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003563 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003564 init_special_inode(inode, inode->i_mode, rdev);
3565 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003566 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003567
David Sterba7b6a2212018-03-26 18:40:21 +02003568 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003569 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003570}
3571
Chris Masond352ac62008-09-29 15:18:18 -04003572/*
3573 * given a leaf and an inode, copy the inode fields into the leaf
3574 */
Chris Masone02119d2008-09-05 16:13:11 -04003575static void fill_inode_item(struct btrfs_trans_handle *trans,
3576 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003577 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003578 struct inode *inode)
3579{
Liu Bo51fab692012-12-27 09:01:21 +00003580 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003581
David Sterbac82f8232019-08-09 17:48:21 +02003582 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003583
David Sterbacc4c13d2020-04-29 02:15:56 +02003584 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3585 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3586 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3587 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3588 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003589
David Sterbacc4c13d2020-04-29 02:15:56 +02003590 btrfs_set_token_timespec_sec(&token, &item->atime,
3591 inode->i_atime.tv_sec);
3592 btrfs_set_token_timespec_nsec(&token, &item->atime,
3593 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003594
David Sterbacc4c13d2020-04-29 02:15:56 +02003595 btrfs_set_token_timespec_sec(&token, &item->mtime,
3596 inode->i_mtime.tv_sec);
3597 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3598 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003599
David Sterbacc4c13d2020-04-29 02:15:56 +02003600 btrfs_set_token_timespec_sec(&token, &item->ctime,
3601 inode->i_ctime.tv_sec);
3602 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3603 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003604
David Sterbacc4c13d2020-04-29 02:15:56 +02003605 btrfs_set_token_timespec_sec(&token, &item->otime,
3606 BTRFS_I(inode)->i_otime.tv_sec);
3607 btrfs_set_token_timespec_nsec(&token, &item->otime,
3608 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303609
David Sterbacc4c13d2020-04-29 02:15:56 +02003610 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3611 btrfs_set_token_inode_generation(&token, item,
3612 BTRFS_I(inode)->generation);
3613 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3614 btrfs_set_token_inode_transid(&token, item, trans->transid);
3615 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3616 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3617 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003618}
3619
Chris Masond352ac62008-09-29 15:18:18 -04003620/*
3621 * copy everything in the in-memory inode into the btree.
3622 */
Chris Mason21151332011-11-10 20:39:08 -05003623static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003624 struct btrfs_root *root,
3625 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003626{
3627 struct btrfs_inode_item *inode_item;
3628 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003629 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003630 int ret;
3631
3632 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003633 if (!path)
3634 return -ENOMEM;
3635
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003636 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003637 if (ret) {
3638 if (ret > 0)
3639 ret = -ENOENT;
3640 goto failed;
3641 }
3642
Chris Mason5f39d392007-10-15 16:14:19 -04003643 leaf = path->nodes[0];
3644 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003645 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003646
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003647 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003648 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003649 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003650 ret = 0;
3651failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003652 btrfs_free_path(path);
3653 return ret;
3654}
3655
Chris Masond352ac62008-09-29 15:18:18 -04003656/*
Chris Mason21151332011-11-10 20:39:08 -05003657 * copy everything in the in-memory inode into the btree.
3658 */
3659noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003660 struct btrfs_root *root,
3661 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003662{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003663 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003664 int ret;
3665
3666 /*
3667 * If the inode is a free space inode, we can deadlock during commit
3668 * if we put it into the delayed code.
3669 *
3670 * The data relocation inode should also be directly updated
3671 * without delay
3672 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003673 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003674 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003675 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003676 btrfs_update_root_times(trans, root);
3677
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003678 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003679 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003680 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05003681 return ret;
3682 }
3683
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003684 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003685}
3686
Josef Bacikbe6aef62012-10-22 15:43:12 -04003687noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3688 struct btrfs_root *root,
3689 struct inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003690{
3691 int ret;
3692
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003693 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason21151332011-11-10 20:39:08 -05003694 if (ret == -ENOSPC)
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003695 return btrfs_update_inode_item(trans, root, BTRFS_I(inode));
Chris Mason21151332011-11-10 20:39:08 -05003696 return ret;
3697}
3698
3699/*
Chris Masond352ac62008-09-29 15:18:18 -04003700 * unlink helper that gets used here in inode.c and in the tree logging
3701 * recovery code. It remove a link in a directory with a given name, and
3702 * also drops the back refs in the inode to the directory
3703 */
Al Viro92986792011-03-04 17:14:37 +00003704static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3705 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003706 struct btrfs_inode *dir,
3707 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003708 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003709{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003710 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003711 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003712 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003713 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003714 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003715 u64 ino = btrfs_ino(inode);
3716 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003717
3718 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003719 if (!path) {
3720 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003721 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003722 }
3723
Li Zefan33345d012011-04-20 10:31:50 +08003724 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003725 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003726 if (IS_ERR_OR_NULL(di)) {
3727 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003728 goto err;
3729 }
Chris Mason39279cc2007-06-12 06:35:45 -04003730 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003731 if (ret)
3732 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003733 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003734
Miao Xie67de1172013-12-26 13:07:06 +08003735 /*
3736 * If we don't have dir index, we have to get it by looking up
3737 * the inode ref, since we get the inode ref, remove it directly,
3738 * it is unnecessary to do delayed deletion.
3739 *
3740 * But if we have dir index, needn't search inode ref to get it.
3741 * Since the inode ref is close to the inode item, it is better
3742 * that we delay to delete it, and just do this deletion when
3743 * we update the inode item.
3744 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003745 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003746 ret = btrfs_delayed_delete_inode_ref(inode);
3747 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003748 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003749 goto skip_backref;
3750 }
3751 }
3752
Li Zefan33345d012011-04-20 10:31:50 +08003753 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3754 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003755 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003756 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003757 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003758 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003759 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003760 goto err;
3761 }
Miao Xie67de1172013-12-26 13:07:06 +08003762skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003763 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003764 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003765 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003766 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003767 }
Chris Mason39279cc2007-06-12 06:35:45 -04003768
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003769 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3770 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003771 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003772 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003773 goto err;
3774 }
Chris Masone02119d2008-09-05 16:13:11 -04003775
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003776 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3777 index);
Chris Mason6418c962010-10-30 07:34:24 -04003778 if (ret == -ENOENT)
3779 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003780 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003781 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003782
3783 /*
3784 * If we have a pending delayed iput we could end up with the final iput
3785 * being run in btrfs-cleaner context. If we have enough of these built
3786 * up we can end up burning a lot of time in btrfs-cleaner without any
3787 * way to throttle the unlinks. Since we're currently holding a ref on
3788 * the inode we can run the delayed iput here without any issues as the
3789 * final iput won't be done until after we drop the ref we're currently
3790 * holding.
3791 */
3792 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003793err:
3794 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003795 if (ret)
3796 goto out;
3797
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003798 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003799 inode_inc_iversion(&inode->vfs_inode);
3800 inode_inc_iversion(&dir->vfs_inode);
3801 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3802 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003803 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003804out:
Chris Mason39279cc2007-06-12 06:35:45 -04003805 return ret;
3806}
3807
Al Viro92986792011-03-04 17:14:37 +00003808int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3809 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003810 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003811 const char *name, int name_len)
3812{
3813 int ret;
3814 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3815 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003816 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003817 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00003818 }
3819 return ret;
3820}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003821
3822/*
3823 * helper to start transaction for unlink and rmdir.
3824 *
Josef Bacikd52be812013-05-29 14:54:47 -04003825 * unlink and rmdir are special in btrfs, they do not always free space, so
3826 * if we cannot make our reservations the normal way try and see if there is
3827 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3828 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003829 */
Josef Bacikd52be812013-05-29 14:54:47 -04003830static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003831{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003832 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003833
Josef Bacike70bea52011-10-11 14:18:24 -04003834 /*
3835 * 1 for the possible orphan item
3836 * 1 for the dir item
3837 * 1 for the dir index
3838 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003839 * 1 for the inode
3840 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003841 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003842}
3843
Chris Mason39279cc2007-06-12 06:35:45 -04003844static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3845{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003846 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003847 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003848 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003849 int ret;
3850
Josef Bacikd52be812013-05-29 14:54:47 -04003851 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003852 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003853 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003854
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003855 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3856 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003857
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003858 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3859 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3860 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003861 if (ret)
3862 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003863
Yan, Zhenga22285a2010-05-16 10:48:46 -04003864 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003865 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003866 if (ret)
3867 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003868 }
Josef Bacik7b128762008-07-24 12:17:14 -04003869
Tsutomu Itohb5324022011-07-19 07:27:20 +00003870out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003871 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003872 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003873 return ret;
3874}
3875
Misono Tomohirof60a2362018-04-18 11:34:52 +09003876static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003877 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003878{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003879 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003880 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003881 struct btrfs_path *path;
3882 struct extent_buffer *leaf;
3883 struct btrfs_dir_item *di;
3884 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003885 const char *name = dentry->d_name.name;
3886 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003887 u64 index;
3888 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003889 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003890 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003891
Josef Bacik045d3962019-12-18 17:20:27 -05003892 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3893 objectid = inode->root->root_key.objectid;
3894 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3895 objectid = inode->location.objectid;
3896 } else {
3897 WARN_ON(1);
3898 return -EINVAL;
3899 }
3900
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003901 path = btrfs_alloc_path();
3902 if (!path)
3903 return -ENOMEM;
3904
Li Zefan33345d012011-04-20 10:31:50 +08003905 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003906 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003907 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003908 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003909 goto out;
3910 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003911
3912 leaf = path->nodes[0];
3913 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3914 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3915 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003916 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003917 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003918 goto out;
3919 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003920 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003921
Josef Bacikd49d3282019-12-18 17:20:28 -05003922 /*
3923 * This is a placeholder inode for a subvolume we didn't have a
3924 * reference to at the time of the snapshot creation. In the meantime
3925 * we could have renamed the real subvol link into our snapshot, so
3926 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3927 * Instead simply lookup the dir_index_item for this entry so we can
3928 * remove it. Otherwise we know we have a ref to the root and we can
3929 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3930 */
3931 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003932 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003933 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003934 if (IS_ERR_OR_NULL(di)) {
3935 if (!di)
3936 ret = -ENOENT;
3937 else
3938 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003939 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003940 goto out;
3941 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003942
3943 leaf = path->nodes[0];
3944 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003945 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003946 btrfs_release_path(path);
3947 } else {
3948 ret = btrfs_del_root_ref(trans, objectid,
3949 root->root_key.objectid, dir_ino,
3950 &index, name, name_len);
3951 if (ret) {
3952 btrfs_abort_transaction(trans, ret);
3953 goto out;
3954 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003955 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003956
Lu Fengqi9add2942018-08-01 11:32:26 +08003957 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003958 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003959 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003960 goto out;
3961 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003962
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003963 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003964 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003965 dir->i_mtime = dir->i_ctime = current_time(dir);
Josef Bacik5a24e842012-08-08 10:12:59 -06003966 ret = btrfs_update_inode_fallback(trans, root, dir);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003967 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003968 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003969out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003970 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003971 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003972}
3973
Misono Tomohiroec42f162018-04-18 11:34:13 +09003974/*
3975 * Helper to check if the subvolume references other subvolumes or if it's
3976 * default.
3977 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09003978static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09003979{
3980 struct btrfs_fs_info *fs_info = root->fs_info;
3981 struct btrfs_path *path;
3982 struct btrfs_dir_item *di;
3983 struct btrfs_key key;
3984 u64 dir_id;
3985 int ret;
3986
3987 path = btrfs_alloc_path();
3988 if (!path)
3989 return -ENOMEM;
3990
3991 /* Make sure this root isn't set as the default subvol */
3992 dir_id = btrfs_super_root_dir(fs_info->super_copy);
3993 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
3994 dir_id, "default", 7, 0);
3995 if (di && !IS_ERR(di)) {
3996 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
3997 if (key.objectid == root->root_key.objectid) {
3998 ret = -EPERM;
3999 btrfs_err(fs_info,
4000 "deleting default subvolume %llu is not allowed",
4001 key.objectid);
4002 goto out;
4003 }
4004 btrfs_release_path(path);
4005 }
4006
4007 key.objectid = root->root_key.objectid;
4008 key.type = BTRFS_ROOT_REF_KEY;
4009 key.offset = (u64)-1;
4010
4011 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4012 if (ret < 0)
4013 goto out;
4014 BUG_ON(ret == 0);
4015
4016 ret = 0;
4017 if (path->slots[0] > 0) {
4018 path->slots[0]--;
4019 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4020 if (key.objectid == root->root_key.objectid &&
4021 key.type == BTRFS_ROOT_REF_KEY)
4022 ret = -ENOTEMPTY;
4023 }
4024out:
4025 btrfs_free_path(path);
4026 return ret;
4027}
4028
Nikolay Borisov20a68002018-04-27 14:36:24 +03004029/* Delete all dentries for inodes belonging to the root */
4030static void btrfs_prune_dentries(struct btrfs_root *root)
4031{
4032 struct btrfs_fs_info *fs_info = root->fs_info;
4033 struct rb_node *node;
4034 struct rb_node *prev;
4035 struct btrfs_inode *entry;
4036 struct inode *inode;
4037 u64 objectid = 0;
4038
4039 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4040 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4041
4042 spin_lock(&root->inode_lock);
4043again:
4044 node = root->inode_tree.rb_node;
4045 prev = NULL;
4046 while (node) {
4047 prev = node;
4048 entry = rb_entry(node, struct btrfs_inode, rb_node);
4049
David Sterba37508512018-06-29 10:56:40 +02004050 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004051 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004052 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004053 node = node->rb_right;
4054 else
4055 break;
4056 }
4057 if (!node) {
4058 while (prev) {
4059 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004060 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004061 node = prev;
4062 break;
4063 }
4064 prev = rb_next(prev);
4065 }
4066 }
4067 while (node) {
4068 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004069 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004070 inode = igrab(&entry->vfs_inode);
4071 if (inode) {
4072 spin_unlock(&root->inode_lock);
4073 if (atomic_read(&inode->i_count) > 1)
4074 d_prune_aliases(inode);
4075 /*
4076 * btrfs_drop_inode will have it removed from the inode
4077 * cache when its usage count hits zero.
4078 */
4079 iput(inode);
4080 cond_resched();
4081 spin_lock(&root->inode_lock);
4082 goto again;
4083 }
4084
4085 if (cond_resched_lock(&root->inode_lock))
4086 goto again;
4087
4088 node = rb_next(node);
4089 }
4090 spin_unlock(&root->inode_lock);
4091}
4092
Misono Tomohirof60a2362018-04-18 11:34:52 +09004093int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4094{
4095 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4096 struct btrfs_root *root = BTRFS_I(dir)->root;
4097 struct inode *inode = d_inode(dentry);
4098 struct btrfs_root *dest = BTRFS_I(inode)->root;
4099 struct btrfs_trans_handle *trans;
4100 struct btrfs_block_rsv block_rsv;
4101 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004102 int ret;
4103 int err;
4104
4105 /*
4106 * Don't allow to delete a subvolume with send in progress. This is
4107 * inside the inode lock so the error handling that has to drop the bit
4108 * again is not run concurrently.
4109 */
4110 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004111 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004112 spin_unlock(&dest->root_item_lock);
4113 btrfs_warn(fs_info,
4114 "attempt to delete subvolume %llu during send",
4115 dest->root_key.objectid);
4116 return -EPERM;
4117 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004118 root_flags = btrfs_root_flags(&dest->root_item);
4119 btrfs_set_root_flags(&dest->root_item,
4120 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4121 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004122
4123 down_write(&fs_info->subvol_sem);
4124
4125 err = may_destroy_subvol(dest);
4126 if (err)
4127 goto out_up_write;
4128
4129 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4130 /*
4131 * One for dir inode,
4132 * two for dir entries,
4133 * two for root ref/backref.
4134 */
Gu JinXiangc4c129d2018-05-30 11:00:38 +08004135 err = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004136 if (err)
4137 goto out_up_write;
4138
4139 trans = btrfs_start_transaction(root, 0);
4140 if (IS_ERR(trans)) {
4141 err = PTR_ERR(trans);
4142 goto out_release;
4143 }
4144 trans->block_rsv = &block_rsv;
4145 trans->bytes_reserved = block_rsv.size;
4146
4147 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4148
Josef Bacik045d3962019-12-18 17:20:27 -05004149 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004150 if (ret) {
4151 err = ret;
4152 btrfs_abort_transaction(trans, ret);
4153 goto out_end_trans;
4154 }
4155
4156 btrfs_record_root_in_trans(trans, dest);
4157
4158 memset(&dest->root_item.drop_progress, 0,
4159 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004160 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004161 btrfs_set_root_refs(&dest->root_item, 0);
4162
4163 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4164 ret = btrfs_insert_orphan_item(trans,
4165 fs_info->tree_root,
4166 dest->root_key.objectid);
4167 if (ret) {
4168 btrfs_abort_transaction(trans, ret);
4169 err = ret;
4170 goto out_end_trans;
4171 }
4172 }
4173
Lu Fengqid1957792018-05-29 15:01:54 +08004174 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004175 BTRFS_UUID_KEY_SUBVOL,
4176 dest->root_key.objectid);
4177 if (ret && ret != -ENOENT) {
4178 btrfs_abort_transaction(trans, ret);
4179 err = ret;
4180 goto out_end_trans;
4181 }
4182 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004183 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004184 dest->root_item.received_uuid,
4185 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4186 dest->root_key.objectid);
4187 if (ret && ret != -ENOENT) {
4188 btrfs_abort_transaction(trans, ret);
4189 err = ret;
4190 goto out_end_trans;
4191 }
4192 }
4193
Qu Wenruo082b6c92020-06-16 10:17:37 +08004194 free_anon_bdev(dest->anon_dev);
4195 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004196out_end_trans:
4197 trans->block_rsv = NULL;
4198 trans->bytes_reserved = 0;
4199 ret = btrfs_end_transaction(trans);
4200 if (ret && !err)
4201 err = ret;
4202 inode->i_flags |= S_DEAD;
4203out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004204 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004205out_up_write:
4206 up_write(&fs_info->subvol_sem);
4207 if (err) {
4208 spin_lock(&dest->root_item_lock);
4209 root_flags = btrfs_root_flags(&dest->root_item);
4210 btrfs_set_root_flags(&dest->root_item,
4211 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4212 spin_unlock(&dest->root_item_lock);
4213 } else {
4214 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004215 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004216 ASSERT(dest->send_in_progress == 0);
4217
4218 /* the last ref */
4219 if (dest->ino_cache_inode) {
4220 iput(dest->ino_cache_inode);
4221 dest->ino_cache_inode = NULL;
4222 }
4223 }
4224
4225 return err;
4226}
4227
Chris Mason39279cc2007-06-12 06:35:45 -04004228static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4229{
David Howells2b0143b2015-03-17 22:25:59 +00004230 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004231 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004232 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004233 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004234 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004235
David Sterbab3ae2442012-09-13 16:04:34 -06004236 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004237 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004238 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004239 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004240
Josef Bacikd52be812013-05-29 14:54:47 -04004241 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004242 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004243 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004244
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004245 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004246 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004247 goto out;
4248 }
4249
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004250 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004251 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004252 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004253
Filipe Manana44f714d2016-06-06 16:11:13 +01004254 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4255
Chris Mason39279cc2007-06-12 06:35:45 -04004256 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004257 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4258 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4259 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004260 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004261 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004262 /*
4263 * Propagate the last_unlink_trans value of the deleted dir to
4264 * its parent directory. This is to prevent an unrecoverable
4265 * log tree in the case we do something like this:
4266 * 1) create dir foo
4267 * 2) create snapshot under dir foo
4268 * 3) delete the snapshot
4269 * 4) rmdir foo
4270 * 5) mkdir foo
4271 * 6) fsync foo or some file inside foo
4272 */
4273 if (last_unlink_trans >= trans->transid)
4274 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4275 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004276out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004277 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004278 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004279
Chris Mason39279cc2007-06-12 06:35:45 -04004280 return err;
4281}
4282
Josef Bacikddfae632017-10-19 14:16:02 -04004283/*
4284 * Return this if we need to call truncate_block for the last bit of the
4285 * truncate.
4286 */
4287#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004288
Chris Mason323ac952008-10-01 19:05:46 -04004289/*
Chris Mason39279cc2007-06-12 06:35:45 -04004290 * this can truncate away extent items, csum items and directory items.
4291 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004292 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004293 *
4294 * csum items that cross the new i_size are truncated to the new size
4295 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004296 *
4297 * min_type is the minimum key type to truncate down to. If set to 0, this
4298 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004299 */
Yan, Zheng80825102009-11-12 09:35:36 +00004300int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4301 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004302 struct btrfs_inode *inode,
Yan, Zheng80825102009-11-12 09:35:36 +00004303 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004304{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004305 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004306 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004307 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004308 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004309 struct btrfs_key key;
4310 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004311 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004312 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004313 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004314 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004315 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004316 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004317 int found_extent;
4318 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004319 int pending_del_nr = 0;
4320 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004321 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004322 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004323 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004324 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004325 bool be_nice = false;
4326 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004327 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4328 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004329
4330 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004331
Chris Mason28ed1342014-12-17 09:41:04 -08004332 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004333 * For non-free space inodes and non-shareable roots, we want to back
4334 * off from time to time. This means all inodes in subvolume roots,
4335 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004336 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004337 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004338 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004339 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004340
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004341 path = btrfs_alloc_path();
4342 if (!path)
4343 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004344 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004345
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004346 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004347 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004348 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004349
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004350 /*
4351 * We want to drop from the next block forward in case this
4352 * new size is not block aligned since we will be keeping the
4353 * last block of the extent just the way it is.
4354 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004355 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004356 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004357 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004358 }
Yan, Zheng80825102009-11-12 09:35:36 +00004359
Miao Xie16cdcec2011-04-22 18:12:22 +08004360 /*
4361 * This function is also used to drop the items in the log tree before
4362 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004363 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004364 * items.
4365 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004366 if (min_type == 0 && root == inode->root)
4367 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004368
Li Zefan33345d012011-04-20 10:31:50 +08004369 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004370 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004371 key.type = (u8)-1;
4372
Chris Mason85e21ba2008-01-29 15:11:36 -05004373search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004374 /*
4375 * with a 16K leaf size and 128MB extents, you can actually queue
4376 * up a huge file in a single leaf. Most of the time that
4377 * bytes_deleted is > 0, it will be huge by the time we get here
4378 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004379 if (be_nice && bytes_deleted > SZ_32M &&
4380 btrfs_should_end_transaction(trans)) {
4381 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004382 goto out;
4383 }
Chris Masond3977122009-01-05 21:25:51 -05004384
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004385 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4386 if (ret < 0)
4387 goto out;
4388
Chris Mason85e21ba2008-01-29 15:11:36 -05004389 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004390 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004391 /* there are no items in the tree for us to truncate, we're
4392 * done
4393 */
Yan, Zheng80825102009-11-12 09:35:36 +00004394 if (path->slots[0] == 0)
4395 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004396 path->slots[0]--;
4397 }
4398
Chris Masond3977122009-01-05 21:25:51 -05004399 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004400 u64 clear_start = 0, clear_len = 0;
4401
Chris Mason39279cc2007-06-12 06:35:45 -04004402 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004403 leaf = path->nodes[0];
4404 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004405 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004406
Li Zefan33345d012011-04-20 10:31:50 +08004407 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004408 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004409
Chris Mason85e21ba2008-01-29 15:11:36 -05004410 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004411 break;
4412
Chris Mason5f39d392007-10-15 16:14:19 -04004413 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004414 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004415 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004416 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004417 extent_type = btrfs_file_extent_type(leaf, fi);
4418 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004419 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004420 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004421
4422 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004423 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004424 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004425 item_end += btrfs_file_extent_ram_bytes(leaf,
4426 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004427
4428 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004429 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004430 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004431 }
Yan008630c2007-11-07 13:31:09 -05004432 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004433 }
Yan, Zheng80825102009-11-12 09:35:36 +00004434 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004435 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004436 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004437 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004438 break;
4439 if (found_key.offset >= new_size)
4440 del_item = 1;
4441 else
4442 del_item = 0;
4443 }
Chris Mason39279cc2007-06-12 06:35:45 -04004444 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004445 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004446 if (found_type != BTRFS_EXTENT_DATA_KEY)
4447 goto delete;
4448
4449 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004450 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004451
4452 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004453 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004454 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004455 u64 orig_num_bytes =
4456 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004457 extent_num_bytes = ALIGN(new_size -
4458 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004459 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004460 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004461 btrfs_set_file_extent_num_bytes(leaf, fi,
4462 extent_num_bytes);
4463 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004464 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004465 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004466 &root->state) &&
4467 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004468 inode_sub_bytes(&inode->vfs_inode,
4469 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004470 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004471 } else {
Chris Masondb945352007-10-15 16:15:53 -04004472 extent_num_bytes =
4473 btrfs_file_extent_disk_num_bytes(leaf,
4474 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004475 extent_offset = found_key.offset -
4476 btrfs_file_extent_offset(leaf, fi);
4477
Chris Mason39279cc2007-06-12 06:35:45 -04004478 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004479 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004480 if (extent_start != 0) {
4481 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004482 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004483 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004484 inode_sub_bytes(&inode->vfs_inode,
4485 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004486 }
4487 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004488 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004489 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004490 /*
4491 * we can't truncate inline items that have had
4492 * special encodings
4493 */
4494 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004495 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004496 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4497 btrfs_file_extent_compression(leaf, fi) == 0) {
4498 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004499
Josef Bacikddfae632017-10-19 14:16:02 -04004500 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4501 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004502 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004503 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004504 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004505 * We have to bail so the last_size is set to
4506 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004507 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004508 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004509 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004510 } else {
4511 /*
4512 * Inline extents are special, we just treat
4513 * them as a full sector worth in the file
4514 * extent tree just for simplicity sake.
4515 */
4516 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004517 }
Josef Bacikddfae632017-10-19 14:16:02 -04004518
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004519 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004520 inode_sub_bytes(&inode->vfs_inode,
4521 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004522 }
Chris Mason179e29e2007-11-01 11:28:41 -04004523delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004524 /*
4525 * We use btrfs_truncate_inode_items() to clean up log trees for
4526 * multiple fsyncs, and in this case we don't want to clear the
4527 * file extent range because it's just the log.
4528 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004529 if (root == inode->root) {
4530 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004531 clear_start, clear_len);
4532 if (ret) {
4533 btrfs_abort_transaction(trans, ret);
4534 break;
4535 }
4536 }
4537
Josef Bacikddfae632017-10-19 14:16:02 -04004538 if (del_item)
4539 last_size = found_key.offset;
4540 else
4541 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004542 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004543 if (!pending_del_nr) {
4544 /* no pending yet, add ourselves */
4545 pending_del_slot = path->slots[0];
4546 pending_del_nr = 1;
4547 } else if (pending_del_nr &&
4548 path->slots[0] + 1 == pending_del_slot) {
4549 /* hop on the pending chunk */
4550 pending_del_nr++;
4551 pending_del_slot = path->slots[0];
4552 } else {
Chris Masond3977122009-01-05 21:25:51 -05004553 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004554 }
Chris Mason39279cc2007-06-12 06:35:45 -04004555 } else {
4556 break;
4557 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004558 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004559
Miao Xie27cdeb72014-04-02 19:51:05 +08004560 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004561 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004562 struct btrfs_ref ref = { 0 };
4563
Chris Mason28ed1342014-12-17 09:41:04 -08004564 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004565
4566 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4567 extent_start, extent_num_bytes, 0);
4568 ref.real_root = root->root_key.objectid;
4569 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4570 ino, extent_offset);
4571 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004572 if (ret) {
4573 btrfs_abort_transaction(trans, ret);
4574 break;
4575 }
Chris Mason28f75a02015-02-04 06:59:29 -08004576 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004577 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004578 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004579 }
Chris Mason39279cc2007-06-12 06:35:45 -04004580 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004581
Yan, Zheng80825102009-11-12 09:35:36 +00004582 if (found_type == BTRFS_INODE_ITEM_KEY)
4583 break;
4584
4585 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004586 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004587 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004588 if (pending_del_nr) {
4589 ret = btrfs_del_items(trans, root, path,
4590 pending_del_slot,
4591 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004592 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004593 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004594 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004595 }
Yan, Zheng80825102009-11-12 09:35:36 +00004596 pending_del_nr = 0;
4597 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004598 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004599
Chris Mason28f75a02015-02-04 06:59:29 -08004600 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004601 * We can generate a lot of delayed refs, so we need to
4602 * throttle every once and a while and make sure we're
4603 * adding enough space to keep up with the work we are
4604 * generating. Since we hold a transaction here we
4605 * can't flush, and we don't want to FLUSH_LIMIT because
4606 * we could have generated too many delayed refs to
4607 * actually allocate, so just bail if we're short and
4608 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004609 */
Josef Bacik28bad212018-12-03 10:20:38 -05004610 if (should_throttle) {
4611 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4612 BTRFS_RESERVE_NO_FLUSH);
4613 if (ret) {
4614 ret = -EAGAIN;
4615 break;
4616 }
Chris Mason28f75a02015-02-04 06:59:29 -08004617 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004618 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004619 } else {
4620 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004621 }
Chris Mason39279cc2007-06-12 06:35:45 -04004622 }
Yan, Zheng80825102009-11-12 09:35:36 +00004623out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004624 if (ret >= 0 && pending_del_nr) {
4625 int err;
4626
4627 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004628 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004629 if (err) {
4630 btrfs_abort_transaction(trans, err);
4631 ret = err;
4632 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004633 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004634 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4635 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004636 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004637 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004638 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4639 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4640 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004641 }
Chris Mason28ed1342014-12-17 09:41:04 -08004642
Chris Mason39279cc2007-06-12 06:35:45 -04004643 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004644 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004645}
4646
Chris Masona52d9a82007-08-27 16:49:44 -04004647/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304648 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004649 * @inode - inode that we're zeroing
4650 * @from - the offset to start zeroing
4651 * @len - the length to zero, 0 to zero the entire range respective to the
4652 * offset
4653 * @front - zero up to the offset instead of from the offset on
4654 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304655 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004656 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004657 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304658int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
Josef Bacik2aaa6652012-08-29 14:27:18 -04004659 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004660{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004661 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004662 struct address_space *mapping = inode->i_mapping;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004663 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4664 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004665 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004666 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004667 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004668 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004669 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004670 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304671 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004672 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004673 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004674 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004675 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304676 u64 block_start;
4677 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004678
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004679 if (IS_ALIGNED(offset, blocksize) &&
4680 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004681 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304682
Josef Bacik8b62f872017-10-19 14:15:55 -04004683 block_start = round_down(from, blocksize);
4684 block_end = block_start + blocksize - 1;
4685
Nikolay Borisov36ea6f32020-06-03 08:55:41 +03004686 ret = btrfs_check_data_free_space(BTRFS_I(inode), &data_reserved,
4687 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004688 if (ret < 0) {
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004689 if (btrfs_check_nocow_lock(BTRFS_I(inode), block_start,
4690 &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004691 /* For nocow case, no need to reserve data space */
4692 only_release_metadata = true;
4693 } else {
4694 goto out;
4695 }
4696 }
4697 ret = btrfs_delalloc_reserve_metadata(BTRFS_I(inode), blocksize);
4698 if (ret < 0) {
4699 if (!only_release_metadata)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03004700 btrfs_free_reserved_data_space(BTRFS_I(inode),
4701 data_reserved, block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004702 goto out;
4703 }
Chris Mason211c17f2008-05-15 09:13:45 -04004704again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004705 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004706 if (!page) {
Nikolay Borisov86d52922020-06-03 08:55:40 +03004707 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo43b18592017-12-12 15:34:32 +08004708 block_start, blocksize, true);
Qu Wenruo8702ba92019-10-14 14:34:51 +08004709 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004710 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004711 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004712 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004713
Chris Masona52d9a82007-08-27 16:49:44 -04004714 if (!PageUptodate(page)) {
4715 ret = btrfs_readpage(NULL, page);
4716 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004717 if (page->mapping != mapping) {
4718 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004719 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004720 goto again;
4721 }
Chris Masona52d9a82007-08-27 16:49:44 -04004722 if (!PageUptodate(page)) {
4723 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004724 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004725 }
4726 }
Chris Mason211c17f2008-05-15 09:13:45 -04004727 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004728
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304729 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004730 set_page_extent_mapped(page);
4731
Nikolay Borisovc3504372020-06-03 08:55:03 +03004732 ordered = btrfs_lookup_ordered_extent(BTRFS_I(inode), block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004733 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304734 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004735 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004736 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004737 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004738 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004739 btrfs_put_ordered_extent(ordered);
4740 goto again;
4741 }
4742
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304743 clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004744 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4745 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004746
Nikolay Borisovc2566f22020-06-03 08:55:35 +03004747 ret = btrfs_set_extent_delalloc(BTRFS_I(inode), block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004748 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004749 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304750 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004751 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004752 goto out_unlock;
4753 }
4754
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304755 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004756 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304757 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004758 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004759 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 memset(kaddr + (block_start - page_offset(page)),
4761 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004762 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304763 memset(kaddr + (block_start - page_offset(page)) + offset,
4764 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004765 flush_dcache_page(page);
4766 kunmap(page);
4767 }
Chris Mason247e7432008-07-17 12:53:51 -04004768 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004769 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004770 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004771
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004772 if (only_release_metadata)
4773 set_extent_bit(&BTRFS_I(inode)->io_tree, block_start,
Qu Wenruo3f6bb4a2020-10-21 14:24:51 +08004774 block_end, EXTENT_NORESERVE, NULL, GFP_NOFS);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004775
Chris Mason89642222008-07-24 09:41:53 -04004776out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004777 if (ret) {
4778 if (only_release_metadata)
4779 btrfs_delalloc_release_metadata(BTRFS_I(inode),
4780 blocksize, true);
4781 else
Nikolay Borisov86d52922020-06-03 08:55:40 +03004782 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004783 block_start, blocksize, true);
4784 }
Qu Wenruo8702ba92019-10-14 14:34:51 +08004785 btrfs_delalloc_release_extents(BTRFS_I(inode), blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004786 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004787 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004788out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004789 if (only_release_metadata)
Qu Wenruo38d37aa2020-06-24 07:23:52 +08004790 btrfs_check_nocow_unlock(BTRFS_I(inode));
Qu Wenruo364ecf32017-02-27 15:10:38 +08004791 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004792 return ret;
4793}
4794
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004795static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04004796 u64 offset, u64 len)
4797{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004798 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04004799 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00004800 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04004801 int ret;
4802
4803 /*
4804 * Still need to make sure the inode looks like it's been updated so
4805 * that any holes get logged if we fsync.
4806 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004807 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004808 inode->last_trans = fs_info->generation;
4809 inode->last_sub_trans = root->log_transid;
4810 inode->last_log_commit = root->last_log_commit;
Josef Bacik16e75492013-10-22 12:18:51 -04004811 return 0;
4812 }
4813
4814 /*
4815 * 1 - for the one we're dropping
4816 * 1 - for the one we're adding
4817 * 1 - for updating the inode.
4818 */
4819 trans = btrfs_start_transaction(root, 3);
4820 if (IS_ERR(trans))
4821 return PTR_ERR(trans);
4822
Filipe Manana5893dfb2020-11-04 11:07:32 +00004823 drop_args.start = offset;
4824 drop_args.end = offset + len;
4825 drop_args.drop_cache = true;
4826
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004827 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04004828 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004829 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004830 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004831 return ret;
4832 }
4833
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004834 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004835 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00004836 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004837 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00004838 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004839 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
4840 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00004841 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004842 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004843 return ret;
4844}
4845
Josef Bacik695a0d02011-03-04 15:46:53 -05004846/*
4847 * This function puts in dummy file extents for the area we're creating a hole
4848 * for. So if we are truncating this file to a larger size we need to insert
4849 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4850 * the range between oldsize and size
4851 */
Josef Bacika41ad392011-01-31 15:30:16 -05004852int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004853{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004854 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Yan Zheng9036c102008-10-30 14:19:41 -04004855 struct btrfs_root *root = BTRFS_I(inode)->root;
4856 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004857 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004858 struct extent_state *cached_state = NULL;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004859 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004860 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4861 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004862 u64 last_byte;
4863 u64 cur_offset;
4864 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004865 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004866
Josef Bacika71754f2013-06-17 17:14:39 -04004867 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304868 * If our size started in the middle of a block we need to zero out the
4869 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004870 * expose stale data.
4871 */
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304872 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004873 if (err)
4874 return err;
4875
Yan Zheng9036c102008-10-30 14:19:41 -04004876 if (size <= hole_start)
4877 return 0;
4878
David Sterbab272ae22020-02-05 19:09:33 +01004879 btrfs_lock_and_flush_ordered_range(BTRFS_I(inode), hole_start,
Nikolay Borisov23d31bd2019-05-07 10:19:23 +03004880 block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004881 cur_offset = hole_start;
4882 while (1) {
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02004883 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004884 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004885 if (IS_ERR(em)) {
4886 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004887 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004888 break;
4889 }
Yan Zheng9036c102008-10-30 14:19:41 -04004890 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004891 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004892 hole_size = last_byte - cur_offset;
4893
Yan, Zheng80825102009-11-12 09:35:36 +00004894 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004895 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004896
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004897 err = maybe_insert_hole(root, BTRFS_I(inode),
4898 cur_offset, hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04004899 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004900 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004901
4902 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4903 cur_offset, hole_size);
4904 if (err)
4905 break;
4906
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004907 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004908 cur_offset + hole_size - 1, 0);
4909 hole_em = alloc_extent_map();
4910 if (!hole_em) {
4911 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4912 &BTRFS_I(inode)->runtime_flags);
4913 goto next;
4914 }
4915 hole_em->start = cur_offset;
4916 hole_em->len = hole_size;
4917 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004918
Josef Bacik5dc562c2012-08-17 13:14:17 -04004919 hole_em->block_start = EXTENT_MAP_HOLE;
4920 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004921 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004922 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004923 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004924 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004925
4926 while (1) {
4927 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004928 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004929 write_unlock(&em_tree->lock);
4930 if (err != -EEXIST)
4931 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02004932 btrfs_drop_extent_cache(BTRFS_I(inode),
4933 cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004934 cur_offset +
4935 hole_size - 1, 0);
4936 }
4937 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004938 } else {
4939 err = btrfs_inode_set_file_extent_range(BTRFS_I(inode),
4940 cur_offset, hole_size);
4941 if (err)
4942 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004943 }
Josef Bacik16e75492013-10-22 12:18:51 -04004944next:
Yan Zheng9036c102008-10-30 14:19:41 -04004945 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004946 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004947 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004948 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004949 break;
4950 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004951 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004952 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004953 return err;
4954}
4955
Eric Sandeen3972f262013-01-12 02:57:22 +00004956static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004957{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004958 struct btrfs_root *root = BTRFS_I(inode)->root;
4959 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004960 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004961 loff_t newsize = attr->ia_size;
4962 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004963 int ret;
4964
Eric Sandeen3972f262013-01-12 02:57:22 +00004965 /*
4966 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4967 * special case where we need to update the times despite not having
4968 * these flags set. For all other operations the VFS set these flags
4969 * explicitly if it wants a timestamp update.
4970 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004971 if (newsize != oldsize) {
4972 inode_inc_iversion(inode);
4973 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4974 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004975 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004976 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004977
Josef Bacika41ad392011-01-31 15:30:16 -05004978 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004979 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004980 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004981 * This is to ensure the snapshot captures a fully consistent
4982 * state of this file - if the snapshot captures this expanding
4983 * truncation, it must capture all writes that happened before
4984 * this truncation.
4985 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004986 btrfs_drew_write_lock(&root->snapshot_lock);
Josef Bacika41ad392011-01-31 15:30:16 -05004987 ret = btrfs_cont_expand(inode, oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004988 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004989 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004990 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004991 }
Yan, Zheng80825102009-11-12 09:35:36 +00004992
Miao Xief4a2f4c2011-12-14 20:12:01 -05004993 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004994 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004995 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05004996 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004997 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05004998
4999 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005000 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305001 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005002 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005003 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005004 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005005 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005006
Josef Bacika41ad392011-01-31 15:30:16 -05005007 /*
5008 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005009 * zero. Make sure any new writes to the file get on disk
5010 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005011 */
5012 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005013 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005014 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005015
Josef Bacika41ad392011-01-31 15:30:16 -05005016 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005017
Miao Xie2e60a512013-02-08 07:01:08 +00005018 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005019
Filipe Manana213e8c52018-02-06 20:40:31 +00005020 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005021 if (ret && inode->i_nlink) {
5022 int err;
5023
5024 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005025 * Truncate failed, so fix up the in-memory size. We
5026 * adjusted disk_i_size down as we removed extents, so
5027 * wait for disk_i_size to be stable and then update the
5028 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005029 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005030 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005031 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005032 return err;
5033 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005034 }
Yan, Zheng80825102009-11-12 09:35:36 +00005035 }
5036
Josef Bacika41ad392011-01-31 15:30:16 -05005037 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005038}
5039
Chris Mason39279cc2007-06-12 06:35:45 -04005040static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5041{
David Howells2b0143b2015-03-17 22:25:59 +00005042 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005043 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005044 int err;
5045
Li Zefanb83cc962010-12-20 16:04:08 +08005046 if (btrfs_root_readonly(root))
5047 return -EROFS;
5048
Jan Kara31051c82016-05-26 16:55:18 +02005049 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005050 if (err)
5051 return err;
5052
Chris Mason5a3f23d2009-03-31 13:27:11 -04005053 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005054 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005055 if (err)
5056 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005057 }
Yan Zheng9036c102008-10-30 14:19:41 -04005058
Christoph Hellwig10257742010-06-04 11:30:02 +02005059 if (attr->ia_valid) {
5060 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005061 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005062 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005063
Josef Bacik22c44fe2011-11-30 10:45:38 -05005064 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005065 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005066 }
5067
Chris Mason39279cc2007-06-12 06:35:45 -04005068 return err;
5069}
Chris Mason61295eb2008-01-14 16:24:38 -05005070
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005071/*
5072 * While truncating the inode pages during eviction, we get the VFS calling
5073 * btrfs_invalidatepage() against each page of the inode. This is slow because
5074 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5075 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5076 * extent_state structures over and over, wasting lots of time.
5077 *
5078 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5079 * those expensive operations on a per page basis and do only the ordered io
5080 * finishing, while we release here the extent_map and extent_state structures,
5081 * without the excessive merging and splitting.
5082 */
5083static void evict_inode_truncate_pages(struct inode *inode)
5084{
5085 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5086 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5087 struct rb_node *node;
5088
5089 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005090 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005091
5092 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005093 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005094 struct extent_map *em;
5095
Liu Bo07e1ce02018-08-23 03:51:52 +08005096 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005097 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005098 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5099 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005100 remove_extent_mapping(map_tree, em);
5101 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005102 if (need_resched()) {
5103 write_unlock(&map_tree->lock);
5104 cond_resched();
5105 write_lock(&map_tree->lock);
5106 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005107 }
5108 write_unlock(&map_tree->lock);
5109
Filipe Manana6ca07092015-05-26 00:55:42 +01005110 /*
5111 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005112 * We can have ongoing bios started by readahead that have
5113 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005114 * still in progress (unlocked the pages in the bio but did not yet
5115 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005116 * ranges can still be locked and eviction started because before
5117 * submitting those bios, which are executed by a separate task (work
5118 * queue kthread), inode references (inode->i_count) were not taken
5119 * (which would be dropped in the end io callback of each bio).
5120 * Therefore here we effectively end up waiting for those bios and
5121 * anyone else holding locked ranges without having bumped the inode's
5122 * reference count - if we don't do it, when they access the inode's
5123 * io_tree to unlock a range it may be too late, leading to an
5124 * use-after-free issue.
5125 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005126 spin_lock(&io_tree->lock);
5127 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5128 struct extent_state *state;
5129 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005130 u64 start;
5131 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005132 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005133
5134 node = rb_first(&io_tree->state);
5135 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005136 start = state->start;
5137 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005138 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005139 spin_unlock(&io_tree->lock);
5140
David Sterbaff13db42015-12-03 14:30:40 +01005141 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005142
5143 /*
5144 * If still has DELALLOC flag, the extent didn't reach disk,
5145 * and its reserved space won't be freed by delayed_ref.
5146 * So we need to free its reserved space here.
5147 * (Refer to comment in btrfs_invalidatepage, case 2)
5148 *
5149 * Note, end is the bytenr of last byte, so we need + 1 here.
5150 */
Filipe Manana421f0922018-10-12 13:02:48 +01005151 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005152 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5153 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005154
Filipe Manana6ca07092015-05-26 00:55:42 +01005155 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005156 EXTENT_LOCKED | EXTENT_DELALLOC |
5157 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5158 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005159
Filipe Manana7064dd52014-08-08 02:47:05 +01005160 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005161 spin_lock(&io_tree->lock);
5162 }
5163 spin_unlock(&io_tree->lock);
5164}
5165
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005166static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005167 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005168{
5169 struct btrfs_fs_info *fs_info = root->fs_info;
5170 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005171 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005172 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005173 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005174
Josef Bacikd3984c92019-08-01 18:19:37 -04005175 /*
5176 * Eviction should be taking place at some place safe because of our
5177 * delayed iputs. However the normal flushing code will run delayed
5178 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5179 *
5180 * We reserve the delayed_refs_extra here again because we can't use
5181 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5182 * above. We reserve our extra bit here because we generate a ton of
5183 * delayed refs activity by truncating.
5184 *
5185 * If we cannot make our reservation we'll attempt to steal from the
5186 * global reserve, because we really want to be able to free up space.
5187 */
5188 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5189 BTRFS_RESERVE_FLUSH_EVICT);
5190 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005191 /*
5192 * Try to steal from the global reserve if there is space for
5193 * it.
5194 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005195 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5196 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5197 btrfs_warn(fs_info,
5198 "could not allocate space for delete; will truncate on mount");
5199 return ERR_PTR(-ENOSPC);
5200 }
5201 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005202 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005203
5204 trans = btrfs_join_transaction(root);
5205 if (IS_ERR(trans))
5206 return trans;
5207
5208 if (delayed_refs_extra) {
5209 trans->block_rsv = &fs_info->trans_block_rsv;
5210 trans->bytes_reserved = delayed_refs_extra;
5211 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5212 delayed_refs_extra, 1);
5213 }
5214 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005215}
5216
Al Virobd555972010-06-07 11:35:40 -04005217void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005218{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005219 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005220 struct btrfs_trans_handle *trans;
5221 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005222 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005223 int ret;
5224
liubo1abe9b82011-03-24 11:18:59 +00005225 trace_btrfs_inode_evict(inode);
5226
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005227 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005228 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005229 return;
5230 }
5231
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005232 evict_inode_truncate_pages(inode);
5233
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005234 if (inode->i_nlink &&
5235 ((btrfs_root_refs(&root->root_item) != 0 &&
5236 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005237 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005238 goto no_delete;
5239
Omar Sandoval27919062018-05-11 13:13:37 -07005240 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005241 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005242
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005243 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005244
Omar Sandoval7b40b692018-05-11 13:13:33 -07005245 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005246 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005247
Yan, Zheng76dda932009-09-21 16:00:26 -04005248 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005249 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5250 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005251 goto no_delete;
5252 }
5253
Nikolay Borisovaa790212017-01-10 20:35:40 +02005254 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005255 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005256 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005257
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005258 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005259 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005260 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005261 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005262 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005263
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005264 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005265
Yan, Zheng80825102009-11-12 09:35:36 +00005266 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005267 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005268 if (IS_ERR(trans))
5269 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005270
5271 trans->block_rsv = rsv;
5272
Nikolay Borisov50743392020-11-02 16:48:55 +02005273 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
5274 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005275 trans->block_rsv = &fs_info->trans_block_rsv;
5276 btrfs_end_transaction(trans);
5277 btrfs_btree_balance_dirty(fs_info);
5278 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5279 goto free_rsv;
5280 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005281 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005282 }
5283
Josef Bacik4ef31a42013-08-13 14:10:08 -04005284 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005285 * Errors here aren't a big deal, it just means we leave orphan items in
5286 * the tree. They will be cleaned up on the next mount. If the inode
5287 * number gets reused, cleanup deletes the orphan item without doing
5288 * anything, and unlink reuses the existing orphan item.
5289 *
5290 * If it turns out that we are dropping too many of these, we might want
5291 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005292 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005293 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005294 if (!IS_ERR(trans)) {
5295 trans->block_rsv = rsv;
5296 btrfs_orphan_del(trans, BTRFS_I(inode));
5297 trans->block_rsv = &fs_info->trans_block_rsv;
5298 btrfs_end_transaction(trans);
5299 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005300
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005301 if (!(root == fs_info->tree_root ||
Li Zefan581bb052011-04-20 10:06:11 +08005302 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005303 btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
Li Zefan581bb052011-04-20 10:06:11 +08005304
Omar Sandoval27919062018-05-11 13:13:37 -07005305free_rsv:
5306 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005307no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005308 /*
5309 * If we didn't successfully delete, the orphan item will still be in
5310 * the tree and we'll retry on the next mount. Again, we might also want
5311 * to retry these periodically in the future.
5312 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005313 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005314 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005315}
5316
5317/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005318 * Return the key found in the dir entry in the location pointer, fill @type
5319 * with BTRFS_FT_*, and return 0.
5320 *
Su Yue005d6712018-03-05 17:13:37 +08005321 * If no dir entries were found, returns -ENOENT.
5322 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005323 */
5324static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005325 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005326{
5327 const char *name = dentry->d_name.name;
5328 int namelen = dentry->d_name.len;
5329 struct btrfs_dir_item *di;
5330 struct btrfs_path *path;
5331 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005332 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005333
5334 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005335 if (!path)
5336 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005337
David Sterbaf85b7372017-01-20 14:54:07 +01005338 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5339 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005340 if (IS_ERR_OR_NULL(di)) {
5341 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005342 goto out;
5343 }
Chris Masond3977122009-01-05 21:25:51 -05005344
Chris Mason5f39d392007-10-15 16:14:19 -04005345 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005346 if (location->type != BTRFS_INODE_ITEM_KEY &&
5347 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005348 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005349 btrfs_warn(root->fs_info,
5350"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5351 __func__, name, btrfs_ino(BTRFS_I(dir)),
5352 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005353 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005354 if (!ret)
5355 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005356out:
Chris Mason39279cc2007-06-12 06:35:45 -04005357 btrfs_free_path(path);
5358 return ret;
5359}
5360
5361/*
5362 * when we hit a tree root in a directory, the btrfs part of the inode
5363 * needs to be changed to reflect the root directory of the tree root. This
5364 * is kind of like crossing a mount point.
5365 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005366static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005367 struct inode *dir,
5368 struct dentry *dentry,
5369 struct btrfs_key *location,
5370 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005371{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005372 struct btrfs_path *path;
5373 struct btrfs_root *new_root;
5374 struct btrfs_root_ref *ref;
5375 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005376 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005377 int ret;
5378 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005379
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005380 path = btrfs_alloc_path();
5381 if (!path) {
5382 err = -ENOMEM;
5383 goto out;
5384 }
Chris Mason39279cc2007-06-12 06:35:45 -04005385
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005386 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005387 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5388 key.type = BTRFS_ROOT_REF_KEY;
5389 key.offset = location->objectid;
5390
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005391 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005392 if (ret) {
5393 if (ret < 0)
5394 err = ret;
5395 goto out;
5396 }
Chris Mason39279cc2007-06-12 06:35:45 -04005397
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005398 leaf = path->nodes[0];
5399 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005400 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005401 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5402 goto out;
5403
5404 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5405 (unsigned long)(ref + 1),
5406 dentry->d_name.len);
5407 if (ret)
5408 goto out;
5409
David Sterbab3b4aa72011-04-21 01:20:15 +02005410 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005411
David Sterba56e93572020-05-15 19:35:55 +02005412 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005413 if (IS_ERR(new_root)) {
5414 err = PTR_ERR(new_root);
5415 goto out;
5416 }
5417
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005418 *sub_root = new_root;
5419 location->objectid = btrfs_root_dirid(&new_root->root_item);
5420 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005421 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005422 err = 0;
5423out:
5424 btrfs_free_path(path);
5425 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005426}
5427
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005428static void inode_tree_add(struct inode *inode)
5429{
5430 struct btrfs_root *root = BTRFS_I(inode)->root;
5431 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005432 struct rb_node **p;
5433 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005434 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005435 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005436
Al Viro1d3382cb2010-10-23 15:19:20 -04005437 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005438 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005439 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005440 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005441 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005442 while (*p) {
5443 parent = *p;
5444 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5445
David Sterba37508512018-06-29 10:56:40 +02005446 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005447 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005448 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005449 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005450 else {
5451 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005452 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005453 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005454 RB_CLEAR_NODE(parent);
5455 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005456 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005457 }
5458 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005459 rb_link_node(new, parent, p);
5460 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005461 spin_unlock(&root->inode_lock);
5462}
5463
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005464static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005465{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005466 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005467 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005468
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005469 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005470 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5471 rb_erase(&inode->rb_node, &root->inode_tree);
5472 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005473 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005474 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005475 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005476
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005477 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005478 spin_lock(&root->inode_lock);
5479 empty = RB_EMPTY_ROOT(&root->inode_tree);
5480 spin_unlock(&root->inode_lock);
5481 if (empty)
5482 btrfs_add_dead_root(root);
5483 }
5484}
5485
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005486
Chris Masone02119d2008-09-05 16:13:11 -04005487static int btrfs_init_locked_inode(struct inode *inode, void *p)
5488{
5489 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005490
5491 inode->i_ino = args->ino;
5492 BTRFS_I(inode)->location.objectid = args->ino;
5493 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5494 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005495 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5496 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005497 return 0;
5498}
5499
5500static int btrfs_find_actor(struct inode *inode, void *opaque)
5501{
5502 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005503
5504 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005505 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005506}
5507
David Sterba0202e832020-05-15 19:35:59 +02005508static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005509 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005510{
5511 struct inode *inode;
5512 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005513 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005514
David Sterba0202e832020-05-15 19:35:59 +02005515 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005516 args.root = root;
5517
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005518 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005519 btrfs_init_locked_inode,
5520 (void *)&args);
5521 return inode;
5522}
5523
David Sterba4c66e0d2019-10-03 19:09:35 +02005524/*
David Sterba0202e832020-05-15 19:35:59 +02005525 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005526 * Path can be preallocated to prevent recursing back to iget through
5527 * allocator. NULL is also valid but may require an additional allocation
5528 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305529 */
David Sterba0202e832020-05-15 19:35:59 +02005530struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005531 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305532{
5533 struct inode *inode;
5534
David Sterba0202e832020-05-15 19:35:59 +02005535 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305536 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005537 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305538
5539 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005540 int ret;
5541
Filipe Manana4222ea72018-10-24 10:13:03 +01005542 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005543 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005544 inode_tree_add(inode);
5545 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005546 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005547 iget_failed(inode);
5548 /*
5549 * ret > 0 can come from btrfs_search_slot called by
5550 * btrfs_read_locked_inode, this means the inode item
5551 * was not found.
5552 */
5553 if (ret > 0)
5554 ret = -ENOENT;
5555 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005556 }
5557 }
5558
Balaji Rao1a54ef82008-07-21 02:01:04 +05305559 return inode;
5560}
5561
David Sterba0202e832020-05-15 19:35:59 +02005562struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005563{
David Sterba0202e832020-05-15 19:35:59 +02005564 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005565}
5566
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005567static struct inode *new_simple_dir(struct super_block *s,
5568 struct btrfs_key *key,
5569 struct btrfs_root *root)
5570{
5571 struct inode *inode = new_inode(s);
5572
5573 if (!inode)
5574 return ERR_PTR(-ENOMEM);
5575
Josef Bacik5c8fd992020-02-14 16:11:43 -05005576 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005577 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005578 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005579
5580 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005581 /*
5582 * We only need lookup, the rest is read-only and there's no inode
5583 * associated with the dentry
5584 */
5585 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005586 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005587 inode->i_fop = &simple_dir_operations;
5588 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005589 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305590 inode->i_atime = inode->i_mtime;
5591 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005592 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005593
5594 return inode;
5595}
5596
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005597static inline u8 btrfs_inode_type(struct inode *inode)
5598{
5599 /*
5600 * Compile-time asserts that generic FT_* types still match
5601 * BTRFS_FT_* types
5602 */
5603 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5604 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5605 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5606 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5607 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5608 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5609 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5610 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5611
5612 return fs_umode_to_ftype(inode->i_mode);
5613}
5614
Chris Mason3de45862008-11-17 21:02:50 -05005615struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005616{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005617 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005618 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005619 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005620 struct btrfs_root *sub_root = root;
5621 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005622 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005623 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005624
5625 if (dentry->d_name.len > BTRFS_NAME_LEN)
5626 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005627
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005628 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005629 if (ret < 0)
5630 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005631
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005632 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005633 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005634 if (IS_ERR(inode))
5635 return inode;
5636
5637 /* Do extra check against inode mode with di_type */
5638 if (btrfs_inode_type(inode) != di_type) {
5639 btrfs_crit(fs_info,
5640"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5641 inode->i_mode, btrfs_inode_type(inode),
5642 di_type);
5643 iput(inode);
5644 return ERR_PTR(-EUCLEAN);
5645 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005646 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005647 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005648
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005649 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005650 &location, &sub_root);
5651 if (ret < 0) {
5652 if (ret != -ENOENT)
5653 inode = ERR_PTR(ret);
5654 else
5655 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5656 } else {
David Sterba0202e832020-05-15 19:35:59 +02005657 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005658 }
Josef Bacik87270022020-01-24 09:32:31 -05005659 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005660 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005661
Julia Lawall34d19ba2011-01-24 19:55:19 +00005662 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005663 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005664 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005665 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005666 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005667 if (ret) {
5668 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005669 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005670 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005671 }
5672
Chris Mason3de45862008-11-17 21:02:50 -05005673 return inode;
5674}
5675
Nick Pigginfe15ce42011-01-07 17:49:23 +11005676static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005677{
5678 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005679 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005680
Li Zefan848cce02012-02-21 17:04:28 +08005681 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005682 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005683
Li Zefan848cce02012-02-21 17:04:28 +08005684 if (inode) {
5685 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005686 if (btrfs_root_refs(&root->root_item) == 0)
5687 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005688
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005689 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005690 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005691 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005692 return 0;
5693}
5694
Chris Mason3de45862008-11-17 21:02:50 -05005695static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005696 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005697{
Al Viro3837d202018-10-10 16:38:27 -04005698 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005699
Al Viro3837d202018-10-10 16:38:27 -04005700 if (inode == ERR_PTR(-ENOENT))
5701 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005702 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005703}
5704
Josef Bacik23b5ec72017-07-24 15:14:25 -04005705/*
5706 * All this infrastructure exists because dir_emit can fault, and we are holding
5707 * the tree lock when doing readdir. For now just allocate a buffer and copy
5708 * our information into that, and then dir_emit from the buffer. This is
5709 * similar to what NFS does, only we don't keep the buffer around in pagecache
5710 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5711 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5712 * tree lock.
5713 */
5714static int btrfs_opendir(struct inode *inode, struct file *file)
5715{
5716 struct btrfs_file_private *private;
5717
5718 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5719 if (!private)
5720 return -ENOMEM;
5721 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5722 if (!private->filldir_buf) {
5723 kfree(private);
5724 return -ENOMEM;
5725 }
5726 file->private_data = private;
5727 return 0;
5728}
5729
5730struct dir_entry {
5731 u64 ino;
5732 u64 offset;
5733 unsigned type;
5734 int name_len;
5735};
5736
5737static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5738{
5739 while (entries--) {
5740 struct dir_entry *entry = addr;
5741 char *name = (char *)(entry + 1);
5742
David Sterba92d32172018-04-16 21:10:14 +02005743 ctx->pos = get_unaligned(&entry->offset);
5744 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5745 get_unaligned(&entry->ino),
5746 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005747 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005748 addr += sizeof(struct dir_entry) +
5749 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005750 ctx->pos++;
5751 }
5752 return 0;
5753}
5754
Al Viro9cdda8d2013-05-22 16:48:09 -04005755static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005756{
Al Viro9cdda8d2013-05-22 16:48:09 -04005757 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005758 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005759 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005760 struct btrfs_dir_item *di;
5761 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005762 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005763 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005764 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005765 struct list_head ins_list;
5766 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005767 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005768 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005769 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005770 char *name_ptr;
5771 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005772 int entries = 0;
5773 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005774 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005775 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005776
Al Viro9cdda8d2013-05-22 16:48:09 -04005777 if (!dir_emit_dots(file, ctx))
5778 return 0;
5779
David Woodhouse49593bf2008-08-17 17:08:36 +01005780 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005781 if (!path)
5782 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005783
Josef Bacik23b5ec72017-07-24 15:14:25 -04005784 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005785 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005786
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005787 INIT_LIST_HEAD(&ins_list);
5788 INIT_LIST_HEAD(&del_list);
5789 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005790
Josef Bacik23b5ec72017-07-24 15:14:25 -04005791again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005792 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005793 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005794 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005795
Chris Mason39279cc2007-06-12 06:35:45 -04005796 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5797 if (ret < 0)
5798 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005799
5800 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005801 struct dir_entry *entry;
5802
Chris Mason5f39d392007-10-15 16:14:19 -04005803 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005804 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005805 if (slot >= btrfs_header_nritems(leaf)) {
5806 ret = btrfs_next_leaf(root, path);
5807 if (ret < 0)
5808 goto err;
5809 else if (ret > 0)
5810 break;
5811 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005812 }
Chris Mason3de45862008-11-17 21:02:50 -05005813
Chris Mason5f39d392007-10-15 16:14:19 -04005814 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5815
5816 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005817 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005818 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005819 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005820 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005821 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005822 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005823 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005824 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005825 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005826 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5827 PAGE_SIZE) {
5828 btrfs_release_path(path);
5829 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5830 if (ret)
5831 goto nopos;
5832 addr = private->filldir_buf;
5833 entries = 0;
5834 total_len = 0;
5835 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005836 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005837
5838 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005839 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005840 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005841 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5842 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005843 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005844 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005845 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005846 put_unaligned(location.objectid, &entry->ino);
5847 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005848 entries++;
5849 addr += sizeof(struct dir_entry) + name_len;
5850 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005851next:
5852 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005853 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005854 btrfs_release_path(path);
5855
5856 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5857 if (ret)
5858 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005859
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005860 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005861 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005862 goto nopos;
5863
Zach Browndb62efb2013-07-11 16:19:42 -07005864 /*
5865 * Stop new entries from being returned after we return the last
5866 * entry.
5867 *
5868 * New directory entries are assigned a strictly increasing
5869 * offset. This means that new entries created during readdir
5870 * are *guaranteed* to be seen in the future by that readdir.
5871 * This has broken buggy programs which operate on names as
5872 * they're returned by readdir. Until we re-use freed offsets
5873 * we have this hack to stop new entries from being returned
5874 * under the assumption that they'll never reach this huge
5875 * offset.
5876 *
5877 * This is being careful not to overflow 32bit loff_t unless the
5878 * last entry requires it because doing so has broken 32bit apps
5879 * in the past.
5880 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005881 if (ctx->pos >= INT_MAX)
5882 ctx->pos = LLONG_MAX;
5883 else
5884 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005885nopos:
5886 ret = 0;
5887err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005888 if (put)
5889 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005890 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005891 return ret;
5892}
5893
Chris Mason39279cc2007-06-12 06:35:45 -04005894/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005895 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005896 * inode changes. But, it is most likely to find the inode in cache.
5897 * FIXME, needs more benchmarking...there are no reasons other than performance
5898 * to keep or drop this code.
5899 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005900static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005901{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005902 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005903 struct btrfs_root *root = BTRFS_I(inode)->root;
5904 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005905 int ret;
5906
Josef Bacik72ac3c02012-05-23 14:13:11 -04005907 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005908 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005909
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005910 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005911 if (IS_ERR(trans))
5912 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005913
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005914 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04005915 if (ret && ret == -ENOSPC) {
5916 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005917 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005918 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005919 if (IS_ERR(trans))
5920 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005921
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005922 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04005923 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005924 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005925 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005926 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005927
5928 return ret;
5929}
5930
5931/*
5932 * This is a copy of file_update_time. We need this so we can return error on
5933 * ENOSPC for updating the inode in the case of file write and mmap writes.
5934 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005935static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005936 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005937{
Alexander Block2bc5565282012-06-15 09:49:33 +02005938 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005939 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005940
5941 if (btrfs_root_readonly(root))
5942 return -EROFS;
5943
Josef Bacike41f9412012-03-26 09:46:47 -04005944 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005945 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005946 if (flags & S_CTIME)
5947 inode->i_ctime = *now;
5948 if (flags & S_MTIME)
5949 inode->i_mtime = *now;
5950 if (flags & S_ATIME)
5951 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005952 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005953}
5954
Chris Masond352ac62008-09-29 15:18:18 -04005955/*
5956 * find the highest existing sequence number in a directory
5957 * and then set the in-memory index_cnt variable to reflect
5958 * free sequence numbers
5959 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005960static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005961{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005962 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005963 struct btrfs_key key, found_key;
5964 struct btrfs_path *path;
5965 struct extent_buffer *leaf;
5966 int ret;
5967
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005968 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005969 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005970 key.offset = (u64)-1;
5971
5972 path = btrfs_alloc_path();
5973 if (!path)
5974 return -ENOMEM;
5975
5976 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5977 if (ret < 0)
5978 goto out;
5979 /* FIXME: we should be able to handle this */
5980 if (ret == 0)
5981 goto out;
5982 ret = 0;
5983
5984 /*
5985 * MAGIC NUMBER EXPLANATION:
5986 * since we search a directory based on f_pos we have to start at 2
5987 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5988 * else has to start at 2
5989 */
5990 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005991 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005992 goto out;
5993 }
5994
5995 path->slots[0]--;
5996
5997 leaf = path->nodes[0];
5998 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5999
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006000 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006001 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006002 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006003 goto out;
6004 }
6005
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006006 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006007out:
6008 btrfs_free_path(path);
6009 return ret;
6010}
6011
Chris Masond352ac62008-09-29 15:18:18 -04006012/*
6013 * helper to find a free sequence number in a given directory. This current
6014 * code is very simple, later versions will do smarter things in the btree
6015 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006016int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006017{
6018 int ret = 0;
6019
Nikolay Borisov877574e2017-02-20 13:50:33 +02006020 if (dir->index_cnt == (u64)-1) {
6021 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006022 if (ret) {
6023 ret = btrfs_set_inode_index_count(dir);
6024 if (ret)
6025 return ret;
6026 }
Josef Bacikaec74772008-07-24 12:12:38 -04006027 }
6028
Nikolay Borisov877574e2017-02-20 13:50:33 +02006029 *index = dir->index_cnt;
6030 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006031
6032 return ret;
6033}
6034
Chris Masonb0d5d102014-09-08 13:08:51 -07006035static int btrfs_insert_inode_locked(struct inode *inode)
6036{
6037 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006038
6039 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006040 args.root = BTRFS_I(inode)->root;
6041
6042 return insert_inode_locked4(inode,
6043 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6044 btrfs_find_actor, &args);
6045}
6046
Anand Jain19aee8d2017-07-18 17:37:05 +08006047/*
6048 * Inherit flags from the parent inode.
6049 *
6050 * Currently only the compression flags and the cow flags are inherited.
6051 */
6052static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6053{
6054 unsigned int flags;
6055
6056 if (!dir)
6057 return;
6058
6059 flags = BTRFS_I(dir)->flags;
6060
6061 if (flags & BTRFS_INODE_NOCOMPRESS) {
6062 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6063 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6064 } else if (flags & BTRFS_INODE_COMPRESS) {
6065 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6066 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6067 }
6068
6069 if (flags & BTRFS_INODE_NODATACOW) {
6070 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6071 if (S_ISREG(inode->i_mode))
6072 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6073 }
6074
David Sterba7b6a2212018-03-26 18:40:21 +02006075 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006076}
6077
Chris Mason39279cc2007-06-12 06:35:45 -04006078static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6079 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006080 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006081 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006082 u64 ref_objectid, u64 objectid,
6083 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006084{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006085 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006086 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006087 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006088 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006089 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006090 struct btrfs_inode_ref *ref;
6091 struct btrfs_key key[2];
6092 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006093 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006094 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006095 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006096 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006097
Chris Mason5f39d392007-10-15 16:14:19 -04006098 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006099 if (!path)
6100 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006101
Josef Bacik11a19a92019-09-09 10:12:04 -04006102 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006103 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006104 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006105 if (!inode) {
6106 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006107 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006108 }
Chris Mason39279cc2007-06-12 06:35:45 -04006109
Li Zefan581bb052011-04-20 10:06:11 +08006110 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006111 * O_TMPFILE, set link count to 0, so that after this point,
6112 * we fill in an inode item with the correct link count.
6113 */
6114 if (!name)
6115 set_nlink(inode, 0);
6116
6117 /*
Li Zefan581bb052011-04-20 10:06:11 +08006118 * we have to initialize this early, so we can reclaim the inode
6119 * number if we fail afterwards in this function.
6120 */
6121 inode->i_ino = objectid;
6122
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006123 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006124 trace_btrfs_inode_request(dir);
6125
Nikolay Borisov877574e2017-02-20 13:50:33 +02006126 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006127 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006128 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006129 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006130 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006131 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006132 } else if (dir) {
6133 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006134 }
6135 /*
6136 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006137 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006138 * number
6139 */
6140 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006141 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006142 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006143 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006144 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006145
Josef Bacik5dc562c2012-08-17 13:14:17 -04006146 /*
6147 * We could have gotten an inode number from somebody who was fsynced
6148 * and then removed in this same transaction, so let's just set full
6149 * sync since it will be a full sync anyway and this will blow away the
6150 * old info in the log.
6151 */
6152 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6153
Chris Mason9c583092008-01-29 15:15:18 -05006154 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006155 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006156 key[0].offset = 0;
6157
Chris Mason9c583092008-01-29 15:15:18 -05006158 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006159
6160 if (name) {
6161 /*
6162 * Start new inodes with an inode_ref. This is slightly more
6163 * efficient for small numbers of hard links since they will
6164 * be packed into one item. Extended refs will kick in if we
6165 * add more hard links than can fit in the ref item.
6166 */
6167 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006168 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006169 key[1].offset = ref_objectid;
6170
6171 sizes[1] = name_len + sizeof(*ref);
6172 }
Chris Mason9c583092008-01-29 15:15:18 -05006173
Chris Masonb0d5d102014-09-08 13:08:51 -07006174 location = &BTRFS_I(inode)->location;
6175 location->objectid = objectid;
6176 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006177 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006178
6179 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006180 if (ret < 0) {
6181 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006182 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006183 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006184
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006185 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006186 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006187 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006188
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006189 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006190 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306191
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006192 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306193 inode->i_atime = inode->i_mtime;
6194 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006195 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306196
Chris Mason5f39d392007-10-15 16:14:19 -04006197 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6198 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006199 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006200 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006201 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006202
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006203 if (name) {
6204 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6205 struct btrfs_inode_ref);
6206 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6207 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6208 ptr = (unsigned long)(ref + 1);
6209 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6210 }
Chris Mason9c583092008-01-29 15:15:18 -05006211
Chris Mason5f39d392007-10-15 16:14:19 -04006212 btrfs_mark_buffer_dirty(path->nodes[0]);
6213 btrfs_free_path(path);
6214
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006215 btrfs_inherit_iflags(inode, dir);
6216
Al Viro569254b2011-07-24 17:08:40 -04006217 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006218 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006219 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006220 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006221 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6222 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006223 }
6224
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006225 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006226
6227 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006228 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006229
Alexander Block8ea05e32012-07-25 17:35:53 +02006230 btrfs_update_root_times(trans, root);
6231
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006232 ret = btrfs_inode_inherit_props(trans, inode, dir);
6233 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006234 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006235 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006236 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006237
Chris Mason39279cc2007-06-12 06:35:45 -04006238 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006239
6240fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006241 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006242fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006243 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006244 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006245 btrfs_free_path(path);
6246 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006247}
6248
Chris Masond352ac62008-09-29 15:18:18 -04006249/*
6250 * utility function to add 'inode' into 'parent_inode' with
6251 * a give name and a given sequence number.
6252 * if 'add_backref' is true, also insert a backref from the
6253 * inode to the parent directory.
6254 */
Chris Masone02119d2008-09-05 16:13:11 -04006255int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006256 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006257 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006258{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006259 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006260 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006261 struct btrfs_root *root = parent_inode->root;
6262 u64 ino = btrfs_ino(inode);
6263 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006264
Li Zefan33345d012011-04-20 10:31:50 +08006265 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006266 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006267 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006268 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006269 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006270 key.offset = 0;
6271 }
Chris Mason39279cc2007-06-12 06:35:45 -04006272
Li Zefan33345d012011-04-20 10:31:50 +08006273 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006274 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006275 root->root_key.objectid, parent_ino,
6276 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006277 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006278 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6279 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006280 }
6281
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006282 /* Nothing to clean up yet */
6283 if (ret)
6284 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006285
Lu Fengqi684572d2018-08-04 21:10:57 +08006286 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006287 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006288 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006289 goto fail_dir_item;
6290 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006291 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006292 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006293 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006294
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006295 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006296 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006297 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006298 /*
6299 * If we are replaying a log tree, we do not want to update the mtime
6300 * and ctime of the parent directory with the current time, since the
6301 * log replay procedure is responsible for setting them to their correct
6302 * values (the ones it had when the fsync was done).
6303 */
6304 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6305 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6306
6307 parent_inode->vfs_inode.i_mtime = now;
6308 parent_inode->vfs_inode.i_ctime = now;
6309 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006310 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006311 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006312 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006313 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006314
6315fail_dir_item:
6316 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6317 u64 local_index;
6318 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006319 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006320 root->root_key.objectid, parent_ino,
6321 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006322 if (err)
6323 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006324 } else if (add_backref) {
6325 u64 local_index;
6326 int err;
6327
6328 err = btrfs_del_inode_ref(trans, root, name, name_len,
6329 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006330 if (err)
6331 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006332 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006333
6334 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006335 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006336}
6337
6338static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006339 struct btrfs_inode *dir, struct dentry *dentry,
6340 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006341{
Josef Bacika1b075d2010-11-19 20:36:11 +00006342 int err = btrfs_add_link(trans, dir, inode,
6343 dentry->d_name.name, dentry->d_name.len,
6344 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006345 if (err > 0)
6346 err = -EEXIST;
6347 return err;
6348}
6349
Josef Bacik618e21d2007-07-11 10:18:17 -04006350static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006351 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006352{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006353 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006354 struct btrfs_trans_handle *trans;
6355 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006356 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006357 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006358 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006359 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006360
Josef Bacik9ed74f22009-09-11 16:12:44 -04006361 /*
6362 * 2 for inode item and ref
6363 * 2 for dir items
6364 * 1 for xattr if selinux is on
6365 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006366 trans = btrfs_start_transaction(root, 5);
6367 if (IS_ERR(trans))
6368 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006369
Li Zefan581bb052011-04-20 10:06:11 +08006370 err = btrfs_find_free_ino(root, &objectid);
6371 if (err)
6372 goto out_unlock;
6373
Josef Bacikaec74772008-07-24 12:12:38 -04006374 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006375 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6376 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006377 if (IS_ERR(inode)) {
6378 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006379 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006380 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006381 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006382
Casey Schauflerad19db72011-12-15 10:09:07 -05006383 /*
6384 * If the active LSM wants to access the inode during
6385 * d_instantiate it needs these. Smack checks to see
6386 * if the filesystem supports xattrs by looking at the
6387 * ops vector.
6388 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006389 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006390 init_special_inode(inode, inode->i_mode, rdev);
6391
6392 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006393 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006394 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006395
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006396 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6397 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006398 if (err)
6399 goto out_unlock;
6400
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006401 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006402 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006403
Josef Bacik618e21d2007-07-11 10:18:17 -04006404out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006405 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006406 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006407 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006408 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006409 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006410 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006411 return err;
6412}
6413
Chris Mason39279cc2007-06-12 06:35:45 -04006414static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006415 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006416{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006417 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006418 struct btrfs_trans_handle *trans;
6419 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006420 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006421 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006422 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006423 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006424
Josef Bacik9ed74f22009-09-11 16:12:44 -04006425 /*
6426 * 2 for inode item and ref
6427 * 2 for dir items
6428 * 1 for xattr if selinux is on
6429 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006430 trans = btrfs_start_transaction(root, 5);
6431 if (IS_ERR(trans))
6432 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006433
Li Zefan581bb052011-04-20 10:06:11 +08006434 err = btrfs_find_free_ino(root, &objectid);
6435 if (err)
6436 goto out_unlock;
6437
Josef Bacikaec74772008-07-24 12:12:38 -04006438 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006439 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6440 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006441 if (IS_ERR(inode)) {
6442 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006443 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006444 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006445 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006446 /*
6447 * If the active LSM wants to access the inode during
6448 * d_instantiate it needs these. Smack checks to see
6449 * if the filesystem supports xattrs by looking at the
6450 * ops vector.
6451 */
6452 inode->i_fop = &btrfs_file_operations;
6453 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006454 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006455
6456 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6457 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006458 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006459
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006460 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006461 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006462 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006463
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006464 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6465 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006466 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006467 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006468
Al Viro1e2e5472018-05-04 08:23:01 -04006469 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006470
Chris Mason39279cc2007-06-12 06:35:45 -04006471out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006472 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006473 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006474 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006475 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006476 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006477 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006478 return err;
6479}
6480
6481static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6482 struct dentry *dentry)
6483{
Filipe Manana271dba452016-01-05 16:24:05 +00006484 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006485 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006486 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006487 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006488 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006489 int err;
6490 int drop_inode = 0;
6491
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006492 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006493 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006494 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006495
Mark Fashehf1863732012-08-08 11:32:27 -07006496 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006497 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006498
Nikolay Borisov877574e2017-02-20 13:50:33 +02006499 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006500 if (err)
6501 goto fail;
6502
Yan, Zhenga22285a2010-05-16 10:48:46 -04006503 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006504 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006505 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006506 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006507 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006508 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006509 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006510 if (IS_ERR(trans)) {
6511 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006512 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006513 goto fail;
6514 }
Chris Mason5f39d392007-10-15 16:14:19 -04006515
Miao Xie67de1172013-12-26 13:07:06 +08006516 /* There are several dir indexes for this inode, clear the cache. */
6517 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006518 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006519 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006520 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006521 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006522 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006523
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006524 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6525 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006526
Yan, Zhenga5719522009-09-24 09:17:31 -04006527 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006528 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006529 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006530 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006531
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006532 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006533 if (err)
6534 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006535 if (inode->i_nlink == 1) {
6536 /*
6537 * If new hard link count is 1, it's a file created
6538 * with open(2) O_TMPFILE flag.
6539 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006540 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006541 if (err)
6542 goto fail;
6543 }
Al Viro08c422c2011-12-23 07:58:13 -05006544 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006545 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006546 }
Chris Mason39279cc2007-06-12 06:35:45 -04006547
Chris Mason1832a6d2007-12-21 16:27:21 -05006548fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006549 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006550 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006551 if (drop_inode) {
6552 inode_dec_link_count(inode);
6553 iput(inode);
6554 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006555 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006556 return err;
6557}
6558
Al Viro18bb1db2011-07-26 01:41:39 -04006559static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006560{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006561 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006562 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006563 struct btrfs_trans_handle *trans;
6564 struct btrfs_root *root = BTRFS_I(dir)->root;
6565 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006566 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006567 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006568
Josef Bacik9ed74f22009-09-11 16:12:44 -04006569 /*
6570 * 2 items for inode and ref
6571 * 2 items for dir items
6572 * 1 for xattr if selinux is on
6573 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006574 trans = btrfs_start_transaction(root, 5);
6575 if (IS_ERR(trans))
6576 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006577
Li Zefan581bb052011-04-20 10:06:11 +08006578 err = btrfs_find_free_ino(root, &objectid);
6579 if (err)
6580 goto out_fail;
6581
Josef Bacikaec74772008-07-24 12:12:38 -04006582 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006583 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6584 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006585 if (IS_ERR(inode)) {
6586 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006587 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006588 goto out_fail;
6589 }
Chris Mason5f39d392007-10-15 16:14:19 -04006590
Chris Masonb0d5d102014-09-08 13:08:51 -07006591 /* these must be set before we unlock the inode */
6592 inode->i_op = &btrfs_dir_inode_operations;
6593 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006594
Eric Paris2a7dba32011-02-01 11:05:39 -05006595 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006596 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006597 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006598
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006599 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006600 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006601 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006602 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006603
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006604 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6605 dentry->d_name.name,
6606 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006607 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006608 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006609
Al Viro1e2e5472018-05-04 08:23:01 -04006610 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006611
6612out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006613 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006614 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006615 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006616 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006617 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006618 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006619 return err;
6620}
6621
Chris Masonc8b97812008-10-29 14:49:59 -04006622static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006623 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006624 size_t pg_offset, u64 extent_offset,
6625 struct btrfs_file_extent_item *item)
6626{
6627 int ret;
6628 struct extent_buffer *leaf = path->nodes[0];
6629 char *tmp;
6630 size_t max_size;
6631 unsigned long inline_size;
6632 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006633 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006634
6635 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006636 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006637 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6638 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006639 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006640 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006641 if (!tmp)
6642 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006643 ptr = btrfs_file_extent_inline_start(item);
6644
6645 read_extent_buffer(leaf, tmp, ptr, inline_size);
6646
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006647 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006648 ret = btrfs_decompress(compress_type, tmp, page,
6649 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006650
6651 /*
6652 * decompression code contains a memset to fill in any space between the end
6653 * of the uncompressed data and the end of max_size in case the decompressed
6654 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6655 * the end of an inline extent and the beginning of the next block, so we
6656 * cover that region here.
6657 */
6658
6659 if (max_size + pg_offset < PAGE_SIZE) {
6660 char *map = kmap(page);
6661 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6662 kunmap(page);
6663 }
Chris Masonc8b97812008-10-29 14:49:59 -04006664 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006665 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006666}
6667
Omar Sandoval39b07b52019-12-02 17:34:23 -08006668/**
6669 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6670 * @inode: file to search in
6671 * @page: page to read extent data into if the extent is inline
6672 * @pg_offset: offset into @page to copy to
6673 * @start: file offset
6674 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006675 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006676 * This returns the first &struct extent_map which overlaps with the given
6677 * range, reading it from the B-tree and caching it if necessary. Note that
6678 * there may be more extents which overlap the given range after the returned
6679 * extent_map.
6680 *
6681 * If @page is not NULL and the extent is inline, this also reads the extent
6682 * data directly into the page and marks the extent up to date in the io_tree.
6683 *
6684 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006685 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006686struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006687 struct page *page, size_t pg_offset,
6688 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006689{
David Sterba3ffbd682018-06-29 10:56:42 +02006690 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006691 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006692 u64 extent_start = 0;
6693 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006694 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006695 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006696 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006697 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006698 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006699 struct extent_buffer *leaf;
6700 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006701 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006702 struct extent_map_tree *em_tree = &inode->extent_tree;
6703 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006704
Chris Mason890871b2009-09-02 16:24:52 -04006705 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006706 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006707 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006708
Chris Masona52d9a82007-08-27 16:49:44 -04006709 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006710 if (em->start > start || em->start + em->len <= start)
6711 free_extent_map(em);
6712 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006713 free_extent_map(em);
6714 else
6715 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006716 }
David Sterba172ddd62011-04-21 00:48:27 +02006717 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006718 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006719 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006720 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006721 }
Chris Masond1310b22008-01-24 16:13:08 -05006722 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006723 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006724 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006725 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006726
Liu Bobee6ec82018-08-17 05:05:28 +08006727 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006728 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006729 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006730 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006731 }
6732
Liu Bobee6ec82018-08-17 05:05:28 +08006733 /* Chances are we'll be called again, so go ahead and do readahead */
6734 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04006735
6736 /*
6737 * The same explanation in load_free_space_cache applies here as well,
6738 * we only read when we're loading the free space cache, and at that
6739 * point the commit_root has everything we need.
6740 */
6741 if (btrfs_is_free_space_inode(inode)) {
6742 path->search_commit_root = 1;
6743 path->skip_locking = 1;
6744 }
Josef Bacik51899412020-08-20 11:46:01 -04006745
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006746 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006747 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006748 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006749 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006750 if (path->slots[0] == 0)
6751 goto not_found;
6752 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006753 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006754 }
6755
Chris Mason5f39d392007-10-15 16:14:19 -04006756 leaf = path->nodes[0];
6757 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006758 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006759 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006760 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006761 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006762 /*
6763 * If we backup past the first extent we want to move forward
6764 * and see if there is an extent in front of us, otherwise we'll
6765 * say there is a hole for our whole search range which can
6766 * cause problems.
6767 */
6768 extent_end = start;
6769 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006770 }
6771
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006772 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006773 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006774 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006775 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6776 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006777 /* Only regular file could have regular/prealloc extent */
6778 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006779 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006780 btrfs_crit(fs_info,
6781 "regular/prealloc extent found for non-regular inode %llu",
6782 btrfs_ino(inode));
6783 goto out;
6784 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006785 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6786 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006787 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006788 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6789 path->slots[0],
6790 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006791 }
Josef Bacik25a50342013-10-14 12:08:38 -04006792next:
Yan Zheng9036c102008-10-30 14:19:41 -04006793 if (start >= extent_end) {
6794 path->slots[0]++;
6795 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6796 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006797 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006798 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006799 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006800 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006801
Yan Zheng9036c102008-10-30 14:19:41 -04006802 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006803 }
Yan Zheng9036c102008-10-30 14:19:41 -04006804 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6805 if (found_key.objectid != objectid ||
6806 found_key.type != BTRFS_EXTENT_DATA_KEY)
6807 goto not_found;
6808 if (start + len <= found_key.offset)
6809 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006810 if (start > found_key.offset)
6811 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006812
6813 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006814 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006815 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006816 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006817 em->block_start = EXTENT_MAP_HOLE;
6818 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006819 }
6820
Omar Sandoval39b07b52019-12-02 17:34:23 -08006821 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006822
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006823 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6824 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006825 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006826 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006827 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006828 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006829 size_t size;
6830 size_t extent_offset;
6831 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006832
Omar Sandoval39b07b52019-12-02 17:34:23 -08006833 if (!page)
Yan689f9342007-10-29 11:41:07 -04006834 goto out;
Yan689f9342007-10-29 11:41:07 -04006835
Qu Wenruoe41ca582018-06-06 15:41:49 +08006836 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006837 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006838 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6839 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006840 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006841 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006842 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006843 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006844 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006845
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006846 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006847 if (btrfs_file_extent_compression(leaf, item) !=
6848 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006849 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006850 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006851 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04006852 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04006853 } else {
6854 map = kmap(page);
6855 read_extent_buffer(leaf, map + pg_offset, ptr,
6856 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006857 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006858 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006859 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006860 copy_size);
6861 }
Chris Masonc8b97812008-10-29 14:49:59 -04006862 kunmap(page);
6863 }
Chris Mason179e29e2007-11-01 11:28:41 -04006864 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006865 }
Chris Masond1310b22008-01-24 16:13:08 -05006866 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006867 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006868 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006869 }
6870not_found:
6871 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006872 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006873 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006874 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006875insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006876 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02006877 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006878 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006879 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006880 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6881 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006882 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04006883 goto out;
6884 }
Chris Masond1310b22008-01-24 16:13:08 -05006885
Chris Mason890871b2009-09-02 16:24:52 -04006886 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006887 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006888 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006889out:
Liu Boc6414282018-08-23 07:36:17 +08006890 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006891
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006892 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006893
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006894 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04006895 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006896 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04006897 }
6898 return em;
6899}
6900
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006901struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006902 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006903{
6904 struct extent_map *em;
6905 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006906 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006907 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006908 u64 delalloc_len;
6909 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006910 int err = 0;
6911
Omar Sandoval39b07b52019-12-02 17:34:23 -08006912 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006913 if (IS_ERR(em))
6914 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006915 /*
6916 * If our em maps to:
6917 * - a hole or
6918 * - a pre-alloc extent,
6919 * there might actually be delalloc bytes behind it.
6920 */
6921 if (em->block_start != EXTENT_MAP_HOLE &&
6922 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6923 return em;
6924 else
6925 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006926
6927 /* check to see if we've wrapped (len == -1 or similar) */
6928 end = start + len;
6929 if (end < start)
6930 end = (u64)-1;
6931 else
6932 end -= 1;
6933
6934 em = NULL;
6935
6936 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006937 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006938 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006939 delalloc_end = delalloc_start + delalloc_len;
6940 if (delalloc_end < delalloc_start)
6941 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006942
6943 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006944 * We didn't find anything useful, return the original results from
6945 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006946 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006947 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006948 em = hole_em;
6949 hole_em = NULL;
6950 goto out;
6951 }
6952
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006953 /*
6954 * Adjust the delalloc_start to make sure it doesn't go backwards from
6955 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006956 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006957 delalloc_start = max(start, delalloc_start);
6958 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006959
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006960 if (delalloc_len > 0) {
6961 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006962 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006963 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006964
David Sterba172ddd62011-04-21 00:48:27 +02006965 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006966 if (!em) {
6967 err = -ENOMEM;
6968 goto out;
6969 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006970
6971 ASSERT(hole_em);
6972 /*
6973 * When btrfs_get_extent can't find anything it returns one
6974 * huge hole
6975 *
6976 * Make sure what it found really fits our range, and adjust to
6977 * make sure it is based on the start from the caller
6978 */
6979 if (hole_end <= start || hole_em->start > end) {
6980 free_extent_map(hole_em);
6981 hole_em = NULL;
6982 } else {
6983 hole_start = max(hole_em->start, start);
6984 hole_len = hole_end - hole_start;
6985 }
6986
6987 if (hole_em && delalloc_start > hole_start) {
6988 /*
6989 * Our hole starts before our delalloc, so we have to
6990 * return just the parts of the hole that go until the
6991 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006992 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006993 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006994 em->start = hole_start;
6995 em->orig_start = hole_start;
6996 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006997 * Don't adjust block start at all, it is fixed at
6998 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05006999 */
7000 em->block_start = hole_em->block_start;
7001 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007002 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7003 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007004 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007005 /*
7006 * Hole is out of passed range or it starts after
7007 * delalloc range
7008 */
7009 em->start = delalloc_start;
7010 em->len = delalloc_len;
7011 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007012 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007013 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007014 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007015 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007016 return hole_em;
7017 }
7018out:
7019
7020 free_extent_map(hole_em);
7021 if (err) {
7022 free_extent_map(em);
7023 return ERR_PTR(err);
7024 }
7025 return em;
7026}
7027
Nikolay Borisov64f54182020-06-03 08:55:31 +03007028static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007029 const u64 start,
7030 const u64 len,
7031 const u64 orig_start,
7032 const u64 block_start,
7033 const u64 block_len,
7034 const u64 orig_block_len,
7035 const u64 ram_bytes,
7036 const int type)
7037{
7038 struct extent_map *em = NULL;
7039 int ret;
7040
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007041 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007042 em = create_io_em(inode, start, len, orig_start, block_start,
7043 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007044 BTRFS_COMPRESS_NONE, /* compress_type */
7045 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007046 if (IS_ERR(em))
7047 goto out;
7048 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007049 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7050 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007051 if (ret) {
7052 if (em) {
7053 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007054 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007055 }
7056 em = ERR_PTR(ret);
7057 }
7058 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007059
7060 return em;
7061}
7062
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007063static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007064 u64 start, u64 len)
7065{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007066 struct btrfs_root *root = inode->root;
7067 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007068 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007069 struct btrfs_key ins;
7070 u64 alloc_hint;
7071 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007072
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007073 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007074 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007075 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007076 if (ret)
7077 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007078
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007079 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007080 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007081 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007082 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007083 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007084 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7085 1);
Josef Bacik00361582013-08-14 14:02:47 -04007086
Josef Bacik4b46fce2010-05-23 11:00:55 -04007087 return em;
7088}
7089
Chris Mason46bfbb52010-05-26 11:04:10 -04007090/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007091 * Check if we can do nocow write into the range [@offset, @offset + @len)
7092 *
7093 * @offset: File offset
7094 * @len: The length to write, will be updated to the nocow writeable
7095 * range
7096 * @orig_start: (optional) Return the original file offset of the file extent
7097 * @orig_len: (optional) Return the original on-disk length of the file extent
7098 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007099 * @strict: if true, omit optimizations that might force us into unnecessary
7100 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007101 *
7102 * This function will flush ordered extents in the range to ensure proper
7103 * nocow checks for (nowait == false) case.
7104 *
7105 * Return:
7106 * >0 and update @len if we can do nocow write
7107 * 0 if we can't do nocow write
7108 * <0 if error happened
7109 *
7110 * NOTE: This only checks the file extents, caller is responsible to wait for
7111 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007112 */
Josef Bacik00361582013-08-14 14:02:47 -04007113noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007114 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007115 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007116{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007117 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007118 struct btrfs_path *path;
7119 int ret;
7120 struct extent_buffer *leaf;
7121 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007122 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007123 struct btrfs_file_extent_item *fi;
7124 struct btrfs_key key;
7125 u64 disk_bytenr;
7126 u64 backref_offset;
7127 u64 extent_end;
7128 u64 num_bytes;
7129 int slot;
7130 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007131 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007132
Chris Mason46bfbb52010-05-26 11:04:10 -04007133 path = btrfs_alloc_path();
7134 if (!path)
7135 return -ENOMEM;
7136
David Sterbaf85b7372017-01-20 14:54:07 +01007137 ret = btrfs_lookup_file_extent(NULL, root, path,
7138 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007139 if (ret < 0)
7140 goto out;
7141
7142 slot = path->slots[0];
7143 if (ret == 1) {
7144 if (slot == 0) {
7145 /* can't find the item, must cow */
7146 ret = 0;
7147 goto out;
7148 }
7149 slot--;
7150 }
7151 ret = 0;
7152 leaf = path->nodes[0];
7153 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007154 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007155 key.type != BTRFS_EXTENT_DATA_KEY) {
7156 /* not our file or wrong item type, must cow */
7157 goto out;
7158 }
7159
7160 if (key.offset > offset) {
7161 /* Wrong offset, must cow */
7162 goto out;
7163 }
7164
7165 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7166 found_type = btrfs_file_extent_type(leaf, fi);
7167 if (found_type != BTRFS_FILE_EXTENT_REG &&
7168 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7169 /* not a regular extent, must cow */
7170 goto out;
7171 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007172
7173 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7174 goto out;
7175
Miao Xiee77751a2013-12-27 21:11:50 +08007176 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7177 if (extent_end <= offset)
7178 goto out;
7179
Chris Mason46bfbb52010-05-26 11:04:10 -04007180 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007181 if (disk_bytenr == 0)
7182 goto out;
7183
7184 if (btrfs_file_extent_compression(leaf, fi) ||
7185 btrfs_file_extent_encryption(leaf, fi) ||
7186 btrfs_file_extent_other_encoding(leaf, fi))
7187 goto out;
7188
Ethan Lien78d42952018-05-17 14:58:29 +08007189 /*
7190 * Do the same check as in btrfs_cross_ref_exist but without the
7191 * unnecessary search.
7192 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007193 if (!strict &&
7194 (btrfs_file_extent_generation(leaf, fi) <=
7195 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007196 goto out;
7197
Chris Mason46bfbb52010-05-26 11:04:10 -04007198 backref_offset = btrfs_file_extent_offset(leaf, fi);
7199
Josef Bacik7ee9e442013-06-21 16:37:03 -04007200 if (orig_start) {
7201 *orig_start = key.offset - backref_offset;
7202 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7203 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7204 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007205
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007206 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007207 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007208
7209 num_bytes = min(offset + *len, extent_end) - offset;
7210 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7211 u64 range_end;
7212
Jeff Mahoneyda170662016-06-15 09:22:56 -04007213 range_end = round_up(offset + num_bytes,
7214 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007215 ret = test_range_bit(io_tree, offset, range_end,
7216 EXTENT_DELALLOC, 0, NULL);
7217 if (ret) {
7218 ret = -EAGAIN;
7219 goto out;
7220 }
7221 }
7222
Josef Bacik1bda19e2013-10-18 12:10:36 -04007223 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007224
7225 /*
7226 * look for other files referencing this extent, if we
7227 * find any we must cow
7228 */
Josef Bacik00361582013-08-14 14:02:47 -04007229
Liu Boe4c3b2d2017-01-30 12:25:28 -08007230 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007231 key.offset - backref_offset, disk_bytenr,
7232 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007233 if (ret) {
7234 ret = 0;
7235 goto out;
7236 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007237
7238 /*
7239 * adjust disk_bytenr and num_bytes to cover just the bytes
7240 * in this extent we are about to write. If there
7241 * are any csums in that range we have to cow in order
7242 * to keep the csums correct
7243 */
7244 disk_bytenr += backref_offset;
7245 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007246 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7247 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007248 /*
7249 * all of the above have passed, it is safe to overwrite this extent
7250 * without cow
7251 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007252 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007253 ret = 1;
7254out:
7255 btrfs_free_path(path);
7256 return ret;
7257}
7258
Josef Bacikeb838e72012-07-31 16:28:48 -04007259static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007260 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007261{
7262 struct btrfs_ordered_extent *ordered;
7263 int ret = 0;
7264
7265 while (1) {
7266 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007267 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007268 /*
7269 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007270 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007271 * extents in this range.
7272 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007273 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007274 lockend - lockstart + 1);
7275
7276 /*
7277 * We need to make sure there are no buffered pages in this
7278 * range either, we could have raced between the invalidate in
7279 * generic_file_direct_write and locking the extent. The
7280 * invalidate needs to happen so that reads after a write do not
7281 * get stale data.
7282 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007283 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007284 (!writing || !filemap_range_has_page(inode->i_mapping,
7285 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007286 break;
7287
7288 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007289 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007290
7291 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007292 /*
7293 * If we are doing a DIO read and the ordered extent we
7294 * found is for a buffered write, we can not wait for it
7295 * to complete and retry, because if we do so we can
7296 * deadlock with concurrent buffered writes on page
7297 * locks. This happens only if our DIO read covers more
7298 * than one extent map, if at this point has already
7299 * created an ordered extent for a previous extent map
7300 * and locked its range in the inode's io tree, and a
7301 * concurrent write against that previous extent map's
7302 * range and this range started (we unlock the ranges
7303 * in the io tree only when the bios complete and
7304 * buffered writes always lock pages before attempting
7305 * to lock range in the io tree).
7306 */
7307 if (writing ||
7308 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007309 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007310 else
7311 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007312 btrfs_put_ordered_extent(ordered);
7313 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007314 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007315 * We could trigger writeback for this range (and wait
7316 * for it to complete) and then invalidate the pages for
7317 * this range (through invalidate_inode_pages2_range()),
7318 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007319 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007320 * triggered a readahead) on a page lock due to an
7321 * ordered dio extent we created before but did not have
7322 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007323 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007324 * ordered extent to complete while holding a lock on
7325 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007326 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007327 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007328 }
7329
Filipe Mananaade77022016-02-18 14:28:55 +00007330 if (ret)
7331 break;
7332
Josef Bacikeb838e72012-07-31 16:28:48 -04007333 cond_resched();
7334 }
7335
7336 return ret;
7337}
7338
Liu Bo6f9994d2017-01-31 07:50:22 -08007339/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007340static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7341 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007342 u64 block_len, u64 orig_block_len,
7343 u64 ram_bytes, int compress_type,
7344 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007345{
7346 struct extent_map_tree *em_tree;
7347 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007348 int ret;
7349
Liu Bo6f9994d2017-01-31 07:50:22 -08007350 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7351 type == BTRFS_ORDERED_COMPRESSED ||
7352 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007353 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007354
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007355 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007356 em = alloc_extent_map();
7357 if (!em)
7358 return ERR_PTR(-ENOMEM);
7359
7360 em->start = start;
7361 em->orig_start = orig_start;
7362 em->len = len;
7363 em->block_len = block_len;
7364 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007365 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007366 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007367 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007368 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007369 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007370 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007371 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007372 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7373 em->compress_type = compress_type;
7374 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007375
7376 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007377 btrfs_drop_extent_cache(inode, em->start,
7378 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007379 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007380 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007381 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007382 /*
7383 * The caller has taken lock_extent(), who could race with us
7384 * to add em?
7385 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007386 } while (ret == -EEXIST);
7387
7388 if (ret) {
7389 free_extent_map(em);
7390 return ERR_PTR(ret);
7391 }
7392
Liu Bo6f9994d2017-01-31 07:50:22 -08007393 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007394 return em;
7395}
7396
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007397
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007398static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007399 struct inode *inode,
7400 struct btrfs_dio_data *dio_data,
7401 u64 start, u64 len)
7402{
7403 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7404 struct extent_map *em = *map;
7405 int ret = 0;
7406
7407 /*
7408 * We don't allocate a new extent in the following cases
7409 *
7410 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7411 * existing extent.
7412 * 2) The extent is marked as PREALLOC. We're good to go here and can
7413 * just use the extent.
7414 *
7415 */
7416 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7417 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7418 em->block_start != EXTENT_MAP_HOLE)) {
7419 int type;
7420 u64 block_start, orig_start, orig_block_len, ram_bytes;
7421
7422 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7423 type = BTRFS_ORDERED_PREALLOC;
7424 else
7425 type = BTRFS_ORDERED_NOCOW;
7426 len = min(len, em->len - (start - em->start));
7427 block_start = em->block_start + (start - em->start);
7428
7429 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007430 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007431 btrfs_inc_nocow_writers(fs_info, block_start)) {
7432 struct extent_map *em2;
7433
Nikolay Borisov64f54182020-06-03 08:55:31 +03007434 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007435 orig_start, block_start,
7436 len, orig_block_len,
7437 ram_bytes, type);
7438 btrfs_dec_nocow_writers(fs_info, block_start);
7439 if (type == BTRFS_ORDERED_PREALLOC) {
7440 free_extent_map(em);
7441 *map = em = em2;
7442 }
7443
7444 if (em2 && IS_ERR(em2)) {
7445 ret = PTR_ERR(em2);
7446 goto out;
7447 }
7448 /*
7449 * For inode marked NODATACOW or extent marked PREALLOC,
7450 * use the existing or preallocated extent, so does not
7451 * need to adjust btrfs_space_info's bytes_may_use.
7452 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007453 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007454 goto skip_cow;
7455 }
7456 }
7457
7458 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007459 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007460 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007461 if (IS_ERR(em)) {
7462 ret = PTR_ERR(em);
7463 goto out;
7464 }
7465
7466 len = min(len, em->len - (start - em->start));
7467
7468skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007469 /*
7470 * Need to update the i_size under the extent lock so buffered
7471 * readers will get the updated i_size when we unlock.
7472 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007473 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007474 i_size_write(inode, start + len);
7475
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007476 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007477out:
7478 return ret;
7479}
7480
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007481static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7482 loff_t length, unsigned int flags, struct iomap *iomap,
7483 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007484{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007485 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007486 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007487 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307488 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007489 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007490 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007491 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007492 u64 len = length;
7493 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007494
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007495 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007496 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007497
Josef Bacikc3298612012-08-03 16:49:19 -04007498 lockstart = start;
7499 lockend = start + len - 1;
7500
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007501 /*
7502 * The generic stuff only does filemap_write_and_wait_range, which
7503 * isn't enough if we've written compressed pages to this area, so we
7504 * need to flush the dirty pages again to make absolutely sure that any
7505 * outstanding dirty pages are on disk.
7506 */
7507 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7508 &BTRFS_I(inode)->runtime_flags)) {
7509 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7510 start + length - 1);
7511 if (ret)
7512 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007513 }
7514
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007515 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7516 if (!dio_data)
7517 return -ENOMEM;
7518
7519 dio_data->length = length;
7520 if (write) {
7521 dio_data->reserve = round_up(length, fs_info->sectorsize);
7522 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7523 &dio_data->data_reserved,
7524 start, dio_data->reserve);
7525 if (ret) {
7526 extent_changeset_free(dio_data->data_reserved);
7527 kfree(dio_data);
7528 return ret;
7529 }
7530 }
7531 iomap->private = dio_data;
7532
7533
Josef Bacikeb838e72012-07-31 16:28:48 -04007534 /*
7535 * If this errors out it's because we couldn't invalidate pagecache for
7536 * this range and we need to fallback to buffered.
7537 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007538 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007539 ret = -ENOTBLK;
7540 goto err;
7541 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007542
Omar Sandoval39b07b52019-12-02 17:34:23 -08007543 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007544 if (IS_ERR(em)) {
7545 ret = PTR_ERR(em);
7546 goto unlock_err;
7547 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007548
7549 /*
7550 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7551 * io. INLINE is special, and we could probably kludge it in here, but
7552 * it's still buffered so for safety lets just fall back to the generic
7553 * buffered path.
7554 *
7555 * For COMPRESSED we _have_ to read the entire extent in so we can
7556 * decompress it, so there will be buffering required no matter what we
7557 * do, so go ahead and fallback to buffered.
7558 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007559 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007560 * to buffered IO. Don't blame me, this is the price we pay for using
7561 * the generic code.
7562 */
7563 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7564 em->block_start == EXTENT_MAP_INLINE) {
7565 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007566 ret = -ENOTBLK;
7567 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007568 }
7569
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007570 len = min(len, em->len - (start - em->start));
7571 if (write) {
7572 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7573 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007574 if (ret < 0)
7575 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007576 unlock_extents = true;
7577 /* Recalc len in case the new em is smaller than requested */
7578 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007579 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007580 /*
7581 * We need to unlock only the end area that we aren't using.
7582 * The rest is going to be unlocked by the endio routine.
7583 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007584 lockstart = start + len;
7585 if (lockstart < lockend)
7586 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007587 }
7588
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007589 if (unlock_extents)
7590 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7591 lockstart, lockend, &cached_state);
7592 else
7593 free_extent_state(cached_state);
7594
7595 /*
7596 * Translate extent map information to iomap.
7597 * We trim the extents (and move the addr) even though iomap code does
7598 * that, since we have locked only the parts we are performing I/O in.
7599 */
7600 if ((em->block_start == EXTENT_MAP_HOLE) ||
7601 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7602 iomap->addr = IOMAP_NULL_ADDR;
7603 iomap->type = IOMAP_HOLE;
7604 } else {
7605 iomap->addr = em->block_start + (start - em->start);
7606 iomap->type = IOMAP_MAPPED;
7607 }
7608 iomap->offset = start;
7609 iomap->bdev = fs_info->fs_devices->latest_bdev;
7610 iomap->length = len;
7611
Josef Bacik4b46fce2010-05-23 11:00:55 -04007612 free_extent_map(em);
7613
7614 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007615
7616unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007617 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7618 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007619err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007620 if (dio_data) {
7621 btrfs_delalloc_release_space(BTRFS_I(inode),
7622 dio_data->data_reserved, start,
7623 dio_data->reserve, true);
7624 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7625 extent_changeset_free(dio_data->data_reserved);
7626 kfree(dio_data);
7627 }
7628 return ret;
7629}
7630
7631static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7632 ssize_t written, unsigned int flags, struct iomap *iomap)
7633{
7634 int ret = 0;
7635 struct btrfs_dio_data *dio_data = iomap->private;
7636 size_t submitted = dio_data->submitted;
7637 const bool write = !!(flags & IOMAP_WRITE);
7638
7639 if (!write && (iomap->type == IOMAP_HOLE)) {
7640 /* If reading from a hole, unlock and return */
7641 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7642 goto out;
7643 }
7644
7645 if (submitted < length) {
7646 pos += submitted;
7647 length -= submitted;
7648 if (write)
7649 __endio_write_update_ordered(BTRFS_I(inode), pos,
7650 length, false);
7651 else
7652 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7653 pos + length - 1);
7654 ret = -ENOTBLK;
7655 }
7656
7657 if (write) {
7658 if (dio_data->reserve)
7659 btrfs_delalloc_release_space(BTRFS_I(inode),
7660 dio_data->data_reserved, pos,
7661 dio_data->reserve, true);
7662 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7663 extent_changeset_free(dio_data->data_reserved);
7664 }
7665out:
7666 kfree(dio_data);
7667 iomap->private = NULL;
7668
Josef Bacikeb838e72012-07-31 16:28:48 -04007669 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007670}
7671
Omar Sandoval769b4f22020-04-16 14:46:22 -07007672static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7673{
7674 /*
7675 * This implies a barrier so that stores to dio_bio->bi_status before
7676 * this and loads of dio_bio->bi_status after this are fully ordered.
7677 */
7678 if (!refcount_dec_and_test(&dip->refs))
7679 return;
7680
7681 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007682 __endio_write_update_ordered(BTRFS_I(dip->inode),
7683 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007684 dip->bytes,
7685 !dip->dio_bio->bi_status);
7686 } else {
7687 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7688 dip->logical_offset,
7689 dip->logical_offset + dip->bytes - 1);
7690 }
7691
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007692 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007693 kfree(dip);
7694}
7695
Omar Sandoval77d5d682020-04-16 14:46:25 -07007696static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7697 int mirror_num,
7698 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007699{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007700 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007701 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007702 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007703
Mike Christie37226b22016-06-05 14:31:52 -05007704 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007705
Omar Sandoval5c047a62020-04-16 14:46:24 -07007706 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007707 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007708 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007709
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007710 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007711 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007712 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007713 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007714 return ret;
7715}
7716
Omar Sandoval769b4f22020-04-16 14:46:22 -07007717static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7718 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007719 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007720{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007721 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7722 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007723 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007724 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7725 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007726 struct bio_vec bvec;
7727 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007728 u64 start = io_bio->logical;
7729 int icsum = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007730 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007731
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007732 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7733 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307734
Liu Bo17347ce2017-05-15 15:33:27 -07007735 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7736 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007737 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007738 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007739 if (uptodate &&
7740 (!csum || !check_data_csum(inode, io_bio, icsum,
Qu Wenruo265d4ac2020-10-21 14:24:54 +08007741 bvec.bv_page, pgoff))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007742 clean_io_failure(fs_info, failure_tree, io_tree,
7743 start, bvec.bv_page,
7744 btrfs_ino(BTRFS_I(inode)),
7745 pgoff);
7746 } else {
7747 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007748
Omar Sandoval77d5d682020-04-16 14:46:25 -07007749 status = btrfs_submit_read_repair(inode,
7750 &io_bio->bio,
7751 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007752 bvec.bv_page, pgoff,
7753 start,
7754 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007755 io_bio->mirror_num,
7756 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007757 if (status)
7758 err = status;
7759 }
7760 start += sectorsize;
7761 icsum++;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307762 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307763 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007764 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007765 return err;
7766}
7767
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007768static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007769 const u64 offset, const u64 bytes,
7770 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007771{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007772 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007773 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007774 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007775 u64 ordered_offset = offset;
7776 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007777 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007778
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007779 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007780 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007781 else
Qu Wenruo524272602017-03-08 10:25:52 +08007782 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007783
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007784 while (ordered_offset < offset + bytes) {
7785 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007786 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007787 &ordered_offset,
7788 ordered_bytes,
7789 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007790 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7791 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007792 btrfs_queue_work(wq, &ordered->work);
7793 }
7794 /*
7795 * If btrfs_dec_test_ordered_pending does not find any ordered
7796 * extent in the range, we can exit.
7797 */
7798 if (ordered_offset == last_offset)
7799 return;
7800 /*
7801 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007802 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007803 */
7804 if (ordered_offset < offset + bytes) {
7805 ordered_bytes = offset + bytes - ordered_offset;
7806 ordered = NULL;
7807 }
Chris Mason163cf092010-11-28 19:56:33 -05007808 }
Filipe Manana14543772015-11-24 16:23:54 +00007809}
7810
Qu Wenruo8896a082020-10-21 14:24:53 +08007811static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
7812 struct bio *bio, u64 offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007813{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09007814 return btrfs_csum_one_bio(BTRFS_I(inode), bio, offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007815}
7816
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007817static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007818{
7819 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007820 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007821
Miao Xie8b110e32014-09-12 18:44:03 +08007822 if (err)
7823 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007824 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007825 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
7826 bio->bi_opf,
Miao Xie8b110e32014-09-12 18:44:03 +08007827 (unsigned long long)bio->bi_iter.bi_sector,
7828 bio->bi_iter.bi_size, err);
7829
Omar Sandoval769b4f22020-04-16 14:46:22 -07007830 if (bio_op(bio) == REQ_OP_READ) {
7831 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007832 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007833 }
7834
Omar Sandoval769b4f22020-04-16 14:46:22 -07007835 if (err)
7836 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007837
Miao Xiee65e1532010-11-22 03:04:43 +00007838 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007839 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007840}
7841
David Sterbad0ee3932018-03-08 14:35:48 +01007842static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7843 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007844{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007845 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007846 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007847 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007848 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007849
Liu Bo4c274bc2017-11-01 17:19:27 -06007850 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007851 if (async_submit)
7852 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7853
Josef Bacik5fd02042012-05-02 14:00:54 -04007854 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007855 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007856 if (ret)
7857 goto err;
7858 }
Miao Xiee65e1532010-11-22 03:04:43 +00007859
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007860 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007861 goto map;
7862
7863 if (write && async_submit) {
Qu Wenruo8896a082020-10-21 14:24:53 +08007864 ret = btrfs_wq_submit_bio(inode, bio, 0, 0,
7865 file_offset,
David Sterbae288c082018-07-18 17:36:24 +02007866 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007867 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007868 } else if (write) {
7869 /*
7870 * If we aren't doing async submit, calculate the csum of the
7871 * bio now.
7872 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007873 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007874 if (ret)
7875 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007876 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007877 u64 csum_offset;
7878
7879 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02007880 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02007881 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07007882 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007883 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007884map:
Chris Mason08635ba2019-07-10 12:28:14 -07007885 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007886err:
Miao Xiee65e1532010-11-22 03:04:43 +00007887 return ret;
7888}
7889
Omar Sandovalc36cac22020-04-16 14:46:13 -07007890/*
7891 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7892 * or ordered extents whether or not we submit any bios.
7893 */
7894static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7895 struct inode *inode,
7896 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007897{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007898 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007899 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7900 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007901 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007902
Omar Sandoval85879572020-04-16 14:46:21 -07007903 dip_size = sizeof(*dip);
7904 if (!write && csum) {
7905 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07007906 size_t nblocks;
7907
David Sterba265fdfa2020-07-01 21:19:09 +02007908 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02007909 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07007910 }
7911
7912 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007913 if (!dip)
7914 return NULL;
7915
Omar Sandovalc36cac22020-04-16 14:46:13 -07007916 dip->inode = inode;
7917 dip->logical_offset = file_offset;
7918 dip->bytes = dio_bio->bi_iter.bi_size;
7919 dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007920 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007921 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007922 return dip;
7923}
7924
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007925static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
7926 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007927{
7928 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007929 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007930 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7931 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007932 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007933 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007934 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007935 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007936 u64 submit_len;
7937 int clone_offset = 0;
7938 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307939 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007940 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007941 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007942 struct btrfs_dio_data *dio_data = iomap->private;
Miao Xiee65e1532010-11-22 03:04:43 +00007943
Omar Sandovalc36cac22020-04-16 14:46:13 -07007944 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7945 if (!dip) {
7946 if (!write) {
7947 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7948 file_offset + dio_bio->bi_iter.bi_size - 1);
7949 }
7950 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007951 bio_endio(dio_bio);
7952 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04007953 }
7954
Josef Bacik334c16d2020-10-16 11:29:14 -04007955 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07007956 /*
7957 * Load the csums up front to reduce csum tree searches and
7958 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04007959 *
7960 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07007961 */
7962 status = btrfs_lookup_bio_sums(inode, dio_bio, file_offset,
7963 dip->csums);
7964 if (status != BLK_STS_OK)
7965 goto out_err;
7966 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007967
Omar Sandoval769b4f22020-04-16 14:46:22 -07007968 start_sector = dio_bio->bi_iter.bi_sector;
7969 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007970
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007971 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007972 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7973 start_sector << 9, submit_len,
7974 &geom);
7975 if (ret) {
7976 status = errno_to_blk_status(ret);
7977 goto out_err;
7978 }
7979 ASSERT(geom.len <= INT_MAX);
7980
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007981 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007982
Liu Bo725130b2017-05-16 09:51:39 -07007983 /*
7984 * This will never fail as it's passing GPF_NOFS and
7985 * the allocation is backed by btrfs_bioset.
7986 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007987 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007988 bio->bi_private = dip;
7989 bio->bi_end_io = btrfs_end_dio_bio;
7990 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007991
Liu Bo725130b2017-05-16 09:51:39 -07007992 ASSERT(submit_len >= clone_len);
7993 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007994
Liu Bo725130b2017-05-16 09:51:39 -07007995 /*
7996 * Increase the count before we submit the bio so we know
7997 * the end IO handler won't happen before we increase the
7998 * count. Otherwise, the dip might get freed before we're
7999 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008000 *
8001 * We transfer the initial reference to the last bio, so we
8002 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008003 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008004 if (submit_len > 0) {
8005 refcount_inc(&dip->refs);
8006 /*
8007 * If we are submitting more than one bio, submit them
8008 * all asynchronously. The exception is RAID 5 or 6, as
8009 * asynchronous checksums make it difficult to collect
8010 * full stripe writes.
8011 */
8012 if (!raid56)
8013 async_submit = 1;
8014 }
Miao Xiee65e1532010-11-22 03:04:43 +00008015
David Sterbad0ee3932018-03-08 14:35:48 +01008016 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008017 async_submit);
8018 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008019 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008020 if (submit_len > 0)
8021 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07008022 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008023 }
Liu Bo725130b2017-05-16 09:51:39 -07008024
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008025 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008026 clone_offset += clone_len;
8027 start_sector += clone_len >> 9;
8028 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008029 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008030 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008031
Miao Xiee65e1532010-11-22 03:04:43 +00008032out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008033 dip->dio_bio->bi_status = status;
8034 btrfs_dio_private_put(dip);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008035 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008036}
8037
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008038const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008039 .iomap_begin = btrfs_dio_iomap_begin,
8040 .iomap_end = btrfs_dio_iomap_end,
8041};
8042
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008043const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008044 .submit_io = btrfs_submit_direct,
8045};
8046
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008047static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008048 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008049{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008050 int ret;
8051
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008052 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008053 if (ret)
8054 return ret;
8055
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008056 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008057}
8058
Chris Mason9ebefb182007-06-15 13:50:00 -04008059int btrfs_readpage(struct file *file, struct page *page)
8060{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008061 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8062 u64 start = page_offset(page);
8063 u64 end = start + PAGE_SIZE - 1;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008064 unsigned long bio_flags = 0;
Nikolay Borisov0f208812020-09-14 14:39:16 +03008065 struct bio *bio = NULL;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008066 int ret;
8067
Nikolay Borisov0f208812020-09-14 14:39:16 +03008068 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8069
8070 ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008071 if (bio)
8072 ret = submit_one_bio(bio, 0, bio_flags);
8073 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008074}
Chris Mason1832a6d2007-12-21 16:27:21 -05008075
Chris Mason39279cc2007-06-12 06:35:45 -04008076static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8077{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008078 struct inode *inode = page->mapping->host;
8079 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008080
8081 if (current->flags & PF_MEMALLOC) {
8082 redirty_page_for_writepage(wbc, page);
8083 unlock_page(page);
8084 return 0;
8085 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008086
8087 /*
8088 * If we are under memory pressure we will call this directly from the
8089 * VM, we need to make sure we have the inode referenced for the ordered
8090 * extent. If not just return like we didn't do anything.
8091 */
8092 if (!igrab(inode)) {
8093 redirty_page_for_writepage(wbc, page);
8094 return AOP_WRITEPAGE_ACTIVATE;
8095 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008096 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008097 btrfs_add_delayed_iput(inode);
8098 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008099}
Chris Mason39279cc2007-06-12 06:35:45 -04008100
Eric Sandeen48a3b632013-04-25 20:41:01 +00008101static int btrfs_writepages(struct address_space *mapping,
8102 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008103{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008104 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008105}
8106
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008107static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008108{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008109 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008110}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008111
Chris Masone6dcd2d2008-07-17 12:53:50 -04008112static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008113{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008114 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008115 if (ret == 1)
8116 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008117 return ret;
8118}
Chris Mason39279cc2007-06-12 06:35:45 -04008119
Chris Masone6dcd2d2008-07-17 12:53:50 -04008120static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8121{
Chris Mason98509cf2008-09-11 15:51:43 -04008122 if (PageWriteback(page) || PageDirty(page))
8123 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008124 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008125}
8126
Roman Gushchinf8e66082020-03-04 16:57:35 -08008127#ifdef CONFIG_MIGRATION
8128static int btrfs_migratepage(struct address_space *mapping,
8129 struct page *newpage, struct page *page,
8130 enum migrate_mode mode)
8131{
8132 int ret;
8133
8134 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8135 if (ret != MIGRATEPAGE_SUCCESS)
8136 return ret;
8137
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008138 if (page_has_private(page))
8139 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008140
8141 if (PagePrivate2(page)) {
8142 ClearPagePrivate2(page);
8143 SetPagePrivate2(newpage);
8144 }
8145
8146 if (mode != MIGRATE_SYNC_NO_COPY)
8147 migrate_page_copy(newpage, page);
8148 else
8149 migrate_page_states(newpage, page);
8150 return MIGRATEPAGE_SUCCESS;
8151}
8152#endif
8153
Lukas Czernerd47992f2013-05-21 23:17:23 -04008154static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8155 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008156{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008157 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8158 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008159 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008160 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008161 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008162 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308163 u64 start;
8164 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008165 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Filipe Manana2766ff62020-11-04 11:07:34 +00008166 bool found_ordered = false;
8167 bool completed_ordered = false;
Chris Masona52d9a82007-08-27 16:49:44 -04008168
Chris Mason8b62b722009-09-02 16:53:46 -04008169 /*
8170 * we have the page locked, so new writeback can't start,
8171 * and the dirty bit won't be cleared while we are here.
8172 *
8173 * Wait for IO on this page so that we can safely clear
8174 * the PagePrivate2 bit and do ordered accounting
8175 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008176 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008177
Chris Masone6dcd2d2008-07-17 12:53:50 -04008178 if (offset) {
8179 btrfs_releasepage(page, GFP_NOFS);
8180 return;
8181 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008182
8183 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008184 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308185again:
8186 start = page_start;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008187 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008188 if (ordered) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008189 found_ordered = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08008190 end = min(page_end,
8191 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008192 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008193 * IO on this page will never be started, so we need to account
8194 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8195 * here, must leave that up for the ordered extent completion.
Chris Masoneb84ae02008-07-17 13:53:27 -04008196 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008197 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308198 clear_extent_bit(tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008199 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008200 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008201 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008202 /*
8203 * whoever cleared the private bit is responsible
8204 * for the finish_ordered_io
8205 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008206 if (TestClearPagePrivate2(page)) {
8207 struct btrfs_ordered_inode_tree *tree;
8208 u64 new_len;
8209
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008210 tree = &inode->ordered_tree;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008211
8212 spin_lock_irq(&tree->lock);
8213 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308214 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008215 if (new_len < ordered->truncated_len)
8216 ordered->truncated_len = new_len;
8217 spin_unlock_irq(&tree->lock);
8218
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008219 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8220 start,
Filipe Manana2766ff62020-11-04 11:07:34 +00008221 end - start + 1, 1)) {
Josef Bacik77cef2e2013-08-29 13:57:21 -04008222 btrfs_finish_ordered_io(ordered);
Filipe Manana2766ff62020-11-04 11:07:34 +00008223 completed_ordered = true;
8224 }
Chris Mason8b62b722009-09-02 16:53:46 -04008225 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008226 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008227 if (!inode_evicting) {
8228 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308229 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008230 &cached_state);
8231 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308232
8233 start = end + 1;
8234 if (start < page_end)
8235 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008236 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008237
Qu Wenruob9d0b382015-09-29 10:35:16 +08008238 /*
8239 * Qgroup reserved space handler
8240 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008241 * 1) Already written to disk or ordered extent already submitted
8242 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8243 * Qgroup will be handled by its qgroup_record then.
8244 * btrfs_qgroup_free_data() call will do nothing here.
8245 *
8246 * 2) Not written to disk yet
8247 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8248 * bit of its io_tree, and free the qgroup reserved data space.
8249 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008250 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008251 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008252 if (!inode_evicting) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008253 bool delete = true;
8254
8255 /*
8256 * If there's an ordered extent for this range and we have not
8257 * finished it ourselves, we must leave EXTENT_DELALLOC_NEW set
8258 * in the range for the ordered extent completion. We must also
8259 * not delete the range, otherwise we would lose that bit (and
8260 * any other bits set in the range). Make sure EXTENT_UPTODATE
8261 * is cleared if we don't delete, otherwise it can lead to
8262 * corruptions if the i_size is extented later.
8263 */
8264 if (found_ordered && !completed_ordered)
8265 delete = false;
Omar Sandovale1821632019-08-15 14:04:04 -07008266 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008267 EXTENT_DELALLOC | EXTENT_UPTODATE |
8268 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
8269 delete, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008270
8271 __btrfs_releasepage(page, GFP_NOFS);
8272 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008273
Chris Mason4a096752008-07-21 10:29:44 -04008274 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008275 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008276}
8277
Chris Mason9ebefb182007-06-15 13:50:00 -04008278/*
8279 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8280 * called from a page fault handler when a page is first dirtied. Hence we must
8281 * be careful to check for EOF conditions here. We set the page up correctly
8282 * for a written page which means we get ENOSPC checking when writing into
8283 * holes and correct delalloc and unwritten extent mapping on filesystems that
8284 * support these features.
8285 *
8286 * We are not allowed to take the i_mutex here so we have to play games to
8287 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008288 * truncate_setsize() writes the inode size before removing pages, once we have
8289 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008290 * beyond EOF, then the page is guaranteed safe against truncation until we
8291 * unlock the page.
8292 */
Souptick Joardera528a242018-06-06 19:54:44 +05308293vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008294{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008295 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008296 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008297 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008298 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8299 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008300 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008301 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008302 char *kaddr;
8303 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008304 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308305 vm_fault_t ret;
8306 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008307 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308308 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008309 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008310 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308311 u64 end;
8312
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008313 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008314
Jan Karab2b5ef52012-06-12 16:20:45 +02008315 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008316 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008317 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308318 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008319
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308320 /*
8321 * Reserving delalloc space after obtaining the page lock can lead to
8322 * deadlock. For example, if a dirty page is locked by this function
8323 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8324 * dirty page write out, then the btrfs_writepage() function could
8325 * end up waiting indefinitely to get a lock on the page currently
8326 * being processed by btrfs_page_mkwrite() function.
8327 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008328 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8329 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308330 if (!ret2) {
8331 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008332 reserved = 1;
8333 }
Souptick Joardera528a242018-06-06 19:54:44 +05308334 if (ret2) {
8335 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008336 if (reserved)
8337 goto out;
8338 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008339 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008340
Nick Piggin56a76f82009-03-31 15:23:23 -07008341 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008342again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008343 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008344 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008345
Chris Mason9ebefb182007-06-15 13:50:00 -04008346 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008347 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008348 /* page got truncated out from underneath us */
8349 goto out_unlock;
8350 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008351 wait_on_page_writeback(page);
8352
David Sterbaff13db42015-12-03 14:30:40 +01008353 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008354 set_page_extent_mapped(page);
8355
Chris Masoneb84ae02008-07-17 13:53:27 -04008356 /*
8357 * we can't set the delalloc bits if there are pending ordered
8358 * extents. Drop our locks and wait for them to finish
8359 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008360 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8361 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008362 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008363 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008364 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008365 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008366 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008367 btrfs_put_ordered_extent(ordered);
8368 goto again;
8369 }
8370
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008371 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008372 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008373 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008374 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308375 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008376 btrfs_delalloc_release_space(BTRFS_I(inode),
8377 data_reserved, page_start,
8378 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308379 }
8380 }
8381
Josef Bacikfbf19082009-10-01 17:10:23 -04008382 /*
Liu Bo54160342016-12-13 12:15:19 -08008383 * page_mkwrite gets called when the page is firstly dirtied after it's
8384 * faulted in, but write(2) could also dirty a page and set delalloc
8385 * bits, thus in this case for space account reason, we still need to
8386 * clear any delalloc bits within this page range since we have to
8387 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008388 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308389 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008390 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8391 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008392
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008393 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008394 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308395 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008396 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008397 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008398 ret = VM_FAULT_SIGBUS;
8399 goto out_unlock;
8400 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008401
8402 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008403 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008404 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008405 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008406 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008407
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008408 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008409 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008410 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008411 flush_dcache_page(page);
8412 kunmap(page);
8413 }
Chris Mason247e7432008-07-17 12:53:51 -04008414 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008415 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008416 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008417
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008418 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008419 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008420 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008421
David Sterbae43bbe52017-12-12 21:43:52 +01008422 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008423
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008424 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8425 sb_end_pagefault(inode->i_sb);
8426 extent_changeset_free(data_reserved);
8427 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008428
8429out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008430 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008431out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008432 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008433 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008434 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008435out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008436 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008437 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008438 return ret;
8439}
8440
Filipe Manana213e8c52018-02-06 20:40:31 +00008441static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008442{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008443 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008444 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008445 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008446 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008447 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008448 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008449 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008450
Filipe Manana213e8c52018-02-06 20:40:31 +00008451 if (!skip_writeback) {
8452 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8453 (u64)-1);
8454 if (ret)
8455 return ret;
8456 }
Chris Mason39279cc2007-06-12 06:35:45 -04008457
Josef Bacikfcb80c22011-05-03 10:40:22 -04008458 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008459 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8460 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008461 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008462 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008463 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008464 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008465 * be free'd up by the truncate operation, but also have some slack
8466 * space reserved in case it uses space during the truncate (thank you
8467 * very much snapshotting).
8468 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008469 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008470 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008471 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008472 * doesn't end up using space reserved for updating the inode. We also
8473 * need to be able to stop the transaction and start a new one, which
8474 * means we need to be able to update the inode several times, and we
8475 * have no idea of knowing how many times that will be, so we can't just
8476 * reserve 1 item for the entirety of the operation, so that has to be
8477 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008478 *
8479 * So that leaves us with
8480 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008481 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008482 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008483 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008484 * updating the inode.
8485 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008486 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008487 if (!rsv)
8488 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008489 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008490 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008491
Josef Bacik907cbce2011-08-08 13:46:15 -04008492 /*
Josef Bacik07127182011-08-19 10:29:59 -04008493 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008494 * 1 for updating the inode.
8495 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008496 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008497 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008498 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008499 goto out;
8500 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008501
Josef Bacik907cbce2011-08-08 13:46:15 -04008502 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008503 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008504 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008505 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008506
Chris Mason5a3f23d2009-03-31 13:27:11 -04008507 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008508 * So if we truncate and then write and fsync we normally would just
8509 * write the extents that changed, which is a problem if we need to
8510 * first truncate that entire inode. So set this flag so we write out
8511 * all of the extents in the inode to the sync log so we're completely
8512 * safe.
8513 */
8514 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008515 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008516
Yan, Zheng80825102009-11-12 09:35:36 +00008517 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008518 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008519 inode->i_size,
8520 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008521 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008522 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008523 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008524
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008525 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008526 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008527 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008528
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008529 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008530 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008531
8532 trans = btrfs_start_transaction(root, 2);
8533 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008534 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008535 trans = NULL;
8536 break;
8537 }
8538
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008539 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008540 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008541 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008542 BUG_ON(ret); /* shouldn't happen */
8543 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008544 }
8545
Josef Bacikddfae632017-10-19 14:16:02 -04008546 /*
8547 * We can't call btrfs_truncate_block inside a trans handle as we could
8548 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8549 * we've truncated everything except the last little bit, and can do
8550 * btrfs_truncate_block and then update the disk_i_size.
8551 */
8552 if (ret == NEED_TRUNCATE_BLOCK) {
8553 btrfs_end_transaction(trans);
8554 btrfs_btree_balance_dirty(fs_info);
8555
8556 ret = btrfs_truncate_block(inode, inode->i_size, 0, 0);
8557 if (ret)
8558 goto out;
8559 trans = btrfs_start_transaction(root, 1);
8560 if (IS_ERR(trans)) {
8561 ret = PTR_ERR(trans);
8562 goto out;
8563 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008564 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008565 }
8566
Chris Mason917c16b2011-11-08 14:49:59 -05008567 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008568 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008569
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008570 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008571 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008572 if (ret2 && !ret)
8573 ret = ret2;
8574
8575 ret2 = btrfs_end_transaction(trans);
8576 if (ret2 && !ret)
8577 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008578 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008579 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008580out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008581 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008582
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008583 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008584}
8585
Sven Wegener3b963622008-06-09 21:57:42 -04008586/*
Chris Masond352ac62008-09-29 15:18:18 -04008587 * create a new subvolume directory/inode (helper for the ioctl).
8588 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008589int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008590 struct btrfs_root *new_root,
8591 struct btrfs_root *parent_root,
8592 u64 new_dirid)
Chris Mason39279cc2007-06-12 06:35:45 -04008593{
Chris Mason39279cc2007-06-12 06:35:45 -04008594 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008595 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008596 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04008597
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008598 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
8599 new_dirid, new_dirid,
8600 S_IFDIR | (~current_umask() & S_IRWXUGO),
8601 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008602 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008603 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008604 inode->i_op = &btrfs_dir_inode_operations;
8605 inode->i_fop = &btrfs_dir_file_operations;
8606
Miklos Szeredibfe86842011-10-28 14:13:29 +02008607 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008608 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008609 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008610
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008611 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8612 if (err)
8613 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008614 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008615 new_root->root_key.objectid, err);
8616
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008617 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008618
Yan, Zheng76dda932009-09-21 16:00:26 -04008619 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008620 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008621}
8622
Chris Mason39279cc2007-06-12 06:35:45 -04008623struct inode *btrfs_alloc_inode(struct super_block *sb)
8624{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008625 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008626 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008627 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008628
David Sterba712e36c2017-10-31 17:08:27 +01008629 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008630 if (!ei)
8631 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008632
8633 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008634 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008635 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008636 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008637 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008638 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008639 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008640 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008641 ei->disk_i_size = 0;
8642 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008643 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008644 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008645 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008646 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008647 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008648 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008649
Josef Bacik9e0baf62011-07-15 15:16:44 +00008650 spin_lock_init(&ei->lock);
8651 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008652 if (sb->s_magic != BTRFS_TEST_MAGIC)
8653 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8654 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008655 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008656 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008657 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008658
Miao Xie16cdcec2011-04-22 18:12:22 +08008659 ei->delayed_node = NULL;
8660
chandan r9cc97d62012-07-04 12:48:07 +05308661 ei->i_otime.tv_sec = 0;
8662 ei->i_otime.tv_nsec = 0;
8663
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008664 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008665 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008666 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8667 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8668 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008669 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8670 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008671 ei->io_tree.track_uptodate = true;
8672 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008673 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008674 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008675 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008676 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008677 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008678 RB_CLEAR_NODE(&ei->rb_node);
8679
8680 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008681}
8682
Josef Bacikaaedb552013-10-11 14:44:09 -04008683#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8684void btrfs_test_destroy_inode(struct inode *inode)
8685{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008686 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008687 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8688}
8689#endif
8690
Al Viro26602ca2019-04-10 15:14:41 -04008691void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008692{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008693 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8694}
8695
Nikolay Borisov633cc812020-09-18 12:15:49 +03008696void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008697{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008698 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008699 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8700 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008701
Nikolay Borisov633cc812020-09-18 12:15:49 +03008702 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8703 WARN_ON(vfs_inode->i_data.nrpages);
8704 WARN_ON(inode->block_rsv.reserved);
8705 WARN_ON(inode->block_rsv.size);
8706 WARN_ON(inode->outstanding_extents);
8707 WARN_ON(inode->delalloc_bytes);
8708 WARN_ON(inode->new_delalloc_bytes);
8709 WARN_ON(inode->csum_bytes);
8710 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008711
Chris Mason5a3f23d2009-03-31 13:27:11 -04008712 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008713 * This can happen where we create an inode, but somebody else also
8714 * created the same inode and we need to destroy the one we already
8715 * created.
8716 */
8717 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008718 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008719
Chris Masond3977122009-01-05 21:25:51 -05008720 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008721 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008722 if (!ordered)
8723 break;
8724 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008725 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008726 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008727 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008728 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008729 btrfs_put_ordered_extent(ordered);
8730 btrfs_put_ordered_extent(ordered);
8731 }
8732 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008733 btrfs_qgroup_check_reserved_leak(inode);
8734 inode_tree_del(inode);
8735 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8736 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8737 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008738}
8739
Al Viro45321ac2010-06-07 13:43:19 -04008740int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008741{
8742 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008743
Naohiro Aota6379ef92013-06-06 09:56:34 +00008744 if (root == NULL)
8745 return 1;
8746
Liu Bofa6ac872013-02-20 14:10:23 +00008747 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008748 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008749 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008750 else
Al Viro45321ac2010-06-07 13:43:19 -04008751 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008752}
8753
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008754static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008755{
8756 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8757
8758 inode_init_once(&ei->vfs_inode);
8759}
8760
David Sterbae67c7182018-02-19 17:24:18 +01008761void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008762{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008763 /*
8764 * Make sure all delayed rcu free inodes are flushed before we
8765 * destroy cache.
8766 */
8767 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008768 kmem_cache_destroy(btrfs_inode_cachep);
8769 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008770 kmem_cache_destroy(btrfs_path_cachep);
8771 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008772 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008773}
8774
Liu Bof5c29bd2017-11-02 17:21:50 -06008775int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008776{
David Sterba837e1972012-09-07 03:00:48 -06008777 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008778 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008779 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8780 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008781 if (!btrfs_inode_cachep)
8782 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008783
David Sterba837e1972012-09-07 03:00:48 -06008784 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008785 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008786 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008787 if (!btrfs_trans_handle_cachep)
8788 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008789
David Sterba837e1972012-09-07 03:00:48 -06008790 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008791 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008792 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008793 if (!btrfs_path_cachep)
8794 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008795
David Sterba837e1972012-09-07 03:00:48 -06008796 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008797 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008798 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008799 if (!btrfs_free_space_cachep)
8800 goto fail;
8801
Christophe Leroy3acd4852019-08-21 15:05:55 +00008802 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8803 PAGE_SIZE, PAGE_SIZE,
8804 SLAB_RED_ZONE, NULL);
8805 if (!btrfs_free_space_bitmap_cachep)
8806 goto fail;
8807
Chris Mason39279cc2007-06-12 06:35:45 -04008808 return 0;
8809fail:
8810 btrfs_destroy_cachep();
8811 return -ENOMEM;
8812}
8813
David Howellsa528d352017-01-31 16:46:22 +00008814static int btrfs_getattr(const struct path *path, struct kstat *stat,
8815 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008816{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008817 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00008818 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008819 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008820 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008821 u32 bi_flags = BTRFS_I(inode)->flags;
8822
8823 stat->result_mask |= STATX_BTIME;
8824 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8825 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8826 if (bi_flags & BTRFS_INODE_APPEND)
8827 stat->attributes |= STATX_ATTR_APPEND;
8828 if (bi_flags & BTRFS_INODE_COMPRESS)
8829 stat->attributes |= STATX_ATTR_COMPRESSED;
8830 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8831 stat->attributes |= STATX_ATTR_IMMUTABLE;
8832 if (bi_flags & BTRFS_INODE_NODUMP)
8833 stat->attributes |= STATX_ATTR_NODUMP;
8834
8835 stat->attributes_mask |= (STATX_ATTR_APPEND |
8836 STATX_ATTR_COMPRESSED |
8837 STATX_ATTR_IMMUTABLE |
8838 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008839
Chris Mason39279cc2007-06-12 06:35:45 -04008840 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008841 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008842
8843 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008844 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00008845 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008846 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00008847 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008848 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008849 return 0;
8850}
8851
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008852static int btrfs_rename_exchange(struct inode *old_dir,
8853 struct dentry *old_dentry,
8854 struct inode *new_dir,
8855 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008856{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008857 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008858 struct btrfs_trans_handle *trans;
8859 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008860 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008861 struct inode *new_inode = new_dentry->d_inode;
8862 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008863 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008864 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8865 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008866 u64 old_idx = 0;
8867 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008868 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01008869 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008870 bool root_log_pinned = false;
8871 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008872
8873 /* we only allow rename subvolume link between subvolumes */
8874 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8875 return -EXDEV;
8876
8877 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008878 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8879 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008880 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008881
8882 /*
8883 * We want to reserve the absolute worst case amount of items. So if
8884 * both inodes are subvols and we need to unlink them then that would
8885 * require 4 item modifications, but if they are both normal inodes it
8886 * would require 5 item modifications, so we'll assume their normal
8887 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8888 * should cover the worst case number of items we'll modify.
8889 */
8890 trans = btrfs_start_transaction(root, 12);
8891 if (IS_ERR(trans)) {
8892 ret = PTR_ERR(trans);
8893 goto out_notrans;
8894 }
8895
Josef Bacik3e174092019-11-15 15:43:06 -05008896 if (dest != root)
8897 btrfs_record_root_in_trans(trans, dest);
8898
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008899 /*
8900 * We need to find a free sequence number both in the source and
8901 * in the destination directory for the exchange.
8902 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008903 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008904 if (ret)
8905 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008906 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008907 if (ret)
8908 goto out_fail;
8909
8910 BTRFS_I(old_inode)->dir_index = 0ULL;
8911 BTRFS_I(new_inode)->dir_index = 0ULL;
8912
8913 /* Reference for the source. */
8914 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8915 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008916 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008917 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008918 btrfs_pin_log_trans(root);
8919 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008920 ret = btrfs_insert_inode_ref(trans, dest,
8921 new_dentry->d_name.name,
8922 new_dentry->d_name.len,
8923 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008924 btrfs_ino(BTRFS_I(new_dir)),
8925 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008926 if (ret)
8927 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008928 }
8929
8930 /* And now for the dest. */
8931 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8932 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008933 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008934 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008935 btrfs_pin_log_trans(dest);
8936 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008937 ret = btrfs_insert_inode_ref(trans, root,
8938 old_dentry->d_name.name,
8939 old_dentry->d_name.len,
8940 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008941 btrfs_ino(BTRFS_I(old_dir)),
8942 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008943 if (ret)
8944 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008945 }
8946
8947 /* Update inode version and ctime/mtime. */
8948 inode_inc_iversion(old_dir);
8949 inode_inc_iversion(new_dir);
8950 inode_inc_iversion(old_inode);
8951 inode_inc_iversion(new_inode);
8952 old_dir->i_ctime = old_dir->i_mtime = ctime;
8953 new_dir->i_ctime = new_dir->i_mtime = ctime;
8954 old_inode->i_ctime = ctime;
8955 new_inode->i_ctime = ctime;
8956
8957 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008958 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8959 BTRFS_I(old_inode), 1);
8960 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8961 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008962 }
8963
8964 /* src is a subvolume */
8965 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008966 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008967 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008968 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8969 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008970 old_dentry->d_name.name,
8971 old_dentry->d_name.len);
8972 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008973 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008974 }
8975 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008976 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008977 goto out_fail;
8978 }
8979
8980 /* dest is a subvolume */
8981 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008982 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008983 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008984 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8985 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008986 new_dentry->d_name.name,
8987 new_dentry->d_name.len);
8988 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008989 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008990 }
8991 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008992 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008993 goto out_fail;
8994 }
8995
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02008996 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008997 new_dentry->d_name.name,
8998 new_dentry->d_name.len, 0, old_idx);
8999 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009000 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009001 goto out_fail;
9002 }
9003
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009004 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009005 old_dentry->d_name.name,
9006 old_dentry->d_name.len, 0, new_idx);
9007 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009008 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009009 goto out_fail;
9010 }
9011
9012 if (old_inode->i_nlink == 1)
9013 BTRFS_I(old_inode)->dir_index = old_idx;
9014 if (new_inode->i_nlink == 1)
9015 BTRFS_I(new_inode)->dir_index = new_idx;
9016
Filipe Manana86e8aa02016-05-05 02:02:27 +01009017 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009018 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9019 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009020 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009021 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009022 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009023 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009024 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9025 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009026 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009027 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009028 }
9029out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009030 /*
9031 * If we have pinned a log and an error happened, we unpin tasks
9032 * trying to sync the log and force them to fallback to a transaction
9033 * commit if the log currently contains any of the inodes involved in
9034 * this rename operation (to ensure we do not persist a log with an
9035 * inconsistent state for any of these inodes or leading to any
9036 * inconsistencies when replayed). If the transaction was aborted, the
9037 * abortion reason is propagated to userspace when attempting to commit
9038 * the transaction. If the log does not contain any of these inodes, we
9039 * allow the tasks to sync it.
9040 */
9041 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009042 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9043 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9044 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009045 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009046 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009047 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009048
9049 if (root_log_pinned) {
9050 btrfs_end_log_trans(root);
9051 root_log_pinned = false;
9052 }
9053 if (dest_log_pinned) {
9054 btrfs_end_log_trans(dest);
9055 dest_log_pinned = false;
9056 }
9057 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009058 ret2 = btrfs_end_transaction(trans);
9059 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009060out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009061 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9062 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009063 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009064
9065 return ret;
9066}
9067
9068static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9069 struct btrfs_root *root,
9070 struct inode *dir,
9071 struct dentry *dentry)
9072{
9073 int ret;
9074 struct inode *inode;
9075 u64 objectid;
9076 u64 index;
9077
9078 ret = btrfs_find_free_ino(root, &objectid);
9079 if (ret)
9080 return ret;
9081
9082 inode = btrfs_new_inode(trans, root, dir,
9083 dentry->d_name.name,
9084 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009085 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009086 objectid,
9087 S_IFCHR | WHITEOUT_MODE,
9088 &index);
9089
9090 if (IS_ERR(inode)) {
9091 ret = PTR_ERR(inode);
9092 return ret;
9093 }
9094
9095 inode->i_op = &btrfs_special_inode_operations;
9096 init_special_inode(inode, inode->i_mode,
9097 WHITEOUT_DEV);
9098
9099 ret = btrfs_init_inode_security(trans, inode, dir,
9100 &dentry->d_name);
9101 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009102 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009103
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009104 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9105 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009106 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009107 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009108
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009109 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009110out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009111 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009112 if (ret)
9113 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009114 iput(inode);
9115
Filipe Mananac9901612016-05-05 01:41:57 +01009116 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009117}
9118
Chris Mason39279cc2007-06-12 06:35:45 -04009119static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009120 struct inode *new_dir, struct dentry *new_dentry,
9121 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009122{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009123 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009124 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009125 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009126 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9127 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009128 struct inode *new_inode = d_inode(new_dentry);
9129 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009130 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009131 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009132 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009133 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009134 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009135
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009136 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009137 return -EPERM;
9138
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009139 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009140 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009141 return -EXDEV;
9142
Li Zefan33345d012011-04-20 10:31:50 +08009143 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009144 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009145 return -ENOTEMPTY;
9146
Chris Mason39279cc2007-06-12 06:35:45 -04009147 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009148 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009149 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009150
9151
9152 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009153 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009154 new_dentry->d_name.name,
9155 new_dentry->d_name.len);
9156
9157 if (ret) {
9158 if (ret == -EEXIST) {
9159 /* we shouldn't get
9160 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309161 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009162 return ret;
9163 }
9164 } else {
9165 /* maybe -EOVERFLOW */
9166 return ret;
9167 }
9168 }
9169 ret = 0;
9170
Chris Mason5a3f23d2009-03-31 13:27:11 -04009171 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009172 * we're using rename to replace one file with another. Start IO on it
9173 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009174 */
Chris Mason8d875f92014-08-12 10:47:42 -07009175 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009176 filemap_flush(old_inode->i_mapping);
9177
Yan, Zheng76dda932009-09-21 16:00:26 -04009178 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009179 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009180 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009181 /*
9182 * We want to reserve the absolute worst case amount of items. So if
9183 * both inodes are subvols and we need to unlink them then that would
9184 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009185 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009186 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9187 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009188 * If our rename has the whiteout flag, we need more 5 units for the
9189 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9190 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009191 */
Filipe Manana5062af32016-05-05 10:26:26 +01009192 trans_num_items = 11;
9193 if (flags & RENAME_WHITEOUT)
9194 trans_num_items += 5;
9195 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009196 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009197 ret = PTR_ERR(trans);
9198 goto out_notrans;
9199 }
Chris Mason5f39d392007-10-15 16:14:19 -04009200
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009201 if (dest != root)
9202 btrfs_record_root_in_trans(trans, dest);
9203
Nikolay Borisov877574e2017-02-20 13:50:33 +02009204 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009205 if (ret)
9206 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009207
Miao Xie67de1172013-12-26 13:07:06 +08009208 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009209 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009210 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009211 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009212 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009213 btrfs_pin_log_trans(root);
9214 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009215 ret = btrfs_insert_inode_ref(trans, dest,
9216 new_dentry->d_name.name,
9217 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009218 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009219 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009220 if (ret)
9221 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009222 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009223
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009224 inode_inc_iversion(old_dir);
9225 inode_inc_iversion(new_dir);
9226 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009227 old_dir->i_ctime = old_dir->i_mtime =
9228 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009229 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009230
Chris Mason12fcfd22009-03-24 10:24:20 -04009231 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009232 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9233 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009234
Li Zefan33345d012011-04-20 10:31:50 +08009235 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009236 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009237 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009238 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9239 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009240 old_dentry->d_name.name,
9241 old_dentry->d_name.len);
9242 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009243 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009244 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009245 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009246 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009247 goto out_fail;
9248 }
Chris Mason39279cc2007-06-12 06:35:45 -04009249
9250 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009251 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009252 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009253 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009254 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009255 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009256 BUG_ON(new_inode->i_nlink == 0);
9257 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009258 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9259 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009260 new_dentry->d_name.name,
9261 new_dentry->d_name.len);
9262 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009263 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009264 ret = btrfs_orphan_add(trans,
9265 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009266 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009267 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009268 goto out_fail;
9269 }
Chris Mason39279cc2007-06-12 06:35:45 -04009270 }
Josef Bacikaec74772008-07-24 12:12:38 -04009271
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009272 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009273 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009274 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009275 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009276 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009277 goto out_fail;
9278 }
Chris Mason39279cc2007-06-12 06:35:45 -04009279
Miao Xie67de1172013-12-26 13:07:06 +08009280 if (old_inode->i_nlink == 1)
9281 BTRFS_I(old_inode)->dir_index = index;
9282
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009283 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009284 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9285 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009286 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009287 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009288 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009289
9290 if (flags & RENAME_WHITEOUT) {
9291 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9292 old_dentry);
9293
9294 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009295 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009296 goto out_fail;
9297 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009298 }
Chris Mason39279cc2007-06-12 06:35:45 -04009299out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009300 /*
9301 * If we have pinned the log and an error happened, we unpin tasks
9302 * trying to sync the log and force them to fallback to a transaction
9303 * commit if the log currently contains any of the inodes involved in
9304 * this rename operation (to ensure we do not persist a log with an
9305 * inconsistent state for any of these inodes or leading to any
9306 * inconsistencies when replayed). If the transaction was aborted, the
9307 * abortion reason is propagated to userspace when attempting to commit
9308 * the transaction. If the log does not contain any of these inodes, we
9309 * allow the tasks to sync it.
9310 */
9311 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009312 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9313 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9314 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009315 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009316 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009317 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009318
9319 btrfs_end_log_trans(root);
9320 log_pinned = false;
9321 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009322 ret2 = btrfs_end_transaction(trans);
9323 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009324out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009325 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009326 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009327
Chris Mason39279cc2007-06-12 06:35:45 -04009328 return ret;
9329}
9330
Miklos Szeredi80ace852014-07-23 15:15:32 +02009331static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9332 struct inode *new_dir, struct dentry *new_dentry,
9333 unsigned int flags)
9334{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009335 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009336 return -EINVAL;
9337
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009338 if (flags & RENAME_EXCHANGE)
9339 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9340 new_dentry);
9341
9342 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009343}
9344
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009345struct btrfs_delalloc_work {
9346 struct inode *inode;
9347 struct completion completion;
9348 struct list_head list;
9349 struct btrfs_work work;
9350};
9351
Miao Xie8ccf6f192012-10-25 09:28:04 +00009352static void btrfs_run_delalloc_work(struct btrfs_work *work)
9353{
9354 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009355 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009356
9357 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9358 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009359 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009360 filemap_flush(inode->i_mapping);
9361 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9362 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009363 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009364
Nikolay Borisov076da912018-04-23 10:54:16 +03009365 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009366 complete(&delalloc_work->completion);
9367}
9368
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009369static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009370{
9371 struct btrfs_delalloc_work *work;
9372
David Sterba100d5702015-12-08 14:39:32 +01009373 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009374 if (!work)
9375 return NULL;
9376
9377 init_completion(&work->completion);
9378 INIT_LIST_HEAD(&work->list);
9379 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009380 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009381
9382 return work;
9383}
9384
Chris Masond352ac62008-09-29 15:18:18 -04009385/*
9386 * some fairly slow code that needs optimization. This walks the list
9387 * of all the inodes with pending delalloc and forces them to disk.
9388 */
Josef Bacikb4912132020-07-21 10:22:12 -04009389static int start_delalloc_inodes(struct btrfs_root *root, u64 *nr, bool snapshot)
Chris Masonea8c2812008-08-04 23:17:27 -04009390{
Chris Masonea8c2812008-08-04 23:17:27 -04009391 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009392 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009393 struct btrfs_delalloc_work *work, *next;
9394 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009395 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009396 int ret = 0;
Chris Masonea8c2812008-08-04 23:17:27 -04009397
Miao Xie8ccf6f192012-10-25 09:28:04 +00009398 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009399 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009400
Miao Xie573bfb72014-03-06 13:55:03 +08009401 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009402 spin_lock(&root->delalloc_lock);
9403 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009404 while (!list_empty(&splice)) {
9405 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009406 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009407
Miao Xieeb73c1b2013-05-15 07:48:22 +00009408 list_move_tail(&binode->delalloc_inodes,
9409 &root->delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009410 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009411 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009412 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009413 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009414 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009415 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009416
Ethan Lien3cd24c62018-11-01 14:49:03 +08009417 if (snapshot)
9418 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9419 &binode->runtime_flags);
Nikolay Borisov076da912018-04-23 10:54:16 +03009420 work = btrfs_alloc_delalloc_work(inode);
David Sterba5d99a9982014-09-29 19:20:37 +02009421 if (!work) {
Nikolay Borisov4fbb5142018-04-23 10:54:15 +03009422 iput(inode);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009423 ret = -ENOMEM;
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009424 goto out;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009425 }
Miao Xie1eafa6c2013-01-22 10:49:00 +00009426 list_add_tail(&work->list, &works);
Qu Wenruoa44903a2014-02-28 10:46:09 +08009427 btrfs_queue_work(root->fs_info->flush_workers,
9428 &work->work);
Josef Bacikb4912132020-07-21 10:22:12 -04009429 if (*nr != U64_MAX) {
9430 (*nr)--;
9431 if (*nr == 0)
9432 goto out;
9433 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009434 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009435 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009436 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009437 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009438
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009439out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009440 list_for_each_entry_safe(work, next, &works, list) {
9441 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009442 wait_for_completion(&work->completion);
9443 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009444 }
9445
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009446 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009447 spin_lock(&root->delalloc_lock);
9448 list_splice_tail(&splice, &root->delalloc_inodes);
9449 spin_unlock(&root->delalloc_lock);
9450 }
Miao Xie573bfb72014-03-06 13:55:03 +08009451 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009452 return ret;
9453}
9454
Ethan Lien3cd24c62018-11-01 14:49:03 +08009455int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009456{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009457 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacikb4912132020-07-21 10:22:12 -04009458 u64 nr = U64_MAX;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009459
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009460 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009461 return -EROFS;
9462
Josef Bacikb4912132020-07-21 10:22:12 -04009463 return start_delalloc_inodes(root, &nr, true);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009464}
9465
Josef Bacikb4912132020-07-21 10:22:12 -04009466int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, u64 nr)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009467{
9468 struct btrfs_root *root;
9469 struct list_head splice;
9470 int ret;
9471
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009472 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009473 return -EROFS;
9474
9475 INIT_LIST_HEAD(&splice);
9476
Miao Xie573bfb72014-03-06 13:55:03 +08009477 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009478 spin_lock(&fs_info->delalloc_root_lock);
9479 list_splice_init(&fs_info->delalloc_roots, &splice);
Miao Xie6c255e62014-03-06 13:55:01 +08009480 while (!list_empty(&splice) && nr) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009481 root = list_first_entry(&splice, struct btrfs_root,
9482 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009483 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009484 BUG_ON(!root);
9485 list_move_tail(&root->delalloc_root,
9486 &fs_info->delalloc_roots);
9487 spin_unlock(&fs_info->delalloc_root_lock);
9488
Josef Bacikb4912132020-07-21 10:22:12 -04009489 ret = start_delalloc_inodes(root, &nr, false);
Josef Bacik00246522020-01-24 09:33:01 -05009490 btrfs_put_root(root);
Miao Xie6c255e62014-03-06 13:55:01 +08009491 if (ret < 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009492 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009493 spin_lock(&fs_info->delalloc_root_lock);
9494 }
9495 spin_unlock(&fs_info->delalloc_root_lock);
9496
Miao Xie6c255e62014-03-06 13:55:01 +08009497 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009498out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009499 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009500 spin_lock(&fs_info->delalloc_root_lock);
9501 list_splice_tail(&splice, &fs_info->delalloc_roots);
9502 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009503 }
Miao Xie573bfb72014-03-06 13:55:03 +08009504 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009505 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009506}
9507
Chris Mason39279cc2007-06-12 06:35:45 -04009508static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9509 const char *symname)
9510{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009511 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009512 struct btrfs_trans_handle *trans;
9513 struct btrfs_root *root = BTRFS_I(dir)->root;
9514 struct btrfs_path *path;
9515 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009516 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009517 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009518 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309519 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009520 int name_len;
9521 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009522 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009523 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009524 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009525
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009526 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009527 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009528 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009529
Josef Bacik9ed74f22009-09-11 16:12:44 -04009530 /*
9531 * 2 items for inode item and ref
9532 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009533 * 1 item for updating parent inode item
9534 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009535 * 1 item for xattr if selinux is on
9536 */
Filipe Manana9269d122015-12-31 18:16:29 +00009537 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009538 if (IS_ERR(trans))
9539 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009540
Li Zefan581bb052011-04-20 10:06:11 +08009541 err = btrfs_find_free_ino(root, &objectid);
9542 if (err)
9543 goto out_unlock;
9544
Josef Bacikaec74772008-07-24 12:12:38 -04009545 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009546 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9547 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009548 if (IS_ERR(inode)) {
9549 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009550 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009551 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009552 }
Chris Mason39279cc2007-06-12 06:35:45 -04009553
Casey Schauflerad19db72011-12-15 10:09:07 -05009554 /*
9555 * If the active LSM wants to access the inode during
9556 * d_instantiate it needs these. Smack checks to see
9557 * if the filesystem supports xattrs by looking at the
9558 * ops vector.
9559 */
9560 inode->i_fop = &btrfs_file_operations;
9561 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009562 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009563
9564 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9565 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009566 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009567
Chris Mason39279cc2007-06-12 06:35:45 -04009568 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009569 if (!path) {
9570 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009571 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009572 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009573 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009574 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009575 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009576 datasize = btrfs_file_extent_calc_inline_size(name_len);
9577 err = btrfs_insert_empty_item(trans, root, path, &key,
9578 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009579 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009580 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009581 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009582 }
Chris Mason5f39d392007-10-15 16:14:19 -04009583 leaf = path->nodes[0];
9584 ei = btrfs_item_ptr(leaf, path->slots[0],
9585 struct btrfs_file_extent_item);
9586 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9587 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009588 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009589 btrfs_set_file_extent_encryption(leaf, ei, 0);
9590 btrfs_set_file_extent_compression(leaf, ei, 0);
9591 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9592 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9593
Chris Mason39279cc2007-06-12 06:35:45 -04009594 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009595 write_extent_buffer(leaf, symname, ptr, name_len);
9596 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009597 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009598
Chris Mason39279cc2007-06-12 06:35:45 -04009599 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009600 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009601 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009602 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009603 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +00009604 /*
9605 * Last step, add directory indexes for our symlink inode. This is the
9606 * last step to avoid extra cleanup of these indexes if an error happens
9607 * elsewhere above.
9608 */
9609 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009610 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9611 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009612 if (err)
9613 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009614
Al Viro1e2e5472018-05-04 08:23:01 -04009615 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009616
9617out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009618 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009619 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009620 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009621 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009622 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009623 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009624 return err;
9625}
Chris Mason16432982008-04-10 10:23:21 -04009626
Filipe Manana8fccebf2020-09-08 11:27:20 +01009627static struct btrfs_trans_handle *insert_prealloc_file_extent(
9628 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009629 struct btrfs_inode *inode,
9630 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009631 u64 file_offset)
9632{
9633 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009634 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009635 struct btrfs_trans_handle *trans = trans_in;
9636 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009637 u64 start = ins->objectid;
9638 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009639 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009640
9641 memset(&stack_fi, 0, sizeof(stack_fi));
9642
9643 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9644 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9645 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9646 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9647 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9648 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9649 /* Encryption and other encoding is reserved and all 0 */
9650
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009651 ret = btrfs_qgroup_release_data(inode, file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009652 if (ret < 0)
Filipe Manana8fccebf2020-09-08 11:27:20 +01009653 return ERR_PTR(ret);
9654
9655 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009656 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +00009657 file_offset, &stack_fi,
9658 true, ret);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009659 if (ret)
9660 return ERR_PTR(ret);
9661 return trans;
9662 }
9663
9664 extent_info.disk_offset = start;
9665 extent_info.disk_len = len;
9666 extent_info.data_offset = 0;
9667 extent_info.data_len = len;
9668 extent_info.file_offset = file_offset;
9669 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009670 extent_info.is_new_extent = true;
9671 extent_info.qgroup_reserved = ret;
9672 extent_info.insertions = 0;
9673
9674 path = btrfs_alloc_path();
9675 if (!path)
9676 return ERR_PTR(-ENOMEM);
9677
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009678 ret = btrfs_replace_file_extents(&inode->vfs_inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009679 file_offset + len - 1, &extent_info,
9680 &trans);
9681 btrfs_free_path(path);
9682 if (ret)
9683 return ERR_PTR(ret);
9684
9685 return trans;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009686}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009687
Josef Bacik0af3d002010-06-21 14:48:16 -04009688static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9689 u64 start, u64 num_bytes, u64 min_size,
9690 loff_t actual_len, u64 *alloc_hint,
9691 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009692{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009693 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009694 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9695 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009696 struct btrfs_root *root = BTRFS_I(inode)->root;
9697 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009698 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009699 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009700 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009701 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009702 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009703 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009704 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009705 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009706
Josef Bacik0af3d002010-06-21 14:48:16 -04009707 if (trans)
9708 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009709 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009710 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009711 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009712 /*
9713 * If we are severely fragmented we could end up with really
9714 * small allocations, so if the allocator is returning small
9715 * chunks lets make its job easier by only searching for those
9716 * sized chunks.
9717 */
9718 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009719 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9720 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009721 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -04009722 break;
Josef Bacikb778cf92020-02-13 10:47:31 -05009723
9724 /*
9725 * We've reserved this space, and thus converted it from
9726 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9727 * from here on out we will only need to clear our reservation
9728 * for the remaining unreserved area, so advance our
9729 * clear_offset by our extent size.
9730 */
9731 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009732
Josef Bacik0b670dc2015-09-23 17:11:16 -04009733 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009734 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
9735 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +01009736 /*
9737 * Now that we inserted the prealloc extent we can finally
9738 * decrement the number of reservations in the block group.
9739 * If we did it before, we could race with relocation and have
9740 * relocation miss the reserved extent, making it fail later.
9741 */
9742 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009743 if (IS_ERR(trans)) {
9744 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009745 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009746 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009747 break;
9748 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009749
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009750 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009751 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009752
Josef Bacik5dc562c2012-08-17 13:14:17 -04009753 em = alloc_extent_map();
9754 if (!em) {
9755 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9756 &BTRFS_I(inode)->runtime_flags);
9757 goto next;
9758 }
9759
9760 em->start = cur_offset;
9761 em->orig_start = cur_offset;
9762 em->len = ins.offset;
9763 em->block_start = ins.objectid;
9764 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009765 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009766 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009767 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9768 em->generation = trans->transid;
9769
9770 while (1) {
9771 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009772 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009773 write_unlock(&em_tree->lock);
9774 if (ret != -EEXIST)
9775 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009776 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009777 cur_offset + ins.offset - 1,
9778 0);
9779 }
9780 free_extent_map(em);
9781next:
Yan Zhengd899e052008-10-30 14:25:28 -04009782 num_bytes -= ins.offset;
9783 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009784 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009785
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009786 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009787 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009788 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009789 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009790 (actual_len > inode->i_size) &&
9791 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009792 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009793 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009794 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009795 i_size = cur_offset;
9796 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +02009797 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009798 }
9799
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009800 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009801
9802 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009803 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009804 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009805 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009806 break;
9807 }
Yan Zhengd899e052008-10-30 14:25:28 -04009808
Filipe Manana8fccebf2020-09-08 11:27:20 +01009809 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009810 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009811 trans = NULL;
9812 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009813 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009814 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009815 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009816 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009817 return ret;
9818}
9819
Josef Bacik0af3d002010-06-21 14:48:16 -04009820int btrfs_prealloc_file_range(struct inode *inode, int mode,
9821 u64 start, u64 num_bytes, u64 min_size,
9822 loff_t actual_len, u64 *alloc_hint)
9823{
9824 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9825 min_size, actual_len, alloc_hint,
9826 NULL);
9827}
9828
9829int btrfs_prealloc_file_range_trans(struct inode *inode,
9830 struct btrfs_trans_handle *trans, int mode,
9831 u64 start, u64 num_bytes, u64 min_size,
9832 loff_t actual_len, u64 *alloc_hint)
9833{
9834 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9835 min_size, actual_len, alloc_hint, trans);
9836}
9837
Chris Masone6dcd2d2008-07-17 12:53:50 -04009838static int btrfs_set_page_dirty(struct page *page)
9839{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009840 return __set_page_dirty_nobuffers(page);
9841}
9842
Al Viro10556cb22011-06-20 19:28:19 -04009843static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009844{
Li Zefanb83cc962010-12-20 16:04:08 +08009845 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009846 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009847
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009848 if (mask & MAY_WRITE &&
9849 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9850 if (btrfs_root_readonly(root))
9851 return -EROFS;
9852 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9853 return -EACCES;
9854 }
Al Viro2830ba72011-06-20 19:16:29 -04009855 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009856}
Chris Mason39279cc2007-06-12 06:35:45 -04009857
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009858static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9859{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009860 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009861 struct btrfs_trans_handle *trans;
9862 struct btrfs_root *root = BTRFS_I(dir)->root;
9863 struct inode *inode = NULL;
9864 u64 objectid;
9865 u64 index;
9866 int ret = 0;
9867
9868 /*
9869 * 5 units required for adding orphan entry
9870 */
9871 trans = btrfs_start_transaction(root, 5);
9872 if (IS_ERR(trans))
9873 return PTR_ERR(trans);
9874
9875 ret = btrfs_find_free_ino(root, &objectid);
9876 if (ret)
9877 goto out;
9878
9879 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009880 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009881 if (IS_ERR(inode)) {
9882 ret = PTR_ERR(inode);
9883 inode = NULL;
9884 goto out;
9885 }
9886
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009887 inode->i_fop = &btrfs_file_operations;
9888 inode->i_op = &btrfs_file_inode_operations;
9889
9890 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009891
Chris Masonb0d5d102014-09-08 13:08:51 -07009892 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9893 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009894 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009895
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009896 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07009897 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009898 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009899 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009900 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009901 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009902
Filipe Manana5762b5c2014-08-01 00:10:32 +01009903 /*
9904 * We set number of links to 0 in btrfs_new_inode(), and here we set
9905 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9906 * through:
9907 *
9908 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9909 */
9910 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009911 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009912 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009913 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009914out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009915 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009916 if (ret && inode)
9917 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009918 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009919 return ret;
9920}
9921
David Sterba5cdc84b2018-07-18 20:32:52 +02009922void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009923{
David Sterba5cdc84b2018-07-18 20:32:52 +02009924 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009925 unsigned long index = start >> PAGE_SHIFT;
9926 unsigned long end_index = end >> PAGE_SHIFT;
9927 struct page *page;
9928
9929 while (index <= end_index) {
9930 page = find_get_page(inode->i_mapping, index);
9931 ASSERT(page); /* Pages should be in the extent_io_tree */
9932 set_page_writeback(page);
9933 put_page(page);
9934 index++;
9935 }
9936}
9937
Omar Sandovaled46ff32016-11-03 10:28:14 -07009938#ifdef CONFIG_SWAP
9939/*
9940 * Add an entry indicating a block group or device which is pinned by a
9941 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9942 * negative errno on failure.
9943 */
9944static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9945 bool is_block_group)
9946{
9947 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9948 struct btrfs_swapfile_pin *sp, *entry;
9949 struct rb_node **p;
9950 struct rb_node *parent = NULL;
9951
9952 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9953 if (!sp)
9954 return -ENOMEM;
9955 sp->ptr = ptr;
9956 sp->inode = inode;
9957 sp->is_block_group = is_block_group;
9958
9959 spin_lock(&fs_info->swapfile_pins_lock);
9960 p = &fs_info->swapfile_pins.rb_node;
9961 while (*p) {
9962 parent = *p;
9963 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
9964 if (sp->ptr < entry->ptr ||
9965 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
9966 p = &(*p)->rb_left;
9967 } else if (sp->ptr > entry->ptr ||
9968 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
9969 p = &(*p)->rb_right;
9970 } else {
9971 spin_unlock(&fs_info->swapfile_pins_lock);
9972 kfree(sp);
9973 return 1;
9974 }
9975 }
9976 rb_link_node(&sp->node, parent, p);
9977 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
9978 spin_unlock(&fs_info->swapfile_pins_lock);
9979 return 0;
9980}
9981
9982/* Free all of the entries pinned by this swapfile. */
9983static void btrfs_free_swapfile_pins(struct inode *inode)
9984{
9985 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9986 struct btrfs_swapfile_pin *sp;
9987 struct rb_node *node, *next;
9988
9989 spin_lock(&fs_info->swapfile_pins_lock);
9990 node = rb_first(&fs_info->swapfile_pins);
9991 while (node) {
9992 next = rb_next(node);
9993 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
9994 if (sp->inode == inode) {
9995 rb_erase(&sp->node, &fs_info->swapfile_pins);
9996 if (sp->is_block_group)
9997 btrfs_put_block_group(sp->ptr);
9998 kfree(sp);
9999 }
10000 node = next;
10001 }
10002 spin_unlock(&fs_info->swapfile_pins_lock);
10003}
10004
10005struct btrfs_swap_info {
10006 u64 start;
10007 u64 block_start;
10008 u64 block_len;
10009 u64 lowest_ppage;
10010 u64 highest_ppage;
10011 unsigned long nr_pages;
10012 int nr_extents;
10013};
10014
10015static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10016 struct btrfs_swap_info *bsi)
10017{
10018 unsigned long nr_pages;
10019 u64 first_ppage, first_ppage_reported, next_ppage;
10020 int ret;
10021
10022 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10023 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10024 PAGE_SIZE) >> PAGE_SHIFT;
10025
10026 if (first_ppage >= next_ppage)
10027 return 0;
10028 nr_pages = next_ppage - first_ppage;
10029
10030 first_ppage_reported = first_ppage;
10031 if (bsi->start == 0)
10032 first_ppage_reported++;
10033 if (bsi->lowest_ppage > first_ppage_reported)
10034 bsi->lowest_ppage = first_ppage_reported;
10035 if (bsi->highest_ppage < (next_ppage - 1))
10036 bsi->highest_ppage = next_ppage - 1;
10037
10038 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10039 if (ret < 0)
10040 return ret;
10041 bsi->nr_extents += ret;
10042 bsi->nr_pages += nr_pages;
10043 return 0;
10044}
10045
10046static void btrfs_swap_deactivate(struct file *file)
10047{
10048 struct inode *inode = file_inode(file);
10049
10050 btrfs_free_swapfile_pins(inode);
10051 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10052}
10053
10054static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10055 sector_t *span)
10056{
10057 struct inode *inode = file_inode(file);
10058 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10059 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10060 struct extent_state *cached_state = NULL;
10061 struct extent_map *em = NULL;
10062 struct btrfs_device *device = NULL;
10063 struct btrfs_swap_info bsi = {
10064 .lowest_ppage = (sector_t)-1ULL,
10065 };
10066 int ret = 0;
10067 u64 isize;
10068 u64 start;
10069
10070 /*
10071 * If the swap file was just created, make sure delalloc is done. If the
10072 * file changes again after this, the user is doing something stupid and
10073 * we don't really care.
10074 */
10075 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10076 if (ret)
10077 return ret;
10078
10079 /*
10080 * The inode is locked, so these flags won't change after we check them.
10081 */
10082 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10083 btrfs_warn(fs_info, "swapfile must not be compressed");
10084 return -EINVAL;
10085 }
10086 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10087 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10088 return -EINVAL;
10089 }
10090 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10091 btrfs_warn(fs_info, "swapfile must not be checksummed");
10092 return -EINVAL;
10093 }
10094
10095 /*
10096 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010097 * under us. The exclop protection makes sure they aren't running/won't
10098 * run concurrently while we are mapping the swap extents, and
10099 * fs_info->swapfile_pins prevents them from running while the swap
10100 * file is active and moving the extents. Note that this also prevents
10101 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010102 * really worth the trouble to allow it.
10103 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010104 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010105 btrfs_warn(fs_info,
10106 "cannot activate swapfile while exclusive operation is running");
10107 return -EBUSY;
10108 }
10109 /*
10110 * Snapshots can create extents which require COW even if NODATACOW is
10111 * set. We use this counter to prevent snapshots. We must increment it
10112 * before walking the extents because we don't want a concurrent
10113 * snapshot to run after we've already checked the extents.
10114 */
10115 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10116
10117 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10118
10119 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10120 start = 0;
10121 while (start < isize) {
10122 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010123 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010124 u64 len = isize - start;
10125
Omar Sandoval39b07b52019-12-02 17:34:23 -080010126 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010127 if (IS_ERR(em)) {
10128 ret = PTR_ERR(em);
10129 goto out;
10130 }
10131
10132 if (em->block_start == EXTENT_MAP_HOLE) {
10133 btrfs_warn(fs_info, "swapfile must not have holes");
10134 ret = -EINVAL;
10135 goto out;
10136 }
10137 if (em->block_start == EXTENT_MAP_INLINE) {
10138 /*
10139 * It's unlikely we'll ever actually find ourselves
10140 * here, as a file small enough to fit inline won't be
10141 * big enough to store more than the swap header, but in
10142 * case something changes in the future, let's catch it
10143 * here rather than later.
10144 */
10145 btrfs_warn(fs_info, "swapfile must not be inline");
10146 ret = -EINVAL;
10147 goto out;
10148 }
10149 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10150 btrfs_warn(fs_info, "swapfile must not be compressed");
10151 ret = -EINVAL;
10152 goto out;
10153 }
10154
10155 logical_block_start = em->block_start + (start - em->start);
10156 len = min(len, em->len - (start - em->start));
10157 free_extent_map(em);
10158 em = NULL;
10159
Boris Burkova84d5d42020-08-18 11:00:05 -070010160 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010161 if (ret < 0) {
10162 goto out;
10163 } else if (ret) {
10164 ret = 0;
10165 } else {
10166 btrfs_warn(fs_info,
10167 "swapfile must not be copy-on-write");
10168 ret = -EINVAL;
10169 goto out;
10170 }
10171
10172 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10173 if (IS_ERR(em)) {
10174 ret = PTR_ERR(em);
10175 goto out;
10176 }
10177
10178 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10179 btrfs_warn(fs_info,
10180 "swapfile must have single data profile");
10181 ret = -EINVAL;
10182 goto out;
10183 }
10184
10185 if (device == NULL) {
10186 device = em->map_lookup->stripes[0].dev;
10187 ret = btrfs_add_swapfile_pin(inode, device, false);
10188 if (ret == 1)
10189 ret = 0;
10190 else if (ret)
10191 goto out;
10192 } else if (device != em->map_lookup->stripes[0].dev) {
10193 btrfs_warn(fs_info, "swapfile must be on one device");
10194 ret = -EINVAL;
10195 goto out;
10196 }
10197
10198 physical_block_start = (em->map_lookup->stripes[0].physical +
10199 (logical_block_start - em->start));
10200 len = min(len, em->len - (logical_block_start - em->start));
10201 free_extent_map(em);
10202 em = NULL;
10203
10204 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10205 if (!bg) {
10206 btrfs_warn(fs_info,
10207 "could not find block group containing swapfile");
10208 ret = -EINVAL;
10209 goto out;
10210 }
10211
10212 ret = btrfs_add_swapfile_pin(inode, bg, true);
10213 if (ret) {
10214 btrfs_put_block_group(bg);
10215 if (ret == 1)
10216 ret = 0;
10217 else
10218 goto out;
10219 }
10220
10221 if (bsi.block_len &&
10222 bsi.block_start + bsi.block_len == physical_block_start) {
10223 bsi.block_len += len;
10224 } else {
10225 if (bsi.block_len) {
10226 ret = btrfs_add_swap_extent(sis, &bsi);
10227 if (ret)
10228 goto out;
10229 }
10230 bsi.start = start;
10231 bsi.block_start = physical_block_start;
10232 bsi.block_len = len;
10233 }
10234
10235 start += len;
10236 }
10237
10238 if (bsi.block_len)
10239 ret = btrfs_add_swap_extent(sis, &bsi);
10240
10241out:
10242 if (!IS_ERR_OR_NULL(em))
10243 free_extent_map(em);
10244
10245 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10246
10247 if (ret)
10248 btrfs_swap_deactivate(file);
10249
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010250 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010251
10252 if (ret)
10253 return ret;
10254
10255 if (device)
10256 sis->bdev = device->bdev;
10257 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10258 sis->max = bsi.nr_pages;
10259 sis->pages = bsi.nr_pages - 1;
10260 sis->highest_bit = bsi.nr_pages - 1;
10261 return bsi.nr_extents;
10262}
10263#else
10264static void btrfs_swap_deactivate(struct file *file)
10265{
10266}
10267
10268static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10269 sector_t *span)
10270{
10271 return -EOPNOTSUPP;
10272}
10273#endif
10274
Filipe Manana2766ff62020-11-04 11:07:34 +000010275/*
10276 * Update the number of bytes used in the VFS' inode. When we replace extents in
10277 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10278 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10279 * always get a correct value.
10280 */
10281void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10282 const u64 add_bytes,
10283 const u64 del_bytes)
10284{
10285 if (add_bytes == del_bytes)
10286 return;
10287
10288 spin_lock(&inode->lock);
10289 if (del_bytes > 0)
10290 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10291 if (add_bytes > 0)
10292 inode_add_bytes(&inode->vfs_inode, add_bytes);
10293 spin_unlock(&inode->lock);
10294}
10295
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010296static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010297 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010298 .lookup = btrfs_lookup,
10299 .create = btrfs_create,
10300 .unlink = btrfs_unlink,
10301 .link = btrfs_link,
10302 .mkdir = btrfs_mkdir,
10303 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010304 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010305 .symlink = btrfs_symlink,
10306 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010307 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010308 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010309 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010310 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010311 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010312 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010313 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010314};
Yan, Zheng76dda932009-09-21 16:00:26 -040010315
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010316static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010317 .llseek = generic_file_llseek,
10318 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010319 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010320 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010321 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010322#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010323 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010324#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010325 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010326 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010327};
10328
Chris Mason35054392009-01-21 13:11:13 -050010329/*
10330 * btrfs doesn't support the bmap operation because swapfiles
10331 * use bmap to make a mapping of extents in the file. They assume
10332 * these extents won't change over the life of the file and they
10333 * use the bmap result to do IO directly to the drive.
10334 *
10335 * the btrfs bmap call would return logical addresses that aren't
10336 * suitable for IO and they also will change frequently as COW
10337 * operations happen. So, swapfile + btrfs == corruption.
10338 *
10339 * For now we're avoiding this by dropping bmap.
10340 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010341static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010342 .readpage = btrfs_readpage,
10343 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010344 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010345 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010346 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010347 .invalidatepage = btrfs_invalidatepage,
10348 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010349#ifdef CONFIG_MIGRATION
10350 .migratepage = btrfs_migratepage,
10351#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010352 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010353 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010354 .swap_activate = btrfs_swap_activate,
10355 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010356};
10357
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010358static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010359 .getattr = btrfs_getattr,
10360 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010361 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010362 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010363 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010364 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010365 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010366 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010367};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010368static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010369 .getattr = btrfs_getattr,
10370 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010371 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010372 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010373 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010374 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010375 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010376};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010377static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010378 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010379 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010380 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010381 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010382 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010383 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010384};
Yan, Zheng76dda932009-09-21 16:00:26 -040010385
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010386const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010387 .d_delete = btrfs_dentry_delete,
10388};