blob: 3337c8ee79280793bfe0cb2cca5c1e8b773762fb [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"
Filipe David Borba Manana63541922014-01-07 11:47:46 +000048#include "props.h"
Dongsheng Yang31193212014-12-12 16:44:35 +080049#include "qgroup.h"
Josef Bacik86736342019-06-19 15:12:00 -040050#include "delalloc-space.h"
Josef Bacikaac00232019-06-20 15:37:44 -040051#include "block-group.h"
Filipe Manana467dc472020-05-27 11:16:07 +010052#include "space-info.h"
Chris Mason39279cc2007-06-12 06:35:45 -040053
54struct btrfs_iget_args {
David Sterba0202e832020-05-15 19:35:59 +020055 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -040056 struct btrfs_root *root;
57};
58
Filipe Mananaf28a4922015-12-08 19:23:20 +000059struct btrfs_dio_data {
Filipe Mananaf28a4922015-12-08 19:23:20 +000060 u64 reserve;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050061 loff_t length;
62 ssize_t submitted;
63 struct extent_changeset *data_reserved;
Filipe Mananaf28a4922015-12-08 19:23:20 +000064};
65
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070066static const struct inode_operations btrfs_dir_inode_operations;
67static const struct inode_operations btrfs_symlink_inode_operations;
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070068static const struct inode_operations btrfs_special_inode_operations;
69static const struct inode_operations btrfs_file_inode_operations;
Alexey Dobriyan7f094102009-09-21 17:01:10 -070070static const struct address_space_operations btrfs_aops;
Alexey Dobriyan828c0952009-10-01 15:43:56 -070071static const struct file_operations btrfs_dir_file_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040072
73static struct kmem_cache *btrfs_inode_cachep;
74struct kmem_cache *btrfs_trans_handle_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040075struct kmem_cache *btrfs_path_cachep;
Josef Bacikdc89e982011-01-28 17:05:48 -050076struct kmem_cache *btrfs_free_space_cachep;
Christophe Leroy3acd4852019-08-21 15:05:55 +000077struct kmem_cache *btrfs_free_space_bitmap_cachep;
Chris Mason39279cc2007-06-12 06:35:45 -040078
Eric Sandeen3972f262013-01-12 02:57:22 +000079static int btrfs_setsize(struct inode *inode, struct iattr *attr);
Filipe Manana213e8c52018-02-06 20:40:31 +000080static int btrfs_truncate(struct inode *inode, bool skip_writeback);
Josef Bacik5fd02042012-05-02 14:00:54 -040081static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
Nikolay Borisov6e26c442020-06-03 08:55:14 +030082static noinline int cow_file_range(struct btrfs_inode *inode,
Chris Mason771ed682008-11-06 22:02:51 -050083 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +030084 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +030085 unsigned long *nr_written, int unlock);
Nikolay Borisov4b67c112020-06-03 08:55:05 +030086static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
87 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -080088 u64 block_len, u64 orig_block_len,
89 u64 ram_bytes, int compress_type,
90 int type);
Josef Bacik7b128762008-07-24 12:17:14 -040091
Nikolay Borisovb672b5c2020-06-03 08:55:24 +030092static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +080093 const u64 offset, const u64 bytes,
94 const bool uptodate);
95
96/*
Goldwyn Rodriguesa14b78a2020-09-24 11:39:16 -050097 * btrfs_inode_lock - lock inode i_rwsem based on arguments passed
98 *
99 * ilock_flags can have the following bit set:
100 *
101 * BTRFS_ILOCK_SHARED - acquire a shared lock on the inode
102 * BTRFS_ILOCK_TRY - try to acquire the lock, if fails on first attempt
103 * return -EAGAIN
104 */
105int btrfs_inode_lock(struct inode *inode, unsigned int ilock_flags)
106{
107 if (ilock_flags & BTRFS_ILOCK_SHARED) {
108 if (ilock_flags & BTRFS_ILOCK_TRY) {
109 if (!inode_trylock_shared(inode))
110 return -EAGAIN;
111 else
112 return 0;
113 }
114 inode_lock_shared(inode);
115 } else {
116 if (ilock_flags & BTRFS_ILOCK_TRY) {
117 if (!inode_trylock(inode))
118 return -EAGAIN;
119 else
120 return 0;
121 }
122 inode_lock(inode);
123 }
124 return 0;
125}
126
127/*
128 * btrfs_inode_unlock - unock inode i_rwsem
129 *
130 * ilock_flags should contain the same bits set as passed to btrfs_inode_lock()
131 * to decide whether the lock acquired is shared or exclusive.
132 */
133void btrfs_inode_unlock(struct inode *inode, unsigned int ilock_flags)
134{
135 if (ilock_flags & BTRFS_ILOCK_SHARED)
136 inode_unlock_shared(inode);
137 else
138 inode_unlock(inode);
139}
140
141/*
Qu Wenruo524272602017-03-08 10:25:52 +0800142 * Cleanup all submitted ordered extents in specified range to handle errors
Andrea Gelmini52042d82018-11-28 12:05:13 +0100143 * from the btrfs_run_delalloc_range() callback.
Qu Wenruo524272602017-03-08 10:25:52 +0800144 *
145 * NOTE: caller must ensure that when an error happens, it can not call
146 * extent_clear_unlock_delalloc() to clear both the bits EXTENT_DO_ACCOUNTING
147 * and EXTENT_DELALLOC simultaneously, because that causes the reserved metadata
148 * to be released, which we want to happen only when finishing the ordered
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200149 * extent (btrfs_finish_ordered_io()).
Qu Wenruo524272602017-03-08 10:25:52 +0800150 */
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300151static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200152 struct page *locked_page,
153 u64 offset, u64 bytes)
Qu Wenruo524272602017-03-08 10:25:52 +0800154{
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900155 unsigned long index = offset >> PAGE_SHIFT;
156 unsigned long end_index = (offset + bytes - 1) >> PAGE_SHIFT;
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200157 u64 page_start = page_offset(locked_page);
158 u64 page_end = page_start + PAGE_SIZE - 1;
159
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900160 struct page *page;
161
162 while (index <= end_index) {
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300163 page = find_get_page(inode->vfs_inode.i_mapping, index);
Naohiro Aota63d71450c2017-09-01 17:58:47 +0900164 index++;
165 if (!page)
166 continue;
167 ClearPagePrivate2(page);
168 put_page(page);
169 }
Nikolay Borisovd1051d62018-11-21 17:10:52 +0200170
171 /*
172 * In case this page belongs to the delalloc range being instantiated
173 * then skip it, since the first page of a range is going to be
174 * properly cleaned up by the caller of run_delalloc_range
175 */
176 if (page_start >= offset && page_end <= (offset + bytes - 1)) {
177 offset += PAGE_SIZE;
178 bytes -= PAGE_SIZE;
179 }
180
Nikolay Borisov64e1db52020-06-03 08:55:25 +0300181 return __endio_write_update_ordered(inode, offset, bytes, false);
Qu Wenruo524272602017-03-08 10:25:52 +0800182}
183
Eric Sandeen48a3b632013-04-25 20:41:01 +0000184static int btrfs_dirty_inode(struct inode *inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400185
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000186static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
Eric Paris2a7dba32011-02-01 11:05:39 -0500187 struct inode *inode, struct inode *dir,
188 const struct qstr *qstr)
Jim Owens0279b4c2009-02-04 09:29:13 -0500189{
190 int err;
191
Yan, Zhengf34f57a2009-11-12 09:35:27 +0000192 err = btrfs_init_acl(trans, inode, dir);
Jim Owens0279b4c2009-02-04 09:29:13 -0500193 if (!err)
Eric Paris2a7dba32011-02-01 11:05:39 -0500194 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
Jim Owens0279b4c2009-02-04 09:29:13 -0500195 return err;
196}
197
Chris Masond352ac62008-09-29 15:18:18 -0400198/*
Chris Masonc8b97812008-10-29 14:49:59 -0400199 * this does all the hard work for inserting an inline extent into
200 * the btree. The caller should have done a btrfs_drop_extents so that
201 * no overlapping inline items exist in the btree
202 */
Chris Mason40f76582014-05-21 13:35:51 -0700203static int insert_inline_extent(struct btrfs_trans_handle *trans,
Filipe Manana5893dfb2020-11-04 11:07:32 +0000204 struct btrfs_path *path, bool extent_inserted,
Chris Masonc8b97812008-10-29 14:49:59 -0400205 struct btrfs_root *root, struct inode *inode,
206 u64 start, size_t size, size_t compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000207 int compress_type,
Chris Masonc8b97812008-10-29 14:49:59 -0400208 struct page **compressed_pages)
209{
Chris Masonc8b97812008-10-29 14:49:59 -0400210 struct extent_buffer *leaf;
211 struct page *page = NULL;
212 char *kaddr;
213 unsigned long ptr;
214 struct btrfs_file_extent_item *ei;
Chris Masonc8b97812008-10-29 14:49:59 -0400215 int ret;
216 size_t cur_size = size;
Chris Masonc8b97812008-10-29 14:49:59 -0400217 unsigned long offset;
Chris Masonc8b97812008-10-29 14:49:59 -0400218
Jia-Ju Bai982f1f5d2019-07-27 16:51:13 +0800219 ASSERT((compressed_size > 0 && compressed_pages) ||
220 (compressed_size == 0 && !compressed_pages));
221
Li Zefanfe3f5662011-03-28 08:30:38 +0000222 if (compressed_size && compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400223 cur_size = compressed_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400224
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000225 if (!extent_inserted) {
226 struct btrfs_key key;
227 size_t datasize;
228
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +0200229 key.objectid = btrfs_ino(BTRFS_I(inode));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000230 key.offset = start;
David Sterba962a2982014-06-04 18:41:45 +0200231 key.type = BTRFS_EXTENT_DATA_KEY;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000232
233 datasize = btrfs_file_extent_calc_inline_size(cur_size);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000234 ret = btrfs_insert_empty_item(trans, root, path, &key,
235 datasize);
David Sterba79b4f4c2017-06-15 19:09:51 +0200236 if (ret)
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000237 goto fail;
Chris Masonc8b97812008-10-29 14:49:59 -0400238 }
239 leaf = path->nodes[0];
240 ei = btrfs_item_ptr(leaf, path->slots[0],
241 struct btrfs_file_extent_item);
242 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
243 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
244 btrfs_set_file_extent_encryption(leaf, ei, 0);
245 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
246 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
247 ptr = btrfs_file_extent_inline_start(ei);
248
Li Zefan261507a02010-12-17 14:21:50 +0800249 if (compress_type != BTRFS_COMPRESS_NONE) {
Chris Masonc8b97812008-10-29 14:49:59 -0400250 struct page *cpage;
251 int i = 0;
Chris Masond3977122009-01-05 21:25:51 -0500252 while (compressed_size > 0) {
Chris Masonc8b97812008-10-29 14:49:59 -0400253 cpage = compressed_pages[i];
Chris Mason5b050f02008-11-11 09:34:41 -0500254 cur_size = min_t(unsigned long, compressed_size,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300255 PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400256
Cong Wang7ac687d2011-11-25 23:14:28 +0800257 kaddr = kmap_atomic(cpage);
Chris Masonc8b97812008-10-29 14:49:59 -0400258 write_extent_buffer(leaf, kaddr, ptr, cur_size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800259 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400260
261 i++;
262 ptr += cur_size;
263 compressed_size -= cur_size;
264 }
265 btrfs_set_file_extent_compression(leaf, ei,
Li Zefan261507a02010-12-17 14:21:50 +0800266 compress_type);
Chris Masonc8b97812008-10-29 14:49:59 -0400267 } else {
268 page = find_get_page(inode->i_mapping,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300269 start >> PAGE_SHIFT);
Chris Masonc8b97812008-10-29 14:49:59 -0400270 btrfs_set_file_extent_compression(leaf, ei, 0);
Cong Wang7ac687d2011-11-25 23:14:28 +0800271 kaddr = kmap_atomic(page);
Johannes Thumshirn70730172018-12-05 15:23:03 +0100272 offset = offset_in_page(start);
Chris Masonc8b97812008-10-29 14:49:59 -0400273 write_extent_buffer(leaf, kaddr + offset, ptr, size);
Cong Wang7ac687d2011-11-25 23:14:28 +0800274 kunmap_atomic(kaddr);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300275 put_page(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400276 }
277 btrfs_mark_buffer_dirty(leaf);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000278 btrfs_release_path(path);
Chris Masonc8b97812008-10-29 14:49:59 -0400279
Yan, Zhengc2167752009-11-12 09:34:21 +0000280 /*
Josef Bacik9ddc9592020-01-17 09:02:22 -0500281 * We align size to sectorsize for inline extents just for simplicity
282 * sake.
283 */
284 size = ALIGN(size, root->fs_info->sectorsize);
285 ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), start, size);
286 if (ret)
287 goto fail;
288
289 /*
Yan, Zhengc2167752009-11-12 09:34:21 +0000290 * we're an inline extent, so nobody can
291 * extend the file past i_size without locking
292 * a page we already have locked.
293 *
294 * We must do any isize and inode updates
295 * before we unlock the pages. Otherwise we
296 * could end up racing with unlink.
297 */
Chris Masonc8b97812008-10-29 14:49:59 -0400298 BTRFS_I(inode)->disk_i_size = inode->i_size;
Chris Masonc8b97812008-10-29 14:49:59 -0400299fail:
David Sterba79b4f4c2017-06-15 19:09:51 +0200300 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400301}
302
303
304/*
305 * conditionally insert an inline extent into the file. This
306 * does the checks required to make sure the data is small enough
307 * to fit as an inline extent.
308 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300309static noinline int cow_file_range_inline(struct btrfs_inode *inode, u64 start,
Josef Bacik00361582013-08-14 14:02:47 -0400310 u64 end, size_t compressed_size,
311 int compress_type,
312 struct page **compressed_pages)
Chris Masonc8b97812008-10-29 14:49:59 -0400313{
Filipe Manana5893dfb2020-11-04 11:07:32 +0000314 struct btrfs_drop_extents_args drop_args = { 0 };
Nikolay Borisova0349402020-06-03 08:55:12 +0300315 struct btrfs_root *root = inode->root;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400316 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik00361582013-08-14 14:02:47 -0400317 struct btrfs_trans_handle *trans;
Nikolay Borisova0349402020-06-03 08:55:12 +0300318 u64 isize = i_size_read(&inode->vfs_inode);
Chris Masonc8b97812008-10-29 14:49:59 -0400319 u64 actual_end = min(end + 1, isize);
320 u64 inline_len = actual_end - start;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400321 u64 aligned_end = ALIGN(end, fs_info->sectorsize);
Chris Masonc8b97812008-10-29 14:49:59 -0400322 u64 data_len = inline_len;
323 int ret;
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000324 struct btrfs_path *path;
Chris Masonc8b97812008-10-29 14:49:59 -0400325
326 if (compressed_size)
327 data_len = compressed_size;
328
329 if (start > 0 ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400330 actual_end > fs_info->sectorsize ||
331 data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400332 (!compressed_size &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400333 (actual_end & (fs_info->sectorsize - 1)) == 0) ||
Chris Masonc8b97812008-10-29 14:49:59 -0400334 end + 1 < isize ||
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400335 data_len > fs_info->max_inline) {
Chris Masonc8b97812008-10-29 14:49:59 -0400336 return 1;
337 }
338
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000339 path = btrfs_alloc_path();
340 if (!path)
341 return -ENOMEM;
342
Josef Bacik00361582013-08-14 14:02:47 -0400343 trans = btrfs_join_transaction(root);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000344 if (IS_ERR(trans)) {
345 btrfs_free_path(path);
Josef Bacik00361582013-08-14 14:02:47 -0400346 return PTR_ERR(trans);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000347 }
Nikolay Borisova0349402020-06-03 08:55:12 +0300348 trans->block_rsv = &inode->block_rsv;
Josef Bacik00361582013-08-14 14:02:47 -0400349
Filipe Manana5893dfb2020-11-04 11:07:32 +0000350 drop_args.path = path;
351 drop_args.start = start;
352 drop_args.end = aligned_end;
353 drop_args.drop_cache = true;
354 drop_args.replace_extent = true;
355
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000356 if (compressed_size && compressed_pages)
Filipe Manana5893dfb2020-11-04 11:07:32 +0000357 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000358 compressed_size);
359 else
Filipe Manana5893dfb2020-11-04 11:07:32 +0000360 drop_args.extent_item_size = btrfs_file_extent_calc_inline_size(
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000361 inline_len);
362
Filipe Manana5893dfb2020-11-04 11:07:32 +0000363 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik00361582013-08-14 14:02:47 -0400364 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400365 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400366 goto out;
367 }
Chris Masonc8b97812008-10-29 14:49:59 -0400368
369 if (isize > actual_end)
370 inline_len = min_t(u64, isize, actual_end);
Filipe Manana5893dfb2020-11-04 11:07:32 +0000371 ret = insert_inline_extent(trans, path, drop_args.extent_inserted,
Nikolay Borisova0349402020-06-03 08:55:12 +0300372 root, &inode->vfs_inode, start,
Chris Masonc8b97812008-10-29 14:49:59 -0400373 inline_len, compressed_size,
Li Zefanfe3f5662011-03-28 08:30:38 +0000374 compress_type, compressed_pages);
Josef Bacik2adcac12012-05-23 16:10:14 -0400375 if (ret && ret != -ENOSPC) {
Jeff Mahoney66642832016-06-10 18:19:25 -0400376 btrfs_abort_transaction(trans, ret);
Josef Bacik00361582013-08-14 14:02:47 -0400377 goto out;
Josef Bacik2adcac12012-05-23 16:10:14 -0400378 } else if (ret == -ENOSPC) {
Josef Bacik00361582013-08-14 14:02:47 -0400379 ret = 1;
380 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100381 }
Josef Bacik2adcac12012-05-23 16:10:14 -0400382
Filipe Manana2766ff62020-11-04 11:07:34 +0000383 btrfs_update_inode_bytes(inode, inline_len, drop_args.bytes_found);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +0200384 ret = btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +0000385 if (ret && ret != -ENOSPC) {
386 btrfs_abort_transaction(trans, ret);
387 goto out;
388 } else if (ret == -ENOSPC) {
389 ret = 1;
390 goto out;
391 }
392
Nikolay Borisova0349402020-06-03 08:55:12 +0300393 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &inode->runtime_flags);
Josef Bacik00361582013-08-14 14:02:47 -0400394out:
Qu Wenruo94ed9382015-09-08 17:25:56 +0800395 /*
396 * Don't forget to free the reserved space, as for inlined extent
397 * it won't count as data extent, free them directly here.
398 * And at reserve time, it's always aligned to page size, so
399 * just free one page here.
400 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300401 btrfs_qgroup_free_data(inode, NULL, 0, PAGE_SIZE);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +0000402 btrfs_free_path(path);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -0400403 btrfs_end_transaction(trans);
Josef Bacik00361582013-08-14 14:02:47 -0400404 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -0400405}
406
Chris Mason771ed682008-11-06 22:02:51 -0500407struct async_extent {
408 u64 start;
409 u64 ram_size;
410 u64 compressed_size;
411 struct page **pages;
412 unsigned long nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800413 int compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500414 struct list_head list;
415};
416
Nikolay Borisov97db1202019-03-12 17:20:24 +0200417struct async_chunk {
Chris Mason771ed682008-11-06 22:02:51 -0500418 struct inode *inode;
Chris Mason771ed682008-11-06 22:02:51 -0500419 struct page *locked_page;
420 u64 start;
421 u64 end;
Liu Bof82b7352017-10-23 23:18:16 -0600422 unsigned int write_flags;
Chris Mason771ed682008-11-06 22:02:51 -0500423 struct list_head extents;
Chris Masonec39f762019-07-10 12:28:17 -0700424 struct cgroup_subsys_state *blkcg_css;
Chris Mason771ed682008-11-06 22:02:51 -0500425 struct btrfs_work work;
Nikolay Borisov97db1202019-03-12 17:20:24 +0200426 atomic_t *pending;
Chris Mason771ed682008-11-06 22:02:51 -0500427};
428
Nikolay Borisov97db1202019-03-12 17:20:24 +0200429struct async_cow {
430 /* Number of chunks in flight; must be first in the structure */
431 atomic_t num_chunks;
432 struct async_chunk chunks[];
433};
434
435static noinline int add_async_extent(struct async_chunk *cow,
Chris Mason771ed682008-11-06 22:02:51 -0500436 u64 start, u64 ram_size,
437 u64 compressed_size,
438 struct page **pages,
Li Zefan261507a02010-12-17 14:21:50 +0800439 unsigned long nr_pages,
440 int compress_type)
Chris Mason771ed682008-11-06 22:02:51 -0500441{
442 struct async_extent *async_extent;
443
444 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100445 BUG_ON(!async_extent); /* -ENOMEM */
Chris Mason771ed682008-11-06 22:02:51 -0500446 async_extent->start = start;
447 async_extent->ram_size = ram_size;
448 async_extent->compressed_size = compressed_size;
449 async_extent->pages = pages;
450 async_extent->nr_pages = nr_pages;
Li Zefan261507a02010-12-17 14:21:50 +0800451 async_extent->compress_type = compress_type;
Chris Mason771ed682008-11-06 22:02:51 -0500452 list_add_tail(&async_extent->list, &cow->extents);
453 return 0;
454}
455
Qu Wenruo42c16da2019-07-01 05:12:46 +0000456/*
457 * Check if the inode has flags compatible with compression
458 */
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300459static inline bool inode_can_compress(struct btrfs_inode *inode)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000460{
Nikolay Borisov99c88dc2020-06-03 08:55:26 +0300461 if (inode->flags & BTRFS_INODE_NODATACOW ||
462 inode->flags & BTRFS_INODE_NODATASUM)
Qu Wenruo42c16da2019-07-01 05:12:46 +0000463 return false;
464 return true;
465}
466
467/*
468 * Check if the inode needs to be submitted to compression, based on mount
469 * options, defragmentation, properties or heuristics.
470 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300471static inline int inode_need_compress(struct btrfs_inode *inode, u64 start,
472 u64 end)
Wang Shilongf79707b2014-07-17 11:44:09 +0800473{
Nikolay Borisov808a1292020-06-03 08:55:27 +0300474 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Wang Shilongf79707b2014-07-17 11:44:09 +0800475
Nikolay Borisov808a1292020-06-03 08:55:27 +0300476 if (!inode_can_compress(inode)) {
Qu Wenruo42c16da2019-07-01 05:12:46 +0000477 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
478 KERN_ERR "BTRFS: unexpected compression for ino %llu\n",
Nikolay Borisov808a1292020-06-03 08:55:27 +0300479 btrfs_ino(inode));
Qu Wenruo42c16da2019-07-01 05:12:46 +0000480 return 0;
481 }
Wang Shilongf79707b2014-07-17 11:44:09 +0800482 /* force compress */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400483 if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
Wang Shilongf79707b2014-07-17 11:44:09 +0800484 return 1;
David Sterbaeec63c62017-07-17 19:41:31 +0200485 /* defrag ioctl */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300486 if (inode->defrag_compress)
David Sterbaeec63c62017-07-17 19:41:31 +0200487 return 1;
Wang Shilongf79707b2014-07-17 11:44:09 +0800488 /* bad compression ratios */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300489 if (inode->flags & BTRFS_INODE_NOCOMPRESS)
Wang Shilongf79707b2014-07-17 11:44:09 +0800490 return 0;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400491 if (btrfs_test_opt(fs_info, COMPRESS) ||
Nikolay Borisov808a1292020-06-03 08:55:27 +0300492 inode->flags & BTRFS_INODE_COMPRESS ||
493 inode->prop_compress)
494 return btrfs_compress_heuristic(&inode->vfs_inode, start, end);
Wang Shilongf79707b2014-07-17 11:44:09 +0800495 return 0;
496}
497
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200498static inline void inode_should_defrag(struct btrfs_inode *inode,
Anand Jain26d30f82016-12-19 19:09:06 +0800499 u64 start, u64 end, u64 num_bytes, u64 small_write)
500{
501 /* If this is a small write inside eof, kick off a defrag */
502 if (num_bytes < small_write &&
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200503 (start > 0 || end + 1 < inode->disk_i_size))
Anand Jain26d30f82016-12-19 19:09:06 +0800504 btrfs_add_inode_defrag(NULL, inode);
505}
506
Chris Masonc8b97812008-10-29 14:49:59 -0400507/*
Chris Mason771ed682008-11-06 22:02:51 -0500508 * we create compressed extents in two phases. The first
509 * phase compresses a range of pages that have already been
510 * locked (both pages and state bits are locked).
Chris Masonc8b97812008-10-29 14:49:59 -0400511 *
Chris Mason771ed682008-11-06 22:02:51 -0500512 * This is done inside an ordered work queue, and the compression
513 * is spread across many cpus. The actual IO submission is step
514 * two, and the ordered work queue takes care of making sure that
515 * happens in the same order things were put onto the queue by
516 * writepages and friends.
Chris Masonc8b97812008-10-29 14:49:59 -0400517 *
Chris Mason771ed682008-11-06 22:02:51 -0500518 * If this code finds it can't get good compression, it puts an
519 * entry onto the work queue to write the uncompressed bytes. This
520 * makes sure that both compressed inodes and uncompressed inodes
Artem Bityutskiyb2570312012-07-25 18:12:06 +0300521 * are written in the same order that the flusher thread sent them
522 * down.
Chris Masond352ac62008-09-29 15:18:18 -0400523 */
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300524static noinline int compress_file_range(struct async_chunk *async_chunk)
Chris Masonb888db22007-08-27 16:49:44 -0400525{
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200526 struct inode *inode = async_chunk->inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400527 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400528 u64 blocksize = fs_info->sectorsize;
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200529 u64 start = async_chunk->start;
530 u64 end = async_chunk->end;
Chris Masonc8b97812008-10-29 14:49:59 -0400531 u64 actual_end;
Josef Bacikd98da492019-10-11 09:03:54 -0400532 u64 i_size;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400533 int ret = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400534 struct page **pages = NULL;
535 unsigned long nr_pages;
Chris Masonc8b97812008-10-29 14:49:59 -0400536 unsigned long total_compressed = 0;
537 unsigned long total_in = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400538 int i;
539 int will_compress;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400540 int compress_type = fs_info->compress_type;
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300541 int compressed_extents = 0;
Chris Mason4adaa612013-03-26 13:07:00 -0400542 int redirty = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400543
Nikolay Borisov6158e1c2017-02-20 13:50:43 +0200544 inode_should_defrag(BTRFS_I(inode), start, end, end - start + 1,
545 SZ_16K);
Chris Mason4cb53002011-05-24 15:35:30 -0400546
Josef Bacikd98da492019-10-11 09:03:54 -0400547 /*
548 * We need to save i_size before now because it could change in between
549 * us evaluating the size and assigning it. This is because we lock and
550 * unlock the page in truncate and fallocate, and then modify the i_size
551 * later on.
552 *
553 * The barriers are to emulate READ_ONCE, remove that once i_size_read
554 * does that for us.
555 */
556 barrier();
557 i_size = i_size_read(inode);
558 barrier();
559 actual_end = min_t(u64, i_size, end + 1);
Chris Masonc8b97812008-10-29 14:49:59 -0400560again:
561 will_compress = 0;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300562 nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
David Sterba069eac72017-02-14 19:36:54 +0100563 BUILD_BUG_ON((BTRFS_MAX_COMPRESSED % PAGE_SIZE) != 0);
564 nr_pages = min_t(unsigned long, nr_pages,
565 BTRFS_MAX_COMPRESSED / PAGE_SIZE);
Chris Masonc8b97812008-10-29 14:49:59 -0400566
Chris Masonf03d9301f2009-02-04 09:31:06 -0500567 /*
568 * we don't want to send crud past the end of i_size through
569 * compression, that's just a waste of CPU time. So, if the
570 * end of the file is before the start of our current
571 * requested range of bytes, we bail out to the uncompressed
572 * cleanup code that can deal with all of this.
573 *
574 * It isn't really the fastest way to fix things, but this is a
575 * very uncommon corner.
576 */
577 if (actual_end <= start)
578 goto cleanup_and_bail_uncompressed;
579
Chris Masonc8b97812008-10-29 14:49:59 -0400580 total_compressed = actual_end - start;
581
Shilong Wang4bcbb332014-10-07 18:44:35 -0400582 /*
583 * skip compression for a small file range(<=blocksize) that
Nicholas D Steeves01327612016-05-19 21:18:45 -0400584 * isn't an inline extent, since it doesn't save disk space at all.
Shilong Wang4bcbb332014-10-07 18:44:35 -0400585 */
586 if (total_compressed <= blocksize &&
587 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
588 goto cleanup_and_bail_uncompressed;
589
David Sterba069eac72017-02-14 19:36:54 +0100590 total_compressed = min_t(unsigned long, total_compressed,
591 BTRFS_MAX_UNCOMPRESSED);
Chris Masonc8b97812008-10-29 14:49:59 -0400592 total_in = 0;
593 ret = 0;
Chris Masondb945352007-10-15 16:15:53 -0400594
Chris Mason771ed682008-11-06 22:02:51 -0500595 /*
596 * we do compression for mount -o compress and when the
597 * inode has not been flagged as nocompress. This flag can
598 * change at any time if we discover bad compression ratios.
Chris Masonc8b97812008-10-29 14:49:59 -0400599 */
Nikolay Borisov808a1292020-06-03 08:55:27 +0300600 if (inode_need_compress(BTRFS_I(inode), start, end)) {
Chris Masonc8b97812008-10-29 14:49:59 -0400601 WARN_ON(pages);
David Sterba31e818f2015-02-20 18:00:26 +0100602 pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
Li Zefan560f7d72011-09-08 10:22:01 +0800603 if (!pages) {
604 /* just bail out to the uncompressed code */
Filipe Manana3527a012018-10-13 00:37:25 +0100605 nr_pages = 0;
Li Zefan560f7d72011-09-08 10:22:01 +0800606 goto cont;
607 }
Chris Mason179e29e2007-11-01 11:28:41 -0400608
David Sterbaeec63c62017-07-17 19:41:31 +0200609 if (BTRFS_I(inode)->defrag_compress)
610 compress_type = BTRFS_I(inode)->defrag_compress;
611 else if (BTRFS_I(inode)->prop_compress)
David Sterbab52aa8c2017-07-17 19:17:20 +0200612 compress_type = BTRFS_I(inode)->prop_compress;
Li Zefan261507a02010-12-17 14:21:50 +0800613
Chris Mason4adaa612013-03-26 13:07:00 -0400614 /*
615 * we need to call clear_page_dirty_for_io on each
616 * page in the range. Otherwise applications with the file
617 * mmap'd can wander in and change the page contents while
618 * we are compressing them.
619 *
620 * If the compression fails for any reason, we set the pages
621 * dirty again later on.
Timofey Titovetse9679de2017-10-24 01:29:48 +0300622 *
623 * Note that the remaining part is redirtied, the start pointer
624 * has moved, the end is the original one.
Chris Mason4adaa612013-03-26 13:07:00 -0400625 */
Timofey Titovetse9679de2017-10-24 01:29:48 +0300626 if (!redirty) {
627 extent_range_clear_dirty_for_io(inode, start, end);
628 redirty = 1;
629 }
David Sterbaf51d2b52017-09-15 17:36:57 +0200630
631 /* Compression level is applied here and only here */
632 ret = btrfs_compress_pages(
633 compress_type | (fs_info->compress_level << 4),
Li Zefan261507a02010-12-17 14:21:50 +0800634 inode->i_mapping, start,
David Sterba38c31462017-02-14 19:04:07 +0100635 pages,
David Sterba4d3a8002017-02-14 19:04:07 +0100636 &nr_pages,
Li Zefan261507a02010-12-17 14:21:50 +0800637 &total_in,
David Sterbae5d74902017-02-14 19:45:05 +0100638 &total_compressed);
Chris Masonc8b97812008-10-29 14:49:59 -0400639
640 if (!ret) {
Johannes Thumshirn70730172018-12-05 15:23:03 +0100641 unsigned long offset = offset_in_page(total_compressed);
David Sterba4d3a8002017-02-14 19:04:07 +0100642 struct page *page = pages[nr_pages - 1];
Chris Masonc8b97812008-10-29 14:49:59 -0400643 char *kaddr;
644
645 /* zero the tail end of the last page, we might be
646 * sending it down to disk
647 */
648 if (offset) {
Cong Wang7ac687d2011-11-25 23:14:28 +0800649 kaddr = kmap_atomic(page);
Chris Masonc8b97812008-10-29 14:49:59 -0400650 memset(kaddr + offset, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300651 PAGE_SIZE - offset);
Cong Wang7ac687d2011-11-25 23:14:28 +0800652 kunmap_atomic(kaddr);
Chris Masonc8b97812008-10-29 14:49:59 -0400653 }
654 will_compress = 1;
655 }
656 }
Li Zefan560f7d72011-09-08 10:22:01 +0800657cont:
Chris Masonc8b97812008-10-29 14:49:59 -0400658 if (start == 0) {
659 /* lets try to make an inline extent */
Timofey Titovets6018ba02017-09-15 01:57:26 +0300660 if (ret || total_in < actual_end) {
Chris Masonc8b97812008-10-29 14:49:59 -0400661 /* we didn't compress the entire range, try
Chris Mason771ed682008-11-06 22:02:51 -0500662 * to make an uncompressed inline extent.
Chris Masonc8b97812008-10-29 14:49:59 -0400663 */
Nikolay Borisova0349402020-06-03 08:55:12 +0300664 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
665 0, BTRFS_COMPRESS_NONE,
666 NULL);
Chris Masonc8b97812008-10-29 14:49:59 -0400667 } else {
Chris Mason771ed682008-11-06 22:02:51 -0500668 /* try making a compressed inline extent */
Nikolay Borisova0349402020-06-03 08:55:12 +0300669 ret = cow_file_range_inline(BTRFS_I(inode), start, end,
Li Zefanfe3f5662011-03-28 08:30:38 +0000670 total_compressed,
671 compress_type, pages);
Chris Masonc8b97812008-10-29 14:49:59 -0400672 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100673 if (ret <= 0) {
Josef Bacik151a41b2013-07-29 13:22:24 -0400674 unsigned long clear_flags = EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -0400675 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
676 EXTENT_DO_ACCOUNTING;
Filipe Mananae6eb4312014-10-10 10:45:12 +0100677 unsigned long page_error_op;
678
Filipe Mananae6eb4312014-10-10 10:45:12 +0100679 page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
Josef Bacik151a41b2013-07-29 13:22:24 -0400680
Chris Mason771ed682008-11-06 22:02:51 -0500681 /*
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100682 * inline extent creation worked or returned error,
683 * we don't need to create any more async work items.
684 * Unlock and free up our temp pages.
Josef Bacik8b62f872017-10-19 14:15:55 -0400685 *
686 * We use DO_ACCOUNTING here because we need the
687 * delalloc_release_metadata to be done _after_ we drop
688 * our outstanding extent for clearing delalloc for this
689 * range.
Chris Mason771ed682008-11-06 22:02:51 -0500690 */
Nikolay Borisovad7ff172020-06-03 08:55:06 +0300691 extent_clear_unlock_delalloc(BTRFS_I(inode), start, end,
692 NULL,
Nikolay Borisov74e91942019-07-17 16:18:16 +0300693 clear_flags,
Qu Wenruoba8b04c2016-07-19 16:50:36 +0800694 PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800695 PAGE_START_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100696 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400697 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300698
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800699 /*
700 * Ensure we only free the compressed pages if we have
701 * them allocated, as we can still reach here with
702 * inode_need_compress() == false.
703 */
704 if (pages) {
705 for (i = 0; i < nr_pages; i++) {
706 WARN_ON(pages[i]->mapping);
707 put_page(pages[i]);
708 }
709 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300710 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300711 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400712 }
713 }
714
715 if (will_compress) {
716 /*
717 * we aren't doing an inline extent round the compressed size
718 * up to a block size boundary so the allocator does sane
719 * things
720 */
Qu Wenruofda28322013-02-26 08:10:22 +0000721 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400722
723 /*
724 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300725 * win, compare the page count read with the blocks on disk,
726 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400727 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300728 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300729 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300730 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700731
732 /*
733 * The async work queues will take care of doing actual
734 * allocation on disk for these compressed pages, and
735 * will submit them to the elevator.
736 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200737 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100738 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700739 compress_type);
740
Timofey Titovets11708622017-10-03 18:06:01 +0300741 if (start + total_in < end) {
742 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700743 pages = NULL;
744 cond_resched();
745 goto again;
746 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300747 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400748 }
749 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700750 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400751 /*
752 * the compression code ran but failed to make things smaller,
753 * free any pages it allocated and our page pointer array
754 */
David Sterba4d3a8002017-02-14 19:04:07 +0100755 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400756 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300757 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400758 }
759 kfree(pages);
760 pages = NULL;
761 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100762 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400763
764 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400765 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200766 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500767 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500768 }
Chris Masonc8b97812008-10-29 14:49:59 -0400769 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500770cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700771 /*
772 * No compression, but we still need to write the pages in the file
773 * we've been given so far. redirty the locked page if it corresponds
774 * to our extent and set things up for the async work queue to run
775 * cow_file_range to do the normal delalloc dance.
776 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700777 if (async_chunk->locked_page &&
778 (page_offset(async_chunk->locked_page) >= start &&
779 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200780 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700781 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700782 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700783
784 if (redirty)
785 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200786 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700787 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300788 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500789
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300790 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500791}
Chris Mason771ed682008-11-06 22:02:51 -0500792
Filipe Manana40ae8372014-10-06 22:14:24 +0100793static void free_async_extent_pages(struct async_extent *async_extent)
794{
795 int i;
796
797 if (!async_extent->pages)
798 return;
799
800 for (i = 0; i < async_extent->nr_pages; i++) {
801 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300802 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100803 }
804 kfree(async_extent->pages);
805 async_extent->nr_pages = 0;
806 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500807}
808
809/*
810 * phase two of compressed writeback. This is the ordered portion
811 * of the code, which only gets called in the order the work was
812 * queued. We walk all the async extents created by compress_file_range
813 * and send them down to the disk.
814 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200815static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500816{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300817 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
818 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500819 struct async_extent *async_extent;
820 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500821 struct btrfs_key ins;
822 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300823 struct btrfs_root *root = inode->root;
824 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500825 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500826
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500827again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200828 while (!list_empty(&async_chunk->extents)) {
829 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500830 struct async_extent, list);
831 list_del(&async_extent->list);
832
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500833retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200834 lock_extent(io_tree, async_extent->start,
835 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500836 /* did the compression code fall back to uncompressed IO? */
837 if (!async_extent->pages) {
838 int page_started = 0;
839 unsigned long nr_written = 0;
840
Chris Mason771ed682008-11-06 22:02:51 -0500841 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300842 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500843 async_extent->start,
844 async_extent->start +
845 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300846 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500847
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100848 /* JDM XXX */
849
Chris Mason771ed682008-11-06 22:02:51 -0500850 /*
851 * if page_started, cow_file_range inserted an
852 * inline extent and took care of all the unlocking
853 * and IO for us. Otherwise, we need to submit
854 * all those pages down to the drive.
855 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500856 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300857 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200858 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500859 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500860 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500861 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700862 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200863 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500864 kfree(async_extent);
865 cond_resched();
866 continue;
867 }
868
Wang Xiaoguang18513092016-07-25 15:51:40 +0800869 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500870 async_extent->compressed_size,
871 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800872 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500873 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100874 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500875
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400876 if (ret == -ENOSPC) {
877 unlock_extent(io_tree, async_extent->start,
878 async_extent->start +
879 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800880
881 /*
882 * we need to redirty the pages if we decide to
883 * fallback to uncompressed IO, otherwise we
884 * will not submit these pages down to lower
885 * layers.
886 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300887 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800888 async_extent->start,
889 async_extent->start +
890 async_extent->ram_size - 1);
891
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100892 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400893 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500894 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500895 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000896 /*
897 * here we're doing allocation and writeback of the
898 * compressed pages
899 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300900 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800901 async_extent->ram_size, /* len */
902 async_extent->start, /* orig_start */
903 ins.objectid, /* block_start */
904 ins.offset, /* block_len */
905 ins.offset, /* orig_block_len */
906 async_extent->ram_size, /* ram_bytes */
907 async_extent->compress_type,
908 BTRFS_ORDERED_COMPRESSED);
909 if (IS_ERR(em))
910 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500911 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800912 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500913
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300914 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800915 async_extent->start,
916 ins.objectid,
917 async_extent->ram_size,
918 ins.offset,
Li Zefan261507a02010-12-17 14:21:50 +0800919 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100920 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300921 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100922 async_extent->start +
923 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500924 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100925 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400926 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500927
Chris Mason771ed682008-11-06 22:02:51 -0500928 /*
929 * clear dirty, set writeback and unlock the pages.
930 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300931 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400932 async_extent->start +
933 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400934 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800935 PAGE_UNLOCK | PAGE_START_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300936 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500937 async_extent->ram_size,
938 ins.objectid,
939 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600940 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700941 async_chunk->write_flags,
942 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100943 struct page *p = async_extent->pages[0];
944 const u64 start = async_extent->start;
945 const u64 end = start + async_extent->ram_size - 1;
946
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300947 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200948 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200949
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100950 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300951 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100952 PAGE_END_WRITEBACK |
953 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100954 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100955 }
Chris Mason771ed682008-11-06 22:02:51 -0500956 alloc_hint = ins.objectid + ins.offset;
957 kfree(async_extent);
958 cond_resched();
959 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100960 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500961out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400962 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400963 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100964out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300965 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500966 async_extent->start +
967 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400968 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100969 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400970 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
Qu Wenruo6869b0a2021-01-26 16:33:45 +0800971 PAGE_UNLOCK | PAGE_START_WRITEBACK |
972 PAGE_END_WRITEBACK | PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100973 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100974 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500975 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500976}
977
Nikolay Borisov43c69842020-06-03 08:55:02 +0300978static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400979 u64 num_bytes)
980{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300981 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400982 struct extent_map *em;
983 u64 alloc_hint = 0;
984
985 read_lock(&em_tree->lock);
986 em = search_extent_mapping(em_tree, start, num_bytes);
987 if (em) {
988 /*
989 * if block start isn't an actual block number then find the
990 * first block in this inode and use that as a hint. If that
991 * block is also bogus then just don't worry about it.
992 */
993 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
994 free_extent_map(em);
995 em = search_extent_mapping(em_tree, 0, 0);
996 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
997 alloc_hint = em->block_start;
998 if (em)
999 free_extent_map(em);
1000 } else {
1001 alloc_hint = em->block_start;
1002 free_extent_map(em);
1003 }
1004 }
1005 read_unlock(&em_tree->lock);
1006
1007 return alloc_hint;
1008}
1009
Chris Mason771ed682008-11-06 22:02:51 -05001010/*
1011 * when extent_io.c finds a delayed allocation range in the file,
1012 * the call backs end up in this code. The basic idea is to
1013 * allocate extents on disk for the range, and create ordered data structs
1014 * in ram to track those extents.
1015 *
1016 * locked_page is the page that writepage had locked already. We use
1017 * it to make sure we don't do extra locks or unlocks.
1018 *
1019 * *page_started is set to one if we unlock locked_page and do everything
1020 * required to start IO on it. It may be clean and already done with
1021 * IO when we return.
1022 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001023static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001024 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001025 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001026 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001027{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001028 struct btrfs_root *root = inode->root;
1029 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001030 u64 alloc_hint = 0;
1031 u64 num_bytes;
1032 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001033 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001034 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001035 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001036 struct btrfs_key ins;
1037 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001038 unsigned clear_bits;
1039 unsigned long page_ops;
1040 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001041 int ret = 0;
1042
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001043 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001044 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001045 ret = -EINVAL;
1046 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001047 }
Chris Mason771ed682008-11-06 22:02:51 -05001048
Qu Wenruofda28322013-02-26 08:10:22 +00001049 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001050 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001051 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001052
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001053 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001054
Chris Mason771ed682008-11-06 22:02:51 -05001055 if (start == 0) {
1056 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001057 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001058 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001059 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001060 /*
1061 * We use DO_ACCOUNTING here because we need the
1062 * delalloc_release_metadata to be run _after_ we drop
1063 * our outstanding extent for clearing delalloc for this
1064 * range.
1065 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001066 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001067 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001068 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1069 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001070 PAGE_START_WRITEBACK | PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001071 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001072 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001073 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001074 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001075 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001076 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001077 }
1078 }
Chris Masonc8b97812008-10-29 14:49:59 -04001079
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001080 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1081 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001082
Filipe Manana432cd2a2020-06-08 13:32:55 +01001083 /*
1084 * Relocation relies on the relocated extents to have exactly the same
1085 * size as the original extents. Normally writeback for relocation data
1086 * extents follows a NOCOW path because relocation preallocates the
1087 * extents. However, due to an operation such as scrub turning a block
1088 * group to RO mode, it may fallback to COW mode, so we must make sure
1089 * an extent allocated during COW has exactly the requested size and can
1090 * not be split into smaller extents, otherwise relocation breaks and
1091 * fails during the stage where it updates the bytenr of file extent
1092 * items.
1093 */
1094 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1095 min_alloc_size = num_bytes;
1096 else
1097 min_alloc_size = fs_info->sectorsize;
1098
Anand Jain3752d222018-02-15 12:29:38 +08001099 while (num_bytes > 0) {
1100 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001101 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001102 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001103 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001104 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001105 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001106 cur_alloc_size = ins.offset;
1107 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001108
Chris Mason771ed682008-11-06 22:02:51 -05001109 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001110 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001111 start, /* orig_start */
1112 ins.objectid, /* block_start */
1113 ins.offset, /* block_len */
1114 ins.offset, /* orig_block_len */
1115 ram_size, /* ram_bytes */
1116 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001117 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001118 if (IS_ERR(em)) {
1119 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001120 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001121 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001122 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001123
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001124 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Qu Wenruo3c198fe2021-01-21 14:13:54 +08001125 ram_size, cur_alloc_size,
1126 BTRFS_ORDERED_REGULAR);
Liu Boace68ba2013-04-22 10:53:47 +00001127 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001128 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001129
Yan Zheng17d217f2008-12-12 10:03:38 -05001130 if (root->root_key.objectid ==
1131 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001132 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001133 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001134 /*
1135 * Only drop cache here, and process as normal.
1136 *
1137 * We must not allow extent_clear_unlock_delalloc()
1138 * at out_unlock label to free meta of this ordered
1139 * extent, as its meta should be freed by
1140 * btrfs_finish_ordered_io().
1141 *
1142 * So we must continue until @start is increased to
1143 * skip current ordered extent.
1144 */
Josef Bacik00361582013-08-14 14:02:47 -04001145 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001146 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001147 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001148 }
1149
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001150 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001151
Chris Masonc8b97812008-10-29 14:49:59 -04001152 /* we're not doing compressed IO, don't unlock the first
1153 * page (which the caller expects to stay locked), don't
1154 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001155 *
1156 * Do set the Private2 bit so we know this page was properly
1157 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001158 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001159 page_ops = unlock ? PAGE_UNLOCK : 0;
1160 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001161
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001162 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001163 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001164 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001165 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001166 if (num_bytes < cur_alloc_size)
1167 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001168 else
Anand Jain3752d222018-02-15 12:29:38 +08001169 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001170 alloc_hint = ins.objectid + ins.offset;
1171 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001172 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001173
1174 /*
1175 * btrfs_reloc_clone_csums() error, since start is increased
1176 * extent_clear_unlock_delalloc() at out_unlock label won't
1177 * free metadata of current ordered extent, we're OK to exit.
1178 */
1179 if (ret)
1180 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001181 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001182out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001183 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001184
Filipe Mananad9f85962014-08-25 10:43:00 +01001185out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001186 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001187out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001188 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001189 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001190out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001191 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1192 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001193 page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK | PAGE_END_WRITEBACK;
Filipe Mananaa315e682017-03-06 23:04:20 +00001194 /*
1195 * If we reserved an extent for our delalloc range (or a subrange) and
1196 * failed to create the respective ordered extent, then it means that
1197 * when we reserved the extent we decremented the extent's size from
1198 * the data space_info's bytes_may_use counter and incremented the
1199 * space_info's bytes_reserved counter by the same amount. We must make
1200 * sure extent_clear_unlock_delalloc() does not try to decrement again
1201 * the data space_info's bytes_may_use counter, therefore we do not pass
1202 * it the flag EXTENT_CLEAR_DATA_RESV.
1203 */
1204 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001205 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001206 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001207 locked_page,
1208 clear_bits,
1209 page_ops);
1210 start += cur_alloc_size;
1211 if (start >= end)
1212 goto out;
1213 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001214 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001215 clear_bits | EXTENT_CLEAR_DATA_RESV,
1216 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001217 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001218}
Chris Masonc8b97812008-10-29 14:49:59 -04001219
Chris Mason771ed682008-11-06 22:02:51 -05001220/*
1221 * work queue call back to started compression on a file and pages
1222 */
1223static noinline void async_cow_start(struct btrfs_work *work)
1224{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001225 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001226 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001227
Nikolay Borisovb5326272019-03-12 17:20:25 +02001228 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001229
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001230 compressed_extents = compress_file_range(async_chunk);
1231 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001232 btrfs_add_delayed_iput(async_chunk->inode);
1233 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001234 }
Chris Mason771ed682008-11-06 22:02:51 -05001235}
1236
1237/*
1238 * work queue call back to submit previously compressed pages
1239 */
1240static noinline void async_cow_submit(struct btrfs_work *work)
1241{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001242 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1243 work);
1244 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001245 unsigned long nr_pages;
1246
Nikolay Borisovb5326272019-03-12 17:20:25 +02001247 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001248 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001249
David Sterba093258e2018-02-26 16:15:17 +01001250 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001251 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001252 5 * SZ_1M)
1253 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001254
Nikolay Borisov4546d172019-01-03 10:50:03 +02001255 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001256 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001257 * in which case we don't have anything to submit, yet we need to
1258 * always adjust ->async_delalloc_pages as its paired with the init
1259 * happening in cow_file_range_async
1260 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001261 if (async_chunk->inode)
1262 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001263}
Chris Masonc8b97812008-10-29 14:49:59 -04001264
Chris Mason771ed682008-11-06 22:02:51 -05001265static noinline void async_cow_free(struct btrfs_work *work)
1266{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001267 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001268
Nikolay Borisovb5326272019-03-12 17:20:25 +02001269 async_chunk = container_of(work, struct async_chunk, work);
1270 if (async_chunk->inode)
1271 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001272 if (async_chunk->blkcg_css)
1273 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001274 /*
1275 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001276 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001277 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001278 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001279 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001280}
1281
Nikolay Borisov751b6432020-06-03 08:55:22 +03001282static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001283 struct writeback_control *wbc,
1284 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001285 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001286 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001287{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001288 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001289 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001290 struct async_cow *ctx;
1291 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001292 unsigned long nr_pages;
1293 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001294 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1295 int i;
1296 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001297 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001298 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001299
Nikolay Borisov751b6432020-06-03 08:55:22 +03001300 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001301
Nikolay Borisov751b6432020-06-03 08:55:22 +03001302 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001303 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1304 num_chunks = 1;
1305 should_compress = false;
1306 } else {
1307 should_compress = true;
1308 }
1309
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001310 nofs_flag = memalloc_nofs_save();
1311 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1312 memalloc_nofs_restore(nofs_flag);
1313
Nikolay Borisov97db1202019-03-12 17:20:24 +02001314 if (!ctx) {
1315 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1316 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1317 EXTENT_DO_ACCOUNTING;
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001318 unsigned long page_ops = PAGE_UNLOCK | PAGE_START_WRITEBACK |
1319 PAGE_END_WRITEBACK | PAGE_SET_ERROR;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001320
Nikolay Borisov751b6432020-06-03 08:55:22 +03001321 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1322 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001323 return -ENOMEM;
1324 }
1325
1326 async_chunk = ctx->chunks;
1327 atomic_set(&ctx->num_chunks, num_chunks);
1328
1329 for (i = 0; i < num_chunks; i++) {
1330 if (should_compress)
1331 cur_end = min(end, start + SZ_512K - 1);
1332 else
1333 cur_end = end;
1334
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001335 /*
1336 * igrab is called higher up in the call chain, take only the
1337 * lightweight reference for the callback lifetime
1338 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001339 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001340 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001341 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001342 async_chunk[i].start = start;
1343 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001344 async_chunk[i].write_flags = write_flags;
1345 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001346
Chris Mason1d53c9e2019-07-10 12:28:16 -07001347 /*
1348 * The locked_page comes all the way from writepage and its
1349 * the original page we were actually given. As we spread
1350 * this large delalloc region across multiple async_chunk
1351 * structs, only the first struct needs a pointer to locked_page
1352 *
1353 * This way we don't need racey decisions about who is supposed
1354 * to unlock it.
1355 */
1356 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001357 /*
1358 * Depending on the compressibility, the pages might or
1359 * might not go through async. We want all of them to
1360 * be accounted against wbc once. Let's do it here
1361 * before the paths diverge. wbc accounting is used
1362 * only for foreign writeback detection and doesn't
1363 * need full accuracy. Just account the whole thing
1364 * against the first page.
1365 */
1366 wbc_account_cgroup_owner(wbc, locked_page,
1367 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001368 async_chunk[i].locked_page = locked_page;
1369 locked_page = NULL;
1370 } else {
1371 async_chunk[i].locked_page = NULL;
1372 }
1373
Chris Masonec39f762019-07-10 12:28:17 -07001374 if (blkcg_css != blkcg_root_css) {
1375 css_get(blkcg_css);
1376 async_chunk[i].blkcg_css = blkcg_css;
1377 } else {
1378 async_chunk[i].blkcg_css = NULL;
1379 }
1380
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001381 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1382 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001383
Nikolay Borisov97db1202019-03-12 17:20:24 +02001384 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001385 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001386
Nikolay Borisov97db1202019-03-12 17:20:24 +02001387 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001388
Chris Mason771ed682008-11-06 22:02:51 -05001389 *nr_written += nr_pages;
1390 start = cur_end + 1;
1391 }
1392 *page_started = 1;
1393 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001394}
1395
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001396static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001397 u64 bytenr, u64 num_bytes)
1398{
1399 int ret;
1400 struct btrfs_ordered_sum *sums;
1401 LIST_HEAD(list);
1402
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001403 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001404 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001405 if (ret == 0 && list_empty(&list))
1406 return 0;
1407
1408 while (!list_empty(&list)) {
1409 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1410 list_del(&sums->list);
1411 kfree(sums);
1412 }
Liu Bo58113752018-01-31 17:09:13 -07001413 if (ret < 0)
1414 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001415 return 1;
1416}
1417
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001418static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001419 const u64 start, const u64 end,
1420 int *page_started, unsigned long *nr_written)
1421{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001422 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1423 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001424 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001425 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001426 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001427 u64 range_start = start;
1428 u64 count;
1429
1430 /*
1431 * If EXTENT_NORESERVE is set it means that when the buffered write was
1432 * made we had not enough available data space and therefore we did not
1433 * reserve data space for it, since we though we could do NOCOW for the
1434 * respective file range (either there is prealloc extent or the inode
1435 * has the NOCOW bit set).
1436 *
1437 * However when we need to fallback to COW mode (because for example the
1438 * block group for the corresponding extent was turned to RO mode by a
1439 * scrub or relocation) we need to do the following:
1440 *
1441 * 1) We increment the bytes_may_use counter of the data space info.
1442 * If COW succeeds, it allocates a new data extent and after doing
1443 * that it decrements the space info's bytes_may_use counter and
1444 * increments its bytes_reserved counter by the same amount (we do
1445 * this at btrfs_add_reserved_bytes()). So we need to increment the
1446 * bytes_may_use counter to compensate (when space is reserved at
1447 * buffered write time, the bytes_may_use counter is incremented);
1448 *
1449 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1450 * that if the COW path fails for any reason, it decrements (through
1451 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1452 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001453 *
1454 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001455 * space cache inode or an inode of the data relocation tree, we must
1456 * also increment bytes_may_use of the data space_info for the same
1457 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001458 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001459 * group that contains that extent to RO mode and therefore force COW
1460 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001461 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001462 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001463 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001464 if (count > 0 || is_space_ino || is_reloc_ino) {
1465 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001466 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001467 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1468
Filipe Manana6bd335b2020-06-08 13:33:05 +01001469 if (is_space_ino || is_reloc_ino)
1470 bytes = range_bytes;
1471
Filipe Manana467dc472020-05-27 11:16:07 +01001472 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001473 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001474 spin_unlock(&sinfo->lock);
1475
Filipe Manana2166e5e2020-05-27 11:16:19 +01001476 if (count > 0)
1477 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1478 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001479 }
1480
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001481 return cow_file_range(inode, locked_page, start, end, page_started,
1482 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001483}
1484
Chris Masond352ac62008-09-29 15:18:18 -04001485/*
1486 * when nowcow writeback call back. This checks for snapshots or COW copies
1487 * of the extents that exist in the file, and COWs the file as required.
1488 *
1489 * If no cow copies or snapshots exist, we write directly to the existing
1490 * blocks on disk
1491 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001492static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001493 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001494 const u64 start, const u64 end,
1495 int *page_started, int force,
1496 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001497{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001498 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1499 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001500 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001501 u64 cow_start = (u64)-1;
1502 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001503 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001504 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001505 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1506 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001507 bool nocow = false;
1508 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001509
1510 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001511 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001512 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001513 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001514 EXTENT_DO_ACCOUNTING |
1515 EXTENT_DEFRAG, PAGE_UNLOCK |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001516 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001517 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001518 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001519 }
Li Zefan82d59022011-04-20 10:33:24 +08001520
Yan Zheng80ff3852008-10-30 14:20:02 -04001521 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001522 struct btrfs_key found_key;
1523 struct btrfs_file_extent_item *fi;
1524 struct extent_buffer *leaf;
1525 u64 extent_end;
1526 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001527 u64 num_bytes = 0;
1528 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001529 u64 ram_bytes;
1530 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001531
1532 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001533
Liu Boe4c3b2d2017-01-30 12:25:28 -08001534 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001535 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001536 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001537 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001538
1539 /*
1540 * If there is no extent for our range when doing the initial
1541 * search, then go back to the previous slot as it will be the
1542 * one containing the search offset
1543 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001544 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1545 leaf = path->nodes[0];
1546 btrfs_item_key_to_cpu(leaf, &found_key,
1547 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001548 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001549 found_key.type == BTRFS_EXTENT_DATA_KEY)
1550 path->slots[0]--;
1551 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001552 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001553next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001554 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001555 leaf = path->nodes[0];
1556 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1557 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001558 if (ret < 0) {
1559 if (cow_start != (u64)-1)
1560 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001561 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001562 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001563 if (ret > 0)
1564 break;
1565 leaf = path->nodes[0];
1566 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001567
Yan Zheng80ff3852008-10-30 14:20:02 -04001568 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001569
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001570 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001571 if (found_key.objectid > ino)
1572 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001573 /*
1574 * Keep searching until we find an EXTENT_ITEM or there are no
1575 * more extents for this inode
1576 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001577 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1578 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1579 path->slots[0]++;
1580 goto next_slot;
1581 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001582
1583 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001584 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001585 found_key.offset > end)
1586 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001587
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001588 /*
1589 * If the found extent starts after requested offset, then
1590 * adjust extent_end to be right before this extent begins
1591 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001592 if (found_key.offset > cur_offset) {
1593 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001594 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001595 goto out_check;
1596 }
Chris Masonc31f8832008-01-08 15:46:31 -05001597
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001598 /*
1599 * Found extent which begins before our range and potentially
1600 * intersect it
1601 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001602 fi = btrfs_item_ptr(leaf, path->slots[0],
1603 struct btrfs_file_extent_item);
1604 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001605
Josef Bacikcc95bef2013-04-04 14:31:27 -04001606 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001607 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1608 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001609 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001610 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001611 extent_end = found_key.offset +
1612 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001613 disk_num_bytes =
1614 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001615 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001616 * If the extent we got ends before our current offset,
1617 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001618 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001619 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001620 path->slots[0]++;
1621 goto next_slot;
1622 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001623 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001624 if (disk_bytenr == 0)
1625 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001626 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001627 if (btrfs_file_extent_compression(leaf, fi) ||
1628 btrfs_file_extent_encryption(leaf, fi) ||
1629 btrfs_file_extent_other_encoding(leaf, fi))
1630 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001631 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001632 * If extent is created before the last volume's snapshot
1633 * this implies the extent is shared, hence we can't do
1634 * nocow. This is the same check as in
1635 * btrfs_cross_ref_exist but without calling
1636 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001637 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001638 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001639 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001640 btrfs_root_last_snapshot(&root->root_item))
1641 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001642 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1643 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001644
1645 /*
1646 * The following checks can be expensive, as they need to
1647 * take other locks and do btree or rbtree searches, so
1648 * release the path to avoid blocking other tasks for too
1649 * long.
1650 */
1651 btrfs_release_path(path);
1652
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;
Filipe Mananac65ca982020-11-18 11:00:17 +00001728 if (!path->nodes[0])
1729 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001730 path->slots[0]++;
1731 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001732 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001733
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001734 /*
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 |
Qu Wenruo6869b0a2021-01-26 16:33:45 +08001838 PAGE_START_WRITEBACK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001839 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001840 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001841 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001842}
1843
Nikolay Borisov0c494222020-06-03 08:55:28 +03001844static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001845{
1846
Nikolay Borisov0c494222020-06-03 08:55:28 +03001847 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1848 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001849 return 0;
1850
1851 /*
1852 * @defrag_bytes is a hint value, no spinlock held here,
1853 * if is not zero, it means the file is defragging.
1854 * Force cow if given extent needs to be defragged.
1855 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001856 if (inode->defrag_bytes &&
1857 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001858 return 1;
1859
1860 return 0;
1861}
1862
Chris Masond352ac62008-09-29 15:18:18 -04001863/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001864 * Function to process delayed allocation (create CoW) for ranges which are
1865 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001866 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001867int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001868 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1869 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001870{
Chris Masonbe20aa92007-12-17 20:14:01 -05001871 int ret;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001872 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001873
Nikolay Borisov98456b92020-06-03 08:55:29 +03001874 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1875 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001876 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001877 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1878 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001879 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001880 } else if (!inode_can_compress(inode) ||
1881 !inode_need_compress(inode, start, end)) {
1882 ret = cow_file_range(inode, locked_page, start, end,
1883 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001884 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001885 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1886 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001887 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001888 }
Qu Wenruo524272602017-03-08 10:25:52 +08001889 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001890 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001891 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001892 return ret;
1893}
1894
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001895void btrfs_split_delalloc_extent(struct inode *inode,
1896 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001897{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001898 u64 size;
1899
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001900 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001901 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001902 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001903
Josef Bacikdcab6a32015-02-11 15:08:59 -05001904 size = orig->end - orig->start + 1;
1905 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001906 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001907 u64 new_size;
1908
1909 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001910 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001911 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001912 */
1913 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001914 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001915 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001916 num_extents += count_max_extents(new_size);
1917 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001918 return;
1919 }
1920
Josef Bacik9e0baf62011-07-15 15:16:44 +00001921 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001922 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001923 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001924}
1925
1926/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001927 * Handle merged delayed allocation extents so we can keep track of new extents
1928 * that are just merged onto old extents, such as when we are doing sequential
1929 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001930 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001931void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1932 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001933{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001934 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001935 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001936
Josef Bacik9ed74f22009-09-11 16:12:44 -04001937 /* not delalloc, ignore it */
1938 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001939 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001940
Josef Bacik8461a3d2015-03-13 15:12:08 -04001941 if (new->start > other->start)
1942 new_size = new->end - other->start + 1;
1943 else
1944 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001945
1946 /* we're not bigger than the max, unreserve the space and go */
1947 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1948 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001949 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001950 spin_unlock(&BTRFS_I(inode)->lock);
1951 return;
1952 }
1953
1954 /*
Josef Bacikba117212015-03-13 15:01:24 -04001955 * We have to add up either side to figure out how many extents were
1956 * accounted for before we merged into one big extent. If the number of
1957 * extents we accounted for is <= the amount we need for the new range
1958 * then we can return, otherwise drop. Think of it like this
1959 *
1960 * [ 4k][MAX_SIZE]
1961 *
1962 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1963 * need 2 outstanding extents, on one side we have 1 and the other side
1964 * we have 1 so they are == and we can return. But in this case
1965 *
1966 * [MAX_SIZE+4k][MAX_SIZE+4k]
1967 *
1968 * Each range on their own accounts for 2 extents, but merged together
1969 * they are only 3 extents worth of accounting, so we need to drop in
1970 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001971 */
Josef Bacikba117212015-03-13 15:01:24 -04001972 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001973 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001974 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001975 num_extents += count_max_extents(old_size);
1976 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001977 return;
1978
Josef Bacik9e0baf62011-07-15 15:16:44 +00001979 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001980 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001981 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001982}
1983
Miao Xieeb73c1b2013-05-15 07:48:22 +00001984static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1985 struct inode *inode)
1986{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001987 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1988
Miao Xieeb73c1b2013-05-15 07:48:22 +00001989 spin_lock(&root->delalloc_lock);
1990 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1991 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1992 &root->delalloc_inodes);
1993 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1994 &BTRFS_I(inode)->runtime_flags);
1995 root->nr_delalloc_inodes++;
1996 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001997 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00001998 BUG_ON(!list_empty(&root->delalloc_root));
1999 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002000 &fs_info->delalloc_roots);
2001 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002002 }
2003 }
2004 spin_unlock(&root->delalloc_lock);
2005}
2006
Nikolay Borisov2b877332018-04-27 12:21:51 +03002007
2008void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2009 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002010{
David Sterba3ffbd682018-06-29 10:56:42 +02002011 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002012
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002013 if (!list_empty(&inode->delalloc_inodes)) {
2014 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002015 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002016 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002017 root->nr_delalloc_inodes--;
2018 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002019 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002020 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002021 BUG_ON(list_empty(&root->delalloc_root));
2022 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002023 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002024 }
2025 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002026}
2027
2028static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2029 struct btrfs_inode *inode)
2030{
2031 spin_lock(&root->delalloc_lock);
2032 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002033 spin_unlock(&root->delalloc_lock);
2034}
2035
Chris Masond352ac62008-09-29 15:18:18 -04002036/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002037 * Properly track delayed allocation bytes in the inode and to maintain the
2038 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002039 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002040void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2041 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002042{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002043 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2044
Wang Shilong47059d92014-07-03 18:22:07 +08002045 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2046 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002047 /*
2048 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002049 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002050 * bit, which is only set or cleared with irqs on
2051 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002052 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002053 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002054 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002055 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002056 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002057
Josef Bacik8b62f872017-10-19 14:15:55 -04002058 spin_lock(&BTRFS_I(inode)->lock);
2059 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2060 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002061
Josef Bacik6a3891c2015-03-16 17:38:52 -04002062 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002063 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002064 return;
2065
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002066 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2067 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002068 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002069 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002070 if (*bits & EXTENT_DEFRAG)
2071 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002072 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002073 &BTRFS_I(inode)->runtime_flags))
2074 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002075 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002076 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002077
2078 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2079 (*bits & EXTENT_DELALLOC_NEW)) {
2080 spin_lock(&BTRFS_I(inode)->lock);
2081 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2082 state->start;
2083 spin_unlock(&BTRFS_I(inode)->lock);
2084 }
Chris Mason291d6732008-01-29 15:55:23 -05002085}
2086
Chris Masond352ac62008-09-29 15:18:18 -04002087/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002088 * Once a range is no longer delalloc this function ensures that proper
2089 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002090 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002091void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2092 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002093{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002094 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2095 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002096 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002097 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002098
Filipe Manana4a4b9642017-07-27 19:52:55 +01002099 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2100 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002101 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002102 spin_unlock(&inode->lock);
2103 }
Wang Shilong47059d92014-07-03 18:22:07 +08002104
Chris Mason75eff682008-12-15 15:54:40 -05002105 /*
2106 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002107 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002108 * bit, which is only set or cleared with irqs on
2109 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002110 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002111 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002112 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002113
Josef Bacik8b62f872017-10-19 14:15:55 -04002114 spin_lock(&inode->lock);
2115 btrfs_mod_outstanding_extents(inode, -num_extents);
2116 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002117
Josef Bacikb6d08f02013-09-27 14:57:43 -04002118 /*
2119 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002120 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002121 * error.
2122 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002123 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002124 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002125 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002126
Josef Bacik6a3891c2015-03-16 17:38:52 -04002127 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002128 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002129 return;
2130
Filipe Mananaa315e682017-03-06 23:04:20 +00002131 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2132 do_list && !(state->state & EXTENT_NORESERVE) &&
2133 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002134 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002135
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002136 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2137 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002138 spin_lock(&inode->lock);
2139 inode->delalloc_bytes -= len;
2140 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002141 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002142 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002143 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002144 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002145 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002146
2147 if ((state->state & EXTENT_DELALLOC_NEW) &&
2148 (*bits & EXTENT_DELALLOC_NEW)) {
2149 spin_lock(&inode->lock);
2150 ASSERT(inode->new_delalloc_bytes >= len);
2151 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002152 if (*bits & EXTENT_ADD_INODE_BYTES)
2153 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002154 spin_unlock(&inode->lock);
2155 }
Chris Mason291d6732008-01-29 15:55:23 -05002156}
2157
Chris Masond352ac62008-09-29 15:18:18 -04002158/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002159 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2160 * in a chunk's stripe. This function ensures that bios do not span a
2161 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002162 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002163 * @page - The page we are about to add to the bio
2164 * @size - size we want to add to the bio
2165 * @bio - bio we want to ensure is smaller than a stripe
2166 * @bio_flags - flags of the bio
2167 *
2168 * return 1 if page cannot be added to the bio
2169 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002170 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002171 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002172int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2173 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002174{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002175 struct inode *inode = page->mapping->host;
2176 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba1201b582020-11-26 15:41:27 +01002177 u64 logical = bio->bi_iter.bi_sector << 9;
Michal Rostecki42034312021-01-27 14:57:27 +01002178 struct extent_map *em;
Chris Mason239b14b2008-03-24 15:02:07 -04002179 u64 length = 0;
2180 u64 map_length;
Michal Rostecki42034312021-01-27 14:57:27 +01002181 int ret = 0;
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;
Michal Rostecki42034312021-01-27 14:57:27 +01002189 em = btrfs_get_chunk_map(fs_info, logical, map_length);
2190 if (IS_ERR(em))
2191 return PTR_ERR(em);
2192 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(bio), logical,
2193 map_length, &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002194 if (ret < 0)
Michal Rostecki42034312021-01-27 14:57:27 +01002195 goto out;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002196
2197 if (geom.len < length + size)
Michal Rostecki42034312021-01-27 14:57:27 +01002198 ret = 1;
2199out:
2200 free_extent_map(em);
2201 return ret;
Chris Mason239b14b2008-03-24 15:02:07 -04002202}
2203
Chris Masond352ac62008-09-29 15:18:18 -04002204/*
2205 * in order to insert checksums into the metadata in large chunks,
2206 * we wait until bio submission time. All the pages in the bio are
2207 * checksummed and sums are attached onto the ordered extent record.
2208 *
2209 * At IO completion time the cums attached on the ordered extent record
2210 * are inserted into the btree
2211 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002212static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002213 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002214{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002215 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002216}
Chris Masone0156402008-04-16 11:15:20 -04002217
Chris Mason4a69a412008-11-06 22:03:00 -05002218/*
Chris Masoncad321a2008-12-17 14:51:42 -05002219 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002220 * on write, or reading the csums from the tree before a read.
2221 *
2222 * Rules about async/sync submit,
2223 * a) read: sync submit
2224 *
2225 * b) write without checksum: sync submit
2226 *
2227 * c) write with checksum:
2228 * c-1) if bio is issued by fsync: sync submit
2229 * (sync_writers != 0)
2230 *
2231 * c-2) if root is reloc root: sync submit
2232 * (only in case of buffered IO)
2233 *
2234 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002235 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002236blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2237 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002238
Chris Mason44b8bd72008-04-16 11:14:51 -04002239{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002240 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002241 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302242 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002243 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002244 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002245 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002246
Josef Bacik42437a62020-10-16 11:29:18 -04002247 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2248 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002249
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002250 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302251 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002252
Mike Christie37226b22016-06-05 14:31:52 -05002253 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002254 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002255 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002256 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002257
Chris Masond20f7042008-12-08 16:58:54 -05002258 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002259 ret = btrfs_submit_compressed_read(inode, bio,
2260 mirror_num,
2261 bio_flags);
2262 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002263 } else {
2264 /*
2265 * Lookup bio sums does extra checks around whether we
2266 * need to csum or not, which is why we ignore skip_sum
2267 * here.
2268 */
Qu Wenruo62751932020-12-02 14:48:06 +08002269 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002270 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002271 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002272 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002273 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002274 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002275 /* csum items have already been cloned */
2276 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2277 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002278 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002279 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2280 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002281 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002282 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002283 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002284 if (ret)
2285 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002286 }
2287
Chris Mason0b86a832008-03-24 15:01:56 -04002288mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002289 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002290
2291out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002292 if (ret) {
2293 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002294 bio_endio(bio);
2295 }
Stefan Behrens61891922012-11-05 18:51:52 +01002296 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002297}
Chris Mason6885f302008-02-20 16:11:05 -05002298
Chris Masond352ac62008-09-29 15:18:18 -04002299/*
2300 * given a list of ordered sums record them in the inode. This happens
2301 * at IO completion time based on sums calculated at bio submission time.
2302 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002303static int add_pending_csums(struct btrfs_trans_handle *trans,
2304 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002305{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002306 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002307 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002308
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002309 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002310 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002311 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002312 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002313 if (ret)
2314 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002315 }
2316 return 0;
2317}
2318
Filipe Mananac3347302020-11-04 11:07:31 +00002319static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2320 const u64 start,
2321 const u64 len,
2322 struct extent_state **cached_state)
2323{
2324 u64 search_start = start;
2325 const u64 end = start + len - 1;
2326
2327 while (search_start < end) {
2328 const u64 search_len = end - search_start + 1;
2329 struct extent_map *em;
2330 u64 em_len;
2331 int ret = 0;
2332
2333 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2334 if (IS_ERR(em))
2335 return PTR_ERR(em);
2336
2337 if (em->block_start != EXTENT_MAP_HOLE)
2338 goto next;
2339
2340 em_len = em->len;
2341 if (em->start < search_start)
2342 em_len -= search_start - em->start;
2343 if (em_len > search_len)
2344 em_len = search_len;
2345
2346 ret = set_extent_bit(&inode->io_tree, search_start,
2347 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002348 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2349 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002350next:
2351 search_start = extent_map_end(em);
2352 free_extent_map(em);
2353 if (ret)
2354 return ret;
2355 }
2356 return 0;
2357}
2358
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002359int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002360 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002361 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002362{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002363 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002364
2365 if (start >= i_size_read(&inode->vfs_inode) &&
2366 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2367 /*
2368 * There can't be any extents following eof in this case so just
2369 * set the delalloc new bit for the range directly.
2370 */
2371 extra_bits |= EXTENT_DELALLOC_NEW;
2372 } else {
2373 int ret;
2374
2375 ret = btrfs_find_new_delalloc_bytes(inode, start,
2376 end + 1 - start,
2377 cached_state);
2378 if (ret)
2379 return ret;
2380 }
2381
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002382 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2383 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002384}
2385
Chris Masond352ac62008-09-29 15:18:18 -04002386/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002387struct btrfs_writepage_fixup {
2388 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002389 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002390 struct btrfs_work work;
2391};
2392
Christoph Hellwigb2950862008-12-02 09:54:17 -05002393static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002394{
2395 struct btrfs_writepage_fixup *fixup;
2396 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002397 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002398 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002399 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002400 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002401 u64 page_start;
2402 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002403 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002404 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002405
2406 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2407 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002408 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002409 page_start = page_offset(page);
2410 page_end = page_offset(page) + PAGE_SIZE - 1;
2411
2412 /*
2413 * This is similar to page_mkwrite, we need to reserve the space before
2414 * we take the page lock.
2415 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002416 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2417 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002418again:
Chris Mason247e7432008-07-17 12:53:51 -04002419 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002420
Chris Mason25f3c502020-01-21 11:51:42 -05002421 /*
2422 * Before we queued this fixup, we took a reference on the page.
2423 * page->mapping may go NULL, but it shouldn't be moved to a different
2424 * address space.
2425 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002426 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2427 /*
2428 * Unfortunately this is a little tricky, either
2429 *
2430 * 1) We got here and our page had already been dealt with and
2431 * we reserved our space, thus ret == 0, so we need to just
2432 * drop our space reservation and bail. This can happen the
2433 * first time we come into the fixup worker, or could happen
2434 * while waiting for the ordered extent.
2435 * 2) Our page was already dealt with, but we happened to get an
2436 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2437 * this case we obviously don't have anything to release, but
2438 * because the page was already dealt with we don't want to
2439 * mark the page with an error, so make sure we're resetting
2440 * ret to 0. This is why we have this check _before_ the ret
2441 * check, because we do not want to have a surprise ENOSPC
2442 * when the page was already properly dealt with.
2443 */
2444 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002445 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2446 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002447 page_start, PAGE_SIZE,
2448 true);
2449 }
2450 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002451 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002452 }
Chris Mason25f3c502020-01-21 11:51:42 -05002453
2454 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002455 * We can't mess with the page state unless it is locked, so now that
2456 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002457 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002458 if (ret)
2459 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002460
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002461 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002462
2463 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002464 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002465 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002466
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002467 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002468 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002469 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2470 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002471 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002472 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002473 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002474 goto again;
2475 }
Chris Mason247e7432008-07-17 12:53:51 -04002476
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002477 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002478 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002479 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002480 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002481
Chris Mason25f3c502020-01-21 11:51:42 -05002482 /*
2483 * Everything went as planned, we're now the owner of a dirty page with
2484 * delayed allocation bits set and space reserved for our COW
2485 * destination.
2486 *
2487 * The page was dirty when we started, nothing should have cleaned it.
2488 */
2489 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002490 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002491out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002492 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002493 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002494 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2495 PAGE_SIZE, true);
2496 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002497 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002498out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002499 if (ret) {
2500 /*
2501 * We hit ENOSPC or other errors. Update the mapping and page
2502 * to reflect the errors and clean the page.
2503 */
2504 mapping_set_error(page->mapping, ret);
2505 end_extent_writepage(page, ret, page_start, page_end);
2506 clear_page_dirty_for_io(page);
2507 SetPageError(page);
2508 }
2509 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002510 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002511 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002512 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002513 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002514 /*
2515 * As a precaution, do a delayed iput in case it would be the last iput
2516 * that could need flushing space. Recursing back to fixup worker would
2517 * deadlock.
2518 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002519 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002520}
2521
2522/*
2523 * There are a few paths in the higher layers of the kernel that directly
2524 * set the page dirty bit without asking the filesystem if it is a
2525 * good idea. This causes problems because we want to make sure COW
2526 * properly happens and the data=ordered rules are followed.
2527 *
Chris Masonc8b97812008-10-29 14:49:59 -04002528 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002529 * hasn't been properly setup for IO. We kick off an async process
2530 * to fix it up. The async helper will wait for ordered extents, set
2531 * the delalloc bit and make it safe to write the page.
2532 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002533int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002534{
2535 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002536 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002537 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002538
Chris Mason8b62b722009-09-02 16:53:46 -04002539 /* this page is properly in the ordered list */
2540 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002541 return 0;
2542
Chris Mason25f3c502020-01-21 11:51:42 -05002543 /*
2544 * PageChecked is set below when we create a fixup worker for this page,
2545 * don't try to create another one if we're already PageChecked()
2546 *
2547 * The extent_io writepage code will redirty the page if we send back
2548 * EAGAIN.
2549 */
Chris Mason247e7432008-07-17 12:53:51 -04002550 if (PageChecked(page))
2551 return -EAGAIN;
2552
2553 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2554 if (!fixup)
2555 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002556
Josef Bacikf4b13632020-01-21 14:34:52 -05002557 /*
2558 * We are already holding a reference to this inode from
2559 * write_cache_pages. We need to hold it because the space reservation
2560 * takes place outside of the page lock, and we can't trust
2561 * page->mapping outside of the page lock.
2562 */
2563 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002564 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002565 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002566 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002567 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002568 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002569 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002570
2571 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002572}
2573
Yan Zhengd899e052008-10-30 14:25:28 -04002574static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002575 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002576 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002577 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002578 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002579{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002580 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002581 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002582 struct btrfs_path *path;
2583 struct extent_buffer *leaf;
2584 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002585 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2586 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2587 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2588 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002589 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002590 int ret;
2591
2592 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002593 if (!path)
2594 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002595
Chris Masona1ed8352009-09-11 12:27:37 -04002596 /*
2597 * we may be replacing one extent in the tree with another.
2598 * The new extent is pinned in the extent map, and we don't want
2599 * to drop it from the cache until it is completely in the btree.
2600 *
2601 * So, tell btrfs_drop_extents to leave this extent in the cache.
2602 * the caller is expected to unpin it and allow it to be merged
2603 * with the others.
2604 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002605 drop_args.path = path;
2606 drop_args.start = file_pos;
2607 drop_args.end = file_pos + num_bytes;
2608 drop_args.replace_extent = true;
2609 drop_args.extent_item_size = sizeof(*stack_fi);
2610 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002611 if (ret)
2612 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002613
Filipe Manana5893dfb2020-11-04 11:07:32 +00002614 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002615 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002616 ins.offset = file_pos;
2617 ins.type = BTRFS_EXTENT_DATA_KEY;
2618
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002619 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002620 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002621 if (ret)
2622 goto out;
2623 }
Yan Zhengd899e052008-10-30 14:25:28 -04002624 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002625 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2626 write_extent_buffer(leaf, stack_fi,
2627 btrfs_item_ptr_offset(leaf, path->slots[0]),
2628 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002629
Yan Zhengd899e052008-10-30 14:25:28 -04002630 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002631 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002632
Filipe Manana2766ff62020-11-04 11:07:34 +00002633 /*
2634 * If we dropped an inline extent here, we know the range where it is
2635 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2636 * number of bytes only for that range contaning the inline extent.
2637 * The remaining of the range will be processed when clearning the
2638 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2639 */
2640 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2641 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2642
2643 inline_size = drop_args.bytes_found - inline_size;
2644 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2645 drop_args.bytes_found -= inline_size;
2646 num_bytes -= sectorsize;
2647 }
2648
2649 if (update_inode_bytes)
2650 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002651
2652 ins.objectid = disk_bytenr;
2653 ins.offset = disk_num_bytes;
2654 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002655
Nikolay Borisovc553f942020-06-03 08:55:19 +03002656 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002657 if (ret)
2658 goto out;
2659
Nikolay Borisovc553f942020-06-03 08:55:19 +03002660 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002661 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002662out:
Yan Zhengd899e052008-10-30 14:25:28 -04002663 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002664
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002665 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002666}
2667
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002668static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002669 u64 start, u64 len)
2670{
David Sterba32da53862019-10-29 19:20:18 +01002671 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002672
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002673 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002674 ASSERT(cache);
2675
2676 spin_lock(&cache->lock);
2677 cache->delalloc_bytes -= len;
2678 spin_unlock(&cache->lock);
2679
2680 btrfs_put_block_group(cache);
2681}
2682
Qu Wenruo203f44c52020-06-10 09:04:40 +08002683static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002684 struct btrfs_ordered_extent *oe)
2685{
2686 struct btrfs_file_extent_item stack_fi;
2687 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002688 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002689
2690 memset(&stack_fi, 0, sizeof(stack_fi));
2691 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2692 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2693 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2694 oe->disk_num_bytes);
2695 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2696 logical_len = oe->truncated_len;
2697 else
2698 logical_len = oe->num_bytes;
2699 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2700 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2701 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2702 /* Encryption and other encoding is reserved and all 0 */
2703
Filipe Manana2766ff62020-11-04 11:07:34 +00002704 /*
2705 * For delalloc, when completing an ordered extent we update the inode's
2706 * bytes when clearing the range in the inode's io tree, so pass false
2707 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2708 * except if the ordered extent was truncated.
2709 */
2710 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2711 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2712
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002713 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2714 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002715 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002716}
2717
2718/*
2719 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002720 * an ordered extent if the range of bytes in the file it covers are
2721 * fully written.
2722 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002723static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002724{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002725 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2726 struct btrfs_root *root = inode->root;
2727 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002728 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002729 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002730 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002731 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002732 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002733 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002734 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002735 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002736 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002737 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002738 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002739
Omar Sandovalbffe6332019-12-02 17:34:19 -08002740 start = ordered_extent->file_offset;
2741 end = start + ordered_extent->num_bytes - 1;
2742
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002743 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2744 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2745 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00002746 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002747
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002748 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002749
Josef Bacik5fd02042012-05-02 14:00:54 -04002750 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2751 ret = -EIO;
2752 goto out;
2753 }
2754
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002755 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08002756
Josef Bacik77cef2e2013-08-29 13:57:21 -04002757 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2758 truncated = true;
2759 logical_len = ordered_extent->truncated_len;
2760 /* Truncated the entire extent, don't bother adding */
2761 if (!logical_len)
2762 goto out;
2763 }
2764
Yan, Zhengc2167752009-11-12 09:34:21 +00002765 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002766 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002767
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002768 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002769 if (freespace_inode)
2770 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002771 else
2772 trans = btrfs_join_transaction(root);
2773 if (IS_ERR(trans)) {
2774 ret = PTR_ERR(trans);
2775 trans = NULL;
2776 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002777 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002778 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02002779 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002780 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002781 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002782 goto out;
2783 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002784
Filipe Manana2766ff62020-11-04 11:07:34 +00002785 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002786 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002787
Nikolay Borisov8d510122019-10-08 20:43:06 +03002788 if (freespace_inode)
2789 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002790 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002791 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002792 if (IS_ERR(trans)) {
2793 ret = PTR_ERR(trans);
2794 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002795 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002796 }
Chris Masona79b7d42014-05-22 16:18:52 -07002797
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002798 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002799
Chris Masonc8b97812008-10-29 14:49:59 -04002800 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002801 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002802 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002803 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002804 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002805 ordered_extent->file_offset,
2806 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002807 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002808 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002809 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002810 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002811 if (!ret) {
2812 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002813 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002814 ordered_extent->disk_bytenr,
2815 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002816 }
Yan Zhengd899e052008-10-30 14:25:28 -04002817 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002818 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002819 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002820 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002821 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002822 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002823 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002824
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002825 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002826 if (ret) {
2827 btrfs_abort_transaction(trans, ret);
2828 goto out;
2829 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002830
Filipe Manana2766ff62020-11-04 11:07:34 +00002831 /*
2832 * If this is a new delalloc range, clear its new delalloc flag to
2833 * update the inode's number of bytes. This needs to be done first
2834 * before updating the inode item.
2835 */
2836 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
2837 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002838 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00002839 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
2840 0, 0, &cached_state);
2841
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002842 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02002843 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002844 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002845 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002846 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002847 }
2848 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002849out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002850 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002851 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002852 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002853
Miao Xiea698d0752012-09-20 01:51:59 -06002854 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002855 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002856
Josef Bacik77cef2e2013-08-29 13:57:21 -04002857 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002858 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002859
2860 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002861 unwritten_start += logical_len;
2862 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002863
2864 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002865 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002866
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002867 /*
2868 * If the ordered extent had an IOERR or something else went
2869 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002870 * back to the allocator. We only free the extent in the
2871 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002872 *
2873 * If we made it past insert_reserved_file_extent before we
2874 * errored out then we don't need to do this as the accounting
2875 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002876 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002877 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002878 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002879 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002880 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2881 /*
2882 * Discard the range before returning it back to the
2883 * free space pool
2884 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002885 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002886 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002887 ordered_extent->disk_bytenr,
2888 ordered_extent->disk_num_bytes,
2889 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002890 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002891 ordered_extent->disk_bytenr,
2892 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002893 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002894 }
2895
Josef Bacik5fd02042012-05-02 14:00:54 -04002896 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002897 * This needs to be done to make sure anybody waiting knows we are done
2898 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002899 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002900 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04002901
Chris Masone6dcd2d2008-07-17 12:53:50 -04002902 /* once for us */
2903 btrfs_put_ordered_extent(ordered_extent);
2904 /* once for the tree */
2905 btrfs_put_ordered_extent(ordered_extent);
2906
Josef Bacik5fd02042012-05-02 14:00:54 -04002907 return ret;
2908}
2909
2910static void finish_ordered_fn(struct btrfs_work *work)
2911{
2912 struct btrfs_ordered_extent *ordered_extent;
2913 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2914 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002915}
2916
Nikolay Borisovc6297322018-11-08 10:18:08 +02002917void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2918 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002919{
Nikolay Borisov3347c482020-08-31 14:42:44 +03002920 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2921 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04002922 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002923 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002924
liubo1abe9b82011-03-24 11:18:59 +00002925 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2926
Chris Mason8b62b722009-09-02 16:53:46 -04002927 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03002928 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2929 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002930 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002931
Nikolay Borisov3347c482020-08-31 14:42:44 +03002932 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002933 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002934 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002935 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002936
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002937 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002938 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002939}
2940
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002941/*
2942 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002943 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002944 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002945 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002946 * @page: page where is the data to be verified
2947 * @pgoff: offset inside the page
2948 *
2949 * The length of such check is always one sector size.
2950 */
Omar Sandoval47df7762020-04-16 14:46:17 -07002951static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Qu Wenruof44cf412020-12-02 14:47:59 +08002952 u32 bio_offset, struct page *page, u32 pgoff)
Miao Xiedc380ae2014-09-12 18:43:55 +08002953{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002954 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2955 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002956 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002957 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02002958 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002959 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002960 u8 *csum_expected;
2961 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002962
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002963 ASSERT(pgoff + len <= PAGE_SIZE);
2964
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002965 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
2966 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002967
2968 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002969 shash->tfm = fs_info->csum_shash;
2970
Eric Biggersfd080012020-04-30 23:51:59 -07002971 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002972
2973 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002974 goto zeroit;
2975
2976 kunmap_atomic(kaddr);
2977 return 0;
2978zeroit:
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002979 btrfs_print_data_csum_error(BTRFS_I(inode), page_offset(page) + pgoff,
2980 csum, csum_expected, io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03002981 if (io_bio->device)
2982 btrfs_dev_stat_inc_and_print(io_bio->device,
2983 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08002984 memset(kaddr + pgoff, 1, len);
2985 flush_dcache_page(page);
2986 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002987 return -EIO;
2988}
2989
Chris Masond352ac62008-09-29 15:18:18 -04002990/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002991 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002992 * if there's a match, we allow the bio to finish. If not, the code in
2993 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002994 *
2995 * @bio_offset: offset to the beginning of the bio (in bytes)
2996 * @start: file offset of the range start
2997 * @end: file offset of the range end (inclusive)
2998 * @mirror: mirror number
Chris Masond352ac62008-09-29 15:18:18 -04002999 */
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003000int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
Nikolay Borisov9a446d62020-09-18 16:34:33 +03003001 struct page *page, u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003002{
Chris Mason07157aa2007-08-30 08:50:51 -04003003 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003004 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003005 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003006 const u32 sectorsize = root->fs_info->sectorsize;
3007 u32 pg_off;
Chris Masond1310b22008-01-24 16:13:08 -05003008
Chris Masond20f7042008-12-08 16:58:54 -05003009 if (PageChecked(page)) {
3010 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003011 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003012 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003013
3014 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003015 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003016
Josef Bacik42437a62020-10-16 11:29:18 -04003017 if (!root->fs_info->csum_root)
3018 return 0;
3019
Yan Zheng17d217f2008-12-12 10:03:38 -05003020 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003021 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003022 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003023 return 0;
3024 }
3025
Qu Wenruof44cf412020-12-02 14:47:59 +08003026 ASSERT(page_offset(page) <= start &&
3027 end <= page_offset(page) + PAGE_SIZE - 1);
3028 for (pg_off = offset_in_page(start);
3029 pg_off < offset_in_page(end);
3030 pg_off += sectorsize, bio_offset += sectorsize) {
3031 int ret;
3032
3033 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off);
3034 if (ret < 0)
3035 return -EIO;
3036 }
3037 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -04003038}
Chris Masonb888db22007-08-27 16:49:44 -04003039
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003040/*
3041 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3042 *
3043 * @inode: The inode we want to perform iput on
3044 *
3045 * This function uses the generic vfs_inode::i_count to track whether we should
3046 * just decrement it (in case it's > 1) or if this is the last iput then link
3047 * the inode to the delayed iput machinery. Delayed iputs are processed at
3048 * transaction commit time/superblock commit/cleaner kthread.
3049 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003050void btrfs_add_delayed_iput(struct inode *inode)
3051{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003052 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003053 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003054
3055 if (atomic_add_unless(&inode->i_count, -1, 1))
3056 return;
3057
Josef Bacik034f7842018-12-03 11:06:52 -05003058 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003059 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003060 ASSERT(list_empty(&binode->delayed_iput));
3061 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003062 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003063 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3064 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003065}
3066
Josef Bacik63611e72019-06-18 10:59:18 -04003067static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3068 struct btrfs_inode *inode)
3069{
3070 list_del_init(&inode->delayed_iput);
3071 spin_unlock(&fs_info->delayed_iput_lock);
3072 iput(&inode->vfs_inode);
3073 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3074 wake_up(&fs_info->delayed_iputs_wait);
3075 spin_lock(&fs_info->delayed_iput_lock);
3076}
3077
3078static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3079 struct btrfs_inode *inode)
3080{
3081 if (!list_empty(&inode->delayed_iput)) {
3082 spin_lock(&fs_info->delayed_iput_lock);
3083 if (!list_empty(&inode->delayed_iput))
3084 run_delayed_iput_locked(fs_info, inode);
3085 spin_unlock(&fs_info->delayed_iput_lock);
3086 }
3087}
3088
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003089void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003090{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003091
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003092 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003093 while (!list_empty(&fs_info->delayed_iputs)) {
3094 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003095
David Sterba8089fe62015-11-19 14:15:51 +01003096 inode = list_first_entry(&fs_info->delayed_iputs,
3097 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003098 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003099 }
David Sterba8089fe62015-11-19 14:15:51 +01003100 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003101}
3102
Josef Bacik034f7842018-12-03 11:06:52 -05003103/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003104 * Wait for flushing all delayed iputs
3105 *
3106 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003107 *
3108 * This will wait on any delayed iputs that are currently running with KILLABLE
3109 * set. Once they are all done running we will return, unless we are killed in
3110 * which case we return EINTR. This helps in user operations like fallocate etc
3111 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003112 *
3113 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003114 */
3115int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3116{
3117 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3118 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3119 if (ret)
3120 return -EINTR;
3121 return 0;
3122}
3123
Yan, Zhengd68fc572010-05-16 10:49:58 -04003124/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003125 * This creates an orphan entry for the given inode in case something goes wrong
3126 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003127 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003128int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003129 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003130{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003131 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003132
Omar Sandoval27919062018-05-11 13:13:37 -07003133 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3134 if (ret && ret != -EEXIST) {
3135 btrfs_abort_transaction(trans, ret);
3136 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003137 }
3138
Yan, Zhengd68fc572010-05-16 10:49:58 -04003139 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003140}
3141
3142/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003143 * We have done the delete so we can go ahead and remove the orphan item for
3144 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003145 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003146static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003147 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003148{
Omar Sandoval27919062018-05-11 13:13:37 -07003149 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003150}
3151
3152/*
3153 * this cleans up any orphans that may be left on the list from the last use
3154 * of this root.
3155 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003156int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003157{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003158 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003159 struct btrfs_path *path;
3160 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003161 struct btrfs_key key, found_key;
3162 struct btrfs_trans_handle *trans;
3163 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003164 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003165 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003166
Yan, Zhengd68fc572010-05-16 10:49:58 -04003167 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003168 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003169
3170 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003171 if (!path) {
3172 ret = -ENOMEM;
3173 goto out;
3174 }
David Sterbae4058b52015-11-27 16:31:35 +01003175 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003176
3177 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003178 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003179 key.offset = (u64)-1;
3180
Josef Bacik7b128762008-07-24 12:17:14 -04003181 while (1) {
3182 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003183 if (ret < 0)
3184 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003185
3186 /*
3187 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003188 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003189 * find the key and see if we have stuff that matches
3190 */
3191 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003192 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003193 if (path->slots[0] == 0)
3194 break;
3195 path->slots[0]--;
3196 }
3197
3198 /* pull out the item */
3199 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003200 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3201
3202 /* make sure the item matches what we want */
3203 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3204 break;
David Sterba962a2982014-06-04 18:41:45 +02003205 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003206 break;
3207
3208 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003209 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003210
3211 /*
3212 * this is where we are basically btrfs_lookup, without the
3213 * crossing root thing. we store the inode number in the
3214 * offset of the orphan item.
3215 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003216
3217 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003218 btrfs_err(fs_info,
3219 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003220 ret = -EINVAL;
3221 goto out;
3222 }
3223
3224 last_objectid = found_key.offset;
3225
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003226 found_key.objectid = found_key.offset;
3227 found_key.type = BTRFS_INODE_ITEM_KEY;
3228 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003229 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303230 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003231 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003232 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003233
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003234 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003235 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003236 int is_dead_root = 0;
3237
3238 /*
3239 * this is an orphan in the tree root. Currently these
3240 * could come from 2 sources:
3241 * a) a snapshot deletion in progress
3242 * b) a free space cache inode
3243 * We need to distinguish those two, as the snapshot
3244 * orphan must not get deleted.
3245 * find_dead_roots already ran before us, so if this
3246 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003247 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003248 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003249
3250 spin_lock(&fs_info->fs_roots_radix_lock);
3251 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3252 (unsigned long)found_key.objectid);
3253 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3254 is_dead_root = 1;
3255 spin_unlock(&fs_info->fs_roots_radix_lock);
3256
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003257 if (is_dead_root) {
3258 /* prevent this orphan from being found again */
3259 key.offset = found_key.objectid - 1;
3260 continue;
3261 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003262
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003263 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003264
Josef Bacika8c9e572011-09-21 16:55:59 -04003265 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003266 * If we have an inode with links, there are a couple of
3267 * possibilities. Old kernels (before v3.12) used to create an
3268 * orphan item for truncate indicating that there were possibly
3269 * extent items past i_size that needed to be deleted. In v3.12,
3270 * truncate was changed to update i_size in sync with the extent
3271 * items, but the (useless) orphan item was still created. Since
3272 * v4.18, we don't create the orphan item for truncate at all.
3273 *
3274 * So, this item could mean that we need to do a truncate, but
3275 * only if this filesystem was last used on a pre-v3.12 kernel
3276 * and was not cleanly unmounted. The odds of that are quite
3277 * slim, and it's a pain to do the truncate now, so just delete
3278 * the orphan item.
3279 *
3280 * It's also possible that this orphan item was supposed to be
3281 * deleted but wasn't. The inode number may have been reused,
3282 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003283 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003284 if (ret == -ENOENT || inode->i_nlink) {
3285 if (!ret)
3286 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003287 trans = btrfs_start_transaction(root, 1);
3288 if (IS_ERR(trans)) {
3289 ret = PTR_ERR(trans);
3290 goto out;
3291 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003292 btrfs_debug(fs_info, "auto deleting %Lu",
3293 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003294 ret = btrfs_del_orphan_item(trans, root,
3295 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003296 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003297 if (ret)
3298 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003299 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003300 }
Josef Bacik7b128762008-07-24 12:17:14 -04003301
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003302 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003303
3304 /* this will do delete_inode and everything for us */
3305 iput(inode);
3306 }
Miao Xie3254c872011-11-10 20:45:05 -05003307 /* release the path since we're done with it */
3308 btrfs_release_path(path);
3309
Yan, Zhengd68fc572010-05-16 10:49:58 -04003310 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3311
Omar Sandovala575cee2018-05-11 13:13:38 -07003312 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003313 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003314 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003315 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003316 }
Josef Bacik7b128762008-07-24 12:17:14 -04003317
3318 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003319 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003320
3321out:
3322 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003323 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003324 btrfs_free_path(path);
3325 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003326}
3327
Chris Masond352ac62008-09-29 15:18:18 -04003328/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003329 * very simple check to peek ahead in the leaf looking for xattrs. If we
3330 * don't find any xattrs, we know there can't be any acls.
3331 *
3332 * slot is the slot the inode is in, objectid is the objectid of the inode
3333 */
3334static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003335 int slot, u64 objectid,
3336 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003337{
3338 u32 nritems = btrfs_header_nritems(leaf);
3339 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003340 static u64 xattr_access = 0;
3341 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003342 int scanned = 0;
3343
Josef Bacikf23b5a52013-06-19 10:16:26 -04003344 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003345 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3346 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3347 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3348 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003349 }
3350
Chris Mason46a53cc2009-04-27 11:47:50 -04003351 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003352 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003353 while (slot < nritems) {
3354 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3355
3356 /* we found a different objectid, there must not be acls */
3357 if (found_key.objectid != objectid)
3358 return 0;
3359
3360 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003361 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003362 if (*first_xattr_slot == -1)
3363 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003364 if (found_key.offset == xattr_access ||
3365 found_key.offset == xattr_default)
3366 return 1;
3367 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003368
3369 /*
3370 * we found a key greater than an xattr key, there can't
3371 * be any acls later on
3372 */
3373 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3374 return 0;
3375
3376 slot++;
3377 scanned++;
3378
3379 /*
3380 * it goes inode, inode backrefs, xattrs, extents,
3381 * so if there are a ton of hard links to an inode there can
3382 * be a lot of backrefs. Don't waste time searching too hard,
3383 * this is just an optimization
3384 */
3385 if (scanned >= 8)
3386 break;
3387 }
3388 /* we hit the end of the leaf before we found an xattr or
3389 * something larger than an xattr. We have to assume the inode
3390 * has acls
3391 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003392 if (*first_xattr_slot == -1)
3393 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003394 return 1;
3395}
3396
3397/*
Chris Masond352ac62008-09-29 15:18:18 -04003398 * read an inode from the btree into the in-memory inode
3399 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003400static int btrfs_read_locked_inode(struct inode *inode,
3401 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003402{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003403 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003404 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003405 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003406 struct btrfs_inode_item *inode_item;
3407 struct btrfs_root *root = BTRFS_I(inode)->root;
3408 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003409 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003410 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003411 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003412 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003413 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003414 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003415
3416 ret = btrfs_fill_inode(inode, &rdev);
3417 if (!ret)
3418 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003419
Filipe Manana4222ea72018-10-24 10:13:03 +01003420 if (!path) {
3421 path = btrfs_alloc_path();
3422 if (!path)
3423 return -ENOMEM;
3424 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003425
Chris Mason39279cc2007-06-12 06:35:45 -04003426 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003427
Chris Mason39279cc2007-06-12 06:35:45 -04003428 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003429 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003430 if (path != in_path)
3431 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003432 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003433 }
Chris Mason39279cc2007-06-12 06:35:45 -04003434
Chris Mason5f39d392007-10-15 16:14:19 -04003435 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003436
3437 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003438 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003439
Chris Mason5f39d392007-10-15 16:14:19 -04003440 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3441 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003442 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003443 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003444 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3445 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003446 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003447 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3448 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003449
David Sterbaa937b972014-12-12 17:39:12 +01003450 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3451 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003452
David Sterbaa937b972014-12-12 17:39:12 +01003453 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3454 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003455
David Sterbaa937b972014-12-12 17:39:12 +01003456 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3457 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003458
chandan r9cc97d62012-07-04 12:48:07 +05303459 BTRFS_I(inode)->i_otime.tv_sec =
3460 btrfs_timespec_sec(leaf, &inode_item->otime);
3461 BTRFS_I(inode)->i_otime.tv_nsec =
3462 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003463
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003464 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003465 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003466 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3467
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003468 inode_set_iversion_queried(inode,
3469 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003470 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003471 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003472 rdev = btrfs_inode_rdev(leaf, inode_item);
3473
Josef Bacikaec74772008-07-24 12:12:38 -04003474 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003475 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003476
3477cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003478 /*
3479 * If we were modified in the current generation and evicted from memory
3480 * and then re-read we need to do a full sync since we don't have any
3481 * idea about which extents were modified before we were evicted from
3482 * cache.
3483 *
3484 * This is required for both inode re-read from disk and delayed inode
3485 * in delayed_nodes_tree.
3486 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003487 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003488 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3489 &BTRFS_I(inode)->runtime_flags);
3490
Filipe Mananabde6c242015-07-24 00:00:19 +01003491 /*
3492 * We don't persist the id of the transaction where an unlink operation
3493 * against the inode was last made. So here we assume the inode might
3494 * have been evicted, and therefore the exact value of last_unlink_trans
3495 * lost, and set it to last_trans to avoid metadata inconsistencies
3496 * between the inode and its parent if the inode is fsync'ed and the log
3497 * replayed. For example, in the scenario:
3498 *
3499 * touch mydir/foo
3500 * ln mydir/foo mydir/bar
3501 * sync
3502 * unlink mydir/bar
3503 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3504 * xfs_io -c fsync mydir/foo
3505 * <power failure>
3506 * mount fs, triggers fsync log replay
3507 *
3508 * We must make sure that when we fsync our inode foo we also log its
3509 * parent inode, otherwise after log replay the parent still has the
3510 * dentry with the "bar" name but our inode foo has a link count of 1
3511 * and doesn't have an inode ref with the name "bar" anymore.
3512 *
3513 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003514 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003515 * transaction commits on fsync if our inode is a directory, or if our
3516 * inode is not a directory, logging its parent unnecessarily.
3517 */
3518 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3519
Filipe Manana3ebac172020-07-15 12:30:43 +01003520 /*
3521 * Same logic as for last_unlink_trans. We don't persist the generation
3522 * of the last transaction where this inode was used for a reflink
3523 * operation, so after eviction and reloading the inode we must be
3524 * pessimistic and assume the last transaction that modified the inode.
3525 */
3526 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3527
Miao Xie67de1172013-12-26 13:07:06 +08003528 path->slots[0]++;
3529 if (inode->i_nlink != 1 ||
3530 path->slots[0] >= btrfs_header_nritems(leaf))
3531 goto cache_acl;
3532
3533 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003534 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003535 goto cache_acl;
3536
3537 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3538 if (location.type == BTRFS_INODE_REF_KEY) {
3539 struct btrfs_inode_ref *ref;
3540
3541 ref = (struct btrfs_inode_ref *)ptr;
3542 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3543 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3544 struct btrfs_inode_extref *extref;
3545
3546 extref = (struct btrfs_inode_extref *)ptr;
3547 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3548 extref);
3549 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003550cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003551 /*
3552 * try to precache a NULL acl entry for files that don't have
3553 * any xattrs or acls
3554 */
Li Zefan33345d012011-04-20 10:31:50 +08003555 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003556 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003557 if (first_xattr_slot != -1) {
3558 path->slots[0] = first_xattr_slot;
3559 ret = btrfs_load_inode_props(inode, path);
3560 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003561 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003562 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003563 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003564 root->root_key.objectid, ret);
3565 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003566 if (path != in_path)
3567 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003568
Al Viro72c04902009-06-24 16:58:48 -04003569 if (!maybe_acls)
3570 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003571
Chris Mason39279cc2007-06-12 06:35:45 -04003572 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003573 case S_IFREG:
3574 inode->i_mapping->a_ops = &btrfs_aops;
3575 inode->i_fop = &btrfs_file_operations;
3576 inode->i_op = &btrfs_file_inode_operations;
3577 break;
3578 case S_IFDIR:
3579 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003580 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003581 break;
3582 case S_IFLNK:
3583 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003584 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003585 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003586 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003587 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003588 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003589 init_special_inode(inode, inode->i_mode, rdev);
3590 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003591 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003592
David Sterba7b6a2212018-03-26 18:40:21 +02003593 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003594 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003595}
3596
Chris Masond352ac62008-09-29 15:18:18 -04003597/*
3598 * given a leaf and an inode, copy the inode fields into the leaf
3599 */
Chris Masone02119d2008-09-05 16:13:11 -04003600static void fill_inode_item(struct btrfs_trans_handle *trans,
3601 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003602 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003603 struct inode *inode)
3604{
Liu Bo51fab692012-12-27 09:01:21 +00003605 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003606
David Sterbac82f8232019-08-09 17:48:21 +02003607 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003608
David Sterbacc4c13d2020-04-29 02:15:56 +02003609 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3610 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3611 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3612 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3613 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003614
David Sterbacc4c13d2020-04-29 02:15:56 +02003615 btrfs_set_token_timespec_sec(&token, &item->atime,
3616 inode->i_atime.tv_sec);
3617 btrfs_set_token_timespec_nsec(&token, &item->atime,
3618 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003619
David Sterbacc4c13d2020-04-29 02:15:56 +02003620 btrfs_set_token_timespec_sec(&token, &item->mtime,
3621 inode->i_mtime.tv_sec);
3622 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3623 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003624
David Sterbacc4c13d2020-04-29 02:15:56 +02003625 btrfs_set_token_timespec_sec(&token, &item->ctime,
3626 inode->i_ctime.tv_sec);
3627 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3628 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003629
David Sterbacc4c13d2020-04-29 02:15:56 +02003630 btrfs_set_token_timespec_sec(&token, &item->otime,
3631 BTRFS_I(inode)->i_otime.tv_sec);
3632 btrfs_set_token_timespec_nsec(&token, &item->otime,
3633 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303634
David Sterbacc4c13d2020-04-29 02:15:56 +02003635 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3636 btrfs_set_token_inode_generation(&token, item,
3637 BTRFS_I(inode)->generation);
3638 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3639 btrfs_set_token_inode_transid(&token, item, trans->transid);
3640 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3641 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3642 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003643}
3644
Chris Masond352ac62008-09-29 15:18:18 -04003645/*
3646 * copy everything in the in-memory inode into the btree.
3647 */
Chris Mason21151332011-11-10 20:39:08 -05003648static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003649 struct btrfs_root *root,
3650 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003651{
3652 struct btrfs_inode_item *inode_item;
3653 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003654 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003655 int ret;
3656
3657 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003658 if (!path)
3659 return -ENOMEM;
3660
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003661 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003662 if (ret) {
3663 if (ret > 0)
3664 ret = -ENOENT;
3665 goto failed;
3666 }
3667
Chris Mason5f39d392007-10-15 16:14:19 -04003668 leaf = path->nodes[0];
3669 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003670 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003671
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003672 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003673 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003674 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003675 ret = 0;
3676failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003677 btrfs_free_path(path);
3678 return ret;
3679}
3680
Chris Masond352ac62008-09-29 15:18:18 -04003681/*
Chris Mason21151332011-11-10 20:39:08 -05003682 * copy everything in the in-memory inode into the btree.
3683 */
3684noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003685 struct btrfs_root *root,
3686 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003687{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003688 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003689 int ret;
3690
3691 /*
3692 * If the inode is a free space inode, we can deadlock during commit
3693 * if we put it into the delayed code.
3694 *
3695 * The data relocation inode should also be directly updated
3696 * without delay
3697 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003698 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003699 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003700 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003701 btrfs_update_root_times(trans, root);
3702
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003703 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003704 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003705 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05003706 return ret;
3707 }
3708
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003709 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003710}
3711
Nikolay Borisov729f7962020-11-02 16:49:06 +02003712int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3713 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003714{
3715 int ret;
3716
Nikolay Borisov729f7962020-11-02 16:49:06 +02003717 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003718 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02003719 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003720 return ret;
3721}
3722
3723/*
Chris Masond352ac62008-09-29 15:18:18 -04003724 * unlink helper that gets used here in inode.c and in the tree logging
3725 * recovery code. It remove a link in a directory with a given name, and
3726 * also drops the back refs in the inode to the directory
3727 */
Al Viro92986792011-03-04 17:14:37 +00003728static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3729 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003730 struct btrfs_inode *dir,
3731 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003732 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003733{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003734 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003735 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003736 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003737 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003738 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003739 u64 ino = btrfs_ino(inode);
3740 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003741
3742 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003743 if (!path) {
3744 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003745 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003746 }
3747
Li Zefan33345d012011-04-20 10:31:50 +08003748 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003749 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003750 if (IS_ERR_OR_NULL(di)) {
3751 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003752 goto err;
3753 }
Chris Mason39279cc2007-06-12 06:35:45 -04003754 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003755 if (ret)
3756 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003757 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003758
Miao Xie67de1172013-12-26 13:07:06 +08003759 /*
3760 * If we don't have dir index, we have to get it by looking up
3761 * the inode ref, since we get the inode ref, remove it directly,
3762 * it is unnecessary to do delayed deletion.
3763 *
3764 * But if we have dir index, needn't search inode ref to get it.
3765 * Since the inode ref is close to the inode item, it is better
3766 * that we delay to delete it, and just do this deletion when
3767 * we update the inode item.
3768 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003769 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003770 ret = btrfs_delayed_delete_inode_ref(inode);
3771 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003772 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003773 goto skip_backref;
3774 }
3775 }
3776
Li Zefan33345d012011-04-20 10:31:50 +08003777 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3778 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003779 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003780 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003781 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003782 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003783 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003784 goto err;
3785 }
Miao Xie67de1172013-12-26 13:07:06 +08003786skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003787 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003788 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003789 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003790 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003791 }
Chris Mason39279cc2007-06-12 06:35:45 -04003792
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003793 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3794 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003795 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003796 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003797 goto err;
3798 }
Chris Masone02119d2008-09-05 16:13:11 -04003799
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003800 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3801 index);
Chris Mason6418c962010-10-30 07:34:24 -04003802 if (ret == -ENOENT)
3803 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003804 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003805 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003806
3807 /*
3808 * If we have a pending delayed iput we could end up with the final iput
3809 * being run in btrfs-cleaner context. If we have enough of these built
3810 * up we can end up burning a lot of time in btrfs-cleaner without any
3811 * way to throttle the unlinks. Since we're currently holding a ref on
3812 * the inode we can run the delayed iput here without any issues as the
3813 * final iput won't be done until after we drop the ref we're currently
3814 * holding.
3815 */
3816 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003817err:
3818 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003819 if (ret)
3820 goto out;
3821
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003822 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003823 inode_inc_iversion(&inode->vfs_inode);
3824 inode_inc_iversion(&dir->vfs_inode);
3825 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3826 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003827 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003828out:
Chris Mason39279cc2007-06-12 06:35:45 -04003829 return ret;
3830}
3831
Al Viro92986792011-03-04 17:14:37 +00003832int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3833 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003834 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003835 const char *name, int name_len)
3836{
3837 int ret;
3838 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3839 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003840 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003841 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00003842 }
3843 return ret;
3844}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003845
3846/*
3847 * helper to start transaction for unlink and rmdir.
3848 *
Josef Bacikd52be812013-05-29 14:54:47 -04003849 * unlink and rmdir are special in btrfs, they do not always free space, so
3850 * if we cannot make our reservations the normal way try and see if there is
3851 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3852 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003853 */
Josef Bacikd52be812013-05-29 14:54:47 -04003854static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003855{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003856 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003857
Josef Bacike70bea52011-10-11 14:18:24 -04003858 /*
3859 * 1 for the possible orphan item
3860 * 1 for the dir item
3861 * 1 for the dir index
3862 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003863 * 1 for the inode
3864 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003865 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003866}
3867
Chris Mason39279cc2007-06-12 06:35:45 -04003868static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3869{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003870 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003871 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003872 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003873 int ret;
3874
Josef Bacikd52be812013-05-29 14:54:47 -04003875 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003876 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003877 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003878
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003879 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3880 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003881
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003882 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3883 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3884 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003885 if (ret)
3886 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003887
Yan, Zhenga22285a2010-05-16 10:48:46 -04003888 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003889 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003890 if (ret)
3891 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003892 }
Josef Bacik7b128762008-07-24 12:17:14 -04003893
Tsutomu Itohb5324022011-07-19 07:27:20 +00003894out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003895 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003896 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003897 return ret;
3898}
3899
Misono Tomohirof60a2362018-04-18 11:34:52 +09003900static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003901 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003902{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003903 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003904 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003905 struct btrfs_path *path;
3906 struct extent_buffer *leaf;
3907 struct btrfs_dir_item *di;
3908 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003909 const char *name = dentry->d_name.name;
3910 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003911 u64 index;
3912 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003913 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003914 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003915
Josef Bacik045d3962019-12-18 17:20:27 -05003916 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3917 objectid = inode->root->root_key.objectid;
3918 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3919 objectid = inode->location.objectid;
3920 } else {
3921 WARN_ON(1);
3922 return -EINVAL;
3923 }
3924
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003925 path = btrfs_alloc_path();
3926 if (!path)
3927 return -ENOMEM;
3928
Li Zefan33345d012011-04-20 10:31:50 +08003929 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003930 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003931 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003932 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003933 goto out;
3934 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003935
3936 leaf = path->nodes[0];
3937 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3938 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3939 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003940 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003941 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003942 goto out;
3943 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003944 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003945
Josef Bacikd49d3282019-12-18 17:20:28 -05003946 /*
3947 * This is a placeholder inode for a subvolume we didn't have a
3948 * reference to at the time of the snapshot creation. In the meantime
3949 * we could have renamed the real subvol link into our snapshot, so
3950 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3951 * Instead simply lookup the dir_index_item for this entry so we can
3952 * remove it. Otherwise we know we have a ref to the root and we can
3953 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3954 */
3955 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003956 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003957 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003958 if (IS_ERR_OR_NULL(di)) {
3959 if (!di)
3960 ret = -ENOENT;
3961 else
3962 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003963 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003964 goto out;
3965 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003966
3967 leaf = path->nodes[0];
3968 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003969 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003970 btrfs_release_path(path);
3971 } else {
3972 ret = btrfs_del_root_ref(trans, objectid,
3973 root->root_key.objectid, dir_ino,
3974 &index, name, name_len);
3975 if (ret) {
3976 btrfs_abort_transaction(trans, ret);
3977 goto out;
3978 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003979 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003980
Lu Fengqi9add2942018-08-01 11:32:26 +08003981 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003982 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003983 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003984 goto out;
3985 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003986
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003987 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003988 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003989 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02003990 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003991 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003992 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003993out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003994 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003995 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003996}
3997
Misono Tomohiroec42f162018-04-18 11:34:13 +09003998/*
3999 * Helper to check if the subvolume references other subvolumes or if it's
4000 * default.
4001 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004002static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004003{
4004 struct btrfs_fs_info *fs_info = root->fs_info;
4005 struct btrfs_path *path;
4006 struct btrfs_dir_item *di;
4007 struct btrfs_key key;
4008 u64 dir_id;
4009 int ret;
4010
4011 path = btrfs_alloc_path();
4012 if (!path)
4013 return -ENOMEM;
4014
4015 /* Make sure this root isn't set as the default subvol */
4016 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4017 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4018 dir_id, "default", 7, 0);
4019 if (di && !IS_ERR(di)) {
4020 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4021 if (key.objectid == root->root_key.objectid) {
4022 ret = -EPERM;
4023 btrfs_err(fs_info,
4024 "deleting default subvolume %llu is not allowed",
4025 key.objectid);
4026 goto out;
4027 }
4028 btrfs_release_path(path);
4029 }
4030
4031 key.objectid = root->root_key.objectid;
4032 key.type = BTRFS_ROOT_REF_KEY;
4033 key.offset = (u64)-1;
4034
4035 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4036 if (ret < 0)
4037 goto out;
4038 BUG_ON(ret == 0);
4039
4040 ret = 0;
4041 if (path->slots[0] > 0) {
4042 path->slots[0]--;
4043 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4044 if (key.objectid == root->root_key.objectid &&
4045 key.type == BTRFS_ROOT_REF_KEY)
4046 ret = -ENOTEMPTY;
4047 }
4048out:
4049 btrfs_free_path(path);
4050 return ret;
4051}
4052
Nikolay Borisov20a68002018-04-27 14:36:24 +03004053/* Delete all dentries for inodes belonging to the root */
4054static void btrfs_prune_dentries(struct btrfs_root *root)
4055{
4056 struct btrfs_fs_info *fs_info = root->fs_info;
4057 struct rb_node *node;
4058 struct rb_node *prev;
4059 struct btrfs_inode *entry;
4060 struct inode *inode;
4061 u64 objectid = 0;
4062
4063 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4064 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4065
4066 spin_lock(&root->inode_lock);
4067again:
4068 node = root->inode_tree.rb_node;
4069 prev = NULL;
4070 while (node) {
4071 prev = node;
4072 entry = rb_entry(node, struct btrfs_inode, rb_node);
4073
David Sterba37508512018-06-29 10:56:40 +02004074 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004075 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004076 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004077 node = node->rb_right;
4078 else
4079 break;
4080 }
4081 if (!node) {
4082 while (prev) {
4083 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004084 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004085 node = prev;
4086 break;
4087 }
4088 prev = rb_next(prev);
4089 }
4090 }
4091 while (node) {
4092 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004093 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004094 inode = igrab(&entry->vfs_inode);
4095 if (inode) {
4096 spin_unlock(&root->inode_lock);
4097 if (atomic_read(&inode->i_count) > 1)
4098 d_prune_aliases(inode);
4099 /*
4100 * btrfs_drop_inode will have it removed from the inode
4101 * cache when its usage count hits zero.
4102 */
4103 iput(inode);
4104 cond_resched();
4105 spin_lock(&root->inode_lock);
4106 goto again;
4107 }
4108
4109 if (cond_resched_lock(&root->inode_lock))
4110 goto again;
4111
4112 node = rb_next(node);
4113 }
4114 spin_unlock(&root->inode_lock);
4115}
4116
Misono Tomohirof60a2362018-04-18 11:34:52 +09004117int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4118{
4119 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4120 struct btrfs_root *root = BTRFS_I(dir)->root;
4121 struct inode *inode = d_inode(dentry);
4122 struct btrfs_root *dest = BTRFS_I(inode)->root;
4123 struct btrfs_trans_handle *trans;
4124 struct btrfs_block_rsv block_rsv;
4125 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004126 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004127
4128 /*
4129 * Don't allow to delete a subvolume with send in progress. This is
4130 * inside the inode lock so the error handling that has to drop the bit
4131 * again is not run concurrently.
4132 */
4133 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004134 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004135 spin_unlock(&dest->root_item_lock);
4136 btrfs_warn(fs_info,
4137 "attempt to delete subvolume %llu during send",
4138 dest->root_key.objectid);
4139 return -EPERM;
4140 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004141 root_flags = btrfs_root_flags(&dest->root_item);
4142 btrfs_set_root_flags(&dest->root_item,
4143 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4144 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004145
4146 down_write(&fs_info->subvol_sem);
4147
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004148 ret = may_destroy_subvol(dest);
4149 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004150 goto out_up_write;
4151
4152 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4153 /*
4154 * One for dir inode,
4155 * two for dir entries,
4156 * two for root ref/backref.
4157 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004158 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4159 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004160 goto out_up_write;
4161
4162 trans = btrfs_start_transaction(root, 0);
4163 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004164 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004165 goto out_release;
4166 }
4167 trans->block_rsv = &block_rsv;
4168 trans->bytes_reserved = block_rsv.size;
4169
4170 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4171
Josef Bacik045d3962019-12-18 17:20:27 -05004172 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004173 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004174 btrfs_abort_transaction(trans, ret);
4175 goto out_end_trans;
4176 }
4177
4178 btrfs_record_root_in_trans(trans, dest);
4179
4180 memset(&dest->root_item.drop_progress, 0,
4181 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004182 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004183 btrfs_set_root_refs(&dest->root_item, 0);
4184
4185 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4186 ret = btrfs_insert_orphan_item(trans,
4187 fs_info->tree_root,
4188 dest->root_key.objectid);
4189 if (ret) {
4190 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004191 goto out_end_trans;
4192 }
4193 }
4194
Lu Fengqid1957792018-05-29 15:01:54 +08004195 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004196 BTRFS_UUID_KEY_SUBVOL,
4197 dest->root_key.objectid);
4198 if (ret && ret != -ENOENT) {
4199 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004200 goto out_end_trans;
4201 }
4202 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004203 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004204 dest->root_item.received_uuid,
4205 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4206 dest->root_key.objectid);
4207 if (ret && ret != -ENOENT) {
4208 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004209 goto out_end_trans;
4210 }
4211 }
4212
Qu Wenruo082b6c92020-06-16 10:17:37 +08004213 free_anon_bdev(dest->anon_dev);
4214 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004215out_end_trans:
4216 trans->block_rsv = NULL;
4217 trans->bytes_reserved = 0;
4218 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004219 inode->i_flags |= S_DEAD;
4220out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004221 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004222out_up_write:
4223 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004224 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004225 spin_lock(&dest->root_item_lock);
4226 root_flags = btrfs_root_flags(&dest->root_item);
4227 btrfs_set_root_flags(&dest->root_item,
4228 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4229 spin_unlock(&dest->root_item_lock);
4230 } else {
4231 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004232 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004233 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004234 }
4235
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004236 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004237}
4238
Chris Mason39279cc2007-06-12 06:35:45 -04004239static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4240{
David Howells2b0143b2015-03-17 22:25:59 +00004241 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004242 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004243 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004244 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004245 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004246
David Sterbab3ae2442012-09-13 16:04:34 -06004247 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004248 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004249 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004250 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004251
Josef Bacikd52be812013-05-29 14:54:47 -04004252 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004253 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004254 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004255
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004256 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004257 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004258 goto out;
4259 }
4260
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004261 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004262 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004263 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004264
Filipe Manana44f714d2016-06-06 16:11:13 +01004265 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4266
Chris Mason39279cc2007-06-12 06:35:45 -04004267 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004268 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4269 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4270 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004271 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004272 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004273 /*
4274 * Propagate the last_unlink_trans value of the deleted dir to
4275 * its parent directory. This is to prevent an unrecoverable
4276 * log tree in the case we do something like this:
4277 * 1) create dir foo
4278 * 2) create snapshot under dir foo
4279 * 3) delete the snapshot
4280 * 4) rmdir foo
4281 * 5) mkdir foo
4282 * 6) fsync foo or some file inside foo
4283 */
4284 if (last_unlink_trans >= trans->transid)
4285 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4286 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004287out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004288 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004289 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004290
Chris Mason39279cc2007-06-12 06:35:45 -04004291 return err;
4292}
4293
Josef Bacikddfae632017-10-19 14:16:02 -04004294/*
4295 * Return this if we need to call truncate_block for the last bit of the
4296 * truncate.
4297 */
4298#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004299
Chris Mason323ac952008-10-01 19:05:46 -04004300/*
Chris Mason39279cc2007-06-12 06:35:45 -04004301 * this can truncate away extent items, csum items and directory items.
4302 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004303 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004304 *
4305 * csum items that cross the new i_size are truncated to the new size
4306 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004307 *
4308 * min_type is the minimum key type to truncate down to. If set to 0, this
4309 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004310 */
Yan, Zheng80825102009-11-12 09:35:36 +00004311int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4312 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004313 struct btrfs_inode *inode,
Yan, Zheng80825102009-11-12 09:35:36 +00004314 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004315{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004316 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004317 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004318 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004319 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004320 struct btrfs_key key;
4321 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004322 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004323 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004324 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004325 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004326 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004327 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004328 int found_extent;
4329 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004330 int pending_del_nr = 0;
4331 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004332 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004333 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004334 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004335 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004336 bool be_nice = false;
4337 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004338 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4339 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004340
4341 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004342
Chris Mason28ed1342014-12-17 09:41:04 -08004343 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004344 * For non-free space inodes and non-shareable roots, we want to back
4345 * off from time to time. This means all inodes in subvolume roots,
4346 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004347 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004348 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004349 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004350 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004351
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004352 path = btrfs_alloc_path();
4353 if (!path)
4354 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004355 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004356
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004357 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004358 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004359 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004360
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004361 /*
4362 * We want to drop from the next block forward in case this
4363 * new size is not block aligned since we will be keeping the
4364 * last block of the extent just the way it is.
4365 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004366 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004367 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004368 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004369 }
Yan, Zheng80825102009-11-12 09:35:36 +00004370
Miao Xie16cdcec2011-04-22 18:12:22 +08004371 /*
4372 * This function is also used to drop the items in the log tree before
4373 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004374 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004375 * items.
4376 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004377 if (min_type == 0 && root == inode->root)
4378 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004379
Li Zefan33345d012011-04-20 10:31:50 +08004380 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004381 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004382 key.type = (u8)-1;
4383
Chris Mason85e21ba2008-01-29 15:11:36 -05004384search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004385 /*
4386 * with a 16K leaf size and 128MB extents, you can actually queue
4387 * up a huge file in a single leaf. Most of the time that
4388 * bytes_deleted is > 0, it will be huge by the time we get here
4389 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004390 if (be_nice && bytes_deleted > SZ_32M &&
4391 btrfs_should_end_transaction(trans)) {
4392 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004393 goto out;
4394 }
Chris Masond3977122009-01-05 21:25:51 -05004395
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004396 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4397 if (ret < 0)
4398 goto out;
4399
Chris Mason85e21ba2008-01-29 15:11:36 -05004400 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004401 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004402 /* there are no items in the tree for us to truncate, we're
4403 * done
4404 */
Yan, Zheng80825102009-11-12 09:35:36 +00004405 if (path->slots[0] == 0)
4406 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004407 path->slots[0]--;
4408 }
4409
Chris Masond3977122009-01-05 21:25:51 -05004410 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004411 u64 clear_start = 0, clear_len = 0;
4412
Chris Mason39279cc2007-06-12 06:35:45 -04004413 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004414 leaf = path->nodes[0];
4415 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004416 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004417
Li Zefan33345d012011-04-20 10:31:50 +08004418 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004419 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004420
Chris Mason85e21ba2008-01-29 15:11:36 -05004421 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004422 break;
4423
Chris Mason5f39d392007-10-15 16:14:19 -04004424 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004425 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004426 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004427 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004428 extent_type = btrfs_file_extent_type(leaf, fi);
4429 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004430 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004431 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004432
4433 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004434 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004435 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004436 item_end += btrfs_file_extent_ram_bytes(leaf,
4437 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004438
4439 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004440 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004441 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004442 }
Yan008630c2007-11-07 13:31:09 -05004443 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004444 }
Yan, Zheng80825102009-11-12 09:35:36 +00004445 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004446 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004447 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004448 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004449 break;
4450 if (found_key.offset >= new_size)
4451 del_item = 1;
4452 else
4453 del_item = 0;
4454 }
Chris Mason39279cc2007-06-12 06:35:45 -04004455 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004456 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004457 if (found_type != BTRFS_EXTENT_DATA_KEY)
4458 goto delete;
4459
4460 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004461 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004462
4463 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004464 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004465 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004466 u64 orig_num_bytes =
4467 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004468 extent_num_bytes = ALIGN(new_size -
4469 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004470 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004471 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004472 btrfs_set_file_extent_num_bytes(leaf, fi,
4473 extent_num_bytes);
4474 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004475 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004476 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004477 &root->state) &&
4478 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004479 inode_sub_bytes(&inode->vfs_inode,
4480 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004481 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004482 } else {
Chris Masondb945352007-10-15 16:15:53 -04004483 extent_num_bytes =
4484 btrfs_file_extent_disk_num_bytes(leaf,
4485 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004486 extent_offset = found_key.offset -
4487 btrfs_file_extent_offset(leaf, fi);
4488
Chris Mason39279cc2007-06-12 06:35:45 -04004489 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004490 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004491 if (extent_start != 0) {
4492 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004493 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004494 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004495 inode_sub_bytes(&inode->vfs_inode,
4496 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004497 }
4498 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004499 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004500 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004501 /*
4502 * we can't truncate inline items that have had
4503 * special encodings
4504 */
4505 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004506 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004507 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4508 btrfs_file_extent_compression(leaf, fi) == 0) {
4509 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004510
Josef Bacikddfae632017-10-19 14:16:02 -04004511 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4512 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004513 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004514 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004515 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004516 * We have to bail so the last_size is set to
4517 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004518 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004519 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004520 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004521 } else {
4522 /*
4523 * Inline extents are special, we just treat
4524 * them as a full sector worth in the file
4525 * extent tree just for simplicity sake.
4526 */
4527 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004528 }
Josef Bacikddfae632017-10-19 14:16:02 -04004529
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004530 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004531 inode_sub_bytes(&inode->vfs_inode,
4532 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004533 }
Chris Mason179e29e2007-11-01 11:28:41 -04004534delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004535 /*
4536 * We use btrfs_truncate_inode_items() to clean up log trees for
4537 * multiple fsyncs, and in this case we don't want to clear the
4538 * file extent range because it's just the log.
4539 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004540 if (root == inode->root) {
4541 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004542 clear_start, clear_len);
4543 if (ret) {
4544 btrfs_abort_transaction(trans, ret);
4545 break;
4546 }
4547 }
4548
Josef Bacikddfae632017-10-19 14:16:02 -04004549 if (del_item)
4550 last_size = found_key.offset;
4551 else
4552 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004553 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004554 if (!pending_del_nr) {
4555 /* no pending yet, add ourselves */
4556 pending_del_slot = path->slots[0];
4557 pending_del_nr = 1;
4558 } else if (pending_del_nr &&
4559 path->slots[0] + 1 == pending_del_slot) {
4560 /* hop on the pending chunk */
4561 pending_del_nr++;
4562 pending_del_slot = path->slots[0];
4563 } else {
Chris Masond3977122009-01-05 21:25:51 -05004564 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004565 }
Chris Mason39279cc2007-06-12 06:35:45 -04004566 } else {
4567 break;
4568 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004569 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004570
Miao Xie27cdeb72014-04-02 19:51:05 +08004571 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004572 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004573 struct btrfs_ref ref = { 0 };
4574
Chris Mason28ed1342014-12-17 09:41:04 -08004575 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004576
4577 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4578 extent_start, extent_num_bytes, 0);
4579 ref.real_root = root->root_key.objectid;
4580 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4581 ino, extent_offset);
4582 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004583 if (ret) {
4584 btrfs_abort_transaction(trans, ret);
4585 break;
4586 }
Chris Mason28f75a02015-02-04 06:59:29 -08004587 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004588 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004589 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004590 }
Chris Mason39279cc2007-06-12 06:35:45 -04004591 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004592
Yan, Zheng80825102009-11-12 09:35:36 +00004593 if (found_type == BTRFS_INODE_ITEM_KEY)
4594 break;
4595
4596 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004597 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004598 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004599 if (pending_del_nr) {
4600 ret = btrfs_del_items(trans, root, path,
4601 pending_del_slot,
4602 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004603 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004604 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004605 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004606 }
Yan, Zheng80825102009-11-12 09:35:36 +00004607 pending_del_nr = 0;
4608 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004609 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004610
Chris Mason28f75a02015-02-04 06:59:29 -08004611 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004612 * We can generate a lot of delayed refs, so we need to
4613 * throttle every once and a while and make sure we're
4614 * adding enough space to keep up with the work we are
4615 * generating. Since we hold a transaction here we
4616 * can't flush, and we don't want to FLUSH_LIMIT because
4617 * we could have generated too many delayed refs to
4618 * actually allocate, so just bail if we're short and
4619 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004620 */
Josef Bacik28bad212018-12-03 10:20:38 -05004621 if (should_throttle) {
4622 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4623 BTRFS_RESERVE_NO_FLUSH);
4624 if (ret) {
4625 ret = -EAGAIN;
4626 break;
4627 }
Chris Mason28f75a02015-02-04 06:59:29 -08004628 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004629 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004630 } else {
4631 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004632 }
Chris Mason39279cc2007-06-12 06:35:45 -04004633 }
Yan, Zheng80825102009-11-12 09:35:36 +00004634out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004635 if (ret >= 0 && pending_del_nr) {
4636 int err;
4637
4638 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004639 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004640 if (err) {
4641 btrfs_abort_transaction(trans, err);
4642 ret = err;
4643 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004644 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004645 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4646 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004647 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004648 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004649 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4650 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4651 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004652 }
Chris Mason28ed1342014-12-17 09:41:04 -08004653
Chris Mason39279cc2007-06-12 06:35:45 -04004654 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004655 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004656}
4657
Chris Masona52d9a82007-08-27 16:49:44 -04004658/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304659 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004660 * @inode - inode that we're zeroing
4661 * @from - the offset to start zeroing
4662 * @len - the length to zero, 0 to zero the entire range respective to the
4663 * offset
4664 * @front - zero up to the offset instead of from the offset on
4665 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304666 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004667 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004668 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004669int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4670 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004671{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004672 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4673 struct address_space *mapping = inode->vfs_inode.i_mapping;
4674 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004675 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004676 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004677 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004678 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004679 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004680 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004681 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304682 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004683 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004684 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004685 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004686 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304687 u64 block_start;
4688 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004689
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004690 if (IS_ALIGNED(offset, blocksize) &&
4691 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004692 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304693
Josef Bacik8b62f872017-10-19 14:15:55 -04004694 block_start = round_down(from, blocksize);
4695 block_end = block_start + blocksize - 1;
4696
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004697 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4698 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004699 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004700 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004701 /* For nocow case, no need to reserve data space */
4702 only_release_metadata = true;
4703 } else {
4704 goto out;
4705 }
4706 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004707 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004708 if (ret < 0) {
4709 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004710 btrfs_free_reserved_data_space(inode, data_reserved,
4711 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004712 goto out;
4713 }
Chris Mason211c17f2008-05-15 09:13:45 -04004714again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004715 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004716 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004717 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4718 blocksize, true);
4719 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004720 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004721 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004722 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004723
Chris Masona52d9a82007-08-27 16:49:44 -04004724 if (!PageUptodate(page)) {
4725 ret = btrfs_readpage(NULL, page);
4726 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004727 if (page->mapping != mapping) {
4728 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004729 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004730 goto again;
4731 }
Chris Masona52d9a82007-08-27 16:49:44 -04004732 if (!PageUptodate(page)) {
4733 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004734 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004735 }
4736 }
Chris Mason211c17f2008-05-15 09:13:45 -04004737 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004738
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304739 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004740 set_page_extent_mapped(page);
4741
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004742 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004743 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304744 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004745 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004746 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004747 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004748 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004749 btrfs_put_ordered_extent(ordered);
4750 goto again;
4751 }
4752
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004753 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004754 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4755 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004756
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004757 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004758 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004759 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304760 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004761 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004762 goto out_unlock;
4763 }
4764
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304765 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004766 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304767 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004768 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004769 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304770 memset(kaddr + (block_start - page_offset(page)),
4771 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004772 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304773 memset(kaddr + (block_start - page_offset(page)) + offset,
4774 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004775 flush_dcache_page(page);
4776 kunmap(page);
4777 }
Chris Mason247e7432008-07-17 12:53:51 -04004778 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004779 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004780 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004781
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004782 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004783 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02004784 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004785
Chris Mason89642222008-07-24 09:41:53 -04004786out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004787 if (ret) {
4788 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004789 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004790 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004791 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004792 block_start, blocksize, true);
4793 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004794 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004795 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004796 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004797out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004798 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004799 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004800 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004801 return ret;
4802}
4803
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004804static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04004805 u64 offset, u64 len)
4806{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004807 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04004808 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00004809 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04004810 int ret;
4811
4812 /*
4813 * Still need to make sure the inode looks like it's been updated so
4814 * that any holes get logged if we fsync.
4815 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004816 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004817 inode->last_trans = fs_info->generation;
4818 inode->last_sub_trans = root->log_transid;
4819 inode->last_log_commit = root->last_log_commit;
Josef Bacik16e75492013-10-22 12:18:51 -04004820 return 0;
4821 }
4822
4823 /*
4824 * 1 - for the one we're dropping
4825 * 1 - for the one we're adding
4826 * 1 - for updating the inode.
4827 */
4828 trans = btrfs_start_transaction(root, 3);
4829 if (IS_ERR(trans))
4830 return PTR_ERR(trans);
4831
Filipe Manana5893dfb2020-11-04 11:07:32 +00004832 drop_args.start = offset;
4833 drop_args.end = offset + len;
4834 drop_args.drop_cache = true;
4835
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004836 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04004837 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004838 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004839 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004840 return ret;
4841 }
4842
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004843 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004844 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00004845 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004846 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00004847 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004848 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
4849 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00004850 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004851 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004852 return ret;
4853}
4854
Josef Bacik695a0d02011-03-04 15:46:53 -05004855/*
4856 * This function puts in dummy file extents for the area we're creating a hole
4857 * for. So if we are truncating this file to a larger size we need to insert
4858 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4859 * the range between oldsize and size
4860 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004861int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004862{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004863 struct btrfs_root *root = inode->root;
4864 struct btrfs_fs_info *fs_info = root->fs_info;
4865 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004866 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004867 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004868 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004869 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4870 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004871 u64 last_byte;
4872 u64 cur_offset;
4873 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004874 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004875
Josef Bacika71754f2013-06-17 17:14:39 -04004876 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304877 * If our size started in the middle of a block we need to zero out the
4878 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004879 * expose stale data.
4880 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004881 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004882 if (err)
4883 return err;
4884
Yan Zheng9036c102008-10-30 14:19:41 -04004885 if (size <= hole_start)
4886 return 0;
4887
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004888 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
4889 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004890 cur_offset = hole_start;
4891 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004892 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004893 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004894 if (IS_ERR(em)) {
4895 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004896 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004897 break;
4898 }
Yan Zheng9036c102008-10-30 14:19:41 -04004899 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004900 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004901 hole_size = last_byte - cur_offset;
4902
Yan, Zheng80825102009-11-12 09:35:36 +00004903 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004904 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004905
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004906 err = maybe_insert_hole(root, inode, cur_offset,
4907 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04004908 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004909 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004910
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004911 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004912 cur_offset, hole_size);
4913 if (err)
4914 break;
4915
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004916 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004917 cur_offset + hole_size - 1, 0);
4918 hole_em = alloc_extent_map();
4919 if (!hole_em) {
4920 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004921 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004922 goto next;
4923 }
4924 hole_em->start = cur_offset;
4925 hole_em->len = hole_size;
4926 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004927
Josef Bacik5dc562c2012-08-17 13:14:17 -04004928 hole_em->block_start = EXTENT_MAP_HOLE;
4929 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004930 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004931 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004932 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004933 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004934
4935 while (1) {
4936 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004937 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004938 write_unlock(&em_tree->lock);
4939 if (err != -EEXIST)
4940 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004941 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004942 cur_offset +
4943 hole_size - 1, 0);
4944 }
4945 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004946 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004947 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004948 cur_offset, hole_size);
4949 if (err)
4950 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004951 }
Josef Bacik16e75492013-10-22 12:18:51 -04004952next:
Yan Zheng9036c102008-10-30 14:19:41 -04004953 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004954 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004955 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004956 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004957 break;
4958 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004959 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004960 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004961 return err;
4962}
4963
Eric Sandeen3972f262013-01-12 02:57:22 +00004964static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004965{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004966 struct btrfs_root *root = BTRFS_I(inode)->root;
4967 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004968 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004969 loff_t newsize = attr->ia_size;
4970 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004971 int ret;
4972
Eric Sandeen3972f262013-01-12 02:57:22 +00004973 /*
4974 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4975 * special case where we need to update the times despite not having
4976 * these flags set. For all other operations the VFS set these flags
4977 * explicitly if it wants a timestamp update.
4978 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004979 if (newsize != oldsize) {
4980 inode_inc_iversion(inode);
4981 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4982 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004983 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004984 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004985
Josef Bacika41ad392011-01-31 15:30:16 -05004986 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004987 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004988 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004989 * This is to ensure the snapshot captures a fully consistent
4990 * state of this file - if the snapshot captures this expanding
4991 * truncation, it must capture all writes that happened before
4992 * this truncation.
4993 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004994 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004995 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004996 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004997 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00004998 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004999 }
Yan, Zheng80825102009-11-12 09:35:36 +00005000
Miao Xief4a2f4c2011-12-14 20:12:01 -05005001 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005002 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005003 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005004 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005005 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005006
5007 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005008 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305009 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005010 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005011 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005012 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005013 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005014
Josef Bacika41ad392011-01-31 15:30:16 -05005015 /*
5016 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005017 * zero. Make sure any new writes to the file get on disk
5018 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005019 */
5020 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005021 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005022 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005023
Josef Bacika41ad392011-01-31 15:30:16 -05005024 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005025
Miao Xie2e60a512013-02-08 07:01:08 +00005026 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005027
Filipe Manana213e8c52018-02-06 20:40:31 +00005028 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005029 if (ret && inode->i_nlink) {
5030 int err;
5031
5032 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005033 * Truncate failed, so fix up the in-memory size. We
5034 * adjusted disk_i_size down as we removed extents, so
5035 * wait for disk_i_size to be stable and then update the
5036 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005037 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005038 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005039 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005040 return err;
5041 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005042 }
Yan, Zheng80825102009-11-12 09:35:36 +00005043 }
5044
Josef Bacika41ad392011-01-31 15:30:16 -05005045 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005046}
5047
Chris Mason39279cc2007-06-12 06:35:45 -04005048static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5049{
David Howells2b0143b2015-03-17 22:25:59 +00005050 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005051 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005052 int err;
5053
Li Zefanb83cc962010-12-20 16:04:08 +08005054 if (btrfs_root_readonly(root))
5055 return -EROFS;
5056
Jan Kara31051c82016-05-26 16:55:18 +02005057 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005058 if (err)
5059 return err;
5060
Chris Mason5a3f23d2009-03-31 13:27:11 -04005061 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005062 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005063 if (err)
5064 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005065 }
Yan Zheng9036c102008-10-30 14:19:41 -04005066
Christoph Hellwig10257742010-06-04 11:30:02 +02005067 if (attr->ia_valid) {
5068 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005069 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005070 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005071
Josef Bacik22c44fe2011-11-30 10:45:38 -05005072 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005073 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005074 }
5075
Chris Mason39279cc2007-06-12 06:35:45 -04005076 return err;
5077}
Chris Mason61295eb2008-01-14 16:24:38 -05005078
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005079/*
5080 * While truncating the inode pages during eviction, we get the VFS calling
5081 * btrfs_invalidatepage() against each page of the inode. This is slow because
5082 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5083 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5084 * extent_state structures over and over, wasting lots of time.
5085 *
5086 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5087 * those expensive operations on a per page basis and do only the ordered io
5088 * finishing, while we release here the extent_map and extent_state structures,
5089 * without the excessive merging and splitting.
5090 */
5091static void evict_inode_truncate_pages(struct inode *inode)
5092{
5093 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5094 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5095 struct rb_node *node;
5096
5097 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005098 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005099
5100 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005101 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005102 struct extent_map *em;
5103
Liu Bo07e1ce02018-08-23 03:51:52 +08005104 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005105 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005106 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5107 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005108 remove_extent_mapping(map_tree, em);
5109 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005110 if (need_resched()) {
5111 write_unlock(&map_tree->lock);
5112 cond_resched();
5113 write_lock(&map_tree->lock);
5114 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005115 }
5116 write_unlock(&map_tree->lock);
5117
Filipe Manana6ca07092015-05-26 00:55:42 +01005118 /*
5119 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005120 * We can have ongoing bios started by readahead that have
5121 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005122 * still in progress (unlocked the pages in the bio but did not yet
5123 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005124 * ranges can still be locked and eviction started because before
5125 * submitting those bios, which are executed by a separate task (work
5126 * queue kthread), inode references (inode->i_count) were not taken
5127 * (which would be dropped in the end io callback of each bio).
5128 * Therefore here we effectively end up waiting for those bios and
5129 * anyone else holding locked ranges without having bumped the inode's
5130 * reference count - if we don't do it, when they access the inode's
5131 * io_tree to unlock a range it may be too late, leading to an
5132 * use-after-free issue.
5133 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005134 spin_lock(&io_tree->lock);
5135 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5136 struct extent_state *state;
5137 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005138 u64 start;
5139 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005140 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005141
5142 node = rb_first(&io_tree->state);
5143 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005144 start = state->start;
5145 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005146 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005147 spin_unlock(&io_tree->lock);
5148
David Sterbaff13db42015-12-03 14:30:40 +01005149 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005150
5151 /*
5152 * If still has DELALLOC flag, the extent didn't reach disk,
5153 * and its reserved space won't be freed by delayed_ref.
5154 * So we need to free its reserved space here.
5155 * (Refer to comment in btrfs_invalidatepage, case 2)
5156 *
5157 * Note, end is the bytenr of last byte, so we need + 1 here.
5158 */
Filipe Manana421f0922018-10-12 13:02:48 +01005159 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005160 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5161 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005162
Filipe Manana6ca07092015-05-26 00:55:42 +01005163 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005164 EXTENT_LOCKED | EXTENT_DELALLOC |
5165 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5166 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005167
Filipe Manana7064dd52014-08-08 02:47:05 +01005168 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005169 spin_lock(&io_tree->lock);
5170 }
5171 spin_unlock(&io_tree->lock);
5172}
5173
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005174static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005175 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005176{
5177 struct btrfs_fs_info *fs_info = root->fs_info;
5178 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005179 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005180 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005181 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005182
Josef Bacikd3984c92019-08-01 18:19:37 -04005183 /*
5184 * Eviction should be taking place at some place safe because of our
5185 * delayed iputs. However the normal flushing code will run delayed
5186 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5187 *
5188 * We reserve the delayed_refs_extra here again because we can't use
5189 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5190 * above. We reserve our extra bit here because we generate a ton of
5191 * delayed refs activity by truncating.
5192 *
5193 * If we cannot make our reservation we'll attempt to steal from the
5194 * global reserve, because we really want to be able to free up space.
5195 */
5196 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5197 BTRFS_RESERVE_FLUSH_EVICT);
5198 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005199 /*
5200 * Try to steal from the global reserve if there is space for
5201 * it.
5202 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005203 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5204 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5205 btrfs_warn(fs_info,
5206 "could not allocate space for delete; will truncate on mount");
5207 return ERR_PTR(-ENOSPC);
5208 }
5209 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005210 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005211
5212 trans = btrfs_join_transaction(root);
5213 if (IS_ERR(trans))
5214 return trans;
5215
5216 if (delayed_refs_extra) {
5217 trans->block_rsv = &fs_info->trans_block_rsv;
5218 trans->bytes_reserved = delayed_refs_extra;
5219 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5220 delayed_refs_extra, 1);
5221 }
5222 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005223}
5224
Al Virobd555972010-06-07 11:35:40 -04005225void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005226{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005227 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005228 struct btrfs_trans_handle *trans;
5229 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005230 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005231 int ret;
5232
liubo1abe9b82011-03-24 11:18:59 +00005233 trace_btrfs_inode_evict(inode);
5234
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005235 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005236 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005237 return;
5238 }
5239
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005240 evict_inode_truncate_pages(inode);
5241
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005242 if (inode->i_nlink &&
5243 ((btrfs_root_refs(&root->root_item) != 0 &&
5244 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005245 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005246 goto no_delete;
5247
Omar Sandoval27919062018-05-11 13:13:37 -07005248 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005249 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005250
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005251 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005252
Omar Sandoval7b40b692018-05-11 13:13:33 -07005253 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005254 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005255
Yan, Zheng76dda932009-09-21 16:00:26 -04005256 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005257 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5258 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005259 goto no_delete;
5260 }
5261
Nikolay Borisovaa790212017-01-10 20:35:40 +02005262 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005263 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005264 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005265
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005266 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005267 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005268 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005269 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005270 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005271
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005272 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005273
Yan, Zheng80825102009-11-12 09:35:36 +00005274 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005275 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005276 if (IS_ERR(trans))
5277 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005278
5279 trans->block_rsv = rsv;
5280
Nikolay Borisov50743392020-11-02 16:48:55 +02005281 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
5282 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005283 trans->block_rsv = &fs_info->trans_block_rsv;
5284 btrfs_end_transaction(trans);
5285 btrfs_btree_balance_dirty(fs_info);
5286 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5287 goto free_rsv;
5288 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005289 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005290 }
5291
Josef Bacik4ef31a42013-08-13 14:10:08 -04005292 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005293 * Errors here aren't a big deal, it just means we leave orphan items in
5294 * the tree. They will be cleaned up on the next mount. If the inode
5295 * number gets reused, cleanup deletes the orphan item without doing
5296 * anything, and unlink reuses the existing orphan item.
5297 *
5298 * If it turns out that we are dropping too many of these, we might want
5299 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005300 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005301 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005302 if (!IS_ERR(trans)) {
5303 trans->block_rsv = rsv;
5304 btrfs_orphan_del(trans, BTRFS_I(inode));
5305 trans->block_rsv = &fs_info->trans_block_rsv;
5306 btrfs_end_transaction(trans);
5307 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005308
Omar Sandoval27919062018-05-11 13:13:37 -07005309free_rsv:
5310 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005311no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005312 /*
5313 * If we didn't successfully delete, the orphan item will still be in
5314 * the tree and we'll retry on the next mount. Again, we might also want
5315 * to retry these periodically in the future.
5316 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005317 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005318 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005319}
5320
5321/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005322 * Return the key found in the dir entry in the location pointer, fill @type
5323 * with BTRFS_FT_*, and return 0.
5324 *
Su Yue005d6712018-03-05 17:13:37 +08005325 * If no dir entries were found, returns -ENOENT.
5326 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005327 */
5328static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005329 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005330{
5331 const char *name = dentry->d_name.name;
5332 int namelen = dentry->d_name.len;
5333 struct btrfs_dir_item *di;
5334 struct btrfs_path *path;
5335 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005336 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005337
5338 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005339 if (!path)
5340 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005341
David Sterbaf85b7372017-01-20 14:54:07 +01005342 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5343 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005344 if (IS_ERR_OR_NULL(di)) {
5345 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005346 goto out;
5347 }
Chris Masond3977122009-01-05 21:25:51 -05005348
Chris Mason5f39d392007-10-15 16:14:19 -04005349 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005350 if (location->type != BTRFS_INODE_ITEM_KEY &&
5351 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005352 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005353 btrfs_warn(root->fs_info,
5354"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5355 __func__, name, btrfs_ino(BTRFS_I(dir)),
5356 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005357 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005358 if (!ret)
5359 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005360out:
Chris Mason39279cc2007-06-12 06:35:45 -04005361 btrfs_free_path(path);
5362 return ret;
5363}
5364
5365/*
5366 * when we hit a tree root in a directory, the btrfs part of the inode
5367 * needs to be changed to reflect the root directory of the tree root. This
5368 * is kind of like crossing a mount point.
5369 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005370static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005371 struct inode *dir,
5372 struct dentry *dentry,
5373 struct btrfs_key *location,
5374 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005375{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005376 struct btrfs_path *path;
5377 struct btrfs_root *new_root;
5378 struct btrfs_root_ref *ref;
5379 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005380 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005381 int ret;
5382 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005383
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005384 path = btrfs_alloc_path();
5385 if (!path) {
5386 err = -ENOMEM;
5387 goto out;
5388 }
Chris Mason39279cc2007-06-12 06:35:45 -04005389
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005390 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005391 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5392 key.type = BTRFS_ROOT_REF_KEY;
5393 key.offset = location->objectid;
5394
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005395 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005396 if (ret) {
5397 if (ret < 0)
5398 err = ret;
5399 goto out;
5400 }
Chris Mason39279cc2007-06-12 06:35:45 -04005401
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005402 leaf = path->nodes[0];
5403 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005404 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005405 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5406 goto out;
5407
5408 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5409 (unsigned long)(ref + 1),
5410 dentry->d_name.len);
5411 if (ret)
5412 goto out;
5413
David Sterbab3b4aa72011-04-21 01:20:15 +02005414 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005415
David Sterba56e93572020-05-15 19:35:55 +02005416 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005417 if (IS_ERR(new_root)) {
5418 err = PTR_ERR(new_root);
5419 goto out;
5420 }
5421
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005422 *sub_root = new_root;
5423 location->objectid = btrfs_root_dirid(&new_root->root_item);
5424 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005425 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005426 err = 0;
5427out:
5428 btrfs_free_path(path);
5429 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005430}
5431
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005432static void inode_tree_add(struct inode *inode)
5433{
5434 struct btrfs_root *root = BTRFS_I(inode)->root;
5435 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005436 struct rb_node **p;
5437 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005438 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005439 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005440
Al Viro1d3382cb2010-10-23 15:19:20 -04005441 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005442 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005443 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005444 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005445 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005446 while (*p) {
5447 parent = *p;
5448 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5449
David Sterba37508512018-06-29 10:56:40 +02005450 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005451 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005452 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005453 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005454 else {
5455 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005456 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005457 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005458 RB_CLEAR_NODE(parent);
5459 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005460 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005461 }
5462 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005463 rb_link_node(new, parent, p);
5464 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005465 spin_unlock(&root->inode_lock);
5466}
5467
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005468static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005469{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005470 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005471 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005472
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005473 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005474 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5475 rb_erase(&inode->rb_node, &root->inode_tree);
5476 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005477 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005478 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005479 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005480
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005481 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005482 spin_lock(&root->inode_lock);
5483 empty = RB_EMPTY_ROOT(&root->inode_tree);
5484 spin_unlock(&root->inode_lock);
5485 if (empty)
5486 btrfs_add_dead_root(root);
5487 }
5488}
5489
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005490
Chris Masone02119d2008-09-05 16:13:11 -04005491static int btrfs_init_locked_inode(struct inode *inode, void *p)
5492{
5493 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005494
5495 inode->i_ino = args->ino;
5496 BTRFS_I(inode)->location.objectid = args->ino;
5497 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5498 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005499 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5500 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005501 return 0;
5502}
5503
5504static int btrfs_find_actor(struct inode *inode, void *opaque)
5505{
5506 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005507
5508 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005509 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005510}
5511
David Sterba0202e832020-05-15 19:35:59 +02005512static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005513 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005514{
5515 struct inode *inode;
5516 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005517 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005518
David Sterba0202e832020-05-15 19:35:59 +02005519 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005520 args.root = root;
5521
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005522 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005523 btrfs_init_locked_inode,
5524 (void *)&args);
5525 return inode;
5526}
5527
David Sterba4c66e0d2019-10-03 19:09:35 +02005528/*
David Sterba0202e832020-05-15 19:35:59 +02005529 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005530 * Path can be preallocated to prevent recursing back to iget through
5531 * allocator. NULL is also valid but may require an additional allocation
5532 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305533 */
David Sterba0202e832020-05-15 19:35:59 +02005534struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005535 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305536{
5537 struct inode *inode;
5538
David Sterba0202e832020-05-15 19:35:59 +02005539 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305540 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005541 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305542
5543 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005544 int ret;
5545
Filipe Manana4222ea72018-10-24 10:13:03 +01005546 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005547 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005548 inode_tree_add(inode);
5549 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005550 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005551 iget_failed(inode);
5552 /*
5553 * ret > 0 can come from btrfs_search_slot called by
5554 * btrfs_read_locked_inode, this means the inode item
5555 * was not found.
5556 */
5557 if (ret > 0)
5558 ret = -ENOENT;
5559 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005560 }
5561 }
5562
Balaji Rao1a54ef82008-07-21 02:01:04 +05305563 return inode;
5564}
5565
David Sterba0202e832020-05-15 19:35:59 +02005566struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005567{
David Sterba0202e832020-05-15 19:35:59 +02005568 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005569}
5570
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005571static struct inode *new_simple_dir(struct super_block *s,
5572 struct btrfs_key *key,
5573 struct btrfs_root *root)
5574{
5575 struct inode *inode = new_inode(s);
5576
5577 if (!inode)
5578 return ERR_PTR(-ENOMEM);
5579
Josef Bacik5c8fd992020-02-14 16:11:43 -05005580 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005581 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005582 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005583
5584 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005585 /*
5586 * We only need lookup, the rest is read-only and there's no inode
5587 * associated with the dentry
5588 */
5589 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005590 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005591 inode->i_fop = &simple_dir_operations;
5592 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005593 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305594 inode->i_atime = inode->i_mtime;
5595 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005596 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005597
5598 return inode;
5599}
5600
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005601static inline u8 btrfs_inode_type(struct inode *inode)
5602{
5603 /*
5604 * Compile-time asserts that generic FT_* types still match
5605 * BTRFS_FT_* types
5606 */
5607 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5608 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5609 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5610 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5611 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5612 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5613 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5614 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5615
5616 return fs_umode_to_ftype(inode->i_mode);
5617}
5618
Chris Mason3de45862008-11-17 21:02:50 -05005619struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005620{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005621 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005622 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005623 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005624 struct btrfs_root *sub_root = root;
5625 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005626 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005627 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005628
5629 if (dentry->d_name.len > BTRFS_NAME_LEN)
5630 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005631
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005632 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005633 if (ret < 0)
5634 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005635
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005636 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005637 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005638 if (IS_ERR(inode))
5639 return inode;
5640
5641 /* Do extra check against inode mode with di_type */
5642 if (btrfs_inode_type(inode) != di_type) {
5643 btrfs_crit(fs_info,
5644"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5645 inode->i_mode, btrfs_inode_type(inode),
5646 di_type);
5647 iput(inode);
5648 return ERR_PTR(-EUCLEAN);
5649 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005650 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005651 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005653 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005654 &location, &sub_root);
5655 if (ret < 0) {
5656 if (ret != -ENOENT)
5657 inode = ERR_PTR(ret);
5658 else
5659 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5660 } else {
David Sterba0202e832020-05-15 19:35:59 +02005661 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005662 }
Josef Bacik87270022020-01-24 09:32:31 -05005663 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005664 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005665
Julia Lawall34d19ba2011-01-24 19:55:19 +00005666 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005667 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005668 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005669 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005670 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005671 if (ret) {
5672 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005673 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005674 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005675 }
5676
Chris Mason3de45862008-11-17 21:02:50 -05005677 return inode;
5678}
5679
Nick Pigginfe15ce42011-01-07 17:49:23 +11005680static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005681{
5682 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005683 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005684
Li Zefan848cce02012-02-21 17:04:28 +08005685 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005686 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005687
Li Zefan848cce02012-02-21 17:04:28 +08005688 if (inode) {
5689 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005690 if (btrfs_root_refs(&root->root_item) == 0)
5691 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005692
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005693 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005694 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005695 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005696 return 0;
5697}
5698
Chris Mason3de45862008-11-17 21:02:50 -05005699static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005700 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005701{
Al Viro3837d202018-10-10 16:38:27 -04005702 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005703
Al Viro3837d202018-10-10 16:38:27 -04005704 if (inode == ERR_PTR(-ENOENT))
5705 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005706 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005707}
5708
Josef Bacik23b5ec72017-07-24 15:14:25 -04005709/*
5710 * All this infrastructure exists because dir_emit can fault, and we are holding
5711 * the tree lock when doing readdir. For now just allocate a buffer and copy
5712 * our information into that, and then dir_emit from the buffer. This is
5713 * similar to what NFS does, only we don't keep the buffer around in pagecache
5714 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5715 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5716 * tree lock.
5717 */
5718static int btrfs_opendir(struct inode *inode, struct file *file)
5719{
5720 struct btrfs_file_private *private;
5721
5722 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5723 if (!private)
5724 return -ENOMEM;
5725 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5726 if (!private->filldir_buf) {
5727 kfree(private);
5728 return -ENOMEM;
5729 }
5730 file->private_data = private;
5731 return 0;
5732}
5733
5734struct dir_entry {
5735 u64 ino;
5736 u64 offset;
5737 unsigned type;
5738 int name_len;
5739};
5740
5741static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5742{
5743 while (entries--) {
5744 struct dir_entry *entry = addr;
5745 char *name = (char *)(entry + 1);
5746
David Sterba92d32172018-04-16 21:10:14 +02005747 ctx->pos = get_unaligned(&entry->offset);
5748 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5749 get_unaligned(&entry->ino),
5750 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005751 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005752 addr += sizeof(struct dir_entry) +
5753 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005754 ctx->pos++;
5755 }
5756 return 0;
5757}
5758
Al Viro9cdda8d2013-05-22 16:48:09 -04005759static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005760{
Al Viro9cdda8d2013-05-22 16:48:09 -04005761 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005762 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005763 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005764 struct btrfs_dir_item *di;
5765 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005766 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005767 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005768 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005769 struct list_head ins_list;
5770 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005771 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005772 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005773 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005774 char *name_ptr;
5775 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005776 int entries = 0;
5777 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005778 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005779 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005780
Al Viro9cdda8d2013-05-22 16:48:09 -04005781 if (!dir_emit_dots(file, ctx))
5782 return 0;
5783
David Woodhouse49593bf2008-08-17 17:08:36 +01005784 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005785 if (!path)
5786 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005787
Josef Bacik23b5ec72017-07-24 15:14:25 -04005788 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005789 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005790
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005791 INIT_LIST_HEAD(&ins_list);
5792 INIT_LIST_HEAD(&del_list);
5793 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005794
Josef Bacik23b5ec72017-07-24 15:14:25 -04005795again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005796 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005797 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005798 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005799
Chris Mason39279cc2007-06-12 06:35:45 -04005800 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5801 if (ret < 0)
5802 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005803
5804 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005805 struct dir_entry *entry;
5806
Chris Mason5f39d392007-10-15 16:14:19 -04005807 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005808 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005809 if (slot >= btrfs_header_nritems(leaf)) {
5810 ret = btrfs_next_leaf(root, path);
5811 if (ret < 0)
5812 goto err;
5813 else if (ret > 0)
5814 break;
5815 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005816 }
Chris Mason3de45862008-11-17 21:02:50 -05005817
Chris Mason5f39d392007-10-15 16:14:19 -04005818 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5819
5820 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005821 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005822 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005823 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005824 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005825 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005826 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005827 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005828 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005829 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005830 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5831 PAGE_SIZE) {
5832 btrfs_release_path(path);
5833 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5834 if (ret)
5835 goto nopos;
5836 addr = private->filldir_buf;
5837 entries = 0;
5838 total_len = 0;
5839 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005840 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005841
5842 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005843 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005844 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005845 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5846 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005847 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005848 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005849 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005850 put_unaligned(location.objectid, &entry->ino);
5851 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005852 entries++;
5853 addr += sizeof(struct dir_entry) + name_len;
5854 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005855next:
5856 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005857 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005858 btrfs_release_path(path);
5859
5860 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5861 if (ret)
5862 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005863
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005864 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005865 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005866 goto nopos;
5867
Zach Browndb62efb2013-07-11 16:19:42 -07005868 /*
5869 * Stop new entries from being returned after we return the last
5870 * entry.
5871 *
5872 * New directory entries are assigned a strictly increasing
5873 * offset. This means that new entries created during readdir
5874 * are *guaranteed* to be seen in the future by that readdir.
5875 * This has broken buggy programs which operate on names as
5876 * they're returned by readdir. Until we re-use freed offsets
5877 * we have this hack to stop new entries from being returned
5878 * under the assumption that they'll never reach this huge
5879 * offset.
5880 *
5881 * This is being careful not to overflow 32bit loff_t unless the
5882 * last entry requires it because doing so has broken 32bit apps
5883 * in the past.
5884 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005885 if (ctx->pos >= INT_MAX)
5886 ctx->pos = LLONG_MAX;
5887 else
5888 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005889nopos:
5890 ret = 0;
5891err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005892 if (put)
5893 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005894 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005895 return ret;
5896}
5897
Chris Mason39279cc2007-06-12 06:35:45 -04005898/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005899 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005900 * inode changes. But, it is most likely to find the inode in cache.
5901 * FIXME, needs more benchmarking...there are no reasons other than performance
5902 * to keep or drop this code.
5903 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005904static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005905{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005906 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005907 struct btrfs_root *root = BTRFS_I(inode)->root;
5908 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005909 int ret;
5910
Josef Bacik72ac3c02012-05-23 14:13:11 -04005911 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005912 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005913
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005914 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005915 if (IS_ERR(trans))
5916 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005917
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005918 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04005919 if (ret && ret == -ENOSPC) {
5920 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005921 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005922 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005923 if (IS_ERR(trans))
5924 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005925
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005926 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04005927 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005928 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005929 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005930 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005931
5932 return ret;
5933}
5934
5935/*
5936 * This is a copy of file_update_time. We need this so we can return error on
5937 * ENOSPC for updating the inode in the case of file write and mmap writes.
5938 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005939static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005940 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005941{
Alexander Block2bc5565282012-06-15 09:49:33 +02005942 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005943 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005944
5945 if (btrfs_root_readonly(root))
5946 return -EROFS;
5947
Josef Bacike41f9412012-03-26 09:46:47 -04005948 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005949 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005950 if (flags & S_CTIME)
5951 inode->i_ctime = *now;
5952 if (flags & S_MTIME)
5953 inode->i_mtime = *now;
5954 if (flags & S_ATIME)
5955 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005956 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005957}
5958
Chris Masond352ac62008-09-29 15:18:18 -04005959/*
5960 * find the highest existing sequence number in a directory
5961 * and then set the in-memory index_cnt variable to reflect
5962 * free sequence numbers
5963 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005964static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005965{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005966 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005967 struct btrfs_key key, found_key;
5968 struct btrfs_path *path;
5969 struct extent_buffer *leaf;
5970 int ret;
5971
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005972 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005973 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005974 key.offset = (u64)-1;
5975
5976 path = btrfs_alloc_path();
5977 if (!path)
5978 return -ENOMEM;
5979
5980 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5981 if (ret < 0)
5982 goto out;
5983 /* FIXME: we should be able to handle this */
5984 if (ret == 0)
5985 goto out;
5986 ret = 0;
5987
5988 /*
5989 * MAGIC NUMBER EXPLANATION:
5990 * since we search a directory based on f_pos we have to start at 2
5991 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5992 * else has to start at 2
5993 */
5994 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005995 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005996 goto out;
5997 }
5998
5999 path->slots[0]--;
6000
6001 leaf = path->nodes[0];
6002 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6003
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006004 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006005 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006006 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006007 goto out;
6008 }
6009
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006010 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006011out:
6012 btrfs_free_path(path);
6013 return ret;
6014}
6015
Chris Masond352ac62008-09-29 15:18:18 -04006016/*
6017 * helper to find a free sequence number in a given directory. This current
6018 * code is very simple, later versions will do smarter things in the btree
6019 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006020int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006021{
6022 int ret = 0;
6023
Nikolay Borisov877574e2017-02-20 13:50:33 +02006024 if (dir->index_cnt == (u64)-1) {
6025 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006026 if (ret) {
6027 ret = btrfs_set_inode_index_count(dir);
6028 if (ret)
6029 return ret;
6030 }
Josef Bacikaec74772008-07-24 12:12:38 -04006031 }
6032
Nikolay Borisov877574e2017-02-20 13:50:33 +02006033 *index = dir->index_cnt;
6034 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006035
6036 return ret;
6037}
6038
Chris Masonb0d5d102014-09-08 13:08:51 -07006039static int btrfs_insert_inode_locked(struct inode *inode)
6040{
6041 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006042
6043 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006044 args.root = BTRFS_I(inode)->root;
6045
6046 return insert_inode_locked4(inode,
6047 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6048 btrfs_find_actor, &args);
6049}
6050
Anand Jain19aee8d2017-07-18 17:37:05 +08006051/*
6052 * Inherit flags from the parent inode.
6053 *
6054 * Currently only the compression flags and the cow flags are inherited.
6055 */
6056static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6057{
6058 unsigned int flags;
6059
6060 if (!dir)
6061 return;
6062
6063 flags = BTRFS_I(dir)->flags;
6064
6065 if (flags & BTRFS_INODE_NOCOMPRESS) {
6066 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6067 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6068 } else if (flags & BTRFS_INODE_COMPRESS) {
6069 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6070 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6071 }
6072
6073 if (flags & BTRFS_INODE_NODATACOW) {
6074 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6075 if (S_ISREG(inode->i_mode))
6076 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6077 }
6078
David Sterba7b6a2212018-03-26 18:40:21 +02006079 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006080}
6081
Chris Mason39279cc2007-06-12 06:35:45 -04006082static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6083 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006084 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006085 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006086 u64 ref_objectid, u64 objectid,
6087 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006088{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006089 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006090 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006091 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006092 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006093 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006094 struct btrfs_inode_ref *ref;
6095 struct btrfs_key key[2];
6096 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006097 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006098 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006099 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006100 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006101
Chris Mason5f39d392007-10-15 16:14:19 -04006102 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006103 if (!path)
6104 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006105
Josef Bacik11a19a92019-09-09 10:12:04 -04006106 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006107 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006108 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006109 if (!inode) {
6110 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006111 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006112 }
Chris Mason39279cc2007-06-12 06:35:45 -04006113
Li Zefan581bb052011-04-20 10:06:11 +08006114 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006115 * O_TMPFILE, set link count to 0, so that after this point,
6116 * we fill in an inode item with the correct link count.
6117 */
6118 if (!name)
6119 set_nlink(inode, 0);
6120
6121 /*
Li Zefan581bb052011-04-20 10:06:11 +08006122 * we have to initialize this early, so we can reclaim the inode
6123 * number if we fail afterwards in this function.
6124 */
6125 inode->i_ino = objectid;
6126
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006127 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006128 trace_btrfs_inode_request(dir);
6129
Nikolay Borisov877574e2017-02-20 13:50:33 +02006130 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006131 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006132 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006133 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006134 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006135 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006136 } else if (dir) {
6137 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006138 }
6139 /*
6140 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006141 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006142 * number
6143 */
6144 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006145 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006146 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006147 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006148 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006149
Josef Bacik5dc562c2012-08-17 13:14:17 -04006150 /*
6151 * We could have gotten an inode number from somebody who was fsynced
6152 * and then removed in this same transaction, so let's just set full
6153 * sync since it will be a full sync anyway and this will blow away the
6154 * old info in the log.
6155 */
6156 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6157
Chris Mason9c583092008-01-29 15:15:18 -05006158 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006159 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006160 key[0].offset = 0;
6161
Chris Mason9c583092008-01-29 15:15:18 -05006162 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006163
6164 if (name) {
6165 /*
6166 * Start new inodes with an inode_ref. This is slightly more
6167 * efficient for small numbers of hard links since they will
6168 * be packed into one item. Extended refs will kick in if we
6169 * add more hard links than can fit in the ref item.
6170 */
6171 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006172 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006173 key[1].offset = ref_objectid;
6174
6175 sizes[1] = name_len + sizeof(*ref);
6176 }
Chris Mason9c583092008-01-29 15:15:18 -05006177
Chris Masonb0d5d102014-09-08 13:08:51 -07006178 location = &BTRFS_I(inode)->location;
6179 location->objectid = objectid;
6180 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006181 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006182
6183 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006184 if (ret < 0) {
6185 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006186 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006187 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006188
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006189 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006190 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006191 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006192
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006193 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006194 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306195
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006196 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306197 inode->i_atime = inode->i_mtime;
6198 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006199 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306200
Chris Mason5f39d392007-10-15 16:14:19 -04006201 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6202 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006203 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006204 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006205 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006206
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006207 if (name) {
6208 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6209 struct btrfs_inode_ref);
6210 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6211 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6212 ptr = (unsigned long)(ref + 1);
6213 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6214 }
Chris Mason9c583092008-01-29 15:15:18 -05006215
Chris Mason5f39d392007-10-15 16:14:19 -04006216 btrfs_mark_buffer_dirty(path->nodes[0]);
6217 btrfs_free_path(path);
6218
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006219 btrfs_inherit_iflags(inode, dir);
6220
Al Viro569254b2011-07-24 17:08:40 -04006221 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006222 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006223 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006224 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006225 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6226 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006227 }
6228
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006229 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006230
6231 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006232 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006233
Alexander Block8ea05e32012-07-25 17:35:53 +02006234 btrfs_update_root_times(trans, root);
6235
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006236 ret = btrfs_inode_inherit_props(trans, inode, dir);
6237 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006238 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006239 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006240 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006241
Chris Mason39279cc2007-06-12 06:35:45 -04006242 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006243
6244fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006245 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006246fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006247 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006248 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006249 btrfs_free_path(path);
6250 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006251}
6252
Chris Masond352ac62008-09-29 15:18:18 -04006253/*
6254 * utility function to add 'inode' into 'parent_inode' with
6255 * a give name and a given sequence number.
6256 * if 'add_backref' is true, also insert a backref from the
6257 * inode to the parent directory.
6258 */
Chris Masone02119d2008-09-05 16:13:11 -04006259int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006260 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006261 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006262{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006263 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006264 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006265 struct btrfs_root *root = parent_inode->root;
6266 u64 ino = btrfs_ino(inode);
6267 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006268
Li Zefan33345d012011-04-20 10:31:50 +08006269 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006270 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006271 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006272 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006273 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006274 key.offset = 0;
6275 }
Chris Mason39279cc2007-06-12 06:35:45 -04006276
Li Zefan33345d012011-04-20 10:31:50 +08006277 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006278 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006279 root->root_key.objectid, parent_ino,
6280 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006281 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006282 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6283 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006284 }
6285
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006286 /* Nothing to clean up yet */
6287 if (ret)
6288 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006289
Lu Fengqi684572d2018-08-04 21:10:57 +08006290 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006291 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006292 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006293 goto fail_dir_item;
6294 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006295 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006296 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006297 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006298
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006299 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006300 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006301 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006302 /*
6303 * If we are replaying a log tree, we do not want to update the mtime
6304 * and ctime of the parent directory with the current time, since the
6305 * log replay procedure is responsible for setting them to their correct
6306 * values (the ones it had when the fsync was done).
6307 */
6308 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6309 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6310
6311 parent_inode->vfs_inode.i_mtime = now;
6312 parent_inode->vfs_inode.i_ctime = now;
6313 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006314 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006315 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006316 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006317 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006318
6319fail_dir_item:
6320 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6321 u64 local_index;
6322 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006323 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006324 root->root_key.objectid, parent_ino,
6325 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006326 if (err)
6327 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006328 } else if (add_backref) {
6329 u64 local_index;
6330 int err;
6331
6332 err = btrfs_del_inode_ref(trans, root, name, name_len,
6333 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006334 if (err)
6335 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006336 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006337
6338 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006339 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006340}
6341
6342static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006343 struct btrfs_inode *dir, struct dentry *dentry,
6344 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006345{
Josef Bacika1b075d2010-11-19 20:36:11 +00006346 int err = btrfs_add_link(trans, dir, inode,
6347 dentry->d_name.name, dentry->d_name.len,
6348 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006349 if (err > 0)
6350 err = -EEXIST;
6351 return err;
6352}
6353
Josef Bacik618e21d2007-07-11 10:18:17 -04006354static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006355 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006356{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006357 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006358 struct btrfs_trans_handle *trans;
6359 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006360 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006361 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006362 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006363 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006364
Josef Bacik9ed74f22009-09-11 16:12:44 -04006365 /*
6366 * 2 for inode item and ref
6367 * 2 for dir items
6368 * 1 for xattr if selinux is on
6369 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006370 trans = btrfs_start_transaction(root, 5);
6371 if (IS_ERR(trans))
6372 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006373
Nikolay Borisov543068a2020-12-07 17:32:33 +02006374 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006375 if (err)
6376 goto out_unlock;
6377
Josef Bacikaec74772008-07-24 12:12:38 -04006378 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006379 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6380 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006381 if (IS_ERR(inode)) {
6382 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006383 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006384 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006385 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006386
Casey Schauflerad19db72011-12-15 10:09:07 -05006387 /*
6388 * If the active LSM wants to access the inode during
6389 * d_instantiate it needs these. Smack checks to see
6390 * if the filesystem supports xattrs by looking at the
6391 * ops vector.
6392 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006393 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006394 init_special_inode(inode, inode->i_mode, rdev);
6395
6396 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006397 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006398 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006399
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006400 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6401 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006402 if (err)
6403 goto out_unlock;
6404
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006405 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006406 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006407
Josef Bacik618e21d2007-07-11 10:18:17 -04006408out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006409 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006410 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006411 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006412 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006413 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006414 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006415 return err;
6416}
6417
Chris Mason39279cc2007-06-12 06:35:45 -04006418static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006419 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006420{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006421 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006422 struct btrfs_trans_handle *trans;
6423 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006424 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006425 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006426 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006427 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006428
Josef Bacik9ed74f22009-09-11 16:12:44 -04006429 /*
6430 * 2 for inode item and ref
6431 * 2 for dir items
6432 * 1 for xattr if selinux is on
6433 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006434 trans = btrfs_start_transaction(root, 5);
6435 if (IS_ERR(trans))
6436 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006437
Nikolay Borisov543068a2020-12-07 17:32:33 +02006438 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006439 if (err)
6440 goto out_unlock;
6441
Josef Bacikaec74772008-07-24 12:12:38 -04006442 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006443 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6444 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006445 if (IS_ERR(inode)) {
6446 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006447 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006448 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006449 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006450 /*
6451 * If the active LSM wants to access the inode during
6452 * d_instantiate it needs these. Smack checks to see
6453 * if the filesystem supports xattrs by looking at the
6454 * ops vector.
6455 */
6456 inode->i_fop = &btrfs_file_operations;
6457 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006458 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006459
6460 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6461 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006462 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006463
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006464 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006465 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006466 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006467
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006468 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6469 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006470 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006471 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006472
Al Viro1e2e5472018-05-04 08:23:01 -04006473 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006474
Chris Mason39279cc2007-06-12 06:35:45 -04006475out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006476 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006477 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006478 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006479 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006480 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006481 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006482 return err;
6483}
6484
6485static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6486 struct dentry *dentry)
6487{
Filipe Manana271dba452016-01-05 16:24:05 +00006488 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006489 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006490 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006491 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006492 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006493 int err;
6494 int drop_inode = 0;
6495
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006496 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006497 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006498 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006499
Mark Fashehf1863732012-08-08 11:32:27 -07006500 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006501 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006502
Nikolay Borisov877574e2017-02-20 13:50:33 +02006503 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006504 if (err)
6505 goto fail;
6506
Yan, Zhenga22285a2010-05-16 10:48:46 -04006507 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006508 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006509 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006510 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006511 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006512 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006513 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006514 if (IS_ERR(trans)) {
6515 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006516 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006517 goto fail;
6518 }
Chris Mason5f39d392007-10-15 16:14:19 -04006519
Miao Xie67de1172013-12-26 13:07:06 +08006520 /* There are several dir indexes for this inode, clear the cache. */
6521 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006522 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006523 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006524 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006525 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006526 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006527
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006528 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6529 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006530
Yan, Zhenga5719522009-09-24 09:17:31 -04006531 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006532 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006533 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006534 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006535
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006536 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006537 if (err)
6538 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006539 if (inode->i_nlink == 1) {
6540 /*
6541 * If new hard link count is 1, it's a file created
6542 * with open(2) O_TMPFILE flag.
6543 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006544 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006545 if (err)
6546 goto fail;
6547 }
Al Viro08c422c2011-12-23 07:58:13 -05006548 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006549 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006550 }
Chris Mason39279cc2007-06-12 06:35:45 -04006551
Chris Mason1832a6d2007-12-21 16:27:21 -05006552fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006553 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006554 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006555 if (drop_inode) {
6556 inode_dec_link_count(inode);
6557 iput(inode);
6558 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006559 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006560 return err;
6561}
6562
Al Viro18bb1db2011-07-26 01:41:39 -04006563static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006564{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006565 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006566 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006567 struct btrfs_trans_handle *trans;
6568 struct btrfs_root *root = BTRFS_I(dir)->root;
6569 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006570 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006571 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006572
Josef Bacik9ed74f22009-09-11 16:12:44 -04006573 /*
6574 * 2 items for inode and ref
6575 * 2 items for dir items
6576 * 1 for xattr if selinux is on
6577 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006578 trans = btrfs_start_transaction(root, 5);
6579 if (IS_ERR(trans))
6580 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006581
Nikolay Borisov543068a2020-12-07 17:32:33 +02006582 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006583 if (err)
6584 goto out_fail;
6585
Josef Bacikaec74772008-07-24 12:12:38 -04006586 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006587 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6588 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006589 if (IS_ERR(inode)) {
6590 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006591 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006592 goto out_fail;
6593 }
Chris Mason5f39d392007-10-15 16:14:19 -04006594
Chris Masonb0d5d102014-09-08 13:08:51 -07006595 /* these must be set before we unlock the inode */
6596 inode->i_op = &btrfs_dir_inode_operations;
6597 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006598
Eric Paris2a7dba32011-02-01 11:05:39 -05006599 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006600 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006601 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006602
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006603 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006604 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006605 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006606 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006607
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006608 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6609 dentry->d_name.name,
6610 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006611 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006612 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006613
Al Viro1e2e5472018-05-04 08:23:01 -04006614 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006615
6616out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006617 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006618 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006619 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006620 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006621 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006622 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006623 return err;
6624}
6625
Chris Masonc8b97812008-10-29 14:49:59 -04006626static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006627 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006628 size_t pg_offset, u64 extent_offset,
6629 struct btrfs_file_extent_item *item)
6630{
6631 int ret;
6632 struct extent_buffer *leaf = path->nodes[0];
6633 char *tmp;
6634 size_t max_size;
6635 unsigned long inline_size;
6636 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006637 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006638
6639 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006640 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006641 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6642 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006643 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006644 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006645 if (!tmp)
6646 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006647 ptr = btrfs_file_extent_inline_start(item);
6648
6649 read_extent_buffer(leaf, tmp, ptr, inline_size);
6650
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006651 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006652 ret = btrfs_decompress(compress_type, tmp, page,
6653 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006654
6655 /*
6656 * decompression code contains a memset to fill in any space between the end
6657 * of the uncompressed data and the end of max_size in case the decompressed
6658 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6659 * the end of an inline extent and the beginning of the next block, so we
6660 * cover that region here.
6661 */
6662
6663 if (max_size + pg_offset < PAGE_SIZE) {
6664 char *map = kmap(page);
6665 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6666 kunmap(page);
6667 }
Chris Masonc8b97812008-10-29 14:49:59 -04006668 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006669 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006670}
6671
Omar Sandoval39b07b52019-12-02 17:34:23 -08006672/**
6673 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6674 * @inode: file to search in
6675 * @page: page to read extent data into if the extent is inline
6676 * @pg_offset: offset into @page to copy to
6677 * @start: file offset
6678 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006679 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006680 * This returns the first &struct extent_map which overlaps with the given
6681 * range, reading it from the B-tree and caching it if necessary. Note that
6682 * there may be more extents which overlap the given range after the returned
6683 * extent_map.
6684 *
6685 * If @page is not NULL and the extent is inline, this also reads the extent
6686 * data directly into the page and marks the extent up to date in the io_tree.
6687 *
6688 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006689 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006690struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006691 struct page *page, size_t pg_offset,
6692 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006693{
David Sterba3ffbd682018-06-29 10:56:42 +02006694 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006695 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006696 u64 extent_start = 0;
6697 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006698 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006699 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006700 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006701 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006702 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006703 struct extent_buffer *leaf;
6704 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006705 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006706 struct extent_map_tree *em_tree = &inode->extent_tree;
6707 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006708
Chris Mason890871b2009-09-02 16:24:52 -04006709 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006710 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006711 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006712
Chris Masona52d9a82007-08-27 16:49:44 -04006713 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006714 if (em->start > start || em->start + em->len <= start)
6715 free_extent_map(em);
6716 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006717 free_extent_map(em);
6718 else
6719 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006720 }
David Sterba172ddd62011-04-21 00:48:27 +02006721 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006722 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006723 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006724 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006725 }
Chris Masond1310b22008-01-24 16:13:08 -05006726 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006727 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006728 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006729 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006730
Liu Bobee6ec82018-08-17 05:05:28 +08006731 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006732 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006733 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006734 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006735 }
6736
Liu Bobee6ec82018-08-17 05:05:28 +08006737 /* Chances are we'll be called again, so go ahead and do readahead */
6738 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04006739
6740 /*
6741 * The same explanation in load_free_space_cache applies here as well,
6742 * we only read when we're loading the free space cache, and at that
6743 * point the commit_root has everything we need.
6744 */
6745 if (btrfs_is_free_space_inode(inode)) {
6746 path->search_commit_root = 1;
6747 path->skip_locking = 1;
6748 }
Josef Bacik51899412020-08-20 11:46:01 -04006749
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006750 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006751 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006752 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006753 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006754 if (path->slots[0] == 0)
6755 goto not_found;
6756 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006757 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006758 }
6759
Chris Mason5f39d392007-10-15 16:14:19 -04006760 leaf = path->nodes[0];
6761 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006762 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006763 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006764 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006765 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006766 /*
6767 * If we backup past the first extent we want to move forward
6768 * and see if there is an extent in front of us, otherwise we'll
6769 * say there is a hole for our whole search range which can
6770 * cause problems.
6771 */
6772 extent_end = start;
6773 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006774 }
6775
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006776 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006777 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006778 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006779 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6780 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006781 /* Only regular file could have regular/prealloc extent */
6782 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006783 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006784 btrfs_crit(fs_info,
6785 "regular/prealloc extent found for non-regular inode %llu",
6786 btrfs_ino(inode));
6787 goto out;
6788 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006789 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6790 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006791 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006792 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6793 path->slots[0],
6794 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006795 }
Josef Bacik25a50342013-10-14 12:08:38 -04006796next:
Yan Zheng9036c102008-10-30 14:19:41 -04006797 if (start >= extent_end) {
6798 path->slots[0]++;
6799 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6800 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006801 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006802 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006803 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006804 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006805
Yan Zheng9036c102008-10-30 14:19:41 -04006806 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006807 }
Yan Zheng9036c102008-10-30 14:19:41 -04006808 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6809 if (found_key.objectid != objectid ||
6810 found_key.type != BTRFS_EXTENT_DATA_KEY)
6811 goto not_found;
6812 if (start + len <= found_key.offset)
6813 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006814 if (start > found_key.offset)
6815 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006816
6817 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006818 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006819 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006820 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006821 em->block_start = EXTENT_MAP_HOLE;
6822 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006823 }
6824
Omar Sandoval39b07b52019-12-02 17:34:23 -08006825 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006826
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006827 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6828 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006829 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006830 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006831 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006832 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006833 size_t size;
6834 size_t extent_offset;
6835 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006836
Omar Sandoval39b07b52019-12-02 17:34:23 -08006837 if (!page)
Yan689f9342007-10-29 11:41:07 -04006838 goto out;
Yan689f9342007-10-29 11:41:07 -04006839
Qu Wenruoe41ca582018-06-06 15:41:49 +08006840 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006841 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006842 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6843 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006844 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006845 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006846 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006847 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006848 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006849
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006850 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006851 if (btrfs_file_extent_compression(leaf, item) !=
6852 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006853 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006854 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006855 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04006856 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04006857 } else {
6858 map = kmap(page);
6859 read_extent_buffer(leaf, map + pg_offset, ptr,
6860 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006861 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006862 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006863 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006864 copy_size);
6865 }
Chris Masonc8b97812008-10-29 14:49:59 -04006866 kunmap(page);
6867 }
Chris Mason179e29e2007-11-01 11:28:41 -04006868 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006869 }
Chris Masond1310b22008-01-24 16:13:08 -05006870 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006871 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006872 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006873 }
6874not_found:
6875 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006876 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006877 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006878 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006879insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006880 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02006881 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006882 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006883 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006884 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6885 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006886 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04006887 goto out;
6888 }
Chris Masond1310b22008-01-24 16:13:08 -05006889
Chris Mason890871b2009-09-02 16:24:52 -04006890 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006891 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006892 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006893out:
Liu Boc6414282018-08-23 07:36:17 +08006894 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006895
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006896 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006897
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006898 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04006899 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006900 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04006901 }
6902 return em;
6903}
6904
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006905struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006906 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006907{
6908 struct extent_map *em;
6909 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006910 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006911 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006912 u64 delalloc_len;
6913 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006914 int err = 0;
6915
Omar Sandoval39b07b52019-12-02 17:34:23 -08006916 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006917 if (IS_ERR(em))
6918 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006919 /*
6920 * If our em maps to:
6921 * - a hole or
6922 * - a pre-alloc extent,
6923 * there might actually be delalloc bytes behind it.
6924 */
6925 if (em->block_start != EXTENT_MAP_HOLE &&
6926 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6927 return em;
6928 else
6929 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006930
6931 /* check to see if we've wrapped (len == -1 or similar) */
6932 end = start + len;
6933 if (end < start)
6934 end = (u64)-1;
6935 else
6936 end -= 1;
6937
6938 em = NULL;
6939
6940 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006941 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006942 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006943 delalloc_end = delalloc_start + delalloc_len;
6944 if (delalloc_end < delalloc_start)
6945 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006946
6947 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006948 * We didn't find anything useful, return the original results from
6949 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006950 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006951 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006952 em = hole_em;
6953 hole_em = NULL;
6954 goto out;
6955 }
6956
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006957 /*
6958 * Adjust the delalloc_start to make sure it doesn't go backwards from
6959 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006960 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006961 delalloc_start = max(start, delalloc_start);
6962 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006963
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006964 if (delalloc_len > 0) {
6965 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006966 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006967 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006968
David Sterba172ddd62011-04-21 00:48:27 +02006969 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006970 if (!em) {
6971 err = -ENOMEM;
6972 goto out;
6973 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006974
6975 ASSERT(hole_em);
6976 /*
6977 * When btrfs_get_extent can't find anything it returns one
6978 * huge hole
6979 *
6980 * Make sure what it found really fits our range, and adjust to
6981 * make sure it is based on the start from the caller
6982 */
6983 if (hole_end <= start || hole_em->start > end) {
6984 free_extent_map(hole_em);
6985 hole_em = NULL;
6986 } else {
6987 hole_start = max(hole_em->start, start);
6988 hole_len = hole_end - hole_start;
6989 }
6990
6991 if (hole_em && delalloc_start > hole_start) {
6992 /*
6993 * Our hole starts before our delalloc, so we have to
6994 * return just the parts of the hole that go until the
6995 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006996 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006997 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05006998 em->start = hole_start;
6999 em->orig_start = hole_start;
7000 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007001 * Don't adjust block start at all, it is fixed at
7002 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007003 */
7004 em->block_start = hole_em->block_start;
7005 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007006 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7007 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007008 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007009 /*
7010 * Hole is out of passed range or it starts after
7011 * delalloc range
7012 */
7013 em->start = delalloc_start;
7014 em->len = delalloc_len;
7015 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007016 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007017 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007018 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007019 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007020 return hole_em;
7021 }
7022out:
7023
7024 free_extent_map(hole_em);
7025 if (err) {
7026 free_extent_map(em);
7027 return ERR_PTR(err);
7028 }
7029 return em;
7030}
7031
Nikolay Borisov64f54182020-06-03 08:55:31 +03007032static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007033 const u64 start,
7034 const u64 len,
7035 const u64 orig_start,
7036 const u64 block_start,
7037 const u64 block_len,
7038 const u64 orig_block_len,
7039 const u64 ram_bytes,
7040 const int type)
7041{
7042 struct extent_map *em = NULL;
7043 int ret;
7044
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007045 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007046 em = create_io_em(inode, start, len, orig_start, block_start,
7047 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007048 BTRFS_COMPRESS_NONE, /* compress_type */
7049 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007050 if (IS_ERR(em))
7051 goto out;
7052 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007053 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7054 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007055 if (ret) {
7056 if (em) {
7057 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007058 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007059 }
7060 em = ERR_PTR(ret);
7061 }
7062 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007063
7064 return em;
7065}
7066
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007067static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007068 u64 start, u64 len)
7069{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007070 struct btrfs_root *root = inode->root;
7071 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007072 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007073 struct btrfs_key ins;
7074 u64 alloc_hint;
7075 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007076
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007077 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007078 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007079 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007080 if (ret)
7081 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007082
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007083 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007084 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007085 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007086 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007087 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007088 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7089 1);
Josef Bacik00361582013-08-14 14:02:47 -04007090
Josef Bacik4b46fce2010-05-23 11:00:55 -04007091 return em;
7092}
7093
Chris Mason46bfbb52010-05-26 11:04:10 -04007094/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007095 * Check if we can do nocow write into the range [@offset, @offset + @len)
7096 *
7097 * @offset: File offset
7098 * @len: The length to write, will be updated to the nocow writeable
7099 * range
7100 * @orig_start: (optional) Return the original file offset of the file extent
7101 * @orig_len: (optional) Return the original on-disk length of the file extent
7102 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007103 * @strict: if true, omit optimizations that might force us into unnecessary
7104 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007105 *
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007106 * Return:
7107 * >0 and update @len if we can do nocow write
7108 * 0 if we can't do nocow write
7109 * <0 if error happened
7110 *
7111 * NOTE: This only checks the file extents, caller is responsible to wait for
7112 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007113 */
Josef Bacik00361582013-08-14 14:02:47 -04007114noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007115 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007116 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007117{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007118 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007119 struct btrfs_path *path;
7120 int ret;
7121 struct extent_buffer *leaf;
7122 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007123 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007124 struct btrfs_file_extent_item *fi;
7125 struct btrfs_key key;
7126 u64 disk_bytenr;
7127 u64 backref_offset;
7128 u64 extent_end;
7129 u64 num_bytes;
7130 int slot;
7131 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007132 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007133
Chris Mason46bfbb52010-05-26 11:04:10 -04007134 path = btrfs_alloc_path();
7135 if (!path)
7136 return -ENOMEM;
7137
David Sterbaf85b7372017-01-20 14:54:07 +01007138 ret = btrfs_lookup_file_extent(NULL, root, path,
7139 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007140 if (ret < 0)
7141 goto out;
7142
7143 slot = path->slots[0];
7144 if (ret == 1) {
7145 if (slot == 0) {
7146 /* can't find the item, must cow */
7147 ret = 0;
7148 goto out;
7149 }
7150 slot--;
7151 }
7152 ret = 0;
7153 leaf = path->nodes[0];
7154 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007155 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007156 key.type != BTRFS_EXTENT_DATA_KEY) {
7157 /* not our file or wrong item type, must cow */
7158 goto out;
7159 }
7160
7161 if (key.offset > offset) {
7162 /* Wrong offset, must cow */
7163 goto out;
7164 }
7165
7166 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7167 found_type = btrfs_file_extent_type(leaf, fi);
7168 if (found_type != BTRFS_FILE_EXTENT_REG &&
7169 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7170 /* not a regular extent, must cow */
7171 goto out;
7172 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007173
7174 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7175 goto out;
7176
Miao Xiee77751a2013-12-27 21:11:50 +08007177 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7178 if (extent_end <= offset)
7179 goto out;
7180
Chris Mason46bfbb52010-05-26 11:04:10 -04007181 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007182 if (disk_bytenr == 0)
7183 goto out;
7184
7185 if (btrfs_file_extent_compression(leaf, fi) ||
7186 btrfs_file_extent_encryption(leaf, fi) ||
7187 btrfs_file_extent_other_encoding(leaf, fi))
7188 goto out;
7189
Ethan Lien78d42952018-05-17 14:58:29 +08007190 /*
7191 * Do the same check as in btrfs_cross_ref_exist but without the
7192 * unnecessary search.
7193 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007194 if (!strict &&
7195 (btrfs_file_extent_generation(leaf, fi) <=
7196 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007197 goto out;
7198
Chris Mason46bfbb52010-05-26 11:04:10 -04007199 backref_offset = btrfs_file_extent_offset(leaf, fi);
7200
Josef Bacik7ee9e442013-06-21 16:37:03 -04007201 if (orig_start) {
7202 *orig_start = key.offset - backref_offset;
7203 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7204 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7205 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007206
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007207 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007208 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007209
7210 num_bytes = min(offset + *len, extent_end) - offset;
7211 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7212 u64 range_end;
7213
Jeff Mahoneyda170662016-06-15 09:22:56 -04007214 range_end = round_up(offset + num_bytes,
7215 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007216 ret = test_range_bit(io_tree, offset, range_end,
7217 EXTENT_DELALLOC, 0, NULL);
7218 if (ret) {
7219 ret = -EAGAIN;
7220 goto out;
7221 }
7222 }
7223
Josef Bacik1bda19e2013-10-18 12:10:36 -04007224 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007225
7226 /*
7227 * look for other files referencing this extent, if we
7228 * find any we must cow
7229 */
Josef Bacik00361582013-08-14 14:02:47 -04007230
Liu Boe4c3b2d2017-01-30 12:25:28 -08007231 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007232 key.offset - backref_offset, disk_bytenr,
7233 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007234 if (ret) {
7235 ret = 0;
7236 goto out;
7237 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007238
7239 /*
7240 * adjust disk_bytenr and num_bytes to cover just the bytes
7241 * in this extent we are about to write. If there
7242 * are any csums in that range we have to cow in order
7243 * to keep the csums correct
7244 */
7245 disk_bytenr += backref_offset;
7246 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007247 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7248 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007249 /*
7250 * all of the above have passed, it is safe to overwrite this extent
7251 * without cow
7252 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007253 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007254 ret = 1;
7255out:
7256 btrfs_free_path(path);
7257 return ret;
7258}
7259
Josef Bacikeb838e72012-07-31 16:28:48 -04007260static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007261 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007262{
7263 struct btrfs_ordered_extent *ordered;
7264 int ret = 0;
7265
7266 while (1) {
7267 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007268 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007269 /*
7270 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007271 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007272 * extents in this range.
7273 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007274 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007275 lockend - lockstart + 1);
7276
7277 /*
7278 * We need to make sure there are no buffered pages in this
7279 * range either, we could have raced between the invalidate in
7280 * generic_file_direct_write and locking the extent. The
7281 * invalidate needs to happen so that reads after a write do not
7282 * get stale data.
7283 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007284 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007285 (!writing || !filemap_range_has_page(inode->i_mapping,
7286 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007287 break;
7288
7289 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007290 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007291
7292 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007293 /*
7294 * If we are doing a DIO read and the ordered extent we
7295 * found is for a buffered write, we can not wait for it
7296 * to complete and retry, because if we do so we can
7297 * deadlock with concurrent buffered writes on page
7298 * locks. This happens only if our DIO read covers more
7299 * than one extent map, if at this point has already
7300 * created an ordered extent for a previous extent map
7301 * and locked its range in the inode's io tree, and a
7302 * concurrent write against that previous extent map's
7303 * range and this range started (we unlock the ranges
7304 * in the io tree only when the bios complete and
7305 * buffered writes always lock pages before attempting
7306 * to lock range in the io tree).
7307 */
7308 if (writing ||
7309 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007310 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007311 else
7312 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007313 btrfs_put_ordered_extent(ordered);
7314 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007315 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007316 * We could trigger writeback for this range (and wait
7317 * for it to complete) and then invalidate the pages for
7318 * this range (through invalidate_inode_pages2_range()),
7319 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007320 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007321 * triggered a readahead) on a page lock due to an
7322 * ordered dio extent we created before but did not have
7323 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007324 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007325 * ordered extent to complete while holding a lock on
7326 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007327 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007328 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007329 }
7330
Filipe Mananaade77022016-02-18 14:28:55 +00007331 if (ret)
7332 break;
7333
Josef Bacikeb838e72012-07-31 16:28:48 -04007334 cond_resched();
7335 }
7336
7337 return ret;
7338}
7339
Liu Bo6f9994d2017-01-31 07:50:22 -08007340/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007341static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7342 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007343 u64 block_len, u64 orig_block_len,
7344 u64 ram_bytes, int compress_type,
7345 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007346{
7347 struct extent_map_tree *em_tree;
7348 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007349 int ret;
7350
Liu Bo6f9994d2017-01-31 07:50:22 -08007351 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7352 type == BTRFS_ORDERED_COMPRESSED ||
7353 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007354 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007355
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007356 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007357 em = alloc_extent_map();
7358 if (!em)
7359 return ERR_PTR(-ENOMEM);
7360
7361 em->start = start;
7362 em->orig_start = orig_start;
7363 em->len = len;
7364 em->block_len = block_len;
7365 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007366 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007367 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007368 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007369 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007370 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007371 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007372 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007373 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7374 em->compress_type = compress_type;
7375 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007376
7377 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007378 btrfs_drop_extent_cache(inode, em->start,
7379 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007380 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007381 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007382 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007383 /*
7384 * The caller has taken lock_extent(), who could race with us
7385 * to add em?
7386 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007387 } while (ret == -EEXIST);
7388
7389 if (ret) {
7390 free_extent_map(em);
7391 return ERR_PTR(ret);
7392 }
7393
Liu Bo6f9994d2017-01-31 07:50:22 -08007394 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007395 return em;
7396}
7397
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007398
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007399static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007400 struct inode *inode,
7401 struct btrfs_dio_data *dio_data,
7402 u64 start, u64 len)
7403{
7404 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7405 struct extent_map *em = *map;
7406 int ret = 0;
7407
7408 /*
7409 * We don't allocate a new extent in the following cases
7410 *
7411 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7412 * existing extent.
7413 * 2) The extent is marked as PREALLOC. We're good to go here and can
7414 * just use the extent.
7415 *
7416 */
7417 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7418 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7419 em->block_start != EXTENT_MAP_HOLE)) {
7420 int type;
7421 u64 block_start, orig_start, orig_block_len, ram_bytes;
7422
7423 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7424 type = BTRFS_ORDERED_PREALLOC;
7425 else
7426 type = BTRFS_ORDERED_NOCOW;
7427 len = min(len, em->len - (start - em->start));
7428 block_start = em->block_start + (start - em->start);
7429
7430 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007431 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007432 btrfs_inc_nocow_writers(fs_info, block_start)) {
7433 struct extent_map *em2;
7434
Nikolay Borisov64f54182020-06-03 08:55:31 +03007435 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007436 orig_start, block_start,
7437 len, orig_block_len,
7438 ram_bytes, type);
7439 btrfs_dec_nocow_writers(fs_info, block_start);
7440 if (type == BTRFS_ORDERED_PREALLOC) {
7441 free_extent_map(em);
7442 *map = em = em2;
7443 }
7444
7445 if (em2 && IS_ERR(em2)) {
7446 ret = PTR_ERR(em2);
7447 goto out;
7448 }
7449 /*
7450 * For inode marked NODATACOW or extent marked PREALLOC,
7451 * use the existing or preallocated extent, so does not
7452 * need to adjust btrfs_space_info's bytes_may_use.
7453 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007454 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007455 goto skip_cow;
7456 }
7457 }
7458
7459 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007460 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007461 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007462 if (IS_ERR(em)) {
7463 ret = PTR_ERR(em);
7464 goto out;
7465 }
7466
7467 len = min(len, em->len - (start - em->start));
7468
7469skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007470 /*
7471 * Need to update the i_size under the extent lock so buffered
7472 * readers will get the updated i_size when we unlock.
7473 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007474 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007475 i_size_write(inode, start + len);
7476
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007477 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007478out:
7479 return ret;
7480}
7481
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007482static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7483 loff_t length, unsigned int flags, struct iomap *iomap,
7484 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007485{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007486 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007487 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007488 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307489 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007490 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007491 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007492 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007493 u64 len = length;
7494 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007495
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007496 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007497 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007498
Josef Bacikc3298612012-08-03 16:49:19 -04007499 lockstart = start;
7500 lockend = start + len - 1;
7501
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007502 /*
7503 * The generic stuff only does filemap_write_and_wait_range, which
7504 * isn't enough if we've written compressed pages to this area, so we
7505 * need to flush the dirty pages again to make absolutely sure that any
7506 * outstanding dirty pages are on disk.
7507 */
7508 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7509 &BTRFS_I(inode)->runtime_flags)) {
7510 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7511 start + length - 1);
7512 if (ret)
7513 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007514 }
7515
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007516 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7517 if (!dio_data)
7518 return -ENOMEM;
7519
7520 dio_data->length = length;
7521 if (write) {
7522 dio_data->reserve = round_up(length, fs_info->sectorsize);
7523 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7524 &dio_data->data_reserved,
7525 start, dio_data->reserve);
7526 if (ret) {
7527 extent_changeset_free(dio_data->data_reserved);
7528 kfree(dio_data);
7529 return ret;
7530 }
7531 }
7532 iomap->private = dio_data;
7533
7534
Josef Bacikeb838e72012-07-31 16:28:48 -04007535 /*
7536 * If this errors out it's because we couldn't invalidate pagecache for
7537 * this range and we need to fallback to buffered.
7538 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007539 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007540 ret = -ENOTBLK;
7541 goto err;
7542 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007543
Omar Sandoval39b07b52019-12-02 17:34:23 -08007544 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007545 if (IS_ERR(em)) {
7546 ret = PTR_ERR(em);
7547 goto unlock_err;
7548 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007549
7550 /*
7551 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7552 * io. INLINE is special, and we could probably kludge it in here, but
7553 * it's still buffered so for safety lets just fall back to the generic
7554 * buffered path.
7555 *
7556 * For COMPRESSED we _have_ to read the entire extent in so we can
7557 * decompress it, so there will be buffering required no matter what we
7558 * do, so go ahead and fallback to buffered.
7559 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007560 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007561 * to buffered IO. Don't blame me, this is the price we pay for using
7562 * the generic code.
7563 */
7564 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7565 em->block_start == EXTENT_MAP_INLINE) {
7566 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007567 ret = -ENOTBLK;
7568 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007569 }
7570
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007571 len = min(len, em->len - (start - em->start));
7572 if (write) {
7573 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7574 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007575 if (ret < 0)
7576 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007577 unlock_extents = true;
7578 /* Recalc len in case the new em is smaller than requested */
7579 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007580 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007581 /*
7582 * We need to unlock only the end area that we aren't using.
7583 * The rest is going to be unlocked by the endio routine.
7584 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007585 lockstart = start + len;
7586 if (lockstart < lockend)
7587 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007588 }
7589
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007590 if (unlock_extents)
7591 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7592 lockstart, lockend, &cached_state);
7593 else
7594 free_extent_state(cached_state);
7595
7596 /*
7597 * Translate extent map information to iomap.
7598 * We trim the extents (and move the addr) even though iomap code does
7599 * that, since we have locked only the parts we are performing I/O in.
7600 */
7601 if ((em->block_start == EXTENT_MAP_HOLE) ||
7602 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7603 iomap->addr = IOMAP_NULL_ADDR;
7604 iomap->type = IOMAP_HOLE;
7605 } else {
7606 iomap->addr = em->block_start + (start - em->start);
7607 iomap->type = IOMAP_MAPPED;
7608 }
7609 iomap->offset = start;
7610 iomap->bdev = fs_info->fs_devices->latest_bdev;
7611 iomap->length = len;
7612
Josef Bacik4b46fce2010-05-23 11:00:55 -04007613 free_extent_map(em);
7614
7615 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007616
7617unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007618 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7619 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007620err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007621 if (dio_data) {
7622 btrfs_delalloc_release_space(BTRFS_I(inode),
7623 dio_data->data_reserved, start,
7624 dio_data->reserve, true);
7625 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7626 extent_changeset_free(dio_data->data_reserved);
7627 kfree(dio_data);
7628 }
7629 return ret;
7630}
7631
7632static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7633 ssize_t written, unsigned int flags, struct iomap *iomap)
7634{
7635 int ret = 0;
7636 struct btrfs_dio_data *dio_data = iomap->private;
7637 size_t submitted = dio_data->submitted;
7638 const bool write = !!(flags & IOMAP_WRITE);
7639
7640 if (!write && (iomap->type == IOMAP_HOLE)) {
7641 /* If reading from a hole, unlock and return */
7642 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7643 goto out;
7644 }
7645
7646 if (submitted < length) {
7647 pos += submitted;
7648 length -= submitted;
7649 if (write)
7650 __endio_write_update_ordered(BTRFS_I(inode), pos,
7651 length, false);
7652 else
7653 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7654 pos + length - 1);
7655 ret = -ENOTBLK;
7656 }
7657
7658 if (write) {
7659 if (dio_data->reserve)
7660 btrfs_delalloc_release_space(BTRFS_I(inode),
7661 dio_data->data_reserved, pos,
7662 dio_data->reserve, true);
7663 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7664 extent_changeset_free(dio_data->data_reserved);
7665 }
7666out:
7667 kfree(dio_data);
7668 iomap->private = NULL;
7669
Josef Bacikeb838e72012-07-31 16:28:48 -04007670 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007671}
7672
Omar Sandoval769b4f22020-04-16 14:46:22 -07007673static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7674{
7675 /*
7676 * This implies a barrier so that stores to dio_bio->bi_status before
7677 * this and loads of dio_bio->bi_status after this are fully ordered.
7678 */
7679 if (!refcount_dec_and_test(&dip->refs))
7680 return;
7681
7682 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007683 __endio_write_update_ordered(BTRFS_I(dip->inode),
7684 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007685 dip->bytes,
7686 !dip->dio_bio->bi_status);
7687 } else {
7688 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7689 dip->logical_offset,
7690 dip->logical_offset + dip->bytes - 1);
7691 }
7692
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007693 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007694 kfree(dip);
7695}
7696
Omar Sandoval77d5d682020-04-16 14:46:25 -07007697static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7698 int mirror_num,
7699 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007700{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007701 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007702 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007703 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007704
Mike Christie37226b22016-06-05 14:31:52 -05007705 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007706
Omar Sandoval5c047a62020-04-16 14:46:24 -07007707 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007708 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007709 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007710
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007711 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007712 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007713 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007714 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007715 return ret;
7716}
7717
Omar Sandoval769b4f22020-04-16 14:46:22 -07007718static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7719 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007720 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007721{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007722 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7723 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007724 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007725 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7726 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007727 struct bio_vec bvec;
7728 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007729 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007730 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007731 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007732
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007733 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7734 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307735
Liu Bo17347ce2017-05-15 15:33:27 -07007736 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7737 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007738 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007739 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007740 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007741 (!csum || !check_data_csum(inode, io_bio,
7742 bio_offset, bvec.bv_page, pgoff))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007743 clean_io_failure(fs_info, failure_tree, io_tree,
7744 start, bvec.bv_page,
7745 btrfs_ino(BTRFS_I(inode)),
7746 pgoff);
7747 } else {
7748 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007749
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007750 ASSERT((start - io_bio->logical) < UINT_MAX);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007751 status = btrfs_submit_read_repair(inode,
7752 &io_bio->bio,
7753 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007754 bvec.bv_page, pgoff,
7755 start,
7756 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007757 io_bio->mirror_num,
7758 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007759 if (status)
7760 err = status;
7761 }
7762 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007763 ASSERT(bio_offset + sectorsize > bio_offset);
7764 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307765 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307766 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007767 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007768 return err;
7769}
7770
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007771static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007772 const u64 offset, const u64 bytes,
7773 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007774{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007775 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007776 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007777 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007778 u64 ordered_offset = offset;
7779 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007780 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007782 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007783 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007784 else
Qu Wenruo524272602017-03-08 10:25:52 +08007785 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007786
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007787 while (ordered_offset < offset + bytes) {
7788 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007789 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007790 &ordered_offset,
7791 ordered_bytes,
7792 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007793 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7794 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007795 btrfs_queue_work(wq, &ordered->work);
7796 }
Qu Wenruo58f74b22020-12-22 13:59:24 +08007797
7798 /* No ordered extent found in the range, exit */
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007799 if (ordered_offset == last_offset)
7800 return;
7801 /*
7802 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007803 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007804 */
7805 if (ordered_offset < offset + bytes) {
7806 ordered_bytes = offset + bytes - ordered_offset;
7807 ordered = NULL;
7808 }
Chris Mason163cf092010-11-28 19:56:33 -05007809 }
Filipe Manana14543772015-11-24 16:23:54 +00007810}
7811
Qu Wenruo8896a082020-10-21 14:24:53 +08007812static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08007813 struct bio *bio,
7814 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007815{
Qu Wenruo1941b642020-12-02 14:47:57 +08007816 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007817}
7818
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007819static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007820{
7821 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007822 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007823
Miao Xie8b110e32014-09-12 18:44:03 +08007824 if (err)
7825 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007826 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007827 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01007828 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08007829 bio->bi_iter.bi_size, err);
7830
Omar Sandoval769b4f22020-04-16 14:46:22 -07007831 if (bio_op(bio) == REQ_OP_READ) {
7832 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007833 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007834 }
7835
Omar Sandoval769b4f22020-04-16 14:46:22 -07007836 if (err)
7837 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007838
Miao Xiee65e1532010-11-22 03:04:43 +00007839 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007840 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007841}
7842
David Sterbad0ee3932018-03-08 14:35:48 +01007843static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7844 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007845{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007846 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007847 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007848 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007849 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007850
Liu Bo4c274bc2017-11-01 17:19:27 -06007851 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007852 if (async_submit)
7853 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7854
Josef Bacik5fd02042012-05-02 14:00:54 -04007855 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007856 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007857 if (ret)
7858 goto err;
7859 }
Miao Xiee65e1532010-11-22 03:04:43 +00007860
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007861 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007862 goto map;
7863
7864 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08007865 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, 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;
David Sterba1201b582020-11-26 15:41:27 +01007919 dip->disk_bytenr = 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;
Michal Rostecki42034312021-01-27 14:57:27 +01007939 u64 logical;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307940 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007941 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007942 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007943 struct btrfs_dio_data *dio_data = iomap->private;
Michal Rostecki42034312021-01-27 14:57:27 +01007944 struct extent_map *em = NULL;
Miao Xiee65e1532010-11-22 03:04:43 +00007945
Omar Sandovalc36cac22020-04-16 14:46:13 -07007946 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7947 if (!dip) {
7948 if (!write) {
7949 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7950 file_offset + dio_bio->bi_iter.bi_size - 1);
7951 }
7952 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007953 bio_endio(dio_bio);
7954 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04007955 }
7956
Josef Bacik334c16d2020-10-16 11:29:14 -04007957 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07007958 /*
7959 * Load the csums up front to reduce csum tree searches and
7960 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04007961 *
7962 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07007963 */
Qu Wenruo62751932020-12-02 14:48:06 +08007964 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07007965 if (status != BLK_STS_OK)
7966 goto out_err;
7967 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007968
Omar Sandoval769b4f22020-04-16 14:46:22 -07007969 start_sector = dio_bio->bi_iter.bi_sector;
7970 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007971
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007972 do {
Michal Rostecki42034312021-01-27 14:57:27 +01007973 logical = start_sector << 9;
7974 em = btrfs_get_chunk_map(fs_info, logical, submit_len);
7975 if (IS_ERR(em)) {
7976 status = errno_to_blk_status(PTR_ERR(em));
7977 em = NULL;
7978 goto out_err_em;
7979 }
7980 ret = btrfs_get_io_geometry(fs_info, em, btrfs_op(dio_bio),
7981 logical, submit_len, &geom);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007982 if (ret) {
7983 status = errno_to_blk_status(ret);
Michal Rostecki42034312021-01-27 14:57:27 +01007984 goto out_err_em;
Omar Sandoval769b4f22020-04-16 14:46:22 -07007985 }
7986 ASSERT(geom.len <= INT_MAX);
7987
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007988 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007989
Liu Bo725130b2017-05-16 09:51:39 -07007990 /*
7991 * This will never fail as it's passing GPF_NOFS and
7992 * the allocation is backed by btrfs_bioset.
7993 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007994 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007995 bio->bi_private = dip;
7996 bio->bi_end_io = btrfs_end_dio_bio;
7997 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007998
Liu Bo725130b2017-05-16 09:51:39 -07007999 ASSERT(submit_len >= clone_len);
8000 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00008001
Liu Bo725130b2017-05-16 09:51:39 -07008002 /*
8003 * Increase the count before we submit the bio so we know
8004 * the end IO handler won't happen before we increase the
8005 * count. Otherwise, the dip might get freed before we're
8006 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008007 *
8008 * We transfer the initial reference to the last bio, so we
8009 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008010 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008011 if (submit_len > 0) {
8012 refcount_inc(&dip->refs);
8013 /*
8014 * If we are submitting more than one bio, submit them
8015 * all asynchronously. The exception is RAID 5 or 6, as
8016 * asynchronous checksums make it difficult to collect
8017 * full stripe writes.
8018 */
8019 if (!raid56)
8020 async_submit = 1;
8021 }
Miao Xiee65e1532010-11-22 03:04:43 +00008022
David Sterbad0ee3932018-03-08 14:35:48 +01008023 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008024 async_submit);
8025 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008026 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008027 if (submit_len > 0)
8028 refcount_dec(&dip->refs);
Michal Rostecki42034312021-01-27 14:57:27 +01008029 goto out_err_em;
Miao Xiee65e1532010-11-22 03:04:43 +00008030 }
Liu Bo725130b2017-05-16 09:51:39 -07008031
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008032 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008033 clone_offset += clone_len;
8034 start_sector += clone_len >> 9;
8035 file_offset += clone_len;
Michal Rostecki42034312021-01-27 14:57:27 +01008036
8037 free_extent_map(em);
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008038 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008039 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008040
Michal Rostecki42034312021-01-27 14:57:27 +01008041out_err_em:
8042 free_extent_map(em);
Miao Xiee65e1532010-11-22 03:04:43 +00008043out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008044 dip->dio_bio->bi_status = status;
8045 btrfs_dio_private_put(dip);
Michal Rostecki42034312021-01-27 14:57:27 +01008046
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008047 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008048}
8049
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008050const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008051 .iomap_begin = btrfs_dio_iomap_begin,
8052 .iomap_end = btrfs_dio_iomap_end,
8053};
8054
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008055const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008056 .submit_io = btrfs_submit_direct,
8057};
8058
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008059static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008060 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008061{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008062 int ret;
8063
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008064 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008065 if (ret)
8066 return ret;
8067
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008068 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008069}
8070
Chris Mason9ebefb182007-06-15 13:50:00 -04008071int btrfs_readpage(struct file *file, struct page *page)
8072{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008073 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8074 u64 start = page_offset(page);
8075 u64 end = start + PAGE_SIZE - 1;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008076 unsigned long bio_flags = 0;
Nikolay Borisov0f208812020-09-14 14:39:16 +03008077 struct bio *bio = NULL;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008078 int ret;
8079
Nikolay Borisov0f208812020-09-14 14:39:16 +03008080 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8081
8082 ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008083 if (bio)
8084 ret = submit_one_bio(bio, 0, bio_flags);
8085 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008086}
Chris Mason1832a6d2007-12-21 16:27:21 -05008087
Chris Mason39279cc2007-06-12 06:35:45 -04008088static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8089{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008090 struct inode *inode = page->mapping->host;
8091 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008092
8093 if (current->flags & PF_MEMALLOC) {
8094 redirty_page_for_writepage(wbc, page);
8095 unlock_page(page);
8096 return 0;
8097 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008098
8099 /*
8100 * If we are under memory pressure we will call this directly from the
8101 * VM, we need to make sure we have the inode referenced for the ordered
8102 * extent. If not just return like we didn't do anything.
8103 */
8104 if (!igrab(inode)) {
8105 redirty_page_for_writepage(wbc, page);
8106 return AOP_WRITEPAGE_ACTIVATE;
8107 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008108 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008109 btrfs_add_delayed_iput(inode);
8110 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008111}
Chris Mason39279cc2007-06-12 06:35:45 -04008112
Eric Sandeen48a3b632013-04-25 20:41:01 +00008113static int btrfs_writepages(struct address_space *mapping,
8114 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008115{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008116 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008117}
8118
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008119static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008120{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008121 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008122}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008123
Chris Masone6dcd2d2008-07-17 12:53:50 -04008124static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008125{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008126 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008127 if (ret == 1)
8128 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008129 return ret;
8130}
Chris Mason39279cc2007-06-12 06:35:45 -04008131
Chris Masone6dcd2d2008-07-17 12:53:50 -04008132static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8133{
Chris Mason98509cf2008-09-11 15:51:43 -04008134 if (PageWriteback(page) || PageDirty(page))
8135 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008136 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008137}
8138
Roman Gushchinf8e66082020-03-04 16:57:35 -08008139#ifdef CONFIG_MIGRATION
8140static int btrfs_migratepage(struct address_space *mapping,
8141 struct page *newpage, struct page *page,
8142 enum migrate_mode mode)
8143{
8144 int ret;
8145
8146 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8147 if (ret != MIGRATEPAGE_SUCCESS)
8148 return ret;
8149
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008150 if (page_has_private(page))
8151 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008152
8153 if (PagePrivate2(page)) {
8154 ClearPagePrivate2(page);
8155 SetPagePrivate2(newpage);
8156 }
8157
8158 if (mode != MIGRATE_SYNC_NO_COPY)
8159 migrate_page_copy(newpage, page);
8160 else
8161 migrate_page_states(newpage, page);
8162 return MIGRATEPAGE_SUCCESS;
8163}
8164#endif
8165
Lukas Czernerd47992f2013-05-21 23:17:23 -04008166static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8167 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008168{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008169 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8170 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008171 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008172 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008173 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008174 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308175 u64 start;
8176 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008177 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Filipe Manana2766ff62020-11-04 11:07:34 +00008178 bool found_ordered = false;
8179 bool completed_ordered = false;
Chris Masona52d9a82007-08-27 16:49:44 -04008180
Chris Mason8b62b722009-09-02 16:53:46 -04008181 /*
8182 * we have the page locked, so new writeback can't start,
8183 * and the dirty bit won't be cleared while we are here.
8184 *
8185 * Wait for IO on this page so that we can safely clear
8186 * the PagePrivate2 bit and do ordered accounting
8187 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008188 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008189
Chris Masone6dcd2d2008-07-17 12:53:50 -04008190 if (offset) {
8191 btrfs_releasepage(page, GFP_NOFS);
8192 return;
8193 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008194
8195 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008196 lock_extent_bits(tree, page_start, page_end, &cached_state);
Qu Wenruo951c80f2021-01-27 14:38:48 +08008197
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308198 start = page_start;
Qu Wenruo951c80f2021-01-27 14:38:48 +08008199again:
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008200 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008201 if (ordered) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008202 found_ordered = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08008203 end = min(page_end,
8204 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008205 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008206 * IO on this page will never be started, so we need to account
8207 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8208 * here, must leave that up for the ordered extent completion.
Chris Masoneb84ae02008-07-17 13:53:27 -04008209 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008210 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308211 clear_extent_bit(tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008212 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008213 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008214 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008215 /*
8216 * whoever cleared the private bit is responsible
8217 * for the finish_ordered_io
8218 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008219 if (TestClearPagePrivate2(page)) {
8220 struct btrfs_ordered_inode_tree *tree;
8221 u64 new_len;
8222
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008223 tree = &inode->ordered_tree;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008224
8225 spin_lock_irq(&tree->lock);
8226 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308227 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008228 if (new_len < ordered->truncated_len)
8229 ordered->truncated_len = new_len;
8230 spin_unlock_irq(&tree->lock);
8231
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008232 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8233 start,
Filipe Manana2766ff62020-11-04 11:07:34 +00008234 end - start + 1, 1)) {
Josef Bacik77cef2e2013-08-29 13:57:21 -04008235 btrfs_finish_ordered_io(ordered);
Filipe Manana2766ff62020-11-04 11:07:34 +00008236 completed_ordered = true;
8237 }
Chris Mason8b62b722009-09-02 16:53:46 -04008238 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008239 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008240 if (!inode_evicting) {
8241 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308242 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008243 &cached_state);
8244 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308245
8246 start = end + 1;
8247 if (start < page_end)
8248 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008249 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008250
Qu Wenruob9d0b382015-09-29 10:35:16 +08008251 /*
8252 * Qgroup reserved space handler
8253 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008254 * 1) Already written to disk or ordered extent already submitted
8255 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8256 * Qgroup will be handled by its qgroup_record then.
8257 * btrfs_qgroup_free_data() call will do nothing here.
8258 *
8259 * 2) Not written to disk yet
8260 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8261 * bit of its io_tree, and free the qgroup reserved data space.
8262 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008263 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008264 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008265 if (!inode_evicting) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008266 bool delete = true;
8267
8268 /*
8269 * If there's an ordered extent for this range and we have not
8270 * finished it ourselves, we must leave EXTENT_DELALLOC_NEW set
8271 * in the range for the ordered extent completion. We must also
8272 * not delete the range, otherwise we would lose that bit (and
8273 * any other bits set in the range). Make sure EXTENT_UPTODATE
8274 * is cleared if we don't delete, otherwise it can lead to
8275 * corruptions if the i_size is extented later.
8276 */
8277 if (found_ordered && !completed_ordered)
8278 delete = false;
Omar Sandovale1821632019-08-15 14:04:04 -07008279 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008280 EXTENT_DELALLOC | EXTENT_UPTODATE |
8281 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
8282 delete, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008283
8284 __btrfs_releasepage(page, GFP_NOFS);
8285 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008286
Chris Mason4a096752008-07-21 10:29:44 -04008287 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008288 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008289}
8290
Chris Mason9ebefb182007-06-15 13:50:00 -04008291/*
8292 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8293 * called from a page fault handler when a page is first dirtied. Hence we must
8294 * be careful to check for EOF conditions here. We set the page up correctly
8295 * for a written page which means we get ENOSPC checking when writing into
8296 * holes and correct delalloc and unwritten extent mapping on filesystems that
8297 * support these features.
8298 *
8299 * We are not allowed to take the i_mutex here so we have to play games to
8300 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008301 * truncate_setsize() writes the inode size before removing pages, once we have
8302 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008303 * beyond EOF, then the page is guaranteed safe against truncation until we
8304 * unlock the page.
8305 */
Souptick Joardera528a242018-06-06 19:54:44 +05308306vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008307{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008308 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008309 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008310 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008311 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8312 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008313 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008314 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008315 char *kaddr;
8316 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008317 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308318 vm_fault_t ret;
8319 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008320 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308321 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008322 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008323 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308324 u64 end;
8325
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008326 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008327
Jan Karab2b5ef52012-06-12 16:20:45 +02008328 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008329 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008330 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308331 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008332
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308333 /*
8334 * Reserving delalloc space after obtaining the page lock can lead to
8335 * deadlock. For example, if a dirty page is locked by this function
8336 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8337 * dirty page write out, then the btrfs_writepage() function could
8338 * end up waiting indefinitely to get a lock on the page currently
8339 * being processed by btrfs_page_mkwrite() function.
8340 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008341 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8342 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308343 if (!ret2) {
8344 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008345 reserved = 1;
8346 }
Souptick Joardera528a242018-06-06 19:54:44 +05308347 if (ret2) {
8348 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008349 if (reserved)
8350 goto out;
8351 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008352 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008353
Nick Piggin56a76f82009-03-31 15:23:23 -07008354 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008355again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008356 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008357 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008358
Chris Mason9ebefb182007-06-15 13:50:00 -04008359 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008360 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008361 /* page got truncated out from underneath us */
8362 goto out_unlock;
8363 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008364 wait_on_page_writeback(page);
8365
David Sterbaff13db42015-12-03 14:30:40 +01008366 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008367 set_page_extent_mapped(page);
8368
Chris Masoneb84ae02008-07-17 13:53:27 -04008369 /*
8370 * we can't set the delalloc bits if there are pending ordered
8371 * extents. Drop our locks and wait for them to finish
8372 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008373 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8374 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008375 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008376 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008377 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008378 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008379 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008380 btrfs_put_ordered_extent(ordered);
8381 goto again;
8382 }
8383
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008384 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008385 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008386 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008387 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308388 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008389 btrfs_delalloc_release_space(BTRFS_I(inode),
8390 data_reserved, page_start,
8391 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308392 }
8393 }
8394
Josef Bacikfbf19082009-10-01 17:10:23 -04008395 /*
Liu Bo54160342016-12-13 12:15:19 -08008396 * page_mkwrite gets called when the page is firstly dirtied after it's
8397 * faulted in, but write(2) could also dirty a page and set delalloc
8398 * bits, thus in this case for space account reason, we still need to
8399 * clear any delalloc bits within this page range since we have to
8400 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008401 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308402 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008403 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8404 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008405
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008406 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008407 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308408 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008409 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008410 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008411 ret = VM_FAULT_SIGBUS;
8412 goto out_unlock;
8413 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008414
8415 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008416 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008417 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008418 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008419 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008420
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008421 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008422 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008423 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008424 flush_dcache_page(page);
8425 kunmap(page);
8426 }
Chris Mason247e7432008-07-17 12:53:51 -04008427 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008428 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008429 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008430
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008431 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008432 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008433 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008434
David Sterbae43bbe52017-12-12 21:43:52 +01008435 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008436
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008437 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8438 sb_end_pagefault(inode->i_sb);
8439 extent_changeset_free(data_reserved);
8440 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008441
8442out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008443 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008444out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008445 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008446 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008447 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008448out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008449 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008450 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008451 return ret;
8452}
8453
Filipe Manana213e8c52018-02-06 20:40:31 +00008454static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008455{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008456 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008457 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008458 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008459 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008460 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008461 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008462 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008463
Filipe Manana213e8c52018-02-06 20:40:31 +00008464 if (!skip_writeback) {
8465 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8466 (u64)-1);
8467 if (ret)
8468 return ret;
8469 }
Chris Mason39279cc2007-06-12 06:35:45 -04008470
Josef Bacikfcb80c22011-05-03 10:40:22 -04008471 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008472 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8473 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008474 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008475 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008476 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008477 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008478 * be free'd up by the truncate operation, but also have some slack
8479 * space reserved in case it uses space during the truncate (thank you
8480 * very much snapshotting).
8481 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008482 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008483 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008484 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008485 * doesn't end up using space reserved for updating the inode. We also
8486 * need to be able to stop the transaction and start a new one, which
8487 * means we need to be able to update the inode several times, and we
8488 * have no idea of knowing how many times that will be, so we can't just
8489 * reserve 1 item for the entirety of the operation, so that has to be
8490 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008491 *
8492 * So that leaves us with
8493 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008494 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008495 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008496 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008497 * updating the inode.
8498 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008499 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008500 if (!rsv)
8501 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008502 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008503 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008504
Josef Bacik907cbce2011-08-08 13:46:15 -04008505 /*
Josef Bacik07127182011-08-19 10:29:59 -04008506 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008507 * 1 for updating the inode.
8508 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008509 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008510 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008511 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008512 goto out;
8513 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008514
Josef Bacik907cbce2011-08-08 13:46:15 -04008515 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008516 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008517 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008518 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008519
Chris Mason5a3f23d2009-03-31 13:27:11 -04008520 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008521 * So if we truncate and then write and fsync we normally would just
8522 * write the extents that changed, which is a problem if we need to
8523 * first truncate that entire inode. So set this flag so we write out
8524 * all of the extents in the inode to the sync log so we're completely
8525 * safe.
8526 */
8527 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008528 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008529
Yan, Zheng80825102009-11-12 09:35:36 +00008530 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008531 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008532 inode->i_size,
8533 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008534 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008535 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008536 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008537
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008538 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008539 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008540 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008541
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008542 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008543 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008544
8545 trans = btrfs_start_transaction(root, 2);
8546 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008547 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008548 trans = NULL;
8549 break;
8550 }
8551
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008552 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008553 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008554 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008555 BUG_ON(ret); /* shouldn't happen */
8556 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008557 }
8558
Josef Bacikddfae632017-10-19 14:16:02 -04008559 /*
8560 * We can't call btrfs_truncate_block inside a trans handle as we could
8561 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8562 * we've truncated everything except the last little bit, and can do
8563 * btrfs_truncate_block and then update the disk_i_size.
8564 */
8565 if (ret == NEED_TRUNCATE_BLOCK) {
8566 btrfs_end_transaction(trans);
8567 btrfs_btree_balance_dirty(fs_info);
8568
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008569 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008570 if (ret)
8571 goto out;
8572 trans = btrfs_start_transaction(root, 1);
8573 if (IS_ERR(trans)) {
8574 ret = PTR_ERR(trans);
8575 goto out;
8576 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008577 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008578 }
8579
Chris Mason917c16b2011-11-08 14:49:59 -05008580 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008581 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008582
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008583 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008584 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008585 if (ret2 && !ret)
8586 ret = ret2;
8587
8588 ret2 = btrfs_end_transaction(trans);
8589 if (ret2 && !ret)
8590 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008591 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008592 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008593out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008594 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008595
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008596 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008597}
8598
Sven Wegener3b963622008-06-09 21:57:42 -04008599/*
Chris Masond352ac62008-09-29 15:18:18 -04008600 * create a new subvolume directory/inode (helper for the ioctl).
8601 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008602int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008603 struct btrfs_root *new_root,
Nikolay Borisov69948022020-12-07 17:32:37 +02008604 struct btrfs_root *parent_root)
Chris Mason39279cc2007-06-12 06:35:45 -04008605{
Chris Mason39279cc2007-06-12 06:35:45 -04008606 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008607 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008608 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02008609 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04008610
Nikolay Borisov23125102020-12-07 17:32:36 +02008611 err = btrfs_get_free_objectid(new_root, &ino);
8612 if (err < 0)
8613 return err;
8614
8615 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008616 S_IFDIR | (~current_umask() & S_IRWXUGO),
8617 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008618 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008619 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008620 inode->i_op = &btrfs_dir_inode_operations;
8621 inode->i_fop = &btrfs_dir_file_operations;
8622
Miklos Szeredibfe86842011-10-28 14:13:29 +02008623 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008624 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008625 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008626
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008627 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8628 if (err)
8629 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008630 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008631 new_root->root_key.objectid, err);
8632
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008633 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008634
Yan, Zheng76dda932009-09-21 16:00:26 -04008635 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008636 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008637}
8638
Chris Mason39279cc2007-06-12 06:35:45 -04008639struct inode *btrfs_alloc_inode(struct super_block *sb)
8640{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008641 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008642 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008643 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008644
David Sterba712e36c2017-10-31 17:08:27 +01008645 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008646 if (!ei)
8647 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008648
8649 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008650 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008651 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008652 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008653 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008654 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008655 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008656 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008657 ei->disk_i_size = 0;
8658 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008659 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008660 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008661 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008662 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008663 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008664 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008665
Josef Bacik9e0baf62011-07-15 15:16:44 +00008666 spin_lock_init(&ei->lock);
8667 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008668 if (sb->s_magic != BTRFS_TEST_MAGIC)
8669 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8670 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008671 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008672 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008673 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008674
Miao Xie16cdcec2011-04-22 18:12:22 +08008675 ei->delayed_node = NULL;
8676
chandan r9cc97d62012-07-04 12:48:07 +05308677 ei->i_otime.tv_sec = 0;
8678 ei->i_otime.tv_nsec = 0;
8679
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008680 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008681 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008682 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8683 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8684 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008685 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8686 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008687 ei->io_tree.track_uptodate = true;
8688 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008689 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008690 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008691 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008692 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008693 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008694 RB_CLEAR_NODE(&ei->rb_node);
8695
8696 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008697}
8698
Josef Bacikaaedb552013-10-11 14:44:09 -04008699#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8700void btrfs_test_destroy_inode(struct inode *inode)
8701{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008702 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008703 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8704}
8705#endif
8706
Al Viro26602ca2019-04-10 15:14:41 -04008707void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008708{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008709 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8710}
8711
Nikolay Borisov633cc812020-09-18 12:15:49 +03008712void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008713{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008714 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008715 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8716 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008717
Nikolay Borisov633cc812020-09-18 12:15:49 +03008718 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8719 WARN_ON(vfs_inode->i_data.nrpages);
8720 WARN_ON(inode->block_rsv.reserved);
8721 WARN_ON(inode->block_rsv.size);
8722 WARN_ON(inode->outstanding_extents);
8723 WARN_ON(inode->delalloc_bytes);
8724 WARN_ON(inode->new_delalloc_bytes);
8725 WARN_ON(inode->csum_bytes);
8726 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008727
Chris Mason5a3f23d2009-03-31 13:27:11 -04008728 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008729 * This can happen where we create an inode, but somebody else also
8730 * created the same inode and we need to destroy the one we already
8731 * created.
8732 */
8733 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008734 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008735
Chris Masond3977122009-01-05 21:25:51 -05008736 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008737 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008738 if (!ordered)
8739 break;
8740 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008741 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008742 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008743 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008744 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008745 btrfs_put_ordered_extent(ordered);
8746 btrfs_put_ordered_extent(ordered);
8747 }
8748 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008749 btrfs_qgroup_check_reserved_leak(inode);
8750 inode_tree_del(inode);
8751 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8752 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8753 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008754}
8755
Al Viro45321ac2010-06-07 13:43:19 -04008756int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008757{
8758 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008759
Naohiro Aota6379ef92013-06-06 09:56:34 +00008760 if (root == NULL)
8761 return 1;
8762
Liu Bofa6ac872013-02-20 14:10:23 +00008763 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008764 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008765 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008766 else
Al Viro45321ac2010-06-07 13:43:19 -04008767 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008768}
8769
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008770static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008771{
8772 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8773
8774 inode_init_once(&ei->vfs_inode);
8775}
8776
David Sterbae67c7182018-02-19 17:24:18 +01008777void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008778{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008779 /*
8780 * Make sure all delayed rcu free inodes are flushed before we
8781 * destroy cache.
8782 */
8783 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008784 kmem_cache_destroy(btrfs_inode_cachep);
8785 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008786 kmem_cache_destroy(btrfs_path_cachep);
8787 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008788 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008789}
8790
Liu Bof5c29bd2017-11-02 17:21:50 -06008791int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008792{
David Sterba837e1972012-09-07 03:00:48 -06008793 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008794 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008795 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8796 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008797 if (!btrfs_inode_cachep)
8798 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008799
David Sterba837e1972012-09-07 03:00:48 -06008800 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008801 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008802 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008803 if (!btrfs_trans_handle_cachep)
8804 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008805
David Sterba837e1972012-09-07 03:00:48 -06008806 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008807 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008808 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008809 if (!btrfs_path_cachep)
8810 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008811
David Sterba837e1972012-09-07 03:00:48 -06008812 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008813 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008814 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008815 if (!btrfs_free_space_cachep)
8816 goto fail;
8817
Christophe Leroy3acd4852019-08-21 15:05:55 +00008818 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8819 PAGE_SIZE, PAGE_SIZE,
8820 SLAB_RED_ZONE, NULL);
8821 if (!btrfs_free_space_bitmap_cachep)
8822 goto fail;
8823
Chris Mason39279cc2007-06-12 06:35:45 -04008824 return 0;
8825fail:
8826 btrfs_destroy_cachep();
8827 return -ENOMEM;
8828}
8829
David Howellsa528d352017-01-31 16:46:22 +00008830static int btrfs_getattr(const struct path *path, struct kstat *stat,
8831 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008832{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008833 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00008834 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008835 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008836 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008837 u32 bi_flags = BTRFS_I(inode)->flags;
8838
8839 stat->result_mask |= STATX_BTIME;
8840 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8841 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8842 if (bi_flags & BTRFS_INODE_APPEND)
8843 stat->attributes |= STATX_ATTR_APPEND;
8844 if (bi_flags & BTRFS_INODE_COMPRESS)
8845 stat->attributes |= STATX_ATTR_COMPRESSED;
8846 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8847 stat->attributes |= STATX_ATTR_IMMUTABLE;
8848 if (bi_flags & BTRFS_INODE_NODUMP)
8849 stat->attributes |= STATX_ATTR_NODUMP;
8850
8851 stat->attributes_mask |= (STATX_ATTR_APPEND |
8852 STATX_ATTR_COMPRESSED |
8853 STATX_ATTR_IMMUTABLE |
8854 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008855
Chris Mason39279cc2007-06-12 06:35:45 -04008856 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008857 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008858
8859 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008860 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00008861 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008862 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00008863 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008864 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008865 return 0;
8866}
8867
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008868static int btrfs_rename_exchange(struct inode *old_dir,
8869 struct dentry *old_dentry,
8870 struct inode *new_dir,
8871 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008872{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008873 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008874 struct btrfs_trans_handle *trans;
8875 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008876 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008877 struct inode *new_inode = new_dentry->d_inode;
8878 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008879 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008880 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8881 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008882 u64 old_idx = 0;
8883 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008884 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01008885 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008886 bool root_log_pinned = false;
8887 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008888
8889 /* we only allow rename subvolume link between subvolumes */
8890 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8891 return -EXDEV;
8892
8893 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008894 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8895 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008896 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008897
8898 /*
8899 * We want to reserve the absolute worst case amount of items. So if
8900 * both inodes are subvols and we need to unlink them then that would
8901 * require 4 item modifications, but if they are both normal inodes it
8902 * would require 5 item modifications, so we'll assume their normal
8903 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8904 * should cover the worst case number of items we'll modify.
8905 */
8906 trans = btrfs_start_transaction(root, 12);
8907 if (IS_ERR(trans)) {
8908 ret = PTR_ERR(trans);
8909 goto out_notrans;
8910 }
8911
Josef Bacik3e174092019-11-15 15:43:06 -05008912 if (dest != root)
8913 btrfs_record_root_in_trans(trans, dest);
8914
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008915 /*
8916 * We need to find a free sequence number both in the source and
8917 * in the destination directory for the exchange.
8918 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008919 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008920 if (ret)
8921 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008922 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008923 if (ret)
8924 goto out_fail;
8925
8926 BTRFS_I(old_inode)->dir_index = 0ULL;
8927 BTRFS_I(new_inode)->dir_index = 0ULL;
8928
8929 /* Reference for the source. */
8930 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8931 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008932 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008933 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008934 btrfs_pin_log_trans(root);
8935 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008936 ret = btrfs_insert_inode_ref(trans, dest,
8937 new_dentry->d_name.name,
8938 new_dentry->d_name.len,
8939 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008940 btrfs_ino(BTRFS_I(new_dir)),
8941 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008942 if (ret)
8943 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008944 }
8945
8946 /* And now for the dest. */
8947 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8948 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008949 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008950 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008951 btrfs_pin_log_trans(dest);
8952 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008953 ret = btrfs_insert_inode_ref(trans, root,
8954 old_dentry->d_name.name,
8955 old_dentry->d_name.len,
8956 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008957 btrfs_ino(BTRFS_I(old_dir)),
8958 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008959 if (ret)
8960 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008961 }
8962
8963 /* Update inode version and ctime/mtime. */
8964 inode_inc_iversion(old_dir);
8965 inode_inc_iversion(new_dir);
8966 inode_inc_iversion(old_inode);
8967 inode_inc_iversion(new_inode);
8968 old_dir->i_ctime = old_dir->i_mtime = ctime;
8969 new_dir->i_ctime = new_dir->i_mtime = ctime;
8970 old_inode->i_ctime = ctime;
8971 new_inode->i_ctime = ctime;
8972
8973 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008974 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8975 BTRFS_I(old_inode), 1);
8976 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8977 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008978 }
8979
8980 /* src is a subvolume */
8981 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008982 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008983 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008984 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8985 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008986 old_dentry->d_name.name,
8987 old_dentry->d_name.len);
8988 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008989 ret = btrfs_update_inode(trans, root, BTRFS_I(old_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
8996 /* dest is a subvolume */
8997 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008998 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008999 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009000 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9001 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009002 new_dentry->d_name.name,
9003 new_dentry->d_name.len);
9004 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009005 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009006 }
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
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009012 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009013 new_dentry->d_name.name,
9014 new_dentry->d_name.len, 0, old_idx);
9015 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009016 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009017 goto out_fail;
9018 }
9019
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009020 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009021 old_dentry->d_name.name,
9022 old_dentry->d_name.len, 0, new_idx);
9023 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009024 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009025 goto out_fail;
9026 }
9027
9028 if (old_inode->i_nlink == 1)
9029 BTRFS_I(old_inode)->dir_index = old_idx;
9030 if (new_inode->i_nlink == 1)
9031 BTRFS_I(new_inode)->dir_index = new_idx;
9032
Filipe Manana86e8aa02016-05-05 02:02:27 +01009033 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009034 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9035 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009036 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009037 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009038 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009039 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009040 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9041 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009042 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009043 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009044 }
9045out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009046 /*
9047 * If we have pinned a log and an error happened, we unpin tasks
9048 * trying to sync the log and force them to fallback to a transaction
9049 * commit if the log currently contains any of the inodes involved in
9050 * this rename operation (to ensure we do not persist a log with an
9051 * inconsistent state for any of these inodes or leading to any
9052 * inconsistencies when replayed). If the transaction was aborted, the
9053 * abortion reason is propagated to userspace when attempting to commit
9054 * the transaction. If the log does not contain any of these inodes, we
9055 * allow the tasks to sync it.
9056 */
9057 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009058 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9059 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9060 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009061 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009062 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009063 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009064
9065 if (root_log_pinned) {
9066 btrfs_end_log_trans(root);
9067 root_log_pinned = false;
9068 }
9069 if (dest_log_pinned) {
9070 btrfs_end_log_trans(dest);
9071 dest_log_pinned = false;
9072 }
9073 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009074 ret2 = btrfs_end_transaction(trans);
9075 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009076out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009077 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9078 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009079 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009080
9081 return ret;
9082}
9083
9084static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9085 struct btrfs_root *root,
9086 struct inode *dir,
9087 struct dentry *dentry)
9088{
9089 int ret;
9090 struct inode *inode;
9091 u64 objectid;
9092 u64 index;
9093
Nikolay Borisov543068a2020-12-07 17:32:33 +02009094 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009095 if (ret)
9096 return ret;
9097
9098 inode = btrfs_new_inode(trans, root, dir,
9099 dentry->d_name.name,
9100 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009101 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009102 objectid,
9103 S_IFCHR | WHITEOUT_MODE,
9104 &index);
9105
9106 if (IS_ERR(inode)) {
9107 ret = PTR_ERR(inode);
9108 return ret;
9109 }
9110
9111 inode->i_op = &btrfs_special_inode_operations;
9112 init_special_inode(inode, inode->i_mode,
9113 WHITEOUT_DEV);
9114
9115 ret = btrfs_init_inode_security(trans, inode, dir,
9116 &dentry->d_name);
9117 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009118 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009119
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009120 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9121 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009122 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009123 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009124
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009125 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009126out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009127 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009128 if (ret)
9129 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009130 iput(inode);
9131
Filipe Mananac9901612016-05-05 01:41:57 +01009132 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009133}
9134
Chris Mason39279cc2007-06-12 06:35:45 -04009135static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009136 struct inode *new_dir, struct dentry *new_dentry,
9137 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009138{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009139 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009140 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009141 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009142 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9143 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009144 struct inode *new_inode = d_inode(new_dentry);
9145 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009146 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009147 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009148 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009149 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009150 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009151
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009152 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009153 return -EPERM;
9154
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009155 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009156 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009157 return -EXDEV;
9158
Li Zefan33345d012011-04-20 10:31:50 +08009159 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009160 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009161 return -ENOTEMPTY;
9162
Chris Mason39279cc2007-06-12 06:35:45 -04009163 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009164 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009165 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009166
9167
9168 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009169 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009170 new_dentry->d_name.name,
9171 new_dentry->d_name.len);
9172
9173 if (ret) {
9174 if (ret == -EEXIST) {
9175 /* we shouldn't get
9176 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309177 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009178 return ret;
9179 }
9180 } else {
9181 /* maybe -EOVERFLOW */
9182 return ret;
9183 }
9184 }
9185 ret = 0;
9186
Chris Mason5a3f23d2009-03-31 13:27:11 -04009187 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009188 * we're using rename to replace one file with another. Start IO on it
9189 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009190 */
Chris Mason8d875f92014-08-12 10:47:42 -07009191 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009192 filemap_flush(old_inode->i_mapping);
9193
Yan, Zheng76dda932009-09-21 16:00:26 -04009194 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009195 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009196 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009197 /*
9198 * We want to reserve the absolute worst case amount of items. So if
9199 * both inodes are subvols and we need to unlink them then that would
9200 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009201 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009202 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9203 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009204 * If our rename has the whiteout flag, we need more 5 units for the
9205 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9206 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009207 */
Filipe Manana5062af32016-05-05 10:26:26 +01009208 trans_num_items = 11;
9209 if (flags & RENAME_WHITEOUT)
9210 trans_num_items += 5;
9211 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009212 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009213 ret = PTR_ERR(trans);
9214 goto out_notrans;
9215 }
Chris Mason5f39d392007-10-15 16:14:19 -04009216
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009217 if (dest != root)
9218 btrfs_record_root_in_trans(trans, dest);
9219
Nikolay Borisov877574e2017-02-20 13:50:33 +02009220 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009221 if (ret)
9222 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009223
Miao Xie67de1172013-12-26 13:07:06 +08009224 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009225 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009226 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009227 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009228 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009229 btrfs_pin_log_trans(root);
9230 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009231 ret = btrfs_insert_inode_ref(trans, dest,
9232 new_dentry->d_name.name,
9233 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009234 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009235 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009236 if (ret)
9237 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009238 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009239
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009240 inode_inc_iversion(old_dir);
9241 inode_inc_iversion(new_dir);
9242 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009243 old_dir->i_ctime = old_dir->i_mtime =
9244 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009245 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009246
Chris Mason12fcfd22009-03-24 10:24:20 -04009247 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009248 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9249 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009250
Li Zefan33345d012011-04-20 10:31:50 +08009251 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009252 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009253 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009254 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9255 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009256 old_dentry->d_name.name,
9257 old_dentry->d_name.len);
9258 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009259 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009260 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009261 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009262 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009263 goto out_fail;
9264 }
Chris Mason39279cc2007-06-12 06:35:45 -04009265
9266 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009267 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009268 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009269 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009270 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009271 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009272 BUG_ON(new_inode->i_nlink == 0);
9273 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009274 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9275 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009276 new_dentry->d_name.name,
9277 new_dentry->d_name.len);
9278 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009279 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009280 ret = btrfs_orphan_add(trans,
9281 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009282 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009283 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009284 goto out_fail;
9285 }
Chris Mason39279cc2007-06-12 06:35:45 -04009286 }
Josef Bacikaec74772008-07-24 12:12:38 -04009287
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009288 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009289 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009290 new_dentry->d_name.len, 0, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009291 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009292 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009293 goto out_fail;
9294 }
Chris Mason39279cc2007-06-12 06:35:45 -04009295
Miao Xie67de1172013-12-26 13:07:06 +08009296 if (old_inode->i_nlink == 1)
9297 BTRFS_I(old_inode)->dir_index = index;
9298
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009299 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009300 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9301 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009302 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009303 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009304 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009305
9306 if (flags & RENAME_WHITEOUT) {
9307 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9308 old_dentry);
9309
9310 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009311 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009312 goto out_fail;
9313 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009314 }
Chris Mason39279cc2007-06-12 06:35:45 -04009315out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009316 /*
9317 * If we have pinned the log and an error happened, we unpin tasks
9318 * trying to sync the log and force them to fallback to a transaction
9319 * commit if the log currently contains any of the inodes involved in
9320 * this rename operation (to ensure we do not persist a log with an
9321 * inconsistent state for any of these inodes or leading to any
9322 * inconsistencies when replayed). If the transaction was aborted, the
9323 * abortion reason is propagated to userspace when attempting to commit
9324 * the transaction. If the log does not contain any of these inodes, we
9325 * allow the tasks to sync it.
9326 */
9327 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009328 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9329 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9330 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009331 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009332 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009333 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009334
9335 btrfs_end_log_trans(root);
9336 log_pinned = false;
9337 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009338 ret2 = btrfs_end_transaction(trans);
9339 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009340out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009341 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009342 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009343
Chris Mason39279cc2007-06-12 06:35:45 -04009344 return ret;
9345}
9346
Miklos Szeredi80ace852014-07-23 15:15:32 +02009347static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9348 struct inode *new_dir, struct dentry *new_dentry,
9349 unsigned int flags)
9350{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009351 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009352 return -EINVAL;
9353
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009354 if (flags & RENAME_EXCHANGE)
9355 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9356 new_dentry);
9357
9358 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009359}
9360
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009361struct btrfs_delalloc_work {
9362 struct inode *inode;
9363 struct completion completion;
9364 struct list_head list;
9365 struct btrfs_work work;
9366};
9367
Miao Xie8ccf6f192012-10-25 09:28:04 +00009368static void btrfs_run_delalloc_work(struct btrfs_work *work)
9369{
9370 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009371 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009372
9373 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9374 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009375 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009376 filemap_flush(inode->i_mapping);
9377 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9378 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009379 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009380
Nikolay Borisov076da912018-04-23 10:54:16 +03009381 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009382 complete(&delalloc_work->completion);
9383}
9384
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009385static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009386{
9387 struct btrfs_delalloc_work *work;
9388
David Sterba100d5702015-12-08 14:39:32 +01009389 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009390 if (!work)
9391 return NULL;
9392
9393 init_completion(&work->completion);
9394 INIT_LIST_HEAD(&work->list);
9395 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009396 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009397
9398 return work;
9399}
9400
Chris Masond352ac62008-09-29 15:18:18 -04009401/*
9402 * some fairly slow code that needs optimization. This walks the list
9403 * of all the inodes with pending delalloc and forces them to disk.
9404 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009405static int start_delalloc_inodes(struct btrfs_root *root,
9406 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009407 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009408{
Chris Masonea8c2812008-08-04 23:17:27 -04009409 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009410 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009411 struct btrfs_delalloc_work *work, *next;
9412 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009413 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009414 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009415 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009416
Miao Xie8ccf6f192012-10-25 09:28:04 +00009417 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009418 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009419
Miao Xie573bfb72014-03-06 13:55:03 +08009420 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009421 spin_lock(&root->delalloc_lock);
9422 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009423 while (!list_empty(&splice)) {
9424 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009425 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009426
Miao Xieeb73c1b2013-05-15 07:48:22 +00009427 list_move_tail(&binode->delalloc_inodes,
9428 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009429
9430 if (in_reclaim_context &&
9431 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9432 continue;
9433
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009434 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009435 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009436 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009437 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009438 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009439 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009440
Ethan Lien3cd24c62018-11-01 14:49:03 +08009441 if (snapshot)
9442 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9443 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009444 if (full_flush) {
9445 work = btrfs_alloc_delalloc_work(inode);
9446 if (!work) {
9447 iput(inode);
9448 ret = -ENOMEM;
9449 goto out;
9450 }
9451 list_add_tail(&work->list, &works);
9452 btrfs_queue_work(root->fs_info->flush_workers,
9453 &work->work);
9454 } else {
9455 ret = sync_inode(inode, wbc);
9456 if (!ret &&
9457 test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9458 &BTRFS_I(inode)->runtime_flags))
9459 ret = sync_inode(inode, wbc);
9460 btrfs_add_delayed_iput(inode);
9461 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009462 goto out;
9463 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009464 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009465 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009466 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009467 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009468
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009469out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009470 list_for_each_entry_safe(work, next, &works, list) {
9471 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009472 wait_for_completion(&work->completion);
9473 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009474 }
9475
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009476 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009477 spin_lock(&root->delalloc_lock);
9478 list_splice_tail(&splice, &root->delalloc_inodes);
9479 spin_unlock(&root->delalloc_lock);
9480 }
Miao Xie573bfb72014-03-06 13:55:03 +08009481 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009482 return ret;
9483}
9484
Ethan Lien3cd24c62018-11-01 14:49:03 +08009485int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009486{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009487 struct writeback_control wbc = {
9488 .nr_to_write = LONG_MAX,
9489 .sync_mode = WB_SYNC_NONE,
9490 .range_start = 0,
9491 .range_end = LLONG_MAX,
9492 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009493 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009494
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009495 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009496 return -EROFS;
9497
Josef Bacike076ab2a2021-01-07 17:08:30 -05009498 return start_delalloc_inodes(root, &wbc, true, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009499}
9500
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009501int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009502 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009503{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009504 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009505 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009506 .sync_mode = WB_SYNC_NONE,
9507 .range_start = 0,
9508 .range_end = LLONG_MAX,
9509 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009510 struct btrfs_root *root;
9511 struct list_head splice;
9512 int ret;
9513
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009514 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009515 return -EROFS;
9516
9517 INIT_LIST_HEAD(&splice);
9518
Miao Xie573bfb72014-03-06 13:55:03 +08009519 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009520 spin_lock(&fs_info->delalloc_root_lock);
9521 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +02009522 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -05009523 /*
9524 * Reset nr_to_write here so we know that we're doing a full
9525 * flush.
9526 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009527 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -05009528 wbc.nr_to_write = LONG_MAX;
9529
Miao Xieeb73c1b2013-05-15 07:48:22 +00009530 root = list_first_entry(&splice, struct btrfs_root,
9531 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009532 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009533 BUG_ON(!root);
9534 list_move_tail(&root->delalloc_root,
9535 &fs_info->delalloc_roots);
9536 spin_unlock(&fs_info->delalloc_root_lock);
9537
Josef Bacike076ab2a2021-01-07 17:08:30 -05009538 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -05009539 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009540 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009541 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009542 spin_lock(&fs_info->delalloc_root_lock);
9543 }
9544 spin_unlock(&fs_info->delalloc_root_lock);
9545
Miao Xie6c255e62014-03-06 13:55:01 +08009546 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009547out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009548 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009549 spin_lock(&fs_info->delalloc_root_lock);
9550 list_splice_tail(&splice, &fs_info->delalloc_roots);
9551 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009552 }
Miao Xie573bfb72014-03-06 13:55:03 +08009553 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009554 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009555}
9556
Chris Mason39279cc2007-06-12 06:35:45 -04009557static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9558 const char *symname)
9559{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009560 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009561 struct btrfs_trans_handle *trans;
9562 struct btrfs_root *root = BTRFS_I(dir)->root;
9563 struct btrfs_path *path;
9564 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009565 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009566 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009567 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309568 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009569 int name_len;
9570 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009571 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009572 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009573 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009574
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009575 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009576 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009577 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009578
Josef Bacik9ed74f22009-09-11 16:12:44 -04009579 /*
9580 * 2 items for inode item and ref
9581 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009582 * 1 item for updating parent inode item
9583 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009584 * 1 item for xattr if selinux is on
9585 */
Filipe Manana9269d122015-12-31 18:16:29 +00009586 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009587 if (IS_ERR(trans))
9588 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009589
Nikolay Borisov543068a2020-12-07 17:32:33 +02009590 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08009591 if (err)
9592 goto out_unlock;
9593
Josef Bacikaec74772008-07-24 12:12:38 -04009594 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009595 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9596 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009597 if (IS_ERR(inode)) {
9598 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009599 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009600 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009601 }
Chris Mason39279cc2007-06-12 06:35:45 -04009602
Casey Schauflerad19db72011-12-15 10:09:07 -05009603 /*
9604 * If the active LSM wants to access the inode during
9605 * d_instantiate it needs these. Smack checks to see
9606 * if the filesystem supports xattrs by looking at the
9607 * ops vector.
9608 */
9609 inode->i_fop = &btrfs_file_operations;
9610 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009611 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009612
9613 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9614 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009615 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009616
Chris Mason39279cc2007-06-12 06:35:45 -04009617 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009618 if (!path) {
9619 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009620 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009621 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009622 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009623 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009624 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009625 datasize = btrfs_file_extent_calc_inline_size(name_len);
9626 err = btrfs_insert_empty_item(trans, root, path, &key,
9627 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009628 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009629 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009630 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009631 }
Chris Mason5f39d392007-10-15 16:14:19 -04009632 leaf = path->nodes[0];
9633 ei = btrfs_item_ptr(leaf, path->slots[0],
9634 struct btrfs_file_extent_item);
9635 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9636 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009637 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009638 btrfs_set_file_extent_encryption(leaf, ei, 0);
9639 btrfs_set_file_extent_compression(leaf, ei, 0);
9640 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9641 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9642
Chris Mason39279cc2007-06-12 06:35:45 -04009643 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009644 write_extent_buffer(leaf, symname, ptr, name_len);
9645 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009646 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009647
Chris Mason39279cc2007-06-12 06:35:45 -04009648 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009649 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009650 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009651 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009652 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +00009653 /*
9654 * Last step, add directory indexes for our symlink inode. This is the
9655 * last step to avoid extra cleanup of these indexes if an error happens
9656 * elsewhere above.
9657 */
9658 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009659 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9660 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009661 if (err)
9662 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009663
Al Viro1e2e5472018-05-04 08:23:01 -04009664 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009665
9666out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009667 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009668 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009669 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009670 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009671 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009672 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009673 return err;
9674}
Chris Mason16432982008-04-10 10:23:21 -04009675
Filipe Manana8fccebf2020-09-08 11:27:20 +01009676static struct btrfs_trans_handle *insert_prealloc_file_extent(
9677 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009678 struct btrfs_inode *inode,
9679 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009680 u64 file_offset)
9681{
9682 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009683 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009684 struct btrfs_trans_handle *trans = trans_in;
9685 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009686 u64 start = ins->objectid;
9687 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009688 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009689
9690 memset(&stack_fi, 0, sizeof(stack_fi));
9691
9692 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9693 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9694 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9695 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9696 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9697 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9698 /* Encryption and other encoding is reserved and all 0 */
9699
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009700 ret = btrfs_qgroup_release_data(inode, file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009701 if (ret < 0)
Filipe Manana8fccebf2020-09-08 11:27:20 +01009702 return ERR_PTR(ret);
9703
9704 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009705 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +00009706 file_offset, &stack_fi,
9707 true, ret);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009708 if (ret)
9709 return ERR_PTR(ret);
9710 return trans;
9711 }
9712
9713 extent_info.disk_offset = start;
9714 extent_info.disk_len = len;
9715 extent_info.data_offset = 0;
9716 extent_info.data_len = len;
9717 extent_info.file_offset = file_offset;
9718 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009719 extent_info.is_new_extent = true;
9720 extent_info.qgroup_reserved = ret;
9721 extent_info.insertions = 0;
9722
9723 path = btrfs_alloc_path();
9724 if (!path)
9725 return ERR_PTR(-ENOMEM);
9726
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009727 ret = btrfs_replace_file_extents(&inode->vfs_inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009728 file_offset + len - 1, &extent_info,
9729 &trans);
9730 btrfs_free_path(path);
9731 if (ret)
9732 return ERR_PTR(ret);
9733
9734 return trans;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009735}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009736
Josef Bacik0af3d002010-06-21 14:48:16 -04009737static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9738 u64 start, u64 num_bytes, u64 min_size,
9739 loff_t actual_len, u64 *alloc_hint,
9740 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009741{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009742 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009743 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9744 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009745 struct btrfs_root *root = BTRFS_I(inode)->root;
9746 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009747 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009748 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009749 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009750 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009751 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009752 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009753 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009754 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009755
Josef Bacik0af3d002010-06-21 14:48:16 -04009756 if (trans)
9757 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009758 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009759 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009760 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009761 /*
9762 * If we are severely fragmented we could end up with really
9763 * small allocations, so if the allocator is returning small
9764 * chunks lets make its job easier by only searching for those
9765 * sized chunks.
9766 */
9767 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009768 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9769 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009770 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -04009771 break;
Josef Bacikb778cf92020-02-13 10:47:31 -05009772
9773 /*
9774 * We've reserved this space, and thus converted it from
9775 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9776 * from here on out we will only need to clear our reservation
9777 * for the remaining unreserved area, so advance our
9778 * clear_offset by our extent size.
9779 */
9780 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009781
Josef Bacik0b670dc2015-09-23 17:11:16 -04009782 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009783 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
9784 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +01009785 /*
9786 * Now that we inserted the prealloc extent we can finally
9787 * decrement the number of reservations in the block group.
9788 * If we did it before, we could race with relocation and have
9789 * relocation miss the reserved extent, making it fail later.
9790 */
9791 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009792 if (IS_ERR(trans)) {
9793 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009794 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009795 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009796 break;
9797 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009798
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009799 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009800 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009801
Josef Bacik5dc562c2012-08-17 13:14:17 -04009802 em = alloc_extent_map();
9803 if (!em) {
9804 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9805 &BTRFS_I(inode)->runtime_flags);
9806 goto next;
9807 }
9808
9809 em->start = cur_offset;
9810 em->orig_start = cur_offset;
9811 em->len = ins.offset;
9812 em->block_start = ins.objectid;
9813 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009814 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009815 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009816 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9817 em->generation = trans->transid;
9818
9819 while (1) {
9820 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009821 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009822 write_unlock(&em_tree->lock);
9823 if (ret != -EEXIST)
9824 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009825 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009826 cur_offset + ins.offset - 1,
9827 0);
9828 }
9829 free_extent_map(em);
9830next:
Yan Zhengd899e052008-10-30 14:25:28 -04009831 num_bytes -= ins.offset;
9832 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009833 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009834
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009835 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009836 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009837 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009838 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009839 (actual_len > inode->i_size) &&
9840 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009841 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009842 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009843 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009844 i_size = cur_offset;
9845 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +02009846 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009847 }
9848
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009849 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009850
9851 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009852 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009853 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009854 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009855 break;
9856 }
Yan Zhengd899e052008-10-30 14:25:28 -04009857
Filipe Manana8fccebf2020-09-08 11:27:20 +01009858 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009859 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009860 trans = NULL;
9861 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009862 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009863 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009864 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009865 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009866 return ret;
9867}
9868
Josef Bacik0af3d002010-06-21 14:48:16 -04009869int btrfs_prealloc_file_range(struct inode *inode, int mode,
9870 u64 start, u64 num_bytes, u64 min_size,
9871 loff_t actual_len, u64 *alloc_hint)
9872{
9873 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9874 min_size, actual_len, alloc_hint,
9875 NULL);
9876}
9877
9878int btrfs_prealloc_file_range_trans(struct inode *inode,
9879 struct btrfs_trans_handle *trans, int mode,
9880 u64 start, u64 num_bytes, u64 min_size,
9881 loff_t actual_len, u64 *alloc_hint)
9882{
9883 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9884 min_size, actual_len, alloc_hint, trans);
9885}
9886
Chris Masone6dcd2d2008-07-17 12:53:50 -04009887static int btrfs_set_page_dirty(struct page *page)
9888{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009889 return __set_page_dirty_nobuffers(page);
9890}
9891
Al Viro10556cb22011-06-20 19:28:19 -04009892static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009893{
Li Zefanb83cc962010-12-20 16:04:08 +08009894 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009895 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009896
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009897 if (mask & MAY_WRITE &&
9898 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9899 if (btrfs_root_readonly(root))
9900 return -EROFS;
9901 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9902 return -EACCES;
9903 }
Al Viro2830ba72011-06-20 19:16:29 -04009904 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009905}
Chris Mason39279cc2007-06-12 06:35:45 -04009906
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009907static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9908{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009909 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009910 struct btrfs_trans_handle *trans;
9911 struct btrfs_root *root = BTRFS_I(dir)->root;
9912 struct inode *inode = NULL;
9913 u64 objectid;
9914 u64 index;
9915 int ret = 0;
9916
9917 /*
9918 * 5 units required for adding orphan entry
9919 */
9920 trans = btrfs_start_transaction(root, 5);
9921 if (IS_ERR(trans))
9922 return PTR_ERR(trans);
9923
Nikolay Borisov543068a2020-12-07 17:32:33 +02009924 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009925 if (ret)
9926 goto out;
9927
9928 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009929 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009930 if (IS_ERR(inode)) {
9931 ret = PTR_ERR(inode);
9932 inode = NULL;
9933 goto out;
9934 }
9935
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009936 inode->i_fop = &btrfs_file_operations;
9937 inode->i_op = &btrfs_file_inode_operations;
9938
9939 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009940
Chris Masonb0d5d102014-09-08 13:08:51 -07009941 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9942 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009943 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009944
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009945 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07009946 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009947 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009948 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009949 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009950 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009951
Filipe Manana5762b5c2014-08-01 00:10:32 +01009952 /*
9953 * We set number of links to 0 in btrfs_new_inode(), and here we set
9954 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9955 * through:
9956 *
9957 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9958 */
9959 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009960 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009961 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009962 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009963out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009964 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009965 if (ret && inode)
9966 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009967 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009968 return ret;
9969}
9970
David Sterba5cdc84b2018-07-18 20:32:52 +02009971void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009972{
David Sterba5cdc84b2018-07-18 20:32:52 +02009973 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009974 unsigned long index = start >> PAGE_SHIFT;
9975 unsigned long end_index = end >> PAGE_SHIFT;
9976 struct page *page;
9977
9978 while (index <= end_index) {
9979 page = find_get_page(inode->i_mapping, index);
9980 ASSERT(page); /* Pages should be in the extent_io_tree */
9981 set_page_writeback(page);
9982 put_page(page);
9983 index++;
9984 }
9985}
9986
Omar Sandovaled46ff32016-11-03 10:28:14 -07009987#ifdef CONFIG_SWAP
9988/*
9989 * Add an entry indicating a block group or device which is pinned by a
9990 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9991 * negative errno on failure.
9992 */
9993static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9994 bool is_block_group)
9995{
9996 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9997 struct btrfs_swapfile_pin *sp, *entry;
9998 struct rb_node **p;
9999 struct rb_node *parent = NULL;
10000
10001 sp = kmalloc(sizeof(*sp), GFP_NOFS);
10002 if (!sp)
10003 return -ENOMEM;
10004 sp->ptr = ptr;
10005 sp->inode = inode;
10006 sp->is_block_group = is_block_group;
10007
10008 spin_lock(&fs_info->swapfile_pins_lock);
10009 p = &fs_info->swapfile_pins.rb_node;
10010 while (*p) {
10011 parent = *p;
10012 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10013 if (sp->ptr < entry->ptr ||
10014 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10015 p = &(*p)->rb_left;
10016 } else if (sp->ptr > entry->ptr ||
10017 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10018 p = &(*p)->rb_right;
10019 } else {
10020 spin_unlock(&fs_info->swapfile_pins_lock);
10021 kfree(sp);
10022 return 1;
10023 }
10024 }
10025 rb_link_node(&sp->node, parent, p);
10026 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10027 spin_unlock(&fs_info->swapfile_pins_lock);
10028 return 0;
10029}
10030
10031/* Free all of the entries pinned by this swapfile. */
10032static void btrfs_free_swapfile_pins(struct inode *inode)
10033{
10034 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10035 struct btrfs_swapfile_pin *sp;
10036 struct rb_node *node, *next;
10037
10038 spin_lock(&fs_info->swapfile_pins_lock);
10039 node = rb_first(&fs_info->swapfile_pins);
10040 while (node) {
10041 next = rb_next(node);
10042 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10043 if (sp->inode == inode) {
10044 rb_erase(&sp->node, &fs_info->swapfile_pins);
10045 if (sp->is_block_group)
10046 btrfs_put_block_group(sp->ptr);
10047 kfree(sp);
10048 }
10049 node = next;
10050 }
10051 spin_unlock(&fs_info->swapfile_pins_lock);
10052}
10053
10054struct btrfs_swap_info {
10055 u64 start;
10056 u64 block_start;
10057 u64 block_len;
10058 u64 lowest_ppage;
10059 u64 highest_ppage;
10060 unsigned long nr_pages;
10061 int nr_extents;
10062};
10063
10064static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10065 struct btrfs_swap_info *bsi)
10066{
10067 unsigned long nr_pages;
10068 u64 first_ppage, first_ppage_reported, next_ppage;
10069 int ret;
10070
10071 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10072 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10073 PAGE_SIZE) >> PAGE_SHIFT;
10074
10075 if (first_ppage >= next_ppage)
10076 return 0;
10077 nr_pages = next_ppage - first_ppage;
10078
10079 first_ppage_reported = first_ppage;
10080 if (bsi->start == 0)
10081 first_ppage_reported++;
10082 if (bsi->lowest_ppage > first_ppage_reported)
10083 bsi->lowest_ppage = first_ppage_reported;
10084 if (bsi->highest_ppage < (next_ppage - 1))
10085 bsi->highest_ppage = next_ppage - 1;
10086
10087 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10088 if (ret < 0)
10089 return ret;
10090 bsi->nr_extents += ret;
10091 bsi->nr_pages += nr_pages;
10092 return 0;
10093}
10094
10095static void btrfs_swap_deactivate(struct file *file)
10096{
10097 struct inode *inode = file_inode(file);
10098
10099 btrfs_free_swapfile_pins(inode);
10100 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10101}
10102
10103static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10104 sector_t *span)
10105{
10106 struct inode *inode = file_inode(file);
10107 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10108 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10109 struct extent_state *cached_state = NULL;
10110 struct extent_map *em = NULL;
10111 struct btrfs_device *device = NULL;
10112 struct btrfs_swap_info bsi = {
10113 .lowest_ppage = (sector_t)-1ULL,
10114 };
10115 int ret = 0;
10116 u64 isize;
10117 u64 start;
10118
10119 /*
10120 * If the swap file was just created, make sure delalloc is done. If the
10121 * file changes again after this, the user is doing something stupid and
10122 * we don't really care.
10123 */
10124 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10125 if (ret)
10126 return ret;
10127
10128 /*
10129 * The inode is locked, so these flags won't change after we check them.
10130 */
10131 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10132 btrfs_warn(fs_info, "swapfile must not be compressed");
10133 return -EINVAL;
10134 }
10135 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10136 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10137 return -EINVAL;
10138 }
10139 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10140 btrfs_warn(fs_info, "swapfile must not be checksummed");
10141 return -EINVAL;
10142 }
10143
10144 /*
10145 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010146 * under us. The exclop protection makes sure they aren't running/won't
10147 * run concurrently while we are mapping the swap extents, and
10148 * fs_info->swapfile_pins prevents them from running while the swap
10149 * file is active and moving the extents. Note that this also prevents
10150 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010151 * really worth the trouble to allow it.
10152 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010153 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010154 btrfs_warn(fs_info,
10155 "cannot activate swapfile while exclusive operation is running");
10156 return -EBUSY;
10157 }
10158 /*
10159 * Snapshots can create extents which require COW even if NODATACOW is
10160 * set. We use this counter to prevent snapshots. We must increment it
10161 * before walking the extents because we don't want a concurrent
10162 * snapshot to run after we've already checked the extents.
10163 */
10164 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10165
10166 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10167
10168 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10169 start = 0;
10170 while (start < isize) {
10171 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010172 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010173 u64 len = isize - start;
10174
Omar Sandoval39b07b52019-12-02 17:34:23 -080010175 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010176 if (IS_ERR(em)) {
10177 ret = PTR_ERR(em);
10178 goto out;
10179 }
10180
10181 if (em->block_start == EXTENT_MAP_HOLE) {
10182 btrfs_warn(fs_info, "swapfile must not have holes");
10183 ret = -EINVAL;
10184 goto out;
10185 }
10186 if (em->block_start == EXTENT_MAP_INLINE) {
10187 /*
10188 * It's unlikely we'll ever actually find ourselves
10189 * here, as a file small enough to fit inline won't be
10190 * big enough to store more than the swap header, but in
10191 * case something changes in the future, let's catch it
10192 * here rather than later.
10193 */
10194 btrfs_warn(fs_info, "swapfile must not be inline");
10195 ret = -EINVAL;
10196 goto out;
10197 }
10198 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10199 btrfs_warn(fs_info, "swapfile must not be compressed");
10200 ret = -EINVAL;
10201 goto out;
10202 }
10203
10204 logical_block_start = em->block_start + (start - em->start);
10205 len = min(len, em->len - (start - em->start));
10206 free_extent_map(em);
10207 em = NULL;
10208
Boris Burkova84d5d42020-08-18 11:00:05 -070010209 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010210 if (ret < 0) {
10211 goto out;
10212 } else if (ret) {
10213 ret = 0;
10214 } else {
10215 btrfs_warn(fs_info,
10216 "swapfile must not be copy-on-write");
10217 ret = -EINVAL;
10218 goto out;
10219 }
10220
10221 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10222 if (IS_ERR(em)) {
10223 ret = PTR_ERR(em);
10224 goto out;
10225 }
10226
10227 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10228 btrfs_warn(fs_info,
10229 "swapfile must have single data profile");
10230 ret = -EINVAL;
10231 goto out;
10232 }
10233
10234 if (device == NULL) {
10235 device = em->map_lookup->stripes[0].dev;
10236 ret = btrfs_add_swapfile_pin(inode, device, false);
10237 if (ret == 1)
10238 ret = 0;
10239 else if (ret)
10240 goto out;
10241 } else if (device != em->map_lookup->stripes[0].dev) {
10242 btrfs_warn(fs_info, "swapfile must be on one device");
10243 ret = -EINVAL;
10244 goto out;
10245 }
10246
10247 physical_block_start = (em->map_lookup->stripes[0].physical +
10248 (logical_block_start - em->start));
10249 len = min(len, em->len - (logical_block_start - em->start));
10250 free_extent_map(em);
10251 em = NULL;
10252
10253 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10254 if (!bg) {
10255 btrfs_warn(fs_info,
10256 "could not find block group containing swapfile");
10257 ret = -EINVAL;
10258 goto out;
10259 }
10260
10261 ret = btrfs_add_swapfile_pin(inode, bg, true);
10262 if (ret) {
10263 btrfs_put_block_group(bg);
10264 if (ret == 1)
10265 ret = 0;
10266 else
10267 goto out;
10268 }
10269
10270 if (bsi.block_len &&
10271 bsi.block_start + bsi.block_len == physical_block_start) {
10272 bsi.block_len += len;
10273 } else {
10274 if (bsi.block_len) {
10275 ret = btrfs_add_swap_extent(sis, &bsi);
10276 if (ret)
10277 goto out;
10278 }
10279 bsi.start = start;
10280 bsi.block_start = physical_block_start;
10281 bsi.block_len = len;
10282 }
10283
10284 start += len;
10285 }
10286
10287 if (bsi.block_len)
10288 ret = btrfs_add_swap_extent(sis, &bsi);
10289
10290out:
10291 if (!IS_ERR_OR_NULL(em))
10292 free_extent_map(em);
10293
10294 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10295
10296 if (ret)
10297 btrfs_swap_deactivate(file);
10298
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010299 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010300
10301 if (ret)
10302 return ret;
10303
10304 if (device)
10305 sis->bdev = device->bdev;
10306 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10307 sis->max = bsi.nr_pages;
10308 sis->pages = bsi.nr_pages - 1;
10309 sis->highest_bit = bsi.nr_pages - 1;
10310 return bsi.nr_extents;
10311}
10312#else
10313static void btrfs_swap_deactivate(struct file *file)
10314{
10315}
10316
10317static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10318 sector_t *span)
10319{
10320 return -EOPNOTSUPP;
10321}
10322#endif
10323
Filipe Manana2766ff62020-11-04 11:07:34 +000010324/*
10325 * Update the number of bytes used in the VFS' inode. When we replace extents in
10326 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10327 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10328 * always get a correct value.
10329 */
10330void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10331 const u64 add_bytes,
10332 const u64 del_bytes)
10333{
10334 if (add_bytes == del_bytes)
10335 return;
10336
10337 spin_lock(&inode->lock);
10338 if (del_bytes > 0)
10339 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10340 if (add_bytes > 0)
10341 inode_add_bytes(&inode->vfs_inode, add_bytes);
10342 spin_unlock(&inode->lock);
10343}
10344
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010345static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010346 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010347 .lookup = btrfs_lookup,
10348 .create = btrfs_create,
10349 .unlink = btrfs_unlink,
10350 .link = btrfs_link,
10351 .mkdir = btrfs_mkdir,
10352 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010353 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010354 .symlink = btrfs_symlink,
10355 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010356 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010357 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010358 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010359 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010360 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010361 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010362 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010363};
Yan, Zheng76dda932009-09-21 16:00:26 -040010364
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010365static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010366 .llseek = generic_file_llseek,
10367 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010368 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010369 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010370 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010371#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010372 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010373#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010374 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010375 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010376};
10377
Chris Mason35054392009-01-21 13:11:13 -050010378/*
10379 * btrfs doesn't support the bmap operation because swapfiles
10380 * use bmap to make a mapping of extents in the file. They assume
10381 * these extents won't change over the life of the file and they
10382 * use the bmap result to do IO directly to the drive.
10383 *
10384 * the btrfs bmap call would return logical addresses that aren't
10385 * suitable for IO and they also will change frequently as COW
10386 * operations happen. So, swapfile + btrfs == corruption.
10387 *
10388 * For now we're avoiding this by dropping bmap.
10389 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010390static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010391 .readpage = btrfs_readpage,
10392 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010393 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010394 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010395 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010396 .invalidatepage = btrfs_invalidatepage,
10397 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010398#ifdef CONFIG_MIGRATION
10399 .migratepage = btrfs_migratepage,
10400#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010401 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010402 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010403 .swap_activate = btrfs_swap_activate,
10404 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010405};
10406
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010407static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010408 .getattr = btrfs_getattr,
10409 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010410 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010411 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010412 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010413 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010414 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010415 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010416};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010417static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010418 .getattr = btrfs_getattr,
10419 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010420 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010421 .listxattr = btrfs_listxattr,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010422 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010423 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010424 .update_time = btrfs_update_time,
Josef Bacik618e21d2007-07-11 10:18:17 -040010425};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010426static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010427 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010428 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010429 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010430 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010431 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010432 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010433};
Yan, Zheng76dda932009-09-21 16:00:26 -040010434
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010435const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010436 .d_delete = btrfs_dentry_delete,
10437};