blob: 0dbe1aaa0b710648628b0cc2fc62ee82249e884b [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 |
Josef Bacikc2790a22013-07-29 11:20:47 -0400695 PAGE_CLEAR_DIRTY |
696 PAGE_SET_WRITEBACK |
Filipe Mananae6eb4312014-10-10 10:45:12 +0100697 page_error_op |
Josef Bacikc2790a22013-07-29 11:20:47 -0400698 PAGE_END_WRITEBACK);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300699
Qu Wenruo1e6e2382020-07-28 16:39:26 +0800700 /*
701 * Ensure we only free the compressed pages if we have
702 * them allocated, as we can still reach here with
703 * inode_need_compress() == false.
704 */
705 if (pages) {
706 for (i = 0; i < nr_pages; i++) {
707 WARN_ON(pages[i]->mapping);
708 put_page(pages[i]);
709 }
710 kfree(pages);
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300711 }
Nikolay Borisovcecc8d92019-07-17 14:41:45 +0300712 return 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400713 }
714 }
715
716 if (will_compress) {
717 /*
718 * we aren't doing an inline extent round the compressed size
719 * up to a block size boundary so the allocator does sane
720 * things
721 */
Qu Wenruofda28322013-02-26 08:10:22 +0000722 total_compressed = ALIGN(total_compressed, blocksize);
Chris Masonc8b97812008-10-29 14:49:59 -0400723
724 /*
725 * one last check to make sure the compression is really a
Timofey Titovets170607e2017-06-06 14:41:15 +0300726 * win, compare the page count read with the blocks on disk,
727 * compression must free at least one sector size
Chris Masonc8b97812008-10-29 14:49:59 -0400728 */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300729 total_in = ALIGN(total_in, PAGE_SIZE);
Timofey Titovets170607e2017-06-06 14:41:15 +0300730 if (total_compressed + blocksize <= total_in) {
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300731 compressed_extents++;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700732
733 /*
734 * The async work queues will take care of doing actual
735 * allocation on disk for these compressed pages, and
736 * will submit them to the elevator.
737 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200738 add_async_extent(async_chunk, start, total_in,
David Sterba4d3a8002017-02-14 19:04:07 +0100739 total_compressed, pages, nr_pages,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700740 compress_type);
741
Timofey Titovets11708622017-10-03 18:06:01 +0300742 if (start + total_in < end) {
743 start += total_in;
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700744 pages = NULL;
745 cond_resched();
746 goto again;
747 }
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300748 return compressed_extents;
Chris Masonc8b97812008-10-29 14:49:59 -0400749 }
750 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700751 if (pages) {
Chris Masonc8b97812008-10-29 14:49:59 -0400752 /*
753 * the compression code ran but failed to make things smaller,
754 * free any pages it allocated and our page pointer array
755 */
David Sterba4d3a8002017-02-14 19:04:07 +0100756 for (i = 0; i < nr_pages; i++) {
Chris Mason70b99e62008-10-31 12:46:39 -0400757 WARN_ON(pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300758 put_page(pages[i]);
Chris Masonc8b97812008-10-29 14:49:59 -0400759 }
760 kfree(pages);
761 pages = NULL;
762 total_compressed = 0;
David Sterba4d3a8002017-02-14 19:04:07 +0100763 nr_pages = 0;
Chris Masonc8b97812008-10-29 14:49:59 -0400764
765 /* flag the file so we don't compress in the future */
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400766 if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
David Sterbab52aa8c2017-07-17 19:17:20 +0200767 !(BTRFS_I(inode)->prop_compress)) {
Chris Masona555f812010-01-28 16:18:15 -0500768 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
Chris Mason1e701a32010-03-11 09:42:04 -0500769 }
Chris Masonc8b97812008-10-29 14:49:59 -0400770 }
Chris Masonf03d9301f2009-02-04 09:31:06 -0500771cleanup_and_bail_uncompressed:
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700772 /*
773 * No compression, but we still need to write the pages in the file
774 * we've been given so far. redirty the locked page if it corresponds
775 * to our extent and set things up for the async work queue to run
776 * cow_file_range to do the normal delalloc dance.
777 */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700778 if (async_chunk->locked_page &&
779 (page_offset(async_chunk->locked_page) >= start &&
780 page_offset(async_chunk->locked_page)) <= end) {
Nikolay Borisov1368c6d2019-03-12 17:20:27 +0200781 __set_page_dirty_nobuffers(async_chunk->locked_page);
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700782 /* unlocked later on in the async handlers */
Chris Mason1d53c9e2019-07-10 12:28:16 -0700783 }
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700784
785 if (redirty)
786 extent_range_redirty_for_io(inode, start, end);
Nikolay Borisovb5326272019-03-12 17:20:25 +0200787 add_async_extent(async_chunk, start, end - start + 1, 0, NULL, 0,
Ashish Samantc8bb0c82016-03-25 19:01:33 -0700788 BTRFS_COMPRESS_NONE);
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300789 compressed_extents++;
Chris Mason771ed682008-11-06 22:02:51 -0500790
Nikolay Borisovac3e9932019-07-17 14:41:44 +0300791 return compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -0500792}
Chris Mason771ed682008-11-06 22:02:51 -0500793
Filipe Manana40ae8372014-10-06 22:14:24 +0100794static void free_async_extent_pages(struct async_extent *async_extent)
795{
796 int i;
797
798 if (!async_extent->pages)
799 return;
800
801 for (i = 0; i < async_extent->nr_pages; i++) {
802 WARN_ON(async_extent->pages[i]->mapping);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300803 put_page(async_extent->pages[i]);
Filipe Manana40ae8372014-10-06 22:14:24 +0100804 }
805 kfree(async_extent->pages);
806 async_extent->nr_pages = 0;
807 async_extent->pages = NULL;
Chris Mason771ed682008-11-06 22:02:51 -0500808}
809
810/*
811 * phase two of compressed writeback. This is the ordered portion
812 * of the code, which only gets called in the order the work was
813 * queued. We walk all the async extents created by compress_file_range
814 * and send them down to the disk.
815 */
Nikolay Borisovb5326272019-03-12 17:20:25 +0200816static noinline void submit_compressed_extents(struct async_chunk *async_chunk)
Chris Mason771ed682008-11-06 22:02:51 -0500817{
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300818 struct btrfs_inode *inode = BTRFS_I(async_chunk->inode);
819 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -0500820 struct async_extent *async_extent;
821 u64 alloc_hint = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500822 struct btrfs_key ins;
823 struct extent_map *em;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300824 struct btrfs_root *root = inode->root;
825 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500826 int ret = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500827
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500828again:
Nikolay Borisovb5326272019-03-12 17:20:25 +0200829 while (!list_empty(&async_chunk->extents)) {
830 async_extent = list_entry(async_chunk->extents.next,
Chris Mason771ed682008-11-06 22:02:51 -0500831 struct async_extent, list);
832 list_del(&async_extent->list);
833
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500834retry:
Nikolay Borisov74475552019-03-12 17:20:30 +0200835 lock_extent(io_tree, async_extent->start,
836 async_extent->start + async_extent->ram_size - 1);
Chris Mason771ed682008-11-06 22:02:51 -0500837 /* did the compression code fall back to uncompressed IO? */
838 if (!async_extent->pages) {
839 int page_started = 0;
840 unsigned long nr_written = 0;
841
Chris Mason771ed682008-11-06 22:02:51 -0500842 /* allocate blocks */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300843 ret = cow_file_range(inode, async_chunk->locked_page,
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500844 async_extent->start,
845 async_extent->start +
846 async_extent->ram_size - 1,
Nikolay Borisov330a5822019-07-17 16:18:17 +0300847 &page_started, &nr_written, 0);
Chris Mason771ed682008-11-06 22:02:51 -0500848
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100849 /* JDM XXX */
850
Chris Mason771ed682008-11-06 22:02:51 -0500851 /*
852 * if page_started, cow_file_range inserted an
853 * inline extent and took care of all the unlocking
854 * and IO for us. Otherwise, we need to submit
855 * all those pages down to the drive.
856 */
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500857 if (!page_started && !ret)
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300858 extent_write_locked_range(&inode->vfs_inode,
Nikolay Borisov5e3ee232017-12-08 15:55:58 +0200859 async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500860 async_extent->start +
Chris Mason771ed682008-11-06 22:02:51 -0500861 async_extent->ram_size - 1,
Chris Mason771ed682008-11-06 22:02:51 -0500862 WB_SYNC_ALL);
Chris Mason1d53c9e2019-07-10 12:28:16 -0700863 else if (ret && async_chunk->locked_page)
Nikolay Borisovb5326272019-03-12 17:20:25 +0200864 unlock_page(async_chunk->locked_page);
Chris Mason771ed682008-11-06 22:02:51 -0500865 kfree(async_extent);
866 cond_resched();
867 continue;
868 }
869
Wang Xiaoguang18513092016-07-25 15:51:40 +0800870 ret = btrfs_reserve_extent(root, async_extent->ram_size,
Chris Mason771ed682008-11-06 22:02:51 -0500871 async_extent->compressed_size,
872 async_extent->compressed_size,
Miao Xiee570fd22014-06-19 10:42:50 +0800873 0, alloc_hint, &ins, 1, 1);
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500874 if (ret) {
Filipe Manana40ae8372014-10-06 22:14:24 +0100875 free_async_extent_pages(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500876
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400877 if (ret == -ENOSPC) {
878 unlock_extent(io_tree, async_extent->start,
879 async_extent->start +
880 async_extent->ram_size - 1);
Liu Boce620032014-07-24 22:48:05 +0800881
882 /*
883 * we need to redirty the pages if we decide to
884 * fallback to uncompressed IO, otherwise we
885 * will not submit these pages down to lower
886 * layers.
887 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300888 extent_range_redirty_for_io(&inode->vfs_inode,
Liu Boce620032014-07-24 22:48:05 +0800889 async_extent->start,
890 async_extent->start +
891 async_extent->ram_size - 1);
892
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100893 goto retry;
Josef Bacikfdf8e2e2013-06-14 16:58:23 -0400894 }
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500895 goto out_free;
Josef Bacikf5a84ee32009-11-10 21:23:48 -0500896 }
Yan, Zhengc2167752009-11-12 09:34:21 +0000897 /*
898 * here we're doing allocation and writeback of the
899 * compressed pages
900 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300901 em = create_io_em(inode, async_extent->start,
Liu Bo6f9994d2017-01-31 07:50:22 -0800902 async_extent->ram_size, /* len */
903 async_extent->start, /* orig_start */
904 ins.objectid, /* block_start */
905 ins.offset, /* block_len */
906 ins.offset, /* orig_block_len */
907 async_extent->ram_size, /* ram_bytes */
908 async_extent->compress_type,
909 BTRFS_ORDERED_COMPRESSED);
910 if (IS_ERR(em))
911 /* ret value is not necessary due to void function */
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500912 goto out_free_reserve;
Liu Bo6f9994d2017-01-31 07:50:22 -0800913 free_extent_map(em);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500914
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300915 ret = btrfs_add_ordered_extent_compress(inode,
Li Zefan261507a02010-12-17 14:21:50 +0800916 async_extent->start,
917 ins.objectid,
918 async_extent->ram_size,
919 ins.offset,
Li Zefan261507a02010-12-17 14:21:50 +0800920 async_extent->compress_type);
Filipe Mananad9f85962014-08-25 10:43:00 +0100921 if (ret) {
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300922 btrfs_drop_extent_cache(inode, async_extent->start,
Filipe Mananad9f85962014-08-25 10:43:00 +0100923 async_extent->start +
924 async_extent->ram_size - 1, 0);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500925 goto out_free_reserve;
Filipe Mananad9f85962014-08-25 10:43:00 +0100926 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400927 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500928
Chris Mason771ed682008-11-06 22:02:51 -0500929 /*
930 * clear dirty, set writeback and unlock the pages.
931 */
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300932 extent_clear_unlock_delalloc(inode, async_extent->start,
Chris Masona791e352009-10-08 11:27:10 -0400933 async_extent->start +
934 async_extent->ram_size - 1,
Josef Bacik151a41b2013-07-29 13:22:24 -0400935 NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
936 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -0400937 PAGE_SET_WRITEBACK);
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300938 if (btrfs_submit_compressed_write(inode, async_extent->start,
Chris Masond3977122009-01-05 21:25:51 -0500939 async_extent->ram_size,
940 ins.objectid,
941 ins.offset, async_extent->pages,
Liu Bof82b7352017-10-23 23:18:16 -0600942 async_extent->nr_pages,
Chris Masonec39f762019-07-10 12:28:17 -0700943 async_chunk->write_flags,
944 async_chunk->blkcg_css)) {
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100945 struct page *p = async_extent->pages[0];
946 const u64 start = async_extent->start;
947 const u64 end = start + async_extent->ram_size - 1;
948
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300949 p->mapping = inode->vfs_inode.i_mapping;
Nikolay Borisovc6297322018-11-08 10:18:08 +0200950 btrfs_writepage_endio_finish_ordered(p, start, end, 0);
Nikolay Borisov7087a9d2018-11-01 14:09:48 +0200951
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100952 p->mapping = NULL;
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300953 extent_clear_unlock_delalloc(inode, start, end, NULL, 0,
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100954 PAGE_END_WRITEBACK |
955 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100956 free_async_extent_pages(async_extent);
Filipe Mananafce2a4e2014-10-06 22:14:23 +0100957 }
Chris Mason771ed682008-11-06 22:02:51 -0500958 alloc_hint = ins.objectid + ins.offset;
959 kfree(async_extent);
960 cond_resched();
961 }
Filipe Mananadec8f172014-10-06 22:14:26 +0100962 return;
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500963out_free_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400964 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -0400965 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100966out_free:
Nikolay Borisova0ff10d2020-06-03 08:55:17 +0300967 extent_clear_unlock_delalloc(inode, async_extent->start,
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500968 async_extent->start +
969 async_extent->ram_size - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -0400970 NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
Filipe Mananaa7e3b972017-04-03 10:45:46 +0100971 EXTENT_DELALLOC_NEW |
Josef Bacik151a41b2013-07-29 13:22:24 -0400972 EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
973 PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
Filipe Manana704de492014-10-06 22:14:22 +0100974 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
975 PAGE_SET_ERROR);
Filipe Manana40ae8372014-10-06 22:14:24 +0100976 free_async_extent_pages(async_extent);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100977 kfree(async_extent);
Josef Bacik3e04e7f2013-02-06 16:49:15 -0500978 goto again;
Chris Mason771ed682008-11-06 22:02:51 -0500979}
980
Nikolay Borisov43c69842020-06-03 08:55:02 +0300981static u64 get_extent_allocation_hint(struct btrfs_inode *inode, u64 start,
Josef Bacik4b46fce2010-05-23 11:00:55 -0400982 u64 num_bytes)
983{
Nikolay Borisov43c69842020-06-03 08:55:02 +0300984 struct extent_map_tree *em_tree = &inode->extent_tree;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400985 struct extent_map *em;
986 u64 alloc_hint = 0;
987
988 read_lock(&em_tree->lock);
989 em = search_extent_mapping(em_tree, start, num_bytes);
990 if (em) {
991 /*
992 * if block start isn't an actual block number then find the
993 * first block in this inode and use that as a hint. If that
994 * block is also bogus then just don't worry about it.
995 */
996 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
997 free_extent_map(em);
998 em = search_extent_mapping(em_tree, 0, 0);
999 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
1000 alloc_hint = em->block_start;
1001 if (em)
1002 free_extent_map(em);
1003 } else {
1004 alloc_hint = em->block_start;
1005 free_extent_map(em);
1006 }
1007 }
1008 read_unlock(&em_tree->lock);
1009
1010 return alloc_hint;
1011}
1012
Chris Mason771ed682008-11-06 22:02:51 -05001013/*
1014 * when extent_io.c finds a delayed allocation range in the file,
1015 * the call backs end up in this code. The basic idea is to
1016 * allocate extents on disk for the range, and create ordered data structs
1017 * in ram to track those extents.
1018 *
1019 * locked_page is the page that writepage had locked already. We use
1020 * it to make sure we don't do extra locks or unlocks.
1021 *
1022 * *page_started is set to one if we unlock locked_page and do everything
1023 * required to start IO on it. It may be clean and already done with
1024 * IO when we return.
1025 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001026static noinline int cow_file_range(struct btrfs_inode *inode,
Josef Bacik00361582013-08-14 14:02:47 -04001027 struct page *locked_page,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001028 u64 start, u64 end, int *page_started,
Nikolay Borisov330a5822019-07-17 16:18:17 +03001029 unsigned long *nr_written, int unlock)
Chris Mason771ed682008-11-06 22:02:51 -05001030{
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001031 struct btrfs_root *root = inode->root;
1032 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason771ed682008-11-06 22:02:51 -05001033 u64 alloc_hint = 0;
1034 u64 num_bytes;
1035 unsigned long ram_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001036 u64 cur_alloc_size = 0;
Filipe Manana432cd2a2020-06-08 13:32:55 +01001037 u64 min_alloc_size;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001038 u64 blocksize = fs_info->sectorsize;
Chris Mason771ed682008-11-06 22:02:51 -05001039 struct btrfs_key ins;
1040 struct extent_map *em;
Filipe Mananaa315e682017-03-06 23:04:20 +00001041 unsigned clear_bits;
1042 unsigned long page_ops;
1043 bool extent_reserved = false;
Chris Mason771ed682008-11-06 22:02:51 -05001044 int ret = 0;
1045
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001046 if (btrfs_is_free_space_inode(inode)) {
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001047 WARN_ON_ONCE(1);
Josef Bacik29bce2f2014-02-07 12:21:23 -05001048 ret = -EINVAL;
1049 goto out_unlock;
Josef Bacik02ecd2c2013-10-25 16:19:08 -04001050 }
Chris Mason771ed682008-11-06 22:02:51 -05001051
Qu Wenruofda28322013-02-26 08:10:22 +00001052 num_bytes = ALIGN(end - start + 1, blocksize);
Chris Mason771ed682008-11-06 22:02:51 -05001053 num_bytes = max(blocksize, num_bytes);
Anand Jain566b1762018-02-15 18:07:59 +08001054 ASSERT(num_bytes <= btrfs_super_total_bytes(fs_info->super_copy));
Chris Mason771ed682008-11-06 22:02:51 -05001055
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001056 inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
Chris Mason4cb53002011-05-24 15:35:30 -04001057
Chris Mason771ed682008-11-06 22:02:51 -05001058 if (start == 0) {
1059 /* lets try to make an inline extent */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001060 ret = cow_file_range_inline(inode, start, end, 0,
Nikolay Borisovd02c0e22018-03-02 09:43:15 +02001061 BTRFS_COMPRESS_NONE, NULL);
Chris Mason771ed682008-11-06 22:02:51 -05001062 if (ret == 0) {
Josef Bacik8b62f872017-10-19 14:15:55 -04001063 /*
1064 * We use DO_ACCOUNTING here because we need the
1065 * delalloc_release_metadata to be run _after_ we drop
1066 * our outstanding extent for clearing delalloc for this
1067 * range.
1068 */
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001069 extent_clear_unlock_delalloc(inode, start, end, NULL,
Josef Bacikc2790a22013-07-29 11:20:47 -04001070 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik8b62f872017-10-19 14:15:55 -04001071 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1072 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001073 PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1074 PAGE_END_WRITEBACK);
Chris Mason771ed682008-11-06 22:02:51 -05001075 *nr_written = *nr_written +
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001076 (end - start + PAGE_SIZE) / PAGE_SIZE;
Chris Mason771ed682008-11-06 22:02:51 -05001077 *page_started = 1;
Chris Mason771ed682008-11-06 22:02:51 -05001078 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001079 } else if (ret < 0) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001080 goto out_unlock;
Chris Mason771ed682008-11-06 22:02:51 -05001081 }
1082 }
Chris Masonc8b97812008-10-29 14:49:59 -04001083
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001084 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
1085 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Mason3b951512008-04-17 11:29:12 -04001086
Filipe Manana432cd2a2020-06-08 13:32:55 +01001087 /*
1088 * Relocation relies on the relocated extents to have exactly the same
1089 * size as the original extents. Normally writeback for relocation data
1090 * extents follows a NOCOW path because relocation preallocates the
1091 * extents. However, due to an operation such as scrub turning a block
1092 * group to RO mode, it may fallback to COW mode, so we must make sure
1093 * an extent allocated during COW has exactly the requested size and can
1094 * not be split into smaller extents, otherwise relocation breaks and
1095 * fails during the stage where it updates the bytenr of file extent
1096 * items.
1097 */
1098 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1099 min_alloc_size = num_bytes;
1100 else
1101 min_alloc_size = fs_info->sectorsize;
1102
Anand Jain3752d222018-02-15 12:29:38 +08001103 while (num_bytes > 0) {
1104 cur_alloc_size = num_bytes;
Wang Xiaoguang18513092016-07-25 15:51:40 +08001105 ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
Filipe Manana432cd2a2020-06-08 13:32:55 +01001106 min_alloc_size, 0, alloc_hint,
Miao Xiee570fd22014-06-19 10:42:50 +08001107 &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04001108 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001109 goto out_unlock;
Filipe Mananaa315e682017-03-06 23:04:20 +00001110 cur_alloc_size = ins.offset;
1111 extent_reserved = true;
Chris Masond3977122009-01-05 21:25:51 -05001112
Chris Mason771ed682008-11-06 22:02:51 -05001113 ram_size = ins.offset;
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001114 em = create_io_em(inode, start, ins.offset, /* len */
Liu Bo6f9994d2017-01-31 07:50:22 -08001115 start, /* orig_start */
1116 ins.objectid, /* block_start */
1117 ins.offset, /* block_len */
1118 ins.offset, /* orig_block_len */
1119 ram_size, /* ram_bytes */
1120 BTRFS_COMPRESS_NONE, /* compress_type */
Liu Bo1af4a0a2017-02-13 15:35:09 -08001121 BTRFS_ORDERED_REGULAR /* type */);
Su Yue090a127a2018-05-30 16:48:56 +08001122 if (IS_ERR(em)) {
1123 ret = PTR_ERR(em);
Liu Boace68ba2013-04-22 10:53:47 +00001124 goto out_reserve;
Su Yue090a127a2018-05-30 16:48:56 +08001125 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001126 free_extent_map(em);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001127
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001128 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Qu Wenruo3c198fe2021-01-21 14:13:54 +08001129 ram_size, cur_alloc_size,
1130 BTRFS_ORDERED_REGULAR);
Liu Boace68ba2013-04-22 10:53:47 +00001131 if (ret)
Filipe Mananad9f85962014-08-25 10:43:00 +01001132 goto out_drop_extent_cache;
Chris Masonc8b97812008-10-29 14:49:59 -04001133
Yan Zheng17d217f2008-12-12 10:03:38 -05001134 if (root->root_key.objectid ==
1135 BTRFS_DATA_RELOC_TREE_OBJECTID) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001136 ret = btrfs_reloc_clone_csums(inode, start,
Yan Zheng17d217f2008-12-12 10:03:38 -05001137 cur_alloc_size);
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001138 /*
1139 * Only drop cache here, and process as normal.
1140 *
1141 * We must not allow extent_clear_unlock_delalloc()
1142 * at out_unlock label to free meta of this ordered
1143 * extent, as its meta should be freed by
1144 * btrfs_finish_ordered_io().
1145 *
1146 * So we must continue until @start is increased to
1147 * skip current ordered extent.
1148 */
Josef Bacik00361582013-08-14 14:02:47 -04001149 if (ret)
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001150 btrfs_drop_extent_cache(inode, start,
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001151 start + ram_size - 1, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001152 }
1153
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001154 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana9cfa3e32016-04-26 15:39:32 +01001155
Chris Masonc8b97812008-10-29 14:49:59 -04001156 /* we're not doing compressed IO, don't unlock the first
1157 * page (which the caller expects to stay locked), don't
1158 * clear any dirty bits and don't set any writeback bits
Chris Mason8b62b722009-09-02 16:53:46 -04001159 *
1160 * Do set the Private2 bit so we know this page was properly
1161 * setup for writepage
Chris Masonc8b97812008-10-29 14:49:59 -04001162 */
Filipe Mananaa315e682017-03-06 23:04:20 +00001163 page_ops = unlock ? PAGE_UNLOCK : 0;
1164 page_ops |= PAGE_SET_PRIVATE2;
Chris Masona791e352009-10-08 11:27:10 -04001165
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001166 extent_clear_unlock_delalloc(inode, start, start + ram_size - 1,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001167 locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001168 EXTENT_LOCKED | EXTENT_DELALLOC,
Filipe Mananaa315e682017-03-06 23:04:20 +00001169 page_ops);
Anand Jain3752d222018-02-15 12:29:38 +08001170 if (num_bytes < cur_alloc_size)
1171 num_bytes = 0;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001172 else
Anand Jain3752d222018-02-15 12:29:38 +08001173 num_bytes -= cur_alloc_size;
Chris Masonc59f8952007-12-17 20:14:04 -05001174 alloc_hint = ins.objectid + ins.offset;
1175 start += cur_alloc_size;
Filipe Mananaa315e682017-03-06 23:04:20 +00001176 extent_reserved = false;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001177
1178 /*
1179 * btrfs_reloc_clone_csums() error, since start is increased
1180 * extent_clear_unlock_delalloc() at out_unlock label won't
1181 * free metadata of current ordered extent, we're OK to exit.
1182 */
1183 if (ret)
1184 goto out_unlock;
Chris Masonb888db22007-08-27 16:49:44 -04001185 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001186out:
Chris Masonbe20aa92007-12-17 20:14:01 -05001187 return ret;
Miao Xieb7d5b0a2012-11-01 07:32:18 +00001188
Filipe Mananad9f85962014-08-25 10:43:00 +01001189out_drop_extent_cache:
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001190 btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
Liu Boace68ba2013-04-22 10:53:47 +00001191out_reserve:
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001192 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001193 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001194out_unlock:
Filipe Mananaa7e3b972017-04-03 10:45:46 +01001195 clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DELALLOC_NEW |
1196 EXTENT_DEFRAG | EXTENT_CLEAR_META_RESV;
Filipe Mananaa315e682017-03-06 23:04:20 +00001197 page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
1198 PAGE_END_WRITEBACK;
1199 /*
1200 * If we reserved an extent for our delalloc range (or a subrange) and
1201 * failed to create the respective ordered extent, then it means that
1202 * when we reserved the extent we decremented the extent's size from
1203 * the data space_info's bytes_may_use counter and incremented the
1204 * space_info's bytes_reserved counter by the same amount. We must make
1205 * sure extent_clear_unlock_delalloc() does not try to decrement again
1206 * the data space_info's bytes_may_use counter, therefore we do not pass
1207 * it the flag EXTENT_CLEAR_DATA_RESV.
1208 */
1209 if (extent_reserved) {
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001210 extent_clear_unlock_delalloc(inode, start,
Filipe Mananae2c8e922020-05-27 11:15:53 +01001211 start + cur_alloc_size - 1,
Filipe Mananaa315e682017-03-06 23:04:20 +00001212 locked_page,
1213 clear_bits,
1214 page_ops);
1215 start += cur_alloc_size;
1216 if (start >= end)
1217 goto out;
1218 }
Nikolay Borisov6e26c442020-06-03 08:55:14 +03001219 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Filipe Mananaa315e682017-03-06 23:04:20 +00001220 clear_bits | EXTENT_CLEAR_DATA_RESV,
1221 page_ops);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001222 goto out;
Chris Mason771ed682008-11-06 22:02:51 -05001223}
Chris Masonc8b97812008-10-29 14:49:59 -04001224
Chris Mason771ed682008-11-06 22:02:51 -05001225/*
1226 * work queue call back to started compression on a file and pages
1227 */
1228static noinline void async_cow_start(struct btrfs_work *work)
1229{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001230 struct async_chunk *async_chunk;
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001231 int compressed_extents;
Chris Mason771ed682008-11-06 22:02:51 -05001232
Nikolay Borisovb5326272019-03-12 17:20:25 +02001233 async_chunk = container_of(work, struct async_chunk, work);
Chris Mason771ed682008-11-06 22:02:51 -05001234
Nikolay Borisovac3e9932019-07-17 14:41:44 +03001235 compressed_extents = compress_file_range(async_chunk);
1236 if (compressed_extents == 0) {
Nikolay Borisovb5326272019-03-12 17:20:25 +02001237 btrfs_add_delayed_iput(async_chunk->inode);
1238 async_chunk->inode = NULL;
Josef Bacik8180ef82012-06-08 15:16:12 -04001239 }
Chris Mason771ed682008-11-06 22:02:51 -05001240}
1241
1242/*
1243 * work queue call back to submit previously compressed pages
1244 */
1245static noinline void async_cow_submit(struct btrfs_work *work)
1246{
Nikolay Borisovc5a68ae2019-03-12 17:20:26 +02001247 struct async_chunk *async_chunk = container_of(work, struct async_chunk,
1248 work);
1249 struct btrfs_fs_info *fs_info = btrfs_work_owner(work);
Chris Mason771ed682008-11-06 22:02:51 -05001250 unsigned long nr_pages;
1251
Nikolay Borisovb5326272019-03-12 17:20:25 +02001252 nr_pages = (async_chunk->end - async_chunk->start + PAGE_SIZE) >>
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001253 PAGE_SHIFT;
Chris Mason771ed682008-11-06 22:02:51 -05001254
David Sterba093258e2018-02-26 16:15:17 +01001255 /* atomic_sub_return implies a barrier */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001256 if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
David Sterba093258e2018-02-26 16:15:17 +01001257 5 * SZ_1M)
1258 cond_wake_up_nomb(&fs_info->async_submit_wait);
Chris Mason771ed682008-11-06 22:02:51 -05001259
Nikolay Borisov4546d172019-01-03 10:50:03 +02001260 /*
Nikolay Borisovb5326272019-03-12 17:20:25 +02001261 * ->inode could be NULL if async_chunk_start has failed to compress,
Nikolay Borisov4546d172019-01-03 10:50:03 +02001262 * in which case we don't have anything to submit, yet we need to
1263 * always adjust ->async_delalloc_pages as its paired with the init
1264 * happening in cow_file_range_async
1265 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001266 if (async_chunk->inode)
1267 submit_compressed_extents(async_chunk);
Chris Mason771ed682008-11-06 22:02:51 -05001268}
Chris Masonc8b97812008-10-29 14:49:59 -04001269
Chris Mason771ed682008-11-06 22:02:51 -05001270static noinline void async_cow_free(struct btrfs_work *work)
1271{
Nikolay Borisovb5326272019-03-12 17:20:25 +02001272 struct async_chunk *async_chunk;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001273
Nikolay Borisovb5326272019-03-12 17:20:25 +02001274 async_chunk = container_of(work, struct async_chunk, work);
1275 if (async_chunk->inode)
1276 btrfs_add_delayed_iput(async_chunk->inode);
Chris Masonec39f762019-07-10 12:28:17 -07001277 if (async_chunk->blkcg_css)
1278 css_put(async_chunk->blkcg_css);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001279 /*
1280 * Since the pointer to 'pending' is at the beginning of the array of
Nikolay Borisovb5326272019-03-12 17:20:25 +02001281 * async_chunk's, freeing it ensures the whole array has been freed.
Nikolay Borisov97db1202019-03-12 17:20:24 +02001282 */
Nikolay Borisovb5326272019-03-12 17:20:25 +02001283 if (atomic_dec_and_test(async_chunk->pending))
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001284 kvfree(async_chunk->pending);
Chris Mason771ed682008-11-06 22:02:51 -05001285}
1286
Nikolay Borisov751b6432020-06-03 08:55:22 +03001287static int cow_file_range_async(struct btrfs_inode *inode,
Chris Masonec39f762019-07-10 12:28:17 -07001288 struct writeback_control *wbc,
1289 struct page *locked_page,
Chris Mason771ed682008-11-06 22:02:51 -05001290 u64 start, u64 end, int *page_started,
David Sterbafac07d22019-10-29 18:28:57 +01001291 unsigned long *nr_written)
Chris Mason771ed682008-11-06 22:02:51 -05001292{
Nikolay Borisov751b6432020-06-03 08:55:22 +03001293 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Chris Masonec39f762019-07-10 12:28:17 -07001294 struct cgroup_subsys_state *blkcg_css = wbc_blkcg_css(wbc);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001295 struct async_cow *ctx;
1296 struct async_chunk *async_chunk;
Chris Mason771ed682008-11-06 22:02:51 -05001297 unsigned long nr_pages;
1298 u64 cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001299 u64 num_chunks = DIV_ROUND_UP(end - start, SZ_512K);
1300 int i;
1301 bool should_compress;
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001302 unsigned nofs_flag;
David Sterbafac07d22019-10-29 18:28:57 +01001303 const unsigned int write_flags = wbc_to_write_flags(wbc);
Chris Mason771ed682008-11-06 22:02:51 -05001304
Nikolay Borisov751b6432020-06-03 08:55:22 +03001305 unlock_extent(&inode->io_tree, start, end);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001306
Nikolay Borisov751b6432020-06-03 08:55:22 +03001307 if (inode->flags & BTRFS_INODE_NOCOMPRESS &&
Nikolay Borisov97db1202019-03-12 17:20:24 +02001308 !btrfs_test_opt(fs_info, FORCE_COMPRESS)) {
1309 num_chunks = 1;
1310 should_compress = false;
1311 } else {
1312 should_compress = true;
1313 }
1314
Nikolay Borisovb1c16ac2019-04-01 11:29:57 +03001315 nofs_flag = memalloc_nofs_save();
1316 ctx = kvmalloc(struct_size(ctx, chunks, num_chunks), GFP_KERNEL);
1317 memalloc_nofs_restore(nofs_flag);
1318
Nikolay Borisov97db1202019-03-12 17:20:24 +02001319 if (!ctx) {
1320 unsigned clear_bits = EXTENT_LOCKED | EXTENT_DELALLOC |
1321 EXTENT_DELALLOC_NEW | EXTENT_DEFRAG |
1322 EXTENT_DO_ACCOUNTING;
1323 unsigned long page_ops = PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1324 PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
1325 PAGE_SET_ERROR;
1326
Nikolay Borisov751b6432020-06-03 08:55:22 +03001327 extent_clear_unlock_delalloc(inode, start, end, locked_page,
1328 clear_bits, page_ops);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001329 return -ENOMEM;
1330 }
1331
1332 async_chunk = ctx->chunks;
1333 atomic_set(&ctx->num_chunks, num_chunks);
1334
1335 for (i = 0; i < num_chunks; i++) {
1336 if (should_compress)
1337 cur_end = min(end, start + SZ_512K - 1);
1338 else
1339 cur_end = end;
1340
Nikolay Borisovbd4691a2019-01-03 10:50:01 +02001341 /*
1342 * igrab is called higher up in the call chain, take only the
1343 * lightweight reference for the callback lifetime
1344 */
Nikolay Borisov751b6432020-06-03 08:55:22 +03001345 ihold(&inode->vfs_inode);
Nikolay Borisov97db1202019-03-12 17:20:24 +02001346 async_chunk[i].pending = &ctx->num_chunks;
Nikolay Borisov751b6432020-06-03 08:55:22 +03001347 async_chunk[i].inode = &inode->vfs_inode;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001348 async_chunk[i].start = start;
1349 async_chunk[i].end = cur_end;
Nikolay Borisov97db1202019-03-12 17:20:24 +02001350 async_chunk[i].write_flags = write_flags;
1351 INIT_LIST_HEAD(&async_chunk[i].extents);
Chris Mason771ed682008-11-06 22:02:51 -05001352
Chris Mason1d53c9e2019-07-10 12:28:16 -07001353 /*
1354 * The locked_page comes all the way from writepage and its
1355 * the original page we were actually given. As we spread
1356 * this large delalloc region across multiple async_chunk
1357 * structs, only the first struct needs a pointer to locked_page
1358 *
1359 * This way we don't need racey decisions about who is supposed
1360 * to unlock it.
1361 */
1362 if (locked_page) {
Chris Masonec39f762019-07-10 12:28:17 -07001363 /*
1364 * Depending on the compressibility, the pages might or
1365 * might not go through async. We want all of them to
1366 * be accounted against wbc once. Let's do it here
1367 * before the paths diverge. wbc accounting is used
1368 * only for foreign writeback detection and doesn't
1369 * need full accuracy. Just account the whole thing
1370 * against the first page.
1371 */
1372 wbc_account_cgroup_owner(wbc, locked_page,
1373 cur_end - start);
Chris Mason1d53c9e2019-07-10 12:28:16 -07001374 async_chunk[i].locked_page = locked_page;
1375 locked_page = NULL;
1376 } else {
1377 async_chunk[i].locked_page = NULL;
1378 }
1379
Chris Masonec39f762019-07-10 12:28:17 -07001380 if (blkcg_css != blkcg_root_css) {
1381 css_get(blkcg_css);
1382 async_chunk[i].blkcg_css = blkcg_css;
1383 } else {
1384 async_chunk[i].blkcg_css = NULL;
1385 }
1386
Omar Sandovala0cac0e2019-09-16 11:30:57 -07001387 btrfs_init_work(&async_chunk[i].work, async_cow_start,
1388 async_cow_submit, async_cow_free);
Chris Mason771ed682008-11-06 22:02:51 -05001389
Nikolay Borisov97db1202019-03-12 17:20:24 +02001390 nr_pages = DIV_ROUND_UP(cur_end - start, PAGE_SIZE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001391 atomic_add(nr_pages, &fs_info->async_delalloc_pages);
Chris Mason771ed682008-11-06 22:02:51 -05001392
Nikolay Borisov97db1202019-03-12 17:20:24 +02001393 btrfs_queue_work(fs_info->delalloc_workers, &async_chunk[i].work);
Chris Mason771ed682008-11-06 22:02:51 -05001394
Chris Mason771ed682008-11-06 22:02:51 -05001395 *nr_written += nr_pages;
1396 start = cur_end + 1;
1397 }
1398 *page_started = 1;
1399 return 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001400}
1401
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001402static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
Yan Zheng17d217f2008-12-12 10:03:38 -05001403 u64 bytenr, u64 num_bytes)
1404{
1405 int ret;
1406 struct btrfs_ordered_sum *sums;
1407 LIST_HEAD(list);
1408
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001409 ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
Arne Jansena2de7332011-03-08 14:14:00 +01001410 bytenr + num_bytes - 1, &list, 0);
Yan Zheng17d217f2008-12-12 10:03:38 -05001411 if (ret == 0 && list_empty(&list))
1412 return 0;
1413
1414 while (!list_empty(&list)) {
1415 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1416 list_del(&sums->list);
1417 kfree(sums);
1418 }
Liu Bo58113752018-01-31 17:09:13 -07001419 if (ret < 0)
1420 return ret;
Yan Zheng17d217f2008-12-12 10:03:38 -05001421 return 1;
1422}
1423
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001424static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page,
Filipe Manana467dc472020-05-27 11:16:07 +01001425 const u64 start, const u64 end,
1426 int *page_started, unsigned long *nr_written)
1427{
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001428 const bool is_space_ino = btrfs_is_free_space_inode(inode);
1429 const bool is_reloc_ino = (inode->root->root_key.objectid ==
Filipe Manana6bd335b2020-06-08 13:33:05 +01001430 BTRFS_DATA_RELOC_TREE_OBJECTID);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001431 const u64 range_bytes = end + 1 - start;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001432 struct extent_io_tree *io_tree = &inode->io_tree;
Filipe Manana467dc472020-05-27 11:16:07 +01001433 u64 range_start = start;
1434 u64 count;
1435
1436 /*
1437 * If EXTENT_NORESERVE is set it means that when the buffered write was
1438 * made we had not enough available data space and therefore we did not
1439 * reserve data space for it, since we though we could do NOCOW for the
1440 * respective file range (either there is prealloc extent or the inode
1441 * has the NOCOW bit set).
1442 *
1443 * However when we need to fallback to COW mode (because for example the
1444 * block group for the corresponding extent was turned to RO mode by a
1445 * scrub or relocation) we need to do the following:
1446 *
1447 * 1) We increment the bytes_may_use counter of the data space info.
1448 * If COW succeeds, it allocates a new data extent and after doing
1449 * that it decrements the space info's bytes_may_use counter and
1450 * increments its bytes_reserved counter by the same amount (we do
1451 * this at btrfs_add_reserved_bytes()). So we need to increment the
1452 * bytes_may_use counter to compensate (when space is reserved at
1453 * buffered write time, the bytes_may_use counter is incremented);
1454 *
1455 * 2) We clear the EXTENT_NORESERVE bit from the range. We do this so
1456 * that if the COW path fails for any reason, it decrements (through
1457 * extent_clear_unlock_delalloc()) the bytes_may_use counter of the
1458 * data space info, which we incremented in the step above.
Filipe Manana2166e5e2020-05-27 11:16:19 +01001459 *
1460 * If we need to fallback to cow and the inode corresponds to a free
Filipe Manana6bd335b2020-06-08 13:33:05 +01001461 * space cache inode or an inode of the data relocation tree, we must
1462 * also increment bytes_may_use of the data space_info for the same
1463 * reason. Space caches and relocated data extents always get a prealloc
Filipe Manana2166e5e2020-05-27 11:16:19 +01001464 * extent for them, however scrub or balance may have set the block
Filipe Manana6bd335b2020-06-08 13:33:05 +01001465 * group that contains that extent to RO mode and therefore force COW
1466 * when starting writeback.
Filipe Manana467dc472020-05-27 11:16:07 +01001467 */
Filipe Manana2166e5e2020-05-27 11:16:19 +01001468 count = count_range_bits(io_tree, &range_start, end, range_bytes,
Filipe Manana467dc472020-05-27 11:16:07 +01001469 EXTENT_NORESERVE, 0);
Filipe Manana6bd335b2020-06-08 13:33:05 +01001470 if (count > 0 || is_space_ino || is_reloc_ino) {
1471 u64 bytes = count;
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001472 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Filipe Manana467dc472020-05-27 11:16:07 +01001473 struct btrfs_space_info *sinfo = fs_info->data_sinfo;
1474
Filipe Manana6bd335b2020-06-08 13:33:05 +01001475 if (is_space_ino || is_reloc_ino)
1476 bytes = range_bytes;
1477
Filipe Manana467dc472020-05-27 11:16:07 +01001478 spin_lock(&sinfo->lock);
Filipe Manana2166e5e2020-05-27 11:16:19 +01001479 btrfs_space_info_update_bytes_may_use(fs_info, sinfo, bytes);
Filipe Manana467dc472020-05-27 11:16:07 +01001480 spin_unlock(&sinfo->lock);
1481
Filipe Manana2166e5e2020-05-27 11:16:19 +01001482 if (count > 0)
1483 clear_extent_bit(io_tree, start, end, EXTENT_NORESERVE,
1484 0, 0, NULL);
Filipe Manana467dc472020-05-27 11:16:07 +01001485 }
1486
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001487 return cow_file_range(inode, locked_page, start, end, page_started,
1488 nr_written, 1);
Filipe Manana467dc472020-05-27 11:16:07 +01001489}
1490
Chris Masond352ac62008-09-29 15:18:18 -04001491/*
1492 * when nowcow writeback call back. This checks for snapshots or COW copies
1493 * of the extents that exist in the file, and COWs the file as required.
1494 *
1495 * If no cow copies or snapshots exist, we write directly to the existing
1496 * blocks on disk
1497 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001498static noinline int run_delalloc_nocow(struct btrfs_inode *inode,
Chris Mason7f366cf2009-03-12 20:12:45 -04001499 struct page *locked_page,
Nikolay Borisov3e024842019-08-21 10:42:03 +03001500 const u64 start, const u64 end,
1501 int *page_started, int force,
1502 unsigned long *nr_written)
Chris Masonbe20aa92007-12-17 20:14:01 -05001503{
Nikolay Borisov968322c2020-06-03 08:55:21 +03001504 struct btrfs_fs_info *fs_info = inode->root->fs_info;
1505 struct btrfs_root *root = inode->root;
Chris Masonbe20aa92007-12-17 20:14:01 -05001506 struct btrfs_path *path;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001507 u64 cow_start = (u64)-1;
1508 u64 cur_offset = start;
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001509 int ret;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001510 bool check_prev = true;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001511 const bool freespace_inode = btrfs_is_free_space_inode(inode);
1512 u64 ino = btrfs_ino(inode);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001513 bool nocow = false;
1514 u64 disk_bytenr = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001515
1516 path = btrfs_alloc_path();
Josef Bacik17ca04a2012-05-31 15:58:55 -04001517 if (!path) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001518 extent_clear_unlock_delalloc(inode, start, end, locked_page,
Josef Bacikc2790a22013-07-29 11:20:47 -04001519 EXTENT_LOCKED | EXTENT_DELALLOC |
Josef Bacik151a41b2013-07-29 13:22:24 -04001520 EXTENT_DO_ACCOUNTING |
1521 EXTENT_DEFRAG, PAGE_UNLOCK |
Josef Bacikc2790a22013-07-29 11:20:47 -04001522 PAGE_CLEAR_DIRTY |
1523 PAGE_SET_WRITEBACK |
1524 PAGE_END_WRITEBACK);
Mark Fashehd8926bb2011-07-13 10:38:47 -07001525 return -ENOMEM;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001526 }
Li Zefan82d59022011-04-20 10:33:24 +08001527
Yan Zheng80ff3852008-10-30 14:20:02 -04001528 while (1) {
Nikolay Borisov3e024842019-08-21 10:42:03 +03001529 struct btrfs_key found_key;
1530 struct btrfs_file_extent_item *fi;
1531 struct extent_buffer *leaf;
1532 u64 extent_end;
1533 u64 extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001534 u64 num_bytes = 0;
1535 u64 disk_num_bytes;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001536 u64 ram_bytes;
1537 int extent_type;
Nikolay Borisov762bf092019-08-22 17:24:20 +03001538
1539 nocow = false;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001540
Liu Boe4c3b2d2017-01-30 12:25:28 -08001541 ret = btrfs_lookup_file_extent(NULL, root, path, ino,
Yan Zheng80ff3852008-10-30 14:20:02 -04001542 cur_offset, 0);
Josef Bacikd788a342013-10-25 16:55:08 -04001543 if (ret < 0)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001544 goto error;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001545
1546 /*
1547 * If there is no extent for our range when doing the initial
1548 * search, then go back to the previous slot as it will be the
1549 * one containing the search offset
1550 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001551 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1552 leaf = path->nodes[0];
1553 btrfs_item_key_to_cpu(leaf, &found_key,
1554 path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +08001555 if (found_key.objectid == ino &&
Yan Zheng80ff3852008-10-30 14:20:02 -04001556 found_key.type == BTRFS_EXTENT_DATA_KEY)
1557 path->slots[0]--;
1558 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001559 check_prev = false;
Yan Zheng80ff3852008-10-30 14:20:02 -04001560next_slot:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001561 /* Go to next leaf if we have exhausted the current one */
Yan Zheng80ff3852008-10-30 14:20:02 -04001562 leaf = path->nodes[0];
1563 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1564 ret = btrfs_next_leaf(root, path);
Liu Boe8916692018-01-25 11:02:50 -07001565 if (ret < 0) {
1566 if (cow_start != (u64)-1)
1567 cur_offset = cow_start;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001568 goto error;
Liu Boe8916692018-01-25 11:02:50 -07001569 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001570 if (ret > 0)
1571 break;
1572 leaf = path->nodes[0];
1573 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001574
Yan Zheng80ff3852008-10-30 14:20:02 -04001575 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001576
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001577 /* Didn't find anything for our INO */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001578 if (found_key.objectid > ino)
1579 break;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001580 /*
1581 * Keep searching until we find an EXTENT_ITEM or there are no
1582 * more extents for this inode
1583 */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001584 if (WARN_ON_ONCE(found_key.objectid < ino) ||
1585 found_key.type < BTRFS_EXTENT_DATA_KEY) {
1586 path->slots[0]++;
1587 goto next_slot;
1588 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001589
1590 /* Found key is not EXTENT_DATA_KEY or starts after req range */
Filipe Manana1d512cb2015-11-09 00:33:58 +00001591 if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001592 found_key.offset > end)
1593 break;
Chris Masonbe20aa92007-12-17 20:14:01 -05001594
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001595 /*
1596 * If the found extent starts after requested offset, then
1597 * adjust extent_end to be right before this extent begins
1598 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001599 if (found_key.offset > cur_offset) {
1600 extent_end = found_key.offset;
Chris Masone9061e22009-10-09 09:57:45 -04001601 extent_type = 0;
Yan Zheng80ff3852008-10-30 14:20:02 -04001602 goto out_check;
1603 }
Chris Masonc31f8832008-01-08 15:46:31 -05001604
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001605 /*
1606 * Found extent which begins before our range and potentially
1607 * intersect it
1608 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001609 fi = btrfs_item_ptr(leaf, path->slots[0],
1610 struct btrfs_file_extent_item);
1611 extent_type = btrfs_file_extent_type(leaf, fi);
Chris Masonbe20aa92007-12-17 20:14:01 -05001612
Josef Bacikcc95bef2013-04-04 14:31:27 -04001613 ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -04001614 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1615 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001616 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001617 extent_offset = btrfs_file_extent_offset(leaf, fi);
Yan Zheng80ff3852008-10-30 14:20:02 -04001618 extent_end = found_key.offset +
1619 btrfs_file_extent_num_bytes(leaf, fi);
Josef Bacikb4939682012-12-03 10:31:19 -05001620 disk_num_bytes =
1621 btrfs_file_extent_disk_num_bytes(leaf, fi);
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001622 /*
Filipe Mananade7999a2019-12-11 09:01:40 +00001623 * If the extent we got ends before our current offset,
1624 * skip to the next extent.
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001625 */
Filipe Mananade7999a2019-12-11 09:01:40 +00001626 if (extent_end <= cur_offset) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001627 path->slots[0]++;
1628 goto next_slot;
1629 }
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001630 /* Skip holes */
Yan Zheng17d217f2008-12-12 10:03:38 -05001631 if (disk_bytenr == 0)
1632 goto out_check;
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001633 /* Skip compressed/encrypted/encoded extents */
Yan Zheng80ff3852008-10-30 14:20:02 -04001634 if (btrfs_file_extent_compression(leaf, fi) ||
1635 btrfs_file_extent_encryption(leaf, fi) ||
1636 btrfs_file_extent_other_encoding(leaf, fi))
1637 goto out_check;
Ethan Lien78d42952018-05-17 14:58:29 +08001638 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001639 * If extent is created before the last volume's snapshot
1640 * this implies the extent is shared, hence we can't do
1641 * nocow. This is the same check as in
1642 * btrfs_cross_ref_exist but without calling
1643 * btrfs_search_slot.
Ethan Lien78d42952018-05-17 14:58:29 +08001644 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001645 if (!freespace_inode &&
Lu Fengqi27a7ff52018-11-29 17:31:32 +08001646 btrfs_file_extent_generation(leaf, fi) <=
Ethan Lien78d42952018-05-17 14:58:29 +08001647 btrfs_root_last_snapshot(&root->root_item))
1648 goto out_check;
Yan Zhengd899e052008-10-30 14:25:28 -04001649 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1650 goto out_check;
Filipe Mananac65ca982020-11-18 11:00:17 +00001651
1652 /*
1653 * The following checks can be expensive, as they need to
1654 * take other locks and do btree or rbtree searches, so
1655 * release the path to avoid blocking other tasks for too
1656 * long.
1657 */
1658 btrfs_release_path(path);
1659
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001660 /* If extent is RO, we must COW it */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04001661 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Yan Zheng80ff3852008-10-30 14:20:02 -04001662 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001663 ret = btrfs_cross_ref_exist(root, ino,
1664 found_key.offset -
Boris Burkova84d5d42020-08-18 11:00:05 -07001665 extent_offset, disk_bytenr, false);
Liu Bo58113752018-01-31 17:09:13 -07001666 if (ret) {
1667 /*
1668 * ret could be -EIO if the above fails to read
1669 * metadata.
1670 */
1671 if (ret < 0) {
1672 if (cow_start != (u64)-1)
1673 cur_offset = cow_start;
1674 goto error;
1675 }
1676
Nikolay Borisov3e024842019-08-21 10:42:03 +03001677 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001678 goto out_check;
Liu Bo58113752018-01-31 17:09:13 -07001679 }
Yan Zheng5d4f98a2009-06-10 10:45:14 -04001680 disk_bytenr += extent_offset;
Yan Zheng17d217f2008-12-12 10:03:38 -05001681 disk_bytenr += cur_offset - found_key.offset;
1682 num_bytes = min(end + 1, extent_end) - cur_offset;
1683 /*
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001684 * If there are pending snapshots for this root, we
1685 * fall into common COW way
Wang Shilonge9894fd2014-03-27 11:12:25 +08001686 */
Nikolay Borisov3e024842019-08-21 10:42:03 +03001687 if (!freespace_inode && atomic_read(&root->snapshot_force_cow))
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001688 goto out_check;
Wang Shilonge9894fd2014-03-27 11:12:25 +08001689 /*
Yan Zheng17d217f2008-12-12 10:03:38 -05001690 * force cow if csum exists in the range.
1691 * this ensure that csum for a given extent are
1692 * either valid or do not exist.
1693 */
Liu Bo58113752018-01-31 17:09:13 -07001694 ret = csum_exist_in_range(fs_info, disk_bytenr,
1695 num_bytes);
1696 if (ret) {
Liu Bo58113752018-01-31 17:09:13 -07001697 /*
1698 * ret could be -EIO if the above fails to read
1699 * metadata.
1700 */
1701 if (ret < 0) {
1702 if (cow_start != (u64)-1)
1703 cur_offset = cow_start;
1704 goto error;
1705 }
Nikolay Borisov3e024842019-08-21 10:42:03 +03001706 WARN_ON_ONCE(freespace_inode);
Yan Zheng17d217f2008-12-12 10:03:38 -05001707 goto out_check;
Robbie Ko91e1f562016-10-07 10:01:29 +08001708 }
Robbie Ko8ecebf4d2018-08-06 10:30:30 +08001709 if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
Filipe Mananaf78c4362016-05-09 13:15:41 +01001710 goto out_check;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001711 nocow = true;
Yan Zheng80ff3852008-10-30 14:20:02 -04001712 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001713 extent_end = found_key.offset + ram_bytes;
1714 extent_end = ALIGN(extent_end, fs_info->sectorsize);
Nikolay Borisov922f0512019-08-05 17:47:06 +03001715 /* Skip extents outside of our requested range */
1716 if (extent_end <= start) {
1717 path->slots[0]++;
1718 goto next_slot;
1719 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001720 } else {
Nikolay Borisove8e21002019-08-22 17:25:23 +03001721 /* If this triggers then we have a memory corruption */
Arnd Bergmann290342f2019-03-25 14:02:25 +01001722 BUG();
Yan Zheng80ff3852008-10-30 14:20:02 -04001723 }
1724out_check:
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001725 /*
1726 * If nocow is false then record the beginning of the range
1727 * that needs to be COWed
1728 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001729 if (!nocow) {
1730 if (cow_start == (u64)-1)
1731 cow_start = cur_offset;
1732 cur_offset = extent_end;
1733 if (cur_offset > end)
1734 break;
Filipe Mananac65ca982020-11-18 11:00:17 +00001735 if (!path->nodes[0])
1736 continue;
Yan Zheng80ff3852008-10-30 14:20:02 -04001737 path->slots[0]++;
1738 goto next_slot;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001739 }
Chris Masonbe20aa92007-12-17 20:14:01 -05001740
Nikolay Borisova6bd9cd2019-08-21 10:42:57 +03001741 /*
1742 * COW range from cow_start to found_key.offset - 1. As the key
1743 * will contain the beginning of the first extent that can be
1744 * NOCOW, following one which needs to be COW'ed
1745 */
Yan Zheng80ff3852008-10-30 14:20:02 -04001746 if (cow_start != (u64)-1) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001747 ret = fallback_to_cow(inode, locked_page,
Nikolay Borisov8ba96f32020-06-03 08:55:20 +03001748 cow_start, found_key.offset - 1,
Filipe Manana467dc472020-05-27 11:16:07 +01001749 page_started, nr_written);
Josef Bacik230ed392020-07-06 09:14:12 -04001750 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001751 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001752 cow_start = (u64)-1;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001753 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001754
Yan Zhengd899e052008-10-30 14:25:28 -04001755 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001756 u64 orig_start = found_key.offset - extent_offset;
Nikolay Borisov3e024842019-08-21 10:42:03 +03001757 struct extent_map *em;
Liu Bo6f9994d2017-01-31 07:50:22 -08001758
Nikolay Borisov968322c2020-06-03 08:55:21 +03001759 em = create_io_em(inode, cur_offset, num_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08001760 orig_start,
1761 disk_bytenr, /* block_start */
1762 num_bytes, /* block_len */
1763 disk_num_bytes, /* orig_block_len */
1764 ram_bytes, BTRFS_COMPRESS_NONE,
1765 BTRFS_ORDERED_PREALLOC);
1766 if (IS_ERR(em)) {
Liu Bo6f9994d2017-01-31 07:50:22 -08001767 ret = PTR_ERR(em);
1768 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001769 }
Liu Bo6f9994d2017-01-31 07:50:22 -08001770 free_extent_map(em);
Nikolay Borisov968322c2020-06-03 08:55:21 +03001771 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001772 disk_bytenr, num_bytes,
1773 num_bytes,
1774 BTRFS_ORDERED_PREALLOC);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001775 if (ret) {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001776 btrfs_drop_extent_cache(inode, cur_offset,
Nikolay Borisov762bf092019-08-22 17:24:20 +03001777 cur_offset + num_bytes - 1,
1778 0);
1779 goto error;
1780 }
Yan Zhengd899e052008-10-30 14:25:28 -04001781 } else {
Nikolay Borisov968322c2020-06-03 08:55:21 +03001782 ret = btrfs_add_ordered_extent(inode, cur_offset,
Nikolay Borisovbb55f622019-08-05 17:47:05 +03001783 disk_bytenr, num_bytes,
1784 num_bytes,
1785 BTRFS_ORDERED_NOCOW);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001786 if (ret)
1787 goto error;
Yan Zhengd899e052008-10-30 14:25:28 -04001788 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001789
Filipe Mananaf78c4362016-05-09 13:15:41 +01001790 if (nocow)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001791 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
Nikolay Borisov762bf092019-08-22 17:24:20 +03001792 nocow = false;
Chris Mason771ed682008-11-06 22:02:51 -05001793
Yan, Zhengefa56462010-05-16 10:49:59 -04001794 if (root->root_key.objectid ==
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001795 BTRFS_DATA_RELOC_TREE_OBJECTID)
1796 /*
1797 * Error handled later, as we must prevent
1798 * extent_clear_unlock_delalloc() in error handler
1799 * from freeing metadata of created ordered extent.
1800 */
Nikolay Borisov968322c2020-06-03 08:55:21 +03001801 ret = btrfs_reloc_clone_csums(inode, cur_offset,
Yan, Zhengefa56462010-05-16 10:49:59 -04001802 num_bytes);
Yan, Zhengefa56462010-05-16 10:49:59 -04001803
Nikolay Borisov968322c2020-06-03 08:55:21 +03001804 extent_clear_unlock_delalloc(inode, cur_offset,
Nikolay Borisov74e91942019-07-17 16:18:16 +03001805 cur_offset + num_bytes - 1,
Josef Bacikc2790a22013-07-29 11:20:47 -04001806 locked_page, EXTENT_LOCKED |
Wang Xiaoguang18513092016-07-25 15:51:40 +08001807 EXTENT_DELALLOC |
1808 EXTENT_CLEAR_DATA_RESV,
1809 PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1810
Yan Zheng80ff3852008-10-30 14:20:02 -04001811 cur_offset = extent_end;
Qu Wenruo4dbd80f2017-03-08 10:25:51 +08001812
1813 /*
1814 * btrfs_reloc_clone_csums() error, now we're OK to call error
1815 * handler, as metadata for created ordered extent will only
1816 * be freed by btrfs_finish_ordered_io().
1817 */
1818 if (ret)
1819 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001820 if (cur_offset > end)
1821 break;
Yan Zheng7ea394f2008-08-05 13:05:02 -04001822 }
David Sterbab3b4aa72011-04-21 01:20:15 +02001823 btrfs_release_path(path);
Yan Zheng80ff3852008-10-30 14:20:02 -04001824
Robbie Ko506481b2018-10-30 18:04:04 +08001825 if (cur_offset <= end && cow_start == (u64)-1)
Yan Zheng80ff3852008-10-30 14:20:02 -04001826 cow_start = cur_offset;
Josef Bacik17ca04a2012-05-31 15:58:55 -04001827
Yan Zheng80ff3852008-10-30 14:20:02 -04001828 if (cow_start != (u64)-1) {
Robbie Ko506481b2018-10-30 18:04:04 +08001829 cur_offset = end;
Nikolay Borisov968322c2020-06-03 08:55:21 +03001830 ret = fallback_to_cow(inode, locked_page, cow_start, end,
1831 page_started, nr_written);
Josef Bacikd788a342013-10-25 16:55:08 -04001832 if (ret)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001833 goto error;
Yan Zheng80ff3852008-10-30 14:20:02 -04001834 }
1835
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001836error:
Nikolay Borisov762bf092019-08-22 17:24:20 +03001837 if (nocow)
1838 btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1839
Josef Bacik17ca04a2012-05-31 15:58:55 -04001840 if (ret && cur_offset < end)
Nikolay Borisov968322c2020-06-03 08:55:21 +03001841 extent_clear_unlock_delalloc(inode, cur_offset, end,
Josef Bacikc2790a22013-07-29 11:20:47 -04001842 locked_page, EXTENT_LOCKED |
Josef Bacik151a41b2013-07-29 13:22:24 -04001843 EXTENT_DELALLOC | EXTENT_DEFRAG |
1844 EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1845 PAGE_CLEAR_DIRTY |
Josef Bacikc2790a22013-07-29 11:20:47 -04001846 PAGE_SET_WRITEBACK |
1847 PAGE_END_WRITEBACK);
Yan Zheng7ea394f2008-08-05 13:05:02 -04001848 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001849 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001850}
1851
Nikolay Borisov0c494222020-06-03 08:55:28 +03001852static inline int need_force_cow(struct btrfs_inode *inode, u64 start, u64 end)
Wang Shilong47059d92014-07-03 18:22:07 +08001853{
1854
Nikolay Borisov0c494222020-06-03 08:55:28 +03001855 if (!(inode->flags & BTRFS_INODE_NODATACOW) &&
1856 !(inode->flags & BTRFS_INODE_PREALLOC))
Wang Shilong47059d92014-07-03 18:22:07 +08001857 return 0;
1858
1859 /*
1860 * @defrag_bytes is a hint value, no spinlock held here,
1861 * if is not zero, it means the file is defragging.
1862 * Force cow if given extent needs to be defragged.
1863 */
Nikolay Borisov0c494222020-06-03 08:55:28 +03001864 if (inode->defrag_bytes &&
1865 test_range_bit(&inode->io_tree, start, end, EXTENT_DEFRAG, 0, NULL))
Wang Shilong47059d92014-07-03 18:22:07 +08001866 return 1;
1867
1868 return 0;
1869}
1870
Chris Masond352ac62008-09-29 15:18:18 -04001871/*
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001872 * Function to process delayed allocation (create CoW) for ranges which are
1873 * being touched for the first time.
Chris Masond352ac62008-09-29 15:18:18 -04001874 */
Nikolay Borisov98456b92020-06-03 08:55:29 +03001875int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct page *locked_page,
Nikolay Borisov5eaad972018-11-01 14:09:46 +02001876 u64 start, u64 end, int *page_started, unsigned long *nr_written,
1877 struct writeback_control *wbc)
Chris Masonbe20aa92007-12-17 20:14:01 -05001878{
Chris Masonbe20aa92007-12-17 20:14:01 -05001879 int ret;
Nikolay Borisov98456b92020-06-03 08:55:29 +03001880 int force_cow = need_force_cow(inode, start, end);
Chris Masona2135012008-06-25 16:01:30 -04001881
Nikolay Borisov98456b92020-06-03 08:55:29 +03001882 if (inode->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1883 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001884 page_started, 1, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001885 } else if (inode->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1886 ret = run_delalloc_nocow(inode, locked_page, start, end,
Chris Masond3977122009-01-05 21:25:51 -05001887 page_started, 0, nr_written);
Nikolay Borisov98456b92020-06-03 08:55:29 +03001888 } else if (!inode_can_compress(inode) ||
1889 !inode_need_compress(inode, start, end)) {
1890 ret = cow_file_range(inode, locked_page, start, end,
1891 page_started, nr_written, 1);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001892 } else {
Nikolay Borisov98456b92020-06-03 08:55:29 +03001893 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT, &inode->runtime_flags);
1894 ret = cow_file_range_async(inode, wbc, locked_page, start, end,
David Sterbafac07d22019-10-29 18:28:57 +01001895 page_started, nr_written);
Josef Bacik7ddf5a42012-06-08 15:26:47 -04001896 }
Qu Wenruo524272602017-03-08 10:25:52 +08001897 if (ret)
Nikolay Borisov98456b92020-06-03 08:55:29 +03001898 btrfs_cleanup_ordered_extents(inode, locked_page, start,
Nikolay Borisovd1051d62018-11-21 17:10:52 +02001899 end - start + 1);
Chris Masonb888db22007-08-27 16:49:44 -04001900 return ret;
1901}
1902
Nikolay Borisovabbb55f2018-11-01 14:09:53 +02001903void btrfs_split_delalloc_extent(struct inode *inode,
1904 struct extent_state *orig, u64 split)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001905{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001906 u64 size;
1907
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001908 /* not delalloc, ignore it */
Josef Bacik9ed74f22009-09-11 16:12:44 -04001909 if (!(orig->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001910 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001911
Josef Bacikdcab6a32015-02-11 15:08:59 -05001912 size = orig->end - orig->start + 1;
1913 if (size > BTRFS_MAX_EXTENT_SIZE) {
David Sterba823bb202017-01-04 11:09:51 +01001914 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001915 u64 new_size;
1916
1917 /*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001918 * See the explanation in btrfs_merge_delalloc_extent, the same
Josef Bacikba117212015-03-13 15:01:24 -04001919 * applies here, just in reverse.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001920 */
1921 new_size = orig->end - split + 1;
David Sterba823bb202017-01-04 11:09:51 +01001922 num_extents = count_max_extents(new_size);
Josef Bacikba117212015-03-13 15:01:24 -04001923 new_size = split - orig->start;
David Sterba823bb202017-01-04 11:09:51 +01001924 num_extents += count_max_extents(new_size);
1925 if (count_max_extents(size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001926 return;
1927 }
1928
Josef Bacik9e0baf62011-07-15 15:16:44 +00001929 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001930 btrfs_mod_outstanding_extents(BTRFS_I(inode), 1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001931 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001932}
1933
1934/*
Nikolay Borisov5c848192018-11-01 14:09:52 +02001935 * Handle merged delayed allocation extents so we can keep track of new extents
1936 * that are just merged onto old extents, such as when we are doing sequential
1937 * writes, so we can properly account for the metadata space we'll need.
Josef Bacik9ed74f22009-09-11 16:12:44 -04001938 */
Nikolay Borisov5c848192018-11-01 14:09:52 +02001939void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
1940 struct extent_state *other)
Josef Bacik9ed74f22009-09-11 16:12:44 -04001941{
Josef Bacikdcab6a32015-02-11 15:08:59 -05001942 u64 new_size, old_size;
David Sterba823bb202017-01-04 11:09:51 +01001943 u32 num_extents;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001944
Josef Bacik9ed74f22009-09-11 16:12:44 -04001945 /* not delalloc, ignore it */
1946 if (!(other->state & EXTENT_DELALLOC))
Jeff Mahoney1bf85042011-07-21 16:56:09 +00001947 return;
Josef Bacik9ed74f22009-09-11 16:12:44 -04001948
Josef Bacik8461a3d2015-03-13 15:12:08 -04001949 if (new->start > other->start)
1950 new_size = new->end - other->start + 1;
1951 else
1952 new_size = other->end - new->start + 1;
Josef Bacikdcab6a32015-02-11 15:08:59 -05001953
1954 /* we're not bigger than the max, unreserve the space and go */
1955 if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1956 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001957 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacikdcab6a32015-02-11 15:08:59 -05001958 spin_unlock(&BTRFS_I(inode)->lock);
1959 return;
1960 }
1961
1962 /*
Josef Bacikba117212015-03-13 15:01:24 -04001963 * We have to add up either side to figure out how many extents were
1964 * accounted for before we merged into one big extent. If the number of
1965 * extents we accounted for is <= the amount we need for the new range
1966 * then we can return, otherwise drop. Think of it like this
1967 *
1968 * [ 4k][MAX_SIZE]
1969 *
1970 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1971 * need 2 outstanding extents, on one side we have 1 and the other side
1972 * we have 1 so they are == and we can return. But in this case
1973 *
1974 * [MAX_SIZE+4k][MAX_SIZE+4k]
1975 *
1976 * Each range on their own accounts for 2 extents, but merged together
1977 * they are only 3 extents worth of accounting, so we need to drop in
1978 * this case.
Josef Bacikdcab6a32015-02-11 15:08:59 -05001979 */
Josef Bacikba117212015-03-13 15:01:24 -04001980 old_size = other->end - other->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001981 num_extents = count_max_extents(old_size);
Josef Bacikba117212015-03-13 15:01:24 -04001982 old_size = new->end - new->start + 1;
David Sterba823bb202017-01-04 11:09:51 +01001983 num_extents += count_max_extents(old_size);
1984 if (count_max_extents(new_size) >= num_extents)
Josef Bacikdcab6a32015-02-11 15:08:59 -05001985 return;
1986
Josef Bacik9e0baf62011-07-15 15:16:44 +00001987 spin_lock(&BTRFS_I(inode)->lock);
Josef Bacik8b62f872017-10-19 14:15:55 -04001988 btrfs_mod_outstanding_extents(BTRFS_I(inode), -1);
Josef Bacik9e0baf62011-07-15 15:16:44 +00001989 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik9ed74f22009-09-11 16:12:44 -04001990}
1991
Miao Xieeb73c1b2013-05-15 07:48:22 +00001992static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1993 struct inode *inode)
1994{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001995 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1996
Miao Xieeb73c1b2013-05-15 07:48:22 +00001997 spin_lock(&root->delalloc_lock);
1998 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1999 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
2000 &root->delalloc_inodes);
2001 set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
2002 &BTRFS_I(inode)->runtime_flags);
2003 root->nr_delalloc_inodes++;
2004 if (root->nr_delalloc_inodes == 1) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002005 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002006 BUG_ON(!list_empty(&root->delalloc_root));
2007 list_add_tail(&root->delalloc_root,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002008 &fs_info->delalloc_roots);
2009 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002010 }
2011 }
2012 spin_unlock(&root->delalloc_lock);
2013}
2014
Nikolay Borisov2b877332018-04-27 12:21:51 +03002015
2016void __btrfs_del_delalloc_inode(struct btrfs_root *root,
2017 struct btrfs_inode *inode)
Miao Xieeb73c1b2013-05-15 07:48:22 +00002018{
David Sterba3ffbd682018-06-29 10:56:42 +02002019 struct btrfs_fs_info *fs_info = root->fs_info;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002020
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002021 if (!list_empty(&inode->delalloc_inodes)) {
2022 list_del_init(&inode->delalloc_inodes);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002023 clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002024 &inode->runtime_flags);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002025 root->nr_delalloc_inodes--;
2026 if (!root->nr_delalloc_inodes) {
Nikolay Borisov7c8a0d32018-04-27 12:21:52 +03002027 ASSERT(list_empty(&root->delalloc_inodes));
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002028 spin_lock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002029 BUG_ON(list_empty(&root->delalloc_root));
2030 list_del_init(&root->delalloc_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002031 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002032 }
2033 }
Nikolay Borisov2b877332018-04-27 12:21:51 +03002034}
2035
2036static void btrfs_del_delalloc_inode(struct btrfs_root *root,
2037 struct btrfs_inode *inode)
2038{
2039 spin_lock(&root->delalloc_lock);
2040 __btrfs_del_delalloc_inode(root, inode);
Miao Xieeb73c1b2013-05-15 07:48:22 +00002041 spin_unlock(&root->delalloc_lock);
2042}
2043
Chris Masond352ac62008-09-29 15:18:18 -04002044/*
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002045 * Properly track delayed allocation bytes in the inode and to maintain the
2046 * list of inodes that have pending delalloc work to be done.
Chris Masond352ac62008-09-29 15:18:18 -04002047 */
Nikolay Borisove06a1fc2018-11-01 14:09:50 +02002048void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
2049 unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002050{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002051 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2052
Wang Shilong47059d92014-07-03 18:22:07 +08002053 if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
2054 WARN_ON(1);
Chris Mason75eff682008-12-15 15:54:40 -05002055 /*
2056 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002057 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002058 * bit, which is only set or cleared with irqs on
2059 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002060 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -05002061 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002062 u64 len = state->end + 1 - state->start;
Josef Bacik8b62f872017-10-19 14:15:55 -04002063 u32 num_extents = count_max_extents(len);
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002064 bool do_list = !btrfs_is_free_space_inode(BTRFS_I(inode));
Josef Bacik9ed74f22009-09-11 16:12:44 -04002065
Josef Bacik8b62f872017-10-19 14:15:55 -04002066 spin_lock(&BTRFS_I(inode)->lock);
2067 btrfs_mod_outstanding_extents(BTRFS_I(inode), num_extents);
2068 spin_unlock(&BTRFS_I(inode)->lock);
Josef Bacik287a0ab2010-03-19 18:07:23 +00002069
Josef Bacik6a3891c2015-03-16 17:38:52 -04002070 /* For sanity tests */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002071 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002072 return;
2073
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002074 percpu_counter_add_batch(&fs_info->delalloc_bytes, len,
2075 fs_info->delalloc_batch);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002076 spin_lock(&BTRFS_I(inode)->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002077 BTRFS_I(inode)->delalloc_bytes += len;
Wang Shilong47059d92014-07-03 18:22:07 +08002078 if (*bits & EXTENT_DEFRAG)
2079 BTRFS_I(inode)->defrag_bytes += len;
Miao Xiedf0af1a2013-01-29 10:11:59 +00002080 if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Miao Xieeb73c1b2013-05-15 07:48:22 +00002081 &BTRFS_I(inode)->runtime_flags))
2082 btrfs_add_delalloc_inodes(root, inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00002083 spin_unlock(&BTRFS_I(inode)->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002084 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002085
2086 if (!(state->state & EXTENT_DELALLOC_NEW) &&
2087 (*bits & EXTENT_DELALLOC_NEW)) {
2088 spin_lock(&BTRFS_I(inode)->lock);
2089 BTRFS_I(inode)->new_delalloc_bytes += state->end + 1 -
2090 state->start;
2091 spin_unlock(&BTRFS_I(inode)->lock);
2092 }
Chris Mason291d6732008-01-29 15:55:23 -05002093}
2094
Chris Masond352ac62008-09-29 15:18:18 -04002095/*
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002096 * Once a range is no longer delalloc this function ensures that proper
2097 * accounting happens.
Chris Masond352ac62008-09-29 15:18:18 -04002098 */
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002099void btrfs_clear_delalloc_extent(struct inode *vfs_inode,
2100 struct extent_state *state, unsigned *bits)
Chris Mason291d6732008-01-29 15:55:23 -05002101{
Nikolay Borisova36bb5f2018-11-01 14:09:51 +02002102 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
2103 struct btrfs_fs_info *fs_info = btrfs_sb(vfs_inode->i_sb);
Wang Shilong47059d92014-07-03 18:22:07 +08002104 u64 len = state->end + 1 - state->start;
David Sterba823bb202017-01-04 11:09:51 +01002105 u32 num_extents = count_max_extents(len);
Wang Shilong47059d92014-07-03 18:22:07 +08002106
Filipe Manana4a4b9642017-07-27 19:52:55 +01002107 if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG)) {
2108 spin_lock(&inode->lock);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002109 inode->defrag_bytes -= len;
Filipe Manana4a4b9642017-07-27 19:52:55 +01002110 spin_unlock(&inode->lock);
2111 }
Wang Shilong47059d92014-07-03 18:22:07 +08002112
Chris Mason75eff682008-12-15 15:54:40 -05002113 /*
2114 * set_bit and clear bit hooks normally require _irqsave/restore
Sergei Trofimovich27160b62011-05-20 20:20:32 +00002115 * but in this case, we are only testing for the DELALLOC
Chris Mason75eff682008-12-15 15:54:40 -05002116 * bit, which is only set or cleared with irqs on
2117 */
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002118 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002119 struct btrfs_root *root = inode->root;
Liu Bo83eea1f2012-07-10 05:28:39 -06002120 bool do_list = !btrfs_is_free_space_inode(inode);
Chris Masonbcbfce82008-04-22 13:26:47 -04002121
Josef Bacik8b62f872017-10-19 14:15:55 -04002122 spin_lock(&inode->lock);
2123 btrfs_mod_outstanding_extents(inode, -num_extents);
2124 spin_unlock(&inode->lock);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002125
Josef Bacikb6d08f02013-09-27 14:57:43 -04002126 /*
2127 * We don't reserve metadata space for space cache inodes so we
Andrea Gelmini52042d82018-11-28 12:05:13 +01002128 * don't need to call delalloc_release_metadata if there is an
Josef Bacikb6d08f02013-09-27 14:57:43 -04002129 * error.
2130 */
Filipe Mananaa315e682017-03-06 23:04:20 +00002131 if (*bits & EXTENT_CLEAR_META_RESV &&
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002132 root != fs_info->tree_root)
Qu Wenruo43b18592017-12-12 15:34:32 +08002133 btrfs_delalloc_release_metadata(inode, len, false);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002134
Josef Bacik6a3891c2015-03-16 17:38:52 -04002135 /* For sanity tests. */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002136 if (btrfs_is_testing(fs_info))
Josef Bacik6a3891c2015-03-16 17:38:52 -04002137 return;
2138
Filipe Mananaa315e682017-03-06 23:04:20 +00002139 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2140 do_list && !(state->state & EXTENT_NORESERVE) &&
2141 (*bits & EXTENT_CLEAR_DATA_RESV))
Nikolay Borisov9db5d512020-06-03 08:55:38 +03002142 btrfs_free_reserved_data_space_noquota(fs_info, len);
Josef Bacik9ed74f22009-09-11 16:12:44 -04002143
Nikolay Borisov104b4e52017-06-20 21:01:20 +03002144 percpu_counter_add_batch(&fs_info->delalloc_bytes, -len,
2145 fs_info->delalloc_batch);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002146 spin_lock(&inode->lock);
2147 inode->delalloc_bytes -= len;
2148 if (do_list && inode->delalloc_bytes == 0 &&
Miao Xiedf0af1a2013-01-29 10:11:59 +00002149 test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
Nikolay Borisov9e3e97f2017-02-20 13:51:07 +02002150 &inode->runtime_flags))
Miao Xieeb73c1b2013-05-15 07:48:22 +00002151 btrfs_del_delalloc_inode(root, inode);
Nikolay Borisov6fc0ef62017-02-20 13:51:03 +02002152 spin_unlock(&inode->lock);
Chris Mason291d6732008-01-29 15:55:23 -05002153 }
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002154
2155 if ((state->state & EXTENT_DELALLOC_NEW) &&
2156 (*bits & EXTENT_DELALLOC_NEW)) {
2157 spin_lock(&inode->lock);
2158 ASSERT(inode->new_delalloc_bytes >= len);
2159 inode->new_delalloc_bytes -= len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002160 if (*bits & EXTENT_ADD_INODE_BYTES)
2161 inode_add_bytes(&inode->vfs_inode, len);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002162 spin_unlock(&inode->lock);
2163 }
Chris Mason291d6732008-01-29 15:55:23 -05002164}
2165
Chris Masond352ac62008-09-29 15:18:18 -04002166/*
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002167 * btrfs_bio_fits_in_stripe - Checks whether the size of the given bio will fit
2168 * in a chunk's stripe. This function ensures that bios do not span a
2169 * stripe/chunk
Liu Bo6f034ec2016-06-22 18:31:49 -07002170 *
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002171 * @page - The page we are about to add to the bio
2172 * @size - size we want to add to the bio
2173 * @bio - bio we want to ensure is smaller than a stripe
2174 * @bio_flags - flags of the bio
2175 *
2176 * return 1 if page cannot be added to the bio
2177 * return 0 if page can be added to the bio
Liu Bo6f034ec2016-06-22 18:31:49 -07002178 * return error otherwise
Chris Masond352ac62008-09-29 15:18:18 -04002179 */
Nikolay Borisovda12fe52018-11-27 20:57:58 +02002180int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
2181 unsigned long bio_flags)
Chris Mason239b14b2008-03-24 15:02:07 -04002182{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002183 struct inode *inode = page->mapping->host;
2184 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba1201b582020-11-26 15:41:27 +01002185 u64 logical = bio->bi_iter.bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -04002186 u64 length = 0;
2187 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -04002188 int ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002189 struct btrfs_io_geometry geom;
Chris Mason239b14b2008-03-24 15:02:07 -04002190
Chris Mason771ed682008-11-06 22:02:51 -05002191 if (bio_flags & EXTENT_BIO_COMPRESSED)
2192 return 0;
2193
Kent Overstreet4f024f32013-10-11 15:44:27 -07002194 length = bio->bi_iter.bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -04002195 map_length = length;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002196 ret = btrfs_get_io_geometry(fs_info, btrfs_op(bio), logical, map_length,
2197 &geom);
Liu Bo6f034ec2016-06-22 18:31:49 -07002198 if (ret < 0)
2199 return ret;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03002200
2201 if (geom.len < length + size)
Chris Mason239b14b2008-03-24 15:02:07 -04002202 return 1;
Jeff Mahoney3444a972011-10-03 23:23:13 -04002203 return 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002204}
2205
Chris Masond352ac62008-09-29 15:18:18 -04002206/*
2207 * in order to insert checksums into the metadata in large chunks,
2208 * we wait until bio submission time. All the pages in the bio are
2209 * checksummed and sums are attached onto the ordered extent record.
2210 *
2211 * At IO completion time the cums attached on the ordered extent record
2212 * are inserted into the btree
2213 */
Qu Wenruo8896a082020-10-21 14:24:53 +08002214static blk_status_t btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
Qu Wenruo1941b642020-12-02 14:47:57 +08002215 u64 dio_file_offset)
Chris Mason065631f2008-02-20 12:07:25 -05002216{
Johannes Thumshirnc965d642020-07-28 20:25:41 +09002217 return btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Chris Mason4a69a412008-11-06 22:03:00 -05002218}
Chris Masone0156402008-04-16 11:15:20 -04002219
Chris Mason4a69a412008-11-06 22:03:00 -05002220/*
Chris Masoncad321a2008-12-17 14:51:42 -05002221 * extent_io.c submission hook. This does the right thing for csum calculation
Liu Bo4c274bc2017-11-01 17:19:27 -06002222 * on write, or reading the csums from the tree before a read.
2223 *
2224 * Rules about async/sync submit,
2225 * a) read: sync submit
2226 *
2227 * b) write without checksum: sync submit
2228 *
2229 * c) write with checksum:
2230 * c-1) if bio is issued by fsync: sync submit
2231 * (sync_writers != 0)
2232 *
2233 * c-2) if root is reloc root: sync submit
2234 * (only in case of buffered IO)
2235 *
2236 * c-3) otherwise: async submit
Chris Masond352ac62008-09-29 15:18:18 -04002237 */
Nikolay Borisov908930f2020-09-18 16:34:37 +03002238blk_status_t btrfs_submit_data_bio(struct inode *inode, struct bio *bio,
2239 int mirror_num, unsigned long bio_flags)
Nikolay Borisov50489a52019-04-10 19:46:04 +03002240
Chris Mason44b8bd72008-04-16 11:14:51 -04002241{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002242 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason44b8bd72008-04-16 11:14:51 -04002243 struct btrfs_root *root = BTRFS_I(inode)->root;
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302244 enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002245 blk_status_t ret = 0;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002246 int skip_sum;
Josef Bacikb812ce22012-11-16 13:56:32 -05002247 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
Chris Mason44b8bd72008-04-16 11:14:51 -04002248
Josef Bacik42437a62020-10-16 11:29:18 -04002249 skip_sum = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) ||
2250 !fs_info->csum_root;
Chris Masoncad321a2008-12-17 14:51:42 -05002251
Nikolay Borisov70ddc552017-02-20 13:50:35 +02002252 if (btrfs_is_free_space_inode(BTRFS_I(inode)))
Chandan Rajendra0d51e282015-07-27 15:26:43 +05302253 metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
Jeff Mahoney04173412011-10-03 23:23:12 -04002254
Mike Christie37226b22016-06-05 14:31:52 -05002255 if (bio_op(bio) != REQ_OP_WRITE) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002256 ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
Josef Bacik5fd02042012-05-02 14:00:54 -04002257 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002258 goto out;
Josef Bacik5fd02042012-05-02 14:00:54 -04002259
Chris Masond20f7042008-12-08 16:58:54 -05002260 if (bio_flags & EXTENT_BIO_COMPRESSED) {
Stefan Behrens61891922012-11-05 18:51:52 +01002261 ret = btrfs_submit_compressed_read(inode, bio,
2262 mirror_num,
2263 bio_flags);
2264 goto out;
Josef Bacik334c16d2020-10-16 11:29:14 -04002265 } else {
2266 /*
2267 * Lookup bio sums does extra checks around whether we
2268 * need to csum or not, which is why we ignore skip_sum
2269 * here.
2270 */
Qu Wenruo62751932020-12-02 14:48:06 +08002271 ret = btrfs_lookup_bio_sums(inode, bio, NULL);
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002272 if (ret)
Stefan Behrens61891922012-11-05 18:51:52 +01002273 goto out;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00002274 }
Chris Mason4d1b5fb2008-08-20 09:44:52 -04002275 goto mapit;
Josef Bacikb812ce22012-11-16 13:56:32 -05002276 } else if (async && !skip_sum) {
Yan Zheng17d217f2008-12-12 10:03:38 -05002277 /* csum items have already been cloned */
2278 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2279 goto mapit;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002280 /* we're doing a write, do the async checksumming */
Qu Wenruo8896a082020-10-21 14:24:53 +08002281 ret = btrfs_wq_submit_bio(inode, bio, mirror_num, bio_flags,
2282 0, btrfs_submit_bio_start);
Stefan Behrens61891922012-11-05 18:51:52 +01002283 goto out;
Josef Bacikb812ce22012-11-16 13:56:32 -05002284 } else if (!skip_sum) {
Nikolay Borisovbd242a02020-06-03 08:55:07 +03002285 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, 0, 0);
Josef Bacikb812ce22012-11-16 13:56:32 -05002286 if (ret)
2287 goto out;
Chris Mason19b9bdb2008-10-30 14:23:13 -04002288 }
2289
Chris Mason0b86a832008-03-24 15:01:56 -04002290mapit:
Chris Mason08635ba2019-07-10 12:28:14 -07002291 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Stefan Behrens61891922012-11-05 18:51:52 +01002292
2293out:
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002294 if (ret) {
2295 bio->bi_status = ret;
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002296 bio_endio(bio);
2297 }
Stefan Behrens61891922012-11-05 18:51:52 +01002298 return ret;
Chris Mason065631f2008-02-20 12:07:25 -05002299}
Chris Mason6885f302008-02-20 16:11:05 -05002300
Chris Masond352ac62008-09-29 15:18:18 -04002301/*
2302 * given a list of ordered sums record them in the inode. This happens
2303 * at IO completion time based on sums calculated at bio submission time.
2304 */
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002305static int add_pending_csums(struct btrfs_trans_handle *trans,
2306 struct list_head *list)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002307{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002308 struct btrfs_ordered_sum *sum;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002309 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002310
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002311 list_for_each_entry(sum, list, list) {
David Sterba7c2871a2017-11-08 01:07:43 +01002312 trans->adding_csums = true;
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002313 ret = btrfs_csum_file_blocks(trans, trans->fs_info->csum_root, sum);
David Sterba7c2871a2017-11-08 01:07:43 +01002314 trans->adding_csums = false;
Nikolay Borisovac01f262018-01-08 10:59:43 +02002315 if (ret)
2316 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002317 }
2318 return 0;
2319}
2320
Filipe Mananac3347302020-11-04 11:07:31 +00002321static int btrfs_find_new_delalloc_bytes(struct btrfs_inode *inode,
2322 const u64 start,
2323 const u64 len,
2324 struct extent_state **cached_state)
2325{
2326 u64 search_start = start;
2327 const u64 end = start + len - 1;
2328
2329 while (search_start < end) {
2330 const u64 search_len = end - search_start + 1;
2331 struct extent_map *em;
2332 u64 em_len;
2333 int ret = 0;
2334
2335 em = btrfs_get_extent(inode, NULL, 0, search_start, search_len);
2336 if (IS_ERR(em))
2337 return PTR_ERR(em);
2338
2339 if (em->block_start != EXTENT_MAP_HOLE)
2340 goto next;
2341
2342 em_len = em->len;
2343 if (em->start < search_start)
2344 em_len -= search_start - em->start;
2345 if (em_len > search_len)
2346 em_len = search_len;
2347
2348 ret = set_extent_bit(&inode->io_tree, search_start,
2349 search_start + em_len - 1,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02002350 EXTENT_DELALLOC_NEW, 0, NULL, cached_state,
2351 GFP_NOFS, NULL);
Filipe Mananac3347302020-11-04 11:07:31 +00002352next:
2353 search_start = extent_map_end(em);
2354 free_extent_map(em);
2355 if (ret)
2356 return ret;
2357 }
2358 return 0;
2359}
2360
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002361int btrfs_set_extent_delalloc(struct btrfs_inode *inode, u64 start, u64 end,
Filipe Mananae3b8a482017-11-04 00:16:59 +00002362 unsigned int extra_bits,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002363 struct extent_state **cached_state)
Chris Masonea8c2812008-08-04 23:17:27 -04002364{
Johannes Thumshirnfdb1e122018-12-05 15:23:04 +01002365 WARN_ON(PAGE_ALIGNED(end));
Filipe Mananac3347302020-11-04 11:07:31 +00002366
2367 if (start >= i_size_read(&inode->vfs_inode) &&
2368 !(inode->flags & BTRFS_INODE_PREALLOC)) {
2369 /*
2370 * There can't be any extents following eof in this case so just
2371 * set the delalloc new bit for the range directly.
2372 */
2373 extra_bits |= EXTENT_DELALLOC_NEW;
2374 } else {
2375 int ret;
2376
2377 ret = btrfs_find_new_delalloc_bytes(inode, start,
2378 end + 1 - start,
2379 cached_state);
2380 if (ret)
2381 return ret;
2382 }
2383
Nikolay Borisovc2566f22020-06-03 08:55:35 +03002384 return set_extent_delalloc(&inode->io_tree, start, end, extra_bits,
2385 cached_state);
Chris Masonea8c2812008-08-04 23:17:27 -04002386}
2387
Chris Masond352ac62008-09-29 15:18:18 -04002388/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -04002389struct btrfs_writepage_fixup {
2390 struct page *page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002391 struct inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002392 struct btrfs_work work;
2393};
2394
Christoph Hellwigb2950862008-12-02 09:54:17 -05002395static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
Chris Mason247e7432008-07-17 12:53:51 -04002396{
2397 struct btrfs_writepage_fixup *fixup;
2398 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002399 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08002400 struct extent_changeset *data_reserved = NULL;
Chris Mason247e7432008-07-17 12:53:51 -04002401 struct page *page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002402 struct btrfs_inode *inode;
Chris Mason247e7432008-07-17 12:53:51 -04002403 u64 page_start;
2404 u64 page_end;
Chris Mason25f3c502020-01-21 11:51:42 -05002405 int ret = 0;
Josef Bacikf4b13632020-01-21 14:34:52 -05002406 bool free_delalloc_space = true;
Chris Mason247e7432008-07-17 12:53:51 -04002407
2408 fixup = container_of(work, struct btrfs_writepage_fixup, work);
2409 page = fixup->page;
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002410 inode = BTRFS_I(fixup->inode);
Josef Bacikf4b13632020-01-21 14:34:52 -05002411 page_start = page_offset(page);
2412 page_end = page_offset(page) + PAGE_SIZE - 1;
2413
2414 /*
2415 * This is similar to page_mkwrite, we need to reserve the space before
2416 * we take the page lock.
2417 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002418 ret = btrfs_delalloc_reserve_space(inode, &data_reserved, page_start,
2419 PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002420again:
Chris Mason247e7432008-07-17 12:53:51 -04002421 lock_page(page);
Chris Mason247e7432008-07-17 12:53:51 -04002422
Chris Mason25f3c502020-01-21 11:51:42 -05002423 /*
2424 * Before we queued this fixup, we took a reference on the page.
2425 * page->mapping may go NULL, but it shouldn't be moved to a different
2426 * address space.
2427 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002428 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
2429 /*
2430 * Unfortunately this is a little tricky, either
2431 *
2432 * 1) We got here and our page had already been dealt with and
2433 * we reserved our space, thus ret == 0, so we need to just
2434 * drop our space reservation and bail. This can happen the
2435 * first time we come into the fixup worker, or could happen
2436 * while waiting for the ordered extent.
2437 * 2) Our page was already dealt with, but we happened to get an
2438 * ENOSPC above from the btrfs_delalloc_reserve_space. In
2439 * this case we obviously don't have anything to release, but
2440 * because the page was already dealt with we don't want to
2441 * mark the page with an error, so make sure we're resetting
2442 * ret to 0. This is why we have this check _before_ the ret
2443 * check, because we do not want to have a surprise ENOSPC
2444 * when the page was already properly dealt with.
2445 */
2446 if (!ret) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002447 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
2448 btrfs_delalloc_release_space(inode, data_reserved,
Josef Bacikf4b13632020-01-21 14:34:52 -05002449 page_start, PAGE_SIZE,
2450 true);
2451 }
2452 ret = 0;
Chris Mason25f3c502020-01-21 11:51:42 -05002453 goto out_page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002454 }
Chris Mason25f3c502020-01-21 11:51:42 -05002455
2456 /*
Josef Bacikf4b13632020-01-21 14:34:52 -05002457 * We can't mess with the page state unless it is locked, so now that
2458 * it is locked bail if we failed to make our space reservation.
Chris Mason25f3c502020-01-21 11:51:42 -05002459 */
Josef Bacikf4b13632020-01-21 14:34:52 -05002460 if (ret)
2461 goto out_page;
Chris Mason247e7432008-07-17 12:53:51 -04002462
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002463 lock_extent_bits(&inode->io_tree, page_start, page_end, &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002464
2465 /* already ordered? We're done */
Chris Mason8b62b722009-09-02 16:53:46 -04002466 if (PagePrivate2(page))
Josef Bacikf4b13632020-01-21 14:34:52 -05002467 goto out_reserved;
Chris Mason4a096752008-07-21 10:29:44 -04002468
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002469 ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
Chris Mason4a096752008-07-21 10:29:44 -04002470 if (ordered) {
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002471 unlock_extent_cached(&inode->io_tree, page_start, page_end,
2472 &cached_state);
Chris Mason4a096752008-07-21 10:29:44 -04002473 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03002474 btrfs_start_ordered_extent(ordered, 1);
Jeff Mahoney87826df2012-02-15 16:23:57 +01002475 btrfs_put_ordered_extent(ordered);
Chris Mason4a096752008-07-21 10:29:44 -04002476 goto again;
2477 }
Chris Mason247e7432008-07-17 12:53:51 -04002478
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002479 ret = btrfs_set_extent_delalloc(inode, page_start, page_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03002480 &cached_state);
Chris Mason25f3c502020-01-21 11:51:42 -05002481 if (ret)
Filipe Manana53687002019-10-09 17:43:59 +01002482 goto out_reserved;
Nikolay Borisovf3038ee2017-12-05 09:29:19 +02002483
Chris Mason25f3c502020-01-21 11:51:42 -05002484 /*
2485 * Everything went as planned, we're now the owner of a dirty page with
2486 * delayed allocation bits set and space reserved for our COW
2487 * destination.
2488 *
2489 * The page was dirty when we started, nothing should have cleaned it.
2490 */
2491 BUG_ON(!PageDirty(page));
Josef Bacikf4b13632020-01-21 14:34:52 -05002492 free_delalloc_space = false;
Filipe Manana53687002019-10-09 17:43:59 +01002493out_reserved:
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002494 btrfs_delalloc_release_extents(inode, PAGE_SIZE);
Josef Bacikf4b13632020-01-21 14:34:52 -05002495 if (free_delalloc_space)
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002496 btrfs_delalloc_release_space(inode, data_reserved, page_start,
2497 PAGE_SIZE, true);
2498 unlock_extent_cached(&inode->io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01002499 &cached_state);
Chris Mason247e7432008-07-17 12:53:51 -04002500out_page:
Chris Mason25f3c502020-01-21 11:51:42 -05002501 if (ret) {
2502 /*
2503 * We hit ENOSPC or other errors. Update the mapping and page
2504 * to reflect the errors and clean the page.
2505 */
2506 mapping_set_error(page->mapping, ret);
2507 end_extent_writepage(page, ret, page_start, page_end);
2508 clear_page_dirty_for_io(page);
2509 SetPageError(page);
2510 }
2511 ClearPageChecked(page);
Chris Mason247e7432008-07-17 12:53:51 -04002512 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002513 put_page(page);
Miao Xieb897abe2011-01-26 16:19:22 +08002514 kfree(fixup);
Qu Wenruo364ecf32017-02-27 15:10:38 +08002515 extent_changeset_free(data_reserved);
Josef Bacikf4b13632020-01-21 14:34:52 -05002516 /*
2517 * As a precaution, do a delayed iput in case it would be the last iput
2518 * that could need flushing space. Recursing back to fixup worker would
2519 * deadlock.
2520 */
Nikolay Borisov65d87f72020-06-05 10:51:51 +03002521 btrfs_add_delayed_iput(&inode->vfs_inode);
Chris Mason247e7432008-07-17 12:53:51 -04002522}
2523
2524/*
2525 * There are a few paths in the higher layers of the kernel that directly
2526 * set the page dirty bit without asking the filesystem if it is a
2527 * good idea. This causes problems because we want to make sure COW
2528 * properly happens and the data=ordered rules are followed.
2529 *
Chris Masonc8b97812008-10-29 14:49:59 -04002530 * In our case any range that doesn't have the ORDERED bit set
Chris Mason247e7432008-07-17 12:53:51 -04002531 * hasn't been properly setup for IO. We kick off an async process
2532 * to fix it up. The async helper will wait for ordered extents, set
2533 * the delalloc bit and make it safe to write the page.
2534 */
Nikolay Borisovd75855b2018-11-01 14:09:47 +02002535int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end)
Chris Mason247e7432008-07-17 12:53:51 -04002536{
2537 struct inode *inode = page->mapping->host;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002538 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason247e7432008-07-17 12:53:51 -04002539 struct btrfs_writepage_fixup *fixup;
Chris Mason247e7432008-07-17 12:53:51 -04002540
Chris Mason8b62b722009-09-02 16:53:46 -04002541 /* this page is properly in the ordered list */
2542 if (TestClearPagePrivate2(page))
Chris Mason247e7432008-07-17 12:53:51 -04002543 return 0;
2544
Chris Mason25f3c502020-01-21 11:51:42 -05002545 /*
2546 * PageChecked is set below when we create a fixup worker for this page,
2547 * don't try to create another one if we're already PageChecked()
2548 *
2549 * The extent_io writepage code will redirty the page if we send back
2550 * EAGAIN.
2551 */
Chris Mason247e7432008-07-17 12:53:51 -04002552 if (PageChecked(page))
2553 return -EAGAIN;
2554
2555 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2556 if (!fixup)
2557 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -04002558
Josef Bacikf4b13632020-01-21 14:34:52 -05002559 /*
2560 * We are already holding a reference to this inode from
2561 * write_cache_pages. We need to hold it because the space reservation
2562 * takes place outside of the page lock, and we can't trust
2563 * page->mapping outside of the page lock.
2564 */
2565 ihold(inode);
Chris Mason247e7432008-07-17 12:53:51 -04002566 SetPageChecked(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03002567 get_page(page);
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002568 btrfs_init_work(&fixup->work, btrfs_writepage_fixup_worker, NULL, NULL);
Chris Mason247e7432008-07-17 12:53:51 -04002569 fixup->page = page;
Josef Bacikf4b13632020-01-21 14:34:52 -05002570 fixup->inode = inode;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002571 btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
Chris Mason25f3c502020-01-21 11:51:42 -05002572
2573 return -EAGAIN;
Chris Mason247e7432008-07-17 12:53:51 -04002574}
2575
Yan Zhengd899e052008-10-30 14:25:28 -04002576static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
Nikolay Borisovc553f942020-06-03 08:55:19 +03002577 struct btrfs_inode *inode, u64 file_pos,
Qu Wenruo9729f102020-06-10 09:04:41 +08002578 struct btrfs_file_extent_item *stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002579 const bool update_inode_bytes,
Qu Wenruo9729f102020-06-10 09:04:41 +08002580 u64 qgroup_reserved)
Yan Zhengd899e052008-10-30 14:25:28 -04002581{
Nikolay Borisovc553f942020-06-03 08:55:19 +03002582 struct btrfs_root *root = inode->root;
Filipe Manana2766ff62020-11-04 11:07:34 +00002583 const u64 sectorsize = root->fs_info->sectorsize;
Yan Zhengd899e052008-10-30 14:25:28 -04002584 struct btrfs_path *path;
2585 struct extent_buffer *leaf;
2586 struct btrfs_key ins;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002587 u64 disk_num_bytes = btrfs_stack_file_extent_disk_num_bytes(stack_fi);
2588 u64 disk_bytenr = btrfs_stack_file_extent_disk_bytenr(stack_fi);
2589 u64 num_bytes = btrfs_stack_file_extent_num_bytes(stack_fi);
2590 u64 ram_bytes = btrfs_stack_file_extent_ram_bytes(stack_fi);
Filipe Manana5893dfb2020-11-04 11:07:32 +00002591 struct btrfs_drop_extents_args drop_args = { 0 };
Yan Zhengd899e052008-10-30 14:25:28 -04002592 int ret;
2593
2594 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07002595 if (!path)
2596 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -04002597
Chris Masona1ed8352009-09-11 12:27:37 -04002598 /*
2599 * we may be replacing one extent in the tree with another.
2600 * The new extent is pinned in the extent map, and we don't want
2601 * to drop it from the cache until it is completely in the btree.
2602 *
2603 * So, tell btrfs_drop_extents to leave this extent in the cache.
2604 * the caller is expected to unpin it and allow it to be merged
2605 * with the others.
2606 */
Filipe Manana5893dfb2020-11-04 11:07:32 +00002607 drop_args.path = path;
2608 drop_args.start = file_pos;
2609 drop_args.end = file_pos + num_bytes;
2610 drop_args.replace_extent = true;
2611 drop_args.extent_item_size = sizeof(*stack_fi);
2612 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002613 if (ret)
2614 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -04002615
Filipe Manana5893dfb2020-11-04 11:07:32 +00002616 if (!drop_args.extent_inserted) {
Nikolay Borisovc553f942020-06-03 08:55:19 +03002617 ins.objectid = btrfs_ino(inode);
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002618 ins.offset = file_pos;
2619 ins.type = BTRFS_EXTENT_DATA_KEY;
2620
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002621 ret = btrfs_insert_empty_item(trans, root, path, &ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002622 sizeof(*stack_fi));
Filipe David Borba Manana1acae572014-01-07 11:42:27 +00002623 if (ret)
2624 goto out;
2625 }
Yan Zhengd899e052008-10-30 14:25:28 -04002626 leaf = path->nodes[0];
Qu Wenruo203f44c52020-06-10 09:04:40 +08002627 btrfs_set_stack_file_extent_generation(stack_fi, trans->transid);
2628 write_extent_buffer(leaf, stack_fi,
2629 btrfs_item_ptr_offset(leaf, path->slots[0]),
2630 sizeof(struct btrfs_file_extent_item));
Chris Masonb9473432009-03-13 11:00:37 -04002631
Yan Zhengd899e052008-10-30 14:25:28 -04002632 btrfs_mark_buffer_dirty(leaf);
Josef Bacikce195332012-09-25 15:26:16 -04002633 btrfs_release_path(path);
Yan Zhengd899e052008-10-30 14:25:28 -04002634
Filipe Manana2766ff62020-11-04 11:07:34 +00002635 /*
2636 * If we dropped an inline extent here, we know the range where it is
2637 * was not marked with the EXTENT_DELALLOC_NEW bit, so we update the
2638 * number of bytes only for that range contaning the inline extent.
2639 * The remaining of the range will be processed when clearning the
2640 * EXTENT_DELALLOC_BIT bit through the ordered extent completion.
2641 */
2642 if (file_pos == 0 && !IS_ALIGNED(drop_args.bytes_found, sectorsize)) {
2643 u64 inline_size = round_down(drop_args.bytes_found, sectorsize);
2644
2645 inline_size = drop_args.bytes_found - inline_size;
2646 btrfs_update_inode_bytes(inode, sectorsize, inline_size);
2647 drop_args.bytes_found -= inline_size;
2648 num_bytes -= sectorsize;
2649 }
2650
2651 if (update_inode_bytes)
2652 btrfs_update_inode_bytes(inode, num_bytes, drop_args.bytes_found);
Yan Zhengd899e052008-10-30 14:25:28 -04002653
2654 ins.objectid = disk_bytenr;
2655 ins.offset = disk_num_bytes;
2656 ins.type = BTRFS_EXTENT_ITEM_KEY;
Qu Wenruoa12b8772017-02-27 15:10:37 +08002657
Nikolay Borisovc553f942020-06-03 08:55:19 +03002658 ret = btrfs_inode_set_file_extent_range(inode, file_pos, ram_bytes);
Josef Bacik9ddc9592020-01-17 09:02:22 -05002659 if (ret)
2660 goto out;
2661
Nikolay Borisovc553f942020-06-03 08:55:19 +03002662 ret = btrfs_alloc_reserved_file_extent(trans, root, btrfs_ino(inode),
Qu Wenruo9729f102020-06-10 09:04:41 +08002663 file_pos, qgroup_reserved, &ins);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002664out:
Yan Zhengd899e052008-10-30 14:25:28 -04002665 btrfs_free_path(path);
Chris Masonb9473432009-03-13 11:00:37 -04002666
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002667 return ret;
Yan Zhengd899e052008-10-30 14:25:28 -04002668}
2669
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002670static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
Miao Xiee570fd22014-06-19 10:42:50 +08002671 u64 start, u64 len)
2672{
David Sterba32da53862019-10-29 19:20:18 +01002673 struct btrfs_block_group *cache;
Miao Xiee570fd22014-06-19 10:42:50 +08002674
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002675 cache = btrfs_lookup_block_group(fs_info, start);
Miao Xiee570fd22014-06-19 10:42:50 +08002676 ASSERT(cache);
2677
2678 spin_lock(&cache->lock);
2679 cache->delalloc_bytes -= len;
2680 spin_unlock(&cache->lock);
2681
2682 btrfs_put_block_group(cache);
2683}
2684
Qu Wenruo203f44c52020-06-10 09:04:40 +08002685static int insert_ordered_extent_file_extent(struct btrfs_trans_handle *trans,
Qu Wenruo203f44c52020-06-10 09:04:40 +08002686 struct btrfs_ordered_extent *oe)
2687{
2688 struct btrfs_file_extent_item stack_fi;
2689 u64 logical_len;
Filipe Manana2766ff62020-11-04 11:07:34 +00002690 bool update_inode_bytes;
Qu Wenruo203f44c52020-06-10 09:04:40 +08002691
2692 memset(&stack_fi, 0, sizeof(stack_fi));
2693 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_REG);
2694 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, oe->disk_bytenr);
2695 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi,
2696 oe->disk_num_bytes);
2697 if (test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags))
2698 logical_len = oe->truncated_len;
2699 else
2700 logical_len = oe->num_bytes;
2701 btrfs_set_stack_file_extent_num_bytes(&stack_fi, logical_len);
2702 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, logical_len);
2703 btrfs_set_stack_file_extent_compression(&stack_fi, oe->compress_type);
2704 /* Encryption and other encoding is reserved and all 0 */
2705
Filipe Manana2766ff62020-11-04 11:07:34 +00002706 /*
2707 * For delalloc, when completing an ordered extent we update the inode's
2708 * bytes when clearing the range in the inode's io tree, so pass false
2709 * as the argument 'update_inode_bytes' to insert_reserved_file_extent(),
2710 * except if the ordered extent was truncated.
2711 */
2712 update_inode_bytes = test_bit(BTRFS_ORDERED_DIRECT, &oe->flags) ||
2713 test_bit(BTRFS_ORDERED_TRUNCATED, &oe->flags);
2714
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002715 return insert_reserved_file_extent(trans, BTRFS_I(oe->inode),
2716 oe->file_offset, &stack_fi,
Filipe Manana2766ff62020-11-04 11:07:34 +00002717 update_inode_bytes, oe->qgroup_rsv);
Qu Wenruo203f44c52020-06-10 09:04:40 +08002718}
2719
2720/*
2721 * As ordered data IO finishes, this gets called so we can finish
Chris Masond352ac62008-09-29 15:18:18 -04002722 * an ordered extent if the range of bytes in the file it covers are
2723 * fully written.
2724 */
Josef Bacik5fd02042012-05-02 14:00:54 -04002725static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002726{
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002727 struct btrfs_inode *inode = BTRFS_I(ordered_extent->inode);
2728 struct btrfs_root *root = inode->root;
2729 struct btrfs_fs_info *fs_info = root->fs_info;
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04002730 struct btrfs_trans_handle *trans = NULL;
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002731 struct extent_io_tree *io_tree = &inode->io_tree;
Josef Bacik2ac55d42010-02-03 19:33:23 +00002732 struct extent_state *cached_state = NULL;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002733 u64 start, end;
Li Zefan261507a02010-12-17 14:21:50 +08002734 int compress_type = 0;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002735 int ret = 0;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002736 u64 logical_len = ordered_extent->num_bytes;
Nikolay Borisov8d510122019-10-08 20:43:06 +03002737 bool freespace_inode;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002738 bool truncated = false;
Josef Bacik49940bd2018-10-11 15:54:21 -04002739 bool clear_reserved_extent = true;
Filipe Manana2766ff62020-11-04 11:07:34 +00002740 unsigned int clear_bits = EXTENT_DEFRAG;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002741
Omar Sandovalbffe6332019-12-02 17:34:19 -08002742 start = ordered_extent->file_offset;
2743 end = start + ordered_extent->num_bytes - 1;
2744
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002745 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2746 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) &&
2747 !test_bit(BTRFS_ORDERED_DIRECT, &ordered_extent->flags))
Filipe Manana2766ff62020-11-04 11:07:34 +00002748 clear_bits |= EXTENT_DELALLOC_NEW;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002749
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002750 freespace_inode = btrfs_is_free_space_inode(inode);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002751
Josef Bacik5fd02042012-05-02 14:00:54 -04002752 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2753 ret = -EIO;
2754 goto out;
2755 }
2756
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002757 btrfs_free_io_failure_record(inode, start, end);
Miao Xief6124962014-09-12 18:44:04 +08002758
Josef Bacik77cef2e2013-08-29 13:57:21 -04002759 if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2760 truncated = true;
2761 logical_len = ordered_extent->truncated_len;
2762 /* Truncated the entire extent, don't bother adding */
2763 if (!logical_len)
2764 goto out;
2765 }
2766
Yan, Zhengc2167752009-11-12 09:34:21 +00002767 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002768 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
Qu Wenruo94ed9382015-09-08 17:25:56 +08002769
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002770 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov8d510122019-10-08 20:43:06 +03002771 if (freespace_inode)
2772 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik6c760c02012-11-09 10:53:21 -05002773 else
2774 trans = btrfs_join_transaction(root);
2775 if (IS_ERR(trans)) {
2776 ret = PTR_ERR(trans);
2777 trans = NULL;
2778 goto out;
Yan, Zhengc2167752009-11-12 09:34:21 +00002779 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002780 trans->block_rsv = &inode->block_rsv;
Nikolay Borisov729f7962020-11-02 16:49:06 +02002781 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002782 if (ret) /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002783 btrfs_abort_transaction(trans, ret);
Yan, Zhengc2167752009-11-12 09:34:21 +00002784 goto out;
2785 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002786
Filipe Manana2766ff62020-11-04 11:07:34 +00002787 clear_bits |= EXTENT_LOCKED;
Omar Sandovalbffe6332019-12-02 17:34:19 -08002788 lock_extent_bits(io_tree, start, end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002789
Nikolay Borisov8d510122019-10-08 20:43:06 +03002790 if (freespace_inode)
2791 trans = btrfs_join_transaction_spacecache(root);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002792 else
Josef Bacik7a7eaa42011-04-13 12:54:33 -04002793 trans = btrfs_join_transaction(root);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002794 if (IS_ERR(trans)) {
2795 ret = PTR_ERR(trans);
2796 trans = NULL;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002797 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002798 }
Chris Masona79b7d42014-05-22 16:18:52 -07002799
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002800 trans->block_rsv = &inode->block_rsv;
Yan, Zhengc2167752009-11-12 09:34:21 +00002801
Chris Masonc8b97812008-10-29 14:49:59 -04002802 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
Li Zefan261507a02010-12-17 14:21:50 +08002803 compress_type = ordered_extent->compress_type;
Yan Zhengd899e052008-10-30 14:25:28 -04002804 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
Li Zefan261507a02010-12-17 14:21:50 +08002805 BUG_ON(compress_type);
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002806 ret = btrfs_mark_extent_written(trans, inode,
Yan Zhengd899e052008-10-30 14:25:28 -04002807 ordered_extent->file_offset,
2808 ordered_extent->file_offset +
Josef Bacik77cef2e2013-08-29 13:57:21 -04002809 logical_len);
Yan Zhengd899e052008-10-30 14:25:28 -04002810 } else {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002811 BUG_ON(root == fs_info->tree_root);
Nikolay Borisov3c38c872020-09-18 12:15:51 +03002812 ret = insert_ordered_extent_file_extent(trans, ordered_extent);
Josef Bacik49940bd2018-10-11 15:54:21 -04002813 if (!ret) {
2814 clear_reserved_extent = false;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002815 btrfs_release_delalloc_bytes(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002816 ordered_extent->disk_bytenr,
2817 ordered_extent->disk_num_bytes);
Josef Bacik49940bd2018-10-11 15:54:21 -04002818 }
Yan Zhengd899e052008-10-30 14:25:28 -04002819 }
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002820 unpin_extent_cache(&inode->extent_tree, ordered_extent->file_offset,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002821 ordered_extent->num_bytes, trans->transid);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002822 if (ret < 0) {
Jeff Mahoney66642832016-06-10 18:19:25 -04002823 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002824 goto out;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002825 }
Josef Bacik2ac55d42010-02-03 19:33:23 +00002826
Nikolay Borisov510f85e2020-09-18 12:15:52 +03002827 ret = add_pending_csums(trans, &ordered_extent->list);
Nikolay Borisovac01f262018-01-08 10:59:43 +02002828 if (ret) {
2829 btrfs_abort_transaction(trans, ret);
2830 goto out;
2831 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002832
Filipe Manana2766ff62020-11-04 11:07:34 +00002833 /*
2834 * If this is a new delalloc range, clear its new delalloc flag to
2835 * update the inode's number of bytes. This needs to be done first
2836 * before updating the inode item.
2837 */
2838 if ((clear_bits & EXTENT_DELALLOC_NEW) &&
2839 !test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags))
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002840 clear_extent_bit(&inode->io_tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00002841 EXTENT_DELALLOC_NEW | EXTENT_ADD_INODE_BYTES,
2842 0, 0, &cached_state);
2843
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002844 btrfs_inode_safe_disk_i_size_write(inode, 0);
Nikolay Borisov729f7962020-11-02 16:49:06 +02002845 ret = btrfs_update_inode_fallback(trans, root, inode);
Josef Bacik6c760c02012-11-09 10:53:21 -05002846 if (ret) { /* -ENOMEM or corruption */
Jeff Mahoney66642832016-06-10 18:19:25 -04002847 btrfs_abort_transaction(trans, ret);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002848 goto out;
Josef Bacik1ef30be2011-04-05 19:25:36 -04002849 }
2850 ret = 0;
Yan, Zhengc2167752009-11-12 09:34:21 +00002851out:
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002852 clear_extent_bit(&inode->io_tree, start, end, clear_bits,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002853 (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0,
Omar Sandoval313facc2019-12-02 17:34:18 -08002854 &cached_state);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01002855
Miao Xiea698d0752012-09-20 01:51:59 -06002856 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04002857 btrfs_end_transaction(trans);
Josef Bacik0cb59c92010-07-02 12:14:14 -04002858
Josef Bacik77cef2e2013-08-29 13:57:21 -04002859 if (ret || truncated) {
Omar Sandovalbffe6332019-12-02 17:34:19 -08002860 u64 unwritten_start = start;
Josef Bacik77cef2e2013-08-29 13:57:21 -04002861
2862 if (truncated)
Omar Sandovalbffe6332019-12-02 17:34:19 -08002863 unwritten_start += logical_len;
2864 clear_extent_uptodate(io_tree, unwritten_start, end, NULL);
Josef Bacik77cef2e2013-08-29 13:57:21 -04002865
2866 /* Drop the cache for the part of the extent we didn't write. */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002867 btrfs_drop_extent_cache(inode, unwritten_start, end, 0);
Josef Bacik5fd02042012-05-02 14:00:54 -04002868
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002869 /*
2870 * If the ordered extent had an IOERR or something else went
2871 * wrong we need to return the space for this ordered extent
Josef Bacik77cef2e2013-08-29 13:57:21 -04002872 * back to the allocator. We only free the extent in the
2873 * truncated case if we didn't write out the extent at all.
Josef Bacik49940bd2018-10-11 15:54:21 -04002874 *
2875 * If we made it past insert_reserved_file_extent before we
2876 * errored out then we don't need to do this as the accounting
2877 * has already been done.
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002878 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04002879 if ((ret || !logical_len) &&
Josef Bacik49940bd2018-10-11 15:54:21 -04002880 clear_reserved_extent &&
Josef Bacik77cef2e2013-08-29 13:57:21 -04002881 !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002882 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2883 /*
2884 * Discard the range before returning it back to the
2885 * free space pool
2886 */
Dennis Zhou46b27f52019-12-13 16:22:11 -08002887 if (ret && btrfs_test_opt(fs_info, DISCARD_SYNC))
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002888 btrfs_discard_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002889 ordered_extent->disk_bytenr,
2890 ordered_extent->disk_num_bytes,
2891 NULL);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04002892 btrfs_free_reserved_extent(fs_info,
Omar Sandovalbffe6332019-12-02 17:34:19 -08002893 ordered_extent->disk_bytenr,
2894 ordered_extent->disk_num_bytes, 1);
Nikolay Borisov4eaaec22019-11-21 14:03:29 +02002895 }
Josef Bacik0bec9ef2013-01-31 14:58:00 -05002896 }
2897
Josef Bacik5fd02042012-05-02 14:00:54 -04002898 /*
Liu Bo8bad3c02012-06-18 12:14:23 +08002899 * This needs to be done to make sure anybody waiting knows we are done
2900 * updating everything for this ordered extent.
Josef Bacik5fd02042012-05-02 14:00:54 -04002901 */
Nikolay Borisov72e7e6e2020-11-02 16:48:56 +02002902 btrfs_remove_ordered_extent(inode, ordered_extent);
Josef Bacik5fd02042012-05-02 14:00:54 -04002903
Chris Masone6dcd2d2008-07-17 12:53:50 -04002904 /* once for us */
2905 btrfs_put_ordered_extent(ordered_extent);
2906 /* once for the tree */
2907 btrfs_put_ordered_extent(ordered_extent);
2908
Josef Bacik5fd02042012-05-02 14:00:54 -04002909 return ret;
2910}
2911
2912static void finish_ordered_fn(struct btrfs_work *work)
2913{
2914 struct btrfs_ordered_extent *ordered_extent;
2915 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2916 btrfs_finish_ordered_io(ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002917}
2918
Nikolay Borisovc6297322018-11-08 10:18:08 +02002919void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
2920 u64 end, int uptodate)
Chris Mason211f90e2008-07-18 11:56:15 -04002921{
Nikolay Borisov3347c482020-08-31 14:42:44 +03002922 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
2923 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik5fd02042012-05-02 14:00:54 -04002924 struct btrfs_ordered_extent *ordered_extent = NULL;
Liu Bo9e0af232014-08-15 23:36:53 +08002925 struct btrfs_workqueue *wq;
Josef Bacik5fd02042012-05-02 14:00:54 -04002926
liubo1abe9b82011-03-24 11:18:59 +00002927 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2928
Chris Mason8b62b722009-09-02 16:53:46 -04002929 ClearPagePrivate2(page);
Nikolay Borisov3347c482020-08-31 14:42:44 +03002930 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2931 end - start + 1, uptodate))
David Sterbac3988d62017-02-17 15:18:32 +01002932 return;
Josef Bacik5fd02042012-05-02 14:00:54 -04002933
Nikolay Borisov3347c482020-08-31 14:42:44 +03002934 if (btrfs_is_free_space_inode(inode))
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002935 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002936 else
Jeff Mahoney0b246af2016-06-22 18:54:23 -04002937 wq = fs_info->endio_write_workers;
Josef Bacik5fd02042012-05-02 14:00:54 -04002938
Omar Sandovala0cac0e2019-09-16 11:30:57 -07002939 btrfs_init_work(&ordered_extent->work, finish_ordered_fn, NULL, NULL);
Liu Bo9e0af232014-08-15 23:36:53 +08002940 btrfs_queue_work(wq, &ordered_extent->work);
Chris Mason211f90e2008-07-18 11:56:15 -04002941}
2942
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002943/*
2944 * check_data_csum - verify checksum of one sector of uncompressed data
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002945 * @inode: inode
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002946 * @io_bio: btrfs_io_bio which contains the csum
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002947 * @bio_offset: offset to the beginning of the bio (in bytes)
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002948 * @page: page where is the data to be verified
2949 * @pgoff: offset inside the page
2950 *
2951 * The length of such check is always one sector size.
2952 */
Omar Sandoval47df7762020-04-16 14:46:17 -07002953static int check_data_csum(struct inode *inode, struct btrfs_io_bio *io_bio,
Qu Wenruof44cf412020-12-02 14:47:59 +08002954 u32 bio_offset, struct page *page, u32 pgoff)
Miao Xiedc380ae2014-09-12 18:43:55 +08002955{
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002956 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2957 SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
Miao Xiedc380ae2014-09-12 18:43:55 +08002958 char *kaddr;
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002959 u32 len = fs_info->sectorsize;
David Sterba223486c2020-07-02 11:27:30 +02002960 const u32 csum_size = fs_info->csum_size;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002961 unsigned int offset_sectors;
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002962 u8 *csum_expected;
2963 u8 csum[BTRFS_CSUM_SIZE];
Miao Xiedc380ae2014-09-12 18:43:55 +08002964
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002965 ASSERT(pgoff + len <= PAGE_SIZE);
2966
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002967 offset_sectors = bio_offset >> fs_info->sectorsize_bits;
2968 csum_expected = ((u8 *)io_bio->csum) + offset_sectors * csum_size;
Miao Xiedc380ae2014-09-12 18:43:55 +08002969
2970 kaddr = kmap_atomic(page);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002971 shash->tfm = fs_info->csum_shash;
2972
Eric Biggersfd080012020-04-30 23:51:59 -07002973 crypto_shash_digest(shash, kaddr + pgoff, len, csum);
Johannes Thumshirnd5178572019-06-03 16:58:57 +02002974
2975 if (memcmp(csum, csum_expected, csum_size))
Miao Xiedc380ae2014-09-12 18:43:55 +08002976 goto zeroit;
2977
2978 kunmap_atomic(kaddr);
2979 return 0;
2980zeroit:
Qu Wenruo265d4ac2020-10-21 14:24:54 +08002981 btrfs_print_data_csum_error(BTRFS_I(inode), page_offset(page) + pgoff,
2982 csum, csum_expected, io_bio->mirror_num);
Nikolay Borisov814723e2020-07-02 15:23:32 +03002983 if (io_bio->device)
2984 btrfs_dev_stat_inc_and_print(io_bio->device,
2985 BTRFS_DEV_STAT_CORRUPTION_ERRS);
Miao Xiedc380ae2014-09-12 18:43:55 +08002986 memset(kaddr + pgoff, 1, len);
2987 flush_dcache_page(page);
2988 kunmap_atomic(kaddr);
Miao Xiedc380ae2014-09-12 18:43:55 +08002989 return -EIO;
2990}
2991
Chris Masond352ac62008-09-29 15:18:18 -04002992/*
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002993 * When reads are done, we need to check csums to verify the data is correct.
Jan Schmidt4a54c8c2011-07-22 15:41:52 +02002994 * if there's a match, we allow the bio to finish. If not, the code in
2995 * extent_io.c will try to find good copies for us.
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08002996 *
2997 * @bio_offset: offset to the beginning of the bio (in bytes)
2998 * @start: file offset of the range start
2999 * @end: file offset of the range end (inclusive)
3000 * @mirror: mirror number
Chris Masond352ac62008-09-29 15:18:18 -04003001 */
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08003002int btrfs_verify_data_csum(struct btrfs_io_bio *io_bio, u32 bio_offset,
Nikolay Borisov9a446d62020-09-18 16:34:33 +03003003 struct page *page, u64 start, u64 end, int mirror)
Chris Mason07157aa2007-08-30 08:50:51 -04003004{
Chris Mason07157aa2007-08-30 08:50:51 -04003005 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -05003006 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masonff79f812007-10-15 16:22:25 -04003007 struct btrfs_root *root = BTRFS_I(inode)->root;
Qu Wenruof44cf412020-12-02 14:47:59 +08003008 const u32 sectorsize = root->fs_info->sectorsize;
3009 u32 pg_off;
Chris Masond1310b22008-01-24 16:13:08 -05003010
Chris Masond20f7042008-12-08 16:58:54 -05003011 if (PageChecked(page)) {
3012 ClearPageChecked(page);
Miao Xiedc380ae2014-09-12 18:43:55 +08003013 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003014 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003015
3016 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Miao Xiedc380ae2014-09-12 18:43:55 +08003017 return 0;
Chris Masond20f7042008-12-08 16:58:54 -05003018
Josef Bacik42437a62020-10-16 11:29:18 -04003019 if (!root->fs_info->csum_root)
3020 return 0;
3021
Yan Zheng17d217f2008-12-12 10:03:38 -05003022 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
Chris Mason9655d292009-09-02 15:22:30 -04003023 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
David Sterba91166212016-04-26 23:54:39 +02003024 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
Yan Zheng17d217f2008-12-12 10:03:38 -05003025 return 0;
3026 }
3027
Qu Wenruof44cf412020-12-02 14:47:59 +08003028 ASSERT(page_offset(page) <= start &&
3029 end <= page_offset(page) + PAGE_SIZE - 1);
3030 for (pg_off = offset_in_page(start);
3031 pg_off < offset_in_page(end);
3032 pg_off += sectorsize, bio_offset += sectorsize) {
3033 int ret;
3034
3035 ret = check_data_csum(inode, io_bio, bio_offset, page, pg_off);
3036 if (ret < 0)
3037 return -EIO;
3038 }
3039 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -04003040}
Chris Masonb888db22007-08-27 16:49:44 -04003041
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003042/*
3043 * btrfs_add_delayed_iput - perform a delayed iput on @inode
3044 *
3045 * @inode: The inode we want to perform iput on
3046 *
3047 * This function uses the generic vfs_inode::i_count to track whether we should
3048 * just decrement it (in case it's > 1) or if this is the last iput then link
3049 * the inode to the delayed iput machinery. Delayed iputs are processed at
3050 * transaction commit time/superblock commit/cleaner kthread.
3051 */
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003052void btrfs_add_delayed_iput(struct inode *inode)
3053{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003054 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
David Sterba8089fe62015-11-19 14:15:51 +01003055 struct btrfs_inode *binode = BTRFS_I(inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003056
3057 if (atomic_add_unless(&inode->i_count, -1, 1))
3058 return;
3059
Josef Bacik034f7842018-12-03 11:06:52 -05003060 atomic_inc(&fs_info->nr_delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003061 spin_lock(&fs_info->delayed_iput_lock);
Nikolay Borisovc1c3fac2018-01-16 09:31:58 +02003062 ASSERT(list_empty(&binode->delayed_iput));
3063 list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003064 spin_unlock(&fs_info->delayed_iput_lock);
Josef Bacikfd340d02019-01-11 10:21:02 -05003065 if (!test_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags))
3066 wake_up_process(fs_info->cleaner_kthread);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003067}
3068
Josef Bacik63611e72019-06-18 10:59:18 -04003069static void run_delayed_iput_locked(struct btrfs_fs_info *fs_info,
3070 struct btrfs_inode *inode)
3071{
3072 list_del_init(&inode->delayed_iput);
3073 spin_unlock(&fs_info->delayed_iput_lock);
3074 iput(&inode->vfs_inode);
3075 if (atomic_dec_and_test(&fs_info->nr_delayed_iputs))
3076 wake_up(&fs_info->delayed_iputs_wait);
3077 spin_lock(&fs_info->delayed_iput_lock);
3078}
3079
3080static void btrfs_run_delayed_iput(struct btrfs_fs_info *fs_info,
3081 struct btrfs_inode *inode)
3082{
3083 if (!list_empty(&inode->delayed_iput)) {
3084 spin_lock(&fs_info->delayed_iput_lock);
3085 if (!list_empty(&inode->delayed_iput))
3086 run_delayed_iput_locked(fs_info, inode);
3087 spin_unlock(&fs_info->delayed_iput_lock);
3088 }
3089}
3090
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003091void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003092{
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003093
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003094 spin_lock(&fs_info->delayed_iput_lock);
David Sterba8089fe62015-11-19 14:15:51 +01003095 while (!list_empty(&fs_info->delayed_iputs)) {
3096 struct btrfs_inode *inode;
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003097
David Sterba8089fe62015-11-19 14:15:51 +01003098 inode = list_first_entry(&fs_info->delayed_iputs,
3099 struct btrfs_inode, delayed_iput);
Josef Bacik63611e72019-06-18 10:59:18 -04003100 run_delayed_iput_locked(fs_info, inode);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003101 }
David Sterba8089fe62015-11-19 14:15:51 +01003102 spin_unlock(&fs_info->delayed_iput_lock);
Yan, Zheng24bbcf02009-11-12 09:36:34 +00003103}
3104
Josef Bacik034f7842018-12-03 11:06:52 -05003105/**
Nikolay Borisov26396312021-01-22 11:57:59 +02003106 * Wait for flushing all delayed iputs
3107 *
3108 * @fs_info: the filesystem
Josef Bacik034f7842018-12-03 11:06:52 -05003109 *
3110 * This will wait on any delayed iputs that are currently running with KILLABLE
3111 * set. Once they are all done running we will return, unless we are killed in
3112 * which case we return EINTR. This helps in user operations like fallocate etc
3113 * that might get blocked on the iputs.
Nikolay Borisov26396312021-01-22 11:57:59 +02003114 *
3115 * Return EINTR if we were killed, 0 if nothing's pending
Josef Bacik034f7842018-12-03 11:06:52 -05003116 */
3117int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info)
3118{
3119 int ret = wait_event_killable(fs_info->delayed_iputs_wait,
3120 atomic_read(&fs_info->nr_delayed_iputs) == 0);
3121 if (ret)
3122 return -EINTR;
3123 return 0;
3124}
3125
Yan, Zhengd68fc572010-05-16 10:49:58 -04003126/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003127 * This creates an orphan entry for the given inode in case something goes wrong
3128 * in the middle of an unlink.
Josef Bacik7b128762008-07-24 12:17:14 -04003129 */
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003130int btrfs_orphan_add(struct btrfs_trans_handle *trans,
Omar Sandoval27919062018-05-11 13:13:37 -07003131 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003132{
Yan, Zhengd68fc572010-05-16 10:49:58 -04003133 int ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003134
Omar Sandoval27919062018-05-11 13:13:37 -07003135 ret = btrfs_insert_orphan_item(trans, inode->root, btrfs_ino(inode));
3136 if (ret && ret != -EEXIST) {
3137 btrfs_abort_transaction(trans, ret);
3138 return ret;
Yan, Zhengd68fc572010-05-16 10:49:58 -04003139 }
3140
Yan, Zhengd68fc572010-05-16 10:49:58 -04003141 return 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003142}
3143
3144/*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003145 * We have done the delete so we can go ahead and remove the orphan item for
3146 * this particular inode.
Josef Bacik7b128762008-07-24 12:17:14 -04003147 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00003148static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02003149 struct btrfs_inode *inode)
Josef Bacik7b128762008-07-24 12:17:14 -04003150{
Omar Sandoval27919062018-05-11 13:13:37 -07003151 return btrfs_del_orphan_item(trans, inode->root, btrfs_ino(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04003152}
3153
3154/*
3155 * this cleans up any orphans that may be left on the list from the last use
3156 * of this root.
3157 */
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003158int btrfs_orphan_cleanup(struct btrfs_root *root)
Josef Bacik7b128762008-07-24 12:17:14 -04003159{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003160 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik7b128762008-07-24 12:17:14 -04003161 struct btrfs_path *path;
3162 struct extent_buffer *leaf;
Josef Bacik7b128762008-07-24 12:17:14 -04003163 struct btrfs_key key, found_key;
3164 struct btrfs_trans_handle *trans;
3165 struct inode *inode;
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003166 u64 last_objectid = 0;
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003167 int ret = 0, nr_unlink = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003168
Yan, Zhengd68fc572010-05-16 10:49:58 -04003169 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003170 return 0;
Yan, Zhengc71bf092009-11-12 09:34:40 +00003171
3172 path = btrfs_alloc_path();
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003173 if (!path) {
3174 ret = -ENOMEM;
3175 goto out;
3176 }
David Sterbae4058b52015-11-27 16:31:35 +01003177 path->reada = READA_BACK;
Josef Bacik7b128762008-07-24 12:17:14 -04003178
3179 key.objectid = BTRFS_ORPHAN_OBJECTID;
David Sterba962a2982014-06-04 18:41:45 +02003180 key.type = BTRFS_ORPHAN_ITEM_KEY;
Josef Bacik7b128762008-07-24 12:17:14 -04003181 key.offset = (u64)-1;
3182
Josef Bacik7b128762008-07-24 12:17:14 -04003183 while (1) {
3184 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003185 if (ret < 0)
3186 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003187
3188 /*
3189 * if ret == 0 means we found what we were searching for, which
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003190 * is weird, but possible, so only screw with path if we didn't
Josef Bacik7b128762008-07-24 12:17:14 -04003191 * find the key and see if we have stuff that matches
3192 */
3193 if (ret > 0) {
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003194 ret = 0;
Josef Bacik7b128762008-07-24 12:17:14 -04003195 if (path->slots[0] == 0)
3196 break;
3197 path->slots[0]--;
3198 }
3199
3200 /* pull out the item */
3201 leaf = path->nodes[0];
Josef Bacik7b128762008-07-24 12:17:14 -04003202 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3203
3204 /* make sure the item matches what we want */
3205 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3206 break;
David Sterba962a2982014-06-04 18:41:45 +02003207 if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
Josef Bacik7b128762008-07-24 12:17:14 -04003208 break;
3209
3210 /* release the path since we're done with it */
David Sterbab3b4aa72011-04-21 01:20:15 +02003211 btrfs_release_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04003212
3213 /*
3214 * this is where we are basically btrfs_lookup, without the
3215 * crossing root thing. we store the inode number in the
3216 * offset of the orphan item.
3217 */
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003218
3219 if (found_key.offset == last_objectid) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003220 btrfs_err(fs_info,
3221 "Error removing orphan entry, stopping orphan cleanup");
Josef Bacik8f6d7f42011-09-26 15:55:20 -04003222 ret = -EINVAL;
3223 goto out;
3224 }
3225
3226 last_objectid = found_key.offset;
3227
Yan Zheng5d4f98a2009-06-10 10:45:14 -04003228 found_key.objectid = found_key.offset;
3229 found_key.type = BTRFS_INODE_ITEM_KEY;
3230 found_key.offset = 0;
David Sterba0202e832020-05-15 19:35:59 +02003231 inode = btrfs_iget(fs_info->sb, last_objectid, root);
Rusty Russell8c6ffba2013-07-15 11:20:32 +09303232 ret = PTR_ERR_OR_ZERO(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003233 if (ret && ret != -ENOENT)
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003234 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003235
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003236 if (ret == -ENOENT && root == fs_info->tree_root) {
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003237 struct btrfs_root *dead_root;
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003238 int is_dead_root = 0;
3239
3240 /*
3241 * this is an orphan in the tree root. Currently these
3242 * could come from 2 sources:
3243 * a) a snapshot deletion in progress
3244 * b) a free space cache inode
3245 * We need to distinguish those two, as the snapshot
3246 * orphan must not get deleted.
3247 * find_dead_roots already ran before us, so if this
3248 * is a snapshot deletion, we should find the root
Robbie Koa619b3c2020-05-07 10:54:40 +08003249 * in the fs_roots radix tree.
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003250 */
Robbie Koa619b3c2020-05-07 10:54:40 +08003251
3252 spin_lock(&fs_info->fs_roots_radix_lock);
3253 dead_root = radix_tree_lookup(&fs_info->fs_roots_radix,
3254 (unsigned long)found_key.objectid);
3255 if (dead_root && btrfs_root_refs(&dead_root->root_item) == 0)
3256 is_dead_root = 1;
3257 spin_unlock(&fs_info->fs_roots_radix_lock);
3258
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003259 if (is_dead_root) {
3260 /* prevent this orphan from being found again */
3261 key.offset = found_key.objectid - 1;
3262 continue;
3263 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003264
Arne Jansenf8e9e0b2011-12-14 20:12:02 -05003265 }
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003266
Josef Bacika8c9e572011-09-21 16:55:59 -04003267 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003268 * If we have an inode with links, there are a couple of
3269 * possibilities. Old kernels (before v3.12) used to create an
3270 * orphan item for truncate indicating that there were possibly
3271 * extent items past i_size that needed to be deleted. In v3.12,
3272 * truncate was changed to update i_size in sync with the extent
3273 * items, but the (useless) orphan item was still created. Since
3274 * v4.18, we don't create the orphan item for truncate at all.
3275 *
3276 * So, this item could mean that we need to do a truncate, but
3277 * only if this filesystem was last used on a pre-v3.12 kernel
3278 * and was not cleanly unmounted. The odds of that are quite
3279 * slim, and it's a pain to do the truncate now, so just delete
3280 * the orphan item.
3281 *
3282 * It's also possible that this orphan item was supposed to be
3283 * deleted but wasn't. The inode number may have been reused,
3284 * but either way, we can delete the orphan item.
Josef Bacika8c9e572011-09-21 16:55:59 -04003285 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003286 if (ret == -ENOENT || inode->i_nlink) {
3287 if (!ret)
3288 iput(inode);
Josef Bacika8c9e572011-09-21 16:55:59 -04003289 trans = btrfs_start_transaction(root, 1);
3290 if (IS_ERR(trans)) {
3291 ret = PTR_ERR(trans);
3292 goto out;
3293 }
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003294 btrfs_debug(fs_info, "auto deleting %Lu",
3295 found_key.objectid);
Josef Bacika8c9e572011-09-21 16:55:59 -04003296 ret = btrfs_del_orphan_item(trans, root,
3297 found_key.objectid);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003298 btrfs_end_transaction(trans);
Josef Bacik4ef31a42013-08-13 14:10:08 -04003299 if (ret)
3300 goto out;
Josef Bacika8c9e572011-09-21 16:55:59 -04003301 continue;
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003302 }
Josef Bacik7b128762008-07-24 12:17:14 -04003303
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07003304 nr_unlink++;
Josef Bacik7b128762008-07-24 12:17:14 -04003305
3306 /* this will do delete_inode and everything for us */
3307 iput(inode);
3308 }
Miao Xie3254c872011-11-10 20:45:05 -05003309 /* release the path since we're done with it */
3310 btrfs_release_path(path);
3311
Yan, Zhengd68fc572010-05-16 10:49:58 -04003312 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3313
Omar Sandovala575cee2018-05-11 13:13:38 -07003314 if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
Josef Bacik7a7eaa42011-04-13 12:54:33 -04003315 trans = btrfs_join_transaction(root);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003316 if (!IS_ERR(trans))
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003317 btrfs_end_transaction(trans);
Yan, Zhengd68fc572010-05-16 10:49:58 -04003318 }
Josef Bacik7b128762008-07-24 12:17:14 -04003319
3320 if (nr_unlink)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003321 btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003322
3323out:
3324 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003325 btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05003326 btrfs_free_path(path);
3327 return ret;
Josef Bacik7b128762008-07-24 12:17:14 -04003328}
3329
Chris Masond352ac62008-09-29 15:18:18 -04003330/*
Chris Mason46a53cc2009-04-27 11:47:50 -04003331 * very simple check to peek ahead in the leaf looking for xattrs. If we
3332 * don't find any xattrs, we know there can't be any acls.
3333 *
3334 * slot is the slot the inode is in, objectid is the objectid of the inode
3335 */
3336static noinline int acls_after_inode_item(struct extent_buffer *leaf,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003337 int slot, u64 objectid,
3338 int *first_xattr_slot)
Chris Mason46a53cc2009-04-27 11:47:50 -04003339{
3340 u32 nritems = btrfs_header_nritems(leaf);
3341 struct btrfs_key found_key;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003342 static u64 xattr_access = 0;
3343 static u64 xattr_default = 0;
Chris Mason46a53cc2009-04-27 11:47:50 -04003344 int scanned = 0;
3345
Josef Bacikf23b5a52013-06-19 10:16:26 -04003346 if (!xattr_access) {
Andreas Gruenbacher97d79292015-12-02 14:44:35 +01003347 xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3348 strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3349 xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3350 strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
Josef Bacikf23b5a52013-06-19 10:16:26 -04003351 }
3352
Chris Mason46a53cc2009-04-27 11:47:50 -04003353 slot++;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003354 *first_xattr_slot = -1;
Chris Mason46a53cc2009-04-27 11:47:50 -04003355 while (slot < nritems) {
3356 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3357
3358 /* we found a different objectid, there must not be acls */
3359 if (found_key.objectid != objectid)
3360 return 0;
3361
3362 /* we found an xattr, assume we've got an acl */
Josef Bacikf23b5a52013-06-19 10:16:26 -04003363 if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003364 if (*first_xattr_slot == -1)
3365 *first_xattr_slot = slot;
Josef Bacikf23b5a52013-06-19 10:16:26 -04003366 if (found_key.offset == xattr_access ||
3367 found_key.offset == xattr_default)
3368 return 1;
3369 }
Chris Mason46a53cc2009-04-27 11:47:50 -04003370
3371 /*
3372 * we found a key greater than an xattr key, there can't
3373 * be any acls later on
3374 */
3375 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3376 return 0;
3377
3378 slot++;
3379 scanned++;
3380
3381 /*
3382 * it goes inode, inode backrefs, xattrs, extents,
3383 * so if there are a ton of hard links to an inode there can
3384 * be a lot of backrefs. Don't waste time searching too hard,
3385 * this is just an optimization
3386 */
3387 if (scanned >= 8)
3388 break;
3389 }
3390 /* we hit the end of the leaf before we found an xattr or
3391 * something larger than an xattr. We have to assume the inode
3392 * has acls
3393 */
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003394 if (*first_xattr_slot == -1)
3395 *first_xattr_slot = slot;
Chris Mason46a53cc2009-04-27 11:47:50 -04003396 return 1;
3397}
3398
3399/*
Chris Masond352ac62008-09-29 15:18:18 -04003400 * read an inode from the btree into the in-memory inode
3401 */
Filipe Manana4222ea72018-10-24 10:13:03 +01003402static int btrfs_read_locked_inode(struct inode *inode,
3403 struct btrfs_path *in_path)
Chris Mason39279cc2007-06-12 06:35:45 -04003404{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003405 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Filipe Manana4222ea72018-10-24 10:13:03 +01003406 struct btrfs_path *path = in_path;
Chris Mason5f39d392007-10-15 16:14:19 -04003407 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003408 struct btrfs_inode_item *inode_item;
3409 struct btrfs_root *root = BTRFS_I(inode)->root;
3410 struct btrfs_key location;
Miao Xie67de1172013-12-26 13:07:06 +08003411 unsigned long ptr;
Chris Mason46a53cc2009-04-27 11:47:50 -04003412 int maybe_acls;
Josef Bacik618e21d2007-07-11 10:18:17 -04003413 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04003414 int ret;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003415 bool filled = false;
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003416 int first_xattr_slot;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003417
3418 ret = btrfs_fill_inode(inode, &rdev);
3419 if (!ret)
3420 filled = true;
Chris Mason39279cc2007-06-12 06:35:45 -04003421
Filipe Manana4222ea72018-10-24 10:13:03 +01003422 if (!path) {
3423 path = btrfs_alloc_path();
3424 if (!path)
3425 return -ENOMEM;
3426 }
Mark Fasheh1748f842011-07-12 11:25:31 -07003427
Chris Mason39279cc2007-06-12 06:35:45 -04003428 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05003429
Chris Mason39279cc2007-06-12 06:35:45 -04003430 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Filipe Manana67710892016-06-06 11:51:25 +01003431 if (ret) {
Filipe Manana4222ea72018-10-24 10:13:03 +01003432 if (path != in_path)
3433 btrfs_free_path(path);
Al Virof5b3a412018-07-29 23:04:51 +01003434 return ret;
Filipe Manana67710892016-06-06 11:51:25 +01003435 }
Chris Mason39279cc2007-06-12 06:35:45 -04003436
Chris Mason5f39d392007-10-15 16:14:19 -04003437 leaf = path->nodes[0];
Miao Xie2f7e33d2011-06-23 07:27:13 +00003438
3439 if (filled)
Miao Xie67de1172013-12-26 13:07:06 +08003440 goto cache_index;
Miao Xie2f7e33d2011-06-23 07:27:13 +00003441
Chris Mason5f39d392007-10-15 16:14:19 -04003442 inode_item = btrfs_item_ptr(leaf, path->slots[0],
3443 struct btrfs_inode_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003444 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
Miklos Szeredibfe86842011-10-28 14:13:29 +02003445 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
Eric W. Biederman2f2f43d2012-02-10 11:05:07 -08003446 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3447 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003448 btrfs_i_size_write(BTRFS_I(inode), btrfs_inode_size(leaf, inode_item));
Josef Bacik41a2ee72020-01-17 09:02:21 -05003449 btrfs_inode_set_file_extent_range(BTRFS_I(inode), 0,
3450 round_up(i_size_read(inode), fs_info->sectorsize));
Chris Mason5f39d392007-10-15 16:14:19 -04003451
David Sterbaa937b972014-12-12 17:39:12 +01003452 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3453 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
Chris Mason5f39d392007-10-15 16:14:19 -04003454
David Sterbaa937b972014-12-12 17:39:12 +01003455 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3456 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
Chris Mason5f39d392007-10-15 16:14:19 -04003457
David Sterbaa937b972014-12-12 17:39:12 +01003458 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3459 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
Chris Mason5f39d392007-10-15 16:14:19 -04003460
chandan r9cc97d62012-07-04 12:48:07 +05303461 BTRFS_I(inode)->i_otime.tv_sec =
3462 btrfs_timespec_sec(leaf, &inode_item->otime);
3463 BTRFS_I(inode)->i_otime.tv_nsec =
3464 btrfs_timespec_nsec(leaf, &inode_item->otime);
Chris Mason5f39d392007-10-15 16:14:19 -04003465
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003466 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003467 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik5dc562c2012-08-17 13:14:17 -04003468 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3469
Jeff Laytonc7f88c42017-12-11 06:35:12 -05003470 inode_set_iversion_queried(inode,
3471 btrfs_inode_sequence(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04003472 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04003473 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04003474 rdev = btrfs_inode_rdev(leaf, inode_item);
3475
Josef Bacikaec74772008-07-24 12:12:38 -04003476 BTRFS_I(inode)->index_cnt = (u64)-1;
Yan Zhengd2fb3432008-12-11 16:30:39 -05003477 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Miao Xie67de1172013-12-26 13:07:06 +08003478
3479cache_index:
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003480 /*
3481 * If we were modified in the current generation and evicted from memory
3482 * and then re-read we need to do a full sync since we don't have any
3483 * idea about which extents were modified before we were evicted from
3484 * cache.
3485 *
3486 * This is required for both inode re-read from disk and delayed inode
3487 * in delayed_nodes_tree.
3488 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003489 if (BTRFS_I(inode)->last_trans == fs_info->generation)
Yang Dongsheng6e17d302015-04-09 12:08:43 +08003490 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3491 &BTRFS_I(inode)->runtime_flags);
3492
Filipe Mananabde6c242015-07-24 00:00:19 +01003493 /*
3494 * We don't persist the id of the transaction where an unlink operation
3495 * against the inode was last made. So here we assume the inode might
3496 * have been evicted, and therefore the exact value of last_unlink_trans
3497 * lost, and set it to last_trans to avoid metadata inconsistencies
3498 * between the inode and its parent if the inode is fsync'ed and the log
3499 * replayed. For example, in the scenario:
3500 *
3501 * touch mydir/foo
3502 * ln mydir/foo mydir/bar
3503 * sync
3504 * unlink mydir/bar
3505 * echo 2 > /proc/sys/vm/drop_caches # evicts inode
3506 * xfs_io -c fsync mydir/foo
3507 * <power failure>
3508 * mount fs, triggers fsync log replay
3509 *
3510 * We must make sure that when we fsync our inode foo we also log its
3511 * parent inode, otherwise after log replay the parent still has the
3512 * dentry with the "bar" name but our inode foo has a link count of 1
3513 * and doesn't have an inode ref with the name "bar" anymore.
3514 *
3515 * Setting last_unlink_trans to last_trans is a pessimistic approach,
Nicholas D Steeves01327612016-05-19 21:18:45 -04003516 * but it guarantees correctness at the expense of occasional full
Filipe Mananabde6c242015-07-24 00:00:19 +01003517 * transaction commits on fsync if our inode is a directory, or if our
3518 * inode is not a directory, logging its parent unnecessarily.
3519 */
3520 BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3521
Filipe Manana3ebac172020-07-15 12:30:43 +01003522 /*
3523 * Same logic as for last_unlink_trans. We don't persist the generation
3524 * of the last transaction where this inode was used for a reflink
3525 * operation, so after eviction and reloading the inode we must be
3526 * pessimistic and assume the last transaction that modified the inode.
3527 */
3528 BTRFS_I(inode)->last_reflink_trans = BTRFS_I(inode)->last_trans;
3529
Miao Xie67de1172013-12-26 13:07:06 +08003530 path->slots[0]++;
3531 if (inode->i_nlink != 1 ||
3532 path->slots[0] >= btrfs_header_nritems(leaf))
3533 goto cache_acl;
3534
3535 btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003536 if (location.objectid != btrfs_ino(BTRFS_I(inode)))
Miao Xie67de1172013-12-26 13:07:06 +08003537 goto cache_acl;
3538
3539 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3540 if (location.type == BTRFS_INODE_REF_KEY) {
3541 struct btrfs_inode_ref *ref;
3542
3543 ref = (struct btrfs_inode_ref *)ptr;
3544 BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3545 } else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3546 struct btrfs_inode_extref *extref;
3547
3548 extref = (struct btrfs_inode_extref *)ptr;
3549 BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3550 extref);
3551 }
Miao Xie2f7e33d2011-06-23 07:27:13 +00003552cache_acl:
Chris Mason46a53cc2009-04-27 11:47:50 -04003553 /*
3554 * try to precache a NULL acl entry for files that don't have
3555 * any xattrs or acls
3556 */
Li Zefan33345d012011-04-20 10:31:50 +08003557 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
David Sterbaf85b7372017-01-20 14:54:07 +01003558 btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003559 if (first_xattr_slot != -1) {
3560 path->slots[0] = first_xattr_slot;
3561 ret = btrfs_load_inode_props(inode, path);
3562 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003563 btrfs_err(fs_info,
David Sterba351fd352014-05-15 16:48:20 +02003564 "error loading props for ino %llu (root %llu): %d",
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003565 btrfs_ino(BTRFS_I(inode)),
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003566 root->root_key.objectid, ret);
3567 }
Filipe Manana4222ea72018-10-24 10:13:03 +01003568 if (path != in_path)
3569 btrfs_free_path(path);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00003570
Al Viro72c04902009-06-24 16:58:48 -04003571 if (!maybe_acls)
3572 cache_no_acl(inode);
Chris Mason46a53cc2009-04-27 11:47:50 -04003573
Chris Mason39279cc2007-06-12 06:35:45 -04003574 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04003575 case S_IFREG:
3576 inode->i_mapping->a_ops = &btrfs_aops;
3577 inode->i_fop = &btrfs_file_operations;
3578 inode->i_op = &btrfs_file_inode_operations;
3579 break;
3580 case S_IFDIR:
3581 inode->i_fop = &btrfs_dir_file_operations;
Omar Sandoval67ade052017-01-25 17:06:38 -08003582 inode->i_op = &btrfs_dir_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -04003583 break;
3584 case S_IFLNK:
3585 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05003586 inode_nohighmem(inode);
Omar Sandoval4779cc02018-09-24 15:16:55 -07003587 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason39279cc2007-06-12 06:35:45 -04003588 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04003589 default:
Jim Owens0279b4c2009-02-04 09:29:13 -05003590 inode->i_op = &btrfs_special_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -04003591 init_special_inode(inode, inode->i_mode, rdev);
3592 break;
Chris Mason39279cc2007-06-12 06:35:45 -04003593 }
Christoph Hellwig6cbff002009-04-17 10:37:41 +02003594
David Sterba7b6a2212018-03-26 18:40:21 +02003595 btrfs_sync_inode_flags_to_i_flags(inode);
Filipe Manana67710892016-06-06 11:51:25 +01003596 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003597}
3598
Chris Masond352ac62008-09-29 15:18:18 -04003599/*
3600 * given a leaf and an inode, copy the inode fields into the leaf
3601 */
Chris Masone02119d2008-09-05 16:13:11 -04003602static void fill_inode_item(struct btrfs_trans_handle *trans,
3603 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04003604 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04003605 struct inode *inode)
3606{
Liu Bo51fab692012-12-27 09:01:21 +00003607 struct btrfs_map_token token;
Chris Mason5f39d392007-10-15 16:14:19 -04003608
David Sterbac82f8232019-08-09 17:48:21 +02003609 btrfs_init_map_token(&token, leaf);
Chris Mason5f39d392007-10-15 16:14:19 -04003610
David Sterbacc4c13d2020-04-29 02:15:56 +02003611 btrfs_set_token_inode_uid(&token, item, i_uid_read(inode));
3612 btrfs_set_token_inode_gid(&token, item, i_gid_read(inode));
3613 btrfs_set_token_inode_size(&token, item, BTRFS_I(inode)->disk_i_size);
3614 btrfs_set_token_inode_mode(&token, item, inode->i_mode);
3615 btrfs_set_token_inode_nlink(&token, item, inode->i_nlink);
Chris Mason5f39d392007-10-15 16:14:19 -04003616
David Sterbacc4c13d2020-04-29 02:15:56 +02003617 btrfs_set_token_timespec_sec(&token, &item->atime,
3618 inode->i_atime.tv_sec);
3619 btrfs_set_token_timespec_nsec(&token, &item->atime,
3620 inode->i_atime.tv_nsec);
Chris Mason5f39d392007-10-15 16:14:19 -04003621
David Sterbacc4c13d2020-04-29 02:15:56 +02003622 btrfs_set_token_timespec_sec(&token, &item->mtime,
3623 inode->i_mtime.tv_sec);
3624 btrfs_set_token_timespec_nsec(&token, &item->mtime,
3625 inode->i_mtime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003626
David Sterbacc4c13d2020-04-29 02:15:56 +02003627 btrfs_set_token_timespec_sec(&token, &item->ctime,
3628 inode->i_ctime.tv_sec);
3629 btrfs_set_token_timespec_nsec(&token, &item->ctime,
3630 inode->i_ctime.tv_nsec);
Liu Bo51fab692012-12-27 09:01:21 +00003631
David Sterbacc4c13d2020-04-29 02:15:56 +02003632 btrfs_set_token_timespec_sec(&token, &item->otime,
3633 BTRFS_I(inode)->i_otime.tv_sec);
3634 btrfs_set_token_timespec_nsec(&token, &item->otime,
3635 BTRFS_I(inode)->i_otime.tv_nsec);
chandan r9cc97d62012-07-04 12:48:07 +05303636
David Sterbacc4c13d2020-04-29 02:15:56 +02003637 btrfs_set_token_inode_nbytes(&token, item, inode_get_bytes(inode));
3638 btrfs_set_token_inode_generation(&token, item,
3639 BTRFS_I(inode)->generation);
3640 btrfs_set_token_inode_sequence(&token, item, inode_peek_iversion(inode));
3641 btrfs_set_token_inode_transid(&token, item, trans->transid);
3642 btrfs_set_token_inode_rdev(&token, item, inode->i_rdev);
3643 btrfs_set_token_inode_flags(&token, item, BTRFS_I(inode)->flags);
3644 btrfs_set_token_inode_block_group(&token, item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003645}
3646
Chris Masond352ac62008-09-29 15:18:18 -04003647/*
3648 * copy everything in the in-memory inode into the btree.
3649 */
Chris Mason21151332011-11-10 20:39:08 -05003650static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003651 struct btrfs_root *root,
3652 struct btrfs_inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04003653{
3654 struct btrfs_inode_item *inode_item;
3655 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04003656 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04003657 int ret;
3658
3659 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08003660 if (!path)
3661 return -ENOMEM;
3662
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003663 ret = btrfs_lookup_inode(trans, root, path, &inode->location, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003664 if (ret) {
3665 if (ret > 0)
3666 ret = -ENOENT;
3667 goto failed;
3668 }
3669
Chris Mason5f39d392007-10-15 16:14:19 -04003670 leaf = path->nodes[0];
3671 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Miao Xie16cdcec2011-04-22 18:12:22 +08003672 struct btrfs_inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04003673
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003674 fill_inode_item(trans, leaf, inode_item, &inode->vfs_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003675 btrfs_mark_buffer_dirty(leaf);
Nikolay Borisovdfeb9e72020-11-02 16:48:58 +02003676 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003677 ret = 0;
3678failed:
Chris Mason39279cc2007-06-12 06:35:45 -04003679 btrfs_free_path(path);
3680 return ret;
3681}
3682
Chris Masond352ac62008-09-29 15:18:18 -04003683/*
Chris Mason21151332011-11-10 20:39:08 -05003684 * copy everything in the in-memory inode into the btree.
3685 */
3686noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003687 struct btrfs_root *root,
3688 struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003689{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003690 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason21151332011-11-10 20:39:08 -05003691 int ret;
3692
3693 /*
3694 * If the inode is a free space inode, we can deadlock during commit
3695 * if we put it into the delayed code.
3696 *
3697 * The data relocation inode should also be directly updated
3698 * without delay
3699 */
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003700 if (!btrfs_is_free_space_inode(inode)
Josef Bacik1d52c782014-09-18 11:30:44 -04003701 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003702 && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
Alexander Block8ea05e32012-07-25 17:35:53 +02003703 btrfs_update_root_times(trans, root);
3704
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003705 ret = btrfs_delayed_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003706 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003707 btrfs_set_inode_last_trans(trans, inode);
Chris Mason21151332011-11-10 20:39:08 -05003708 return ret;
3709 }
3710
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003711 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003712}
3713
Nikolay Borisov729f7962020-11-02 16:49:06 +02003714int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3715 struct btrfs_root *root, struct btrfs_inode *inode)
Chris Mason21151332011-11-10 20:39:08 -05003716{
3717 int ret;
3718
Nikolay Borisov729f7962020-11-02 16:49:06 +02003719 ret = btrfs_update_inode(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003720 if (ret == -ENOSPC)
Nikolay Borisov729f7962020-11-02 16:49:06 +02003721 return btrfs_update_inode_item(trans, root, inode);
Chris Mason21151332011-11-10 20:39:08 -05003722 return ret;
3723}
3724
3725/*
Chris Masond352ac62008-09-29 15:18:18 -04003726 * unlink helper that gets used here in inode.c and in the tree logging
3727 * recovery code. It remove a link in a directory with a given name, and
3728 * also drops the back refs in the inode to the directory
3729 */
Al Viro92986792011-03-04 17:14:37 +00003730static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3731 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003732 struct btrfs_inode *dir,
3733 struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003734 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04003735{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003736 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04003737 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003738 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003739 struct btrfs_dir_item *di;
Josef Bacikaec74772008-07-24 12:12:38 -04003740 u64 index;
Li Zefan33345d012011-04-20 10:31:50 +08003741 u64 ino = btrfs_ino(inode);
3742 u64 dir_ino = btrfs_ino(dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003743
3744 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04003745 if (!path) {
3746 ret = -ENOMEM;
Tsutomu Itoh554233a2011-02-03 03:16:25 +00003747 goto out;
Chris Mason54aa1f42007-06-22 14:16:25 -04003748 }
3749
Li Zefan33345d012011-04-20 10:31:50 +08003750 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Chris Mason39279cc2007-06-12 06:35:45 -04003751 name, name_len, -1);
Liu Bo3cf50682018-09-12 06:06:26 +08003752 if (IS_ERR_OR_NULL(di)) {
3753 ret = di ? PTR_ERR(di) : -ENOENT;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 goto err;
3755 }
Chris Mason39279cc2007-06-12 06:35:45 -04003756 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04003757 if (ret)
3758 goto err;
David Sterbab3b4aa72011-04-21 01:20:15 +02003759 btrfs_release_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04003760
Miao Xie67de1172013-12-26 13:07:06 +08003761 /*
3762 * If we don't have dir index, we have to get it by looking up
3763 * the inode ref, since we get the inode ref, remove it directly,
3764 * it is unnecessary to do delayed deletion.
3765 *
3766 * But if we have dir index, needn't search inode ref to get it.
3767 * Since the inode ref is close to the inode item, it is better
3768 * that we delay to delete it, and just do this deletion when
3769 * we update the inode item.
3770 */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003771 if (inode->dir_index) {
Miao Xie67de1172013-12-26 13:07:06 +08003772 ret = btrfs_delayed_delete_inode_ref(inode);
3773 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003774 index = inode->dir_index;
Miao Xie67de1172013-12-26 13:07:06 +08003775 goto skip_backref;
3776 }
3777 }
3778
Li Zefan33345d012011-04-20 10:31:50 +08003779 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3780 dir_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003781 if (ret) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04003782 btrfs_info(fs_info,
Simon Kirbyc2cf52e2013-03-19 22:41:23 +00003783 "failed to delete reference to %.*s, inode %llu parent %llu",
Geert Uytterhoevenc1c9ff72013-08-20 13:20:07 +02003784 name_len, name, ino, dir_ino);
Jeff Mahoney66642832016-06-10 18:19:25 -04003785 btrfs_abort_transaction(trans, ret);
Josef Bacikaec74772008-07-24 12:12:38 -04003786 goto err;
3787 }
Miao Xie67de1172013-12-26 13:07:06 +08003788skip_backref:
Lu Fengqi9add2942018-08-01 11:32:26 +08003789 ret = btrfs_delete_delayed_dir_index(trans, dir, index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003790 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003791 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04003792 goto err;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003793 }
Chris Mason39279cc2007-06-12 06:35:45 -04003794
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003795 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3796 dir_ino);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003797 if (ret != 0 && ret != -ENOENT) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003798 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003799 goto err;
3800 }
Chris Masone02119d2008-09-05 16:13:11 -04003801
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003802 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3803 index);
Chris Mason6418c962010-10-30 07:34:24 -04003804 if (ret == -ENOENT)
3805 ret = 0;
Zach Brownd4e39912013-04-02 21:02:16 +00003806 else if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003807 btrfs_abort_transaction(trans, ret);
Josef Bacik63611e72019-06-18 10:59:18 -04003808
3809 /*
3810 * If we have a pending delayed iput we could end up with the final iput
3811 * being run in btrfs-cleaner context. If we have enough of these built
3812 * up we can end up burning a lot of time in btrfs-cleaner without any
3813 * way to throttle the unlinks. Since we're currently holding a ref on
3814 * the inode we can run the delayed iput here without any issues as the
3815 * final iput won't be done until after we drop the ref we're currently
3816 * holding.
3817 */
3818 btrfs_run_delayed_iput(fs_info, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003819err:
3820 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04003821 if (ret)
3822 goto out;
3823
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003824 btrfs_i_size_write(dir, dir->vfs_inode.i_size - name_len * 2);
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003825 inode_inc_iversion(&inode->vfs_inode);
3826 inode_inc_iversion(&dir->vfs_inode);
3827 inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3828 dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003829 ret = btrfs_update_inode(trans, root, dir);
Chris Masone02119d2008-09-05 16:13:11 -04003830out:
Chris Mason39279cc2007-06-12 06:35:45 -04003831 return ret;
3832}
3833
Al Viro92986792011-03-04 17:14:37 +00003834int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3835 struct btrfs_root *root,
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003836 struct btrfs_inode *dir, struct btrfs_inode *inode,
Al Viro92986792011-03-04 17:14:37 +00003837 const char *name, int name_len)
3838{
3839 int ret;
3840 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
3841 if (!ret) {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003842 drop_nlink(&inode->vfs_inode);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02003843 ret = btrfs_update_inode(trans, root, inode);
Al Viro92986792011-03-04 17:14:37 +00003844 }
3845 return ret;
3846}
Yan, Zhenga22285a2010-05-16 10:48:46 -04003847
3848/*
3849 * helper to start transaction for unlink and rmdir.
3850 *
Josef Bacikd52be812013-05-29 14:54:47 -04003851 * unlink and rmdir are special in btrfs, they do not always free space, so
3852 * if we cannot make our reservations the normal way try and see if there is
3853 * plenty of slack room in the global reserve to migrate, otherwise we cannot
3854 * allow the unlink to occur.
Yan, Zhenga22285a2010-05-16 10:48:46 -04003855 */
Josef Bacikd52be812013-05-29 14:54:47 -04003856static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
Yan, Zhenga22285a2010-05-16 10:48:46 -04003857{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003858 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003859
Josef Bacike70bea52011-10-11 14:18:24 -04003860 /*
3861 * 1 for the possible orphan item
3862 * 1 for the dir item
3863 * 1 for the dir index
3864 * 1 for the inode ref
Josef Bacike70bea52011-10-11 14:18:24 -04003865 * 1 for the inode
3866 */
Josef Bacik7f9fe612020-03-13 15:58:05 -04003867 return btrfs_start_transaction_fallback_global_rsv(root, 5);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003868}
3869
Chris Mason39279cc2007-06-12 06:35:45 -04003870static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3871{
Yan, Zhenga22285a2010-05-16 10:48:46 -04003872 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003873 struct btrfs_trans_handle *trans;
David Howells2b0143b2015-03-17 22:25:59 +00003874 struct inode *inode = d_inode(dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04003875 int ret;
3876
Josef Bacikd52be812013-05-29 14:54:47 -04003877 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04003878 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05003879 return PTR_ERR(trans);
Chris Mason5f39d392007-10-15 16:14:19 -04003880
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003881 btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
3882 0);
Chris Mason12fcfd22009-03-24 10:24:20 -04003883
Nikolay Borisov4ec59342017-01-18 00:31:44 +02003884 ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
3885 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
3886 dentry->d_name.len);
Tsutomu Itohb5324022011-07-19 07:27:20 +00003887 if (ret)
3888 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04003889
Yan, Zhenga22285a2010-05-16 10:48:46 -04003890 if (inode->i_nlink == 0) {
Nikolay Borisov73f2e542017-02-20 13:50:59 +02003891 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Tsutomu Itohb5324022011-07-19 07:27:20 +00003892 if (ret)
3893 goto out;
Yan, Zhenga22285a2010-05-16 10:48:46 -04003894 }
Josef Bacik7b128762008-07-24 12:17:14 -04003895
Tsutomu Itohb5324022011-07-19 07:27:20 +00003896out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04003897 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04003898 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04003899 return ret;
3900}
3901
Misono Tomohirof60a2362018-04-18 11:34:52 +09003902static int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
Josef Bacik045d3962019-12-18 17:20:27 -05003903 struct inode *dir, struct dentry *dentry)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003904{
Lu Fengqi401b3b12018-08-01 11:32:30 +08003905 struct btrfs_root *root = BTRFS_I(dir)->root;
Josef Bacik045d3962019-12-18 17:20:27 -05003906 struct btrfs_inode *inode = BTRFS_I(d_inode(dentry));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003907 struct btrfs_path *path;
3908 struct extent_buffer *leaf;
3909 struct btrfs_dir_item *di;
3910 struct btrfs_key key;
Josef Bacik045d3962019-12-18 17:20:27 -05003911 const char *name = dentry->d_name.name;
3912 int name_len = dentry->d_name.len;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003913 u64 index;
3914 int ret;
Josef Bacik045d3962019-12-18 17:20:27 -05003915 u64 objectid;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02003916 u64 dir_ino = btrfs_ino(BTRFS_I(dir));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003917
Josef Bacik045d3962019-12-18 17:20:27 -05003918 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID) {
3919 objectid = inode->root->root_key.objectid;
3920 } else if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
3921 objectid = inode->location.objectid;
3922 } else {
3923 WARN_ON(1);
3924 return -EINVAL;
3925 }
3926
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003927 path = btrfs_alloc_path();
3928 if (!path)
3929 return -ENOMEM;
3930
Li Zefan33345d012011-04-20 10:31:50 +08003931 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003932 name, name_len, -1);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003933 if (IS_ERR_OR_NULL(di)) {
Liu Bo3cf50682018-09-12 06:06:26 +08003934 ret = di ? PTR_ERR(di) : -ENOENT;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003935 goto out;
3936 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003937
3938 leaf = path->nodes[0];
3939 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3940 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3941 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003942 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003943 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003944 goto out;
3945 }
David Sterbab3b4aa72011-04-21 01:20:15 +02003946 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003947
Josef Bacikd49d3282019-12-18 17:20:28 -05003948 /*
3949 * This is a placeholder inode for a subvolume we didn't have a
3950 * reference to at the time of the snapshot creation. In the meantime
3951 * we could have renamed the real subvol link into our snapshot, so
3952 * depending on btrfs_del_root_ref to return -ENOENT here is incorret.
3953 * Instead simply lookup the dir_index_item for this entry so we can
3954 * remove it. Otherwise we know we have a ref to the root and we can
3955 * call btrfs_del_root_ref, and it _shouldn't_ fail.
3956 */
3957 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID) {
Li Zefan33345d012011-04-20 10:31:50 +08003958 di = btrfs_search_dir_index_item(root, path, dir_ino,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003959 name, name_len);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003960 if (IS_ERR_OR_NULL(di)) {
3961 if (!di)
3962 ret = -ENOENT;
3963 else
3964 ret = PTR_ERR(di);
Jeff Mahoney66642832016-06-10 18:19:25 -04003965 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003966 goto out;
3967 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003968
3969 leaf = path->nodes[0];
3970 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003971 index = key.offset;
Josef Bacikd49d3282019-12-18 17:20:28 -05003972 btrfs_release_path(path);
3973 } else {
3974 ret = btrfs_del_root_ref(trans, objectid,
3975 root->root_key.objectid, dir_ino,
3976 &index, name, name_len);
3977 if (ret) {
3978 btrfs_abort_transaction(trans, ret);
3979 goto out;
3980 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003981 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003982
Lu Fengqi9add2942018-08-01 11:32:26 +08003983 ret = btrfs_delete_delayed_dir_index(trans, BTRFS_I(dir), index);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003984 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04003985 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003986 goto out;
3987 }
Miao Xie16cdcec2011-04-22 18:12:22 +08003988
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02003989 btrfs_i_size_write(BTRFS_I(dir), dir->i_size - name_len * 2);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04003990 inode_inc_iversion(dir);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07003991 dir->i_mtime = dir->i_ctime = current_time(dir);
Nikolay Borisov729f7962020-11-02 16:49:06 +02003992 ret = btrfs_update_inode_fallback(trans, root, BTRFS_I(dir));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003993 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04003994 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003995out:
Josef Bacik71d7aed2011-06-14 14:24:32 -04003996 btrfs_free_path(path);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01003997 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04003998}
3999
Misono Tomohiroec42f162018-04-18 11:34:13 +09004000/*
4001 * Helper to check if the subvolume references other subvolumes or if it's
4002 * default.
4003 */
Misono Tomohirof60a2362018-04-18 11:34:52 +09004004static noinline int may_destroy_subvol(struct btrfs_root *root)
Misono Tomohiroec42f162018-04-18 11:34:13 +09004005{
4006 struct btrfs_fs_info *fs_info = root->fs_info;
4007 struct btrfs_path *path;
4008 struct btrfs_dir_item *di;
4009 struct btrfs_key key;
4010 u64 dir_id;
4011 int ret;
4012
4013 path = btrfs_alloc_path();
4014 if (!path)
4015 return -ENOMEM;
4016
4017 /* Make sure this root isn't set as the default subvol */
4018 dir_id = btrfs_super_root_dir(fs_info->super_copy);
4019 di = btrfs_lookup_dir_item(NULL, fs_info->tree_root, path,
4020 dir_id, "default", 7, 0);
4021 if (di && !IS_ERR(di)) {
4022 btrfs_dir_item_key_to_cpu(path->nodes[0], di, &key);
4023 if (key.objectid == root->root_key.objectid) {
4024 ret = -EPERM;
4025 btrfs_err(fs_info,
4026 "deleting default subvolume %llu is not allowed",
4027 key.objectid);
4028 goto out;
4029 }
4030 btrfs_release_path(path);
4031 }
4032
4033 key.objectid = root->root_key.objectid;
4034 key.type = BTRFS_ROOT_REF_KEY;
4035 key.offset = (u64)-1;
4036
4037 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
4038 if (ret < 0)
4039 goto out;
4040 BUG_ON(ret == 0);
4041
4042 ret = 0;
4043 if (path->slots[0] > 0) {
4044 path->slots[0]--;
4045 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
4046 if (key.objectid == root->root_key.objectid &&
4047 key.type == BTRFS_ROOT_REF_KEY)
4048 ret = -ENOTEMPTY;
4049 }
4050out:
4051 btrfs_free_path(path);
4052 return ret;
4053}
4054
Nikolay Borisov20a68002018-04-27 14:36:24 +03004055/* Delete all dentries for inodes belonging to the root */
4056static void btrfs_prune_dentries(struct btrfs_root *root)
4057{
4058 struct btrfs_fs_info *fs_info = root->fs_info;
4059 struct rb_node *node;
4060 struct rb_node *prev;
4061 struct btrfs_inode *entry;
4062 struct inode *inode;
4063 u64 objectid = 0;
4064
4065 if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
4066 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4067
4068 spin_lock(&root->inode_lock);
4069again:
4070 node = root->inode_tree.rb_node;
4071 prev = NULL;
4072 while (node) {
4073 prev = node;
4074 entry = rb_entry(node, struct btrfs_inode, rb_node);
4075
David Sterba37508512018-06-29 10:56:40 +02004076 if (objectid < btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004077 node = node->rb_left;
David Sterba37508512018-06-29 10:56:40 +02004078 else if (objectid > btrfs_ino(entry))
Nikolay Borisov20a68002018-04-27 14:36:24 +03004079 node = node->rb_right;
4080 else
4081 break;
4082 }
4083 if (!node) {
4084 while (prev) {
4085 entry = rb_entry(prev, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004086 if (objectid <= btrfs_ino(entry)) {
Nikolay Borisov20a68002018-04-27 14:36:24 +03004087 node = prev;
4088 break;
4089 }
4090 prev = rb_next(prev);
4091 }
4092 }
4093 while (node) {
4094 entry = rb_entry(node, struct btrfs_inode, rb_node);
David Sterba37508512018-06-29 10:56:40 +02004095 objectid = btrfs_ino(entry) + 1;
Nikolay Borisov20a68002018-04-27 14:36:24 +03004096 inode = igrab(&entry->vfs_inode);
4097 if (inode) {
4098 spin_unlock(&root->inode_lock);
4099 if (atomic_read(&inode->i_count) > 1)
4100 d_prune_aliases(inode);
4101 /*
4102 * btrfs_drop_inode will have it removed from the inode
4103 * cache when its usage count hits zero.
4104 */
4105 iput(inode);
4106 cond_resched();
4107 spin_lock(&root->inode_lock);
4108 goto again;
4109 }
4110
4111 if (cond_resched_lock(&root->inode_lock))
4112 goto again;
4113
4114 node = rb_next(node);
4115 }
4116 spin_unlock(&root->inode_lock);
4117}
4118
Misono Tomohirof60a2362018-04-18 11:34:52 +09004119int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry)
4120{
4121 struct btrfs_fs_info *fs_info = btrfs_sb(dentry->d_sb);
4122 struct btrfs_root *root = BTRFS_I(dir)->root;
4123 struct inode *inode = d_inode(dentry);
4124 struct btrfs_root *dest = BTRFS_I(inode)->root;
4125 struct btrfs_trans_handle *trans;
4126 struct btrfs_block_rsv block_rsv;
4127 u64 root_flags;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004128 int ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004129
4130 /*
4131 * Don't allow to delete a subvolume with send in progress. This is
4132 * inside the inode lock so the error handling that has to drop the bit
4133 * again is not run concurrently.
4134 */
4135 spin_lock(&dest->root_item_lock);
Lu Fengqia7176f72018-08-04 21:10:53 +08004136 if (dest->send_in_progress) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004137 spin_unlock(&dest->root_item_lock);
4138 btrfs_warn(fs_info,
4139 "attempt to delete subvolume %llu during send",
4140 dest->root_key.objectid);
4141 return -EPERM;
4142 }
Lu Fengqia7176f72018-08-04 21:10:53 +08004143 root_flags = btrfs_root_flags(&dest->root_item);
4144 btrfs_set_root_flags(&dest->root_item,
4145 root_flags | BTRFS_ROOT_SUBVOL_DEAD);
4146 spin_unlock(&dest->root_item_lock);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004147
4148 down_write(&fs_info->subvol_sem);
4149
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004150 ret = may_destroy_subvol(dest);
4151 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004152 goto out_up_write;
4153
4154 btrfs_init_block_rsv(&block_rsv, BTRFS_BLOCK_RSV_TEMP);
4155 /*
4156 * One for dir inode,
4157 * two for dir entries,
4158 * two for root ref/backref.
4159 */
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004160 ret = btrfs_subvolume_reserve_metadata(root, &block_rsv, 5, true);
4161 if (ret)
Misono Tomohirof60a2362018-04-18 11:34:52 +09004162 goto out_up_write;
4163
4164 trans = btrfs_start_transaction(root, 0);
4165 if (IS_ERR(trans)) {
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004166 ret = PTR_ERR(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004167 goto out_release;
4168 }
4169 trans->block_rsv = &block_rsv;
4170 trans->bytes_reserved = block_rsv.size;
4171
4172 btrfs_record_snapshot_destroy(trans, BTRFS_I(dir));
4173
Josef Bacik045d3962019-12-18 17:20:27 -05004174 ret = btrfs_unlink_subvol(trans, dir, dentry);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004175 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004176 btrfs_abort_transaction(trans, ret);
4177 goto out_end_trans;
4178 }
4179
4180 btrfs_record_root_in_trans(trans, dest);
4181
4182 memset(&dest->root_item.drop_progress, 0,
4183 sizeof(dest->root_item.drop_progress));
David Sterbac8422682020-09-15 21:44:52 +02004184 btrfs_set_root_drop_level(&dest->root_item, 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004185 btrfs_set_root_refs(&dest->root_item, 0);
4186
4187 if (!test_and_set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &dest->state)) {
4188 ret = btrfs_insert_orphan_item(trans,
4189 fs_info->tree_root,
4190 dest->root_key.objectid);
4191 if (ret) {
4192 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004193 goto out_end_trans;
4194 }
4195 }
4196
Lu Fengqid1957792018-05-29 15:01:54 +08004197 ret = btrfs_uuid_tree_remove(trans, dest->root_item.uuid,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004198 BTRFS_UUID_KEY_SUBVOL,
4199 dest->root_key.objectid);
4200 if (ret && ret != -ENOENT) {
4201 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004202 goto out_end_trans;
4203 }
4204 if (!btrfs_is_empty_uuid(dest->root_item.received_uuid)) {
Lu Fengqid1957792018-05-29 15:01:54 +08004205 ret = btrfs_uuid_tree_remove(trans,
Misono Tomohirof60a2362018-04-18 11:34:52 +09004206 dest->root_item.received_uuid,
4207 BTRFS_UUID_KEY_RECEIVED_SUBVOL,
4208 dest->root_key.objectid);
4209 if (ret && ret != -ENOENT) {
4210 btrfs_abort_transaction(trans, ret);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004211 goto out_end_trans;
4212 }
4213 }
4214
Qu Wenruo082b6c92020-06-16 10:17:37 +08004215 free_anon_bdev(dest->anon_dev);
4216 dest->anon_dev = 0;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004217out_end_trans:
4218 trans->block_rsv = NULL;
4219 trans->bytes_reserved = 0;
4220 ret = btrfs_end_transaction(trans);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004221 inode->i_flags |= S_DEAD;
4222out_release:
Qu Wenruoe85fde52020-07-24 14:46:10 +08004223 btrfs_subvolume_release_metadata(root, &block_rsv);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004224out_up_write:
4225 up_write(&fs_info->subvol_sem);
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004226 if (ret) {
Misono Tomohirof60a2362018-04-18 11:34:52 +09004227 spin_lock(&dest->root_item_lock);
4228 root_flags = btrfs_root_flags(&dest->root_item);
4229 btrfs_set_root_flags(&dest->root_item,
4230 root_flags & ~BTRFS_ROOT_SUBVOL_DEAD);
4231 spin_unlock(&dest->root_item_lock);
4232 } else {
4233 d_invalidate(dentry);
Nikolay Borisov20a68002018-04-27 14:36:24 +03004234 btrfs_prune_dentries(dest);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004235 ASSERT(dest->send_in_progress == 0);
Misono Tomohirof60a2362018-04-18 11:34:52 +09004236 }
4237
Nikolay Borisovee0d9042020-11-24 17:49:30 +02004238 return ret;
Misono Tomohirof60a2362018-04-18 11:34:52 +09004239}
4240
Chris Mason39279cc2007-06-12 06:35:45 -04004241static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4242{
David Howells2b0143b2015-03-17 22:25:59 +00004243 struct inode *inode = d_inode(dentry);
Chris Mason1832a6d2007-12-21 16:27:21 -05004244 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004245 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04004246 struct btrfs_trans_handle *trans;
Filipe Manana44f714d2016-06-06 16:11:13 +01004247 u64 last_unlink_trans;
Chris Mason39279cc2007-06-12 06:35:45 -04004248
David Sterbab3ae2442012-09-13 16:04:34 -06004249 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Yan134d4512007-10-25 15:49:25 -04004250 return -ENOTEMPTY;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004251 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
Misono Tomohiroa79a4642018-04-18 11:35:31 +09004252 return btrfs_delete_subvolume(dir, dentry);
Yan134d4512007-10-25 15:49:25 -04004253
Josef Bacikd52be812013-05-29 14:54:47 -04004254 trans = __unlink_start_trans(dir);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004255 if (IS_ERR(trans))
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004256 return PTR_ERR(trans);
Josef Bacik5df6a9f2009-11-10 21:23:48 -05004257
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02004258 if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05004259 err = btrfs_unlink_subvol(trans, dir, dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004260 goto out;
4261 }
4262
Nikolay Borisov73f2e542017-02-20 13:50:59 +02004263 err = btrfs_orphan_add(trans, BTRFS_I(inode));
Josef Bacik7b128762008-07-24 12:17:14 -04004264 if (err)
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004265 goto out;
Josef Bacik7b128762008-07-24 12:17:14 -04004266
Filipe Manana44f714d2016-06-06 16:11:13 +01004267 last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4268
Chris Mason39279cc2007-06-12 06:35:45 -04004269 /* now the directory is empty */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02004270 err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4271 BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4272 dentry->d_name.len);
Filipe Manana44f714d2016-06-06 16:11:13 +01004273 if (!err) {
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02004274 btrfs_i_size_write(BTRFS_I(inode), 0);
Filipe Manana44f714d2016-06-06 16:11:13 +01004275 /*
4276 * Propagate the last_unlink_trans value of the deleted dir to
4277 * its parent directory. This is to prevent an unrecoverable
4278 * log tree in the case we do something like this:
4279 * 1) create dir foo
4280 * 2) create snapshot under dir foo
4281 * 3) delete the snapshot
4282 * 4) rmdir foo
4283 * 5) mkdir foo
4284 * 6) fsync foo or some file inside foo
4285 */
4286 if (last_unlink_trans >= trans->transid)
4287 BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4288 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04004289out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004290 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04004291 btrfs_btree_balance_dirty(root->fs_info);
Chris Mason39544012007-12-12 14:38:19 -05004292
Chris Mason39279cc2007-06-12 06:35:45 -04004293 return err;
4294}
4295
Josef Bacikddfae632017-10-19 14:16:02 -04004296/*
4297 * Return this if we need to call truncate_block for the last bit of the
4298 * truncate.
4299 */
4300#define NEED_TRUNCATE_BLOCK 1
Filipe Manana0305cd52015-10-16 12:34:25 +01004301
Chris Mason323ac952008-10-01 19:05:46 -04004302/*
Chris Mason39279cc2007-06-12 06:35:45 -04004303 * this can truncate away extent items, csum items and directory items.
4304 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04004305 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04004306 *
4307 * csum items that cross the new i_size are truncated to the new size
4308 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04004309 *
4310 * min_type is the minimum key type to truncate down to. If set to 0, this
4311 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04004312 */
Yan, Zheng80825102009-11-12 09:35:36 +00004313int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4314 struct btrfs_root *root,
Nikolay Borisov50743392020-11-02 16:48:55 +02004315 struct btrfs_inode *inode,
Yan, Zheng80825102009-11-12 09:35:36 +00004316 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004317{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004318 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04004319 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04004320 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04004321 struct btrfs_file_extent_item *fi;
Yan, Zheng80825102009-11-12 09:35:36 +00004322 struct btrfs_key key;
4323 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04004324 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04004325 u64 extent_num_bytes = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004326 u64 extent_offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004327 u64 item_end = 0;
Filipe Mananac1aa4572015-06-20 18:20:09 +01004328 u64 last_size = new_size;
Yan, Zheng80825102009-11-12 09:35:36 +00004329 u32 found_type = (u8)-1;
Chris Mason39279cc2007-06-12 06:35:45 -04004330 int found_extent;
4331 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05004332 int pending_del_nr = 0;
4333 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04004334 int extent_type = -1;
Yan, Zheng80825102009-11-12 09:35:36 +00004335 int ret;
Nikolay Borisov50743392020-11-02 16:48:55 +02004336 u64 ino = btrfs_ino(inode);
Chris Mason28ed1342014-12-17 09:41:04 -08004337 u64 bytes_deleted = 0;
Thomas Meyer897ca812017-10-07 16:02:21 +02004338 bool be_nice = false;
4339 bool should_throttle = false;
Filipe Manana28553fa2020-02-07 12:23:09 +00004340 const u64 lock_start = ALIGN_DOWN(new_size, fs_info->sectorsize);
4341 struct extent_state *cached_state = NULL;
Yan, Zheng80825102009-11-12 09:35:36 +00004342
4343 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04004344
Chris Mason28ed1342014-12-17 09:41:04 -08004345 /*
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004346 * For non-free space inodes and non-shareable roots, we want to back
4347 * off from time to time. This means all inodes in subvolume roots,
4348 * reloc roots, and data reloc roots.
Chris Mason28ed1342014-12-17 09:41:04 -08004349 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004350 if (!btrfs_is_free_space_inode(inode) &&
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004351 test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Thomas Meyer897ca812017-10-07 16:02:21 +02004352 be_nice = true;
Chris Mason28ed1342014-12-17 09:41:04 -08004353
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004354 path = btrfs_alloc_path();
4355 if (!path)
4356 return -ENOMEM;
David Sterbae4058b52015-11-27 16:31:35 +01004357 path->reada = READA_BACK;
Mark Fasheh0eb0e192011-07-12 16:44:10 -07004358
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004359 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Nikolay Borisov50743392020-11-02 16:48:55 +02004360 lock_extent_bits(&inode->io_tree, lock_start, (u64)-1,
Filipe Mananaa5ae50d2020-02-20 13:29:49 +00004361 &cached_state);
Filipe Manana28553fa2020-02-07 12:23:09 +00004362
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004363 /*
4364 * We want to drop from the next block forward in case this
4365 * new size is not block aligned since we will be keeping the
4366 * last block of the extent just the way it is.
4367 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004368 btrfs_drop_extent_cache(inode, ALIGN(new_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004369 fs_info->sectorsize),
Jeff Mahoneyda170662016-06-15 09:22:56 -04004370 (u64)-1, 0);
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004371 }
Yan, Zheng80825102009-11-12 09:35:36 +00004372
Miao Xie16cdcec2011-04-22 18:12:22 +08004373 /*
4374 * This function is also used to drop the items in the log tree before
4375 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
Andrea Gelmini52042d82018-11-28 12:05:13 +01004376 * it is used to drop the logged items. So we shouldn't kill the delayed
Miao Xie16cdcec2011-04-22 18:12:22 +08004377 * items.
4378 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004379 if (min_type == 0 && root == inode->root)
4380 btrfs_kill_delayed_inode_items(inode);
Miao Xie16cdcec2011-04-22 18:12:22 +08004381
Li Zefan33345d012011-04-20 10:31:50 +08004382 key.objectid = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04004383 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04004384 key.type = (u8)-1;
4385
Chris Mason85e21ba2008-01-29 15:11:36 -05004386search_again:
Chris Mason28ed1342014-12-17 09:41:04 -08004387 /*
4388 * with a 16K leaf size and 128MB extents, you can actually queue
4389 * up a huge file in a single leaf. Most of the time that
4390 * bytes_deleted is > 0, it will be huge by the time we get here
4391 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004392 if (be_nice && bytes_deleted > SZ_32M &&
4393 btrfs_should_end_transaction(trans)) {
4394 ret = -EAGAIN;
Yan, Zheng80825102009-11-12 09:35:36 +00004395 goto out;
4396 }
Chris Masond3977122009-01-05 21:25:51 -05004397
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004398 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4399 if (ret < 0)
4400 goto out;
4401
Chris Mason85e21ba2008-01-29 15:11:36 -05004402 if (ret > 0) {
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004403 ret = 0;
Chris Masone02119d2008-09-05 16:13:11 -04004404 /* there are no items in the tree for us to truncate, we're
4405 * done
4406 */
Yan, Zheng80825102009-11-12 09:35:36 +00004407 if (path->slots[0] == 0)
4408 goto out;
Chris Mason85e21ba2008-01-29 15:11:36 -05004409 path->slots[0]--;
4410 }
4411
Chris Masond3977122009-01-05 21:25:51 -05004412 while (1) {
Josef Bacik9ddc9592020-01-17 09:02:22 -05004413 u64 clear_start = 0, clear_len = 0;
4414
Chris Mason39279cc2007-06-12 06:35:45 -04004415 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04004416 leaf = path->nodes[0];
4417 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
David Sterba962a2982014-06-04 18:41:45 +02004418 found_type = found_key.type;
Chris Mason39279cc2007-06-12 06:35:45 -04004419
Li Zefan33345d012011-04-20 10:31:50 +08004420 if (found_key.objectid != ino)
Chris Mason39279cc2007-06-12 06:35:45 -04004421 break;
Chris Mason5f39d392007-10-15 16:14:19 -04004422
Chris Mason85e21ba2008-01-29 15:11:36 -05004423 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04004424 break;
4425
Chris Mason5f39d392007-10-15 16:14:19 -04004426 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04004427 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04004428 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04004429 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04004430 extent_type = btrfs_file_extent_type(leaf, fi);
4431 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04004432 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04004433 btrfs_file_extent_num_bytes(leaf, fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004434
4435 trace_btrfs_truncate_show_fi_regular(
Nikolay Borisov50743392020-11-02 16:48:55 +02004436 inode, leaf, fi, found_key.offset);
Chris Mason179e29e2007-11-01 11:28:41 -04004437 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Qu Wenruoe41ca582018-06-06 15:41:49 +08004438 item_end += btrfs_file_extent_ram_bytes(leaf,
4439 fi);
Liu Bo09ed2f12017-03-10 11:09:48 -08004440
4441 trace_btrfs_truncate_show_fi_inline(
Nikolay Borisov50743392020-11-02 16:48:55 +02004442 inode, leaf, fi, path->slots[0],
Liu Bo09ed2f12017-03-10 11:09:48 -08004443 found_key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -04004444 }
Yan008630c2007-11-07 13:31:09 -05004445 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04004446 }
Yan, Zheng80825102009-11-12 09:35:36 +00004447 if (found_type > min_type) {
Chris Mason39279cc2007-06-12 06:35:45 -04004448 del_item = 1;
Yan, Zheng80825102009-11-12 09:35:36 +00004449 } else {
Filipe Manana76b42ab2017-02-14 16:56:01 +00004450 if (item_end < new_size)
Yan, Zheng80825102009-11-12 09:35:36 +00004451 break;
4452 if (found_key.offset >= new_size)
4453 del_item = 1;
4454 else
4455 del_item = 0;
4456 }
Chris Mason39279cc2007-06-12 06:35:45 -04004457 found_extent = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04004458 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04004459 if (found_type != BTRFS_EXTENT_DATA_KEY)
4460 goto delete;
4461
4462 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04004463 u64 num_dec;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004464
4465 clear_start = found_key.offset;
Chris Masondb945352007-10-15 16:15:53 -04004466 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Miao Xief70a9a6b2012-01-12 19:10:12 -05004467 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04004468 u64 orig_num_bytes =
4469 btrfs_file_extent_num_bytes(leaf, fi);
Qu Wenruofda28322013-02-26 08:10:22 +00004470 extent_num_bytes = ALIGN(new_size -
4471 found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004472 fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004473 clear_start = ALIGN(new_size, fs_info->sectorsize);
Chris Masondb945352007-10-15 16:15:53 -04004474 btrfs_set_file_extent_num_bytes(leaf, fi,
4475 extent_num_bytes);
4476 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05004477 extent_num_bytes);
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004478 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004479 &root->state) &&
4480 extent_start != 0)
Nikolay Borisov50743392020-11-02 16:48:55 +02004481 inode_sub_bytes(&inode->vfs_inode,
4482 num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04004483 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04004484 } else {
Chris Masondb945352007-10-15 16:15:53 -04004485 extent_num_bytes =
4486 btrfs_file_extent_disk_num_bytes(leaf,
4487 fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04004488 extent_offset = found_key.offset -
4489 btrfs_file_extent_offset(leaf, fi);
4490
Chris Mason39279cc2007-06-12 06:35:45 -04004491 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05004492 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04004493 if (extent_start != 0) {
4494 found_extent = 1;
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004495 if (test_bit(BTRFS_ROOT_SHAREABLE,
Miao Xie27cdeb72014-04-02 19:51:05 +08004496 &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004497 inode_sub_bytes(&inode->vfs_inode,
4498 num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04004499 }
4500 }
Josef Bacik9ddc9592020-01-17 09:02:22 -05004501 clear_len = num_dec;
Chris Mason90692182008-02-08 13:49:28 -05004502 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Masonc8b97812008-10-29 14:49:59 -04004503 /*
4504 * we can't truncate inline items that have had
4505 * special encodings
4506 */
4507 if (!del_item &&
Chris Masonc8b97812008-10-29 14:49:59 -04004508 btrfs_file_extent_encryption(leaf, fi) == 0 &&
Josef Bacikddfae632017-10-19 14:16:02 -04004509 btrfs_file_extent_other_encoding(leaf, fi) == 0 &&
4510 btrfs_file_extent_compression(leaf, fi) == 0) {
4511 u32 size = (u32)(new_size - found_key.offset);
Chris Mason514ac8a2014-01-03 21:07:00 -08004512
Josef Bacikddfae632017-10-19 14:16:02 -04004513 btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4514 size = btrfs_file_extent_calc_inline_size(size);
David Sterba78ac4f92019-03-20 14:49:12 +01004515 btrfs_truncate_item(path, size, 1);
Josef Bacikddfae632017-10-19 14:16:02 -04004516 } else if (!del_item) {
Chris Mason514ac8a2014-01-03 21:07:00 -08004517 /*
Josef Bacikddfae632017-10-19 14:16:02 -04004518 * We have to bail so the last_size is set to
4519 * just before this extent.
Chris Mason514ac8a2014-01-03 21:07:00 -08004520 */
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004521 ret = NEED_TRUNCATE_BLOCK;
Josef Bacikddfae632017-10-19 14:16:02 -04004522 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004523 } else {
4524 /*
4525 * Inline extents are special, we just treat
4526 * them as a full sector worth in the file
4527 * extent tree just for simplicity sake.
4528 */
4529 clear_len = fs_info->sectorsize;
Chris Mason90692182008-02-08 13:49:28 -05004530 }
Josef Bacikddfae632017-10-19 14:16:02 -04004531
Qu Wenruo92a7cc42020-05-15 14:01:40 +08004532 if (test_bit(BTRFS_ROOT_SHAREABLE, &root->state))
Nikolay Borisov50743392020-11-02 16:48:55 +02004533 inode_sub_bytes(&inode->vfs_inode,
4534 item_end + 1 - new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04004535 }
Chris Mason179e29e2007-11-01 11:28:41 -04004536delete:
Josef Bacik9ddc9592020-01-17 09:02:22 -05004537 /*
4538 * We use btrfs_truncate_inode_items() to clean up log trees for
4539 * multiple fsyncs, and in this case we don't want to clear the
4540 * file extent range because it's just the log.
4541 */
Nikolay Borisov50743392020-11-02 16:48:55 +02004542 if (root == inode->root) {
4543 ret = btrfs_inode_clear_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004544 clear_start, clear_len);
4545 if (ret) {
4546 btrfs_abort_transaction(trans, ret);
4547 break;
4548 }
4549 }
4550
Josef Bacikddfae632017-10-19 14:16:02 -04004551 if (del_item)
4552 last_size = found_key.offset;
4553 else
4554 last_size = new_size;
Chris Mason39279cc2007-06-12 06:35:45 -04004555 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05004556 if (!pending_del_nr) {
4557 /* no pending yet, add ourselves */
4558 pending_del_slot = path->slots[0];
4559 pending_del_nr = 1;
4560 } else if (pending_del_nr &&
4561 path->slots[0] + 1 == pending_del_slot) {
4562 /* hop on the pending chunk */
4563 pending_del_nr++;
4564 pending_del_slot = path->slots[0];
4565 } else {
Chris Masond3977122009-01-05 21:25:51 -05004566 BUG();
Chris Mason85e21ba2008-01-29 15:11:36 -05004567 }
Chris Mason39279cc2007-06-12 06:35:45 -04004568 } else {
4569 break;
4570 }
Thomas Meyer897ca812017-10-07 16:02:21 +02004571 should_throttle = false;
Chris Mason28f75a02015-02-04 06:59:29 -08004572
Miao Xie27cdeb72014-04-02 19:51:05 +08004573 if (found_extent &&
Qu Wenruo82028e0a2020-05-15 14:01:41 +08004574 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004575 struct btrfs_ref ref = { 0 };
4576
Chris Mason28ed1342014-12-17 09:41:04 -08004577 bytes_deleted += extent_num_bytes;
Qu Wenruoffd4bb22019-04-04 14:45:36 +08004578
4579 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF,
4580 extent_start, extent_num_bytes, 0);
4581 ref.real_root = root->root_key.objectid;
4582 btrfs_init_data_ref(&ref, btrfs_header_owner(leaf),
4583 ino, extent_offset);
4584 ret = btrfs_free_extent(trans, &ref);
Omar Sandoval05522102018-05-11 13:13:31 -07004585 if (ret) {
4586 btrfs_abort_transaction(trans, ret);
4587 break;
4588 }
Chris Mason28f75a02015-02-04 06:59:29 -08004589 if (be_nice) {
Lu Fengqi7c861622018-10-11 13:40:36 +08004590 if (btrfs_should_throttle_delayed_refs(trans))
Thomas Meyer897ca812017-10-07 16:02:21 +02004591 should_throttle = true;
Chris Mason28f75a02015-02-04 06:59:29 -08004592 }
Chris Mason39279cc2007-06-12 06:35:45 -04004593 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004594
Yan, Zheng80825102009-11-12 09:35:36 +00004595 if (found_type == BTRFS_INODE_ITEM_KEY)
4596 break;
4597
4598 if (path->slots[0] == 0 ||
Josef Bacik12621332015-02-03 07:50:16 -08004599 path->slots[0] != pending_del_slot ||
Josef Bacik28bad212018-12-03 10:20:38 -05004600 should_throttle) {
Yan, Zheng80825102009-11-12 09:35:36 +00004601 if (pending_del_nr) {
4602 ret = btrfs_del_items(trans, root, path,
4603 pending_del_slot,
4604 pending_del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004605 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004606 btrfs_abort_transaction(trans, ret);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004607 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004608 }
Yan, Zheng80825102009-11-12 09:35:36 +00004609 pending_del_nr = 0;
4610 }
David Sterbab3b4aa72011-04-21 01:20:15 +02004611 btrfs_release_path(path);
Josef Bacik28bad212018-12-03 10:20:38 -05004612
Chris Mason28f75a02015-02-04 06:59:29 -08004613 /*
Josef Bacik28bad212018-12-03 10:20:38 -05004614 * We can generate a lot of delayed refs, so we need to
4615 * throttle every once and a while and make sure we're
4616 * adding enough space to keep up with the work we are
4617 * generating. Since we hold a transaction here we
4618 * can't flush, and we don't want to FLUSH_LIMIT because
4619 * we could have generated too many delayed refs to
4620 * actually allocate, so just bail if we're short and
4621 * let the normal reservation dance happen higher up.
Chris Mason28f75a02015-02-04 06:59:29 -08004622 */
Josef Bacik28bad212018-12-03 10:20:38 -05004623 if (should_throttle) {
4624 ret = btrfs_delayed_refs_rsv_refill(fs_info,
4625 BTRFS_RESERVE_NO_FLUSH);
4626 if (ret) {
4627 ret = -EAGAIN;
4628 break;
4629 }
Chris Mason28f75a02015-02-04 06:59:29 -08004630 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004631 goto search_again;
Yan, Zheng80825102009-11-12 09:35:36 +00004632 } else {
4633 path->slots[0]--;
Chris Mason85e21ba2008-01-29 15:11:36 -05004634 }
Chris Mason39279cc2007-06-12 06:35:45 -04004635 }
Yan, Zheng80825102009-11-12 09:35:36 +00004636out:
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004637 if (ret >= 0 && pending_del_nr) {
4638 int err;
4639
4640 err = btrfs_del_items(trans, root, path, pending_del_slot,
Chris Mason85e21ba2008-01-29 15:11:36 -05004641 pending_del_nr);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004642 if (err) {
4643 btrfs_abort_transaction(trans, err);
4644 ret = err;
4645 }
Chris Mason85e21ba2008-01-29 15:11:36 -05004646 }
Filipe Manana76b42ab2017-02-14 16:56:01 +00004647 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4648 ASSERT(last_size >= new_size);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004649 if (!ret && last_size > new_size)
Filipe Manana76b42ab2017-02-14 16:56:01 +00004650 last_size = new_size;
Nikolay Borisov50743392020-11-02 16:48:55 +02004651 btrfs_inode_safe_disk_i_size_write(inode, last_size);
4652 unlock_extent_cached(&inode->io_tree, lock_start, (u64)-1,
4653 &cached_state);
Filipe Manana76b42ab2017-02-14 16:56:01 +00004654 }
Chris Mason28ed1342014-12-17 09:41:04 -08004655
Chris Mason39279cc2007-06-12 06:35:45 -04004656 btrfs_free_path(path);
Omar Sandovalfd86a3a2018-05-11 13:13:30 -07004657 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04004658}
4659
Chris Masona52d9a82007-08-27 16:49:44 -04004660/*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304661 * btrfs_truncate_block - read, zero a chunk and write a block
Josef Bacik2aaa6652012-08-29 14:27:18 -04004662 * @inode - inode that we're zeroing
4663 * @from - the offset to start zeroing
4664 * @len - the length to zero, 0 to zero the entire range respective to the
4665 * offset
4666 * @front - zero up to the offset instead of from the offset on
4667 *
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304668 * This will find the block for the "from" offset and cow the block and zero the
Josef Bacik2aaa6652012-08-29 14:27:18 -04004669 * part we want to zero. This is used with truncate and hole punching.
Chris Masona52d9a82007-08-27 16:49:44 -04004670 */
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004671int btrfs_truncate_block(struct btrfs_inode *inode, loff_t from, loff_t len,
4672 int front)
Chris Masona52d9a82007-08-27 16:49:44 -04004673{
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004674 struct btrfs_fs_info *fs_info = inode->root->fs_info;
4675 struct address_space *mapping = inode->vfs_inode.i_mapping;
4676 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004677 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004678 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08004679 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004680 char *kaddr;
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004681 bool only_release_metadata = false;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004682 u32 blocksize = fs_info->sectorsize;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004683 pgoff_t index = from >> PAGE_SHIFT;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304684 unsigned offset = from & (blocksize - 1);
Chris Masona52d9a82007-08-27 16:49:44 -04004685 struct page *page;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004686 gfp_t mask = btrfs_alloc_write_mask(mapping);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004687 size_t write_bytes = blocksize;
Chris Masona52d9a82007-08-27 16:49:44 -04004688 int ret = 0;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304689 u64 block_start;
4690 u64 block_end;
Chris Masona52d9a82007-08-27 16:49:44 -04004691
Nikolay Borisovb03ebd92018-01-18 14:47:06 +02004692 if (IS_ALIGNED(offset, blocksize) &&
4693 (!len || IS_ALIGNED(len, blocksize)))
Chris Masona52d9a82007-08-27 16:49:44 -04004694 goto out;
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304695
Josef Bacik8b62f872017-10-19 14:15:55 -04004696 block_start = round_down(from, blocksize);
4697 block_end = block_start + blocksize - 1;
4698
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004699 ret = btrfs_check_data_free_space(inode, &data_reserved, block_start,
4700 blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004701 if (ret < 0) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004702 if (btrfs_check_nocow_lock(inode, block_start, &write_bytes) > 0) {
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004703 /* For nocow case, no need to reserve data space */
4704 only_release_metadata = true;
4705 } else {
4706 goto out;
4707 }
4708 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004709 ret = btrfs_delalloc_reserve_metadata(inode, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004710 if (ret < 0) {
4711 if (!only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004712 btrfs_free_reserved_data_space(inode, data_reserved,
4713 block_start, blocksize);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004714 goto out;
4715 }
Chris Mason211c17f2008-05-15 09:13:45 -04004716again:
Josef Bacik3b16a4e2011-09-21 15:05:58 -04004717 page = find_or_create_page(mapping, index, mask);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004718 if (!page) {
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004719 btrfs_delalloc_release_space(inode, data_reserved, block_start,
4720 blocksize, true);
4721 btrfs_delalloc_release_extents(inode, blocksize);
Miao Xieac6a2b32012-12-05 10:56:13 +00004722 ret = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -04004723 goto out;
Josef Bacik5d5e1032009-10-13 16:46:49 -04004724 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04004725
Chris Masona52d9a82007-08-27 16:49:44 -04004726 if (!PageUptodate(page)) {
4727 ret = btrfs_readpage(NULL, page);
4728 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004729 if (page->mapping != mapping) {
4730 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004731 put_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04004732 goto again;
4733 }
Chris Masona52d9a82007-08-27 16:49:44 -04004734 if (!PageUptodate(page)) {
4735 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04004736 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04004737 }
4738 }
Chris Mason211c17f2008-05-15 09:13:45 -04004739 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004740
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304741 lock_extent_bits(io_tree, block_start, block_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004742 set_page_extent_mapped(page);
4743
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004744 ordered = btrfs_lookup_ordered_extent(inode, block_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004745 if (ordered) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304746 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004747 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004748 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004749 put_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03004750 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004751 btrfs_put_ordered_extent(ordered);
4752 goto again;
4753 }
4754
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004755 clear_extent_bit(&inode->io_tree, block_start, block_end,
Omar Sandovale1821632019-08-15 14:04:04 -07004756 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4757 0, 0, &cached_state);
Josef Bacik5d5e1032009-10-13 16:46:49 -04004758
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004759 ret = btrfs_set_extent_delalloc(inode, block_start, block_end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03004760 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004761 if (ret) {
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304762 unlock_extent_cached(io_tree, block_start, block_end,
David Sterbae43bbe52017-12-12 21:43:52 +01004763 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04004764 goto out_unlock;
4765 }
4766
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304767 if (offset != blocksize) {
Josef Bacik2aaa6652012-08-29 14:27:18 -04004768 if (!len)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304769 len = blocksize - offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -04004770 kaddr = kmap(page);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004771 if (front)
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304772 memset(kaddr + (block_start - page_offset(page)),
4773 0, offset);
Josef Bacik2aaa6652012-08-29 14:27:18 -04004774 else
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304775 memset(kaddr + (block_start - page_offset(page)) + offset,
4776 0, len);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004777 flush_dcache_page(page);
4778 kunmap(page);
4779 }
Chris Mason247e7432008-07-17 12:53:51 -04004780 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04004781 set_page_dirty(page);
David Sterbae43bbe52017-12-12 21:43:52 +01004782 unlock_extent_cached(io_tree, block_start, block_end, &cached_state);
Chris Mason39279cc2007-06-12 06:35:45 -04004783
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004784 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004785 set_extent_bit(&inode->io_tree, block_start, block_end,
Nikolay Borisov1cab5e72020-11-05 11:08:00 +02004786 EXTENT_NORESERVE, 0, NULL, NULL, GFP_NOFS, NULL);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004787
Chris Mason89642222008-07-24 09:41:53 -04004788out_unlock:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004789 if (ret) {
4790 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004791 btrfs_delalloc_release_metadata(inode, blocksize, true);
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004792 else
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004793 btrfs_delalloc_release_space(inode, data_reserved,
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004794 block_start, blocksize, true);
4795 }
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004796 btrfs_delalloc_release_extents(inode, blocksize);
Chris Mason39279cc2007-06-12 06:35:45 -04004797 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03004798 put_page(page);
Chris Mason39279cc2007-06-12 06:35:45 -04004799out:
Qu Wenruo6d4572a2020-06-24 07:23:50 +08004800 if (only_release_metadata)
Nikolay Borisov217f42e2020-11-02 16:49:03 +02004801 btrfs_check_nocow_unlock(inode);
Qu Wenruo364ecf32017-02-27 15:10:38 +08004802 extent_changeset_free(data_reserved);
Chris Mason39279cc2007-06-12 06:35:45 -04004803 return ret;
4804}
4805
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004806static int maybe_insert_hole(struct btrfs_root *root, struct btrfs_inode *inode,
Josef Bacik16e75492013-10-22 12:18:51 -04004807 u64 offset, u64 len)
4808{
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004809 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik16e75492013-10-22 12:18:51 -04004810 struct btrfs_trans_handle *trans;
Filipe Manana5893dfb2020-11-04 11:07:32 +00004811 struct btrfs_drop_extents_args drop_args = { 0 };
Josef Bacik16e75492013-10-22 12:18:51 -04004812 int ret;
4813
4814 /*
4815 * Still need to make sure the inode looks like it's been updated so
4816 * that any holes get logged if we fsync.
4817 */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004818 if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004819 inode->last_trans = fs_info->generation;
4820 inode->last_sub_trans = root->log_transid;
4821 inode->last_log_commit = root->last_log_commit;
Josef Bacik16e75492013-10-22 12:18:51 -04004822 return 0;
4823 }
4824
4825 /*
4826 * 1 - for the one we're dropping
4827 * 1 - for the one we're adding
4828 * 1 - for updating the inode.
4829 */
4830 trans = btrfs_start_transaction(root, 3);
4831 if (IS_ERR(trans))
4832 return PTR_ERR(trans);
4833
Filipe Manana5893dfb2020-11-04 11:07:32 +00004834 drop_args.start = offset;
4835 drop_args.end = offset + len;
4836 drop_args.drop_cache = true;
4837
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004838 ret = btrfs_drop_extents(trans, root, inode, &drop_args);
Josef Bacik16e75492013-10-22 12:18:51 -04004839 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004840 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004841 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004842 return ret;
4843 }
4844
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004845 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode),
David Sterbaf85b7372017-01-20 14:54:07 +01004846 offset, 0, 0, len, 0, len, 0, 0, 0);
Filipe Manana2766ff62020-11-04 11:07:34 +00004847 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04004848 btrfs_abort_transaction(trans, ret);
Filipe Manana2766ff62020-11-04 11:07:34 +00004849 } else {
Nikolay Borisova4ba6cc2020-11-02 16:49:00 +02004850 btrfs_update_inode_bytes(inode, 0, drop_args.bytes_found);
4851 btrfs_update_inode(trans, root, inode);
Filipe Manana2766ff62020-11-04 11:07:34 +00004852 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04004853 btrfs_end_transaction(trans);
Josef Bacik16e75492013-10-22 12:18:51 -04004854 return ret;
4855}
4856
Josef Bacik695a0d02011-03-04 15:46:53 -05004857/*
4858 * This function puts in dummy file extents for the area we're creating a hole
4859 * for. So if we are truncating this file to a larger size we need to insert
4860 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4861 * the range between oldsize and size
4862 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004863int btrfs_cont_expand(struct btrfs_inode *inode, loff_t oldsize, loff_t size)
Yan Zheng9036c102008-10-30 14:19:41 -04004864{
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004865 struct btrfs_root *root = inode->root;
4866 struct btrfs_fs_info *fs_info = root->fs_info;
4867 struct extent_io_tree *io_tree = &inode->io_tree;
Yan, Zhenga22285a2010-05-16 10:48:46 -04004868 struct extent_map *em = NULL;
Josef Bacik2ac55d42010-02-03 19:33:23 +00004869 struct extent_state *cached_state = NULL;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004870 struct extent_map_tree *em_tree = &inode->extent_tree;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004871 u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4872 u64 block_end = ALIGN(size, fs_info->sectorsize);
Yan Zheng9036c102008-10-30 14:19:41 -04004873 u64 last_byte;
4874 u64 cur_offset;
4875 u64 hole_size;
Josef Bacik9ed74f22009-09-11 16:12:44 -04004876 int err = 0;
Yan Zheng9036c102008-10-30 14:19:41 -04004877
Josef Bacika71754f2013-06-17 17:14:39 -04004878 /*
Chandan Rajendra9703fef2016-01-21 15:55:56 +05304879 * If our size started in the middle of a block we need to zero out the
4880 * rest of the block before we expand the i_size, otherwise we could
Josef Bacika71754f2013-06-17 17:14:39 -04004881 * expose stale data.
4882 */
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004883 err = btrfs_truncate_block(inode, oldsize, 0, 0);
Josef Bacika71754f2013-06-17 17:14:39 -04004884 if (err)
4885 return err;
4886
Yan Zheng9036c102008-10-30 14:19:41 -04004887 if (size <= hole_start)
4888 return 0;
4889
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004890 btrfs_lock_and_flush_ordered_range(inode, hole_start, block_end - 1,
4891 &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004892 cur_offset = hole_start;
4893 while (1) {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004894 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
Omar Sandoval39b07b52019-12-02 17:34:23 -08004895 block_end - cur_offset);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004896 if (IS_ERR(em)) {
4897 err = PTR_ERR(em);
Zach Brownf2767952013-01-08 19:37:58 +00004898 em = NULL;
Jeff Mahoney79787ea2012-03-12 16:03:00 +01004899 break;
4900 }
Yan Zheng9036c102008-10-30 14:19:41 -04004901 last_byte = min(extent_map_end(em), block_end);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004902 last_byte = ALIGN(last_byte, fs_info->sectorsize);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004903 hole_size = last_byte - cur_offset;
4904
Yan, Zheng80825102009-11-12 09:35:36 +00004905 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
Josef Bacik5dc562c2012-08-17 13:14:17 -04004906 struct extent_map *hole_em;
Yan, Zheng80825102009-11-12 09:35:36 +00004907
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004908 err = maybe_insert_hole(root, inode, cur_offset,
4909 hole_size);
Josef Bacik16e75492013-10-22 12:18:51 -04004910 if (err)
Chris Mason771ed682008-11-06 22:02:51 -05004911 break;
Josef Bacik9ddc9592020-01-17 09:02:22 -05004912
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004913 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004914 cur_offset, hole_size);
4915 if (err)
4916 break;
4917
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004918 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004919 cur_offset + hole_size - 1, 0);
4920 hole_em = alloc_extent_map();
4921 if (!hole_em) {
4922 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004923 &inode->runtime_flags);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004924 goto next;
4925 }
4926 hole_em->start = cur_offset;
4927 hole_em->len = hole_size;
4928 hole_em->orig_start = cur_offset;
Yan, Zheng80825102009-11-12 09:35:36 +00004929
Josef Bacik5dc562c2012-08-17 13:14:17 -04004930 hole_em->block_start = EXTENT_MAP_HOLE;
4931 hole_em->block_len = 0;
Josef Bacikb4939682012-12-03 10:31:19 -05004932 hole_em->orig_block_len = 0;
Josef Bacikcc95bef2013-04-04 14:31:27 -04004933 hole_em->ram_bytes = hole_size;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004934 hole_em->compress_type = BTRFS_COMPRESS_NONE;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04004935 hole_em->generation = fs_info->generation;
Josef Bacik5dc562c2012-08-17 13:14:17 -04004936
4937 while (1) {
4938 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04004939 err = add_extent_mapping(em_tree, hole_em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04004940 write_unlock(&em_tree->lock);
4941 if (err != -EEXIST)
4942 break;
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004943 btrfs_drop_extent_cache(inode, cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04004944 cur_offset +
4945 hole_size - 1, 0);
4946 }
4947 free_extent_map(hole_em);
Josef Bacik9ddc9592020-01-17 09:02:22 -05004948 } else {
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004949 err = btrfs_inode_set_file_extent_range(inode,
Josef Bacik9ddc9592020-01-17 09:02:22 -05004950 cur_offset, hole_size);
4951 if (err)
4952 break;
Yan Zheng9036c102008-10-30 14:19:41 -04004953 }
Josef Bacik16e75492013-10-22 12:18:51 -04004954next:
Yan Zheng9036c102008-10-30 14:19:41 -04004955 free_extent_map(em);
Yan, Zhenga22285a2010-05-16 10:48:46 -04004956 em = NULL;
Yan Zheng9036c102008-10-30 14:19:41 -04004957 cur_offset = last_byte;
Yan, Zheng80825102009-11-12 09:35:36 +00004958 if (cur_offset >= block_end)
Yan Zheng9036c102008-10-30 14:19:41 -04004959 break;
4960 }
Yan, Zhenga22285a2010-05-16 10:48:46 -04004961 free_extent_map(em);
David Sterbae43bbe52017-12-12 21:43:52 +01004962 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state);
Yan Zheng9036c102008-10-30 14:19:41 -04004963 return err;
4964}
4965
Eric Sandeen3972f262013-01-12 02:57:22 +00004966static int btrfs_setsize(struct inode *inode, struct iattr *attr)
Yan, Zheng80825102009-11-12 09:35:36 +00004967{
Miao Xief4a2f4c2011-12-14 20:12:01 -05004968 struct btrfs_root *root = BTRFS_I(inode)->root;
4969 struct btrfs_trans_handle *trans;
Josef Bacika41ad392011-01-31 15:30:16 -05004970 loff_t oldsize = i_size_read(inode);
Eric Sandeen3972f262013-01-12 02:57:22 +00004971 loff_t newsize = attr->ia_size;
4972 int mask = attr->ia_valid;
Yan, Zheng80825102009-11-12 09:35:36 +00004973 int ret;
4974
Eric Sandeen3972f262013-01-12 02:57:22 +00004975 /*
4976 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4977 * special case where we need to update the times despite not having
4978 * these flags set. For all other operations the VFS set these flags
4979 * explicitly if it wants a timestamp update.
4980 */
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004981 if (newsize != oldsize) {
4982 inode_inc_iversion(inode);
4983 if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4984 inode->i_ctime = inode->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07004985 current_time(inode);
Christoph Hellwigdff6efc2013-11-19 07:17:07 -08004986 }
Eric Sandeen3972f262013-01-12 02:57:22 +00004987
Josef Bacika41ad392011-01-31 15:30:16 -05004988 if (newsize > oldsize) {
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004989 /*
David Sterbaea14b57f2017-06-22 02:19:11 +02004990 * Don't do an expanding truncate while snapshotting is ongoing.
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004991 * This is to ensure the snapshot captures a fully consistent
4992 * state of this file - if the snapshot captures this expanding
4993 * truncation, it must capture all writes that happened before
4994 * this truncation.
4995 */
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004996 btrfs_drew_write_lock(&root->snapshot_lock);
Nikolay Borisovb06359a2020-11-02 16:49:04 +02004997 ret = btrfs_cont_expand(BTRFS_I(inode), oldsize, newsize);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00004998 if (ret) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02004999 btrfs_drew_write_unlock(&root->snapshot_lock);
Yan, Zheng80825102009-11-12 09:35:36 +00005000 return ret;
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005001 }
Yan, Zheng80825102009-11-12 09:35:36 +00005002
Miao Xief4a2f4c2011-12-14 20:12:01 -05005003 trans = btrfs_start_transaction(root, 1);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005004 if (IS_ERR(trans)) {
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005005 btrfs_drew_write_unlock(&root->snapshot_lock);
Miao Xief4a2f4c2011-12-14 20:12:01 -05005006 return PTR_ERR(trans);
Filipe Manana9ea24bb2014-10-29 11:57:59 +00005007 }
Miao Xief4a2f4c2011-12-14 20:12:01 -05005008
5009 i_size_write(inode, newsize);
Nikolay Borisov76aea532020-11-02 16:48:53 +02005010 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Chandan Rajendra27772b62016-01-21 15:56:03 +05305011 pagecache_isize_extended(inode, oldsize, newsize);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005012 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Nikolay Borisovdcc3eb92020-01-30 14:59:45 +02005013 btrfs_drew_write_unlock(&root->snapshot_lock);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005014 btrfs_end_transaction(trans);
Josef Bacika41ad392011-01-31 15:30:16 -05005015 } else {
Yan, Zheng80825102009-11-12 09:35:36 +00005016
Josef Bacika41ad392011-01-31 15:30:16 -05005017 /*
5018 * We're truncating a file that used to have good data down to
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005019 * zero. Make sure any new writes to the file get on disk
5020 * on close.
Josef Bacika41ad392011-01-31 15:30:16 -05005021 */
5022 if (newsize == 0)
Nikolay Borisov1fd40332020-10-01 09:40:39 +03005023 set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
Josef Bacik72ac3c02012-05-23 14:13:11 -04005024 &BTRFS_I(inode)->runtime_flags);
Yan, Zheng80825102009-11-12 09:35:36 +00005025
Josef Bacika41ad392011-01-31 15:30:16 -05005026 truncate_setsize(inode, newsize);
Miao Xie2e60a512013-02-08 07:01:08 +00005027
Miao Xie2e60a512013-02-08 07:01:08 +00005028 inode_dio_wait(inode);
Miao Xie2e60a512013-02-08 07:01:08 +00005029
Filipe Manana213e8c52018-02-06 20:40:31 +00005030 ret = btrfs_truncate(inode, newsize == oldsize);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005031 if (ret && inode->i_nlink) {
5032 int err;
5033
5034 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005035 * Truncate failed, so fix up the in-memory size. We
5036 * adjusted disk_i_size down as we removed extents, so
5037 * wait for disk_i_size to be stable and then update the
5038 * in-memory size to match.
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005039 */
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005040 err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005041 if (err)
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07005042 return err;
5043 i_size_write(inode, BTRFS_I(inode)->disk_i_size);
Josef Bacik7f4f6e02013-08-29 16:43:28 -04005044 }
Yan, Zheng80825102009-11-12 09:35:36 +00005045 }
5046
Josef Bacika41ad392011-01-31 15:30:16 -05005047 return ret;
Yan, Zheng80825102009-11-12 09:35:36 +00005048}
5049
Chris Mason39279cc2007-06-12 06:35:45 -04005050static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5051{
David Howells2b0143b2015-03-17 22:25:59 +00005052 struct inode *inode = d_inode(dentry);
Li Zefanb83cc962010-12-20 16:04:08 +08005053 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005054 int err;
5055
Li Zefanb83cc962010-12-20 16:04:08 +08005056 if (btrfs_root_readonly(root))
5057 return -EROFS;
5058
Jan Kara31051c82016-05-26 16:55:18 +02005059 err = setattr_prepare(dentry, attr);
Chris Mason39279cc2007-06-12 06:35:45 -04005060 if (err)
5061 return err;
5062
Chris Mason5a3f23d2009-03-31 13:27:11 -04005063 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
Eric Sandeen3972f262013-01-12 02:57:22 +00005064 err = btrfs_setsize(inode, attr);
Yan, Zheng80825102009-11-12 09:35:36 +00005065 if (err)
5066 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005067 }
Yan Zheng9036c102008-10-30 14:19:41 -04005068
Christoph Hellwig10257742010-06-04 11:30:02 +02005069 if (attr->ia_valid) {
5070 setattr_copy(inode, attr);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04005071 inode_inc_iversion(inode);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005072 err = btrfs_dirty_inode(inode);
Josef Bacik33268ea2008-07-24 12:16:36 -04005073
Josef Bacik22c44fe2011-11-30 10:45:38 -05005074 if (!err && attr->ia_valid & ATTR_MODE)
Christoph Hellwig996a7102013-12-20 05:16:43 -08005075 err = posix_acl_chmod(inode, inode->i_mode);
Christoph Hellwig10257742010-06-04 11:30:02 +02005076 }
5077
Chris Mason39279cc2007-06-12 06:35:45 -04005078 return err;
5079}
Chris Mason61295eb2008-01-14 16:24:38 -05005080
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005081/*
5082 * While truncating the inode pages during eviction, we get the VFS calling
5083 * btrfs_invalidatepage() against each page of the inode. This is slow because
5084 * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5085 * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5086 * extent_state structures over and over, wasting lots of time.
5087 *
5088 * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5089 * those expensive operations on a per page basis and do only the ordered io
5090 * finishing, while we release here the extent_map and extent_state structures,
5091 * without the excessive merging and splitting.
5092 */
5093static void evict_inode_truncate_pages(struct inode *inode)
5094{
5095 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5096 struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5097 struct rb_node *node;
5098
5099 ASSERT(inode->i_state & I_FREEING);
Johannes Weiner91b0abe2014-04-03 14:47:49 -07005100 truncate_inode_pages_final(&inode->i_data);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005101
5102 write_lock(&map_tree->lock);
Liu Bo07e1ce02018-08-23 03:51:52 +08005103 while (!RB_EMPTY_ROOT(&map_tree->map.rb_root)) {
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005104 struct extent_map *em;
5105
Liu Bo07e1ce02018-08-23 03:51:52 +08005106 node = rb_first_cached(&map_tree->map);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005107 em = rb_entry(node, struct extent_map, rb_node);
Wang Shilong180589e2013-12-14 15:27:31 +08005108 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5109 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005110 remove_extent_mapping(map_tree, em);
5111 free_extent_map(em);
Filipe Manana7064dd52014-08-08 02:47:05 +01005112 if (need_resched()) {
5113 write_unlock(&map_tree->lock);
5114 cond_resched();
5115 write_lock(&map_tree->lock);
5116 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005117 }
5118 write_unlock(&map_tree->lock);
5119
Filipe Manana6ca07092015-05-26 00:55:42 +01005120 /*
5121 * Keep looping until we have no more ranges in the io tree.
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07005122 * We can have ongoing bios started by readahead that have
5123 * their endio callback (extent_io.c:end_bio_extent_readpage)
Filipe Manana9c6429d2015-06-10 12:55:41 +01005124 * still in progress (unlocked the pages in the bio but did not yet
5125 * unlocked the ranges in the io tree). Therefore this means some
Filipe Manana6ca07092015-05-26 00:55:42 +01005126 * ranges can still be locked and eviction started because before
5127 * submitting those bios, which are executed by a separate task (work
5128 * queue kthread), inode references (inode->i_count) were not taken
5129 * (which would be dropped in the end io callback of each bio).
5130 * Therefore here we effectively end up waiting for those bios and
5131 * anyone else holding locked ranges without having bumped the inode's
5132 * reference count - if we don't do it, when they access the inode's
5133 * io_tree to unlock a range it may be too late, leading to an
5134 * use-after-free issue.
5135 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005136 spin_lock(&io_tree->lock);
5137 while (!RB_EMPTY_ROOT(&io_tree->state)) {
5138 struct extent_state *state;
5139 struct extent_state *cached_state = NULL;
Filipe Manana6ca07092015-05-26 00:55:42 +01005140 u64 start;
5141 u64 end;
Filipe Manana421f0922018-10-12 13:02:48 +01005142 unsigned state_flags;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005143
5144 node = rb_first(&io_tree->state);
5145 state = rb_entry(node, struct extent_state, rb_node);
Filipe Manana6ca07092015-05-26 00:55:42 +01005146 start = state->start;
5147 end = state->end;
Filipe Manana421f0922018-10-12 13:02:48 +01005148 state_flags = state->state;
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005149 spin_unlock(&io_tree->lock);
5150
David Sterbaff13db42015-12-03 14:30:40 +01005151 lock_extent_bits(io_tree, start, end, &cached_state);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005152
5153 /*
5154 * If still has DELALLOC flag, the extent didn't reach disk,
5155 * and its reserved space won't be freed by delayed_ref.
5156 * So we need to free its reserved space here.
5157 * (Refer to comment in btrfs_invalidatepage, case 2)
5158 *
5159 * Note, end is the bytenr of last byte, so we need + 1 here.
5160 */
Filipe Manana421f0922018-10-12 13:02:48 +01005161 if (state_flags & EXTENT_DELALLOC)
Nikolay Borisov8b8a9792020-06-03 08:55:11 +03005162 btrfs_qgroup_free_data(BTRFS_I(inode), NULL, start,
5163 end - start + 1);
Qu Wenruob9d0b382015-09-29 10:35:16 +08005164
Filipe Manana6ca07092015-05-26 00:55:42 +01005165 clear_extent_bit(io_tree, start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07005166 EXTENT_LOCKED | EXTENT_DELALLOC |
5167 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
5168 &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005169
Filipe Manana7064dd52014-08-08 02:47:05 +01005170 cond_resched();
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005171 spin_lock(&io_tree->lock);
5172 }
5173 spin_unlock(&io_tree->lock);
5174}
5175
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005176static struct btrfs_trans_handle *evict_refill_and_join(struct btrfs_root *root,
Josef Bacikad80cf52018-09-28 07:18:19 -04005177 struct btrfs_block_rsv *rsv)
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005178{
5179 struct btrfs_fs_info *fs_info = root->fs_info;
5180 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
Josef Bacikd3984c92019-08-01 18:19:37 -04005181 struct btrfs_trans_handle *trans;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005182 u64 delayed_refs_extra = btrfs_calc_insert_metadata_size(fs_info, 1);
Josef Bacikd3984c92019-08-01 18:19:37 -04005183 int ret;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005184
Josef Bacikd3984c92019-08-01 18:19:37 -04005185 /*
5186 * Eviction should be taking place at some place safe because of our
5187 * delayed iputs. However the normal flushing code will run delayed
5188 * iputs, so we cannot use FLUSH_ALL otherwise we'll deadlock.
5189 *
5190 * We reserve the delayed_refs_extra here again because we can't use
5191 * btrfs_start_transaction(root, 0) for the same deadlocky reason as
5192 * above. We reserve our extra bit here because we generate a ton of
5193 * delayed refs activity by truncating.
5194 *
5195 * If we cannot make our reservation we'll attempt to steal from the
5196 * global reserve, because we really want to be able to free up space.
5197 */
5198 ret = btrfs_block_rsv_refill(root, rsv, rsv->size + delayed_refs_extra,
5199 BTRFS_RESERVE_FLUSH_EVICT);
5200 if (ret) {
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005201 /*
5202 * Try to steal from the global reserve if there is space for
5203 * it.
5204 */
Josef Bacikd3984c92019-08-01 18:19:37 -04005205 if (btrfs_check_space_for_delayed_refs(fs_info) ||
5206 btrfs_block_rsv_migrate(global_rsv, rsv, rsv->size, 0)) {
5207 btrfs_warn(fs_info,
5208 "could not allocate space for delete; will truncate on mount");
5209 return ERR_PTR(-ENOSPC);
5210 }
5211 delayed_refs_extra = 0;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005212 }
Josef Bacikd3984c92019-08-01 18:19:37 -04005213
5214 trans = btrfs_join_transaction(root);
5215 if (IS_ERR(trans))
5216 return trans;
5217
5218 if (delayed_refs_extra) {
5219 trans->block_rsv = &fs_info->trans_block_rsv;
5220 trans->bytes_reserved = delayed_refs_extra;
5221 btrfs_block_rsv_migrate(rsv, trans->block_rsv,
5222 delayed_refs_extra, 1);
5223 }
5224 return trans;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005225}
5226
Al Virobd555972010-06-07 11:35:40 -04005227void btrfs_evict_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005228{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005229 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005230 struct btrfs_trans_handle *trans;
5231 struct btrfs_root *root = BTRFS_I(inode)->root;
Omar Sandoval4b9d7b52018-05-11 13:13:36 -07005232 struct btrfs_block_rsv *rsv;
Chris Mason39279cc2007-06-12 06:35:45 -04005233 int ret;
5234
liubo1abe9b82011-03-24 11:18:59 +00005235 trace_btrfs_inode_evict(inode);
5236
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005237 if (!root) {
Liu Boe8f1bc12018-01-25 11:02:53 -07005238 clear_inode(inode);
Nikolay Borisov3d48d982016-06-29 09:46:41 +03005239 return;
5240 }
5241
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00005242 evict_inode_truncate_pages(inode);
5243
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005244 if (inode->i_nlink &&
5245 ((btrfs_root_refs(&root->root_item) != 0 &&
5246 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
Nikolay Borisov70ddc552017-02-20 13:50:35 +02005247 btrfs_is_free_space_inode(BTRFS_I(inode))))
Al Virobd555972010-06-07 11:35:40 -04005248 goto no_delete;
5249
Omar Sandoval27919062018-05-11 13:13:37 -07005250 if (is_bad_inode(inode))
Chris Mason39279cc2007-06-12 06:35:45 -04005251 goto no_delete;
Chris Mason5f39d392007-10-15 16:14:19 -04005252
Nikolay Borisov7ab79562017-02-20 13:50:57 +02005253 btrfs_free_io_failure_record(BTRFS_I(inode), 0, (u64)-1);
Miao Xief6124962014-09-12 18:44:04 +08005254
Omar Sandoval7b40b692018-05-11 13:13:33 -07005255 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
Yan, Zhengc71bf092009-11-12 09:34:40 +00005256 goto no_delete;
Yan, Zhengc71bf092009-11-12 09:34:40 +00005257
Yan, Zheng76dda932009-09-21 16:00:26 -04005258 if (inode->i_nlink > 0) {
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005259 BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5260 root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
Yan, Zheng76dda932009-09-21 16:00:26 -04005261 goto no_delete;
5262 }
5263
Nikolay Borisovaa790212017-01-10 20:35:40 +02005264 ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
Omar Sandoval27919062018-05-11 13:13:37 -07005265 if (ret)
Miao Xie0e8c36a2012-12-19 06:59:51 +00005266 goto no_delete;
Miao Xie0e8c36a2012-12-19 06:59:51 +00005267
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005268 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Omar Sandoval27919062018-05-11 13:13:37 -07005269 if (!rsv)
Josef Bacik4289a662011-08-05 13:22:24 -04005270 goto no_delete;
Josef Bacik2bd36e72019-08-22 15:14:33 -04005271 rsv->size = btrfs_calc_metadata_size(fs_info, 1);
Josef Bacikca7e70f2012-08-27 17:48:15 -04005272 rsv->failfast = 1;
Josef Bacik4289a662011-08-05 13:22:24 -04005273
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02005274 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Mason5f39d392007-10-15 16:14:19 -04005275
Yan, Zheng80825102009-11-12 09:35:36 +00005276 while (1) {
Josef Bacikad80cf52018-09-28 07:18:19 -04005277 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005278 if (IS_ERR(trans))
5279 goto free_rsv;
Josef Bacik4289a662011-08-05 13:22:24 -04005280
5281 trans->block_rsv = rsv;
5282
Nikolay Borisov50743392020-11-02 16:48:55 +02005283 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
5284 0, 0);
Omar Sandoval27919062018-05-11 13:13:37 -07005285 trans->block_rsv = &fs_info->trans_block_rsv;
5286 btrfs_end_transaction(trans);
5287 btrfs_btree_balance_dirty(fs_info);
5288 if (ret && ret != -ENOSPC && ret != -EAGAIN)
5289 goto free_rsv;
5290 else if (!ret)
Yan, Zheng80825102009-11-12 09:35:36 +00005291 break;
Josef Bacik7b128762008-07-24 12:17:14 -04005292 }
5293
Josef Bacik4ef31a42013-08-13 14:10:08 -04005294 /*
Omar Sandoval27919062018-05-11 13:13:37 -07005295 * Errors here aren't a big deal, it just means we leave orphan items in
5296 * the tree. They will be cleaned up on the next mount. If the inode
5297 * number gets reused, cleanup deletes the orphan item without doing
5298 * anything, and unlink reuses the existing orphan item.
5299 *
5300 * If it turns out that we are dropping too many of these, we might want
5301 * to add a mechanism for retrying these after a commit.
Josef Bacik4ef31a42013-08-13 14:10:08 -04005302 */
Josef Bacikad80cf52018-09-28 07:18:19 -04005303 trans = evict_refill_and_join(root, rsv);
Omar Sandoval27919062018-05-11 13:13:37 -07005304 if (!IS_ERR(trans)) {
5305 trans->block_rsv = rsv;
5306 btrfs_orphan_del(trans, BTRFS_I(inode));
5307 trans->block_rsv = &fs_info->trans_block_rsv;
5308 btrfs_end_transaction(trans);
5309 }
Chris Mason85e21ba2008-01-29 15:11:36 -05005310
Omar Sandoval27919062018-05-11 13:13:37 -07005311free_rsv:
5312 btrfs_free_block_rsv(fs_info, rsv);
Chris Mason39279cc2007-06-12 06:35:45 -04005313no_delete:
Omar Sandoval27919062018-05-11 13:13:37 -07005314 /*
5315 * If we didn't successfully delete, the orphan item will still be in
5316 * the tree and we'll retry on the next mount. Again, we might also want
5317 * to retry these periodically in the future.
5318 */
Nikolay Borisovf48d1cf2017-01-10 20:35:39 +02005319 btrfs_remove_delayed_node(BTRFS_I(inode));
Jan Karadbd57682012-05-03 14:48:02 +02005320 clear_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04005321}
5322
5323/*
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005324 * Return the key found in the dir entry in the location pointer, fill @type
5325 * with BTRFS_FT_*, and return 0.
5326 *
Su Yue005d6712018-03-05 17:13:37 +08005327 * If no dir entries were found, returns -ENOENT.
5328 * If found a corrupted location in dir entry, returns -EUCLEAN.
Chris Mason39279cc2007-06-12 06:35:45 -04005329 */
5330static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005331 struct btrfs_key *location, u8 *type)
Chris Mason39279cc2007-06-12 06:35:45 -04005332{
5333 const char *name = dentry->d_name.name;
5334 int namelen = dentry->d_name.len;
5335 struct btrfs_dir_item *di;
5336 struct btrfs_path *path;
5337 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04005338 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005339
5340 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07005341 if (!path)
5342 return -ENOMEM;
Chris Mason39544012007-12-12 14:38:19 -05005343
David Sterbaf85b7372017-01-20 14:54:07 +01005344 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5345 name, namelen, 0);
Liu Bo3cf50682018-09-12 06:06:26 +08005346 if (IS_ERR_OR_NULL(di)) {
5347 ret = di ? PTR_ERR(di) : -ENOENT;
Su Yue005d6712018-03-05 17:13:37 +08005348 goto out;
5349 }
Chris Masond3977122009-01-05 21:25:51 -05005350
Chris Mason5f39d392007-10-15 16:14:19 -04005351 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Liu Bo56a0e702017-10-30 11:14:38 -06005352 if (location->type != BTRFS_INODE_ITEM_KEY &&
5353 location->type != BTRFS_ROOT_ITEM_KEY) {
Su Yue005d6712018-03-05 17:13:37 +08005354 ret = -EUCLEAN;
Liu Bo56a0e702017-10-30 11:14:38 -06005355 btrfs_warn(root->fs_info,
5356"%s gets something invalid in DIR_ITEM (name %s, directory ino %llu, location(%llu %u %llu))",
5357 __func__, name, btrfs_ino(BTRFS_I(dir)),
5358 location->objectid, location->type, location->offset);
Liu Bo56a0e702017-10-30 11:14:38 -06005359 }
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005360 if (!ret)
5361 *type = btrfs_dir_type(path->nodes[0], di);
Chris Mason39279cc2007-06-12 06:35:45 -04005362out:
Chris Mason39279cc2007-06-12 06:35:45 -04005363 btrfs_free_path(path);
5364 return ret;
5365}
5366
5367/*
5368 * when we hit a tree root in a directory, the btrfs part of the inode
5369 * needs to be changed to reflect the root directory of the tree root. This
5370 * is kind of like crossing a mount point.
5371 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005372static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005373 struct inode *dir,
5374 struct dentry *dentry,
5375 struct btrfs_key *location,
5376 struct btrfs_root **sub_root)
Chris Mason39279cc2007-06-12 06:35:45 -04005377{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005378 struct btrfs_path *path;
5379 struct btrfs_root *new_root;
5380 struct btrfs_root_ref *ref;
5381 struct extent_buffer *leaf;
David Sterba1d4c08e2015-01-02 19:36:14 +01005382 struct btrfs_key key;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005383 int ret;
5384 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005385
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005386 path = btrfs_alloc_path();
5387 if (!path) {
5388 err = -ENOMEM;
5389 goto out;
5390 }
Chris Mason39279cc2007-06-12 06:35:45 -04005391
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005392 err = -ENOENT;
David Sterba1d4c08e2015-01-02 19:36:14 +01005393 key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5394 key.type = BTRFS_ROOT_REF_KEY;
5395 key.offset = location->objectid;
5396
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005397 ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005398 if (ret) {
5399 if (ret < 0)
5400 err = ret;
5401 goto out;
5402 }
Chris Mason39279cc2007-06-12 06:35:45 -04005403
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005404 leaf = path->nodes[0];
5405 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005406 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005407 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5408 goto out;
5409
5410 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5411 (unsigned long)(ref + 1),
5412 dentry->d_name.len);
5413 if (ret)
5414 goto out;
5415
David Sterbab3b4aa72011-04-21 01:20:15 +02005416 btrfs_release_path(path);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005417
David Sterba56e93572020-05-15 19:35:55 +02005418 new_root = btrfs_get_fs_root(fs_info, location->objectid, true);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005419 if (IS_ERR(new_root)) {
5420 err = PTR_ERR(new_root);
5421 goto out;
5422 }
5423
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005424 *sub_root = new_root;
5425 location->objectid = btrfs_root_dirid(&new_root->root_item);
5426 location->type = BTRFS_INODE_ITEM_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04005427 location->offset = 0;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005428 err = 0;
5429out:
5430 btrfs_free_path(path);
5431 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04005432}
5433
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005434static void inode_tree_add(struct inode *inode)
5435{
5436 struct btrfs_root *root = BTRFS_I(inode)->root;
5437 struct btrfs_inode *entry;
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005438 struct rb_node **p;
5439 struct rb_node *parent;
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005440 struct rb_node *new = &BTRFS_I(inode)->rb_node;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005441 u64 ino = btrfs_ino(BTRFS_I(inode));
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005442
Al Viro1d3382cb2010-10-23 15:19:20 -04005443 if (inode_unhashed(inode))
Yan, Zheng76dda932009-09-21 16:00:26 -04005444 return;
Miao Xiee1409ce2013-05-15 07:48:16 +00005445 parent = NULL;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005446 spin_lock(&root->inode_lock);
Miao Xiee1409ce2013-05-15 07:48:16 +00005447 p = &root->inode_tree.rb_node;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005448 while (*p) {
5449 parent = *p;
5450 entry = rb_entry(parent, struct btrfs_inode, rb_node);
5451
David Sterba37508512018-06-29 10:56:40 +02005452 if (ino < btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005453 p = &parent->rb_left;
David Sterba37508512018-06-29 10:56:40 +02005454 else if (ino > btrfs_ino(entry))
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005455 p = &parent->rb_right;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005456 else {
5457 WARN_ON(!(entry->vfs_inode.i_state &
Al Viroa4ffdde2010-06-02 17:38:30 -04005458 (I_WILL_FREE | I_FREEING)));
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005459 rb_replace_node(parent, new, &root->inode_tree);
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005460 RB_CLEAR_NODE(parent);
5461 spin_unlock(&root->inode_lock);
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005462 return;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005463 }
5464 }
Filipe David Borba Mananacef21932013-09-02 12:19:13 +01005465 rb_link_node(new, parent, p);
5466 rb_insert_color(new, &root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005467 spin_unlock(&root->inode_lock);
5468}
5469
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005470static void inode_tree_del(struct btrfs_inode *inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005471{
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005472 struct btrfs_root *root = inode->root;
Yan, Zheng76dda932009-09-21 16:00:26 -04005473 int empty = 0;
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005474
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005475 spin_lock(&root->inode_lock);
Nikolay Borisovb79b7242020-08-31 14:42:38 +03005476 if (!RB_EMPTY_NODE(&inode->rb_node)) {
5477 rb_erase(&inode->rb_node, &root->inode_tree);
5478 RB_CLEAR_NODE(&inode->rb_node);
Yan, Zheng76dda932009-09-21 16:00:26 -04005479 empty = RB_EMPTY_ROOT(&root->inode_tree);
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005480 }
From: Nick Piggin03e860b2009-08-21 10:09:44 +02005481 spin_unlock(&root->inode_lock);
Yan, Zheng76dda932009-09-21 16:00:26 -04005482
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02005483 if (empty && btrfs_root_refs(&root->root_item) == 0) {
Yan, Zheng76dda932009-09-21 16:00:26 -04005484 spin_lock(&root->inode_lock);
5485 empty = RB_EMPTY_ROOT(&root->inode_tree);
5486 spin_unlock(&root->inode_lock);
5487 if (empty)
5488 btrfs_add_dead_root(root);
5489 }
5490}
5491
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005492
Chris Masone02119d2008-09-05 16:13:11 -04005493static int btrfs_init_locked_inode(struct inode *inode, void *p)
5494{
5495 struct btrfs_iget_args *args = p;
David Sterba0202e832020-05-15 19:35:59 +02005496
5497 inode->i_ino = args->ino;
5498 BTRFS_I(inode)->location.objectid = args->ino;
5499 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5500 BTRFS_I(inode)->location.offset = 0;
Josef Bacik5c8fd992020-02-14 16:11:43 -05005501 BTRFS_I(inode)->root = btrfs_grab_root(args->root);
5502 BUG_ON(args->root && !BTRFS_I(inode)->root);
Chris Mason39279cc2007-06-12 06:35:45 -04005503 return 0;
5504}
5505
5506static int btrfs_find_actor(struct inode *inode, void *opaque)
5507{
5508 struct btrfs_iget_args *args = opaque;
David Sterba0202e832020-05-15 19:35:59 +02005509
5510 return args->ino == BTRFS_I(inode)->location.objectid &&
Chris Masond3977122009-01-05 21:25:51 -05005511 args->root == BTRFS_I(inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005512}
5513
David Sterba0202e832020-05-15 19:35:59 +02005514static struct inode *btrfs_iget_locked(struct super_block *s, u64 ino,
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005515 struct btrfs_root *root)
Chris Mason39279cc2007-06-12 06:35:45 -04005516{
5517 struct inode *inode;
5518 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02005519 unsigned long hashval = btrfs_inode_hash(ino, root);
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005520
David Sterba0202e832020-05-15 19:35:59 +02005521 args.ino = ino;
Chris Mason39279cc2007-06-12 06:35:45 -04005522 args.root = root;
5523
Filipe David Borba Manana778ba822013-10-06 22:22:33 +01005524 inode = iget5_locked(s, hashval, btrfs_find_actor,
Chris Mason39279cc2007-06-12 06:35:45 -04005525 btrfs_init_locked_inode,
5526 (void *)&args);
5527 return inode;
5528}
5529
David Sterba4c66e0d2019-10-03 19:09:35 +02005530/*
David Sterba0202e832020-05-15 19:35:59 +02005531 * Get an inode object given its inode number and corresponding root.
David Sterba4c66e0d2019-10-03 19:09:35 +02005532 * Path can be preallocated to prevent recursing back to iget through
5533 * allocator. NULL is also valid but may require an additional allocation
5534 * later.
Balaji Rao1a54ef82008-07-21 02:01:04 +05305535 */
David Sterba0202e832020-05-15 19:35:59 +02005536struct inode *btrfs_iget_path(struct super_block *s, u64 ino,
David Sterba4c66e0d2019-10-03 19:09:35 +02005537 struct btrfs_root *root, struct btrfs_path *path)
Balaji Rao1a54ef82008-07-21 02:01:04 +05305538{
5539 struct inode *inode;
5540
David Sterba0202e832020-05-15 19:35:59 +02005541 inode = btrfs_iget_locked(s, ino, root);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305542 if (!inode)
Yan Zheng5d4f98a2009-06-10 10:45:14 -04005543 return ERR_PTR(-ENOMEM);
Balaji Rao1a54ef82008-07-21 02:01:04 +05305544
5545 if (inode->i_state & I_NEW) {
Filipe Manana67710892016-06-06 11:51:25 +01005546 int ret;
5547
Filipe Manana4222ea72018-10-24 10:13:03 +01005548 ret = btrfs_read_locked_inode(inode, path);
Al Viro9bc2cef2018-07-29 23:04:50 +01005549 if (!ret) {
Mark Fasheh1748f842011-07-12 11:25:31 -07005550 inode_tree_add(inode);
5551 unlock_new_inode(inode);
Mark Fasheh1748f842011-07-12 11:25:31 -07005552 } else {
Al Virof5b3a412018-07-29 23:04:51 +01005553 iget_failed(inode);
5554 /*
5555 * ret > 0 can come from btrfs_search_slot called by
5556 * btrfs_read_locked_inode, this means the inode item
5557 * was not found.
5558 */
5559 if (ret > 0)
5560 ret = -ENOENT;
5561 inode = ERR_PTR(ret);
Mark Fasheh1748f842011-07-12 11:25:31 -07005562 }
5563 }
5564
Balaji Rao1a54ef82008-07-21 02:01:04 +05305565 return inode;
5566}
5567
David Sterba0202e832020-05-15 19:35:59 +02005568struct inode *btrfs_iget(struct super_block *s, u64 ino, struct btrfs_root *root)
Filipe Manana4222ea72018-10-24 10:13:03 +01005569{
David Sterba0202e832020-05-15 19:35:59 +02005570 return btrfs_iget_path(s, ino, root, NULL);
Filipe Manana4222ea72018-10-24 10:13:03 +01005571}
5572
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005573static struct inode *new_simple_dir(struct super_block *s,
5574 struct btrfs_key *key,
5575 struct btrfs_root *root)
5576{
5577 struct inode *inode = new_inode(s);
5578
5579 if (!inode)
5580 return ERR_PTR(-ENOMEM);
5581
Josef Bacik5c8fd992020-02-14 16:11:43 -05005582 BTRFS_I(inode)->root = btrfs_grab_root(root);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005583 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
Josef Bacik72ac3c02012-05-23 14:13:11 -04005584 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005585
5586 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
Omar Sandoval6bb6b512019-12-05 10:36:04 -08005587 /*
5588 * We only need lookup, the rest is read-only and there's no inode
5589 * associated with the dentry
5590 */
5591 inode->i_op = &simple_dir_inode_operations;
Omar Sandoval1fdf4192017-01-25 17:06:39 -08005592 inode->i_opflags &= ~IOP_XATTR;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005593 inode->i_fop = &simple_dir_operations;
5594 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
Deepa Dinamanic2050a42016-09-14 07:48:06 -07005595 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05305596 inode->i_atime = inode->i_mtime;
5597 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02005598 BTRFS_I(inode)->i_otime = inode->i_mtime;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005599
5600 return inode;
5601}
5602
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005603static inline u8 btrfs_inode_type(struct inode *inode)
5604{
5605 /*
5606 * Compile-time asserts that generic FT_* types still match
5607 * BTRFS_FT_* types
5608 */
5609 BUILD_BUG_ON(BTRFS_FT_UNKNOWN != FT_UNKNOWN);
5610 BUILD_BUG_ON(BTRFS_FT_REG_FILE != FT_REG_FILE);
5611 BUILD_BUG_ON(BTRFS_FT_DIR != FT_DIR);
5612 BUILD_BUG_ON(BTRFS_FT_CHRDEV != FT_CHRDEV);
5613 BUILD_BUG_ON(BTRFS_FT_BLKDEV != FT_BLKDEV);
5614 BUILD_BUG_ON(BTRFS_FT_FIFO != FT_FIFO);
5615 BUILD_BUG_ON(BTRFS_FT_SOCK != FT_SOCK);
5616 BUILD_BUG_ON(BTRFS_FT_SYMLINK != FT_SYMLINK);
5617
5618 return fs_umode_to_ftype(inode->i_mode);
5619}
5620
Chris Mason3de45862008-11-17 21:02:50 -05005621struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04005622{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005623 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masond3977122009-01-05 21:25:51 -05005624 struct inode *inode;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005625 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04005626 struct btrfs_root *sub_root = root;
5627 struct btrfs_key location;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005628 u8 di_type = 0;
Josef Bacikb4aff1f2011-06-28 16:18:59 -04005629 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005630
5631 if (dentry->d_name.len > BTRFS_NAME_LEN)
5632 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04005633
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005634 ret = btrfs_inode_by_name(dir, dentry, &location, &di_type);
Chris Mason39279cc2007-06-12 06:35:45 -04005635 if (ret < 0)
5636 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04005637
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005638 if (location.type == BTRFS_INODE_ITEM_KEY) {
David Sterba0202e832020-05-15 19:35:59 +02005639 inode = btrfs_iget(dir->i_sb, location.objectid, root);
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08005640 if (IS_ERR(inode))
5641 return inode;
5642
5643 /* Do extra check against inode mode with di_type */
5644 if (btrfs_inode_type(inode) != di_type) {
5645 btrfs_crit(fs_info,
5646"inode mode mismatch with dir: inode mode=0%o btrfs type=%u dir type=%u",
5647 inode->i_mode, btrfs_inode_type(inode),
5648 di_type);
5649 iput(inode);
5650 return ERR_PTR(-EUCLEAN);
5651 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005652 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04005653 }
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005654
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005655 ret = fixup_tree_root_location(fs_info, dir, dentry,
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04005656 &location, &sub_root);
5657 if (ret < 0) {
5658 if (ret != -ENOENT)
5659 inode = ERR_PTR(ret);
5660 else
5661 inode = new_simple_dir(dir->i_sb, &location, sub_root);
5662 } else {
David Sterba0202e832020-05-15 19:35:59 +02005663 inode = btrfs_iget(dir->i_sb, location.objectid, sub_root);
Chris Mason39279cc2007-06-12 06:35:45 -04005664 }
Josef Bacik87270022020-01-24 09:32:31 -05005665 if (root != sub_root)
Josef Bacik00246522020-01-24 09:33:01 -05005666 btrfs_put_root(sub_root);
Yan, Zheng76dda932009-09-21 16:00:26 -04005667
Julia Lawall34d19ba2011-01-24 19:55:19 +00005668 if (!IS_ERR(inode) && root != sub_root) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005669 down_read(&fs_info->cleanup_work_sem);
David Howellsbc98a422017-07-17 08:45:34 +01005670 if (!sb_rdonly(inode->i_sb))
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005671 ret = btrfs_orphan_cleanup(sub_root);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04005672 up_read(&fs_info->cleanup_work_sem);
Josef Bacik01cd3362013-06-03 21:39:49 -04005673 if (ret) {
5674 iput(inode);
Josef Bacik66b4ffd2011-01-31 16:22:42 -05005675 inode = ERR_PTR(ret);
Josef Bacik01cd3362013-06-03 21:39:49 -04005676 }
Yan, Zhengc71bf092009-11-12 09:34:40 +00005677 }
5678
Chris Mason3de45862008-11-17 21:02:50 -05005679 return inode;
5680}
5681
Nick Pigginfe15ce42011-01-07 17:49:23 +11005682static int btrfs_dentry_delete(const struct dentry *dentry)
Yan, Zheng76dda932009-09-21 16:00:26 -04005683{
5684 struct btrfs_root *root;
David Howells2b0143b2015-03-17 22:25:59 +00005685 struct inode *inode = d_inode(dentry);
Yan, Zheng76dda932009-09-21 16:00:26 -04005686
Li Zefan848cce02012-02-21 17:04:28 +08005687 if (!inode && !IS_ROOT(dentry))
David Howells2b0143b2015-03-17 22:25:59 +00005688 inode = d_inode(dentry->d_parent);
Yan, Zheng76dda932009-09-21 16:00:26 -04005689
Li Zefan848cce02012-02-21 17:04:28 +08005690 if (inode) {
5691 root = BTRFS_I(inode)->root;
Yan, Zhengefefb142009-10-09 09:25:16 -04005692 if (btrfs_root_refs(&root->root_item) == 0)
5693 return 1;
Li Zefan848cce02012-02-21 17:04:28 +08005694
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005695 if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Li Zefan848cce02012-02-21 17:04:28 +08005696 return 1;
Yan, Zhengefefb142009-10-09 09:25:16 -04005697 }
Yan, Zheng76dda932009-09-21 16:00:26 -04005698 return 0;
5699}
5700
Chris Mason3de45862008-11-17 21:02:50 -05005701static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -04005702 unsigned int flags)
Chris Mason3de45862008-11-17 21:02:50 -05005703{
Al Viro3837d202018-10-10 16:38:27 -04005704 struct inode *inode = btrfs_lookup_dentry(dir, dentry);
Josef Bacika66e7cc2011-09-18 10:34:03 -04005705
Al Viro3837d202018-10-10 16:38:27 -04005706 if (inode == ERR_PTR(-ENOENT))
5707 inode = NULL;
Al Viro41d28bc2014-10-12 22:24:21 -04005708 return d_splice_alias(inode, dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04005709}
5710
Josef Bacik23b5ec72017-07-24 15:14:25 -04005711/*
5712 * All this infrastructure exists because dir_emit can fault, and we are holding
5713 * the tree lock when doing readdir. For now just allocate a buffer and copy
5714 * our information into that, and then dir_emit from the buffer. This is
5715 * similar to what NFS does, only we don't keep the buffer around in pagecache
5716 * because I'm afraid I'll mess that up. Long term we need to make filldir do
5717 * copy_to_user_inatomic so we don't have to worry about page faulting under the
5718 * tree lock.
5719 */
5720static int btrfs_opendir(struct inode *inode, struct file *file)
5721{
5722 struct btrfs_file_private *private;
5723
5724 private = kzalloc(sizeof(struct btrfs_file_private), GFP_KERNEL);
5725 if (!private)
5726 return -ENOMEM;
5727 private->filldir_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
5728 if (!private->filldir_buf) {
5729 kfree(private);
5730 return -ENOMEM;
5731 }
5732 file->private_data = private;
5733 return 0;
5734}
5735
5736struct dir_entry {
5737 u64 ino;
5738 u64 offset;
5739 unsigned type;
5740 int name_len;
5741};
5742
5743static int btrfs_filldir(void *addr, int entries, struct dir_context *ctx)
5744{
5745 while (entries--) {
5746 struct dir_entry *entry = addr;
5747 char *name = (char *)(entry + 1);
5748
David Sterba92d32172018-04-16 21:10:14 +02005749 ctx->pos = get_unaligned(&entry->offset);
5750 if (!dir_emit(ctx, name, get_unaligned(&entry->name_len),
5751 get_unaligned(&entry->ino),
5752 get_unaligned(&entry->type)))
Josef Bacik23b5ec72017-07-24 15:14:25 -04005753 return 1;
David Sterba92d32172018-04-16 21:10:14 +02005754 addr += sizeof(struct dir_entry) +
5755 get_unaligned(&entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005756 ctx->pos++;
5757 }
5758 return 0;
5759}
5760
Al Viro9cdda8d2013-05-22 16:48:09 -04005761static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
Chris Mason39279cc2007-06-12 06:35:45 -04005762{
Al Viro9cdda8d2013-05-22 16:48:09 -04005763 struct inode *inode = file_inode(file);
Chris Mason39279cc2007-06-12 06:35:45 -04005764 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005765 struct btrfs_file_private *private = file->private_data;
Chris Mason39279cc2007-06-12 06:35:45 -04005766 struct btrfs_dir_item *di;
5767 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04005768 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04005769 struct btrfs_path *path;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005770 void *addr;
Miao Xie16cdcec2011-04-22 18:12:22 +08005771 struct list_head ins_list;
5772 struct list_head del_list;
Chris Mason39279cc2007-06-12 06:35:45 -04005773 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04005774 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04005775 int slot;
Chris Mason5f39d392007-10-15 16:14:19 -04005776 char *name_ptr;
5777 int name_len;
Josef Bacik23b5ec72017-07-24 15:14:25 -04005778 int entries = 0;
5779 int total_len = 0;
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005780 bool put = false;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005781 struct btrfs_key location;
Chris Mason5f39d392007-10-15 16:14:19 -04005782
Al Viro9cdda8d2013-05-22 16:48:09 -04005783 if (!dir_emit_dots(file, ctx))
5784 return 0;
5785
David Woodhouse49593bf2008-08-17 17:08:36 +01005786 path = btrfs_alloc_path();
Miao Xie16cdcec2011-04-22 18:12:22 +08005787 if (!path)
5788 return -ENOMEM;
Chris Masonff5714c2011-05-28 07:00:39 -04005789
Josef Bacik23b5ec72017-07-24 15:14:25 -04005790 addr = private->filldir_buf;
David Sterbae4058b52015-11-27 16:31:35 +01005791 path->reada = READA_FORWARD;
David Woodhouse49593bf2008-08-17 17:08:36 +01005792
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005793 INIT_LIST_HEAD(&ins_list);
5794 INIT_LIST_HEAD(&del_list);
5795 put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
Miao Xie16cdcec2011-04-22 18:12:22 +08005796
Josef Bacik23b5ec72017-07-24 15:14:25 -04005797again:
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005798 key.type = BTRFS_DIR_INDEX_KEY;
Al Viro9cdda8d2013-05-22 16:48:09 -04005799 key.offset = ctx->pos;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02005800 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason5f39d392007-10-15 16:14:19 -04005801
Chris Mason39279cc2007-06-12 06:35:45 -04005802 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5803 if (ret < 0)
5804 goto err;
David Woodhouse49593bf2008-08-17 17:08:36 +01005805
5806 while (1) {
Josef Bacik23b5ec72017-07-24 15:14:25 -04005807 struct dir_entry *entry;
5808
Chris Mason5f39d392007-10-15 16:14:19 -04005809 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -04005810 slot = path->slots[0];
Li Zefanb9e03af2011-03-23 10:43:58 +08005811 if (slot >= btrfs_header_nritems(leaf)) {
5812 ret = btrfs_next_leaf(root, path);
5813 if (ret < 0)
5814 goto err;
5815 else if (ret > 0)
5816 break;
5817 continue;
Chris Mason39279cc2007-06-12 06:35:45 -04005818 }
Chris Mason3de45862008-11-17 21:02:50 -05005819
Chris Mason5f39d392007-10-15 16:14:19 -04005820 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5821
5822 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04005823 break;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005824 if (found_key.type != BTRFS_DIR_INDEX_KEY)
Chris Mason39279cc2007-06-12 06:35:45 -04005825 break;
Al Viro9cdda8d2013-05-22 16:48:09 -04005826 if (found_key.offset < ctx->pos)
Li Zefanb9e03af2011-03-23 10:43:58 +08005827 goto next;
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005828 if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
Miao Xie16cdcec2011-04-22 18:12:22 +08005829 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04005830 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005831 name_len = btrfs_dir_name_len(leaf, di);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005832 if ((total_len + sizeof(struct dir_entry) + name_len) >=
5833 PAGE_SIZE) {
5834 btrfs_release_path(path);
5835 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5836 if (ret)
5837 goto nopos;
5838 addr = private->filldir_buf;
5839 entries = 0;
5840 total_len = 0;
5841 goto again;
Chris Mason39279cc2007-06-12 06:35:45 -04005842 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005843
5844 entry = addr;
David Sterba92d32172018-04-16 21:10:14 +02005845 put_unaligned(name_len, &entry->name_len);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005846 name_ptr = (char *)(entry + 1);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005847 read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5848 name_len);
Phillip Potter7d157c32019-03-26 21:39:34 +00005849 put_unaligned(fs_ftype_to_dtype(btrfs_dir_type(leaf, di)),
David Sterba92d32172018-04-16 21:10:14 +02005850 &entry->type);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005851 btrfs_dir_item_key_to_cpu(leaf, di, &location);
David Sterba92d32172018-04-16 21:10:14 +02005852 put_unaligned(location.objectid, &entry->ino);
5853 put_unaligned(found_key.offset, &entry->offset);
Josef Bacik23b5ec72017-07-24 15:14:25 -04005854 entries++;
5855 addr += sizeof(struct dir_entry) + name_len;
5856 total_len += sizeof(struct dir_entry) + name_len;
Li Zefanb9e03af2011-03-23 10:43:58 +08005857next:
5858 path->slots[0]++;
Chris Mason39279cc2007-06-12 06:35:45 -04005859 }
Josef Bacik23b5ec72017-07-24 15:14:25 -04005860 btrfs_release_path(path);
5861
5862 ret = btrfs_filldir(private->filldir_buf, entries, ctx);
5863 if (ret)
5864 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01005865
Jeff Mahoneyd2fbb2b2016-11-05 13:26:35 -04005866 ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005867 if (ret)
David Sterbabc4ef752015-11-13 13:44:28 +01005868 goto nopos;
5869
Zach Browndb62efb2013-07-11 16:19:42 -07005870 /*
5871 * Stop new entries from being returned after we return the last
5872 * entry.
5873 *
5874 * New directory entries are assigned a strictly increasing
5875 * offset. This means that new entries created during readdir
5876 * are *guaranteed* to be seen in the future by that readdir.
5877 * This has broken buggy programs which operate on names as
5878 * they're returned by readdir. Until we re-use freed offsets
5879 * we have this hack to stop new entries from being returned
5880 * under the assumption that they'll never reach this huge
5881 * offset.
5882 *
5883 * This is being careful not to overflow 32bit loff_t unless the
5884 * last entry requires it because doing so has broken 32bit apps
5885 * in the past.
5886 */
Jeff Mahoneyc2951f32016-11-21 15:59:04 +01005887 if (ctx->pos >= INT_MAX)
5888 ctx->pos = LLONG_MAX;
5889 else
5890 ctx->pos = INT_MAX;
Chris Mason39279cc2007-06-12 06:35:45 -04005891nopos:
5892 ret = 0;
5893err:
Omar Sandoval02dbfc92016-05-20 13:50:33 -07005894 if (put)
5895 btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
Chris Mason39279cc2007-06-12 06:35:45 -04005896 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04005897 return ret;
5898}
5899
Chris Mason39279cc2007-06-12 06:35:45 -04005900/*
Chris Mason54aa1f42007-06-22 14:16:25 -04005901 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04005902 * inode changes. But, it is most likely to find the inode in cache.
5903 * FIXME, needs more benchmarking...there are no reasons other than performance
5904 * to keep or drop this code.
5905 */
Eric Sandeen48a3b632013-04-25 20:41:01 +00005906static int btrfs_dirty_inode(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04005907{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005908 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04005909 struct btrfs_root *root = BTRFS_I(inode)->root;
5910 struct btrfs_trans_handle *trans;
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005911 int ret;
5912
Josef Bacik72ac3c02012-05-23 14:13:11 -04005913 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
Josef Bacik22c44fe2011-11-30 10:45:38 -05005914 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005915
Josef Bacik7a7eaa42011-04-13 12:54:33 -04005916 trans = btrfs_join_transaction(root);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005917 if (IS_ERR(trans))
5918 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005919
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005920 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04005921 if (ret && ret == -ENOSPC) {
5922 /* whoops, lets try again with the full transaction */
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005923 btrfs_end_transaction(trans);
Chris Mason94b60442010-05-26 11:02:00 -04005924 trans = btrfs_start_transaction(root, 1);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005925 if (IS_ERR(trans))
5926 return PTR_ERR(trans);
Yan, Zheng8929ecfa2010-05-16 10:49:58 -04005927
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02005928 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason94b60442010-05-26 11:02:00 -04005929 }
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04005930 btrfs_end_transaction(trans);
Miao Xie16cdcec2011-04-22 18:12:22 +08005931 if (BTRFS_I(inode)->delayed_node)
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04005932 btrfs_balance_delayed_items(fs_info);
Josef Bacik22c44fe2011-11-30 10:45:38 -05005933
5934 return ret;
5935}
5936
5937/*
5938 * This is a copy of file_update_time. We need this so we can return error on
5939 * ENOSPC for updating the inode in the case of file write and mmap writes.
5940 */
Deepa Dinamani95582b02018-05-08 19:36:02 -07005941static int btrfs_update_time(struct inode *inode, struct timespec64 *now,
Josef Bacike41f9412012-03-26 09:46:47 -04005942 int flags)
Josef Bacik22c44fe2011-11-30 10:45:38 -05005943{
Alexander Block2bc5565282012-06-15 09:49:33 +02005944 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005945 bool dirty = flags & ~S_VERSION;
Alexander Block2bc5565282012-06-15 09:49:33 +02005946
5947 if (btrfs_root_readonly(root))
5948 return -EROFS;
5949
Josef Bacike41f9412012-03-26 09:46:47 -04005950 if (flags & S_VERSION)
Jeff Layton3a8c7232017-12-11 06:35:24 -05005951 dirty |= inode_maybe_inc_iversion(inode, dirty);
Josef Bacike41f9412012-03-26 09:46:47 -04005952 if (flags & S_CTIME)
5953 inode->i_ctime = *now;
5954 if (flags & S_MTIME)
5955 inode->i_mtime = *now;
5956 if (flags & S_ATIME)
5957 inode->i_atime = *now;
Jeff Layton3a8c7232017-12-11 06:35:24 -05005958 return dirty ? btrfs_dirty_inode(inode) : 0;
Chris Mason39279cc2007-06-12 06:35:45 -04005959}
5960
Chris Masond352ac62008-09-29 15:18:18 -04005961/*
5962 * find the highest existing sequence number in a directory
5963 * and then set the in-memory index_cnt variable to reflect
5964 * free sequence numbers
5965 */
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005966static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
Josef Bacikaec74772008-07-24 12:12:38 -04005967{
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005968 struct btrfs_root *root = inode->root;
Josef Bacikaec74772008-07-24 12:12:38 -04005969 struct btrfs_key key, found_key;
5970 struct btrfs_path *path;
5971 struct extent_buffer *leaf;
5972 int ret;
5973
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005974 key.objectid = btrfs_ino(inode);
David Sterba962a2982014-06-04 18:41:45 +02005975 key.type = BTRFS_DIR_INDEX_KEY;
Josef Bacikaec74772008-07-24 12:12:38 -04005976 key.offset = (u64)-1;
5977
5978 path = btrfs_alloc_path();
5979 if (!path)
5980 return -ENOMEM;
5981
5982 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5983 if (ret < 0)
5984 goto out;
5985 /* FIXME: we should be able to handle this */
5986 if (ret == 0)
5987 goto out;
5988 ret = 0;
5989
5990 /*
5991 * MAGIC NUMBER EXPLANATION:
5992 * since we search a directory based on f_pos we have to start at 2
5993 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
5994 * else has to start at 2
5995 */
5996 if (path->slots[0] == 0) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02005997 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04005998 goto out;
5999 }
6000
6001 path->slots[0]--;
6002
6003 leaf = path->nodes[0];
6004 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6005
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006006 if (found_key.objectid != btrfs_ino(inode) ||
David Sterba962a2982014-06-04 18:41:45 +02006007 found_key.type != BTRFS_DIR_INDEX_KEY) {
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006008 inode->index_cnt = 2;
Josef Bacikaec74772008-07-24 12:12:38 -04006009 goto out;
6010 }
6011
Nikolay Borisov4c5706552017-02-20 13:50:32 +02006012 inode->index_cnt = found_key.offset + 1;
Josef Bacikaec74772008-07-24 12:12:38 -04006013out:
6014 btrfs_free_path(path);
6015 return ret;
6016}
6017
Chris Masond352ac62008-09-29 15:18:18 -04006018/*
6019 * helper to find a free sequence number in a given directory. This current
6020 * code is very simple, later versions will do smarter things in the btree
6021 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02006022int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04006023{
6024 int ret = 0;
6025
Nikolay Borisov877574e2017-02-20 13:50:33 +02006026 if (dir->index_cnt == (u64)-1) {
6027 ret = btrfs_inode_delayed_dir_index_count(dir);
Miao Xie16cdcec2011-04-22 18:12:22 +08006028 if (ret) {
6029 ret = btrfs_set_inode_index_count(dir);
6030 if (ret)
6031 return ret;
6032 }
Josef Bacikaec74772008-07-24 12:12:38 -04006033 }
6034
Nikolay Borisov877574e2017-02-20 13:50:33 +02006035 *index = dir->index_cnt;
6036 dir->index_cnt++;
Josef Bacikaec74772008-07-24 12:12:38 -04006037
6038 return ret;
6039}
6040
Chris Masonb0d5d102014-09-08 13:08:51 -07006041static int btrfs_insert_inode_locked(struct inode *inode)
6042{
6043 struct btrfs_iget_args args;
David Sterba0202e832020-05-15 19:35:59 +02006044
6045 args.ino = BTRFS_I(inode)->location.objectid;
Chris Masonb0d5d102014-09-08 13:08:51 -07006046 args.root = BTRFS_I(inode)->root;
6047
6048 return insert_inode_locked4(inode,
6049 btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6050 btrfs_find_actor, &args);
6051}
6052
Anand Jain19aee8d2017-07-18 17:37:05 +08006053/*
6054 * Inherit flags from the parent inode.
6055 *
6056 * Currently only the compression flags and the cow flags are inherited.
6057 */
6058static void btrfs_inherit_iflags(struct inode *inode, struct inode *dir)
6059{
6060 unsigned int flags;
6061
6062 if (!dir)
6063 return;
6064
6065 flags = BTRFS_I(dir)->flags;
6066
6067 if (flags & BTRFS_INODE_NOCOMPRESS) {
6068 BTRFS_I(inode)->flags &= ~BTRFS_INODE_COMPRESS;
6069 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
6070 } else if (flags & BTRFS_INODE_COMPRESS) {
6071 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NOCOMPRESS;
6072 BTRFS_I(inode)->flags |= BTRFS_INODE_COMPRESS;
6073 }
6074
6075 if (flags & BTRFS_INODE_NODATACOW) {
6076 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
6077 if (S_ISREG(inode->i_mode))
6078 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6079 }
6080
David Sterba7b6a2212018-03-26 18:40:21 +02006081 btrfs_sync_inode_flags_to_i_flags(inode);
Anand Jain19aee8d2017-07-18 17:37:05 +08006082}
6083
Chris Mason39279cc2007-06-12 06:35:45 -04006084static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6085 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04006086 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05006087 const char *name, int name_len,
Al Viro175a4eb2011-07-26 03:30:54 -04006088 u64 ref_objectid, u64 objectid,
6089 umode_t mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04006090{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006091 struct btrfs_fs_info *fs_info = root->fs_info;
Chris Mason39279cc2007-06-12 06:35:45 -04006092 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04006093 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04006094 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04006095 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05006096 struct btrfs_inode_ref *ref;
6097 struct btrfs_key key[2];
6098 u32 sizes[2];
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006099 int nitems = name ? 2 : 1;
Chris Mason9c583092008-01-29 15:15:18 -05006100 unsigned long ptr;
Josef Bacik11a19a92019-09-09 10:12:04 -04006101 unsigned int nofs_flag;
Chris Mason39279cc2007-06-12 06:35:45 -04006102 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006103
Chris Mason5f39d392007-10-15 16:14:19 -04006104 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07006105 if (!path)
6106 return ERR_PTR(-ENOMEM);
Chris Mason5f39d392007-10-15 16:14:19 -04006107
Josef Bacik11a19a92019-09-09 10:12:04 -04006108 nofs_flag = memalloc_nofs_save();
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006109 inode = new_inode(fs_info->sb);
Josef Bacik11a19a92019-09-09 10:12:04 -04006110 memalloc_nofs_restore(nofs_flag);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006111 if (!inode) {
6112 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04006113 return ERR_PTR(-ENOMEM);
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006114 }
Chris Mason39279cc2007-06-12 06:35:45 -04006115
Li Zefan581bb052011-04-20 10:06:11 +08006116 /*
Filipe Manana5762b5c2014-08-01 00:10:32 +01006117 * O_TMPFILE, set link count to 0, so that after this point,
6118 * we fill in an inode item with the correct link count.
6119 */
6120 if (!name)
6121 set_nlink(inode, 0);
6122
6123 /*
Li Zefan581bb052011-04-20 10:06:11 +08006124 * we have to initialize this early, so we can reclaim the inode
6125 * number if we fail afterwards in this function.
6126 */
6127 inode->i_ino = objectid;
6128
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006129 if (dir && name) {
liubo1abe9b82011-03-24 11:18:59 +00006130 trace_btrfs_inode_request(dir);
6131
Nikolay Borisov877574e2017-02-20 13:50:33 +02006132 ret = btrfs_set_inode_index(BTRFS_I(dir), index);
Shen Feng09771432009-04-02 16:46:06 -04006133 if (ret) {
Yoshinori Sano8fb27642011-04-09 02:30:07 +00006134 btrfs_free_path(path);
Shen Feng09771432009-04-02 16:46:06 -04006135 iput(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04006136 return ERR_PTR(ret);
Shen Feng09771432009-04-02 16:46:06 -04006137 }
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006138 } else if (dir) {
6139 *index = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04006140 }
6141 /*
6142 * index_cnt is ignored for everything but a dir,
Su Yuedf6703e2018-01-12 11:08:02 +08006143 * btrfs_set_inode_index_count has an explanation for the magic
Josef Bacikaec74772008-07-24 12:12:38 -04006144 * number
6145 */
6146 BTRFS_I(inode)->index_cnt = 2;
Miao Xie67de1172013-12-26 13:07:06 +08006147 BTRFS_I(inode)->dir_index = *index;
Josef Bacik5c8fd992020-02-14 16:11:43 -05006148 BTRFS_I(inode)->root = btrfs_grab_root(root);
Chris Masone02119d2008-09-05 16:13:11 -04006149 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik76195852010-11-19 02:18:02 +00006150 inode->i_generation = BTRFS_I(inode)->generation;
Chris Masonb888db22007-08-27 16:49:44 -04006151
Josef Bacik5dc562c2012-08-17 13:14:17 -04006152 /*
6153 * We could have gotten an inode number from somebody who was fsynced
6154 * and then removed in this same transaction, so let's just set full
6155 * sync since it will be a full sync anyway and this will blow away the
6156 * old info in the log.
6157 */
6158 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6159
Chris Mason9c583092008-01-29 15:15:18 -05006160 key[0].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006161 key[0].type = BTRFS_INODE_ITEM_KEY;
Chris Mason9c583092008-01-29 15:15:18 -05006162 key[0].offset = 0;
6163
Chris Mason9c583092008-01-29 15:15:18 -05006164 sizes[0] = sizeof(struct btrfs_inode_item);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006165
6166 if (name) {
6167 /*
6168 * Start new inodes with an inode_ref. This is slightly more
6169 * efficient for small numbers of hard links since they will
6170 * be packed into one item. Extended refs will kick in if we
6171 * add more hard links than can fit in the ref item.
6172 */
6173 key[1].objectid = objectid;
David Sterba962a2982014-06-04 18:41:45 +02006174 key[1].type = BTRFS_INODE_REF_KEY;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006175 key[1].offset = ref_objectid;
6176
6177 sizes[1] = name_len + sizeof(*ref);
6178 }
Chris Mason9c583092008-01-29 15:15:18 -05006179
Chris Masonb0d5d102014-09-08 13:08:51 -07006180 location = &BTRFS_I(inode)->location;
6181 location->objectid = objectid;
6182 location->offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02006183 location->type = BTRFS_INODE_ITEM_KEY;
Chris Masonb0d5d102014-09-08 13:08:51 -07006184
6185 ret = btrfs_insert_inode_locked(inode);
Al Viro32955c52018-05-16 12:20:05 -04006186 if (ret < 0) {
6187 iput(inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006188 goto fail;
Al Viro32955c52018-05-16 12:20:05 -04006189 }
Chris Masonb0d5d102014-09-08 13:08:51 -07006190
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006191 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
Chris Mason9c583092008-01-29 15:15:18 -05006192 if (ret != 0)
Chris Masonb0d5d102014-09-08 13:08:51 -07006193 goto fail_unlock;
Chris Mason5f39d392007-10-15 16:14:19 -04006194
Dmitry Monakhovecc11fab2010-03-04 17:31:47 +03006195 inode_init_owner(inode, dir, mode);
Yan Zhenga76a3cd2008-10-09 11:46:29 -04006196 inode_set_bytes(inode, 0);
chandan r9cc97d62012-07-04 12:48:07 +05306197
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006198 inode->i_mtime = current_time(inode);
chandan r9cc97d62012-07-04 12:48:07 +05306199 inode->i_atime = inode->i_mtime;
6200 inode->i_ctime = inode->i_mtime;
Arnd Bergmannd3c6be6f2018-06-21 18:04:06 +02006201 BTRFS_I(inode)->i_otime = inode->i_mtime;
chandan r9cc97d62012-07-04 12:48:07 +05306202
Chris Mason5f39d392007-10-15 16:14:19 -04006203 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6204 struct btrfs_inode_item);
David Sterbab159fa22016-11-08 18:09:03 +01006205 memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
Li Zefan293f7e02012-07-10 00:58:58 -06006206 sizeof(*inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04006207 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05006208
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006209 if (name) {
6210 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6211 struct btrfs_inode_ref);
6212 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6213 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6214 ptr = (unsigned long)(ref + 1);
6215 write_extent_buffer(path->nodes[0], name, ptr, name_len);
6216 }
Chris Mason9c583092008-01-29 15:15:18 -05006217
Chris Mason5f39d392007-10-15 16:14:19 -04006218 btrfs_mark_buffer_dirty(path->nodes[0]);
6219 btrfs_free_path(path);
6220
Christoph Hellwig6cbff002009-04-17 10:37:41 +02006221 btrfs_inherit_iflags(inode, dir);
6222
Al Viro569254b2011-07-24 17:08:40 -04006223 if (S_ISREG(mode)) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006224 if (btrfs_test_opt(fs_info, NODATASUM))
Chris Mason94272162009-07-02 12:26:06 -04006225 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006226 if (btrfs_test_opt(fs_info, NODATACOW))
Josef Bacikf2bdf9a2013-02-21 15:28:28 -05006227 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6228 BTRFS_INODE_NODATASUM;
Chris Mason94272162009-07-02 12:26:06 -04006229 }
6230
Yan Zheng5d4f98a2009-06-10 10:45:14 -04006231 inode_tree_add(inode);
liubo1abe9b82011-03-24 11:18:59 +00006232
6233 trace_btrfs_inode_new(inode);
Nikolay Borisovd9094412020-06-05 10:41:13 +03006234 btrfs_set_inode_last_trans(trans, BTRFS_I(inode));
liubo1abe9b82011-03-24 11:18:59 +00006235
Alexander Block8ea05e32012-07-25 17:35:53 +02006236 btrfs_update_root_times(trans, root);
6237
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006238 ret = btrfs_inode_inherit_props(trans, inode, dir);
6239 if (ret)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006240 btrfs_err(fs_info,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006241 "error inheriting props for ino %llu (root %llu): %d",
David Sterbaf85b7372017-01-20 14:54:07 +01006242 btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
Filipe David Borba Manana63541922014-01-07 11:47:46 +00006243
Chris Mason39279cc2007-06-12 06:35:45 -04006244 return inode;
Chris Masonb0d5d102014-09-08 13:08:51 -07006245
6246fail_unlock:
Al Viro32955c52018-05-16 12:20:05 -04006247 discard_new_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006248fail:
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006249 if (dir && name)
Josef Bacikaec74772008-07-24 12:12:38 -04006250 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04006251 btrfs_free_path(path);
6252 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006253}
6254
Chris Masond352ac62008-09-29 15:18:18 -04006255/*
6256 * utility function to add 'inode' into 'parent_inode' with
6257 * a give name and a given sequence number.
6258 * if 'add_backref' is true, also insert a backref from the
6259 * inode to the parent directory.
6260 */
Chris Masone02119d2008-09-05 16:13:11 -04006261int btrfs_add_link(struct btrfs_trans_handle *trans,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006262 struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
Chris Masone02119d2008-09-05 16:13:11 -04006263 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006264{
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006265 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006266 struct btrfs_key key;
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006267 struct btrfs_root *root = parent_inode->root;
6268 u64 ino = btrfs_ino(inode);
6269 u64 parent_ino = btrfs_ino(parent_inode);
Chris Mason5f39d392007-10-15 16:14:19 -04006270
Li Zefan33345d012011-04-20 10:31:50 +08006271 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006272 memcpy(&key, &inode->root->root_key, sizeof(key));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006273 } else {
Li Zefan33345d012011-04-20 10:31:50 +08006274 key.objectid = ino;
David Sterba962a2982014-06-04 18:41:45 +02006275 key.type = BTRFS_INODE_ITEM_KEY;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006276 key.offset = 0;
6277 }
Chris Mason39279cc2007-06-12 06:35:45 -04006278
Li Zefan33345d012011-04-20 10:31:50 +08006279 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
Lu Fengqi6025c192018-08-01 11:32:29 +08006280 ret = btrfs_add_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006281 root->root_key.objectid, parent_ino,
6282 index, name, name_len);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006283 } else if (add_backref) {
Li Zefan33345d012011-04-20 10:31:50 +08006284 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6285 parent_ino, index);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006286 }
6287
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006288 /* Nothing to clean up yet */
6289 if (ret)
6290 return ret;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04006291
Lu Fengqi684572d2018-08-04 21:10:57 +08006292 ret = btrfs_insert_dir_item(trans, name, name_len, parent_inode, &key,
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006293 btrfs_inode_type(&inode->vfs_inode), index);
Chris Mason9c520572012-12-17 14:26:57 -05006294 if (ret == -EEXIST || ret == -EOVERFLOW)
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006295 goto fail_dir_item;
6296 else if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04006297 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006298 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006299 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006300
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006301 btrfs_i_size_write(parent_inode, parent_inode->vfs_inode.i_size +
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006302 name_len * 2);
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006303 inode_inc_iversion(&parent_inode->vfs_inode);
Filipe Manana5338e432019-05-15 16:02:47 +01006304 /*
6305 * If we are replaying a log tree, we do not want to update the mtime
6306 * and ctime of the parent directory with the current time, since the
6307 * log replay procedure is responsible for setting them to their correct
6308 * values (the ones it had when the fsync was done).
6309 */
6310 if (!test_bit(BTRFS_FS_LOG_RECOVERING, &root->fs_info->flags)) {
6311 struct timespec64 now = current_time(&parent_inode->vfs_inode);
6312
6313 parent_inode->vfs_inode.i_mtime = now;
6314 parent_inode->vfs_inode.i_ctime = now;
6315 }
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006316 ret = btrfs_update_inode(trans, root, parent_inode);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006317 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04006318 btrfs_abort_transaction(trans, ret);
Chris Mason39279cc2007-06-12 06:35:45 -04006319 return ret;
Chris Masonfe66a052012-02-20 08:40:56 -05006320
6321fail_dir_item:
6322 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6323 u64 local_index;
6324 int err;
Lu Fengqi3ee1c552018-08-01 11:32:28 +08006325 err = btrfs_del_root_ref(trans, key.objectid,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006326 root->root_key.objectid, parent_ino,
6327 &local_index, name, name_len);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006328 if (err)
6329 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006330 } else if (add_backref) {
6331 u64 local_index;
6332 int err;
6333
6334 err = btrfs_del_inode_ref(trans, root, name, name_len,
6335 ino, parent_ino, &local_index);
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006336 if (err)
6337 btrfs_abort_transaction(trans, err);
Chris Masonfe66a052012-02-20 08:40:56 -05006338 }
Johannes Thumshirn1690dd412018-12-12 15:14:17 +01006339
6340 /* Return the original error code */
Chris Masonfe66a052012-02-20 08:40:56 -05006341 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04006342}
6343
6344static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006345 struct btrfs_inode *dir, struct dentry *dentry,
6346 struct btrfs_inode *inode, int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04006347{
Josef Bacika1b075d2010-11-19 20:36:11 +00006348 int err = btrfs_add_link(trans, dir, inode,
6349 dentry->d_name.name, dentry->d_name.len,
6350 backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006351 if (err > 0)
6352 err = -EEXIST;
6353 return err;
6354}
6355
Josef Bacik618e21d2007-07-11 10:18:17 -04006356static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
Al Viro1a67aaf2011-07-26 01:52:52 -04006357 umode_t mode, dev_t rdev)
Josef Bacik618e21d2007-07-11 10:18:17 -04006358{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006359 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Josef Bacik618e21d2007-07-11 10:18:17 -04006360 struct btrfs_trans_handle *trans;
6361 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006362 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006363 int err;
Josef Bacik618e21d2007-07-11 10:18:17 -04006364 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006365 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04006366
Josef Bacik9ed74f22009-09-11 16:12:44 -04006367 /*
6368 * 2 for inode item and ref
6369 * 2 for dir items
6370 * 1 for xattr if selinux is on
6371 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006372 trans = btrfs_start_transaction(root, 5);
6373 if (IS_ERR(trans))
6374 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05006375
Nikolay Borisov543068a2020-12-07 17:32:33 +02006376 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006377 if (err)
6378 goto out_unlock;
6379
Josef Bacikaec74772008-07-24 12:12:38 -04006380 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006381 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6382 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006383 if (IS_ERR(inode)) {
6384 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006385 inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04006386 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006387 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006388
Casey Schauflerad19db72011-12-15 10:09:07 -05006389 /*
6390 * If the active LSM wants to access the inode during
6391 * d_instantiate it needs these. Smack checks to see
6392 * if the filesystem supports xattrs by looking at the
6393 * ops vector.
6394 */
Casey Schauflerad19db72011-12-15 10:09:07 -05006395 inode->i_op = &btrfs_special_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006396 init_special_inode(inode, inode->i_mode, rdev);
6397
6398 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik618e21d2007-07-11 10:18:17 -04006399 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006400 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006401
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006402 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6403 0, index);
Al Viro32955c52018-05-16 12:20:05 -04006404 if (err)
6405 goto out_unlock;
6406
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006407 btrfs_update_inode(trans, root, BTRFS_I(inode));
Al Viro32955c52018-05-16 12:20:05 -04006408 d_instantiate_new(dentry, inode);
Chris Masonb0d5d102014-09-08 13:08:51 -07006409
Josef Bacik618e21d2007-07-11 10:18:17 -04006410out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006411 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006412 btrfs_btree_balance_dirty(fs_info);
Al Viro32955c52018-05-16 12:20:05 -04006413 if (err && inode) {
Josef Bacik618e21d2007-07-11 10:18:17 -04006414 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006415 discard_new_inode(inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04006416 }
Josef Bacik618e21d2007-07-11 10:18:17 -04006417 return err;
6418}
6419
Chris Mason39279cc2007-06-12 06:35:45 -04006420static int btrfs_create(struct inode *dir, struct dentry *dentry,
Al Viroebfc3b42012-06-10 18:05:36 -04006421 umode_t mode, bool excl)
Chris Mason39279cc2007-06-12 06:35:45 -04006422{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006423 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04006424 struct btrfs_trans_handle *trans;
6425 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05006426 struct inode *inode = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006427 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04006428 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006429 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006430
Josef Bacik9ed74f22009-09-11 16:12:44 -04006431 /*
6432 * 2 for inode item and ref
6433 * 2 for dir items
6434 * 1 for xattr if selinux is on
6435 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006436 trans = btrfs_start_transaction(root, 5);
6437 if (IS_ERR(trans))
6438 return PTR_ERR(trans);
Josef Bacik9ed74f22009-09-11 16:12:44 -04006439
Nikolay Borisov543068a2020-12-07 17:32:33 +02006440 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006441 if (err)
6442 goto out_unlock;
6443
Josef Bacikaec74772008-07-24 12:12:38 -04006444 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006445 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6446 mode, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006447 if (IS_ERR(inode)) {
6448 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006449 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006450 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04006451 }
Casey Schauflerad19db72011-12-15 10:09:07 -05006452 /*
6453 * If the active LSM wants to access the inode during
6454 * d_instantiate it needs these. Smack checks to see
6455 * if the filesystem supports xattrs by looking at the
6456 * ops vector.
6457 */
6458 inode->i_fop = &btrfs_file_operations;
6459 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07006460 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07006461
6462 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6463 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006464 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07006465
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006466 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07006467 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006468 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05006469
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006470 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6471 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006472 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006473 goto out_unlock;
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006474
Al Viro1e2e5472018-05-04 08:23:01 -04006475 d_instantiate_new(dentry, inode);
Filipe Brandenburger43baa572012-11-30 03:40:09 +00006476
Chris Mason39279cc2007-06-12 06:35:45 -04006477out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006478 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006479 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04006480 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006481 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006482 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006483 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006484 return err;
6485}
6486
6487static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6488 struct dentry *dentry)
6489{
Filipe Manana271dba452016-01-05 16:24:05 +00006490 struct btrfs_trans_handle *trans = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006491 struct btrfs_root *root = BTRFS_I(dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00006492 struct inode *inode = d_inode(old_dentry);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006493 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason00e4e6b2008-08-05 11:18:09 -04006494 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04006495 int err;
6496 int drop_inode = 0;
6497
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006498 /* do not allow sys_link's with other subvols of the same device */
Misono Tomohiro4fd786e2018-08-06 14:25:24 +09006499 if (root->root_key.objectid != BTRFS_I(inode)->root->root_key.objectid)
Mark Fasheh3ab35642011-03-22 17:20:26 +00006500 return -EXDEV;
TARUISI Hiroaki4a8be422009-11-12 07:14:26 +00006501
Mark Fashehf1863732012-08-08 11:32:27 -07006502 if (inode->i_nlink >= BTRFS_LINK_MAX)
Al Viroc055e992011-03-04 17:15:18 +00006503 return -EMLINK;
Josef Bacik9ed74f22009-09-11 16:12:44 -04006504
Nikolay Borisov877574e2017-02-20 13:50:33 +02006505 err = btrfs_set_inode_index(BTRFS_I(dir), &index);
Josef Bacikaec74772008-07-24 12:12:38 -04006506 if (err)
6507 goto fail;
6508
Yan, Zhenga22285a2010-05-16 10:48:46 -04006509 /*
Miao Xie7e6b6462011-02-18 09:21:17 +00006510 * 2 items for inode and inode ref
Yan, Zhenga22285a2010-05-16 10:48:46 -04006511 * 2 items for dir items
Miao Xie7e6b6462011-02-18 09:21:17 +00006512 * 1 item for parent inode
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006513 * 1 item for orphan item deletion if O_TMPFILE
Yan, Zhenga22285a2010-05-16 10:48:46 -04006514 */
Omar Sandoval399b0bb2018-05-11 13:13:40 -07006515 trans = btrfs_start_transaction(root, inode->i_nlink ? 5 : 6);
Yan, Zhenga22285a2010-05-16 10:48:46 -04006516 if (IS_ERR(trans)) {
6517 err = PTR_ERR(trans);
Filipe Manana271dba452016-01-05 16:24:05 +00006518 trans = NULL;
Yan, Zhenga22285a2010-05-16 10:48:46 -04006519 goto fail;
6520 }
Chris Mason5f39d392007-10-15 16:14:19 -04006521
Miao Xie67de1172013-12-26 13:07:06 +08006522 /* There are several dir indexes for this inode, clear the cache. */
6523 BTRFS_I(inode)->dir_index = 0ULL;
Zach Brown8b558c52013-10-16 12:10:34 -07006524 inc_nlink(inode);
Josef Bacik0c4d2d92012-04-05 15:03:02 -04006525 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07006526 inode->i_ctime = current_time(inode);
Al Viro7de9c6ee2010-10-23 11:11:40 -04006527 ihold(inode);
Josef Bacike9976152012-10-11 15:53:56 -04006528 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
Josef Bacikaec74772008-07-24 12:12:38 -04006529
Nikolay Borisovcef415a2017-02-20 13:51:09 +02006530 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry, BTRFS_I(inode),
6531 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04006532
Yan, Zhenga5719522009-09-24 09:17:31 -04006533 if (err) {
Chris Mason39279cc2007-06-12 06:35:45 -04006534 drop_inode = 1;
Yan, Zhenga5719522009-09-24 09:17:31 -04006535 } else {
Al Viro10d9f302011-07-16 23:09:10 -04006536 struct dentry *parent = dentry->d_parent;
Filipe Mananad4682ba2018-06-11 19:24:28 +01006537
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006538 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01006539 if (err)
6540 goto fail;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006541 if (inode->i_nlink == 1) {
6542 /*
6543 * If new hard link count is 1, it's a file created
6544 * with open(2) O_TMPFILE flag.
6545 */
Nikolay Borisov3d6ae7b2017-02-20 13:50:58 +02006546 err = btrfs_orphan_del(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01006547 if (err)
6548 goto fail;
6549 }
Al Viro08c422c2011-12-23 07:58:13 -05006550 d_instantiate(dentry, inode);
Filipe Manana75b463d2020-08-11 12:43:48 +01006551 btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
Yan, Zhenga5719522009-09-24 09:17:31 -04006552 }
Chris Mason39279cc2007-06-12 06:35:45 -04006553
Chris Mason1832a6d2007-12-21 16:27:21 -05006554fail:
Filipe Manana271dba452016-01-05 16:24:05 +00006555 if (trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006556 btrfs_end_transaction(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006557 if (drop_inode) {
6558 inode_dec_link_count(inode);
6559 iput(inode);
6560 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006561 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006562 return err;
6563}
6564
Al Viro18bb1db2011-07-26 01:41:39 -04006565static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Chris Mason39279cc2007-06-12 06:35:45 -04006566{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006567 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Masonb9d86662008-05-02 16:13:49 -04006568 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006569 struct btrfs_trans_handle *trans;
6570 struct btrfs_root *root = BTRFS_I(dir)->root;
6571 int err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04006572 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04006573 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04006574
Josef Bacik9ed74f22009-09-11 16:12:44 -04006575 /*
6576 * 2 items for inode and ref
6577 * 2 items for dir items
6578 * 1 for xattr if selinux is on
6579 */
Yan, Zhenga22285a2010-05-16 10:48:46 -04006580 trans = btrfs_start_transaction(root, 5);
6581 if (IS_ERR(trans))
6582 return PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04006583
Nikolay Borisov543068a2020-12-07 17:32:33 +02006584 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08006585 if (err)
6586 goto out_fail;
6587
Josef Bacikaec74772008-07-24 12:12:38 -04006588 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01006589 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6590 S_IFDIR | mode, &index);
Chris Mason39279cc2007-06-12 06:35:45 -04006591 if (IS_ERR(inode)) {
6592 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04006593 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04006594 goto out_fail;
6595 }
Chris Mason5f39d392007-10-15 16:14:19 -04006596
Chris Masonb0d5d102014-09-08 13:08:51 -07006597 /* these must be set before we unlock the inode */
6598 inode->i_op = &btrfs_dir_inode_operations;
6599 inode->i_fop = &btrfs_dir_file_operations;
Josef Bacik33268ea2008-07-24 12:16:36 -04006600
Eric Paris2a7dba32011-02-01 11:05:39 -05006601 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
Josef Bacik33268ea2008-07-24 12:16:36 -04006602 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006603 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04006604
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02006605 btrfs_i_size_write(BTRFS_I(inode), 0);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02006606 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04006607 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006608 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006609
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02006610 err = btrfs_add_link(trans, BTRFS_I(dir), BTRFS_I(inode),
6611 dentry->d_name.name,
6612 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04006613 if (err)
Al Viro32955c52018-05-16 12:20:05 -04006614 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04006615
Al Viro1e2e5472018-05-04 08:23:01 -04006616 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04006617
6618out_fail:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04006619 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04006620 if (err && inode) {
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006621 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04006622 discard_new_inode(inode);
Wang Shilongc7cfb8a2014-12-24 14:45:30 +08006623 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04006624 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04006625 return err;
6626}
6627
Chris Masonc8b97812008-10-29 14:49:59 -04006628static noinline int uncompress_inline(struct btrfs_path *path,
Byongho Leee40da0e2015-05-19 23:46:45 +09006629 struct page *page,
Chris Masonc8b97812008-10-29 14:49:59 -04006630 size_t pg_offset, u64 extent_offset,
6631 struct btrfs_file_extent_item *item)
6632{
6633 int ret;
6634 struct extent_buffer *leaf = path->nodes[0];
6635 char *tmp;
6636 size_t max_size;
6637 unsigned long inline_size;
6638 unsigned long ptr;
Li Zefan261507a02010-12-17 14:21:50 +08006639 int compress_type;
Chris Masonc8b97812008-10-29 14:49:59 -04006640
6641 WARN_ON(pg_offset != 0);
Li Zefan261507a02010-12-17 14:21:50 +08006642 compress_type = btrfs_file_extent_compression(leaf, item);
Chris Masonc8b97812008-10-29 14:49:59 -04006643 max_size = btrfs_file_extent_ram_bytes(leaf, item);
6644 inline_size = btrfs_file_extent_inline_item_len(leaf,
Ross Kirkdd3cc162013-09-16 15:58:09 +01006645 btrfs_item_nr(path->slots[0]));
Chris Masonc8b97812008-10-29 14:49:59 -04006646 tmp = kmalloc(inline_size, GFP_NOFS);
Tsutomu Itoh8d413712011-04-25 19:43:52 -04006647 if (!tmp)
6648 return -ENOMEM;
Chris Masonc8b97812008-10-29 14:49:59 -04006649 ptr = btrfs_file_extent_inline_start(item);
6650
6651 read_extent_buffer(leaf, tmp, ptr, inline_size);
6652
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006653 max_size = min_t(unsigned long, PAGE_SIZE, max_size);
Li Zefan261507a02010-12-17 14:21:50 +08006654 ret = btrfs_decompress(compress_type, tmp, page,
6655 extent_offset, inline_size, max_size);
Zygo Blaxelle1699d22017-03-10 16:45:44 -05006656
6657 /*
6658 * decompression code contains a memset to fill in any space between the end
6659 * of the uncompressed data and the end of max_size in case the decompressed
6660 * data ends up shorter than ram_bytes. That doesn't cover the hole between
6661 * the end of an inline extent and the beginning of the next block, so we
6662 * cover that region here.
6663 */
6664
6665 if (max_size + pg_offset < PAGE_SIZE) {
6666 char *map = kmap(page);
6667 memset(map + pg_offset + max_size, 0, PAGE_SIZE - max_size - pg_offset);
6668 kunmap(page);
6669 }
Chris Masonc8b97812008-10-29 14:49:59 -04006670 kfree(tmp);
Zach Brown166ae5a2014-05-09 17:15:10 -04006671 return ret;
Chris Masonc8b97812008-10-29 14:49:59 -04006672}
6673
Omar Sandoval39b07b52019-12-02 17:34:23 -08006674/**
6675 * btrfs_get_extent - Lookup the first extent overlapping a range in a file.
6676 * @inode: file to search in
6677 * @page: page to read extent data into if the extent is inline
6678 * @pg_offset: offset into @page to copy to
6679 * @start: file offset
6680 * @len: length of range starting at @start
Chris Masond352ac62008-09-29 15:18:18 -04006681 *
Omar Sandoval39b07b52019-12-02 17:34:23 -08006682 * This returns the first &struct extent_map which overlaps with the given
6683 * range, reading it from the B-tree and caching it if necessary. Note that
6684 * there may be more extents which overlap the given range after the returned
6685 * extent_map.
6686 *
6687 * If @page is not NULL and the extent is inline, this also reads the extent
6688 * data directly into the page and marks the extent up to date in the io_tree.
6689 *
6690 * Return: ERR_PTR on error, non-NULL extent_map on success.
Chris Masond352ac62008-09-29 15:18:18 -04006691 */
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006692struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
Omar Sandoval39b07b52019-12-02 17:34:23 -08006693 struct page *page, size_t pg_offset,
6694 u64 start, u64 len)
Chris Masona52d9a82007-08-27 16:49:44 -04006695{
David Sterba3ffbd682018-06-29 10:56:42 +02006696 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006697 int ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006698 u64 extent_start = 0;
6699 u64 extent_end = 0;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006700 u64 objectid = btrfs_ino(inode);
Linus Torvalds7e74e232019-05-01 12:19:20 -07006701 int extent_type = -1;
Chris Masonf4219502008-07-22 11:18:09 -04006702 struct btrfs_path *path = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006703 struct btrfs_root *root = inode->root;
Chris Masona52d9a82007-08-27 16:49:44 -04006704 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04006705 struct extent_buffer *leaf;
6706 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04006707 struct extent_map *em = NULL;
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006708 struct extent_map_tree *em_tree = &inode->extent_tree;
6709 struct extent_io_tree *io_tree = &inode->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04006710
Chris Mason890871b2009-09-02 16:24:52 -04006711 read_lock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006712 em = lookup_extent_mapping(em_tree, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006713 read_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -05006714
Chris Masona52d9a82007-08-27 16:49:44 -04006715 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04006716 if (em->start > start || em->start + em->len <= start)
6717 free_extent_map(em);
6718 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05006719 free_extent_map(em);
6720 else
6721 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006722 }
David Sterba172ddd62011-04-21 00:48:27 +02006723 em = alloc_extent_map();
Chris Masona52d9a82007-08-27 16:49:44 -04006724 if (!em) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006725 ret = -ENOMEM;
Chris Masond1310b22008-01-24 16:13:08 -05006726 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04006727 }
Chris Masond1310b22008-01-24 16:13:08 -05006728 em->start = EXTENT_MAP_HOLE;
Chris Mason445a6942008-11-10 11:53:33 -05006729 em->orig_start = EXTENT_MAP_HOLE;
Chris Masond1310b22008-01-24 16:13:08 -05006730 em->len = (u64)-1;
Chris Masonc8b97812008-10-29 14:49:59 -04006731 em->block_len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04006732
Liu Bobee6ec82018-08-17 05:05:28 +08006733 path = btrfs_alloc_path();
Chris Masonf4219502008-07-22 11:18:09 -04006734 if (!path) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006735 ret = -ENOMEM;
Liu Bobee6ec82018-08-17 05:05:28 +08006736 goto out;
Chris Masonf4219502008-07-22 11:18:09 -04006737 }
6738
Liu Bobee6ec82018-08-17 05:05:28 +08006739 /* Chances are we'll be called again, so go ahead and do readahead */
6740 path->reada = READA_FORWARD;
Josef Bacik4d7240f2020-10-23 09:58:09 -04006741
6742 /*
6743 * The same explanation in load_free_space_cache applies here as well,
6744 * we only read when we're loading the free space cache, and at that
6745 * point the commit_root has everything we need.
6746 */
6747 if (btrfs_is_free_space_inode(inode)) {
6748 path->search_commit_root = 1;
6749 path->skip_locking = 1;
6750 }
Josef Bacik51899412020-08-20 11:46:01 -04006751
Nikolay Borisov5c9a7022017-12-01 11:19:40 +02006752 ret = btrfs_lookup_file_extent(NULL, root, path, objectid, start, 0);
Chris Masona52d9a82007-08-27 16:49:44 -04006753 if (ret < 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006754 goto out;
Nikolay Borisovb8eeab72018-12-17 11:49:00 +02006755 } else if (ret > 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04006756 if (path->slots[0] == 0)
6757 goto not_found;
6758 path->slots[0]--;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006759 ret = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04006760 }
6761
Chris Mason5f39d392007-10-15 16:14:19 -04006762 leaf = path->nodes[0];
6763 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04006764 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04006765 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason5f39d392007-10-15 16:14:19 -04006766 if (found_key.objectid != objectid ||
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006767 found_key.type != BTRFS_EXTENT_DATA_KEY) {
Josef Bacik25a50342013-10-14 12:08:38 -04006768 /*
6769 * If we backup past the first extent we want to move forward
6770 * and see if there is an extent in front of us, otherwise we'll
6771 * say there is a hole for our whole search range which can
6772 * cause problems.
6773 */
6774 extent_end = start;
6775 goto next;
Chris Masona52d9a82007-08-27 16:49:44 -04006776 }
6777
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006778 extent_type = btrfs_file_extent_type(leaf, item);
Chris Mason5f39d392007-10-15 16:14:19 -04006779 extent_start = found_key.offset;
Filipe Mananaa5eeb3d2020-03-09 12:41:06 +00006780 extent_end = btrfs_file_extent_end(path);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006781 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6782 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006783 /* Only regular file could have regular/prealloc extent */
6784 if (!S_ISREG(inode->vfs_inode.i_mode)) {
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006785 ret = -EUCLEAN;
Qu Wenruo6bf9e4b2019-03-13 13:55:11 +08006786 btrfs_crit(fs_info,
6787 "regular/prealloc extent found for non-regular inode %llu",
6788 btrfs_ino(inode));
6789 goto out;
6790 }
Liu Bo09ed2f12017-03-10 11:09:48 -08006791 trace_btrfs_get_extent_show_fi_regular(inode, leaf, item,
6792 extent_start);
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006793 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Liu Bo09ed2f12017-03-10 11:09:48 -08006794 trace_btrfs_get_extent_show_fi_inline(inode, leaf, item,
6795 path->slots[0],
6796 extent_start);
Yan Zheng9036c102008-10-30 14:19:41 -04006797 }
Josef Bacik25a50342013-10-14 12:08:38 -04006798next:
Yan Zheng9036c102008-10-30 14:19:41 -04006799 if (start >= extent_end) {
6800 path->slots[0]++;
6801 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6802 ret = btrfs_next_leaf(root, path);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006803 if (ret < 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006804 goto out;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006805 else if (ret > 0)
Yan Zheng9036c102008-10-30 14:19:41 -04006806 goto not_found;
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006807
Yan Zheng9036c102008-10-30 14:19:41 -04006808 leaf = path->nodes[0];
Chris Masona52d9a82007-08-27 16:49:44 -04006809 }
Yan Zheng9036c102008-10-30 14:19:41 -04006810 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6811 if (found_key.objectid != objectid ||
6812 found_key.type != BTRFS_EXTENT_DATA_KEY)
6813 goto not_found;
6814 if (start + len <= found_key.offset)
6815 goto not_found;
Wang Shilonge2eca692014-07-17 11:44:14 +08006816 if (start > found_key.offset)
6817 goto next;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006818
6819 /* New extent overlaps with existing one */
Yan Zheng9036c102008-10-30 14:19:41 -04006820 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006821 em->orig_start = start;
Yan Zheng9036c102008-10-30 14:19:41 -04006822 em->len = found_key.offset - start;
Nikolay Borisov02a033d2018-12-17 10:36:02 +02006823 em->block_start = EXTENT_MAP_HOLE;
6824 goto insert;
Yan Zheng9036c102008-10-30 14:19:41 -04006825 }
6826
Omar Sandoval39b07b52019-12-02 17:34:23 -08006827 btrfs_extent_item_to_extent_map(inode, path, item, !page, em);
Filipe Manana7ffbb592014-06-09 03:48:05 +01006828
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006829 if (extent_type == BTRFS_FILE_EXTENT_REG ||
6830 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Masona52d9a82007-08-27 16:49:44 -04006831 goto insert;
Nikolay Borisov694c12e2018-12-17 10:35:59 +02006832 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04006833 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04006834 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04006835 size_t size;
6836 size_t extent_offset;
6837 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04006838
Omar Sandoval39b07b52019-12-02 17:34:23 -08006839 if (!page)
Yan689f9342007-10-29 11:41:07 -04006840 goto out;
Yan689f9342007-10-29 11:41:07 -04006841
Qu Wenruoe41ca582018-06-06 15:41:49 +08006842 size = btrfs_file_extent_ram_bytes(leaf, item);
Yan Zheng9036c102008-10-30 14:19:41 -04006843 extent_offset = page_offset(page) + pg_offset - extent_start;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006844 copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6845 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04006846 em->start = extent_start + extent_offset;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006847 em->len = ALIGN(copy_size, fs_info->sectorsize);
Josef Bacikb4939682012-12-03 10:31:19 -05006848 em->orig_block_len = em->len;
Josef Bacik70c8a912012-10-11 16:54:30 -04006849 em->orig_start = em->start;
Yan689f9342007-10-29 11:41:07 -04006850 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Liu Boe49aabd2018-08-25 13:47:09 +08006851
Edmund Nadolskibf46f522017-11-20 13:24:49 -07006852 if (!PageUptodate(page)) {
Li Zefan261507a02010-12-17 14:21:50 +08006853 if (btrfs_file_extent_compression(leaf, item) !=
6854 BTRFS_COMPRESS_NONE) {
Byongho Leee40da0e2015-05-19 23:46:45 +09006855 ret = uncompress_inline(path, page, pg_offset,
Chris Masonc8b97812008-10-29 14:49:59 -04006856 extent_offset, item);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006857 if (ret)
Zach Brown166ae5a2014-05-09 17:15:10 -04006858 goto out;
Chris Masonc8b97812008-10-29 14:49:59 -04006859 } else {
6860 map = kmap(page);
6861 read_extent_buffer(leaf, map + pg_offset, ptr,
6862 copy_size);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006863 if (pg_offset + copy_size < PAGE_SIZE) {
Chris Mason93c82d52009-09-11 12:36:29 -04006864 memset(map + pg_offset + copy_size, 0,
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006865 PAGE_SIZE - pg_offset -
Chris Mason93c82d52009-09-11 12:36:29 -04006866 copy_size);
6867 }
Chris Masonc8b97812008-10-29 14:49:59 -04006868 kunmap(page);
6869 }
Chris Mason179e29e2007-11-01 11:28:41 -04006870 flush_dcache_page(page);
Chris Masona52d9a82007-08-27 16:49:44 -04006871 }
Chris Masond1310b22008-01-24 16:13:08 -05006872 set_extent_uptodate(io_tree, em->start,
Arne Jansen507903b2011-04-06 10:02:20 +00006873 extent_map_end(em) - 1, NULL, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04006874 goto insert;
Chris Masona52d9a82007-08-27 16:49:44 -04006875 }
6876not_found:
6877 em->start = start;
Josef Bacik70c8a912012-10-11 16:54:30 -04006878 em->orig_start = start;
Chris Masond1310b22008-01-24 16:13:08 -05006879 em->len = len;
Chris Mason5f39d392007-10-15 16:14:19 -04006880 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04006881insert:
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006882 ret = 0;
David Sterbab3b4aa72011-04-21 01:20:15 +02006883 btrfs_release_path(path);
Chris Masond1310b22008-01-24 16:13:08 -05006884 if (em->start > start || extent_map_end(em) <= start) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04006885 btrfs_err(fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04006886 "bad extent! em: [%llu %llu] passed [%llu %llu]",
6887 em->start, em->len, start, len);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006888 ret = -EIO;
Chris Masona52d9a82007-08-27 16:49:44 -04006889 goto out;
6890 }
Chris Masond1310b22008-01-24 16:13:08 -05006891
Chris Mason890871b2009-09-02 16:24:52 -04006892 write_lock(&em_tree->lock);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006893 ret = btrfs_add_extent_mapping(fs_info, em_tree, &em, start, len);
Chris Mason890871b2009-09-02 16:24:52 -04006894 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04006895out:
Liu Boc6414282018-08-23 07:36:17 +08006896 btrfs_free_path(path);
liubo1abe9b82011-03-24 11:18:59 +00006897
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006898 trace_btrfs_get_extent(root, inode, em);
liubo1abe9b82011-03-24 11:18:59 +00006899
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006900 if (ret) {
Chris Masona52d9a82007-08-27 16:49:44 -04006901 free_extent_map(em);
Nikolay Borisov1028d1c2020-08-03 12:58:46 +03006902 return ERR_PTR(ret);
Chris Masona52d9a82007-08-27 16:49:44 -04006903 }
6904 return em;
6905}
6906
Nikolay Borisovfc4f21b12017-02-20 13:51:06 +02006907struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
Nikolay Borisov4ab47a82018-12-12 09:42:32 +02006908 u64 start, u64 len)
Chris Masonec29ed52011-02-23 16:23:20 -05006909{
6910 struct extent_map *em;
6911 struct extent_map *hole_em = NULL;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006912 u64 delalloc_start = start;
Chris Masonec29ed52011-02-23 16:23:20 -05006913 u64 end;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006914 u64 delalloc_len;
6915 u64 delalloc_end;
Chris Masonec29ed52011-02-23 16:23:20 -05006916 int err = 0;
6917
Omar Sandoval39b07b52019-12-02 17:34:23 -08006918 em = btrfs_get_extent(inode, NULL, 0, start, len);
Chris Masonec29ed52011-02-23 16:23:20 -05006919 if (IS_ERR(em))
6920 return em;
Dan Carpenter99862772017-04-11 11:57:15 +03006921 /*
6922 * If our em maps to:
6923 * - a hole or
6924 * - a pre-alloc extent,
6925 * there might actually be delalloc bytes behind it.
6926 */
6927 if (em->block_start != EXTENT_MAP_HOLE &&
6928 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6929 return em;
6930 else
6931 hole_em = em;
Chris Masonec29ed52011-02-23 16:23:20 -05006932
6933 /* check to see if we've wrapped (len == -1 or similar) */
6934 end = start + len;
6935 if (end < start)
6936 end = (u64)-1;
6937 else
6938 end -= 1;
6939
6940 em = NULL;
6941
6942 /* ok, we didn't find anything, lets look for delalloc */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006943 delalloc_len = count_range_bits(&inode->io_tree, &delalloc_start,
Chris Masonec29ed52011-02-23 16:23:20 -05006944 end, len, EXTENT_DELALLOC, 1);
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006945 delalloc_end = delalloc_start + delalloc_len;
6946 if (delalloc_end < delalloc_start)
6947 delalloc_end = (u64)-1;
Chris Masonec29ed52011-02-23 16:23:20 -05006948
6949 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006950 * We didn't find anything useful, return the original results from
6951 * get_extent()
Chris Masonec29ed52011-02-23 16:23:20 -05006952 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006953 if (delalloc_start > end || delalloc_end <= start) {
Chris Masonec29ed52011-02-23 16:23:20 -05006954 em = hole_em;
6955 hole_em = NULL;
6956 goto out;
6957 }
6958
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006959 /*
6960 * Adjust the delalloc_start to make sure it doesn't go backwards from
6961 * the start they passed in
Chris Masonec29ed52011-02-23 16:23:20 -05006962 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006963 delalloc_start = max(start, delalloc_start);
6964 delalloc_len = delalloc_end - delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05006965
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006966 if (delalloc_len > 0) {
6967 u64 hole_start;
Nikolay Borisov02950af2018-12-12 09:42:34 +02006968 u64 hole_len;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006969 const u64 hole_end = extent_map_end(hole_em);
Chris Masonec29ed52011-02-23 16:23:20 -05006970
David Sterba172ddd62011-04-21 00:48:27 +02006971 em = alloc_extent_map();
Chris Masonec29ed52011-02-23 16:23:20 -05006972 if (!em) {
6973 err = -ENOMEM;
6974 goto out;
6975 }
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006976
6977 ASSERT(hole_em);
6978 /*
6979 * When btrfs_get_extent can't find anything it returns one
6980 * huge hole
6981 *
6982 * Make sure what it found really fits our range, and adjust to
6983 * make sure it is based on the start from the caller
6984 */
6985 if (hole_end <= start || hole_em->start > end) {
6986 free_extent_map(hole_em);
6987 hole_em = NULL;
6988 } else {
6989 hole_start = max(hole_em->start, start);
6990 hole_len = hole_end - hole_start;
6991 }
6992
6993 if (hole_em && delalloc_start > hole_start) {
6994 /*
6995 * Our hole starts before our delalloc, so we have to
6996 * return just the parts of the hole that go until the
6997 * delalloc starts
Chris Masonec29ed52011-02-23 16:23:20 -05006998 */
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02006999 em->len = min(hole_len, delalloc_start - hole_start);
Chris Masonec29ed52011-02-23 16:23:20 -05007000 em->start = hole_start;
7001 em->orig_start = hole_start;
7002 /*
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007003 * Don't adjust block start at all, it is fixed at
7004 * EXTENT_MAP_HOLE
Chris Masonec29ed52011-02-23 16:23:20 -05007005 */
7006 em->block_start = hole_em->block_start;
7007 em->block_len = hole_len;
Liu Bof9e4fb52013-01-07 10:10:12 +00007008 if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7009 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
Chris Masonec29ed52011-02-23 16:23:20 -05007010 } else {
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007011 /*
7012 * Hole is out of passed range or it starts after
7013 * delalloc range
7014 */
7015 em->start = delalloc_start;
7016 em->len = delalloc_len;
7017 em->orig_start = delalloc_start;
Chris Masonec29ed52011-02-23 16:23:20 -05007018 em->block_start = EXTENT_MAP_DELALLOC;
Nikolay Borisovf3714ef2018-12-12 09:42:33 +02007019 em->block_len = delalloc_len;
Chris Masonec29ed52011-02-23 16:23:20 -05007020 }
Nikolay Borisovbf8d32b2017-12-01 11:19:43 +02007021 } else {
Chris Masonec29ed52011-02-23 16:23:20 -05007022 return hole_em;
7023 }
7024out:
7025
7026 free_extent_map(hole_em);
7027 if (err) {
7028 free_extent_map(em);
7029 return ERR_PTR(err);
7030 }
7031 return em;
7032}
7033
Nikolay Borisov64f54182020-06-03 08:55:31 +03007034static struct extent_map *btrfs_create_dio_extent(struct btrfs_inode *inode,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007035 const u64 start,
7036 const u64 len,
7037 const u64 orig_start,
7038 const u64 block_start,
7039 const u64 block_len,
7040 const u64 orig_block_len,
7041 const u64 ram_bytes,
7042 const int type)
7043{
7044 struct extent_map *em = NULL;
7045 int ret;
7046
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007047 if (type != BTRFS_ORDERED_NOCOW) {
Nikolay Borisov64f54182020-06-03 08:55:31 +03007048 em = create_io_em(inode, start, len, orig_start, block_start,
7049 block_len, orig_block_len, ram_bytes,
Liu Bo6f9994d2017-01-31 07:50:22 -08007050 BTRFS_COMPRESS_NONE, /* compress_type */
7051 type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007052 if (IS_ERR(em))
7053 goto out;
7054 }
Nikolay Borisov64f54182020-06-03 08:55:31 +03007055 ret = btrfs_add_ordered_extent_dio(inode, start, block_start, len,
7056 block_len, type);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007057 if (ret) {
7058 if (em) {
7059 free_extent_map(em);
Nikolay Borisov64f54182020-06-03 08:55:31 +03007060 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007061 }
7062 em = ERR_PTR(ret);
7063 }
7064 out:
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007065
7066 return em;
7067}
7068
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007069static struct extent_map *btrfs_new_extent_direct(struct btrfs_inode *inode,
Josef Bacik4b46fce2010-05-23 11:00:55 -04007070 u64 start, u64 len)
7071{
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007072 struct btrfs_root *root = inode->root;
7073 struct btrfs_fs_info *fs_info = root->fs_info;
Josef Bacik70c8a912012-10-11 16:54:30 -04007074 struct extent_map *em;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007075 struct btrfs_key ins;
7076 u64 alloc_hint;
7077 int ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007078
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007079 alloc_hint = get_extent_allocation_hint(inode, start, len);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007080 ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
Jeff Mahoneyda170662016-06-15 09:22:56 -04007081 0, alloc_hint, &ins, 1, 1);
Josef Bacik00361582013-08-14 14:02:47 -04007082 if (ret)
7083 return ERR_PTR(ret);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007084
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007085 em = btrfs_create_dio_extent(inode, start, ins.offset, start,
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007086 ins.objectid, ins.offset, ins.offset,
Liu Bo6288d6e2017-02-21 12:12:58 -08007087 ins.offset, BTRFS_ORDERED_REGULAR);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007088 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana5f9a8a52016-05-12 13:53:36 +01007089 if (IS_ERR(em))
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007090 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset,
7091 1);
Josef Bacik00361582013-08-14 14:02:47 -04007092
Josef Bacik4b46fce2010-05-23 11:00:55 -04007093 return em;
7094}
7095
Chris Mason46bfbb52010-05-26 11:04:10 -04007096/*
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007097 * Check if we can do nocow write into the range [@offset, @offset + @len)
7098 *
7099 * @offset: File offset
7100 * @len: The length to write, will be updated to the nocow writeable
7101 * range
7102 * @orig_start: (optional) Return the original file offset of the file extent
7103 * @orig_len: (optional) Return the original on-disk length of the file extent
7104 * @ram_bytes: (optional) Return the ram_bytes of the file extent
Boris Burkova84d5d42020-08-18 11:00:05 -07007105 * @strict: if true, omit optimizations that might force us into unnecessary
7106 * cow. e.g., don't trust generation number.
Qu Wenruoe4ecaf92020-06-24 07:23:51 +08007107 *
7108 * This function will flush ordered extents in the range to ensure proper
7109 * nocow checks for (nowait == false) case.
7110 *
7111 * Return:
7112 * >0 and update @len if we can do nocow write
7113 * 0 if we can't do nocow write
7114 * <0 if error happened
7115 *
7116 * NOTE: This only checks the file extents, caller is responsible to wait for
7117 * any ordered extents.
Chris Mason46bfbb52010-05-26 11:04:10 -04007118 */
Josef Bacik00361582013-08-14 14:02:47 -04007119noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
Josef Bacik7ee9e442013-06-21 16:37:03 -04007120 u64 *orig_start, u64 *orig_block_len,
Boris Burkova84d5d42020-08-18 11:00:05 -07007121 u64 *ram_bytes, bool strict)
Chris Mason46bfbb52010-05-26 11:04:10 -04007122{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007123 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason46bfbb52010-05-26 11:04:10 -04007124 struct btrfs_path *path;
7125 int ret;
7126 struct extent_buffer *leaf;
7127 struct btrfs_root *root = BTRFS_I(inode)->root;
Miao Xie7b2b7082014-02-27 13:58:05 +08007128 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason46bfbb52010-05-26 11:04:10 -04007129 struct btrfs_file_extent_item *fi;
7130 struct btrfs_key key;
7131 u64 disk_bytenr;
7132 u64 backref_offset;
7133 u64 extent_end;
7134 u64 num_bytes;
7135 int slot;
7136 int found_type;
Josef Bacik7ee9e442013-06-21 16:37:03 -04007137 bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
Miao Xiee77751a2013-12-27 21:11:50 +08007138
Chris Mason46bfbb52010-05-26 11:04:10 -04007139 path = btrfs_alloc_path();
7140 if (!path)
7141 return -ENOMEM;
7142
David Sterbaf85b7372017-01-20 14:54:07 +01007143 ret = btrfs_lookup_file_extent(NULL, root, path,
7144 btrfs_ino(BTRFS_I(inode)), offset, 0);
Chris Mason46bfbb52010-05-26 11:04:10 -04007145 if (ret < 0)
7146 goto out;
7147
7148 slot = path->slots[0];
7149 if (ret == 1) {
7150 if (slot == 0) {
7151 /* can't find the item, must cow */
7152 ret = 0;
7153 goto out;
7154 }
7155 slot--;
7156 }
7157 ret = 0;
7158 leaf = path->nodes[0];
7159 btrfs_item_key_to_cpu(leaf, &key, slot);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02007160 if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
Chris Mason46bfbb52010-05-26 11:04:10 -04007161 key.type != BTRFS_EXTENT_DATA_KEY) {
7162 /* not our file or wrong item type, must cow */
7163 goto out;
7164 }
7165
7166 if (key.offset > offset) {
7167 /* Wrong offset, must cow */
7168 goto out;
7169 }
7170
7171 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7172 found_type = btrfs_file_extent_type(leaf, fi);
7173 if (found_type != BTRFS_FILE_EXTENT_REG &&
7174 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7175 /* not a regular extent, must cow */
7176 goto out;
7177 }
Josef Bacik7ee9e442013-06-21 16:37:03 -04007178
7179 if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7180 goto out;
7181
Miao Xiee77751a2013-12-27 21:11:50 +08007182 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7183 if (extent_end <= offset)
7184 goto out;
7185
Chris Mason46bfbb52010-05-26 11:04:10 -04007186 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Josef Bacik7ee9e442013-06-21 16:37:03 -04007187 if (disk_bytenr == 0)
7188 goto out;
7189
7190 if (btrfs_file_extent_compression(leaf, fi) ||
7191 btrfs_file_extent_encryption(leaf, fi) ||
7192 btrfs_file_extent_other_encoding(leaf, fi))
7193 goto out;
7194
Ethan Lien78d42952018-05-17 14:58:29 +08007195 /*
7196 * Do the same check as in btrfs_cross_ref_exist but without the
7197 * unnecessary search.
7198 */
Boris Burkova84d5d42020-08-18 11:00:05 -07007199 if (!strict &&
7200 (btrfs_file_extent_generation(leaf, fi) <=
7201 btrfs_root_last_snapshot(&root->root_item)))
Ethan Lien78d42952018-05-17 14:58:29 +08007202 goto out;
7203
Chris Mason46bfbb52010-05-26 11:04:10 -04007204 backref_offset = btrfs_file_extent_offset(leaf, fi);
7205
Josef Bacik7ee9e442013-06-21 16:37:03 -04007206 if (orig_start) {
7207 *orig_start = key.offset - backref_offset;
7208 *orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7209 *ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7210 }
Josef Bacikeb384b52013-04-24 16:32:55 -04007211
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007212 if (btrfs_extent_readonly(fs_info, disk_bytenr))
Chris Mason46bfbb52010-05-26 11:04:10 -04007213 goto out;
Miao Xie7b2b7082014-02-27 13:58:05 +08007214
7215 num_bytes = min(offset + *len, extent_end) - offset;
7216 if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7217 u64 range_end;
7218
Jeff Mahoneyda170662016-06-15 09:22:56 -04007219 range_end = round_up(offset + num_bytes,
7220 root->fs_info->sectorsize) - 1;
Miao Xie7b2b7082014-02-27 13:58:05 +08007221 ret = test_range_bit(io_tree, offset, range_end,
7222 EXTENT_DELALLOC, 0, NULL);
7223 if (ret) {
7224 ret = -EAGAIN;
7225 goto out;
7226 }
7227 }
7228
Josef Bacik1bda19e2013-10-18 12:10:36 -04007229 btrfs_release_path(path);
Chris Mason46bfbb52010-05-26 11:04:10 -04007230
7231 /*
7232 * look for other files referencing this extent, if we
7233 * find any we must cow
7234 */
Josef Bacik00361582013-08-14 14:02:47 -04007235
Liu Boe4c3b2d2017-01-30 12:25:28 -08007236 ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
Boris Burkova84d5d42020-08-18 11:00:05 -07007237 key.offset - backref_offset, disk_bytenr,
7238 strict);
Josef Bacik00361582013-08-14 14:02:47 -04007239 if (ret) {
7240 ret = 0;
7241 goto out;
7242 }
Chris Mason46bfbb52010-05-26 11:04:10 -04007243
7244 /*
7245 * adjust disk_bytenr and num_bytes to cover just the bytes
7246 * in this extent we are about to write. If there
7247 * are any csums in that range we have to cow in order
7248 * to keep the csums correct
7249 */
7250 disk_bytenr += backref_offset;
7251 disk_bytenr += offset - key.offset;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007252 if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7253 goto out;
Chris Mason46bfbb52010-05-26 11:04:10 -04007254 /*
7255 * all of the above have passed, it is safe to overwrite this extent
7256 * without cow
7257 */
Josef Bacikeb384b52013-04-24 16:32:55 -04007258 *len = num_bytes;
Chris Mason46bfbb52010-05-26 11:04:10 -04007259 ret = 1;
7260out:
7261 btrfs_free_path(path);
7262 return ret;
7263}
7264
Josef Bacikeb838e72012-07-31 16:28:48 -04007265static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007266 struct extent_state **cached_state, bool writing)
Josef Bacikeb838e72012-07-31 16:28:48 -04007267{
7268 struct btrfs_ordered_extent *ordered;
7269 int ret = 0;
7270
7271 while (1) {
7272 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbaff13db42015-12-03 14:30:40 +01007273 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007274 /*
7275 * We're concerned with the entire range that we're going to be
Nicholas D Steeves01327612016-05-19 21:18:45 -04007276 * doing DIO to, so we need to make sure there's no ordered
Josef Bacikeb838e72012-07-31 16:28:48 -04007277 * extents in this range.
7278 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02007279 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), lockstart,
Josef Bacikeb838e72012-07-31 16:28:48 -04007280 lockend - lockstart + 1);
7281
7282 /*
7283 * We need to make sure there are no buffered pages in this
7284 * range either, we could have raced between the invalidate in
7285 * generic_file_direct_write and locking the extent. The
7286 * invalidate needs to happen so that reads after a write do not
7287 * get stale data.
7288 */
Alex Gartrellfc4adbff2014-05-20 13:07:56 -07007289 if (!ordered &&
David Sterba051c98e2018-03-07 15:33:22 +01007290 (!writing || !filemap_range_has_page(inode->i_mapping,
7291 lockstart, lockend)))
Josef Bacikeb838e72012-07-31 16:28:48 -04007292 break;
7293
7294 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
David Sterbae43bbe52017-12-12 21:43:52 +01007295 cached_state);
Josef Bacikeb838e72012-07-31 16:28:48 -04007296
7297 if (ordered) {
Filipe Mananaade77022016-02-18 14:28:55 +00007298 /*
7299 * If we are doing a DIO read and the ordered extent we
7300 * found is for a buffered write, we can not wait for it
7301 * to complete and retry, because if we do so we can
7302 * deadlock with concurrent buffered writes on page
7303 * locks. This happens only if our DIO read covers more
7304 * than one extent map, if at this point has already
7305 * created an ordered extent for a previous extent map
7306 * and locked its range in the inode's io tree, and a
7307 * concurrent write against that previous extent map's
7308 * range and this range started (we unlock the ranges
7309 * in the io tree only when the bios complete and
7310 * buffered writes always lock pages before attempting
7311 * to lock range in the io tree).
7312 */
7313 if (writing ||
7314 test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
Nikolay Borisovc0a43602020-09-18 12:15:53 +03007315 btrfs_start_ordered_extent(ordered, 1);
Filipe Mananaade77022016-02-18 14:28:55 +00007316 else
7317 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007318 btrfs_put_ordered_extent(ordered);
7319 } else {
Josef Bacikeb838e72012-07-31 16:28:48 -04007320 /*
Filipe Mananab850ae12015-12-08 16:23:16 +00007321 * We could trigger writeback for this range (and wait
7322 * for it to complete) and then invalidate the pages for
7323 * this range (through invalidate_inode_pages2_range()),
7324 * but that can lead us to a deadlock with a concurrent
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007325 * call to readahead (a buffered read or a defrag call
Filipe Mananab850ae12015-12-08 16:23:16 +00007326 * triggered a readahead) on a page lock due to an
7327 * ordered dio extent we created before but did not have
7328 * yet a corresponding bio submitted (whence it can not
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07007329 * complete), which makes readahead wait for that
Filipe Mananab850ae12015-12-08 16:23:16 +00007330 * ordered extent to complete while holding a lock on
7331 * that page.
Josef Bacikeb838e72012-07-31 16:28:48 -04007332 */
Filipe Mananab850ae12015-12-08 16:23:16 +00007333 ret = -ENOTBLK;
Josef Bacikeb838e72012-07-31 16:28:48 -04007334 }
7335
Filipe Mananaade77022016-02-18 14:28:55 +00007336 if (ret)
7337 break;
7338
Josef Bacikeb838e72012-07-31 16:28:48 -04007339 cond_resched();
7340 }
7341
7342 return ret;
7343}
7344
Liu Bo6f9994d2017-01-31 07:50:22 -08007345/* The callers of this must take lock_extent() */
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007346static struct extent_map *create_io_em(struct btrfs_inode *inode, u64 start,
7347 u64 len, u64 orig_start, u64 block_start,
Liu Bo6f9994d2017-01-31 07:50:22 -08007348 u64 block_len, u64 orig_block_len,
7349 u64 ram_bytes, int compress_type,
7350 int type)
Josef Bacik69ffb542012-09-11 15:40:07 -04007351{
7352 struct extent_map_tree *em_tree;
7353 struct extent_map *em;
Josef Bacik69ffb542012-09-11 15:40:07 -04007354 int ret;
7355
Liu Bo6f9994d2017-01-31 07:50:22 -08007356 ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7357 type == BTRFS_ORDERED_COMPRESSED ||
7358 type == BTRFS_ORDERED_NOCOW ||
Liu Bo1af4a0a2017-02-13 15:35:09 -08007359 type == BTRFS_ORDERED_REGULAR);
Liu Bo6f9994d2017-01-31 07:50:22 -08007360
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007361 em_tree = &inode->extent_tree;
Josef Bacik69ffb542012-09-11 15:40:07 -04007362 em = alloc_extent_map();
7363 if (!em)
7364 return ERR_PTR(-ENOMEM);
7365
7366 em->start = start;
7367 em->orig_start = orig_start;
7368 em->len = len;
7369 em->block_len = block_len;
7370 em->block_start = block_start;
Josef Bacikb4939682012-12-03 10:31:19 -05007371 em->orig_block_len = orig_block_len;
Josef Bacikcc95bef2013-04-04 14:31:27 -04007372 em->ram_bytes = ram_bytes;
Josef Bacik70c8a912012-10-11 16:54:30 -04007373 em->generation = -1;
Josef Bacik69ffb542012-09-11 15:40:07 -04007374 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007375 if (type == BTRFS_ORDERED_PREALLOC) {
Josef Bacikb11e2342012-12-03 10:58:15 -05007376 set_bit(EXTENT_FLAG_FILLING, &em->flags);
Liu Bo1af4a0a2017-02-13 15:35:09 -08007377 } else if (type == BTRFS_ORDERED_COMPRESSED) {
Liu Bo6f9994d2017-01-31 07:50:22 -08007378 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7379 em->compress_type = compress_type;
7380 }
Josef Bacik69ffb542012-09-11 15:40:07 -04007381
7382 do {
Nikolay Borisov4b67c112020-06-03 08:55:05 +03007383 btrfs_drop_extent_cache(inode, em->start,
7384 em->start + em->len - 1, 0);
Josef Bacik69ffb542012-09-11 15:40:07 -04007385 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04007386 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik69ffb542012-09-11 15:40:07 -04007387 write_unlock(&em_tree->lock);
Liu Bo6f9994d2017-01-31 07:50:22 -08007388 /*
7389 * The caller has taken lock_extent(), who could race with us
7390 * to add em?
7391 */
Josef Bacik69ffb542012-09-11 15:40:07 -04007392 } while (ret == -EEXIST);
7393
7394 if (ret) {
7395 free_extent_map(em);
7396 return ERR_PTR(ret);
7397 }
7398
Liu Bo6f9994d2017-01-31 07:50:22 -08007399 /* em got 2 refs now, callers needs to do free_extent_map once. */
Josef Bacik69ffb542012-09-11 15:40:07 -04007400 return em;
7401}
7402
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007403
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007404static int btrfs_get_blocks_direct_write(struct extent_map **map,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007405 struct inode *inode,
7406 struct btrfs_dio_data *dio_data,
7407 u64 start, u64 len)
7408{
7409 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7410 struct extent_map *em = *map;
7411 int ret = 0;
7412
7413 /*
7414 * We don't allocate a new extent in the following cases
7415 *
7416 * 1) The inode is marked as NODATACOW. In this case we'll just use the
7417 * existing extent.
7418 * 2) The extent is marked as PREALLOC. We're good to go here and can
7419 * just use the extent.
7420 *
7421 */
7422 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7423 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7424 em->block_start != EXTENT_MAP_HOLE)) {
7425 int type;
7426 u64 block_start, orig_start, orig_block_len, ram_bytes;
7427
7428 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7429 type = BTRFS_ORDERED_PREALLOC;
7430 else
7431 type = BTRFS_ORDERED_NOCOW;
7432 len = min(len, em->len - (start - em->start));
7433 block_start = em->block_start + (start - em->start);
7434
7435 if (can_nocow_extent(inode, start, &len, &orig_start,
Boris Burkova84d5d42020-08-18 11:00:05 -07007436 &orig_block_len, &ram_bytes, false) == 1 &&
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007437 btrfs_inc_nocow_writers(fs_info, block_start)) {
7438 struct extent_map *em2;
7439
Nikolay Borisov64f54182020-06-03 08:55:31 +03007440 em2 = btrfs_create_dio_extent(BTRFS_I(inode), start, len,
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007441 orig_start, block_start,
7442 len, orig_block_len,
7443 ram_bytes, type);
7444 btrfs_dec_nocow_writers(fs_info, block_start);
7445 if (type == BTRFS_ORDERED_PREALLOC) {
7446 free_extent_map(em);
7447 *map = em = em2;
7448 }
7449
7450 if (em2 && IS_ERR(em2)) {
7451 ret = PTR_ERR(em2);
7452 goto out;
7453 }
7454 /*
7455 * For inode marked NODATACOW or extent marked PREALLOC,
7456 * use the existing or preallocated extent, so does not
7457 * need to adjust btrfs_space_info's bytes_may_use.
7458 */
Nikolay Borisov9db5d512020-06-03 08:55:38 +03007459 btrfs_free_reserved_data_space_noquota(fs_info, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007460 goto skip_cow;
7461 }
7462 }
7463
7464 /* this will cow the extent */
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007465 free_extent_map(em);
Nikolay Borisov9fc6f912020-06-03 08:55:32 +03007466 *map = em = btrfs_new_extent_direct(BTRFS_I(inode), start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007467 if (IS_ERR(em)) {
7468 ret = PTR_ERR(em);
7469 goto out;
7470 }
7471
7472 len = min(len, em->len - (start - em->start));
7473
7474skip_cow:
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007475 /*
7476 * Need to update the i_size under the extent lock so buffered
7477 * readers will get the updated i_size when we unlock.
7478 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007479 if (start + len > i_size_read(inode))
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007480 i_size_write(inode, start + len);
7481
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007482 dio_data->reserve -= len;
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007483out:
7484 return ret;
7485}
7486
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007487static int btrfs_dio_iomap_begin(struct inode *inode, loff_t start,
7488 loff_t length, unsigned int flags, struct iomap *iomap,
7489 struct iomap *srcmap)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007490{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007491 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik4b46fce2010-05-23 11:00:55 -04007492 struct extent_map *em;
Josef Bacikeb838e72012-07-31 16:28:48 -04007493 struct extent_state *cached_state = NULL;
chandan50745b02015-08-28 21:10:13 +05307494 struct btrfs_dio_data *dio_data = NULL;
Josef Bacikeb838e72012-07-31 16:28:48 -04007495 u64 lockstart, lockend;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007496 const bool write = !!(flags & IOMAP_WRITE);
Miao Xie09348562013-02-07 10:12:07 +00007497 int ret = 0;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007498 u64 len = length;
7499 bool unlock_extents = false;
Josef Bacikeb838e72012-07-31 16:28:48 -04007500
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007501 if (!write)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007502 len = min_t(u64, len, fs_info->sectorsize);
Josef Bacikeb838e72012-07-31 16:28:48 -04007503
Josef Bacikc3298612012-08-03 16:49:19 -04007504 lockstart = start;
7505 lockend = start + len - 1;
7506
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007507 /*
7508 * The generic stuff only does filemap_write_and_wait_range, which
7509 * isn't enough if we've written compressed pages to this area, so we
7510 * need to flush the dirty pages again to make absolutely sure that any
7511 * outstanding dirty pages are on disk.
7512 */
7513 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
7514 &BTRFS_I(inode)->runtime_flags)) {
7515 ret = filemap_fdatawrite_range(inode->i_mapping, start,
7516 start + length - 1);
7517 if (ret)
7518 return ret;
Josef Bacike1cbbfa2015-03-17 10:52:28 -04007519 }
7520
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007521 dio_data = kzalloc(sizeof(*dio_data), GFP_NOFS);
7522 if (!dio_data)
7523 return -ENOMEM;
7524
7525 dio_data->length = length;
7526 if (write) {
7527 dio_data->reserve = round_up(length, fs_info->sectorsize);
7528 ret = btrfs_delalloc_reserve_space(BTRFS_I(inode),
7529 &dio_data->data_reserved,
7530 start, dio_data->reserve);
7531 if (ret) {
7532 extent_changeset_free(dio_data->data_reserved);
7533 kfree(dio_data);
7534 return ret;
7535 }
7536 }
7537 iomap->private = dio_data;
7538
7539
Josef Bacikeb838e72012-07-31 16:28:48 -04007540 /*
7541 * If this errors out it's because we couldn't invalidate pagecache for
7542 * this range and we need to fallback to buffered.
7543 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007544 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, write)) {
Filipe Manana9c9464c2015-11-04 09:52:04 +00007545 ret = -ENOTBLK;
7546 goto err;
7547 }
Josef Bacikeb838e72012-07-31 16:28:48 -04007548
Omar Sandoval39b07b52019-12-02 17:34:23 -08007549 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Josef Bacikeb838e72012-07-31 16:28:48 -04007550 if (IS_ERR(em)) {
7551 ret = PTR_ERR(em);
7552 goto unlock_err;
7553 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04007554
7555 /*
7556 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7557 * io. INLINE is special, and we could probably kludge it in here, but
7558 * it's still buffered so for safety lets just fall back to the generic
7559 * buffered path.
7560 *
7561 * For COMPRESSED we _have_ to read the entire extent in so we can
7562 * decompress it, so there will be buffering required no matter what we
7563 * do, so go ahead and fallback to buffered.
7564 *
Nicholas D Steeves01327612016-05-19 21:18:45 -04007565 * We return -ENOTBLK because that's what makes DIO go ahead and go back
Josef Bacik4b46fce2010-05-23 11:00:55 -04007566 * to buffered IO. Don't blame me, this is the price we pay for using
7567 * the generic code.
7568 */
7569 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7570 em->block_start == EXTENT_MAP_INLINE) {
7571 free_extent_map(em);
Josef Bacikeb838e72012-07-31 16:28:48 -04007572 ret = -ENOTBLK;
7573 goto unlock_err;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007574 }
7575
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007576 len = min(len, em->len - (start - em->start));
7577 if (write) {
7578 ret = btrfs_get_blocks_direct_write(&em, inode, dio_data,
7579 start, len);
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007580 if (ret < 0)
7581 goto unlock_err;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007582 unlock_extents = true;
7583 /* Recalc len in case the new em is smaller than requested */
7584 len = min(len, em->len - (start - em->start));
Nikolay Borisovc5794e52018-05-02 15:19:33 +03007585 } else {
Nikolay Borisov1c8d0172018-05-02 15:19:32 +03007586 /*
7587 * We need to unlock only the end area that we aren't using.
7588 * The rest is going to be unlocked by the endio routine.
7589 */
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007590 lockstart = start + len;
7591 if (lockstart < lockend)
7592 unlock_extents = true;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007593 }
7594
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007595 if (unlock_extents)
7596 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
7597 lockstart, lockend, &cached_state);
7598 else
7599 free_extent_state(cached_state);
7600
7601 /*
7602 * Translate extent map information to iomap.
7603 * We trim the extents (and move the addr) even though iomap code does
7604 * that, since we have locked only the parts we are performing I/O in.
7605 */
7606 if ((em->block_start == EXTENT_MAP_HOLE) ||
7607 (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) && !write)) {
7608 iomap->addr = IOMAP_NULL_ADDR;
7609 iomap->type = IOMAP_HOLE;
7610 } else {
7611 iomap->addr = em->block_start + (start - em->start);
7612 iomap->type = IOMAP_MAPPED;
7613 }
7614 iomap->offset = start;
7615 iomap->bdev = fs_info->fs_devices->latest_bdev;
7616 iomap->length = len;
7617
Josef Bacik4b46fce2010-05-23 11:00:55 -04007618 free_extent_map(em);
7619
7620 return 0;
Josef Bacikeb838e72012-07-31 16:28:48 -04007621
7622unlock_err:
Omar Sandovale1821632019-08-15 14:04:04 -07007623 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7624 &cached_state);
Filipe Manana9c9464c2015-11-04 09:52:04 +00007625err:
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007626 if (dio_data) {
7627 btrfs_delalloc_release_space(BTRFS_I(inode),
7628 dio_data->data_reserved, start,
7629 dio_data->reserve, true);
7630 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->reserve);
7631 extent_changeset_free(dio_data->data_reserved);
7632 kfree(dio_data);
7633 }
7634 return ret;
7635}
7636
7637static int btrfs_dio_iomap_end(struct inode *inode, loff_t pos, loff_t length,
7638 ssize_t written, unsigned int flags, struct iomap *iomap)
7639{
7640 int ret = 0;
7641 struct btrfs_dio_data *dio_data = iomap->private;
7642 size_t submitted = dio_data->submitted;
7643 const bool write = !!(flags & IOMAP_WRITE);
7644
7645 if (!write && (iomap->type == IOMAP_HOLE)) {
7646 /* If reading from a hole, unlock and return */
7647 unlock_extent(&BTRFS_I(inode)->io_tree, pos, pos + length - 1);
7648 goto out;
7649 }
7650
7651 if (submitted < length) {
7652 pos += submitted;
7653 length -= submitted;
7654 if (write)
7655 __endio_write_update_ordered(BTRFS_I(inode), pos,
7656 length, false);
7657 else
7658 unlock_extent(&BTRFS_I(inode)->io_tree, pos,
7659 pos + length - 1);
7660 ret = -ENOTBLK;
7661 }
7662
7663 if (write) {
7664 if (dio_data->reserve)
7665 btrfs_delalloc_release_space(BTRFS_I(inode),
7666 dio_data->data_reserved, pos,
7667 dio_data->reserve, true);
7668 btrfs_delalloc_release_extents(BTRFS_I(inode), dio_data->length);
7669 extent_changeset_free(dio_data->data_reserved);
7670 }
7671out:
7672 kfree(dio_data);
7673 iomap->private = NULL;
7674
Josef Bacikeb838e72012-07-31 16:28:48 -04007675 return ret;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007676}
7677
Omar Sandoval769b4f22020-04-16 14:46:22 -07007678static void btrfs_dio_private_put(struct btrfs_dio_private *dip)
7679{
7680 /*
7681 * This implies a barrier so that stores to dio_bio->bi_status before
7682 * this and loads of dio_bio->bi_status after this are fully ordered.
7683 */
7684 if (!refcount_dec_and_test(&dip->refs))
7685 return;
7686
7687 if (bio_op(dip->dio_bio) == REQ_OP_WRITE) {
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007688 __endio_write_update_ordered(BTRFS_I(dip->inode),
7689 dip->logical_offset,
Omar Sandoval769b4f22020-04-16 14:46:22 -07007690 dip->bytes,
7691 !dip->dio_bio->bi_status);
7692 } else {
7693 unlock_extent(&BTRFS_I(dip->inode)->io_tree,
7694 dip->logical_offset,
7695 dip->logical_offset + dip->bytes - 1);
7696 }
7697
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007698 bio_endio(dip->dio_bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007699 kfree(dip);
7700}
7701
Omar Sandoval77d5d682020-04-16 14:46:25 -07007702static blk_status_t submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7703 int mirror_num,
7704 unsigned long bio_flags)
Miao Xie8b110e32014-09-12 18:44:03 +08007705{
Omar Sandoval77d5d682020-04-16 14:46:25 -07007706 struct btrfs_dio_private *dip = bio->bi_private;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04007707 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval58efbc92017-08-22 23:45:59 -07007708 blk_status_t ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007709
Mike Christie37226b22016-06-05 14:31:52 -05007710 BUG_ON(bio_op(bio) == REQ_OP_WRITE);
Miao Xie8b110e32014-09-12 18:44:03 +08007711
Omar Sandoval5c047a62020-04-16 14:46:24 -07007712 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Miao Xie8b110e32014-09-12 18:44:03 +08007713 if (ret)
Nikolay Borisovea057f62017-12-13 10:25:38 +02007714 return ret;
Miao Xie8b110e32014-09-12 18:44:03 +08007715
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007716 refcount_inc(&dip->refs);
Chris Mason08635ba2019-07-10 12:28:14 -07007717 ret = btrfs_map_bio(fs_info, bio, mirror_num);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007718 if (ret)
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007719 refcount_dec(&dip->refs);
Miao Xie8b110e32014-09-12 18:44:03 +08007720 return ret;
7721}
7722
Omar Sandoval769b4f22020-04-16 14:46:22 -07007723static blk_status_t btrfs_check_read_dio_bio(struct inode *inode,
7724 struct btrfs_io_bio *io_bio,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007725 const bool uptodate)
Miao Xie8b110e32014-09-12 18:44:03 +08007726{
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007727 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
7728 const u32 sectorsize = fs_info->sectorsize;
Josef Bacik7870d082017-05-05 11:57:15 -04007729 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007730 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7731 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
Liu Bo17347ce2017-05-15 15:33:27 -07007732 struct bio_vec bvec;
7733 struct bvec_iter iter;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007734 u64 start = io_bio->logical;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007735 u32 bio_offset = 0;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007736 blk_status_t err = BLK_STS_OK;
Miao Xiedc380ae2014-09-12 18:43:55 +08007737
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007738 __bio_for_each_segment(bvec, &io_bio->bio, iter, io_bio->iter) {
7739 unsigned int i, nr_sectors, pgoff;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307740
Liu Bo17347ce2017-05-15 15:33:27 -07007741 nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec.bv_len);
7742 pgoff = bvec.bv_offset;
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007743 for (i = 0; i < nr_sectors; i++) {
Liu Bo97bf5a52017-04-07 13:11:10 -07007744 ASSERT(pgoff < PAGE_SIZE);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007745 if (uptodate &&
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007746 (!csum || !check_data_csum(inode, io_bio,
7747 bio_offset, bvec.bv_page, pgoff))) {
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007748 clean_io_failure(fs_info, failure_tree, io_tree,
7749 start, bvec.bv_page,
7750 btrfs_ino(BTRFS_I(inode)),
7751 pgoff);
7752 } else {
7753 blk_status_t status;
Miao Xie8b110e32014-09-12 18:44:03 +08007754
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007755 ASSERT((start - io_bio->logical) < UINT_MAX);
Omar Sandoval77d5d682020-04-16 14:46:25 -07007756 status = btrfs_submit_read_repair(inode,
7757 &io_bio->bio,
7758 start - io_bio->logical,
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007759 bvec.bv_page, pgoff,
7760 start,
7761 start + sectorsize - 1,
Omar Sandoval77d5d682020-04-16 14:46:25 -07007762 io_bio->mirror_num,
7763 submit_dio_repair_bio);
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007764 if (status)
7765 err = status;
7766 }
7767 start += sectorsize;
Qu Wenruo7ffd27e2020-12-02 14:47:58 +08007768 ASSERT(bio_offset + sectorsize > bio_offset);
7769 bio_offset += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307770 pgoff += sectorsize;
Chandan Rajendra2dabb322016-01-21 15:55:55 +05307771 }
Kent Overstreet2c30c712013-11-07 12:20:26 -08007772 }
Miao Xiec1dc0892014-09-12 18:43:56 +08007773 return err;
7774}
7775
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007776static void __endio_write_update_ordered(struct btrfs_inode *inode,
Qu Wenruo524272602017-03-08 10:25:52 +08007777 const u64 offset, const u64 bytes,
7778 const bool uptodate)
Josef Bacik4b46fce2010-05-23 11:00:55 -04007779{
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007780 struct btrfs_fs_info *fs_info = inode->root->fs_info;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007781 struct btrfs_ordered_extent *ordered = NULL;
Qu Wenruo524272602017-03-08 10:25:52 +08007782 struct btrfs_workqueue *wq;
Filipe Manana14543772015-11-24 16:23:54 +00007783 u64 ordered_offset = offset;
7784 u64 ordered_bytes = bytes;
Naohiro Aota67c003f2017-09-01 17:59:07 +09007785 u64 last_offset;
Josef Bacik4b46fce2010-05-23 11:00:55 -04007786
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007787 if (btrfs_is_free_space_inode(inode))
Qu Wenruo524272602017-03-08 10:25:52 +08007788 wq = fs_info->endio_freespace_worker;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007789 else
Qu Wenruo524272602017-03-08 10:25:52 +08007790 wq = fs_info->endio_write_workers;
Qu Wenruo524272602017-03-08 10:25:52 +08007791
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007792 while (ordered_offset < offset + bytes) {
7793 last_offset = ordered_offset;
Nikolay Borisovb672b5c2020-06-03 08:55:24 +03007794 if (btrfs_dec_test_first_ordered_pending(inode, &ordered,
Nikolay Borisov70958212020-06-03 08:55:23 +03007795 &ordered_offset,
7796 ordered_bytes,
7797 uptodate)) {
Omar Sandovala0cac0e2019-09-16 11:30:57 -07007798 btrfs_init_work(&ordered->work, finish_ordered_fn, NULL,
7799 NULL);
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007800 btrfs_queue_work(wq, &ordered->work);
7801 }
Qu Wenruo58f74b22020-12-22 13:59:24 +08007802
7803 /* No ordered extent found in the range, exit */
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007804 if (ordered_offset == last_offset)
7805 return;
7806 /*
7807 * Our bio might span multiple ordered extents. In this case
Andrea Gelmini52042d82018-11-28 12:05:13 +01007808 * we keep going until we have accounted the whole dio.
Nikolay Borisovb25f0d02018-04-11 11:21:17 +03007809 */
7810 if (ordered_offset < offset + bytes) {
7811 ordered_bytes = offset + bytes - ordered_offset;
7812 ordered = NULL;
7813 }
Chris Mason163cf092010-11-28 19:56:33 -05007814 }
Filipe Manana14543772015-11-24 16:23:54 +00007815}
7816
Qu Wenruo8896a082020-10-21 14:24:53 +08007817static blk_status_t btrfs_submit_bio_start_direct_io(struct inode *inode,
Qu Wenruo1941b642020-12-02 14:47:57 +08007818 struct bio *bio,
7819 u64 dio_file_offset)
Chris Masoneaf25d92010-05-25 09:48:28 -04007820{
Qu Wenruo1941b642020-12-02 14:47:57 +08007821 return btrfs_csum_one_bio(BTRFS_I(inode), bio, dio_file_offset, 1);
Chris Masoneaf25d92010-05-25 09:48:28 -04007822}
7823
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02007824static void btrfs_end_dio_bio(struct bio *bio)
Miao Xiee65e1532010-11-22 03:04:43 +00007825{
7826 struct btrfs_dio_private *dip = bio->bi_private;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007827 blk_status_t err = bio->bi_status;
Miao Xiee65e1532010-11-22 03:04:43 +00007828
Miao Xie8b110e32014-09-12 18:44:03 +08007829 if (err)
7830 btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
Mike Christie6296b962016-06-05 14:32:21 -05007831 "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
David Sterbaf85b7372017-01-20 14:54:07 +01007832 btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
David Sterba1201b582020-11-26 15:41:27 +01007833 bio->bi_opf, bio->bi_iter.bi_sector,
Miao Xie8b110e32014-09-12 18:44:03 +08007834 bio->bi_iter.bi_size, err);
7835
Omar Sandoval769b4f22020-04-16 14:46:22 -07007836 if (bio_op(bio) == REQ_OP_READ) {
7837 err = btrfs_check_read_dio_bio(dip->inode, btrfs_io_bio(bio),
Omar Sandovalfd9d6672020-04-16 14:46:23 -07007838 !err);
Miao Xiee65e1532010-11-22 03:04:43 +00007839 }
7840
Omar Sandoval769b4f22020-04-16 14:46:22 -07007841 if (err)
7842 dip->dio_bio->bi_status = err;
Miao Xiee65e1532010-11-22 03:04:43 +00007843
Miao Xiee65e1532010-11-22 03:04:43 +00007844 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007845 btrfs_dio_private_put(dip);
Miao Xiec1dc0892014-09-12 18:43:56 +08007846}
7847
David Sterbad0ee3932018-03-08 14:35:48 +01007848static inline blk_status_t btrfs_submit_dio_bio(struct bio *bio,
7849 struct inode *inode, u64 file_offset, int async_submit)
Miao Xiee65e1532010-11-22 03:04:43 +00007850{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007851 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Miao Xiefacc8a222013-07-25 19:22:34 +08007852 struct btrfs_dio_private *dip = bio->bi_private;
Mike Christie37226b22016-06-05 14:31:52 -05007853 bool write = bio_op(bio) == REQ_OP_WRITE;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02007854 blk_status_t ret;
Miao Xiee65e1532010-11-22 03:04:43 +00007855
Liu Bo4c274bc2017-11-01 17:19:27 -06007856 /* Check btrfs_submit_bio_hook() for rules about async submit. */
Josef Bacikb812ce22012-11-16 13:56:32 -05007857 if (async_submit)
7858 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
7859
Josef Bacik5fd02042012-05-02 14:00:54 -04007860 if (!write) {
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007861 ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
Josef Bacik5fd02042012-05-02 14:00:54 -04007862 if (ret)
7863 goto err;
7864 }
Miao Xiee65e1532010-11-22 03:04:43 +00007865
Nikolay Borisove6961ca2017-08-03 15:44:58 +03007866 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
Josef Bacik1ae39932011-04-06 14:41:34 -04007867 goto map;
7868
7869 if (write && async_submit) {
Qu Wenruo1941b642020-12-02 14:47:57 +08007870 ret = btrfs_wq_submit_bio(inode, bio, 0, 0, file_offset,
David Sterbae288c082018-07-18 17:36:24 +02007871 btrfs_submit_bio_start_direct_io);
Miao Xiee65e1532010-11-22 03:04:43 +00007872 goto err;
Josef Bacik1ae39932011-04-06 14:41:34 -04007873 } else if (write) {
7874 /*
7875 * If we aren't doing async submit, calculate the csum of the
7876 * bio now.
7877 */
Nikolay Borisovbd242a02020-06-03 08:55:07 +03007878 ret = btrfs_csum_one_bio(BTRFS_I(inode), bio, file_offset, 1);
Josef Bacik1ae39932011-04-06 14:41:34 -04007879 if (ret)
7880 goto err;
Miao Xie23ea8e52014-09-12 18:43:54 +08007881 } else {
Omar Sandoval85879572020-04-16 14:46:21 -07007882 u64 csum_offset;
7883
7884 csum_offset = file_offset - dip->logical_offset;
David Sterba265fdfa2020-07-01 21:19:09 +02007885 csum_offset >>= fs_info->sectorsize_bits;
David Sterba55fc29b2020-06-30 02:01:31 +02007886 csum_offset *= fs_info->csum_size;
Omar Sandoval85879572020-04-16 14:46:21 -07007887 btrfs_io_bio(bio)->csum = dip->csums + csum_offset;
Tsutomu Itohc2db1072011-03-01 06:48:31 +00007888 }
Josef Bacik1ae39932011-04-06 14:41:34 -04007889map:
Chris Mason08635ba2019-07-10 12:28:14 -07007890 ret = btrfs_map_bio(fs_info, bio, 0);
Miao Xiee65e1532010-11-22 03:04:43 +00007891err:
Miao Xiee65e1532010-11-22 03:04:43 +00007892 return ret;
7893}
7894
Omar Sandovalc36cac22020-04-16 14:46:13 -07007895/*
7896 * If this succeeds, the btrfs_dio_private is responsible for cleaning up locked
7897 * or ordered extents whether or not we submit any bios.
7898 */
7899static struct btrfs_dio_private *btrfs_create_dio_private(struct bio *dio_bio,
7900 struct inode *inode,
7901 loff_t file_offset)
Miao Xiee65e1532010-11-22 03:04:43 +00007902{
Omar Sandovalc36cac22020-04-16 14:46:13 -07007903 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Omar Sandoval85879572020-04-16 14:46:21 -07007904 const bool csum = !(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM);
7905 size_t dip_size;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007906 struct btrfs_dio_private *dip;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007907
Omar Sandoval85879572020-04-16 14:46:21 -07007908 dip_size = sizeof(*dip);
7909 if (!write && csum) {
7910 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval85879572020-04-16 14:46:21 -07007911 size_t nblocks;
7912
David Sterba265fdfa2020-07-01 21:19:09 +02007913 nblocks = dio_bio->bi_iter.bi_size >> fs_info->sectorsize_bits;
David Sterba223486c2020-07-02 11:27:30 +02007914 dip_size += fs_info->csum_size * nblocks;
Omar Sandoval85879572020-04-16 14:46:21 -07007915 }
7916
7917 dip = kzalloc(dip_size, GFP_NOFS);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007918 if (!dip)
7919 return NULL;
7920
Omar Sandovalc36cac22020-04-16 14:46:13 -07007921 dip->inode = inode;
7922 dip->logical_offset = file_offset;
7923 dip->bytes = dio_bio->bi_iter.bi_size;
David Sterba1201b582020-11-26 15:41:27 +01007924 dip->disk_bytenr = dio_bio->bi_iter.bi_sector << 9;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007925 dip->dio_bio = dio_bio;
Omar Sandovale3b318d2020-04-16 14:46:20 -07007926 refcount_set(&dip->refs, 1);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007927 return dip;
7928}
7929
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007930static blk_qc_t btrfs_submit_direct(struct inode *inode, struct iomap *iomap,
7931 struct bio *dio_bio, loff_t file_offset)
Omar Sandovalc36cac22020-04-16 14:46:13 -07007932{
7933 const bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04007934 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Omar Sandoval769b4f22020-04-16 14:46:22 -07007935 const bool raid56 = (btrfs_data_alloc_profile(fs_info) &
7936 BTRFS_BLOCK_GROUP_RAID56_MASK);
Omar Sandovalc36cac22020-04-16 14:46:13 -07007937 struct btrfs_dio_private *dip;
Miao Xiee65e1532010-11-22 03:04:43 +00007938 struct bio *bio;
Omar Sandovalc36cac22020-04-16 14:46:13 -07007939 u64 start_sector;
Josef Bacik1ae39932011-04-06 14:41:34 -04007940 int async_submit = 0;
Liu Bo725130b2017-05-16 09:51:39 -07007941 u64 submit_len;
7942 int clone_offset = 0;
7943 int clone_len;
Chandan Rajendra5f4dc8f2016-01-21 15:56:00 +05307944 int ret;
Omar Sandoval58efbc92017-08-22 23:45:59 -07007945 blk_status_t status;
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007946 struct btrfs_io_geometry geom;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007947 struct btrfs_dio_data *dio_data = iomap->private;
Miao Xiee65e1532010-11-22 03:04:43 +00007948
Omar Sandovalc36cac22020-04-16 14:46:13 -07007949 dip = btrfs_create_dio_private(dio_bio, inode, file_offset);
7950 if (!dip) {
7951 if (!write) {
7952 unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
7953 file_offset + dio_bio->bi_iter.bi_size - 1);
7954 }
7955 dio_bio->bi_status = BLK_STS_RESOURCE;
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05007956 bio_endio(dio_bio);
7957 return BLK_QC_T_NONE;
Josef Bacik02f57c72011-04-06 14:25:44 -04007958 }
7959
Josef Bacik334c16d2020-10-16 11:29:14 -04007960 if (!write) {
Omar Sandoval85879572020-04-16 14:46:21 -07007961 /*
7962 * Load the csums up front to reduce csum tree searches and
7963 * contention when submitting bios.
Josef Bacik334c16d2020-10-16 11:29:14 -04007964 *
7965 * If we have csums disabled this will do nothing.
Omar Sandoval85879572020-04-16 14:46:21 -07007966 */
Qu Wenruo62751932020-12-02 14:48:06 +08007967 status = btrfs_lookup_bio_sums(inode, dio_bio, dip->csums);
Omar Sandoval85879572020-04-16 14:46:21 -07007968 if (status != BLK_STS_OK)
7969 goto out_err;
7970 }
David Woodhouse53b381b2013-01-29 18:40:14 -05007971
Omar Sandoval769b4f22020-04-16 14:46:22 -07007972 start_sector = dio_bio->bi_iter.bi_sector;
7973 submit_len = dio_bio->bi_iter.bi_size;
Miao Xiee65e1532010-11-22 03:04:43 +00007974
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02007975 do {
Omar Sandoval769b4f22020-04-16 14:46:22 -07007976 ret = btrfs_get_io_geometry(fs_info, btrfs_op(dio_bio),
7977 start_sector << 9, submit_len,
7978 &geom);
7979 if (ret) {
7980 status = errno_to_blk_status(ret);
7981 goto out_err;
7982 }
7983 ASSERT(geom.len <= INT_MAX);
7984
Nikolay Borisov89b798a2019-06-03 12:05:05 +03007985 clone_len = min_t(int, submit_len, geom.len);
Josef Bacik02f57c72011-04-06 14:25:44 -04007986
Liu Bo725130b2017-05-16 09:51:39 -07007987 /*
7988 * This will never fail as it's passing GPF_NOFS and
7989 * the allocation is backed by btrfs_bioset.
7990 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07007991 bio = btrfs_bio_clone_partial(dio_bio, clone_offset, clone_len);
Liu Bo725130b2017-05-16 09:51:39 -07007992 bio->bi_private = dip;
7993 bio->bi_end_io = btrfs_end_dio_bio;
7994 btrfs_io_bio(bio)->logical = file_offset;
Miao Xiee65e1532010-11-22 03:04:43 +00007995
Liu Bo725130b2017-05-16 09:51:39 -07007996 ASSERT(submit_len >= clone_len);
7997 submit_len -= clone_len;
Miao Xiee65e1532010-11-22 03:04:43 +00007998
Liu Bo725130b2017-05-16 09:51:39 -07007999 /*
8000 * Increase the count before we submit the bio so we know
8001 * the end IO handler won't happen before we increase the
8002 * count. Otherwise, the dip might get freed before we're
8003 * done setting it up.
Omar Sandoval769b4f22020-04-16 14:46:22 -07008004 *
8005 * We transfer the initial reference to the last bio, so we
8006 * don't need to increment the reference count for the last one.
Liu Bo725130b2017-05-16 09:51:39 -07008007 */
Omar Sandoval769b4f22020-04-16 14:46:22 -07008008 if (submit_len > 0) {
8009 refcount_inc(&dip->refs);
8010 /*
8011 * If we are submitting more than one bio, submit them
8012 * all asynchronously. The exception is RAID 5 or 6, as
8013 * asynchronous checksums make it difficult to collect
8014 * full stripe writes.
8015 */
8016 if (!raid56)
8017 async_submit = 1;
8018 }
Miao Xiee65e1532010-11-22 03:04:43 +00008019
David Sterbad0ee3932018-03-08 14:35:48 +01008020 status = btrfs_submit_dio_bio(bio, inode, file_offset,
Omar Sandoval58efbc92017-08-22 23:45:59 -07008021 async_submit);
8022 if (status) {
Liu Bo725130b2017-05-16 09:51:39 -07008023 bio_put(bio);
Omar Sandoval769b4f22020-04-16 14:46:22 -07008024 if (submit_len > 0)
8025 refcount_dec(&dip->refs);
Liu Bo725130b2017-05-16 09:51:39 -07008026 goto out_err;
Miao Xiee65e1532010-11-22 03:04:43 +00008027 }
Liu Bo725130b2017-05-16 09:51:39 -07008028
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008029 dio_data->submitted += clone_len;
Liu Bo725130b2017-05-16 09:51:39 -07008030 clone_offset += clone_len;
8031 start_sector += clone_len >> 9;
8032 file_offset += clone_len;
Arnd Bergmann3c91ee62017-05-18 15:33:29 +02008033 } while (submit_len > 0);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008034 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008035
Miao Xiee65e1532010-11-22 03:04:43 +00008036out_err:
Omar Sandoval769b4f22020-04-16 14:46:22 -07008037 dip->dio_bio->bi_status = status;
8038 btrfs_dio_private_put(dip);
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008039 return BLK_QC_T_NONE;
Miao Xiee65e1532010-11-22 03:04:43 +00008040}
8041
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008042const struct iomap_ops btrfs_dio_iomap_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008043 .iomap_begin = btrfs_dio_iomap_begin,
8044 .iomap_end = btrfs_dio_iomap_end,
8045};
8046
Goldwyn Rodrigues4e4cabe2020-09-24 11:39:12 -05008047const struct iomap_dio_ops btrfs_dio_ops = {
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -05008048 .submit_io = btrfs_submit_direct,
8049};
8050
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008051static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
David Sterbabab16e22020-06-23 20:56:12 +02008052 u64 start, u64 len)
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008053{
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008054 int ret;
8055
Christoph Hellwig45dd0522020-05-23 09:30:14 +02008056 ret = fiemap_prep(inode, fieinfo, start, &len, 0);
Tsutomu Itoh05dadc02012-11-29 05:08:26 +00008057 if (ret)
8058 return ret;
8059
Nikolay Borisovfacee0a2020-08-31 14:42:49 +03008060 return extent_fiemap(BTRFS_I(inode), fieinfo, start, len);
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -05008061}
8062
Chris Mason9ebefb182007-06-15 13:50:00 -04008063int btrfs_readpage(struct file *file, struct page *page)
8064{
Nikolay Borisov0f208812020-09-14 14:39:16 +03008065 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8066 u64 start = page_offset(page);
8067 u64 end = start + PAGE_SIZE - 1;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008068 unsigned long bio_flags = 0;
Nikolay Borisov0f208812020-09-14 14:39:16 +03008069 struct bio *bio = NULL;
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008070 int ret;
8071
Nikolay Borisov0f208812020-09-14 14:39:16 +03008072 btrfs_lock_and_flush_ordered_range(inode, start, end, NULL);
8073
8074 ret = btrfs_do_readpage(page, NULL, &bio, &bio_flags, 0, NULL);
Nikolay Borisovc1be9c12020-09-14 12:37:08 +03008075 if (bio)
8076 ret = submit_one_bio(bio, 0, bio_flags);
8077 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008078}
Chris Mason1832a6d2007-12-21 16:27:21 -05008079
Chris Mason39279cc2007-06-12 06:35:45 -04008080static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8081{
Josef Bacikbe7bd732015-10-22 15:05:09 -04008082 struct inode *inode = page->mapping->host;
8083 int ret;
Chris Masonb888db22007-08-27 16:49:44 -04008084
8085 if (current->flags & PF_MEMALLOC) {
8086 redirty_page_for_writepage(wbc, page);
8087 unlock_page(page);
8088 return 0;
8089 }
Josef Bacikbe7bd732015-10-22 15:05:09 -04008090
8091 /*
8092 * If we are under memory pressure we will call this directly from the
8093 * VM, we need to make sure we have the inode referenced for the ordered
8094 * extent. If not just return like we didn't do anything.
8095 */
8096 if (!igrab(inode)) {
8097 redirty_page_for_writepage(wbc, page);
8098 return AOP_WRITEPAGE_ACTIVATE;
8099 }
Nikolay Borisov0a9b0e52017-12-08 15:55:59 +02008100 ret = extent_write_full_page(page, wbc);
Josef Bacikbe7bd732015-10-22 15:05:09 -04008101 btrfs_add_delayed_iput(inode);
8102 return ret;
Chris Masona52d9a82007-08-27 16:49:44 -04008103}
Chris Mason39279cc2007-06-12 06:35:45 -04008104
Eric Sandeen48a3b632013-04-25 20:41:01 +00008105static int btrfs_writepages(struct address_space *mapping,
8106 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04008107{
Nikolay Borisov8ae225a2018-04-19 10:46:38 +03008108 return extent_writepages(mapping, wbc);
Chris Masonb293f02e2007-11-01 19:45:34 -04008109}
8110
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008111static void btrfs_readahead(struct readahead_control *rac)
Chris Mason3ab2fb52007-11-08 10:59:22 -05008112{
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -07008113 extent_readahead(rac);
Chris Mason3ab2fb52007-11-08 10:59:22 -05008114}
Nikolay Borisov2a3ff0a2018-04-19 10:46:36 +03008115
Chris Masone6dcd2d2008-07-17 12:53:50 -04008116static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04008117{
Nikolay Borisov477a30b2018-04-19 10:46:34 +03008118 int ret = try_release_extent_mapping(page, gfp_flags);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008119 if (ret == 1)
8120 detach_page_private(page);
Chris Masona52d9a82007-08-27 16:49:44 -04008121 return ret;
8122}
Chris Mason39279cc2007-06-12 06:35:45 -04008123
Chris Masone6dcd2d2008-07-17 12:53:50 -04008124static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8125{
Chris Mason98509cf2008-09-11 15:51:43 -04008126 if (PageWriteback(page) || PageDirty(page))
8127 return 0;
Michal Hocko3ba7ab22017-01-09 15:39:02 +01008128 return __btrfs_releasepage(page, gfp_flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008129}
8130
Roman Gushchinf8e66082020-03-04 16:57:35 -08008131#ifdef CONFIG_MIGRATION
8132static int btrfs_migratepage(struct address_space *mapping,
8133 struct page *newpage, struct page *page,
8134 enum migrate_mode mode)
8135{
8136 int ret;
8137
8138 ret = migrate_page_move_mapping(mapping, newpage, page, 0);
8139 if (ret != MIGRATEPAGE_SUCCESS)
8140 return ret;
8141
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008142 if (page_has_private(page))
8143 attach_page_private(newpage, detach_page_private(page));
Roman Gushchinf8e66082020-03-04 16:57:35 -08008144
8145 if (PagePrivate2(page)) {
8146 ClearPagePrivate2(page);
8147 SetPagePrivate2(newpage);
8148 }
8149
8150 if (mode != MIGRATE_SYNC_NO_COPY)
8151 migrate_page_copy(newpage, page);
8152 else
8153 migrate_page_states(newpage, page);
8154 return MIGRATEPAGE_SUCCESS;
8155}
8156#endif
8157
Lukas Czernerd47992f2013-05-21 23:17:23 -04008158static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8159 unsigned int length)
Chris Masona52d9a82007-08-27 16:49:44 -04008160{
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008161 struct btrfs_inode *inode = BTRFS_I(page->mapping->host);
8162 struct extent_io_tree *tree = &inode->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008163 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008164 struct extent_state *cached_state = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008165 u64 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008166 u64 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308167 u64 start;
8168 u64 end;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008169 int inode_evicting = inode->vfs_inode.i_state & I_FREEING;
Filipe Manana2766ff62020-11-04 11:07:34 +00008170 bool found_ordered = false;
8171 bool completed_ordered = false;
Chris Masona52d9a82007-08-27 16:49:44 -04008172
Chris Mason8b62b722009-09-02 16:53:46 -04008173 /*
8174 * we have the page locked, so new writeback can't start,
8175 * and the dirty bit won't be cleared while we are here.
8176 *
8177 * Wait for IO on this page so that we can safely clear
8178 * the PagePrivate2 bit and do ordered accounting
8179 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008180 wait_on_page_writeback(page);
Chris Mason8b62b722009-09-02 16:53:46 -04008181
Chris Masone6dcd2d2008-07-17 12:53:50 -04008182 if (offset) {
8183 btrfs_releasepage(page, GFP_NOFS);
8184 return;
8185 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008186
8187 if (!inode_evicting)
David Sterbaff13db42015-12-03 14:30:40 +01008188 lock_extent_bits(tree, page_start, page_end, &cached_state);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308189again:
8190 start = page_start;
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008191 ordered = btrfs_lookup_ordered_range(inode, start, page_end - start + 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008192 if (ordered) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008193 found_ordered = true;
Omar Sandovalbffe6332019-12-02 17:34:19 -08008194 end = min(page_end,
8195 ordered->file_offset + ordered->num_bytes - 1);
Chris Masoneb84ae02008-07-17 13:53:27 -04008196 /*
Filipe Manana2766ff62020-11-04 11:07:34 +00008197 * IO on this page will never be started, so we need to account
8198 * for any ordered extents now. Don't clear EXTENT_DELALLOC_NEW
8199 * here, must leave that up for the ordered extent completion.
Chris Masoneb84ae02008-07-17 13:53:27 -04008200 */
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008201 if (!inode_evicting)
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308202 clear_extent_bit(tree, start, end,
Filipe Manana2766ff62020-11-04 11:07:34 +00008203 EXTENT_DELALLOC |
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008204 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
David Sterbaae0f1622017-10-31 16:37:52 +01008205 EXTENT_DEFRAG, 1, 0, &cached_state);
Chris Mason8b62b722009-09-02 16:53:46 -04008206 /*
8207 * whoever cleared the private bit is responsible
8208 * for the finish_ordered_io
8209 */
Josef Bacik77cef2e2013-08-29 13:57:21 -04008210 if (TestClearPagePrivate2(page)) {
8211 struct btrfs_ordered_inode_tree *tree;
8212 u64 new_len;
8213
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008214 tree = &inode->ordered_tree;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008215
8216 spin_lock_irq(&tree->lock);
8217 set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308218 new_len = start - ordered->file_offset;
Josef Bacik77cef2e2013-08-29 13:57:21 -04008219 if (new_len < ordered->truncated_len)
8220 ordered->truncated_len = new_len;
8221 spin_unlock_irq(&tree->lock);
8222
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008223 if (btrfs_dec_test_ordered_pending(inode, &ordered,
8224 start,
Filipe Manana2766ff62020-11-04 11:07:34 +00008225 end - start + 1, 1)) {
Josef Bacik77cef2e2013-08-29 13:57:21 -04008226 btrfs_finish_ordered_io(ordered);
Filipe Manana2766ff62020-11-04 11:07:34 +00008227 completed_ordered = true;
8228 }
Chris Mason8b62b722009-09-02 16:53:46 -04008229 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008230 btrfs_put_ordered_extent(ordered);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008231 if (!inode_evicting) {
8232 cached_state = NULL;
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308233 lock_extent_bits(tree, start, end,
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008234 &cached_state);
8235 }
Chandan Rajendradbfdb6d2016-01-21 15:55:58 +05308236
8237 start = end + 1;
8238 if (start < page_end)
8239 goto again;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008240 }
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008241
Qu Wenruob9d0b382015-09-29 10:35:16 +08008242 /*
8243 * Qgroup reserved space handler
8244 * Page here will be either
Qu Wenruofa91e4a2020-07-17 15:12:05 +08008245 * 1) Already written to disk or ordered extent already submitted
8246 * Then its QGROUP_RESERVED bit in io_tree is already cleaned.
8247 * Qgroup will be handled by its qgroup_record then.
8248 * btrfs_qgroup_free_data() call will do nothing here.
8249 *
8250 * 2) Not written to disk yet
8251 * Then btrfs_qgroup_free_data() call will clear the QGROUP_RESERVED
8252 * bit of its io_tree, and free the qgroup reserved data space.
8253 * Since the IO will never happen for this page.
Qu Wenruob9d0b382015-09-29 10:35:16 +08008254 */
Nikolay Borisov53ac7ea2020-08-31 14:42:43 +03008255 btrfs_qgroup_free_data(inode, NULL, page_start, PAGE_SIZE);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008256 if (!inode_evicting) {
Filipe Manana2766ff62020-11-04 11:07:34 +00008257 bool delete = true;
8258
8259 /*
8260 * If there's an ordered extent for this range and we have not
8261 * finished it ourselves, we must leave EXTENT_DELALLOC_NEW set
8262 * in the range for the ordered extent completion. We must also
8263 * not delete the range, otherwise we would lose that bit (and
8264 * any other bits set in the range). Make sure EXTENT_UPTODATE
8265 * is cleared if we don't delete, otherwise it can lead to
8266 * corruptions if the i_size is extented later.
8267 */
8268 if (found_ordered && !completed_ordered)
8269 delete = false;
Omar Sandovale1821632019-08-15 14:04:04 -07008270 clear_extent_bit(tree, page_start, page_end, EXTENT_LOCKED |
Filipe Manana2766ff62020-11-04 11:07:34 +00008271 EXTENT_DELALLOC | EXTENT_UPTODATE |
8272 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1,
8273 delete, &cached_state);
Filipe David Borba Manana131e4042013-11-19 22:29:35 +00008274
8275 __btrfs_releasepage(page, GFP_NOFS);
8276 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008277
Chris Mason4a096752008-07-21 10:29:44 -04008278 ClearPageChecked(page);
Guoqing Jiangd1b89bc2020-06-01 21:47:45 -07008279 detach_page_private(page);
Chris Mason39279cc2007-06-12 06:35:45 -04008280}
8281
Chris Mason9ebefb182007-06-15 13:50:00 -04008282/*
8283 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8284 * called from a page fault handler when a page is first dirtied. Hence we must
8285 * be careful to check for EOF conditions here. We set the page up correctly
8286 * for a written page which means we get ENOSPC checking when writing into
8287 * holes and correct delalloc and unwritten extent mapping on filesystems that
8288 * support these features.
8289 *
8290 * We are not allowed to take the i_mutex here so we have to play games to
8291 * protect against truncate races as the page could now be beyond EOF. Because
Omar Sandovald1342aa2018-05-11 13:13:29 -07008292 * truncate_setsize() writes the inode size before removing pages, once we have
8293 * the page lock we can determine safely if the page is beyond EOF. If it is not
Chris Mason9ebefb182007-06-15 13:50:00 -04008294 * beyond EOF, then the page is guaranteed safe against truncation until we
8295 * unlock the page.
8296 */
Souptick Joardera528a242018-06-06 19:54:44 +05308297vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf)
Chris Mason9ebefb182007-06-15 13:50:00 -04008298{
Nick Pigginc2ec1752009-03-31 15:23:21 -07008299 struct page *page = vmf->page;
Dave Jiang11bac802017-02-24 14:56:41 -08008300 struct inode *inode = file_inode(vmf->vma->vm_file);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008301 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008302 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8303 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00008304 struct extent_state *cached_state = NULL;
Qu Wenruo364ecf32017-02-27 15:10:38 +08008305 struct extent_changeset *data_reserved = NULL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008306 char *kaddr;
8307 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04008308 loff_t size;
Souptick Joardera528a242018-06-06 19:54:44 +05308309 vm_fault_t ret;
8310 int ret2;
Chris Mason9998eb72012-01-25 13:47:40 -05008311 int reserved = 0;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308312 u64 reserved_space;
Chris Masona52d9a82007-08-27 16:49:44 -04008313 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04008314 u64 page_end;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308315 u64 end;
8316
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008317 reserved_space = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008318
Jan Karab2b5ef52012-06-12 16:20:45 +02008319 sb_start_pagefault(inode->i_sb);
Qu Wenruodf480632015-09-08 17:25:54 +08008320 page_start = page_offset(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008321 page_end = page_start + PAGE_SIZE - 1;
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308322 end = page_end;
Qu Wenruodf480632015-09-08 17:25:54 +08008323
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308324 /*
8325 * Reserving delalloc space after obtaining the page lock can lead to
8326 * deadlock. For example, if a dirty page is locked by this function
8327 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8328 * dirty page write out, then the btrfs_writepage() function could
8329 * end up waiting indefinitely to get a lock on the page currently
8330 * being processed by btrfs_page_mkwrite() function.
8331 */
Nikolay Borisove5b7231e2020-06-03 08:55:42 +03008332 ret2 = btrfs_delalloc_reserve_space(BTRFS_I(inode), &data_reserved,
8333 page_start, reserved_space);
Souptick Joardera528a242018-06-06 19:54:44 +05308334 if (!ret2) {
8335 ret2 = file_update_time(vmf->vma->vm_file);
Chris Mason9998eb72012-01-25 13:47:40 -05008336 reserved = 1;
8337 }
Souptick Joardera528a242018-06-06 19:54:44 +05308338 if (ret2) {
8339 ret = vmf_error(ret2);
Chris Mason9998eb72012-01-25 13:47:40 -05008340 if (reserved)
8341 goto out;
8342 goto out_noreserve;
Nick Piggin56a76f82009-03-31 15:23:23 -07008343 }
Chris Mason1832a6d2007-12-21 16:27:21 -05008344
Nick Piggin56a76f82009-03-31 15:23:23 -07008345 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
Chris Masone6dcd2d2008-07-17 12:53:50 -04008346again:
Chris Mason9ebefb182007-06-15 13:50:00 -04008347 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04008348 size = i_size_read(inode);
Chris Masona52d9a82007-08-27 16:49:44 -04008349
Chris Mason9ebefb182007-06-15 13:50:00 -04008350 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04008351 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04008352 /* page got truncated out from underneath us */
8353 goto out_unlock;
8354 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04008355 wait_on_page_writeback(page);
8356
David Sterbaff13db42015-12-03 14:30:40 +01008357 lock_extent_bits(io_tree, page_start, page_end, &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008358 set_page_extent_mapped(page);
8359
Chris Masoneb84ae02008-07-17 13:53:27 -04008360 /*
8361 * we can't set the delalloc bits if there are pending ordered
8362 * extents. Drop our locks and wait for them to finish
8363 */
Nikolay Borisova776c6f2017-02-20 13:50:49 +02008364 ordered = btrfs_lookup_ordered_range(BTRFS_I(inode), page_start,
8365 PAGE_SIZE);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008366 if (ordered) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008367 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008368 &cached_state);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008369 unlock_page(page);
Nikolay Borisovc0a43602020-09-18 12:15:53 +03008370 btrfs_start_ordered_extent(ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008371 btrfs_put_ordered_extent(ordered);
8372 goto again;
8373 }
8374
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008375 if (page->index == ((size - 1) >> PAGE_SHIFT)) {
Jeff Mahoneyda170662016-06-15 09:22:56 -04008376 reserved_space = round_up(size - page_start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008377 fs_info->sectorsize);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008378 if (reserved_space < PAGE_SIZE) {
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308379 end = page_start + reserved_space - 1;
Nikolay Borisov86d52922020-06-03 08:55:40 +03008380 btrfs_delalloc_release_space(BTRFS_I(inode),
8381 data_reserved, page_start,
8382 PAGE_SIZE - reserved_space, true);
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308383 }
8384 }
8385
Josef Bacikfbf19082009-10-01 17:10:23 -04008386 /*
Liu Bo54160342016-12-13 12:15:19 -08008387 * page_mkwrite gets called when the page is firstly dirtied after it's
8388 * faulted in, but write(2) could also dirty a page and set delalloc
8389 * bits, thus in this case for space account reason, we still need to
8390 * clear any delalloc bits within this page range since we have to
8391 * reserve data&meta space before lock_page() (see above comments).
Josef Bacikfbf19082009-10-01 17:10:23 -04008392 */
Chandan Rajendrad0b7da882016-01-21 15:55:57 +05308393 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
Omar Sandovale1821632019-08-15 14:04:04 -07008394 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8395 EXTENT_DEFRAG, 0, 0, &cached_state);
Josef Bacikfbf19082009-10-01 17:10:23 -04008396
Nikolay Borisovc2566f22020-06-03 08:55:35 +03008397 ret2 = btrfs_set_extent_delalloc(BTRFS_I(inode), page_start, end, 0,
Nikolay Borisov330a5822019-07-17 16:18:17 +03008398 &cached_state);
Souptick Joardera528a242018-06-06 19:54:44 +05308399 if (ret2) {
Josef Bacik2ac55d42010-02-03 19:33:23 +00008400 unlock_extent_cached(io_tree, page_start, page_end,
David Sterbae43bbe52017-12-12 21:43:52 +01008401 &cached_state);
Josef Bacik9ed74f22009-09-11 16:12:44 -04008402 ret = VM_FAULT_SIGBUS;
8403 goto out_unlock;
8404 }
Chris Mason9ebefb182007-06-15 13:50:00 -04008405
8406 /* page is wholly or partially inside EOF */
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008407 if (page_start + PAGE_SIZE > size)
Johannes Thumshirn70730172018-12-05 15:23:03 +01008408 zero_start = offset_in_page(size);
Chris Mason9ebefb182007-06-15 13:50:00 -04008409 else
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008410 zero_start = PAGE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04008411
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008412 if (zero_start != PAGE_SIZE) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04008413 kaddr = kmap(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03008414 memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008415 flush_dcache_page(page);
8416 kunmap(page);
8417 }
Chris Mason247e7432008-07-17 12:53:51 -04008418 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008419 set_page_dirty(page);
Chris Mason50a9b212009-09-11 12:33:12 -04008420 SetPageUptodate(page);
Chris Mason5a3f23d2009-03-31 13:27:11 -04008421
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008422 BTRFS_I(inode)->last_trans = fs_info->generation;
Chris Mason257c62e2009-10-13 13:21:08 -04008423 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
Liu Bo46d8bc32012-08-29 01:07:55 -06008424 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
Chris Mason257c62e2009-10-13 13:21:08 -04008425
David Sterbae43bbe52017-12-12 21:43:52 +01008426 unlock_extent_cached(io_tree, page_start, page_end, &cached_state);
Chris Mason9ebefb182007-06-15 13:50:00 -04008427
Yunfeng Ye76de60e2019-12-03 16:59:25 +08008428 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
8429 sb_end_pagefault(inode->i_sb);
8430 extent_changeset_free(data_reserved);
8431 return VM_FAULT_LOCKED;
Chris Mason717beb92018-06-25 10:03:41 -07008432
8433out_unlock:
Chris Mason9ebefb182007-06-15 13:50:00 -04008434 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05008435out:
Qu Wenruo8702ba92019-10-14 14:34:51 +08008436 btrfs_delalloc_release_extents(BTRFS_I(inode), PAGE_SIZE);
Nikolay Borisov86d52922020-06-03 08:55:40 +03008437 btrfs_delalloc_release_space(BTRFS_I(inode), data_reserved, page_start,
Qu Wenruo43b18592017-12-12 15:34:32 +08008438 reserved_space, (ret != 0));
Chris Mason9998eb72012-01-25 13:47:40 -05008439out_noreserve:
Jan Karab2b5ef52012-06-12 16:20:45 +02008440 sb_end_pagefault(inode->i_sb);
Qu Wenruo364ecf32017-02-27 15:10:38 +08008441 extent_changeset_free(data_reserved);
Chris Mason9ebefb182007-06-15 13:50:00 -04008442 return ret;
8443}
8444
Filipe Manana213e8c52018-02-06 20:40:31 +00008445static int btrfs_truncate(struct inode *inode, bool skip_writeback)
Chris Mason39279cc2007-06-12 06:35:45 -04008446{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008447 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008448 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacikfcb80c22011-05-03 10:40:22 -04008449 struct btrfs_block_rsv *rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008450 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008451 struct btrfs_trans_handle *trans;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008452 u64 mask = fs_info->sectorsize - 1;
Josef Bacik2bd36e72019-08-22 15:14:33 -04008453 u64 min_size = btrfs_calc_metadata_size(fs_info, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04008454
Filipe Manana213e8c52018-02-06 20:40:31 +00008455 if (!skip_writeback) {
8456 ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
8457 (u64)-1);
8458 if (ret)
8459 return ret;
8460 }
Chris Mason39279cc2007-06-12 06:35:45 -04008461
Josef Bacikfcb80c22011-05-03 10:40:22 -04008462 /*
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008463 * Yes ladies and gentlemen, this is indeed ugly. We have a couple of
8464 * things going on here:
Josef Bacikfcb80c22011-05-03 10:40:22 -04008465 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008466 * 1) We need to reserve space to update our inode.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008467 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008468 * 2) We need to have something to cache all the space that is going to
Josef Bacikfcb80c22011-05-03 10:40:22 -04008469 * be free'd up by the truncate operation, but also have some slack
8470 * space reserved in case it uses space during the truncate (thank you
8471 * very much snapshotting).
8472 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008473 * And we need these to be separate. The fact is we can use a lot of
Josef Bacikfcb80c22011-05-03 10:40:22 -04008474 * space doing the truncate, and we have no earthly idea how much space
Nicholas D Steeves01327612016-05-19 21:18:45 -04008475 * we will use, so we need the truncate reservation to be separate so it
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008476 * doesn't end up using space reserved for updating the inode. We also
8477 * need to be able to stop the transaction and start a new one, which
8478 * means we need to be able to update the inode several times, and we
8479 * have no idea of knowing how many times that will be, so we can't just
8480 * reserve 1 item for the entirety of the operation, so that has to be
8481 * done separately as well.
Josef Bacikfcb80c22011-05-03 10:40:22 -04008482 *
8483 * So that leaves us with
8484 *
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008485 * 1) rsv - for the truncate reservation, which we will steal from the
Josef Bacikfcb80c22011-05-03 10:40:22 -04008486 * transaction reservation.
Omar Sandovalf7e9e8f2018-05-11 13:13:32 -07008487 * 2) fs_info->trans_block_rsv - this will have 1 items worth left for
Josef Bacikfcb80c22011-05-03 10:40:22 -04008488 * updating the inode.
8489 */
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008490 rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008491 if (!rsv)
8492 return -ENOMEM;
Josef Bacik4a338542011-08-29 11:01:31 -04008493 rsv->size = min_size;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008494 rsv->failfast = 1;
Josef Bacikf0cd8462011-03-04 14:37:08 -05008495
Josef Bacik907cbce2011-08-08 13:46:15 -04008496 /*
Josef Bacik07127182011-08-19 10:29:59 -04008497 * 1 for the truncate slack space
Josef Bacik907cbce2011-08-08 13:46:15 -04008498 * 1 for updating the inode.
8499 */
Josef Bacikf3fe8202013-01-07 17:03:21 -05008500 trans = btrfs_start_transaction(root, 2);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008501 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008502 ret = PTR_ERR(trans);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008503 goto out;
8504 }
Josef Bacikf0cd8462011-03-04 14:37:08 -05008505
Josef Bacik907cbce2011-08-08 13:46:15 -04008506 /* Migrate the slack space for the truncate to our reserve */
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008507 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008508 min_size, false);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008509 BUG_ON(ret);
Josef Bacikf0cd8462011-03-04 14:37:08 -05008510
Chris Mason5a3f23d2009-03-31 13:27:11 -04008511 /*
Josef Bacik5dc562c2012-08-17 13:14:17 -04008512 * So if we truncate and then write and fsync we normally would just
8513 * write the extents that changed, which is a problem if we need to
8514 * first truncate that entire inode. So set this flag so we write out
8515 * all of the extents in the inode to the sync log so we're completely
8516 * safe.
8517 */
8518 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008519 trans->block_rsv = rsv;
Josef Bacik5dc562c2012-08-17 13:14:17 -04008520
Yan, Zheng80825102009-11-12 09:35:36 +00008521 while (1) {
Nikolay Borisov50743392020-11-02 16:48:55 +02008522 ret = btrfs_truncate_inode_items(trans, root, BTRFS_I(inode),
Yan, Zheng80825102009-11-12 09:35:36 +00008523 inode->i_size,
8524 BTRFS_EXTENT_DATA_KEY);
Josef Bacikddfae632017-10-19 14:16:02 -04008525 trans->block_rsv = &fs_info->trans_block_rsv;
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008526 if (ret != -ENOSPC && ret != -EAGAIN)
Yan, Zheng80825102009-11-12 09:35:36 +00008527 break;
Chris Mason39279cc2007-06-12 06:35:45 -04008528
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008529 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008530 if (ret)
Josef Bacik3893e332011-01-31 16:03:11 -05008531 break;
Josef Bacikca7e70f2012-08-27 17:48:15 -04008532
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04008533 btrfs_end_transaction(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008534 btrfs_btree_balance_dirty(fs_info);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008535
8536 trans = btrfs_start_transaction(root, 2);
8537 if (IS_ERR(trans)) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008538 ret = PTR_ERR(trans);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008539 trans = NULL;
8540 break;
8541 }
8542
Nikolay Borisov63f018b2020-03-10 10:59:31 +02008543 btrfs_block_rsv_release(fs_info, rsv, -1, NULL);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008544 ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
Lu Fengqi3a584172018-08-04 21:10:55 +08008545 rsv, min_size, false);
Josef Bacikca7e70f2012-08-27 17:48:15 -04008546 BUG_ON(ret); /* shouldn't happen */
8547 trans->block_rsv = rsv;
Yan, Zheng80825102009-11-12 09:35:36 +00008548 }
8549
Josef Bacikddfae632017-10-19 14:16:02 -04008550 /*
8551 * We can't call btrfs_truncate_block inside a trans handle as we could
8552 * deadlock with freeze, if we got NEED_TRUNCATE_BLOCK then we know
8553 * we've truncated everything except the last little bit, and can do
8554 * btrfs_truncate_block and then update the disk_i_size.
8555 */
8556 if (ret == NEED_TRUNCATE_BLOCK) {
8557 btrfs_end_transaction(trans);
8558 btrfs_btree_balance_dirty(fs_info);
8559
Nikolay Borisov217f42e2020-11-02 16:49:03 +02008560 ret = btrfs_truncate_block(BTRFS_I(inode), inode->i_size, 0, 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008561 if (ret)
8562 goto out;
8563 trans = btrfs_start_transaction(root, 1);
8564 if (IS_ERR(trans)) {
8565 ret = PTR_ERR(trans);
8566 goto out;
8567 }
Nikolay Borisov76aea532020-11-02 16:48:53 +02008568 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Josef Bacikddfae632017-10-19 14:16:02 -04008569 }
8570
Chris Mason917c16b2011-11-08 14:49:59 -05008571 if (trans) {
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008572 int ret2;
Josef Bacik7b128762008-07-24 12:17:14 -04008573
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008574 trans->block_rsv = &fs_info->trans_block_rsv;
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008575 ret2 = btrfs_update_inode(trans, root, BTRFS_I(inode));
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008576 if (ret2 && !ret)
8577 ret = ret2;
8578
8579 ret2 = btrfs_end_transaction(trans);
8580 if (ret2 && !ret)
8581 ret = ret2;
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008582 btrfs_btree_balance_dirty(fs_info);
Chris Mason917c16b2011-11-08 14:49:59 -05008583 }
Josef Bacikfcb80c22011-05-03 10:40:22 -04008584out:
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04008585 btrfs_free_block_rsv(fs_info, rsv);
Josef Bacikfcb80c22011-05-03 10:40:22 -04008586
Omar Sandovalad7e1a72018-05-22 09:59:50 -07008587 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04008588}
8589
Sven Wegener3b963622008-06-09 21:57:42 -04008590/*
Chris Masond352ac62008-09-29 15:18:18 -04008591 * create a new subvolume directory/inode (helper for the ioctl).
8592 */
Yan Zhengd2fb3432008-12-11 16:30:39 -05008593int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008594 struct btrfs_root *new_root,
Nikolay Borisov69948022020-12-07 17:32:37 +02008595 struct btrfs_root *parent_root)
Chris Mason39279cc2007-06-12 06:35:45 -04008596{
Chris Mason39279cc2007-06-12 06:35:45 -04008597 struct inode *inode;
Yan, Zheng76dda932009-09-21 16:00:26 -04008598 int err;
Chris Mason00e4e6b2008-08-05 11:18:09 -04008599 u64 index = 0;
Nikolay Borisov23125102020-12-07 17:32:36 +02008600 u64 ino;
Chris Mason39279cc2007-06-12 06:35:45 -04008601
Nikolay Borisov23125102020-12-07 17:32:36 +02008602 err = btrfs_get_free_objectid(new_root, &ino);
8603 if (err < 0)
8604 return err;
8605
8606 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, ino, ino,
Florian Albrechtskirchinger12fc9d02012-02-10 22:15:54 +01008607 S_IFDIR | (~current_umask() & S_IRWXUGO),
8608 &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04008609 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04008610 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04008611 inode->i_op = &btrfs_dir_inode_operations;
8612 inode->i_fop = &btrfs_dir_file_operations;
8613
Miklos Szeredibfe86842011-10-28 14:13:29 +02008614 set_nlink(inode, 1);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02008615 btrfs_i_size_write(BTRFS_I(inode), 0);
Chris Masonb0d5d102014-09-08 13:08:51 -07008616 unlock_new_inode(inode);
Sven Wegener3b963622008-06-09 21:57:42 -04008617
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008618 err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
8619 if (err)
8620 btrfs_err(new_root->fs_info,
David Sterba351fd352014-05-15 16:48:20 +02008621 "error inheriting subvolume %llu properties: %d",
Filipe David Borba Manana63541922014-01-07 11:47:46 +00008622 new_root->root_key.objectid, err);
8623
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008624 err = btrfs_update_inode(trans, new_root, BTRFS_I(inode));
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04008625
Yan, Zheng76dda932009-09-21 16:00:26 -04008626 iput(inode);
Mark Fashehce598972011-07-26 11:32:23 -07008627 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04008628}
8629
Chris Mason39279cc2007-06-12 06:35:45 -04008630struct inode *btrfs_alloc_inode(struct super_block *sb)
8631{
Josef Bacik69fe2d72017-10-19 14:15:57 -04008632 struct btrfs_fs_info *fs_info = btrfs_sb(sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008633 struct btrfs_inode *ei;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008634 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008635
David Sterba712e36c2017-10-31 17:08:27 +01008636 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -04008637 if (!ei)
8638 return NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008639
8640 ei->root = NULL;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008641 ei->generation = 0;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04008642 ei->last_trans = 0;
Chris Mason257c62e2009-10-13 13:21:08 -04008643 ei->last_sub_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04008644 ei->logged_trans = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008645 ei->delalloc_bytes = 0;
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008646 ei->new_delalloc_bytes = 0;
Wang Shilong47059d92014-07-03 18:22:07 +08008647 ei->defrag_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008648 ei->disk_i_size = 0;
8649 ei->flags = 0;
Josef Bacik7709cde2011-08-04 10:25:02 -04008650 ei->csum_bytes = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008651 ei->index_cnt = (u64)-1;
Miao Xie67de1172013-12-26 13:07:06 +08008652 ei->dir_index = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008653 ei->last_unlink_trans = 0;
Filipe Manana3ebac172020-07-15 12:30:43 +01008654 ei->last_reflink_trans = 0;
Liu Bo46d8bc32012-08-29 01:07:55 -06008655 ei->last_log_commit = 0;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008656
Josef Bacik9e0baf62011-07-15 15:16:44 +00008657 spin_lock_init(&ei->lock);
8658 ei->outstanding_extents = 0;
Josef Bacik69fe2d72017-10-19 14:15:57 -04008659 if (sb->s_magic != BTRFS_TEST_MAGIC)
8660 btrfs_init_metadata_block_rsv(fs_info, &ei->block_rsv,
8661 BTRFS_BLOCK_RSV_DELALLOC);
Josef Bacik72ac3c02012-05-23 14:13:11 -04008662 ei->runtime_flags = 0;
David Sterbab52aa8c2017-07-17 19:17:20 +02008663 ei->prop_compress = BTRFS_COMPRESS_NONE;
David Sterbaeec63c62017-07-17 19:41:31 +02008664 ei->defrag_compress = BTRFS_COMPRESS_NONE;
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008665
Miao Xie16cdcec2011-04-22 18:12:22 +08008666 ei->delayed_node = NULL;
8667
chandan r9cc97d62012-07-04 12:48:07 +05308668 ei->i_otime.tv_sec = 0;
8669 ei->i_otime.tv_nsec = 0;
8670
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008671 inode = &ei->vfs_inode;
David Sterbaa8067e02011-04-21 00:34:43 +02008672 extent_map_tree_init(&ei->extent_tree);
Qu Wenruo43eb5f22019-03-01 10:47:59 +08008673 extent_io_tree_init(fs_info, &ei->io_tree, IO_TREE_INODE_IO, inode);
8674 extent_io_tree_init(fs_info, &ei->io_failure_tree,
8675 IO_TREE_INODE_IO_FAILURE, inode);
Josef Bacik41a2ee72020-01-17 09:02:21 -05008676 extent_io_tree_init(fs_info, &ei->file_extent_tree,
8677 IO_TREE_INODE_FILE_EXTENT, inode);
David Sterba7b439732019-03-11 15:58:30 +01008678 ei->io_tree.track_uptodate = true;
8679 ei->io_failure_tree.track_uptodate = true;
Josef Bacikb812ce22012-11-16 13:56:32 -05008680 atomic_set(&ei->sync_writers, 0);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008681 mutex_init(&ei->log_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008682 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008683 INIT_LIST_HEAD(&ei->delalloc_inodes);
David Sterba8089fe62015-11-19 14:15:51 +01008684 INIT_LIST_HEAD(&ei->delayed_iput);
Yan, Zheng2ead6ae2010-05-16 10:46:25 -04008685 RB_CLEAR_NODE(&ei->rb_node);
8686
8687 return inode;
Chris Mason39279cc2007-06-12 06:35:45 -04008688}
8689
Josef Bacikaaedb552013-10-11 14:44:09 -04008690#ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
8691void btrfs_test_destroy_inode(struct inode *inode)
8692{
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02008693 btrfs_drop_extent_cache(BTRFS_I(inode), 0, (u64)-1, 0);
Josef Bacikaaedb552013-10-11 14:44:09 -04008694 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8695}
8696#endif
8697
Al Viro26602ca2019-04-10 15:14:41 -04008698void btrfs_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008699{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11008700 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
8701}
8702
Nikolay Borisov633cc812020-09-18 12:15:49 +03008703void btrfs_destroy_inode(struct inode *vfs_inode)
Chris Mason39279cc2007-06-12 06:35:45 -04008704{
Chris Masone6dcd2d2008-07-17 12:53:50 -04008705 struct btrfs_ordered_extent *ordered;
Nikolay Borisov633cc812020-09-18 12:15:49 +03008706 struct btrfs_inode *inode = BTRFS_I(vfs_inode);
8707 struct btrfs_root *root = inode->root;
Chris Mason5a3f23d2009-03-31 13:27:11 -04008708
Nikolay Borisov633cc812020-09-18 12:15:49 +03008709 WARN_ON(!hlist_empty(&vfs_inode->i_dentry));
8710 WARN_ON(vfs_inode->i_data.nrpages);
8711 WARN_ON(inode->block_rsv.reserved);
8712 WARN_ON(inode->block_rsv.size);
8713 WARN_ON(inode->outstanding_extents);
8714 WARN_ON(inode->delalloc_bytes);
8715 WARN_ON(inode->new_delalloc_bytes);
8716 WARN_ON(inode->csum_bytes);
8717 WARN_ON(inode->defrag_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04008718
Chris Mason5a3f23d2009-03-31 13:27:11 -04008719 /*
Josef Bacika6dbd422009-11-11 15:53:34 -05008720 * This can happen where we create an inode, but somebody else also
8721 * created the same inode and we need to destroy the one we already
8722 * created.
8723 */
8724 if (!root)
Al Viro26602ca2019-04-10 15:14:41 -04008725 return;
Josef Bacika6dbd422009-11-11 15:53:34 -05008726
Chris Masond3977122009-01-05 21:25:51 -05008727 while (1) {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008728 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008729 if (!ordered)
8730 break;
8731 else {
Nikolay Borisov633cc812020-09-18 12:15:49 +03008732 btrfs_err(root->fs_info,
Jeff Mahoney5d163e02016-09-20 10:05:00 -04008733 "found ordered extent %llu %llu on inode cleanup",
Omar Sandovalbffe6332019-12-02 17:34:19 -08008734 ordered->file_offset, ordered->num_bytes);
Nikolay Borisov71fe0a52020-09-18 12:15:50 +03008735 btrfs_remove_ordered_extent(inode, ordered);
Chris Masone6dcd2d2008-07-17 12:53:50 -04008736 btrfs_put_ordered_extent(ordered);
8737 btrfs_put_ordered_extent(ordered);
8738 }
8739 }
Nikolay Borisov633cc812020-09-18 12:15:49 +03008740 btrfs_qgroup_check_reserved_leak(inode);
8741 inode_tree_del(inode);
8742 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
8743 btrfs_inode_clear_file_extent_range(inode, 0, (u64)-1);
8744 btrfs_put_root(inode->root);
Chris Mason39279cc2007-06-12 06:35:45 -04008745}
8746
Al Viro45321ac2010-06-07 13:43:19 -04008747int btrfs_drop_inode(struct inode *inode)
Yan, Zheng76dda932009-09-21 16:00:26 -04008748{
8749 struct btrfs_root *root = BTRFS_I(inode)->root;
Al Viro45321ac2010-06-07 13:43:19 -04008750
Naohiro Aota6379ef92013-06-06 09:56:34 +00008751 if (root == NULL)
8752 return 1;
8753
Liu Bofa6ac872013-02-20 14:10:23 +00008754 /* the snap/subvol tree is on deleting */
Stefan Behrens69e9c6c2013-09-05 16:58:43 +02008755 if (btrfs_root_refs(&root->root_item) == 0)
Al Viro45321ac2010-06-07 13:43:19 -04008756 return 1;
Yan, Zheng76dda932009-09-21 16:00:26 -04008757 else
Al Viro45321ac2010-06-07 13:43:19 -04008758 return generic_drop_inode(inode);
Yan, Zheng76dda932009-09-21 16:00:26 -04008759}
8760
Sven Wegener0ee0fda2008-07-30 16:54:26 -04008761static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04008762{
8763 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
8764
8765 inode_init_once(&ei->vfs_inode);
8766}
8767
David Sterbae67c7182018-02-19 17:24:18 +01008768void __cold btrfs_destroy_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008769{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10008770 /*
8771 * Make sure all delayed rcu free inodes are flushed before we
8772 * destroy cache.
8773 */
8774 rcu_barrier();
Kinglong Mee5598e902016-01-29 21:36:35 +08008775 kmem_cache_destroy(btrfs_inode_cachep);
8776 kmem_cache_destroy(btrfs_trans_handle_cachep);
Kinglong Mee5598e902016-01-29 21:36:35 +08008777 kmem_cache_destroy(btrfs_path_cachep);
8778 kmem_cache_destroy(btrfs_free_space_cachep);
Christophe Leroy3acd4852019-08-21 15:05:55 +00008779 kmem_cache_destroy(btrfs_free_space_bitmap_cachep);
Chris Mason39279cc2007-06-12 06:35:45 -04008780}
8781
Liu Bof5c29bd2017-11-02 17:21:50 -06008782int __init btrfs_init_cachep(void)
Chris Mason39279cc2007-06-12 06:35:45 -04008783{
David Sterba837e1972012-09-07 03:00:48 -06008784 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008785 sizeof(struct btrfs_inode), 0,
Vladimir Davydov5d097052016-01-14 15:18:21 -08008786 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
8787 init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04008788 if (!btrfs_inode_cachep)
8789 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008790
David Sterba837e1972012-09-07 03:00:48 -06008791 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008792 sizeof(struct btrfs_trans_handle), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008793 SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008794 if (!btrfs_trans_handle_cachep)
8795 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008796
David Sterba837e1972012-09-07 03:00:48 -06008797 btrfs_path_cachep = kmem_cache_create("btrfs_path",
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008798 sizeof(struct btrfs_path), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008799 SLAB_MEM_SPREAD, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04008800 if (!btrfs_path_cachep)
8801 goto fail;
Christoph Hellwig9601e3f2009-04-13 15:33:09 +02008802
David Sterba837e1972012-09-07 03:00:48 -06008803 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
Josef Bacikdc89e982011-01-28 17:05:48 -05008804 sizeof(struct btrfs_free_space), 0,
Nikolay Borisovfba4b692016-06-23 21:17:08 +03008805 SLAB_MEM_SPREAD, NULL);
Josef Bacikdc89e982011-01-28 17:05:48 -05008806 if (!btrfs_free_space_cachep)
8807 goto fail;
8808
Christophe Leroy3acd4852019-08-21 15:05:55 +00008809 btrfs_free_space_bitmap_cachep = kmem_cache_create("btrfs_free_space_bitmap",
8810 PAGE_SIZE, PAGE_SIZE,
8811 SLAB_RED_ZONE, NULL);
8812 if (!btrfs_free_space_bitmap_cachep)
8813 goto fail;
8814
Chris Mason39279cc2007-06-12 06:35:45 -04008815 return 0;
8816fail:
8817 btrfs_destroy_cachep();
8818 return -ENOMEM;
8819}
8820
David Howellsa528d352017-01-31 16:46:22 +00008821static int btrfs_getattr(const struct path *path, struct kstat *stat,
8822 u32 request_mask, unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04008823{
Miao Xiedf0af1a2013-01-29 10:11:59 +00008824 u64 delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00008825 u64 inode_bytes;
David Howellsa528d352017-01-31 16:46:22 +00008826 struct inode *inode = d_inode(path->dentry);
David Sterbafadc0d82011-11-20 07:33:38 -05008827 u32 blocksize = inode->i_sb->s_blocksize;
Yonghong Song04a87e32017-05-12 15:07:43 -07008828 u32 bi_flags = BTRFS_I(inode)->flags;
8829
8830 stat->result_mask |= STATX_BTIME;
8831 stat->btime.tv_sec = BTRFS_I(inode)->i_otime.tv_sec;
8832 stat->btime.tv_nsec = BTRFS_I(inode)->i_otime.tv_nsec;
8833 if (bi_flags & BTRFS_INODE_APPEND)
8834 stat->attributes |= STATX_ATTR_APPEND;
8835 if (bi_flags & BTRFS_INODE_COMPRESS)
8836 stat->attributes |= STATX_ATTR_COMPRESSED;
8837 if (bi_flags & BTRFS_INODE_IMMUTABLE)
8838 stat->attributes |= STATX_ATTR_IMMUTABLE;
8839 if (bi_flags & BTRFS_INODE_NODUMP)
8840 stat->attributes |= STATX_ATTR_NODUMP;
8841
8842 stat->attributes_mask |= (STATX_ATTR_APPEND |
8843 STATX_ATTR_COMPRESSED |
8844 STATX_ATTR_IMMUTABLE |
8845 STATX_ATTR_NODUMP);
David Sterbafadc0d82011-11-20 07:33:38 -05008846
Chris Mason39279cc2007-06-12 06:35:45 -04008847 generic_fillattr(inode, stat);
Al Viro0ee5dc62011-07-07 15:44:25 -04008848 stat->dev = BTRFS_I(inode)->root->anon_dev;
Miao Xiedf0af1a2013-01-29 10:11:59 +00008849
8850 spin_lock(&BTRFS_I(inode)->lock);
Filipe Mananaa7e3b972017-04-03 10:45:46 +01008851 delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes;
Filipe Manana2766ff62020-11-04 11:07:34 +00008852 inode_bytes = inode_get_bytes(inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00008853 spin_unlock(&BTRFS_I(inode)->lock);
Filipe Manana2766ff62020-11-04 11:07:34 +00008854 stat->blocks = (ALIGN(inode_bytes, blocksize) +
Miao Xiedf0af1a2013-01-29 10:11:59 +00008855 ALIGN(delalloc_bytes, blocksize)) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04008856 return 0;
8857}
8858
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008859static int btrfs_rename_exchange(struct inode *old_dir,
8860 struct dentry *old_dentry,
8861 struct inode *new_dir,
8862 struct dentry *new_dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04008863{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008864 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04008865 struct btrfs_trans_handle *trans;
8866 struct btrfs_root *root = BTRFS_I(old_dir)->root;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04008867 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008868 struct inode *new_inode = new_dentry->d_inode;
8869 struct inode *old_inode = old_dentry->d_inode;
Deepa Dinamani95582b02018-05-08 19:36:02 -07008870 struct timespec64 ctime = current_time(old_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02008871 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
8872 u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008873 u64 old_idx = 0;
8874 u64 new_idx = 0;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008875 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01008876 int ret2;
Filipe Manana86e8aa02016-05-05 02:02:27 +01008877 bool root_log_pinned = false;
8878 bool dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008879
8880 /* we only allow rename subvolume link between subvolumes */
8881 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
8882 return -EXDEV;
8883
8884 /* close the race window with snapshot create/destroy ioctl */
Josef Bacik943eb3b2019-11-19 13:59:20 -05008885 if (old_ino == BTRFS_FIRST_FREE_OBJECTID ||
8886 new_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04008887 down_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008888
8889 /*
8890 * We want to reserve the absolute worst case amount of items. So if
8891 * both inodes are subvols and we need to unlink them then that would
8892 * require 4 item modifications, but if they are both normal inodes it
8893 * would require 5 item modifications, so we'll assume their normal
8894 * inodes. So 5 * 2 is 10, plus 2 for the new links, so 12 total items
8895 * should cover the worst case number of items we'll modify.
8896 */
8897 trans = btrfs_start_transaction(root, 12);
8898 if (IS_ERR(trans)) {
8899 ret = PTR_ERR(trans);
8900 goto out_notrans;
8901 }
8902
Josef Bacik3e174092019-11-15 15:43:06 -05008903 if (dest != root)
8904 btrfs_record_root_in_trans(trans, dest);
8905
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008906 /*
8907 * We need to find a free sequence number both in the source and
8908 * in the destination directory for the exchange.
8909 */
Nikolay Borisov877574e2017-02-20 13:50:33 +02008910 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008911 if (ret)
8912 goto out_fail;
Nikolay Borisov877574e2017-02-20 13:50:33 +02008913 ret = btrfs_set_inode_index(BTRFS_I(old_dir), &new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008914 if (ret)
8915 goto out_fail;
8916
8917 BTRFS_I(old_inode)->dir_index = 0ULL;
8918 BTRFS_I(new_inode)->dir_index = 0ULL;
8919
8920 /* Reference for the source. */
8921 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
8922 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008923 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008924 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008925 btrfs_pin_log_trans(root);
8926 root_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008927 ret = btrfs_insert_inode_ref(trans, dest,
8928 new_dentry->d_name.name,
8929 new_dentry->d_name.len,
8930 old_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008931 btrfs_ino(BTRFS_I(new_dir)),
8932 old_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008933 if (ret)
8934 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008935 }
8936
8937 /* And now for the dest. */
8938 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
8939 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01008940 btrfs_set_log_full_commit(trans);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008941 } else {
Filipe Manana376e5a52016-05-05 02:08:56 +01008942 btrfs_pin_log_trans(dest);
8943 dest_log_pinned = true;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008944 ret = btrfs_insert_inode_ref(trans, root,
8945 old_dentry->d_name.name,
8946 old_dentry->d_name.len,
8947 new_ino,
David Sterbaf85b7372017-01-20 14:54:07 +01008948 btrfs_ino(BTRFS_I(old_dir)),
8949 new_idx);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008950 if (ret)
8951 goto out_fail;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008952 }
8953
8954 /* Update inode version and ctime/mtime. */
8955 inode_inc_iversion(old_dir);
8956 inode_inc_iversion(new_dir);
8957 inode_inc_iversion(old_inode);
8958 inode_inc_iversion(new_inode);
8959 old_dir->i_ctime = old_dir->i_mtime = ctime;
8960 new_dir->i_ctime = new_dir->i_mtime = ctime;
8961 old_inode->i_ctime = ctime;
8962 new_inode->i_ctime = ctime;
8963
8964 if (old_dentry->d_parent != new_dentry->d_parent) {
David Sterbaf85b7372017-01-20 14:54:07 +01008965 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
8966 BTRFS_I(old_inode), 1);
8967 btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
8968 BTRFS_I(new_inode), 1);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008969 }
8970
8971 /* src is a subvolume */
8972 if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008973 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008974 } else { /* src is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008975 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
8976 BTRFS_I(old_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008977 old_dentry->d_name.name,
8978 old_dentry->d_name.len);
8979 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008980 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008981 }
8982 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008983 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008984 goto out_fail;
8985 }
8986
8987 /* dest is a subvolume */
8988 if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
Josef Bacik045d3962019-12-18 17:20:27 -05008989 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008990 } else { /* dest is an inode */
Nikolay Borisov4ec59342017-01-18 00:31:44 +02008991 ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
8992 BTRFS_I(new_dentry->d_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008993 new_dentry->d_name.name,
8994 new_dentry->d_name.len);
8995 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02008996 ret = btrfs_update_inode(trans, dest, BTRFS_I(new_inode));
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01008997 }
8998 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04008999 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009000 goto out_fail;
9001 }
9002
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009003 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009004 new_dentry->d_name.name,
9005 new_dentry->d_name.len, 0, old_idx);
9006 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009007 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009008 goto out_fail;
9009 }
9010
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009011 ret = btrfs_add_link(trans, BTRFS_I(old_dir), BTRFS_I(new_inode),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009012 old_dentry->d_name.name,
9013 old_dentry->d_name.len, 0, new_idx);
9014 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009015 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009016 goto out_fail;
9017 }
9018
9019 if (old_inode->i_nlink == 1)
9020 BTRFS_I(old_inode)->dir_index = old_idx;
9021 if (new_inode->i_nlink == 1)
9022 BTRFS_I(new_inode)->dir_index = new_idx;
9023
Filipe Manana86e8aa02016-05-05 02:02:27 +01009024 if (root_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009025 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9026 new_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009027 btrfs_end_log_trans(root);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009028 root_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009029 }
Filipe Manana86e8aa02016-05-05 02:02:27 +01009030 if (dest_log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009031 btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9032 old_dentry->d_parent);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009033 btrfs_end_log_trans(dest);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009034 dest_log_pinned = false;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009035 }
9036out_fail:
Filipe Manana86e8aa02016-05-05 02:02:27 +01009037 /*
9038 * If we have pinned a log and an error happened, we unpin tasks
9039 * trying to sync the log and force them to fallback to a transaction
9040 * commit if the log currently contains any of the inodes involved in
9041 * this rename operation (to ensure we do not persist a log with an
9042 * inconsistent state for any of these inodes or leading to any
9043 * inconsistencies when replayed). If the transaction was aborted, the
9044 * abortion reason is propagated to userspace when attempting to commit
9045 * the transaction. If the log does not contain any of these inodes, we
9046 * allow the tasks to sync it.
9047 */
9048 if (ret && (root_log_pinned || dest_log_pinned)) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009049 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9050 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9051 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana86e8aa02016-05-05 02:02:27 +01009052 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009053 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009054 btrfs_set_log_full_commit(trans);
Filipe Manana86e8aa02016-05-05 02:02:27 +01009055
9056 if (root_log_pinned) {
9057 btrfs_end_log_trans(root);
9058 root_log_pinned = false;
9059 }
9060 if (dest_log_pinned) {
9061 btrfs_end_log_trans(dest);
9062 dest_log_pinned = false;
9063 }
9064 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009065 ret2 = btrfs_end_transaction(trans);
9066 ret = ret ? ret : ret2;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009067out_notrans:
Josef Bacik943eb3b2019-11-19 13:59:20 -05009068 if (new_ino == BTRFS_FIRST_FREE_OBJECTID ||
9069 old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009070 up_read(&fs_info->subvol_sem);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009071
9072 return ret;
9073}
9074
9075static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9076 struct btrfs_root *root,
9077 struct inode *dir,
9078 struct dentry *dentry)
9079{
9080 int ret;
9081 struct inode *inode;
9082 u64 objectid;
9083 u64 index;
9084
Nikolay Borisov543068a2020-12-07 17:32:33 +02009085 ret = btrfs_get_free_objectid(root, &objectid);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009086 if (ret)
9087 return ret;
9088
9089 inode = btrfs_new_inode(trans, root, dir,
9090 dentry->d_name.name,
9091 dentry->d_name.len,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009092 btrfs_ino(BTRFS_I(dir)),
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009093 objectid,
9094 S_IFCHR | WHITEOUT_MODE,
9095 &index);
9096
9097 if (IS_ERR(inode)) {
9098 ret = PTR_ERR(inode);
9099 return ret;
9100 }
9101
9102 inode->i_op = &btrfs_special_inode_operations;
9103 init_special_inode(inode, inode->i_mode,
9104 WHITEOUT_DEV);
9105
9106 ret = btrfs_init_inode_security(trans, inode, dir,
9107 &dentry->d_name);
9108 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009109 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009110
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009111 ret = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9112 BTRFS_I(inode), 0, index);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009113 if (ret)
Filipe Mananac9901612016-05-05 01:41:57 +01009114 goto out;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009115
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009116 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananac9901612016-05-05 01:41:57 +01009117out:
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009118 unlock_new_inode(inode);
Filipe Mananac9901612016-05-05 01:41:57 +01009119 if (ret)
9120 inode_dec_link_count(inode);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009121 iput(inode);
9122
Filipe Mananac9901612016-05-05 01:41:57 +01009123 return ret;
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009124}
9125
Chris Mason39279cc2007-06-12 06:35:45 -04009126static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009127 struct inode *new_dir, struct dentry *new_dentry,
9128 unsigned int flags)
Chris Mason39279cc2007-06-12 06:35:45 -04009129{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009130 struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009131 struct btrfs_trans_handle *trans;
Filipe Manana5062af32016-05-05 10:26:26 +01009132 unsigned int trans_num_items;
Chris Mason39279cc2007-06-12 06:35:45 -04009133 struct btrfs_root *root = BTRFS_I(old_dir)->root;
9134 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
David Howells2b0143b2015-03-17 22:25:59 +00009135 struct inode *new_inode = d_inode(new_dentry);
9136 struct inode *old_inode = d_inode(old_dentry);
Chris Mason00e4e6b2008-08-05 11:18:09 -04009137 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009138 int ret;
Filipe Manana75b463d2020-08-11 12:43:48 +01009139 int ret2;
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009140 u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009141 bool log_pinned = false;
Chris Mason39279cc2007-06-12 06:35:45 -04009142
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009143 if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
Yan, Zhengf679a842009-09-24 09:17:31 -04009144 return -EPERM;
9145
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009146 /* we only allow rename subvolume link between subvolumes */
Li Zefan33345d012011-04-20 10:31:50 +08009147 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
Chris Mason3394e162008-11-17 20:42:26 -05009148 return -EXDEV;
9149
Li Zefan33345d012011-04-20 10:31:50 +08009150 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009151 (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009152 return -ENOTEMPTY;
9153
Chris Mason39279cc2007-06-12 06:35:45 -04009154 if (S_ISDIR(old_inode->i_mode) && new_inode &&
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009155 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
Chris Mason39279cc2007-06-12 06:35:45 -04009156 return -ENOTEMPTY;
Chris Mason9c520572012-12-17 14:26:57 -05009157
9158
9159 /* check for collisions, even if the name isn't there */
Josef Bacik4871c152013-10-09 12:24:04 -04009160 ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
Chris Mason9c520572012-12-17 14:26:57 -05009161 new_dentry->d_name.name,
9162 new_dentry->d_name.len);
9163
9164 if (ret) {
9165 if (ret == -EEXIST) {
9166 /* we shouldn't get
9167 * eexist without a new_inode */
Dulshani Gunawardhanafae7f212013-10-31 10:30:08 +05309168 if (WARN_ON(!new_inode)) {
Chris Mason9c520572012-12-17 14:26:57 -05009169 return ret;
9170 }
9171 } else {
9172 /* maybe -EOVERFLOW */
9173 return ret;
9174 }
9175 }
9176 ret = 0;
9177
Chris Mason5a3f23d2009-03-31 13:27:11 -04009178 /*
Chris Mason8d875f92014-08-12 10:47:42 -07009179 * we're using rename to replace one file with another. Start IO on it
9180 * now so we don't add too much work to the end of the transaction
Chris Mason5a3f23d2009-03-31 13:27:11 -04009181 */
Chris Mason8d875f92014-08-12 10:47:42 -07009182 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
Chris Mason5a3f23d2009-03-31 13:27:11 -04009183 filemap_flush(old_inode->i_mapping);
9184
Yan, Zheng76dda932009-09-21 16:00:26 -04009185 /* close the racy window with snapshot create/destroy ioctl */
Li Zefan33345d012011-04-20 10:31:50 +08009186 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009187 down_read(&fs_info->subvol_sem);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009188 /*
9189 * We want to reserve the absolute worst case amount of items. So if
9190 * both inodes are subvols and we need to unlink them then that would
9191 * require 4 item modifications, but if they are both normal inodes it
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009192 * would require 5 item modifications, so we'll assume they are normal
Yan, Zhenga22285a2010-05-16 10:48:46 -04009193 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9194 * should cover the worst case number of items we'll modify.
Filipe Manana5062af32016-05-05 10:26:26 +01009195 * If our rename has the whiteout flag, we need more 5 units for the
9196 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9197 * when selinux is enabled).
Yan, Zhenga22285a2010-05-16 10:48:46 -04009198 */
Filipe Manana5062af32016-05-05 10:26:26 +01009199 trans_num_items = 11;
9200 if (flags & RENAME_WHITEOUT)
9201 trans_num_items += 5;
9202 trans = btrfs_start_transaction(root, trans_num_items);
Johann Lombardib44c59a2011-03-31 13:23:47 +00009203 if (IS_ERR(trans)) {
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009204 ret = PTR_ERR(trans);
9205 goto out_notrans;
9206 }
Chris Mason5f39d392007-10-15 16:14:19 -04009207
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009208 if (dest != root)
9209 btrfs_record_root_in_trans(trans, dest);
9210
Nikolay Borisov877574e2017-02-20 13:50:33 +02009211 ret = btrfs_set_inode_index(BTRFS_I(new_dir), &index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009212 if (ret)
9213 goto out_fail;
Chris Mason5a3f23d2009-03-31 13:27:11 -04009214
Miao Xie67de1172013-12-26 13:07:06 +08009215 BTRFS_I(old_inode)->dir_index = 0ULL;
Li Zefan33345d012011-04-20 10:31:50 +08009216 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009217 /* force full log commit if subvolume involved. */
David Sterba90787762019-03-20 13:28:05 +01009218 btrfs_set_log_full_commit(trans);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009219 } else {
Filipe Mananac4aba952016-04-29 13:14:42 +01009220 btrfs_pin_log_trans(root);
9221 log_pinned = true;
Yan, Zhenga5719522009-09-24 09:17:31 -04009222 ret = btrfs_insert_inode_ref(trans, dest,
9223 new_dentry->d_name.name,
9224 new_dentry->d_name.len,
Li Zefan33345d012011-04-20 10:31:50 +08009225 old_ino,
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009226 btrfs_ino(BTRFS_I(new_dir)), index);
Yan, Zhenga5719522009-09-24 09:17:31 -04009227 if (ret)
9228 goto out_fail;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009229 }
Chris Mason5a3f23d2009-03-31 13:27:11 -04009230
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009231 inode_inc_iversion(old_dir);
9232 inode_inc_iversion(new_dir);
9233 inode_inc_iversion(old_inode);
Deepa Dinamani04b285f2016-02-06 23:57:21 -08009234 old_dir->i_ctime = old_dir->i_mtime =
9235 new_dir->i_ctime = new_dir->i_mtime =
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009236 old_inode->i_ctime = current_time(old_dir);
Chris Mason5f39d392007-10-15 16:14:19 -04009237
Chris Mason12fcfd22009-03-24 10:24:20 -04009238 if (old_dentry->d_parent != new_dentry->d_parent)
David Sterbaf85b7372017-01-20 14:54:07 +01009239 btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9240 BTRFS_I(old_inode), 1);
Chris Mason12fcfd22009-03-24 10:24:20 -04009241
Li Zefan33345d012011-04-20 10:31:50 +08009242 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009243 ret = btrfs_unlink_subvol(trans, old_dir, old_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009244 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009245 ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9246 BTRFS_I(d_inode(old_dentry)),
Al Viro92986792011-03-04 17:14:37 +00009247 old_dentry->d_name.name,
9248 old_dentry->d_name.len);
9249 if (!ret)
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009250 ret = btrfs_update_inode(trans, root, BTRFS_I(old_inode));
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009251 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009252 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009253 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009254 goto out_fail;
9255 }
Chris Mason39279cc2007-06-12 06:35:45 -04009256
9257 if (new_inode) {
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009258 inode_inc_iversion(new_inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009259 new_inode->i_ctime = current_time(new_inode);
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009260 if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009261 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
Josef Bacik045d3962019-12-18 17:20:27 -05009262 ret = btrfs_unlink_subvol(trans, new_dir, new_dentry);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009263 BUG_ON(new_inode->i_nlink == 0);
9264 } else {
Nikolay Borisov4ec59342017-01-18 00:31:44 +02009265 ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9266 BTRFS_I(d_inode(new_dentry)),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009267 new_dentry->d_name.name,
9268 new_dentry->d_name.len);
9269 }
Josef Bacik4ef31a42013-08-13 14:10:08 -04009270 if (!ret && new_inode->i_nlink == 0)
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009271 ret = btrfs_orphan_add(trans,
9272 BTRFS_I(d_inode(new_dentry)));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009273 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009274 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009275 goto out_fail;
9276 }
Chris Mason39279cc2007-06-12 06:35:45 -04009277 }
Josef Bacikaec74772008-07-24 12:12:38 -04009278
Nikolay Borisovdb0a6692017-02-20 13:51:08 +02009279 ret = btrfs_add_link(trans, BTRFS_I(new_dir), BTRFS_I(old_inode),
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009280 new_dentry->d_name.name,
Yan, Zhenga5719522009-09-24 09:17:31 -04009281 new_dentry->d_name.len, 0, index);
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
Miao Xie67de1172013-12-26 13:07:06 +08009287 if (old_inode->i_nlink == 1)
9288 BTRFS_I(old_inode)->dir_index = index;
9289
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009290 if (log_pinned) {
Filipe Manana75b463d2020-08-11 12:43:48 +01009291 btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9292 new_dentry->d_parent);
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009293 btrfs_end_log_trans(root);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009294 log_pinned = false;
Yan, Zheng4df27c4d2009-09-21 15:56:00 -04009295 }
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009296
9297 if (flags & RENAME_WHITEOUT) {
9298 ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9299 old_dentry);
9300
9301 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009302 btrfs_abort_transaction(trans, ret);
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009303 goto out_fail;
9304 }
Chris Mason12fcfd22009-03-24 10:24:20 -04009305 }
Chris Mason39279cc2007-06-12 06:35:45 -04009306out_fail:
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009307 /*
9308 * If we have pinned the log and an error happened, we unpin tasks
9309 * trying to sync the log and force them to fallback to a transaction
9310 * commit if the log currently contains any of the inodes involved in
9311 * this rename operation (to ensure we do not persist a log with an
9312 * inconsistent state for any of these inodes or leading to any
9313 * inconsistencies when replayed). If the transaction was aborted, the
9314 * abortion reason is propagated to userspace when attempting to commit
9315 * the transaction. If the log does not contain any of these inodes, we
9316 * allow the tasks to sync it.
9317 */
9318 if (ret && log_pinned) {
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009319 if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9320 btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9321 btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009322 (new_inode &&
Nikolay Borisov0f8939b2017-01-18 00:31:30 +02009323 btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
David Sterba90787762019-03-20 13:28:05 +01009324 btrfs_set_log_full_commit(trans);
Filipe Manana3dc9e8f72016-04-29 11:34:22 +01009325
9326 btrfs_end_log_trans(root);
9327 log_pinned = false;
9328 }
Filipe Manana75b463d2020-08-11 12:43:48 +01009329 ret2 = btrfs_end_transaction(trans);
9330 ret = ret ? ret : ret2;
Johann Lombardib44c59a2011-03-31 13:23:47 +00009331out_notrans:
Li Zefan33345d012011-04-20 10:31:50 +08009332 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009333 up_read(&fs_info->subvol_sem);
Josef Bacik9ed74f22009-09-11 16:12:44 -04009334
Chris Mason39279cc2007-06-12 06:35:45 -04009335 return ret;
9336}
9337
Miklos Szeredi80ace852014-07-23 15:15:32 +02009338static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9339 struct inode *new_dir, struct dentry *new_dentry,
9340 unsigned int flags)
9341{
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009342 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredi80ace852014-07-23 15:15:32 +02009343 return -EINVAL;
9344
Dan Fuhrycdd1fed2016-03-17 15:23:38 +01009345 if (flags & RENAME_EXCHANGE)
9346 return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9347 new_dentry);
9348
9349 return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
Miklos Szeredi80ace852014-07-23 15:15:32 +02009350}
9351
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009352struct btrfs_delalloc_work {
9353 struct inode *inode;
9354 struct completion completion;
9355 struct list_head list;
9356 struct btrfs_work work;
9357};
9358
Miao Xie8ccf6f192012-10-25 09:28:04 +00009359static void btrfs_run_delalloc_work(struct btrfs_work *work)
9360{
9361 struct btrfs_delalloc_work *delalloc_work;
Josef Bacik9f23e282013-10-28 15:03:41 -04009362 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009363
9364 delalloc_work = container_of(work, struct btrfs_delalloc_work,
9365 work);
Josef Bacik9f23e282013-10-28 15:03:41 -04009366 inode = delalloc_work->inode;
David Sterba30424602015-11-27 19:27:11 +01009367 filemap_flush(inode->i_mapping);
9368 if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9369 &BTRFS_I(inode)->runtime_flags))
Josef Bacik9f23e282013-10-28 15:03:41 -04009370 filemap_flush(inode->i_mapping);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009371
Nikolay Borisov076da912018-04-23 10:54:16 +03009372 iput(inode);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009373 complete(&delalloc_work->completion);
9374}
9375
Nikolay Borisov3a2f8c02018-04-24 17:23:59 +03009376static struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode)
Miao Xie8ccf6f192012-10-25 09:28:04 +00009377{
9378 struct btrfs_delalloc_work *work;
9379
David Sterba100d5702015-12-08 14:39:32 +01009380 work = kmalloc(sizeof(*work), GFP_NOFS);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009381 if (!work)
9382 return NULL;
9383
9384 init_completion(&work->completion);
9385 INIT_LIST_HEAD(&work->list);
9386 work->inode = inode;
Omar Sandovala0cac0e2019-09-16 11:30:57 -07009387 btrfs_init_work(&work->work, btrfs_run_delalloc_work, NULL, NULL);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009388
9389 return work;
9390}
9391
Chris Masond352ac62008-09-29 15:18:18 -04009392/*
9393 * some fairly slow code that needs optimization. This walks the list
9394 * of all the inodes with pending delalloc and forces them to disk.
9395 */
Josef Bacike076ab2a2021-01-07 17:08:30 -05009396static int start_delalloc_inodes(struct btrfs_root *root,
9397 struct writeback_control *wbc, bool snapshot,
Filipe Manana3d45f222020-12-02 11:55:58 +00009398 bool in_reclaim_context)
Chris Masonea8c2812008-08-04 23:17:27 -04009399{
Chris Masonea8c2812008-08-04 23:17:27 -04009400 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009401 struct inode *inode;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009402 struct btrfs_delalloc_work *work, *next;
9403 struct list_head works;
Miao Xie1eafa6c2013-01-22 10:49:00 +00009404 struct list_head splice;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009405 int ret = 0;
Josef Bacike076ab2a2021-01-07 17:08:30 -05009406 bool full_flush = wbc->nr_to_write == LONG_MAX;
Chris Masonea8c2812008-08-04 23:17:27 -04009407
Miao Xie8ccf6f192012-10-25 09:28:04 +00009408 INIT_LIST_HEAD(&works);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009409 INIT_LIST_HEAD(&splice);
Miao Xie63607cc2013-01-22 10:50:35 +00009410
Miao Xie573bfb72014-03-06 13:55:03 +08009411 mutex_lock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009412 spin_lock(&root->delalloc_lock);
9413 list_splice_init(&root->delalloc_inodes, &splice);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009414 while (!list_empty(&splice)) {
9415 binode = list_entry(splice.next, struct btrfs_inode,
Chris Masonea8c2812008-08-04 23:17:27 -04009416 delalloc_inodes);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009417
Miao Xieeb73c1b2013-05-15 07:48:22 +00009418 list_move_tail(&binode->delalloc_inodes,
9419 &root->delalloc_inodes);
Filipe Manana3d45f222020-12-02 11:55:58 +00009420
9421 if (in_reclaim_context &&
9422 test_bit(BTRFS_INODE_NO_DELALLOC_FLUSH, &binode->runtime_flags))
9423 continue;
9424
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009425 inode = igrab(&binode->vfs_inode);
Miao Xiedf0af1a2013-01-29 10:11:59 +00009426 if (!inode) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009427 cond_resched_lock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009428 continue;
Miao Xiedf0af1a2013-01-29 10:11:59 +00009429 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009430 spin_unlock(&root->delalloc_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009431
Ethan Lien3cd24c62018-11-01 14:49:03 +08009432 if (snapshot)
9433 set_bit(BTRFS_INODE_SNAPSHOT_FLUSH,
9434 &binode->runtime_flags);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009435 if (full_flush) {
9436 work = btrfs_alloc_delalloc_work(inode);
9437 if (!work) {
9438 iput(inode);
9439 ret = -ENOMEM;
9440 goto out;
9441 }
9442 list_add_tail(&work->list, &works);
9443 btrfs_queue_work(root->fs_info->flush_workers,
9444 &work->work);
9445 } else {
9446 ret = sync_inode(inode, wbc);
9447 if (!ret &&
9448 test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9449 &BTRFS_I(inode)->runtime_flags))
9450 ret = sync_inode(inode, wbc);
9451 btrfs_add_delayed_iput(inode);
9452 if (ret || wbc->nr_to_write <= 0)
Josef Bacikb4912132020-07-21 10:22:12 -04009453 goto out;
9454 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04009455 cond_resched();
Miao Xieeb73c1b2013-05-15 07:48:22 +00009456 spin_lock(&root->delalloc_lock);
Chris Masonea8c2812008-08-04 23:17:27 -04009457 }
Miao Xieeb73c1b2013-05-15 07:48:22 +00009458 spin_unlock(&root->delalloc_lock);
Chris Mason8c8bee12008-09-29 11:19:10 -04009459
Wang Shilonga1ecaab2014-04-02 19:53:32 +08009460out:
Miao Xie1eafa6c2013-01-22 10:49:00 +00009461 list_for_each_entry_safe(work, next, &works, list) {
9462 list_del_init(&work->list);
Nikolay Borisov40012f92018-04-19 10:46:39 +03009463 wait_for_completion(&work->completion);
9464 kfree(work);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009465 }
9466
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009467 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009468 spin_lock(&root->delalloc_lock);
9469 list_splice_tail(&splice, &root->delalloc_inodes);
9470 spin_unlock(&root->delalloc_lock);
9471 }
Miao Xie573bfb72014-03-06 13:55:03 +08009472 mutex_unlock(&root->delalloc_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009473 return ret;
9474}
9475
Ethan Lien3cd24c62018-11-01 14:49:03 +08009476int btrfs_start_delalloc_snapshot(struct btrfs_root *root)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009477{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009478 struct writeback_control wbc = {
9479 .nr_to_write = LONG_MAX,
9480 .sync_mode = WB_SYNC_NONE,
9481 .range_start = 0,
9482 .range_end = LLONG_MAX,
9483 };
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009484 struct btrfs_fs_info *fs_info = root->fs_info;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009485
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009486 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009487 return -EROFS;
9488
Josef Bacike076ab2a2021-01-07 17:08:30 -05009489 return start_delalloc_inodes(root, &wbc, true, false);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009490}
9491
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009492int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, long nr,
Filipe Manana3d45f222020-12-02 11:55:58 +00009493 bool in_reclaim_context)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009494{
Josef Bacike076ab2a2021-01-07 17:08:30 -05009495 struct writeback_control wbc = {
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009496 .nr_to_write = nr,
Josef Bacike076ab2a2021-01-07 17:08:30 -05009497 .sync_mode = WB_SYNC_NONE,
9498 .range_start = 0,
9499 .range_end = LLONG_MAX,
9500 };
Miao Xieeb73c1b2013-05-15 07:48:22 +00009501 struct btrfs_root *root;
9502 struct list_head splice;
9503 int ret;
9504
Wang Shilong2c21b4d2014-01-14 19:42:20 +08009505 if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
Miao Xieeb73c1b2013-05-15 07:48:22 +00009506 return -EROFS;
9507
9508 INIT_LIST_HEAD(&splice);
9509
Miao Xie573bfb72014-03-06 13:55:03 +08009510 mutex_lock(&fs_info->delalloc_root_mutex);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009511 spin_lock(&fs_info->delalloc_root_lock);
9512 list_splice_init(&fs_info->delalloc_roots, &splice);
Nikolay Borisovd7830b72021-01-11 12:58:12 +02009513 while (!list_empty(&splice)) {
Josef Bacike076ab2a2021-01-07 17:08:30 -05009514 /*
9515 * Reset nr_to_write here so we know that we're doing a full
9516 * flush.
9517 */
Nikolay Borisov9db4dc22021-01-11 12:58:11 +02009518 if (nr == LONG_MAX)
Josef Bacike076ab2a2021-01-07 17:08:30 -05009519 wbc.nr_to_write = LONG_MAX;
9520
Miao Xieeb73c1b2013-05-15 07:48:22 +00009521 root = list_first_entry(&splice, struct btrfs_root,
9522 delalloc_root);
Josef Bacik00246522020-01-24 09:33:01 -05009523 root = btrfs_grab_root(root);
Miao Xieeb73c1b2013-05-15 07:48:22 +00009524 BUG_ON(!root);
9525 list_move_tail(&root->delalloc_root,
9526 &fs_info->delalloc_roots);
9527 spin_unlock(&fs_info->delalloc_root_lock);
9528
Josef Bacike076ab2a2021-01-07 17:08:30 -05009529 ret = start_delalloc_inodes(root, &wbc, false, in_reclaim_context);
Josef Bacik00246522020-01-24 09:33:01 -05009530 btrfs_put_root(root);
Josef Bacike076ab2a2021-01-07 17:08:30 -05009531 if (ret < 0 || wbc.nr_to_write <= 0)
Miao Xieeb73c1b2013-05-15 07:48:22 +00009532 goto out;
Miao Xieeb73c1b2013-05-15 07:48:22 +00009533 spin_lock(&fs_info->delalloc_root_lock);
9534 }
9535 spin_unlock(&fs_info->delalloc_root_lock);
9536
Miao Xie6c255e62014-03-06 13:55:01 +08009537 ret = 0;
Miao Xie8ccf6f192012-10-25 09:28:04 +00009538out:
Nikolay Borisov81f1d392018-04-19 10:46:37 +03009539 if (!list_empty(&splice)) {
Miao Xieeb73c1b2013-05-15 07:48:22 +00009540 spin_lock(&fs_info->delalloc_root_lock);
9541 list_splice_tail(&splice, &fs_info->delalloc_roots);
9542 spin_unlock(&fs_info->delalloc_root_lock);
Miao Xie1eafa6c2013-01-22 10:49:00 +00009543 }
Miao Xie573bfb72014-03-06 13:55:03 +08009544 mutex_unlock(&fs_info->delalloc_root_mutex);
Miao Xie8ccf6f192012-10-25 09:28:04 +00009545 return ret;
Chris Masonea8c2812008-08-04 23:17:27 -04009546}
9547
Chris Mason39279cc2007-06-12 06:35:45 -04009548static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
9549 const char *symname)
9550{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009551 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04009552 struct btrfs_trans_handle *trans;
9553 struct btrfs_root *root = BTRFS_I(dir)->root;
9554 struct btrfs_path *path;
9555 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05009556 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009557 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04009558 u64 objectid;
Dulshani Gunawardhana67871252013-10-31 10:33:04 +05309559 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04009560 int name_len;
9561 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04009562 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04009563 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04009564 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04009565
Filipe David Borba Mananaf06becc2013-09-16 09:53:28 +01009566 name_len = strlen(symname);
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009567 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
Chris Mason39279cc2007-06-12 06:35:45 -04009568 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05009569
Josef Bacik9ed74f22009-09-11 16:12:44 -04009570 /*
9571 * 2 items for inode item and ref
9572 * 2 items for dir items
Filipe Manana9269d122015-12-31 18:16:29 +00009573 * 1 item for updating parent inode item
9574 * 1 item for the inline extent item
Josef Bacik9ed74f22009-09-11 16:12:44 -04009575 * 1 item for xattr if selinux is on
9576 */
Filipe Manana9269d122015-12-31 18:16:29 +00009577 trans = btrfs_start_transaction(root, 7);
Yan, Zhenga22285a2010-05-16 10:48:46 -04009578 if (IS_ERR(trans))
9579 return PTR_ERR(trans);
Chris Mason1832a6d2007-12-21 16:27:21 -05009580
Nikolay Borisov543068a2020-12-07 17:32:33 +02009581 err = btrfs_get_free_objectid(root, &objectid);
Li Zefan581bb052011-04-20 10:06:11 +08009582 if (err)
9583 goto out_unlock;
9584
Josef Bacikaec74772008-07-24 12:12:38 -04009585 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
David Sterbaf85b7372017-01-20 14:54:07 +01009586 dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
9587 objectid, S_IFLNK|S_IRWXUGO, &index);
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009588 if (IS_ERR(inode)) {
9589 err = PTR_ERR(inode);
Al Viro32955c52018-05-16 12:20:05 -04009590 inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04009591 goto out_unlock;
Tsutomu Itoh7cf96da2011-04-25 19:43:53 -04009592 }
Chris Mason39279cc2007-06-12 06:35:45 -04009593
Casey Schauflerad19db72011-12-15 10:09:07 -05009594 /*
9595 * If the active LSM wants to access the inode during
9596 * d_instantiate it needs these. Smack checks to see
9597 * if the filesystem supports xattrs by looking at the
9598 * ops vector.
9599 */
9600 inode->i_fop = &btrfs_file_operations;
9601 inode->i_op = &btrfs_file_inode_operations;
Chris Masonb0d5d102014-09-08 13:08:51 -07009602 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masonb0d5d102014-09-08 13:08:51 -07009603
9604 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
9605 if (err)
Al Viro32955c52018-05-16 12:20:05 -04009606 goto out_unlock;
Casey Schauflerad19db72011-12-15 10:09:07 -05009607
Chris Mason39279cc2007-06-12 06:35:45 -04009608 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -07009609 if (!path) {
9610 err = -ENOMEM;
Al Viro32955c52018-05-16 12:20:05 -04009611 goto out_unlock;
Mark Fashehd8926bb2011-07-13 10:38:47 -07009612 }
Nikolay Borisov4a0cc7c2017-01-10 20:35:31 +02009613 key.objectid = btrfs_ino(BTRFS_I(inode));
Chris Mason39279cc2007-06-12 06:35:45 -04009614 key.offset = 0;
David Sterba962a2982014-06-04 18:41:45 +02009615 key.type = BTRFS_EXTENT_DATA_KEY;
Chris Mason39279cc2007-06-12 06:35:45 -04009616 datasize = btrfs_file_extent_calc_inline_size(name_len);
9617 err = btrfs_insert_empty_item(trans, root, path, &key,
9618 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04009619 if (err) {
Julia Lawallb0839162011-05-14 07:10:51 +00009620 btrfs_free_path(path);
Al Viro32955c52018-05-16 12:20:05 -04009621 goto out_unlock;
Chris Mason54aa1f42007-06-22 14:16:25 -04009622 }
Chris Mason5f39d392007-10-15 16:14:19 -04009623 leaf = path->nodes[0];
9624 ei = btrfs_item_ptr(leaf, path->slots[0],
9625 struct btrfs_file_extent_item);
9626 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
9627 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04009628 BTRFS_FILE_EXTENT_INLINE);
Chris Masonc8b97812008-10-29 14:49:59 -04009629 btrfs_set_file_extent_encryption(leaf, ei, 0);
9630 btrfs_set_file_extent_compression(leaf, ei, 0);
9631 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
9632 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
9633
Chris Mason39279cc2007-06-12 06:35:45 -04009634 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04009635 write_extent_buffer(leaf, symname, ptr, name_len);
9636 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04009637 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04009638
Chris Mason39279cc2007-06-12 06:35:45 -04009639 inode->i_op = &btrfs_symlink_inode_operations;
Al Viro21fc61c2015-11-17 01:07:57 -05009640 inode_nohighmem(inode);
Yan Zhengd899e052008-10-30 14:25:28 -04009641 inode_set_bytes(inode, name_len);
Nikolay Borisov6ef06d22017-02-20 13:50:34 +02009642 btrfs_i_size_write(BTRFS_I(inode), name_len);
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009643 err = btrfs_update_inode(trans, root, BTRFS_I(inode));
Filipe Mananad50866d2015-12-31 18:08:24 +00009644 /*
9645 * Last step, add directory indexes for our symlink inode. This is the
9646 * last step to avoid extra cleanup of these indexes if an error happens
9647 * elsewhere above.
9648 */
9649 if (!err)
Nikolay Borisovcef415a2017-02-20 13:51:09 +02009650 err = btrfs_add_nondir(trans, BTRFS_I(dir), dentry,
9651 BTRFS_I(inode), 0, index);
Al Viro32955c52018-05-16 12:20:05 -04009652 if (err)
9653 goto out_unlock;
Chris Masonb0d5d102014-09-08 13:08:51 -07009654
Al Viro1e2e5472018-05-04 08:23:01 -04009655 d_instantiate_new(dentry, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009656
9657out_unlock:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009658 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009659 if (err && inode) {
Chris Mason39279cc2007-06-12 06:35:45 -04009660 inode_dec_link_count(inode);
Al Viro32955c52018-05-16 12:20:05 -04009661 discard_new_inode(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04009662 }
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009663 btrfs_btree_balance_dirty(fs_info);
Chris Mason39279cc2007-06-12 06:35:45 -04009664 return err;
9665}
Chris Mason16432982008-04-10 10:23:21 -04009666
Filipe Manana8fccebf2020-09-08 11:27:20 +01009667static struct btrfs_trans_handle *insert_prealloc_file_extent(
9668 struct btrfs_trans_handle *trans_in,
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009669 struct btrfs_inode *inode,
9670 struct btrfs_key *ins,
Qu Wenruo203f44c52020-06-10 09:04:40 +08009671 u64 file_offset)
9672{
9673 struct btrfs_file_extent_item stack_fi;
Filipe Mananabf385642020-09-08 11:27:22 +01009674 struct btrfs_replace_extent_info extent_info;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009675 struct btrfs_trans_handle *trans = trans_in;
9676 struct btrfs_path *path;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009677 u64 start = ins->objectid;
9678 u64 len = ins->offset;
Qu Wenruo9729f102020-06-10 09:04:41 +08009679 int ret;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009680
9681 memset(&stack_fi, 0, sizeof(stack_fi));
9682
9683 btrfs_set_stack_file_extent_type(&stack_fi, BTRFS_FILE_EXTENT_PREALLOC);
9684 btrfs_set_stack_file_extent_disk_bytenr(&stack_fi, start);
9685 btrfs_set_stack_file_extent_disk_num_bytes(&stack_fi, len);
9686 btrfs_set_stack_file_extent_num_bytes(&stack_fi, len);
9687 btrfs_set_stack_file_extent_ram_bytes(&stack_fi, len);
9688 btrfs_set_stack_file_extent_compression(&stack_fi, BTRFS_COMPRESS_NONE);
9689 /* Encryption and other encoding is reserved and all 0 */
9690
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009691 ret = btrfs_qgroup_release_data(inode, file_offset, len);
Qu Wenruo9729f102020-06-10 09:04:41 +08009692 if (ret < 0)
Filipe Manana8fccebf2020-09-08 11:27:20 +01009693 return ERR_PTR(ret);
9694
9695 if (trans) {
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009696 ret = insert_reserved_file_extent(trans, inode,
Filipe Manana2766ff62020-11-04 11:07:34 +00009697 file_offset, &stack_fi,
9698 true, ret);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009699 if (ret)
9700 return ERR_PTR(ret);
9701 return trans;
9702 }
9703
9704 extent_info.disk_offset = start;
9705 extent_info.disk_len = len;
9706 extent_info.data_offset = 0;
9707 extent_info.data_len = len;
9708 extent_info.file_offset = file_offset;
9709 extent_info.extent_buf = (char *)&stack_fi;
Filipe Manana8fccebf2020-09-08 11:27:20 +01009710 extent_info.is_new_extent = true;
9711 extent_info.qgroup_reserved = ret;
9712 extent_info.insertions = 0;
9713
9714 path = btrfs_alloc_path();
9715 if (!path)
9716 return ERR_PTR(-ENOMEM);
9717
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009718 ret = btrfs_replace_file_extents(&inode->vfs_inode, path, file_offset,
Filipe Manana8fccebf2020-09-08 11:27:20 +01009719 file_offset + len - 1, &extent_info,
9720 &trans);
9721 btrfs_free_path(path);
9722 if (ret)
9723 return ERR_PTR(ret);
9724
9725 return trans;
Qu Wenruo203f44c52020-06-10 09:04:40 +08009726}
Filipe Manana8fccebf2020-09-08 11:27:20 +01009727
Josef Bacik0af3d002010-06-21 14:48:16 -04009728static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
9729 u64 start, u64 num_bytes, u64 min_size,
9730 loff_t actual_len, u64 *alloc_hint,
9731 struct btrfs_trans_handle *trans)
Yan Zhengd899e052008-10-30 14:25:28 -04009732{
Jeff Mahoney0b246af2016-06-22 18:54:23 -04009733 struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009734 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
9735 struct extent_map *em;
Yan Zhengd899e052008-10-30 14:25:28 -04009736 struct btrfs_root *root = BTRFS_I(inode)->root;
9737 struct btrfs_key ins;
Yan Zhengd899e052008-10-30 14:25:28 -04009738 u64 cur_offset = start;
Josef Bacikb778cf92020-02-13 10:47:31 -05009739 u64 clear_offset = start;
Josef Bacik55a61d12010-11-22 18:50:32 +00009740 u64 i_size;
Chris Mason154ea282013-03-05 11:11:26 -05009741 u64 cur_bytes;
Josef Bacik0b670dc2015-09-23 17:11:16 -04009742 u64 last_alloc = (u64)-1;
Yan Zhengd899e052008-10-30 14:25:28 -04009743 int ret = 0;
Josef Bacik0af3d002010-06-21 14:48:16 -04009744 bool own_trans = true;
Wang Xiaoguang18513092016-07-25 15:51:40 +08009745 u64 end = start + num_bytes - 1;
Yan Zhengd899e052008-10-30 14:25:28 -04009746
Josef Bacik0af3d002010-06-21 14:48:16 -04009747 if (trans)
9748 own_trans = false;
Yan Zhengd899e052008-10-30 14:25:28 -04009749 while (num_bytes > 0) {
Byongho Leeee221842015-12-15 01:42:10 +09009750 cur_bytes = min_t(u64, num_bytes, SZ_256M);
Chris Mason154ea282013-03-05 11:11:26 -05009751 cur_bytes = max(cur_bytes, min_size);
Josef Bacik0b670dc2015-09-23 17:11:16 -04009752 /*
9753 * If we are severely fragmented we could end up with really
9754 * small allocations, so if the allocator is returning small
9755 * chunks lets make its job easier by only searching for those
9756 * sized chunks.
9757 */
9758 cur_bytes = min(cur_bytes, last_alloc);
Wang Xiaoguang18513092016-07-25 15:51:40 +08009759 ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
9760 min_size, 0, *alloc_hint, &ins, 1, 0);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009761 if (ret)
Yan, Zhenga22285a2010-05-16 10:48:46 -04009762 break;
Josef Bacikb778cf92020-02-13 10:47:31 -05009763
9764 /*
9765 * We've reserved this space, and thus converted it from
9766 * ->bytes_may_use to ->bytes_reserved. Any error that happens
9767 * from here on out we will only need to clear our reservation
9768 * for the remaining unreserved area, so advance our
9769 * clear_offset by our extent size.
9770 */
9771 clear_offset += ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009772
Josef Bacik0b670dc2015-09-23 17:11:16 -04009773 last_alloc = ins.offset;
Nikolay Borisov90dffd02020-11-02 16:48:54 +02009774 trans = insert_prealloc_file_extent(trans, BTRFS_I(inode),
9775 &ins, cur_offset);
Filipe Manana1afc7082020-10-14 10:10:36 +01009776 /*
9777 * Now that we inserted the prealloc extent we can finally
9778 * decrement the number of reservations in the block group.
9779 * If we did it before, we could race with relocation and have
9780 * relocation miss the reserved extent, making it fail later.
9781 */
9782 btrfs_dec_block_group_reservations(fs_info, ins.objectid);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009783 if (IS_ERR(trans)) {
9784 ret = PTR_ERR(trans);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009785 btrfs_free_reserved_extent(fs_info, ins.objectid,
Miao Xiee570fd22014-06-19 10:42:50 +08009786 ins.offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009787 break;
9788 }
Dongsheng Yang31193212014-12-12 16:44:35 +08009789
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009790 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Chris Masona1ed8352009-09-11 12:27:37 -04009791 cur_offset + ins.offset -1, 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009792
Josef Bacik5dc562c2012-08-17 13:14:17 -04009793 em = alloc_extent_map();
9794 if (!em) {
9795 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
9796 &BTRFS_I(inode)->runtime_flags);
9797 goto next;
9798 }
9799
9800 em->start = cur_offset;
9801 em->orig_start = cur_offset;
9802 em->len = ins.offset;
9803 em->block_start = ins.objectid;
9804 em->block_len = ins.offset;
Josef Bacikb4939682012-12-03 10:31:19 -05009805 em->orig_block_len = ins.offset;
Josef Bacikcc95bef2013-04-04 14:31:27 -04009806 em->ram_bytes = ins.offset;
Josef Bacik5dc562c2012-08-17 13:14:17 -04009807 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
9808 em->generation = trans->transid;
9809
9810 while (1) {
9811 write_lock(&em_tree->lock);
Josef Bacik09a2a8f92013-04-05 16:51:15 -04009812 ret = add_extent_mapping(em_tree, em, 1);
Josef Bacik5dc562c2012-08-17 13:14:17 -04009813 write_unlock(&em_tree->lock);
9814 if (ret != -EEXIST)
9815 break;
Nikolay Borisovdcdbc052017-02-20 13:50:45 +02009816 btrfs_drop_extent_cache(BTRFS_I(inode), cur_offset,
Josef Bacik5dc562c2012-08-17 13:14:17 -04009817 cur_offset + ins.offset - 1,
9818 0);
9819 }
9820 free_extent_map(em);
9821next:
Yan Zhengd899e052008-10-30 14:25:28 -04009822 num_bytes -= ins.offset;
9823 cur_offset += ins.offset;
Yan, Zhengefa56462010-05-16 10:49:59 -04009824 *alloc_hint = ins.objectid + ins.offset;
Yan, Zheng5a303d52009-11-12 09:34:52 +00009825
Josef Bacik0c4d2d92012-04-05 15:03:02 -04009826 inode_inc_iversion(inode);
Deepa Dinamanic2050a42016-09-14 07:48:06 -07009827 inode->i_ctime = current_time(inode);
Christoph Hellwig6cbff002009-04-17 10:37:41 +02009828 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
Yan Zhengd899e052008-10-30 14:25:28 -04009829 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
Yan, Zhengefa56462010-05-16 10:49:59 -04009830 (actual_len > inode->i_size) &&
9831 (cur_offset > inode->i_size)) {
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009832 if (cur_offset > actual_len)
Josef Bacik55a61d12010-11-22 18:50:32 +00009833 i_size = actual_len;
Aneesh Kumar K.Vd1ea6a62010-01-20 07:28:54 +00009834 else
Josef Bacik55a61d12010-11-22 18:50:32 +00009835 i_size = cur_offset;
9836 i_size_write(inode, i_size);
Nikolay Borisov76aea532020-11-02 16:48:53 +02009837 btrfs_inode_safe_disk_i_size_write(BTRFS_I(inode), 0);
Yan, Zheng5a303d52009-11-12 09:34:52 +00009838 }
9839
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009840 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009841
9842 if (ret) {
Jeff Mahoney66642832016-06-10 18:19:25 -04009843 btrfs_abort_transaction(trans, ret);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009844 if (own_trans)
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009845 btrfs_end_transaction(trans);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01009846 break;
9847 }
Yan Zhengd899e052008-10-30 14:25:28 -04009848
Filipe Manana8fccebf2020-09-08 11:27:20 +01009849 if (own_trans) {
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009850 btrfs_end_transaction(trans);
Filipe Manana8fccebf2020-09-08 11:27:20 +01009851 trans = NULL;
9852 }
Yan, Zheng5a303d52009-11-12 09:34:52 +00009853 }
Josef Bacikb778cf92020-02-13 10:47:31 -05009854 if (clear_offset < end)
Nikolay Borisov25ce28c2020-06-03 08:55:39 +03009855 btrfs_free_reserved_data_space(BTRFS_I(inode), NULL, clear_offset,
Josef Bacikb778cf92020-02-13 10:47:31 -05009856 end - clear_offset + 1);
Yan Zhengd899e052008-10-30 14:25:28 -04009857 return ret;
9858}
9859
Josef Bacik0af3d002010-06-21 14:48:16 -04009860int btrfs_prealloc_file_range(struct inode *inode, int mode,
9861 u64 start, u64 num_bytes, u64 min_size,
9862 loff_t actual_len, u64 *alloc_hint)
9863{
9864 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9865 min_size, actual_len, alloc_hint,
9866 NULL);
9867}
9868
9869int btrfs_prealloc_file_range_trans(struct inode *inode,
9870 struct btrfs_trans_handle *trans, int mode,
9871 u64 start, u64 num_bytes, u64 min_size,
9872 loff_t actual_len, u64 *alloc_hint)
9873{
9874 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
9875 min_size, actual_len, alloc_hint, trans);
9876}
9877
Chris Masone6dcd2d2008-07-17 12:53:50 -04009878static int btrfs_set_page_dirty(struct page *page)
9879{
Chris Masone6dcd2d2008-07-17 12:53:50 -04009880 return __set_page_dirty_nobuffers(page);
9881}
9882
Al Viro10556cb22011-06-20 19:28:19 -04009883static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05009884{
Li Zefanb83cc962010-12-20 16:04:08 +08009885 struct btrfs_root *root = BTRFS_I(inode)->root;
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009886 umode_t mode = inode->i_mode;
Li Zefanb83cc962010-12-20 16:04:08 +08009887
Jeff Mahoneycb6db4e2011-08-15 17:27:21 +00009888 if (mask & MAY_WRITE &&
9889 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
9890 if (btrfs_root_readonly(root))
9891 return -EROFS;
9892 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
9893 return -EACCES;
9894 }
Al Viro2830ba72011-06-20 19:16:29 -04009895 return generic_permission(inode, mask);
Yanfdebe2b2008-01-14 13:26:08 -05009896}
Chris Mason39279cc2007-06-12 06:35:45 -04009897
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009898static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
9899{
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009900 struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009901 struct btrfs_trans_handle *trans;
9902 struct btrfs_root *root = BTRFS_I(dir)->root;
9903 struct inode *inode = NULL;
9904 u64 objectid;
9905 u64 index;
9906 int ret = 0;
9907
9908 /*
9909 * 5 units required for adding orphan entry
9910 */
9911 trans = btrfs_start_transaction(root, 5);
9912 if (IS_ERR(trans))
9913 return PTR_ERR(trans);
9914
Nikolay Borisov543068a2020-12-07 17:32:33 +02009915 ret = btrfs_get_free_objectid(root, &objectid);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009916 if (ret)
9917 goto out;
9918
9919 inode = btrfs_new_inode(trans, root, dir, NULL, 0,
David Sterbaf85b7372017-01-20 14:54:07 +01009920 btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009921 if (IS_ERR(inode)) {
9922 ret = PTR_ERR(inode);
9923 inode = NULL;
9924 goto out;
9925 }
9926
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009927 inode->i_fop = &btrfs_file_operations;
9928 inode->i_op = &btrfs_file_inode_operations;
9929
9930 inode->i_mapping->a_ops = &btrfs_aops;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009931
Chris Masonb0d5d102014-09-08 13:08:51 -07009932 ret = btrfs_init_inode_security(trans, inode, dir, NULL);
9933 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009934 goto out;
Chris Masonb0d5d102014-09-08 13:08:51 -07009935
Nikolay Borisov9a56fcd2020-11-02 16:48:59 +02009936 ret = btrfs_update_inode(trans, root, BTRFS_I(inode));
Chris Masonb0d5d102014-09-08 13:08:51 -07009937 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009938 goto out;
Nikolay Borisov73f2e542017-02-20 13:50:59 +02009939 ret = btrfs_orphan_add(trans, BTRFS_I(inode));
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009940 if (ret)
Al Viro32955c52018-05-16 12:20:05 -04009941 goto out;
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009942
Filipe Manana5762b5c2014-08-01 00:10:32 +01009943 /*
9944 * We set number of links to 0 in btrfs_new_inode(), and here we set
9945 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
9946 * through:
9947 *
9948 * d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
9949 */
9950 set_nlink(inode, 1);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009951 d_tmpfile(dentry, inode);
Al Viro32955c52018-05-16 12:20:05 -04009952 unlock_new_inode(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009953 mark_inode_dirty(inode);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009954out:
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04009955 btrfs_end_transaction(trans);
Al Viro32955c52018-05-16 12:20:05 -04009956 if (ret && inode)
9957 discard_new_inode(inode);
Jeff Mahoney2ff7e612016-06-22 18:54:24 -04009958 btrfs_btree_balance_dirty(fs_info);
Filipe Mananaef3b9af2014-04-27 20:40:45 +01009959 return ret;
9960}
9961
David Sterba5cdc84b2018-07-18 20:32:52 +02009962void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end)
Josef Bacikc6100a42017-05-05 11:57:13 -04009963{
David Sterba5cdc84b2018-07-18 20:32:52 +02009964 struct inode *inode = tree->private_data;
Josef Bacikc6100a42017-05-05 11:57:13 -04009965 unsigned long index = start >> PAGE_SHIFT;
9966 unsigned long end_index = end >> PAGE_SHIFT;
9967 struct page *page;
9968
9969 while (index <= end_index) {
9970 page = find_get_page(inode->i_mapping, index);
9971 ASSERT(page); /* Pages should be in the extent_io_tree */
9972 set_page_writeback(page);
9973 put_page(page);
9974 index++;
9975 }
9976}
9977
Omar Sandovaled46ff32016-11-03 10:28:14 -07009978#ifdef CONFIG_SWAP
9979/*
9980 * Add an entry indicating a block group or device which is pinned by a
9981 * swapfile. Returns 0 on success, 1 if there is already an entry for it, or a
9982 * negative errno on failure.
9983 */
9984static int btrfs_add_swapfile_pin(struct inode *inode, void *ptr,
9985 bool is_block_group)
9986{
9987 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
9988 struct btrfs_swapfile_pin *sp, *entry;
9989 struct rb_node **p;
9990 struct rb_node *parent = NULL;
9991
9992 sp = kmalloc(sizeof(*sp), GFP_NOFS);
9993 if (!sp)
9994 return -ENOMEM;
9995 sp->ptr = ptr;
9996 sp->inode = inode;
9997 sp->is_block_group = is_block_group;
9998
9999 spin_lock(&fs_info->swapfile_pins_lock);
10000 p = &fs_info->swapfile_pins.rb_node;
10001 while (*p) {
10002 parent = *p;
10003 entry = rb_entry(parent, struct btrfs_swapfile_pin, node);
10004 if (sp->ptr < entry->ptr ||
10005 (sp->ptr == entry->ptr && sp->inode < entry->inode)) {
10006 p = &(*p)->rb_left;
10007 } else if (sp->ptr > entry->ptr ||
10008 (sp->ptr == entry->ptr && sp->inode > entry->inode)) {
10009 p = &(*p)->rb_right;
10010 } else {
10011 spin_unlock(&fs_info->swapfile_pins_lock);
10012 kfree(sp);
10013 return 1;
10014 }
10015 }
10016 rb_link_node(&sp->node, parent, p);
10017 rb_insert_color(&sp->node, &fs_info->swapfile_pins);
10018 spin_unlock(&fs_info->swapfile_pins_lock);
10019 return 0;
10020}
10021
10022/* Free all of the entries pinned by this swapfile. */
10023static void btrfs_free_swapfile_pins(struct inode *inode)
10024{
10025 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10026 struct btrfs_swapfile_pin *sp;
10027 struct rb_node *node, *next;
10028
10029 spin_lock(&fs_info->swapfile_pins_lock);
10030 node = rb_first(&fs_info->swapfile_pins);
10031 while (node) {
10032 next = rb_next(node);
10033 sp = rb_entry(node, struct btrfs_swapfile_pin, node);
10034 if (sp->inode == inode) {
10035 rb_erase(&sp->node, &fs_info->swapfile_pins);
10036 if (sp->is_block_group)
10037 btrfs_put_block_group(sp->ptr);
10038 kfree(sp);
10039 }
10040 node = next;
10041 }
10042 spin_unlock(&fs_info->swapfile_pins_lock);
10043}
10044
10045struct btrfs_swap_info {
10046 u64 start;
10047 u64 block_start;
10048 u64 block_len;
10049 u64 lowest_ppage;
10050 u64 highest_ppage;
10051 unsigned long nr_pages;
10052 int nr_extents;
10053};
10054
10055static int btrfs_add_swap_extent(struct swap_info_struct *sis,
10056 struct btrfs_swap_info *bsi)
10057{
10058 unsigned long nr_pages;
10059 u64 first_ppage, first_ppage_reported, next_ppage;
10060 int ret;
10061
10062 first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
10063 next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
10064 PAGE_SIZE) >> PAGE_SHIFT;
10065
10066 if (first_ppage >= next_ppage)
10067 return 0;
10068 nr_pages = next_ppage - first_ppage;
10069
10070 first_ppage_reported = first_ppage;
10071 if (bsi->start == 0)
10072 first_ppage_reported++;
10073 if (bsi->lowest_ppage > first_ppage_reported)
10074 bsi->lowest_ppage = first_ppage_reported;
10075 if (bsi->highest_ppage < (next_ppage - 1))
10076 bsi->highest_ppage = next_ppage - 1;
10077
10078 ret = add_swap_extent(sis, bsi->nr_pages, nr_pages, first_ppage);
10079 if (ret < 0)
10080 return ret;
10081 bsi->nr_extents += ret;
10082 bsi->nr_pages += nr_pages;
10083 return 0;
10084}
10085
10086static void btrfs_swap_deactivate(struct file *file)
10087{
10088 struct inode *inode = file_inode(file);
10089
10090 btrfs_free_swapfile_pins(inode);
10091 atomic_dec(&BTRFS_I(inode)->root->nr_swapfiles);
10092}
10093
10094static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10095 sector_t *span)
10096{
10097 struct inode *inode = file_inode(file);
10098 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
10099 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
10100 struct extent_state *cached_state = NULL;
10101 struct extent_map *em = NULL;
10102 struct btrfs_device *device = NULL;
10103 struct btrfs_swap_info bsi = {
10104 .lowest_ppage = (sector_t)-1ULL,
10105 };
10106 int ret = 0;
10107 u64 isize;
10108 u64 start;
10109
10110 /*
10111 * If the swap file was just created, make sure delalloc is done. If the
10112 * file changes again after this, the user is doing something stupid and
10113 * we don't really care.
10114 */
10115 ret = btrfs_wait_ordered_range(inode, 0, (u64)-1);
10116 if (ret)
10117 return ret;
10118
10119 /*
10120 * The inode is locked, so these flags won't change after we check them.
10121 */
10122 if (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS) {
10123 btrfs_warn(fs_info, "swapfile must not be compressed");
10124 return -EINVAL;
10125 }
10126 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)) {
10127 btrfs_warn(fs_info, "swapfile must not be copy-on-write");
10128 return -EINVAL;
10129 }
10130 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
10131 btrfs_warn(fs_info, "swapfile must not be checksummed");
10132 return -EINVAL;
10133 }
10134
10135 /*
10136 * Balance or device remove/replace/resize can move stuff around from
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010137 * under us. The exclop protection makes sure they aren't running/won't
10138 * run concurrently while we are mapping the swap extents, and
10139 * fs_info->swapfile_pins prevents them from running while the swap
10140 * file is active and moving the extents. Note that this also prevents
10141 * a concurrent device add which isn't actually necessary, but it's not
Omar Sandovaled46ff32016-11-03 10:28:14 -070010142 * really worth the trouble to allow it.
10143 */
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010144 if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_SWAP_ACTIVATE)) {
Omar Sandovaled46ff32016-11-03 10:28:14 -070010145 btrfs_warn(fs_info,
10146 "cannot activate swapfile while exclusive operation is running");
10147 return -EBUSY;
10148 }
10149 /*
10150 * Snapshots can create extents which require COW even if NODATACOW is
10151 * set. We use this counter to prevent snapshots. We must increment it
10152 * before walking the extents because we don't want a concurrent
10153 * snapshot to run after we've already checked the extents.
10154 */
10155 atomic_inc(&BTRFS_I(inode)->root->nr_swapfiles);
10156
10157 isize = ALIGN_DOWN(inode->i_size, fs_info->sectorsize);
10158
10159 lock_extent_bits(io_tree, 0, isize - 1, &cached_state);
10160 start = 0;
10161 while (start < isize) {
10162 u64 logical_block_start, physical_block_start;
David Sterba32da53862019-10-29 19:20:18 +010010163 struct btrfs_block_group *bg;
Omar Sandovaled46ff32016-11-03 10:28:14 -070010164 u64 len = isize - start;
10165
Omar Sandoval39b07b52019-12-02 17:34:23 -080010166 em = btrfs_get_extent(BTRFS_I(inode), NULL, 0, start, len);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010167 if (IS_ERR(em)) {
10168 ret = PTR_ERR(em);
10169 goto out;
10170 }
10171
10172 if (em->block_start == EXTENT_MAP_HOLE) {
10173 btrfs_warn(fs_info, "swapfile must not have holes");
10174 ret = -EINVAL;
10175 goto out;
10176 }
10177 if (em->block_start == EXTENT_MAP_INLINE) {
10178 /*
10179 * It's unlikely we'll ever actually find ourselves
10180 * here, as a file small enough to fit inline won't be
10181 * big enough to store more than the swap header, but in
10182 * case something changes in the future, let's catch it
10183 * here rather than later.
10184 */
10185 btrfs_warn(fs_info, "swapfile must not be inline");
10186 ret = -EINVAL;
10187 goto out;
10188 }
10189 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
10190 btrfs_warn(fs_info, "swapfile must not be compressed");
10191 ret = -EINVAL;
10192 goto out;
10193 }
10194
10195 logical_block_start = em->block_start + (start - em->start);
10196 len = min(len, em->len - (start - em->start));
10197 free_extent_map(em);
10198 em = NULL;
10199
Boris Burkova84d5d42020-08-18 11:00:05 -070010200 ret = can_nocow_extent(inode, start, &len, NULL, NULL, NULL, true);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010201 if (ret < 0) {
10202 goto out;
10203 } else if (ret) {
10204 ret = 0;
10205 } else {
10206 btrfs_warn(fs_info,
10207 "swapfile must not be copy-on-write");
10208 ret = -EINVAL;
10209 goto out;
10210 }
10211
10212 em = btrfs_get_chunk_map(fs_info, logical_block_start, len);
10213 if (IS_ERR(em)) {
10214 ret = PTR_ERR(em);
10215 goto out;
10216 }
10217
10218 if (em->map_lookup->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
10219 btrfs_warn(fs_info,
10220 "swapfile must have single data profile");
10221 ret = -EINVAL;
10222 goto out;
10223 }
10224
10225 if (device == NULL) {
10226 device = em->map_lookup->stripes[0].dev;
10227 ret = btrfs_add_swapfile_pin(inode, device, false);
10228 if (ret == 1)
10229 ret = 0;
10230 else if (ret)
10231 goto out;
10232 } else if (device != em->map_lookup->stripes[0].dev) {
10233 btrfs_warn(fs_info, "swapfile must be on one device");
10234 ret = -EINVAL;
10235 goto out;
10236 }
10237
10238 physical_block_start = (em->map_lookup->stripes[0].physical +
10239 (logical_block_start - em->start));
10240 len = min(len, em->len - (logical_block_start - em->start));
10241 free_extent_map(em);
10242 em = NULL;
10243
10244 bg = btrfs_lookup_block_group(fs_info, logical_block_start);
10245 if (!bg) {
10246 btrfs_warn(fs_info,
10247 "could not find block group containing swapfile");
10248 ret = -EINVAL;
10249 goto out;
10250 }
10251
10252 ret = btrfs_add_swapfile_pin(inode, bg, true);
10253 if (ret) {
10254 btrfs_put_block_group(bg);
10255 if (ret == 1)
10256 ret = 0;
10257 else
10258 goto out;
10259 }
10260
10261 if (bsi.block_len &&
10262 bsi.block_start + bsi.block_len == physical_block_start) {
10263 bsi.block_len += len;
10264 } else {
10265 if (bsi.block_len) {
10266 ret = btrfs_add_swap_extent(sis, &bsi);
10267 if (ret)
10268 goto out;
10269 }
10270 bsi.start = start;
10271 bsi.block_start = physical_block_start;
10272 bsi.block_len = len;
10273 }
10274
10275 start += len;
10276 }
10277
10278 if (bsi.block_len)
10279 ret = btrfs_add_swap_extent(sis, &bsi);
10280
10281out:
10282 if (!IS_ERR_OR_NULL(em))
10283 free_extent_map(em);
10284
10285 unlock_extent_cached(io_tree, 0, isize - 1, &cached_state);
10286
10287 if (ret)
10288 btrfs_swap_deactivate(file);
10289
Goldwyn Rodriguesc3e1f962020-08-25 10:02:32 -050010290 btrfs_exclop_finish(fs_info);
Omar Sandovaled46ff32016-11-03 10:28:14 -070010291
10292 if (ret)
10293 return ret;
10294
10295 if (device)
10296 sis->bdev = device->bdev;
10297 *span = bsi.highest_ppage - bsi.lowest_ppage + 1;
10298 sis->max = bsi.nr_pages;
10299 sis->pages = bsi.nr_pages - 1;
10300 sis->highest_bit = bsi.nr_pages - 1;
10301 return bsi.nr_extents;
10302}
10303#else
10304static void btrfs_swap_deactivate(struct file *file)
10305{
10306}
10307
10308static int btrfs_swap_activate(struct swap_info_struct *sis, struct file *file,
10309 sector_t *span)
10310{
10311 return -EOPNOTSUPP;
10312}
10313#endif
10314
Filipe Manana2766ff62020-11-04 11:07:34 +000010315/*
10316 * Update the number of bytes used in the VFS' inode. When we replace extents in
10317 * a range (clone, dedupe, fallocate's zero range), we must update the number of
10318 * bytes used by the inode in an atomic manner, so that concurrent stat(2) calls
10319 * always get a correct value.
10320 */
10321void btrfs_update_inode_bytes(struct btrfs_inode *inode,
10322 const u64 add_bytes,
10323 const u64 del_bytes)
10324{
10325 if (add_bytes == del_bytes)
10326 return;
10327
10328 spin_lock(&inode->lock);
10329 if (del_bytes > 0)
10330 inode_sub_bytes(&inode->vfs_inode, del_bytes);
10331 if (add_bytes > 0)
10332 inode_add_bytes(&inode->vfs_inode, add_bytes);
10333 spin_unlock(&inode->lock);
10334}
10335
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010336static const struct inode_operations btrfs_dir_inode_operations = {
Chris Mason3394e162008-11-17 20:42:26 -050010337 .getattr = btrfs_getattr,
Chris Mason39279cc2007-06-12 06:35:45 -040010338 .lookup = btrfs_lookup,
10339 .create = btrfs_create,
10340 .unlink = btrfs_unlink,
10341 .link = btrfs_link,
10342 .mkdir = btrfs_mkdir,
10343 .rmdir = btrfs_rmdir,
Miklos Szeredi2773bf02016-09-27 11:03:58 +020010344 .rename = btrfs_rename2,
Chris Mason39279cc2007-06-12 06:35:45 -040010345 .symlink = btrfs_symlink,
10346 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -040010347 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -050010348 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010349 .permission = btrfs_permission,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010350 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010351 .set_acl = btrfs_set_acl,
Guangyu Sun93fd63c2013-09-16 10:42:03 -070010352 .update_time = btrfs_update_time,
Filipe Mananaef3b9af2014-04-27 20:40:45 +010010353 .tmpfile = btrfs_tmpfile,
Chris Mason39279cc2007-06-12 06:35:45 -040010354};
Yan, Zheng76dda932009-09-21 16:00:26 -040010355
Alexey Dobriyan828c0952009-10-01 15:43:56 -070010356static const struct file_operations btrfs_dir_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010357 .llseek = generic_file_llseek,
10358 .read = generic_read_dir,
Omar Sandoval02dbfc92016-05-20 13:50:33 -070010359 .iterate_shared = btrfs_real_readdir,
Josef Bacik23b5ec72017-07-24 15:14:25 -040010360 .open = btrfs_opendir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -040010361 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010362#ifdef CONFIG_COMPAT
Luke Dashjr4c63c242015-10-29 08:22:21 +000010363 .compat_ioctl = btrfs_compat_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -040010364#endif
Sage Weil6bf13c02008-06-10 10:07:39 -040010365 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -040010366 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -040010367};
10368
Chris Mason35054392009-01-21 13:11:13 -050010369/*
10370 * btrfs doesn't support the bmap operation because swapfiles
10371 * use bmap to make a mapping of extents in the file. They assume
10372 * these extents won't change over the life of the file and they
10373 * use the bmap result to do IO directly to the drive.
10374 *
10375 * the btrfs bmap call would return logical addresses that aren't
10376 * suitable for IO and they also will change frequently as COW
10377 * operations happen. So, swapfile + btrfs == corruption.
10378 *
10379 * For now we're avoiding this by dropping bmap.
10380 */
Alexey Dobriyan7f094102009-09-21 17:01:10 -070010381static const struct address_space_operations btrfs_aops = {
Chris Mason39279cc2007-06-12 06:35:45 -040010382 .readpage = btrfs_readpage,
10383 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -040010384 .writepages = btrfs_writepages,
Matthew Wilcox (Oracle)ba206a02020-06-01 21:47:05 -070010385 .readahead = btrfs_readahead,
Goldwyn Rodriguesf85781f2020-08-17 11:18:21 -050010386 .direct_IO = noop_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -040010387 .invalidatepage = btrfs_invalidatepage,
10388 .releasepage = btrfs_releasepage,
Roman Gushchinf8e66082020-03-04 16:57:35 -080010389#ifdef CONFIG_MIGRATION
10390 .migratepage = btrfs_migratepage,
10391#endif
Chris Masone6dcd2d2008-07-17 12:53:50 -040010392 .set_page_dirty = btrfs_set_page_dirty,
Andi Kleen465fdd92009-09-16 11:50:18 +020010393 .error_remove_page = generic_error_remove_page,
Omar Sandovaled46ff32016-11-03 10:28:14 -070010394 .swap_activate = btrfs_swap_activate,
10395 .swap_deactivate = btrfs_swap_deactivate,
Chris Mason39279cc2007-06-12 06:35:45 -040010396};
10397
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010398static const struct inode_operations btrfs_file_inode_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -040010399 .getattr = btrfs_getattr,
10400 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -050010401 .listxattr = btrfs_listxattr,
Yanfdebe2b2008-01-14 13:26:08 -050010402 .permission = btrfs_permission,
Yehuda Sadeh1506fcc2009-01-21 14:39:14 -050010403 .fiemap = btrfs_fiemap,
Christoph Hellwig4e34e712011-07-23 17:37:31 +020010404 .get_acl = btrfs_get_acl,
Christoph Hellwig996a7102013-12-20 05:16:43 -080010405 .set_acl = btrfs_set_acl,
Josef Bacike41f9412012-03-26 09:46:47 -040010406 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010407};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010408static const struct inode_operations btrfs_special_inode_operations = {
Josef Bacik618e21d2007-07-11 10:18:17 -040010409 .getattr = btrfs_getattr,
10410 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010411 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -040010412 .listxattr = btrfs_listxattr,
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,
Josef Bacik618e21d2007-07-11 10:18:17 -040010416};
Alexey Dobriyan6e1d5dc2009-09-21 17:01:11 -070010417static const struct inode_operations btrfs_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -050010418 .get_link = page_get_link,
Li Zefanf2095612010-11-19 02:05:24 +000010419 .getattr = btrfs_getattr,
Josef Bacik22c44fe2011-11-30 10:45:38 -050010420 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -050010421 .permission = btrfs_permission,
Jim Owens0279b4c2009-02-04 09:29:13 -050010422 .listxattr = btrfs_listxattr,
Josef Bacike41f9412012-03-26 09:46:47 -040010423 .update_time = btrfs_update_time,
Chris Mason39279cc2007-06-12 06:35:45 -040010424};
Yan, Zheng76dda932009-09-21 16:00:26 -040010425
Alexey Dobriyan82d339d2009-10-09 09:54:36 -040010426const struct dentry_operations btrfs_dentry_operations = {
Yan, Zheng76dda932009-09-21 16:00:26 -040010427 .d_delete = btrfs_dentry_delete,
10428};